parse(text: string) - > Result<JSON Value, { column: int, line: int, message: 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· 1836 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 68 - compiler/crates/topaz_resolve/src/std/521/json.tpz at
b48527d0a174· 3 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1836 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 217 - 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· 978 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1247
Receiver
Parameter constraints
parse accepts one complete JSON value with leading and trailing space, tab, CR or LF. An empty input, a byte order mark, comments, trailing commas and trailing nonwhitespace return Err. Scalars are valid roots.
JSON numbers follow the JSON decimal grammar. A leading plus, redundant leading zero, missing fraction or exponent digits, NaN and infinities are invalid. Every accepted lexeme is stored exactly without conversion to binary floating point. Its magnitude need not fit int or float.
Quoted strings decode the JSON escapes and valid UTF 16 surrogate pairs into Unicode scalar values. Unescaped controls, malformed escapes and unpaired surrogates return Err. No Unicode normalization is performed.
Object names are compared after escape decoding. A repeated name returns Err before its value is converted into the object. The second name does not overwrite the first. Arrays preserve source order and object traversal follows the decoded key order.
The root starts at depth zero. Descending into an array element or object value adds one. Depths through 128 are accepted and the first value at depth 129 returns Err with a nesting limit message.
A parse failure returns a record containing a nonempty cause message and one based line and column integers. Columns count Unicode scalar values. CRLF is one line break and a lone CR or LF also starts the next line. The first offending scalar is the location. An invalid escape points to its backslash. A repeated name points to its second opening quote. An incomplete input points immediately after the final scalar.
The text expression is evaluated once before parsing. The parser consumes through trailing whitespace before returning Ok. It performs no I/O, invokes no typed decoder and exposes no partial tree. Malformed data uses the Result error channel rather than a runtime diagnostic.
Result
The declared result type is Result<JSON.
Effects
Failures
Result and effects
LinkParse a complete JSON document without losing number text. Return a structured cause and scalar position for invalid input.
Related entries
LinkParse a complete JSON document
LinkParse a complete JSON document
parse accepts one complete JSON value with leading and trailing space, tab, CR or LF. An empty input, a byte order mark, comments, trailing commas and trailing nonwhitespace return Err. Scalars are valid roots.
JSON numbers follow the JSON decimal grammar. A leading plus, redundant leading zero, missing fraction or exponent digits, NaN and infinities are invalid. Every accepted lexeme is stored exactly without conversion to binary floating point. Its magnitude need not fit int or float.
Quoted strings decode the JSON escapes and valid UTF 16 surrogate pairs into Unicode scalar values. Unescaped controls, malformed escapes and unpaired surrogates return Err. No Unicode normalization is performed.
Object names are compared after escape decoding. A repeated name returns Err before its value is converted into the object. The second name does not overwrite the first. Arrays preserve source order and object traversal follows the decoded key order.
The root starts at depth zero. Descending into an array element or object value adds one. Depths through 128 are accepted and the first value at depth 129 returns Err with a nesting limit message.
A parse failure returns a record containing a nonempty cause message and one based line and column integers. Columns count Unicode scalar values. CRLF is one line break and a lone CR or LF also starts the next line. The first offending scalar is the location. An invalid escape points to its backslash. A repeated name points to its second opening quote. An incomplete input points immediately after the final scalar.
The text expression is evaluated once before parsing. The parser consumes through trailing whitespace before returning Ok. It performs no I/O, invokes no typed decoder and exposes no partial tree. Malformed data uses the Result error channel rather than a runtime diagnostic.
Why
Exact number storage avoids losing document data before a caller chooses a numeric type. Rejecting duplicate names prevents source order from silently selecting a value. A single scalar based location rule makes errors actionable across Unicode text and newline styles.
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 78 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 108 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 289 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parseatworking-tree - topaz.decision.json-parse-boundaries.1 at
topaz.decisi
Retain numbers beyond binary and integer precision
Linkimport std.jsonimport std.testimport std.iofor text in (["9007199254740993", "9223372036854775808", "-0", "1.2300e+03", "1e9999"]) {let value = test.assertOk(json.parse(text))io.writeLine(test.assertSome(value.numberText()))}
9007199254740993 9223372036854775808 -0 1.2300e+03 1e9999
Expected result
Expected result
Execution completes. Exit code 0.
90071992547409939223372036854775808-01.2300e+031e9999
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 78 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 108 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 289 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parseatworking-tree - topaz.decision.json-parse-boundaries.1 at
topaz.decisi
Locate the first invalid scalar and end of input
Linkimport std.jsonimport std.iofor text in (["", "true false", "[1,]", "[1 x]", "[\rtrue,\r?]"]) {match (json.parse(text)) {case Ok(_) => io.writeLine("accepted")case Err(error) => io.writeLine("{error.line},{error.column}")}}
1,1 1,6 1,4 1,4 3,1
Expected result
Expected result
Execution completes. Exit code 0.
1,11,61,41,43,1
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 78 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 108 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 289 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parseatworking-tree - topaz.decision.json-parse-boundaries.1 at
topaz.decisi
Reject a repeated decoded key at its opening quote
Linkimport std.jsonimport std.iomatch (json.parse("\{\"a\":1,\"\\u0061\":2\}")) {case Ok(_) => io.writeLine("accepted")case Err(error) => io.writeLine("{error.line},{error.column}")}
1,8
Expected result
Expected result
Execution completes. Exit code 0.
1,8
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 78 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 108 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 289 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parseatworking-tree - topaz.decision.json-parse-boundaries.1 at
topaz.decisi
Decode surrogate pairs and reject malformed JSON spellings
Linkimport std.jsonimport std.testimport std.iolet value = test.assertOk(json.parse("\"\\uD83D\\uDE00\""))test.assertEq(test.assertSome(value.asString()), "😀")for text in (["01", "+1", "1.", "1e", "NaN", "\"\\uD800\"", "\"\\uDC00\""]) {match (json.parse(text)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("invalid")}}
invalid invalid invalid invalid invalid invalid invalid
Expected result
Expected result
Execution completes. Exit code 0.
invalidinvalidinvalidinvalidinvalidinvalidinvalid
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 78 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 108 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 289 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parseatworking-tree - topaz.decision.json-parse-boundaries.1 at
topaz.decisi
Accept depth 128 and reject the next child
Linkimport std.jsonimport std.iolet mut text = "0"for level in (0 ..< 128) {text = "[" + text + "]"}match (json.parse(text)) {case Ok(_) => io.writeLine("depth 128 accepted")case Err(_) => io.writeLine("depth 128 rejected")}match (json.parse("[" + text + "]")) {case Ok(_) => io.writeLine("depth 129 accepted")case Err(_) => io.writeLine("depth 129 rejected")}
depth 128 accepted depth 129 rejected
Expected result
Expected result
Execution completes. Exit code 0.
depth 128 accepteddepth 129 rejected
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 78 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 108 - compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 289 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parseatworking-tree - topaz.decision.json-parse-boundaries.1 at
topaz.decisi