as Bool() - > Option<bool>- 수신자
- JSON
Value - 최소 인자 수
- 0
- 최대 인자 수
- 0
Declaration and parameters
링크사용 조건
기본 포함 · 미정
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.
수신자
JSONValue
매개변수 제약
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.
관련 규칙 · Project JSON values without coercion →결과
The declared result type is Option<bool>.
효과
실패
Result and effects
링크Return the boolean payload for a JSON boolean and None for another kind.
Related entries
링크Project JSON values without coercion
링크Project 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.
이유
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.
Inspect present values and missing children
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
present values remain distinct from missing children
Keep integer projection within the declared spelling and range
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
-9223372036854775808 = -92233720368547758089223372036854775807 = 92233720368547758079223372036854775808 = none-0 = 01.0 = none1e0 = none1.5 = none
Read matching keys and values in scalar order
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
a = nullz = 1