> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.
> For full documentation content, see https://docs.nvidia.com/nemo/guardrails/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.colang.v2_x.runtime.serialization

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

## Module Contents

### Functions

| Name                                                                                     | Description                                         |
| ---------------------------------------------------------------------------------------- | --------------------------------------------------- |
| [`decode_from_dict`](#nemoguardrails-colang-v2_x-runtime-serialization-decode_from_dict) | Helper to decode a hierarchy of objects to a dict.  |
| [`encode_to_dict`](#nemoguardrails-colang-v2_x-runtime-serialization-encode_to_dict)     | Helper to encode a hierarchy of objects to a dict.  |
| [`json_to_state`](#nemoguardrails-colang-v2_x-runtime-serialization-json_to_state)       | Helper to decode a State object from a JSON string. |
| [`state_to_json`](#nemoguardrails-colang-v2_x-runtime-serialization-state_to_json)       | Helper to encode a State object to a JSON string.   |

### Data

[`attribute`](#nemoguardrails-colang-v2_x-runtime-serialization-attribute)

[`name_to_class`](#nemoguardrails-colang-v2_x-runtime-serialization-name_to_class)

### API

```python
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:**

The dictionary that must be decoded.

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

```python
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:**

The object that must be encoded.

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

```python
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.

```python
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:**

The state that must be encoded.

Whether the JSON should be nicely indented.

```python
nemoguardrails.colang.v2_x.runtime.serialization.attribute = getattr(module, attribute_name)
```

```python
nemoguardrails.colang.v2_x.runtime.serialization.name_to_class = {}
```