본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.json

    JSONValue.kind

    Returns the outermost kind of a JSON value.

    kind() -> string
    수신자
    JSONValue
    최소 인자 수
    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

    매개변수 제약

    JSONValue.kind() -> string returns exactly one of null, bool, number, string, array, or object. The name describes the JSON value's outermost kind. Both true and false have kind bool. Integer and fractional JSON numbers have kind number.

    Test whether a JSON value is null with value.kind() == "null". This comparison is false for false, zero, the empty string, and empty arrays and objects. Reading the kind does not modify or decode the JSON value.

    A present array element or object member containing JSON null is still present. at and get return Some(value) containing that JSONValue. An absent element or member returns None(). Inspect presence first, then compare the contained value's kind.

    JSONValue does not expose an isNull member. Use the same kind comparison for null as for every other JSON kind.

    결과

    The declared result type is string.

    효과

    실패

    Result and effects

    링크

    The result is null, bool, number, string, array, or object. Compare it with "null" to test for JSON null. This query preserves the value and distinguishes stored null from an absent array element or object member.

    Why

    링크

    One query covers every JSON kind, and ordinary equality expresses each kind test.

    Related entries

    링크

    std.json →

    Inspect JSON kinds with kind

    링크
    규칙topaz.stdlib.contract.json-kind.rule

    Inspect JSON kinds with kind

    JSONValue.kind() -> string returns exactly one of null, bool, number, string, array, or object. The name describes the JSON value's outermost kind. Both true and false have kind bool. Integer and fractional JSON numbers have kind number.

    Test whether a JSON value is null with value.kind() == "null". This comparison is false for false, zero, the empty string, and empty arrays and objects. Reading the kind does not modify or decode the JSON value.

    A present array element or object member containing JSON null is still present. at and get return Some(value) containing that JSONValue. An absent element or member returns None(). Inspect presence first, then compare the contained value's kind.

    JSONValue does not expose an isNull member. Use the same kind comparison for null as for every other JSON kind.

    이유

    One kind query distinguishes every JSON value kind. Comparing that result uses the ordinary equality rule for null, booleans, numbers, strings, arrays, and objects. A separate null predicate would duplicate one case and give it a second spelling. Keeping presence separate also prevents a stored null from being mistaken for a missing entry.

    Distinguish null from every other JSON kind

    링크
    main.tpzDistinguish null from every other JSON kind플레이그라운드 열기 →
    1. import std.json
    2. import std.test
    3. import std.io
    4. for source in (["null", "false", "0", "\"\"", "[]", "\{\}"]) {
    5. let value = test.assertOk(json.parse(source))
    6. let kind = value.kind()
    7. let nullKind = kind == "null"
    8. io.writeLine("{kind}:{nullKind}")
    9. }
    기대 출력종료 코드 0
    null:true
    bool:false
    number:false
    string:false
    array:false
    object:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. null:true
    2. bool:false
    3. number:false
    4. string:false
    5. array:false
    6. object:false

    Keep present null values distinct from missing entries

    링크
    main.tpzKeep present null values distinct from missing entries플레이그라운드 열기 →
    1. import std.json
    2. import std.test
    3. import std.io
    4. let array = test.assertOk(json.parse("[null]"))
    5. let object = test.assertOk(json.parse("\{\"present\":null\}"))
    6. let arrayValue = test.assertSome(array.at(0))
    7. let objectValue = test.assertSome(object.get("present"))
    8. test.assertNone(array.at(1))
    9. test.assertNone(object.get("absent"))
    10. io.writeLine("{arrayValue.kind()}:{objectValue.kind()}")
    기대 출력종료 코드 0
    null:null
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. null:null

    Reject the removed JSONValue.isNull member

    링크
    main.tpzReject the removed JSONValue.isNull member플레이그라운드 열기 →
    1. import std.json { JSONValue }
    2. function inspect(value: JSONValue) -> () {
    3. let result = value.isNull()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Use kind to test a JSON value for null

    링크
    main.tpzUse kind to test a JSON value for null플레이그라운드 열기 →
    1. import std.json { JSONValue }
    2. function inspect(value: JSONValue) -> () {
    3. let result = value.kind() == "null"
    4. return ()
    5. }
    기대 출력종료 코드 0
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    검사가 프로그램을 받아들입니다. 종료 코드 0.

    Diagnostics

    링크

    Unknown member or field →

    메뉴

    다운로드 →