~leonardr/lazr.restful/use-bleedthrough

« back to all changes in this revision

Viewing changes to src/lazr/restful/docs/webservice-marshallers.txt

  • Committer: Leonard Richardson
  • Date: 2009-11-10 14:31:44 UTC
  • mfrom: (89.1.3 474522-unmarshall-uri)
  • Revision ID: leonard.richardson@canonical.com-20091110143144-imh084yzquinbhl6
[r=bac] Fixed a bug that raised an unhandled exception when a client tried to set a URL field to a non-string value.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    >>> request.setPublication(WebServiceTestPublication(application))
18
18
    >>> request.processInputs()
19
19
 
20
 
==========================================
21
20
IFieldMarshaller and SimpleFieldMarshaller
22
21
==========================================
23
22
 
63
62
    >>> print marshaller.marshall_from_json_data(None)
64
63
    None
65
64
 
66
 
=======================
67
65
marshall_from_request()
68
66
=======================
69
67
 
107
105
    >>> marshaller.marshall_from_request(['value1', 'value2'])
108
106
    ['value1', 'value2']
109
107
 
110
 
 
111
108
unmarshall() and variants
112
109
=========================
113
110
 
132
129
    'foo'
133
130
 
134
131
 
135
 
================================
136
132
Marshallers for basic data types
137
133
================================
138
134
 
139
135
Bool
140
 
====
 
136
----
141
137
 
142
138
The marshaller for a Bool field checks that the JSON value is either
143
139
True or False. A ValueError is raised when its not the case.
188
184
    ValueError: got 'str', expected bool: 'True'
189
185
 
190
186
Int
191
 
===
 
187
---
192
188
 
193
189
The marshaller for an Int field checks that the JSON value is an
194
190
integer. A ValueError is raised when its not the case.
245
241
    ValueError: got 'unicode', expected int: u'0x04'
246
242
 
247
243
Float
248
 
=====
 
244
-----
249
245
 
250
246
The marshaller for a Float field checks that the JSON value is indeed a
251
247
float.  A ValueError is raised when it's not the case.
290
286
    ValueError: got 'str', expected float, int: 'True'
291
287
 
292
288
Datetime
293
 
========
 
289
--------
294
290
 
295
291
The marshaller for a Datetime field checks that the JSON value is indeed a
296
292
parsable datetime stamp.
346
342
    ValueError: Value doesn't look like a date.
347
343
 
348
344
Date
349
 
====
 
345
----
350
346
 
351
347
The marshaller for a Date field checks that the JSON value is indeed a
352
348
parsable date.
367
363
    datetime.date(2009, 7, 7)
368
364
 
369
365
Text
370
 
====
 
366
----
371
367
 
372
368
The marshaller for IText field checks that the value is a unicode
373
369
string. A ValueError is raised when that's not the case.
412
408
    None
413
409
 
414
410
Bytes
415
 
=====
 
411
-----
416
412
 
417
413
Since there is no way to represent a bytes string in JSON, all strings
418
414
are converted to a byte string using UTF-8 encoding. If the value isn't
480
476
    >>> marshaller.unmarshall(entry_resource, None)
481
477
    'http://.../cookbooks/Everyday%20Greens/data'
482
478
 
483
 
---------
484
479
ASCIILine
485
480
---------
486
481
 
502
497
    >>> marshaller.marshall_from_json_data(u'int\xe9ressant')
503
498
    u'int\xe9ressant'
504
499
 
505
 
Non-string alues like floats are rejected.
 
500
Non-string values like floats are rejected.
506
501
 
507
502
    >>> marshaller.marshall_from_json_data(1.0)
508
503
    Traceback (most recent call last):
542
537
    >>> marshaller.representation_name
543
538
    'field'
544
539
 
545
 
==================
546
540
Choice marshallers
547
541
==================
548
542
 
552
546
    >>> from zope.schema import Choice
553
547
 
554
548
Choice for IVocabularyTokenized
555
 
===============================
 
549
-------------------------------
556
550
 
557
551
The default marshaller will use the vocabulary getTermByToken to
558
552
retrieve the value to use. It raises an error if the value isn't in the
595
589
    [('title', None), ('token', 'a value')]
596
590
    [('title', None), ('token', 'True')]
597
591
 
598
 
 
599
592
Choice of EnumeratedTypes
600
 
=========================
 
593
-------------------------
601
594
 
602
595
The JSON representation of the enumerated value is its title.  A string
603
596
that corresponds to one of the values is marshalled to the appropriate
641
634
    [('title', 'Vegetarian'), ('token', 'VEGETARIAN')]
642
635
 
643
636
 
644
 
=======
645
637
Objects
646
 
=======
 
638
-------
647
639
 
648
640
An object is marshalled to its URL.
649
641
 
667
659
    >>> cookbook.name
668
660
    u'Mastering the Art of French Cooking'
669
661
 
670
 
===========
 
662
    >>> reference_marshaller.marshall_from_json_data("not a url")
 
663
    Traceback (most recent call last):
 
664
    ...
 
665
    ValueError: "not a url" is not a valid URI.
 
666
 
 
667
    >>> reference_marshaller.marshall_from_json_data(4)
 
668
    Traceback (most recent call last):
 
669
    ...
 
670
    ValueError: got 'int', expected string: 4
 
671
 
 
672
    >>> print reference_marshaller.marshall_from_json_data(None)
 
673
    None
 
674
 
671
675
Collections
672
 
===========
 
676
-----------
673
677
 
674
678
The most complicated kind of marshaller is one that manages a
675
679
collection of objects associated with some other object. The generic
787
791
    >>> sorted(set_marshaller.unmarshall(None, marshalled_set))
788
792
    ['Dessert', 'Vegetarian']
789
793
 
790
 
===============
791
794
CollectionField
792
 
===============
 
795
---------------
793
796
 
794
797
Since CollectionField are really a list of references to other
795
798
objects, and they are exposed using a dedicated CollectionResource,