length: Option<int>- 수신자
- 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
매개변수 제약
Bytes.length and ByteBuffer.length are read-only int properties that report the number of bytes.
JSONValue.length is an Option<int> property. Arrays and objects return Some(count) for their element or member count. Other JSON kinds return None().
JSONValue.keys and JSONValue.values are properties returning Option<Array<string>> and Option<Array<JSON. Objects return Some(keys) or Some(values). Other JSON kinds return None().
Read these properties without parentheses, as with Array.length, Map.length, Map.keys, Map.values, and Set.length. Calling a property value is rejected as a non-callable value.
관련 규칙 · Read collection sizes, keys, and values as properties →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<int>.
효과
실패
Result and effects
링크Select this field from a value of its named type without call parentheses.
Read the optional number of array elements or object members through a property.
Why
링크Collection observations use the same property syntax as Array, Map, and Set. Parentheses identify calls rather than stored or derived values.
Related entries
링크Read collection sizes, keys, and values as properties
링크Read collection sizes, keys, and values as properties
Bytes.length and ByteBuffer.length are read-only int properties that report the number of bytes.
JSONValue.length is an Option<int> property. Arrays and objects return Some(count) for their element or member count. Other JSON kinds return None().
JSONValue.keys and JSONValue.values are properties returning Option<Array<string>> and Option<Array<JSON. Objects return Some(keys) or Some(values). Other JSON kinds return None().
Read these properties without parentheses, as with Array.length, Map.length, Map.keys, Map.values, and Set.length. Calling a property value is rejected as a non-callable value.
이유
The same collection observation should use the same syntax across core collections and library values. Keeping JSONValue results optional preserves its distinction between collections and scalar values.
Read the same properties on core collections
링크import std.iolet array = [2, 3]let entries = map { "name": "Topaz" }let values = set { 2, 3 }io.writeLine("{array.length}:{entries.length}:{values.length}")io.writeLine(entries.keys[0])io.writeLine(entries.values[0])
2:1:2
name
Topaz
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
2:1:2nameTopaz
Read a byte sequence size
링크import std.bytes { Bytes }import std.iolet bytes = Bytes.empty()let size: int = bytes.lengthio.writeLine("{size}")
0
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
0
Read a byte buffer size
링크import std.bytes { ByteBuffer }import std.iolet buffer = ByteBuffer.allocate(3)let size: int = buffer.lengthio.writeLine("{size}")
3
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
3
Read JSON sizes and preserve optional results
링크import std.jsonimport std.testimport std.iolet array = test.assertOk(json.parse("[1,2]"))let object = test.assertOk(json.parse("\{\"name\":\"Topaz\"\}"))let scalar = test.assertOk(json.parse("42"))let arraySize: Option<int> = array.lengthlet objectSize: Option<int> = object.lengthlet scalarSize: Option<int> = scalar.lengthtest.assertEq(test.assertSome(arraySize), 2)test.assertEq(test.assertSome(objectSize), 1)test.assertNone(scalarSize)io.writeLine("JSON sizes read")
JSON sizes read
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
JSON sizes read
Read JSON object keys
링크import std.jsonimport std.testimport std.iolet object = test.assertOk(json.parse("\{\"name\":\"Topaz\"\}"))let array = test.assertOk(json.parse("[]"))let scalar = test.assertOk(json.parse("42"))let names: Option<Array<string>> = object.keystest.assertEq(test.assertSome(names)[0], "name")test.assertNone(array.keys)test.assertNone(scalar.keys)io.writeLine("JSON keys read")
JSON keys read
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
JSON keys read
Read JSON object values
링크import std.jsonimport std.testimport std.iolet object = test.assertOk(json.parse("\{\"name\":\"Topaz\"\}"))let array = test.assertOk(json.parse("[]"))let scalar = test.assertOk(json.parse("42"))let values = test.assertSome(object.values)test.assertEq(test.assertSome(values[0].asString()), "Topaz")test.assertNone(array.values)test.assertNone(scalar.values)io.writeLine("JSON values read")
JSON values read
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
JSON values read
Reject calling Bytes.length
링크import std.bytes { Bytes }function inspect(value: Bytes) -> () {let result = value.length()return ()}
표준 출력 없음.기대 결과
Reject calling ByteBuffer.length
링크import std.bytes { ByteBuffer }function inspect(value: ByteBuffer) -> () {let result = value.length()return ()}
표준 출력 없음.기대 결과
Reject calling JSONValue.length
링크import std.json { JSONValue }function inspect(value: JSONValue) -> () {let result = value.length()return ()}
표준 출력 없음.기대 결과
Reject calling JSONValue.keys
링크import std.json { JSONValue }function inspect(value: JSONValue) -> () {let result = value.keys()return ()}
표준 출력 없음.기대 결과
Reject calling JSONValue.values
링크import std.json { JSONValue }function inspect(value: JSONValue) -> () {let result = value.values()return ()}
표준 출력 없음.기대 결과
Diagnostics
링크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