JSON Encoding and Decoding¶
Encoding¶
Py3oJSONEncoder is a JSON encoder that can interpret Py3o type
instances. It is used in the same way as any other JSON encoder:
data_dict = {
'invoice_date': config.date.strptime('2016-04-20', '%Y-%m-%d'),
'amount': config.float(430.27),
}
data_json = Py3oJSONEncoder().encode(data_dict)
Note that no configuration will be included in the result. The JSON
output is intended to be interpreted in the context of a separate
Py3oTypeConfig object, presumably extracted from a ODF template.
In addition to the basic JSON types and the Py3o types, the parser will also encode some objects from the Python standard library. They will be encoded in the same way as their equivalent Py3o type.
| Standard Class | Py3o Class |
|---|---|
datetime.date |
Py3oDate |
datetime.time |
Py3oTime |
datetime.datetime |
Py3oDatetime |
Decoding¶
Py3oJSONDecoder is a JSON decoder intended to interpret the output
of a Py3oJSONEncoder instance:
decoder = Py3oJSONDecoder(config=config)
data_dict = decoder.decode(data_json)
The config argument will be used to provide the appropriate types when decoding JSON data that corresponds to a Py3o object. In addition, integers and floats are decoded as config.integer and config.float instances respectively.