DataDecoder

class compas.data.DataDecoder[source]

Bases: JSONDecoder

Data decoder for custom JSON serialization with support for COMPAS data structures and geometric primitives.

The decoder hooks into the JSON deserialisation process to reconstruct compas.data.Data objects, such as geometric primitives and shapes, data structures, robots, …, from the serialized data when possible.

The reconstruction is possible if

  • the serialized data has the following structure: {"dtype": "...", 'data': {...}};

  • a class can be imported into the current scope from the info in o["dtype"]; and

  • the imported class has a method __from_data__.

Examples

Explicit use case.

>>> import json
>>> from compas.data import DataDecoder
>>> with open("point.json", "r") as f:
...     point = json.load(f, cls=DataDecoder)

Implicit use case.

>>> from compas.data import json_load
>>> point = json_load("point.json")

Methods

object_hook

Reconstruct a deserialized object.

Inherited Methods

decode

Return the Python representation of s (a str instance containing a JSON document).

raw_decode

Decode a JSON document from s (a str beginning with a JSON document) and return a 2-tuple of the Python representation and the index in s where the document ended.