number Text() - > Option<string>- Module
- std.
json - Receiver
- JSON
Value - Minimum arity
- 0
- Maximum arity
- 0
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· 1160 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 68 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1160 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 401 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 201
Receiver
JSONValue
Parameter constraints
JSON is an immutable JSON tree with the six JSON kinds. Reading a member preserves the tree.
as and as return Some for the matching kind and None() for every other kind. False and empty text are present values. Neither function parses text or converts a number.
number returns the exact accepted number lexeme for a number and None() for another kind. It preserves a negative zero, exponent spelling and fractional trailing zeroes.
as returns Some for a number spelled as an optional minus and decimal integer digits within the signed 64 bit range. Negative zero becomes zero. A decimal point, exponent, overflow or different kind returns None() without rounding.
as returns the elements of an array in source order. Another kind returns None(). The returned array is a separate collection. Changing that collection does not change the JSON tree.
at uses a zero based array index. A negative index, an index at or beyond the array length, or a receiver of another kind returns None(). get uses an exact decoded object key and returns None() for an absent key or another receiver kind. A present null child is Some containing JSON null.
length is a property containing the array element count or object member count. Scalar kinds yield None(). keys and values are object properties. They contain arrays in ascending Unicode scalar order of decoded keys, with corresponding positions. Array and scalar receivers yield None() for both properties.
The receiver and supplied arguments are evaluated once in ordinary call order. Inspection performs no I/O and invokes no user conversion. Incorrect argument types follow ordinary checking diagnostics rather than becoming absent values.
Related rule: Project JSON values without coercionResult
The declared result type is Option<string>.
Effects
Failures
Result and effects
LinkReturn the exact accepted JSON number lexeme without numeric conversion.
Related entries
LinkProject JSON values without coercion
LinkProject JSON values without coercion
JSON is an immutable JSON tree with the six JSON kinds. Reading a member preserves the tree.
as and as return Some for the matching kind and None() for every other kind. False and empty text are present values. Neither function parses text or converts a number.
number returns the exact accepted number lexeme for a number and None() for another kind. It preserves a negative zero, exponent spelling and fractional trailing zeroes.
as returns Some for a number spelled as an optional minus and decimal integer digits within the signed 64 bit range. Negative zero becomes zero. A decimal point, exponent, overflow or different kind returns None() without rounding.
as returns the elements of an array in source order. Another kind returns None(). The returned array is a separate collection. Changing that collection does not change the JSON tree.
at uses a zero based array index. A negative index, an index at or beyond the array length, or a receiver of another kind returns None(). get uses an exact decoded object key and returns None() for an absent key or another receiver kind. A present null child is Some containing JSON null.
length is a property containing the array element count or object member count. Scalar kinds yield None(). keys and values are object properties. They contain arrays in ascending Unicode scalar order of decoded keys, with corresponding positions. Array and scalar receivers yield None() for both properties.
The receiver and supplied arguments are evaluated once in ordinary call order. Inspection performs no I/O and invokes no user conversion. Incorrect argument types follow ordinary checking diagnostics rather than becoming absent values.
Why
Optional projection keeps a missing child distinct from a stored null. Number text preserves the original representation, while the integer projection has an explicit spelling and range. Sorted key and value positions support deterministic traversal.
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 659 -
topaz.stdlib.contract.json-valueatworking-tree - topaz.decision.json-number-projection.1 at
topaz.decisi
Inspect present values and missing children
Linkimport std.jsonimport std.testimport std.iolet value = test.assertOk(json.parse("[false,\"\",null,7]"))let items = test.assertSome(value.asArray())test.assertEq(test.assertSome(items[0].asBool()), false)test.assertEq(test.assertSome(items[1].asString()), "")test.assertEq(items[2].kind(), "null")test.assertEq(test.assertSome(items[3].asInt()), 7)test.assertNone(value.at(-1))test.assertNone(value.at(4))test.assertNone(value.get("0"))test.assertNone(items[0].asString())test.assertNone(items[1].numberText())test.assertNone(items[2].asArray())let object = test.assertOk(json.parse("\{\"present\":null\}"))test.assertEq(test.assertSome(object.get("present")).kind(), "null")test.assertNone(object.get("missing"))test.assertNone(object.at(0))io.writeLine("present values remain distinct from missing children")
present values remain distinct from missing children
Expected result
Expected result
Execution completes. Exit code 0.
present values remain distinct from missing children
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 659 -
topaz.stdlib.contract.json-valueatworking-tree - topaz.decision.json-number-projection.1 at
topaz.decisi
Keep integer projection within the declared spelling and range
Linkimport std.jsonimport std.testimport std.iofor text in (["-9223372036854775808", "9223372036854775807", "9223372036854775808", "-0", "1.0", "1e0", "1.5"]) {let value = test.assertOk(json.parse(text))match (value.asInt()) {case Some(number) => io.writeLine("{text} = {number}")case None() => io.writeLine("{text} = none")}}
-9223372036854775808 = -9223372036854775808 9223372036854775807 = 9223372036854775807 9223372036854775808 = none -0 = 0 1.0 = none 1e0 = none 1.5 = none
Expected result
Expected result
Execution completes. Exit code 0.
-9223372036854775808 = -92233720368547758089223372036854775807 = 92233720368547758079223372036854775808 = none-0 = 01.0 = none1e0 = none1.5 = none
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 659 -
topaz.stdlib.contract.json-valueatworking-tree - topaz.decision.json-number-projection.1 at
topaz.decisi
Read matching keys and values in scalar order
Linkimport std.jsonimport std.testimport std.iolet object = test.assertOk(json.parse("\{\"z\":1,\"a\":null\}"))let names = test.assertSome(object.keys)let values = test.assertSome(object.values)test.assertEq(test.assertSome(object.length), 2)io.writeLine("{names[0]} = {values[0].kind()}")io.writeLine("{names[1]} = {test.assertSome(values[1].asInt())}")let array = test.assertOk(json.parse("[]"))test.assertEq(test.assertSome(array.length), 0)test.assertNone(array.keys)test.assertNone(array.values)let scalar = test.assertOk(json.parse("\"text\""))test.assertNone(scalar.length)
a = null z = 1
Expected result
Expected result
Execution completes. Exit code 0.
a = nullz = 1
Source evidence
- compiler/crates/topaz_value/src/value/json/parser.rs at
912a5e86e1f9· 20 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 659 -
topaz.stdlib.contract.json-valueatworking-tree - topaz.decision.json-number-projection.1 at
topaz.decisi