DataEncoder

class compas.data.DataEncoder[source]

Bases: JSONEncoder

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

The encoder adds the following conversions to the JSON serialisation process:

  • Numpy objects to their Python equivalents;

  • iterables to lists; and

  • compas.data.Data objects, such as geometric primitives and shapes, data structures, robots, …, to a dict with the following structure: {'dtype': o.__dtype__, 'data': o.__data__}

Examples

Explicit use case.

>>> import json
>>> from compas.data import DataEncoder
>>> from compas.geometry import Point
>>> point = Point(0, 0, 0)
>>> with open("point.json", "w") as f:
...     json.dump(point, f, cls=DataEncoder)

Implicit use case.

>>> from compas.data import json_dump
>>> from compas.geometry import Point
>>> point = Point(0, 0, 0)
>>> json_dump(point, "point.json")

Methods

default

Return an object in serialized form.

Inherited Methods

encode

Return a JSON string representation of a Python data structure.

iterencode

Encode the given object and yield each string representation as available.