parse As<T>(text: string) - > Result<T, 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· 1826 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 68 - compiler/crates/topaz_resolve/src/std/521/json.tpz at
b48527d0a174· 5 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1826 - 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· 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· 1249
Receiver
Parameter constraints
parse requires an explicit JSON decodable result type. The ordinary JSON capability checks apply. Omitting a required result type or selecting a type without decoding support produces TPZ5534.
The function completes JSON parsing before calling JSON.. A malformed document returns an error string containing the parse cause, line and column. It never calls the decoder on a malformed or trailing input.
After parsing succeeds, the selected decoder is called once. Its Ok payload or Err message becomes the result. Custom decoder effects occur after parsing and follow ordinary function evaluation. Built in decoding performs no I/O.
The JSON protocol defines supported representations. Derived records ignore unknown fields and use declared defaults for missing fields. A missing required field, wrong kind, unknown enum tag, wrong payload count or value outside the target numeric range returns Err. No string to number or string to boolean coercion is performed.
Built in errors describe the expected shape and first failing path rooted at $. Arrays are checked in increasing index order and object fields in ascending Unicode scalar order. Built in int decoding requires an exactly integral value within signed 64 bit bounds. Float decoding follows the protocol rounding and overflow rules. Optional null and nested option normalization follow the same protocol rule as JSON.. Use numberText and asInt when the input must retain the integer spelling boundary.
Decode an already parsed tree with JSON.. There is no second JSONValue receiver decoding form. Parsing failure and decoding failure are recoverable Result errors and do not produce assertion diagnostics by themselves.
Result
The declared result type is Result<T, string>.
Effects
Failures
Result and effects
LinkParse a complete JSON document before invoking the requested JSON decoder.
Related entries
LinkParse text before typed JSON decoding
LinkParse text before typed JSON decoding
parse requires an explicit JSON decodable result type. The ordinary JSON capability checks apply. Omitting a required result type or selecting a type without decoding support produces TPZ5534.
The function completes JSON parsing before calling JSON.. A malformed document returns an error string containing the parse cause, line and column. It never calls the decoder on a malformed or trailing input.
After parsing succeeds, the selected decoder is called once. Its Ok payload or Err message becomes the result. Custom decoder effects occur after parsing and follow ordinary function evaluation. Built in decoding performs no I/O.
The JSON protocol defines supported representations. Derived records ignore unknown fields and use declared defaults for missing fields. A missing required field, wrong kind, unknown enum tag, wrong payload count or value outside the target numeric range returns Err. No string to number or string to boolean coercion is performed.
Built in errors describe the expected shape and first failing path rooted at $. Arrays are checked in increasing index order and object fields in ascending Unicode scalar order. Built in int decoding requires an exactly integral value within signed 64 bit bounds. Float decoding follows the protocol rounding and overflow rules. Optional null and nested option normalization follow the same protocol rule as JSON.. Use numberText and asInt when the input must retain the integer spelling boundary.
Decode an already parsed tree with JSON.. There is no second JSONValue receiver decoding form. Parsing failure and decoding failure are recoverable Result errors and do not produce assertion diagnostics by themselves.
Why
Parsing and decoding are separate boundaries. A complete syntax check prevents a user decoder from acting on malformed input, and one protocol dispatch keeps text and tree conversion consistent.
Source evidence
- compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 716 - compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 828 - compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 930 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parse-asatworking-tree - topaz.decision.json-write-order.1 at
topaz.decisi
Decode a record after parsing and report invalid data
Linkimport std.jsonimport std.testimport std.iorecord Item derives JSON { name: string, count: int = 7 }let item = test.assertOk(json.parseAs<Item>("\{\"name\":\"ink\",\"extra\":true\}"))io.writeLine("{item.name},{item.count}")for text in (["\{\}", "17", "[", "\{\"name\":\"ink\",\"count\":9223372036854775808\}"]) {match (json.parseAs<Item>(text)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("invalid data")}}
ink,7 invalid data invalid data invalid data invalid data
Expected result
Expected result
Execution completes. Exit code 0.
ink,7invalid datainvalid datainvalid datainvalid data
Source evidence
- compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 716 - compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 828 - compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 930 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parse-asatworking-tree - topaz.decision.json-write-order.1 at
topaz.decisi
Call a custom decoder once after complete parsing
Linkimport std.json { JSONValue, parseAs }import std.iorecord Token { text: string }impl JSON<Token> {function encode(value: Token) -> Result<JSONValue, string> {JSON.encode(value.text)}function decode(value: JSONValue) -> Result<Token, string> {io.writeLine("decode")match (value.asString()) {case Some(text) => Ok(Token { text: text })case None() => Err("expected token string")}}}match (parseAs<Token>("\"ink\"")) {case Ok(value) => io.writeLine(value.text)case Err(message) => io.writeLine(message)}match (parseAs<Token>("\"ink\" false")) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("parse failed before decode")}
decode ink parse failed before decode
Expected result
Expected result
Execution completes. Exit code 0.
decodeinkparse failed before decode
Source evidence
- compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 716 - compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 828 - compiler/crates/topaz_value/src/value/json/schema.rs at
912a5e86e1f9· 930 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 152 -
topaz.stdlib.contract.json-parse-asatworking-tree - topaz.decision.json-write-order.1 at
topaz.decisi