Saving and loaded of resources is handled by codecs for each format.
JSON data is loaded and saved using the :pyodin.codecs.json_codec
module. This module exposes an API that is very similar to Pythons built in :pyjson
module.
Using the Book and Author resources presented in the creating-resources
section:
# Import the resources we created from our "library" app
>>> from library.resources import Author, Book
# Create an instance of an Author
>>> a = Author(name="Iain M. Banks")
# Create an instance of a Book
>>> b = Book(title="Consider Phlebas", author=a, genre="Space Opera", num_pages=471)
# Dump as a JSON encoded string
from odin.codecs import json_codec
>>> json_codec.dumps(b)
'{"genre": "Space Opera", "title": "Consider Phlebas", "num_pages": 471, "$": "library.resources.Book", "author": {
"name": "Iain M. Banks", "$": "library.resources.Author"}}'
Note
Note the $ entries. The $ symbol is used to keep track of an objects type in the serialised state and to aid deserialization of data.
Similarly data can be deserialized back into an object graph using the :pyodin.codecs.json_codec.loads
method.
Odin includes codecs for many different file formats including:
../ref/codecs/yaml_codec
../ref/codecs/toml_codec
../ref/codecs/msgpack_codec
../ref/codecs/xml_codec
1
Or using each resource as a row:
../ref/codecs/csv_codec
XML is write only↩