stringify<T>(value: T) - > Result<string, string>- Module
- std.
json - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.json in language mode 5.21.
The current compiler selects std/json.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1844 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 68 - compiler/crates/topaz_resolve/src/std/521/json.tpz at
b48527d0a174· 4 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1844 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 219 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973 - compiler/crates/topaz_self_frontend/topaz/checker/call_facts.tpz at
b48527d0a174· 676 - compiler/crates/topaz_self_frontend/topaz/checker/call_facts.tpz at
b48527d0a174· 978 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1248
Receiver
Parameter constraints
stringify requires the JSON conformance of its input type. It calls JSON. once and writes the returned JSON tree. A type without that conformance is rejected during checking with TPZ5533. The protocol representation rules define records, enums, arrays, maps, options, results and custom codecs.
Successful output contains one compact JSON value with no leading whitespace or terminal newline. Object names follow the decoded key order. Arrays retain their element order. Map insertion order and record declaration order do not choose serialization order.
The writer escapes quotation marks and backslashes. It uses the short escapes for LF, CR and tab. Other controls below U+0020 use four digit lowercase Unicode escapes. Other scalar values are emitted unchanged. It does not escape a slash or normalize Unicode.
An existing JSONValue retains every accepted number lexeme. An int uses decimal digits and an optional minus. Float encoding follows JSON numeric conversion, including its shortest binary64 roundtrip spelling and negative zero. NaN and infinities return Err.
The depth and active value cycle rules of JSON conversion apply. A depth above 128 or a value cycle returns an error beginning JSON_. Other value failures return the selected codec error. Built in conversion errors identify the first failing child with a path rooted at $.
The input expression is evaluated before conversion. Built in conversions visit arrays in index order and objects in sorted key order. A custom encoder retains its ordinary evaluation and side effects. An encoder error prevents writing. This function returns text and performs no filesystem or stream write.
Related rule: Serialize JSON in a deterministic orderResult
The declared result type is Result<string, string>.
Effects
Failures
Result and effects
LinkEncode through the JSON protocol and return compact deterministic JSON text.
Related entries
LinkSerialize JSON in a deterministic order
LinkSerialize JSON in a deterministic order
stringify requires the JSON conformance of its input type. It calls JSON. once and writes the returned JSON tree. A type without that conformance is rejected during checking with TPZ5533. The protocol representation rules define records, enums, arrays, maps, options, results and custom codecs.
Successful output contains one compact JSON value with no leading whitespace or terminal newline. Object names follow the decoded key order. Arrays retain their element order. Map insertion order and record declaration order do not choose serialization order.
The writer escapes quotation marks and backslashes. It uses the short escapes for LF, CR and tab. Other controls below U+0020 use four digit lowercase Unicode escapes. Other scalar values are emitted unchanged. It does not escape a slash or normalize Unicode.
An existing JSONValue retains every accepted number lexeme. An int uses decimal digits and an optional minus. Float encoding follows JSON numeric conversion, including its shortest binary64 roundtrip spelling and negative zero. NaN and infinities return Err.
The depth and active value cycle rules of JSON conversion apply. A depth above 128 or a value cycle returns an error beginning JSON_. Other value failures return the selected codec error. Built in conversion errors identify the first failing child with a path rooted at $.
The input expression is evaluated before conversion. Built in conversions visit arrays in index order and objects in sorted key order. A custom encoder retains its ordinary evaluation and side effects. An encoder error prevents writing. This function returns text and performs no filesystem or stream write.
Why
Stable JSON bytes support fixtures and reproducible document inputs. The existing JSON conformance supplies one conversion path, while a Result distinguishes invalid values from successful text.
Source evidence
- compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 71 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 204 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 221 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 151 -
topaz.stdlib.contract.json-stringifyatworking-tree - topaz.decision.json-write-order.1 at
topaz.decisi
Write sorted objects and preserve a parsed number lexeme
Linkimport std.jsonimport std.testimport std.ioio.writeLine(test.assertOk(json.stringify(map { "z": 1, "a": 2 })))io.writeLine(test.assertOk(json.stringify(["a,b", "x\"y", "\n", "é"])))let number = test.assertOk(json.parse("1.2300e+03"))io.writeLine(test.assertOk(json.stringify(number)))let empty: Array<int> = []io.writeLine(test.assertOk(json.stringify(empty)))
{"a":2,"z":1}
["a,b","x\"y","\n","é"]
1.2300e+03
[]
Expected result
Expected result
Execution completes. Exit code 0.
{"a":2,"z":1}["a,b","x\"y","\n","é"]1.2300e+03[]
Source evidence
- compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 71 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 204 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 221 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 151 -
topaz.stdlib.contract.json-stringifyatworking-tree - topaz.decision.json-write-order.1 at
topaz.decisi
Write finite float values and reject a nonfinite value
Linkimport std.jsonimport std.iofor value in ([1.5, -0.0]) {match (json.stringify(value)) {case Ok(text) => io.writeLine(text)case Err(_) => io.writeLine("rejected")}}match (json.stringify(0.0 / 0.0)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("nonfinite rejected")}
1.5 -0 nonfinite rejected
Expected result
Expected result
Execution completes. Exit code 0.
1.5-0nonfinite rejected
Source evidence
- compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 71 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 204 - compiler/crates/topaz_value/src/value/json/codec.rs at
912a5e86e1f9· 221 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 151 -
topaz.stdlib.contract.json-stringifyatworking-tree - topaz.decision.json-write-order.1 at
topaz.decisi