nemoguardrails.colang.v2_x.runtime.serialization

View as Markdown

Utilities for serializing and deserializing state objects to and from JSON.

Module Contents

Functions

NameDescription
decode_from_dictHelper to decode a hierarchy of objects to a dict.
encode_to_dictHelper to encode a hierarchy of objects to a dict.
json_to_stateHelper to decode a State object from a JSON string.
state_to_jsonHelper to encode a State object to a JSON string.

Data

attribute

name_to_class

API

nemoguardrails.colang.v2_x.runtime.serialization.decode_from_dict(
d: typing.Any,
refs: typing.Dict[int, typing.Any]
)

Helper to decode a hierarchy of objects to a dict.

The decoding is able to correctly restore references to the same object, using the markers with the form:

{“__type”: “ref”, “__id”: obj_id}

Parameters:

d
Any

The dictionary that must be decoded.

refs
Dict[int, Any]

An index with all the objects with references that have been decoded so far.

nemoguardrails.colang.v2_x.runtime.serialization.encode_to_dict(
obj: typing.Any,
refs: typing.Dict[int, typing.Any]
)

Helper to encode a hierarchy of objects to a dict.

The encoding is able to mark correctly references to the same object. When an object is encountered a second time, only a reference marker will be added:

{“__type”: “ref”, “__id”: obj_id}

Parameters:

obj
Any

The object that must be encoded.

refs
Dict[int, Any]

An index with all the objects that have been encoded so far.

nemoguardrails.colang.v2_x.runtime.serialization.json_to_state(
s: str
) -> nemoguardrails.colang.v2_x.runtime.flows.State

Helper to decode a State object from a JSON string.

nemoguardrails.colang.v2_x.runtime.serialization.state_to_json(
state: nemoguardrails.colang.v2_x.runtime.flows.State,
indent: bool = False
)

Helper to encode a State object to a JSON string.

TODO: to make the size of the JSON even smaller, we can try to minify it.

Parameters:

state
State

The state that must be encoded.

indent
boolDefaults to False

Whether the JSON should be nicely indented.

nemoguardrails.colang.v2_x.runtime.serialization.attribute = getattr(module, attribute_name)
nemoguardrails.colang.v2_x.runtime.serialization.name_to_class = {}