from Json(value: JSON Value) - > Result<TOML Value, string>- Module
- std.
toml - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.toml in language mode 5.21.
The current compiler selects std/toml.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· 1808 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 70 - compiler/crates/topaz_resolve/src/std/521/toml.tpz at
b48527d0a174· 5 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1808 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 210 - 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· 1256
Receiver
Parameter constraints
from recursively converts a JSONValue into a TOMLValue and returns Result<TOML. Booleans, strings, arrays and objects retain their values and structure. JSON null has no TOML representation and returns Err, including a nested null.
An integer lexeme must fit signed 64 bit and becomes a TOML integer. An out of range integer returns Err rather than changing into a float. A fractional or exponent lexeme becomes binary64 with nearest ties to even rounding. Overflow to infinity returns Err. Finite rounding and underflow may change the exact decimal value.
A string is kept as a string even when it resembles a date or time. Converting a JSON scalar or array is permitted, but writing it as a complete TOML document still requires a root table.
TOML recursively converts a TOML value. Tables become objects and arrays retain order. Finite numbers follow JSON numeric conversion. Nonfinite numbers return Err rather than becoming a string or null.
TOML dates and times become strings in the normalized TOML spelling used by the writer. This conversion loses their TOML kind. JSON also does not distinguish an integer from an integral float. A reverse conversion therefore need not restore the original TOML kinds. Comments and source number spellings are not preserved.
Both conversions visit array elements by index and object or table keys in the decoded key order. They stop at the first unrepresentable child. The nonempty error identifies its path rooted at $ and its cause. Inputs remain unchanged and neither conversion performs I/O.
Result
The declared result type is Result<TOML.
Effects
Failures
Result and effects
LinkConvert JSON recursively and return an error for null or an unrepresentable number.
Related entries
LinkConvert TOML and JSON with explicit failures
LinkConvert TOML and JSON with explicit failures
from recursively converts a JSONValue into a TOMLValue and returns Result<TOML. Booleans, strings, arrays and objects retain their values and structure. JSON null has no TOML representation and returns Err, including a nested null.
An integer lexeme must fit signed 64 bit and becomes a TOML integer. An out of range integer returns Err rather than changing into a float. A fractional or exponent lexeme becomes binary64 with nearest ties to even rounding. Overflow to infinity returns Err. Finite rounding and underflow may change the exact decimal value.
A string is kept as a string even when it resembles a date or time. Converting a JSON scalar or array is permitted, but writing it as a complete TOML document still requires a root table.
TOML recursively converts a TOML value. Tables become objects and arrays retain order. Finite numbers follow JSON numeric conversion. Nonfinite numbers return Err rather than becoming a string or null.
TOML dates and times become strings in the normalized TOML spelling used by the writer. This conversion loses their TOML kind. JSON also does not distinguish an integer from an integral float. A reverse conversion therefore need not restore the original TOML kinds. Comments and source number spellings are not preserved.
Both conversions visit array elements by index and object or table keys in the decoded key order. They stop at the first unrepresentable child. The nonempty error identifies its path rooted at $ and its cause. Inputs remain unchanged and neither conversion performs I/O.
Why
A format conversion must expose values that its destination cannot represent. A Result keeps nonfinite failure explicit and avoids inventing a second conversion function or silently changing a numeric value into another kind.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1428 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1456 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1534 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 138 -
topaz.stdlib.contract.toml-jsonatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Reject null and out of range integer conversion
Linkimport std.jsonimport std.tomlimport std.testimport std.iofor text in (["null", "9223372036854775808", "1e9999"]) {let value = test.assertOk(json.parse(text))match (toml.fromJson(value)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("unrepresentable")}}
unrepresentable unrepresentable unrepresentable
Expected result
Expected result
Execution completes. Exit code 0.
unrepresentableunrepresentableunrepresentable
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1428 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1456 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1534 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 138 -
topaz.stdlib.contract.toml-jsonatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Identify the first unrepresentable child
Linkimport std.jsonimport std.tomlimport std.testimport std.iolet value = test.assertOk(json.parse("\{\"x\":[null]\}"))let error = test.assertErr(toml.fromJson(value))test.assertContains(error, "$.x[0]")io.writeLine("child located")
child located
Expected result
Expected result
Execution completes. Exit code 0.
child located
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1428 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1456 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1534 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 138 -
topaz.stdlib.contract.toml-jsonatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Convert a TOML table through a recoverable JSON result
Linkimport std.jsonimport std.tomlimport std.testimport std.iolet value = test.assertOk(toml.parse("name = \"Topaz\"\ncount = 6\n"))let tree = test.assertOk(value.toJson())io.writeLine(test.assertOk(json.stringify(tree)))let restored = test.assertOk(toml.fromJson(tree))io.writeLine(test.assertOk(toml.stringify(restored)))
{"count":6,"name":"Topaz"}
count = 6
name = "Topaz"
Expected result
Expected result
Execution completes. Exit code 0.
{"count":6,"name":"Topaz"}count = 6name = "Topaz"
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1428 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1456 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1534 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 138 -
topaz.stdlib.contract.toml-jsonatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Return an error for a TOML infinity during JSON conversion
Linkimport std.tomlimport std.testimport std.iolet value = test.assertOk(toml.parse("value = inf\n"))match (value.toJson()) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("nonfinite rejected")}
nonfinite rejected
Expected result
Expected result
Execution completes. Exit code 0.
nonfinite rejected
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1428 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1456 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1534 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 138 -
topaz.stdlib.contract.toml-jsonatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi