Example usage ============= Parse a json document into a Collection object .. code-block:: python >>> from collection_json import Collection >>> data = '{"collection": {"version": "1.0", "href": "..."}}' >>> collection = Collection.from_json(data) >>> collection Serialize a Collection object into a dictionary >>> from collection_json import Collection >>> data = '{"collection": {"version": "1.0", "href": "..."}}' >>> collection = Collection.from_json(data) >>> expected = {'collection': {'version': '1.0', 'href': '...'}} >>> collection.to_dict() == expected True Serialize a Collection object into a json string >>> import json >>> from collection_json import Collection >>> data = '{"collection": {"version": "1.0", "href": "..."}}' >>> collection = Collection.from_json(data) >>> expected = json.dumps(collection.to_dict()) >>> str(collection) == expected True Inspect collection properties .. code-block:: python >>> from collection_json import Collection >>> data = '{"collection": {"version": "1.0", "href": "..."}}' >>> collection = Collection.from_json(data) >>> collection.version '1.0' >>> collection.href '...' >>> collection.error is None True >>> collection.template is None True >>> collection.items [] >>> collection.links [] >>> collection.queries [] Inspecting an error .. code-block:: python >>> from collection_json import Collection >>> error = {'code': '1234', 'message': 'Something bad happened.', 'title': 'Error 1234'} >>> collection = Collection('...', error=error) >>> collection.error >>> collection.error.code '1234' Inspecting a template .. code-block:: python >>> from collection_json import Collection >>> template = {'data': [ ... {'name': 'first_name', 'value': '', 'prompt': 'First name'}, ... {'name': 'last_name', 'value': '', 'prompt': 'Last name'}]} >>> collection = Collection('...', template=template) >>> collection.template >>> collection.template.first_name Inspecting items in a collection .. code-block:: python >>> from collection_json import Collection >>> data = '{"collection": {"version": "1.0", "href": "...", "items": [ ... {"href": "...", "data": [ ... {"prompt": "First name", "name": "first_name", "value": "John"}, ... {"prompt": "Last name", "name": "last_name", "value": "Doe"}]}, ... {"href": "...", "data": [ ... {"prompt": "First name", "name": "first_name", "value": "Jane"}, ... {"prompt": "Last name", "name": "last_name", "value": "Doe"}]}]}}' >>> collection = Collection.from_json(data) >>> collection >>> collection.items [, ] >>> collection.items[0].properties ['first_name', 'last_name'] >>> collection.items[0].first_name >>> collection.items[0].first_name.value 'John' >>> collection.items[0].data [, ] >>> collection.items[0].data[0].name 'first_name' >>> collection.items[0].data[0].value 'John' Serializing a template from json .. code-block:: python >>> from collection_json import Template >>> data = '{"template": {"data": [ {"name": "first_name", "value": "John"}, {"name": "last_name", "value": "Doe}' >>> template = Template.from_json(data) >>> template.first_name.value 'John' >>> template.last_name.value 'Doe'