Home

Awesome

Build Status codecov.io Documentation Status

HGI JSON

A Python 3 library for easily JSON encoding/decoding complex class-based Python models, using an arbitrarily complex (but easy to write!) mapping schema.

Features

Overview

Basic Steps

  1. Define schema for mapping an object to and/or from JSON representation using a list of JsonPropertyMapping definitions.
  2. Use MappingJSONEncoderClassBuilder with the mappings to build a subclass of JSONEncode for serializing instances of a specific type. Similar with decode.
  3. Use created encoder class with Python's in-built json.dumps via the cls parameter. Similar with decoder.

Defining Encoders/Decoders

Encoders and decoders are generated based on a defined JSON <-> Model mapping. A mapping can be written that allows complex classes, such as that below, to be mapped to and from any JSON representation:

class CustomClass(SupportFor, MultipleInheritance):
    self __init__(self, support_for_constructor_parameters):
    self.support_for_all_types_of_properties = ""
    self.including_sets = set()
    self.and_lists = list()
    self.and_dictionaries = dict()
    self.and_complex_properties = ComplexClass()
    self.and_nested_objects_of_the_same_type = CustomClass()
    self.and_properties_not_in_json_if_none = None

    self support_for_setters(self, value):
        """..."""

    self support_for_getters(self):
        """..."""
        
CustomClassJSONEncoder = MappingJSONEncoderClassBuilder(...).build()    # type: JSONEncoder
CustomClassJSONDecoder = MappingJSONDecoderClassBuilder(...).build()    # type: JSONDecoder

custom_class_as_json = json.dumps(custom_class, cls=CustomClassJSONEncoder)     # type: str
custom_class = json.loads("<custom_class_as_json>", cls=CustomClassJSONDecoder)     # type: CustomClass

Documentation

For more details, including information on how to setup and use the library, please view the documentation on ReadTheDocs or read it from /docs.