JsonStringify$ ( json[,indent] )
Parameters
|
json - JSON handle to serialize indent (optional) - spaces per nesting level: 0: compact single-line output 1 to 8: pretty-printed with that many spaces; 2 (default) |
Description
|
Serializes a JSON value to deterministic UTF-8 text. Indent 0 produces compact one-line output - good for logs, network messages and hashing. Indents 1 through 8 pretty-print with that many spaces per level - good for humans reading their own save files. The output is deterministic: the same document produces exactly the same bytes on every run, build and machine. Object insertion order is preserved, escapes are fixed, integers print as plain base-10, Doubles use a shortest round-trippable form that keeps a decimal point or exponent (so a JSON_NUMBER never reparses as JSON_INTEGER), lines end with LF, and there is no BOM and no final newline. That determinism is what makes Sha256Text fingerprints of a document meaningful - equal documents hash equal. Feeding the text back through ParseJson reproduces the identical document regardless of indent - whitespace is the only difference. To write the text to disk, prefer SaveJsonAtomic, which serializes and publishes atomically in one step. An indent outside 0 to 8 is a programming error. If the output would exceed the 64 MiB limit, JsonStringify returns an empty string and sets JsonError. For the full rules, see the Application Data language reference. Requires Extended mode. See also: SaveJsonAtomic, ParseJson, JsonError, Sha256Text. |
Example
; JsonStringify Example ; --------------------- ; Requires Extended mode. ; Build a small save-game document by hand save=CreateJsonObject() v=CreateJsonString("Aria") JsonObjectSet save,"player",v FreeJson v v=CreateJsonInteger(1250L) JsonObjectSet save,"gold",v FreeJson v inv=ParseJson("["+Chr(34)+"sword"+Chr(34)+","+Chr(34)+"potion"+Chr(34)+"]") JsonObjectSet save,"inventory",inv FreeJson inv ; Indent 0 gives compact one-line output - good for logs and hashing Print "Compact (indent 0):" Print JsonStringify(save,0) Print "" ; Indent 1..8 pretty-prints with that many spaces - good for humans Print "Pretty (indent 2):" Print JsonStringify(save,2) FreeJson save Print "" Print "Press any key to close the example" WaitKey End
Index