Skip to content

JSON Writer

The JSON writer serializes headerkit IR into JSON, useful for inspection, debugging, inter-process communication, or as input to custom code generators.

Writer Class

JsonWriter

JsonWriter(indent=2)

Writer that serializes headerkit IR to JSON.

Options

indent : int | None JSON indentation level. Defaults to 2. None for compact output.

Example

::

from headerkit.writers import get_writer

writer = get_writer("json", indent=4)
json_string = writer.write(header)

name property

name

Human-readable name of this writer.

format_description property

format_description

Short description of the output format.

write

write(header)

Convert header IR to JSON string.

Convenience Functions

header_to_json

header_to_json(header, indent=2)

Convert a Header IR to a JSON string.

Parameters:

Name Type Description Default
header Header

Parsed header IR.

required
indent int | None

JSON indentation level. None for compact output.

2

Returns:

Type Description
str

JSON string representing the header and all declarations.

header_to_json_dict

header_to_json_dict(header)

Convert a Header IR to a JSON-serializable dict (no string encoding).

Parameters:

Name Type Description Default
header Header

Parsed header IR.

required

Returns:

Type Description
dict[str, Any]

Dict representation of the header suitable for json.dumps().