The JSON encoding¶
The same layout tree the .tosc holds, written as JSON. Reading and writing it is a load and a save away -- the extension chooses on the way out and the content decides on the way in -- and these are the functions underneath that, for when the text rather than the file is what you have.
The format itself is described in The .json format.
to_json ¶
Serialize a document to JSON text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document
|
The layout to write. |
required |
indent
|
int | None
|
Spaces per level, or |
2
|
Returns:
| Type | Description |
|---|---|
str
|
The complete document. The indented form ends in a newline, as the |
str
|
pretty XML export does. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layout holds a number JSON cannot represent. Only a non-finite one can be, and those are escaped rather than refused, so this is a guard rather than something a real layout meets. |
Source code in src/py2tosc/json_codec.py
from_json ¶
Parse a layout from JSON text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | bytes
|
The document, as text or UTF-8 bytes. |
required |
Returns:
| Type | Description |
|---|---|
Document
|
The parsed document. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the source is not JSON, is not a py2tosc layout, or holds a node that cannot be read. The message names the node it gave up on. |
SchemaError
|
If it declares a schema this release does not read. |
Source code in src/py2tosc/json_codec.py
supports ¶
Whether this release reads a file declaring that schema.
The question a producer has before it writes one, and the reason to ask it
rather than to build and catch: an answer here names its own remedy, where
a SchemaError several steps into a file is about the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
int
|
The schema number a file declares or would declare. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this release reads it. |
Source code in src/py2tosc/json_codec.py
encode ¶
Write one control and everything under it as JSON-ready data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control
|
Control
|
The control to encode. Its children come with it. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict of nothing but JSON types. |
Source code in src/py2tosc/json_codec.py
decode ¶
Read one control and everything under it.
A node with no id is given one, as Control does. Nothing else is filled
in: a node with no properties decodes to a control with none, rather than
to the default set for its type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Any
|
The decoded JSON for a single node. |
required |
where
|
str
|
What to call this node when something in it will not read. The
default names the root, and children extend it, so a message says
|
'root'
|
Returns:
| Type | Description |
|---|---|
Control
|
The control, with its children. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the node is not an object, names no type or an unknown one, holds a key nothing reads, or holds a property, value or binding that cannot be read. |