Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Describe how complex types are serialized/deserialized. #82

Merged
merged 1 commit into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Contents

introduction
crud
serialization
searching
content-negotiation
http-status-codes
Expand Down
141 changes: 141 additions & 0 deletions docs/source/serialization.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
=============
Serialization
=============

Throughout the REST API, content needs to be serialized and deserialized
to and from JSON representations.

In general, the format used for serializing content when reading from the API
is the same as is used to submit content to the API for writing.

Basic Types
===========

Basic Python data types that have a corresponding type in JSON, like integers
or strings, will simply be translated between the Python type and the
respective JSON type.

Dates and Times
===============

Since JSON doesn't have native support for dates/times, the Python/Zope
datetime types will be serialized to an ISO 8601 datestring.

======================================= ======================================
Python JSON
======================================= ======================================
``time(19, 45, 55)`` ``'19:45:55'``
``date(2015, 11, 23)`` ``'2015-11-23'``
``datetime(2015, 11, 23, 19, 45, 55)`` ``'2015-11-23T19:45:55'``
``DateTime('2015/11/23 19:45:55')`` ``'2015-11-23T19:45:55'``
======================================= ======================================


RichText fields
===============

RichText fields will be serialized as follows:

A ``RichTextValue`` like

.. code:: python

RichTextValue(u'<p>Hallöchen</p>',
mimeType='text/html',
outputMimeType='text/html')

will be serialized to

.. code:: json

{
"data": "<p>Hall\u00f6chen</p>",
"content-type": "text/html",
"encoding": "utf-8"
}

File Fields
===========

Download (serialization)
------------------------

For download, the file field will simply be serialized to a string that
contains the download URL for the file.

.. code:: json

{
"...": "",
"@type": "File",
"title": "My file",
"file": "http://localhost:55001/plone/file/@@download/file"
}

That URL points to the regular Plone
download view.

This means that when accessing that URL, your request won't be handled by
the API but a regular Plone browser view. Therefore you must **not** send
the ``Accept: application/json`` header in this case.

Upload (deserialization)
------------------------

For a field of type ``Named[Blob]File`` (Dexterity) or ``FileField``
(Archetypes), represent the field content as a dictionary with these four keys:

- ``data`` - the base64 encoded contents of the file
- ``encoding`` - the encoding you used to encode the data, so usually `base64`
- ``content-type`` - the MIME type of the file
- ``filename`` - the name of the file, including extension

.. code:: json

{
"...": "",
"@type": "File",
"title": "My file",
"file": {
"data": "TG9yZW0gSXBzdW0uCg==",
"encoding": "base64",
"filename": "lorem.txt",
"content-type": "text/plain"}
}


Relations
=========

Serialization
-------------

A ``RelationValue`` will be serialized to a short summary representation of
the referenced object:

.. code:: json

{
'@id': 'http://nohost/plone/doc1',
'@type': 'DXTestDocument',
'title': 'Document 1',
'description': 'Description'
}

The ``RelationList`` containing that reference will be represended as a list
in JSON.

Deserialization
---------------

In order to set a relation when creating or updating content, you can use one
of several ways to specify relations:

======================================= ======================================
Type Example
======================================= ======================================
UID ``'9b6a4eadb9074dde97d86171bb332ae9'``
IntId ``123456``
Path ``'/plone/doc1'``
URL ``'http://localhost:8080/plone/doc1'``
======================================= ======================================