from Json(value: JSON Value) - > Result<TOML Value, string>- Модуль
- std.
toml → - Получатель
- Неприменимо
- Минимальное число аргументов
- 1
- Максимальное число аргументов
- 1
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
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.
Получатель
Ограничения параметров
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.
Результат
The declared result type is Result<TOML.
Эффекты
Ошибки
Result and effects
СсылкаConvert JSON recursively and return an error for null or an unrepresentable number.
Related entries
СсылкаConvert TOML and JSON with explicit failures
СсылкаConvert 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.
Причина
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.
Reject null and out of range integer conversion
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
unrepresentableunrepresentableunrepresentable
Identify the first unrepresentable child
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
child located
Convert a TOML table through a recoverable JSON result
Ссылкаimport 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"
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
{"count":6,"name":"Topaz"}count = 6name = "Topaz"
Return an error for a TOML infinity during JSON conversion
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
nonfinite rejected