DataEncoder

class compas.data.DataEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[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

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

Examples

Explicit use case.

>>> import json
>>> import compas
>>> from compas.data import DataEncoder
>>> from compas.geometry import Point
>>> point = Point(0, 0, 0)
>>> with open(compas.get('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, compas.get('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.