pg.to_json

Accessible via pg.to_json, pg.symbolic.to_json.

to_json(value, *, force_dict=False, **kwargs)[source]

Serializes a (maybe) symbolic value into a plain Python object.

Example:

@pg.members([
  ('x', pg.typing.Any())
])
class A(pg.Object):
  pass

a1 = A(1)
json = a1.to_json()
a2 = pg.from_json(json)
assert pg.eq(a1, a2)
Return type:

Any

Parameters:
  • value

    value to serialize. Applicable value types are:

    • Builtin python types: None, bool, int, float, string;

    • JSONConvertible types;

    • List types;

    • Tuple types;

    • Dict types.

  • force_dict – If True, “_type” keys will be renamed to “type_name”. As a result, JSONConvertible objects will be returned as dict.

  • **kwargs – Keyword arguments to pass to value.to_json if value is JSONConvertible.

Returns:

JSON value.