JSON Codec

Codec for serialising and de-serialising JSON data. Supports both array and objects for mapping into resources or collections of resources.

The JSON codec uses the simplejson module if available and falls back to the json module included in the Python standard library.

Methods

odin.codecs.json_codec.load(fp, resource=None, full_clean=True, default_to_not_supplied=False)[source]

Load from a JSON encoded file.

See loads() for more details of the loading operation.

Parameters:
  • fp – a file pointer to read JSON data from.

  • resource – A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied.

  • full_clean – Do a full clean of the object as part of the loading process.

  • default_to_not_supplied – Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED.

Returns:

A resource object or object graph of resources loaded from file.

odin.codecs.json_codec.loads(s, resource=None, full_clean=True, default_to_not_supplied=False)[source]

Load from a JSON encoded string.

If a resource value is supplied it is used as the base resource for the supplied JSON. I one is not supplied a resource type field $ is used to obtain the type represented by the dictionary. A ValidationError will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree.

Parameters:
  • s – String to load and parse.

  • resource – A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied.

  • full_clean – Do a full clean of the object as part of the loading process.

  • default_to_not_supplied – Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED.

Returns:

A resource object or object graph of resources parsed from supplied string.

odin.codecs.json_codec.dump(resource, fp, cls=<class 'odin.codecs.json_codec.OdinEncoder'>, **kwargs)[source]

Dump to a JSON encoded file.

Parameters:
  • resource – The root resource to dump to a JSON encoded file.

  • cls – Encoder to use serializing to a string; default is the OdinEncoder.

  • fp – The file pointer that represents the output file.

odin.codecs.json_codec.dumps(resource, cls=<class 'odin.codecs.json_codec.OdinEncoder'>, **kwargs)[source]

Dump to a JSON encoded string.

Parameters:
  • resource – The root resource to dump to a JSON encoded file.

  • cls – Encoder to use serializing to a string; default is the OdinEncoder.

Returns:

JSON encoded string.

Customising Encoding

Serialisation of Odin resources is handled by a customised json.Encoder. Additional data types can be appended to the odin.codecs.json_codec.JSON_TYPES dictionary.

Example usage

Loading a resource from a file:

from odin.codecs import json_codec

with open('my_resource.json') as f:
    resource = json_codec.load(f)