forked from jakke/z3c.schema2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.txt
561 lines (484 loc) · 14.6 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
.. -*- coding: utf-8 -*-
Schema To JSON
**************
Introduction
============
``z3c.schema2json`` can take objects enriched with schema definitions
into the popular JSON serialisation format. It is also capable of
converting this JSON back into objects.
This package requires ``simplejson`` or Python version 2.6 (which has
simplejson as part of the standard library).
Only object attributes described by zope schema are recognised by
this package. Any attributes that do no have corresponding schema
descriptions will be lost in the serialization/deserialization process.
Installation
============
Add ``z3c.schema2json`` as a dependency to your project.
Don't forget to add ``z3c.schema2json`` to your configure.zcml
<grok:grok package="z3c.schema2json" /> or your adapters won't be registered
to the global adapter registry.
Serialization
=============
Let's first define a simple Zope 3 schema::
>>> from zope import interface, schema
>>> class IName(interface.Interface):
... first_name = schema.TextLine(title=u'First name')
... last_name = schema.TextLine(title=u'Last name')
Let's now create a class that implements this schema::
>>> from zope.interface import implements
>>> class Name(object):
... implements(IName)
... def __init__(self, first_name, last_name):
... self.first_name = first_name
... self.last_name = last_name
Now we make an instance of the class::
>>> name = Name('Paul', 'Tom')
Now let's serialize it to JSON::
>>> from z3c.schema2json import serialize
>>> print serialize(IName, name)
{
"first_name": "Paul",
"last_name": "Tom"
}
We can also serialise other fields too::
>>> from zope import interface, schema
>>> class IAddress(interface.Interface):
... street_name = schema.TextLine(title=u'Street name')
... number = schema.Int(title=u'House Number')
>>> class Address(object):
... implements(IAddress)
... def __init__(self, street_name, number):
... self.street_name = street_name
... self.number = number
>>> address = Address('Pendle Road', 15)
>>> print serialize(IAddress, address)
{
"number": 15,
"street_name": "Pendle Road"
}
A schema can define an ``Object`` field with its own schema, which the
serialisation process can handle::
>>> class IPerson(interface.Interface):
... name = schema.Object(title=u"Name", schema=IName)
... address =schema.Object(title=u"Address", schema=IAddress)
>>> class Person(object):
... implements(IPerson)
... def __init__(self, name, address):
... self.name = name
... self.address = address
>>> person = Person(name, address)
>>> print serialize(IPerson, person)
{
"address": {
"number": 15,
"street_name": "Pendle Road"
},
"name": {
"first_name": "Paul",
"last_name": "Tom"
}
}
The serialisation process also works for the List schema type containing
elements with their own schema::
>>> class ICommission(interface.Interface):
... members = schema.List(
... title = u"Commission",
... value_type=schema.Object(__name__='person',
... schema=IPerson))
Note that we have to explicitly specify __name__ for the field that's
used for value_type here, otherwise we have no name to serialize to
JSON with::
>>> class Commission(object):
... implements(ICommission)
... def __init__(self, members):
... self.members = members
>>> commission = Commission(
... [person, Person(Name('Jim', 'Bo'), Address('Sci Road', 3))])
>>> print serialize(ICommission, commission)
{
"members": [
{
"address": {
"number": 15,
"street_name": "Pendle Road"
},
"name": {
"first_name": "Paul",
"last_name": "Tom"
}
},
{
"address": {
"number": 3,
"street_name": "Sci Road"
},
"name": {
"first_name": "Jim",
"last_name": "Bo"
}
}
]
}
We get an adapter lookup failure whenever we attempt to serialize a field
type which there's no serializer::
>>> class IWithNonSerializableField(interface.Interface):
... field = schema.Field(title=u"Commission")
>>> class NotSerializable(object):
... implements(IWithNonSerializableField)
... def __init__(self, value):
... self.field = value
>>> not_serializable = NotSerializable(None)
>>> serialize(IWithNonSerializableField, not_serializable)
Traceback (most recent call last):
...
TypeError: ('Could not adapt', <zope.schema._bootstrapfields.Field object at ...>, <InterfaceClass z3c.schema2json._schema2json.IJSONGenerator>)
List fields an also contain more primitive values (rather than Objects) such as Ints::
>>> class ILottery(interface.Interface):
... numbers = schema.List(
... title = u"Lottery Numbers",
... value_type = schema.Int(title=u"A Lottery Number",
... min=0, max=49))
>>> class Lottery(object):
... implements(ILottery)
... def __init__(self, numbers):
... self.numbers = numbers
...
>>> lotto = Lottery([10, 17, 20, 21, 23, 31, 32])
>>> print serialize(ILottery, lotto)
{
"numbers": [
10,
17,
20,
21,
23,
31,
32
]
}
Deserialization
===============
Now we would like to take some objects represented as JSON strings and
deserialize them::
>>> from z3c.schema2json import deserialize
>>> json = '''
... {
... "first_name": "Guido",
... "last_name": "Van Rossum"
... }
... '''
... name = Name('', '')
>>> deserialize(json, IName, name)
>>> name.first_name
u'Guido'
>>> name.last_name
u'Van Rossum'
The order of the fields in the JSON string is irrelevant::
>>> json = '''
... {
... "last_name": "Van Rossum",
... "first_name": "Guido"
... }
... '''
... name = Name('', '')
>>> deserialize(json, IName, name)
>>> name.first_name
u'Guido'
>>> name.last_name
u'Van Rossum'
After deserialization, the object ``alsoProvides`` the schema interface::
>>> IName.providedBy(name)
True
This also works for other kinds of fields::
>>> json = '''
... {
... "street_name": "Baker Street",
... "number": 221
... }
... '''
>>> address = Address('',0)
>>> deserialize(json, IAddress, address)
>>> address.street_name
u'Baker Street'
>>> address.number
221
If a schema defines an Object field with its own schema, the serialization
can also handle this::
>>> json = '''
... {
... "name": {
... "first_name": "Sherlock",
... "last_name": "Holmes"
... },
... "address": {
... "street_name": "Baker Street",
... "number": 221
... }
... }
... '''
>>> person = Person(Name('', ''), Address('', 0))
>>> deserialize(json, IPerson, person)
>>> person.name.first_name
u'Sherlock'
>>> person.name.last_name
u'Holmes'
>>> person.address.street_name
u'Baker Street'
>>> person.address.number
221
>>> IPerson.providedBy(person)
True
>>> IName.providedBy(person.name)
True
>>> IAddress.providedBy(person.address)
True
Again the order in which the fields come in JSON shouldn't matter::
>>> json = '''
... {
... "address": {
... "number": 221,
... "street_name": "Baker Street"
... },
... "name": {
... "last_name": "Holmes",
... "first_name": "Sherlock"
... }
... }
... '''
>>> person = Person(Name('', ''), Address('', 0))
>>> deserialize(json, IPerson, person)
>>> person.name.first_name
u'Sherlock'
>>> person.name.last_name
u'Holmes'
>>> person.address.street_name
u'Baker Street'
>>> person.address.number
221
>>> IPerson.providedBy(person)
True
>>> IName.providedBy(person.name)
True
>>> IAddress.providedBy(person.address)
True
We can deserialise List types also::
>>> json = '''
... {
... "members": [
... {
... "name": {
... "first_name": "Melanie",
... "last_name": "Parker"
... },
... "address": {
... "street_name": "Chaigley Court",
... "number": 23
... }
... },
... {
... "name": {
... "first_name": "Rob",
... "last_name": "Hall"
... },
... "address": {
... "street_name": "Queenswood Mount",
... "number": 2
... }
... }
... ]
... }
... '''
>>> commission = Commission([])
>>> deserialize(json, ICommission, commission)
>>> len(commission.members)
2
>>> member = commission.members[0]
>>> member.name.first_name
u'Melanie'
>>> member.address.street_name
u'Chaigley Court'
>>> member = commission.members[1]
>>> member.name.first_name
u'Rob'
>>> member.address.street_name
u'Queenswood Mount'
Whenever an item is null, the resulting value should be None::
>>> json = '''
... {
... "first_name": "",
... "last_name": null
... }
... '''
>>> name = Name('', '')
>>> deserialize(json, IName, name)
>>> name.first_name == ''
True
>>> name.last_name is None
True
For all kinds of fields, like strings and ints...::
>>> json = '''
... {
... "street_name": null,
... "number": null
... }
... '''
>>> address = Address('', 0)
>>> deserialize(json, IAddress, address)
>>> address.street_name is None
True
>>> address.number is None
True
...and the fields of subobjects (but not the subobject themselves!)::
>>> json = '''
... {
... "name": {
... "first_name": null,
... "last_name": null
... },
... "address": {
... "street_name": null,
... "number": null
... }
... }
... '''
>>> person = Person(Name('', ''), Address('', 0))
>>> deserialize(json, IPerson, person)
>>> person.name.first_name is None
True
>>> person.name.last_name is None
True
>>> IPerson.providedBy(person)
True
>>> IName.providedBy(person.name)
True
>>> person.address is None
False
>>> person.address.street_name is None
True
>>> person.address.number is None
True
>>> IAddress.providedBy(person.address)
True
Similarly, empty sequences result in an empty list::
>>> json = '''
... {
... "members" : null
... }
... '''
>>> commission = Commission([])
>>> deserialize(json, ICommission, commission)
>>> len(commission.members)
0
Thus concludes the testing for TextLine, Int Object and List. We will now test other supported field types.
Datetime
========
Datetime objects::
>>> from datetime import datetime
>>> class IWithDatetime(interface.Interface):
... datetime = schema.Datetime(title=u'Date and Time')
>>> class WithDatetime(object):
... implements(IWithDatetime)
... def __init__(self, datetime):
... self.datetime = datetime
>>> with_datetime = WithDatetime(datetime(2008, 12, 31))
>>> json = serialize(IWithDatetime, with_datetime)
>>> print json
{
"datetime": "2008-12-31T00:00:00"
}
>>> new_datetime = WithDatetime(None)
>>> deserialize(json, IWithDatetime, new_datetime)
>>> new_datetime.datetime.year
2008
>>> new_datetime.datetime.month
12
>>> new_datetime.datetime.day
31
Let's try it with the field not filled in::
>>> with_datetime = WithDatetime(None)
>>> json = serialize(IWithDatetime, with_datetime)
>>> print json
{
"datetime": null
}
>>> new_datetime = WithDatetime(None)
>>> deserialize(json, IWithDatetime, new_datetime)
>>> new_datetime.datetime is None
True
Choice
======
Choice fields. Currently, only Choice fields with text values are supported::
>>> from zc.sourcefactory.basic import BasicSourceFactory
>>> class ChoiceSource(BasicSourceFactory):
... def getValues(self):
... return [u'alpha', u'beta']
>>> class IWithChoice(interface.Interface):
... choice = schema.Choice(title=u'Choice', required=False,
... source=ChoiceSource())
>>> class WithChoice(object):
... implements(IWithChoice)
... def __init__(self, choice):
... self.choice = choice
Let's serialize a choice::
>>> with_choice = WithChoice('alpha')
>>> json = serialize(IWithChoice, with_choice)
>>> print json
{
"choice": "alpha"
}
And then deserialize it::
>>> new_choice = WithChoice(None)
>>> deserialize(json, IWithChoice, new_choice)
>>> new_choice.choice
'alpha'
Serializing empty choices::
>>> with_choice = WithChoice(None)
>>> json = serialize(IWithChoice, with_choice)
>>> print json
{
"choice": null
}
And then deserializing them::
>>> deserialize(json, IWithChoice, new_choice)
>>> new_choice.choice is None
True
Set
===
Set fields are very similar to List fields::
>>> class IWithSet(interface.Interface):
... set = schema.Set(title=u'Set', required=False,
... value_type=schema.Choice(__name__='choice',
... source=ChoiceSource()))
>>> class WithSet(object):
... implements(IWithSet)
... def __init__(self, set):
... self.set = set
>>> with_set = WithSet(set(['alpha']))
>>> json = serialize(IWithSet, with_set)
>>> print json
{
"set": [
"alpha"
]
}
>>> with_set = WithSet(set(['alpha', 'beta']))
>>> json = serialize(IWithSet, with_set)
>>> print json
{
"set": [
"alpha",
"beta"
]
}
>>> new_set = WithSet(None)
>>> deserialize(json, IWithSet, new_set)
>>> new_set.set
set(['alpha', 'beta'])
Formatting
==========
The serializer allows both a human readible format and a compact
JSON representation to be produced. By default, the human readible
option is selected, but this may be overridden::
>>> name = Name('Paul', 'Tom')
>>> print serialize(IName, name, pretty_print=False)
{"first_name": "Paul", "last_name": "Tom"}