Skip to main content

Module pyjson

Module pyjson 

Source
Expand description

JSON as CPython 3.11’s json.dumps(value, indent=2) writes it at its other defaults (docs/plans/extract.md E24): ensure_ascii, so every character outside ' '..='~' is a \uXXXX escape (lowercase hex, a character past U+FFFF as its surrogate pair) but \b, \f, \n, \r and \t, which have short forms, as " and \ do; allow_nan, so a NaN is NaN and the infinities Infinity and -Infinity; a float as its repr; the separators , and : that indent implies; an empty list [] and an empty object {}, each on its line.

A value is a typetree value as UnityPy 1.25.2’s read_typetree() gives it (unityrs_core::Value): a dict as an object in its field order, a pair or any list as an array, an f32 widened exactly to the double, a string decoded from its bytes as Python’s surrogateescape decodes them (each byte of an invalid UTF-8 sequence becomes the lone surrogate U+DC00 + byte, written \udcXX). Bytes (TypelessData) are a value json.dumps cannot write (Unwritable).

Options::MONO_BEHAVIOUR selects four spaces and ensure_ascii=False. In this mode valid non-ASCII characters remain UTF-8 (including DEL and U+2028); escaped source bytes are written back verbatim, modeling the final UTF-8 surrogateescape encoding. Only U+DC80..U+DCFF can arise from a raw byte string; Python’s encoder would raise on other lone surrogates, which this value model cannot represent.

Every write goes through a Sink: Count measures a document before anything is allocated for it, so the caller charges the exact size first, then writes into a buffer of that capacity. The value walk keeps an explicit stack, one frame per open container, charged to the caller’s meter before it grows, so no value’s depth costs the thread’s stack.

Structs§

Count
A Sink that only counts.
Options
JSON formatting; MONO_BEHAVIOUR includes the extractor’s final UTF-8 surrogateescape encoding, so malformed source bytes round-trip.
Unwritable
A value json.dumps raises on: bytes (TypelessData), which E25 makes the atlas JSON’s skip, json_unwritable.

Enums§

WriteError
Why a value was not written: a value json.dumps cannot write, or the refused charge of the walk’s stack.

Traits§

Sink
Where the writer puts its bytes.

Functions§

decode_escaped
bytes decoded as Python’s surrogateescape decodes them: each valid UTF-8 character as itself (Ok), each byte of an invalid sequence as one item of its own (Err(byte)), in order.
float
Python’s repr of a double, as json.dumps writes a float.
float_repr
Python’s repr(x) for a float as json.dumps writes it: the core’s py_float_repr (the nearest of the shortest digits that read back, ties to even), but JSON’s NaN, Infinity and -Infinity.
int
A decimal integer.
newline
A newline and level levels of two spaces.
string
A JSON string of bytes, decoded as decode_escaped decodes them and escaped as ensure_ascii escapes (module docs).
value
Writes value, read with parsed’s node lists (which name a dict’s keys), as it sits at level levels of indentation: its first line continues the current one, its nested lines are indented below it. The walk’s stack, a frame per open container, is charged to meter before it grows (review round 2’s S3), and returned when the write ends.
value_with
value with explicit formatting and final UTF-8 surrogateescape encoding.