pg.from_json

Accessible via pg.from_json, pg.symbolic.from_json.

from_json(json_value, *, context=None, auto_symbolic=True, auto_import=True, convert_unknown=False, allow_partial=False, root_path=None, value_spec=None, **kwargs)[source]

Deserializes a (maybe) symbolic value from JSON value.

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:
  • json_value – Input JSON value.

  • context – JSON conversion context.

  • auto_symbolic – If True, list and dict will be automatically converted to pg.List and pg.Dict. Otherwise, they will be plain lists and dicts.

  • auto_import – If True, when a ‘_type’ is not registered, PyGlove will identify its parent module and automatically import it. For example, if the type is ‘foo.bar.A’, PyGlove will try to import ‘foo.bar’ and find the class ‘A’ within the imported module.

  • convert_unknown

    If True, when a ‘_type’ is not registered and cannot be imported, PyGlove will create objects of:

    • pg.symbolic.UnknownType for unknown types;

    • pg.symbolic.UnknownTypedObject for objects of unknown types;

    • pg.symbolic.UnknownFunction for unknown functions;

    • pg.symbolic.UnknownMethod for unknown methods.

    If False, TypeError will be raised.

  • allow_partial – Whether to allow elements of the list to be partial.

  • root_path – KeyPath of loaded object in its object tree.

  • value_spec – The value spec for the symbolic list or dict.

  • **kwargs – Allow passing through keyword arguments to from_json of specific types.

Returns:

Deserialized value, which is * pg.Dict for dict. * pg.List for list. * symbolic.Object for dict with ‘_type’ property. * value itself.