json
Native JSON parsing and encoding with a tagged JSON value.
Provides load, loads, dump, dumps, JSONDecoder, JSONEncoder, and
JSONDecodeError. Standard formatting options and non-finite number defaults
follow Python.
import json
value = json.loads('{"items": [1, true, null]}')
number = value["items"][0].as_int()
value["items"].append("text")
print(json.dumps(value, indent=2))
Differences from Python:
- Decoding returns
JSON. Usekind,is_null(), and typed accessorsas_bool(),as_int(),as_float(),as_str(); wrong kinds raiseTypeError(as_float()also accepts integers).JSON()is null;JSON(value)converts supported native scalars, optionals, lists, tuples, and string-keyed dicts.JSON.array()andJSON.object()create empty containers. - Use
values(),keys(), oritems()instead of direct iteration. Containers support indexing and mutation; copies share storage. Scalars are immutable. Equality is structural and kind-sensitive (JSON(1) != JSON(1.0)); values are unhashable.str(value)serializes JSON, including quotes around strings. - Integers are signed 64-bit; oversized literals raise
OverflowError. Fractional and exponent-form numbers use binary64. - Keys must be strings.
skipkeys,cls,default,object_hook,object_pairs_hook,parse_int,parse_float, andparse_constantare unsupported; unsupported native types, including nested members, are compile-time errors. - Parsing/encoding use
max_depth=512, raisingRecursionErrorwhen exceeded, even withcheck_circular=False. Structural comparison also has a 512 limit. - I/O is not streaming:
load()reads the whole stream;dump()writes a complete string to a text stream.iterencode()is not provided. - Decode-error locations use Unicode code points; trailing-comma diagnostics follow CPython 3.14.
class JSON
Two-word value with a packed tag and string metadata, plus scalar bits or a pointer.
Constructors
2
Properties
1
Methods
17
Special methods
13
class JSONDecodeError
Fields
5
msg
str
doc
str
pos
int
lineno
int
colno
int
Constructors
1
class JSONDecoder
Fields
2
strict
bool
max_depth
int
Constructors
1
Methods
2
loads(source, strict: bool = True, max_depth: int = 512)
load(fp, strict: bool = True, max_depth: int = 512)
class JSONEncoder
Fields
8
ensure_ascii
bool
allow_nan
bool
check_circular
bool
sort_keys
bool
pretty
bool
indent
str
separators
Tuple[str, str]
max_depth
int
Constructors
1
Methods
1
dumps(value, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent = None, separators: Optional[Tuple[str, str]] = None, sort_keys: bool = False, max_depth: int = 512)
dump(value, fp, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent = None, separators: Optional[Tuple[str, str]] = None, sort_keys: bool = False, max_depth: int = 512)