Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

json

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Syntax:

Category#

format

None.

Examples#

radix/corpus/reference/type/json/json.fab (canonical · type)#

# =============================================================================
# json — JSON serialization and deserialization
# =============================================================================
#
# What this teaches:
#   • JSON type conversions using `↦` — converting between `json`, `value`, and concrete Faber types
#   • the `@json` annotation for custom field naming and genus-level JSON configuration
#   • roundtrip serialization with `json.stringify()` and deserialization with `json.parse()`
#
# Common mistakes:
#   • Applying @ json to a non-genus declaration — @ json is only supported on class declarations and their fields.
#
# See also:
# =============================================================================

import from "norma:json" json

@ json
class Payload {
    @ json { name = "wire_name" }
    mut string name
}

main {
    let json literal ← { "wire_name": "Ada", "items": [1, { "ok": true }] }
    let value broad ← literal ↦ value
    let json narrowed ← broad ↦ json
    let Payload payload ← narrowed ↦ Payload
    let json encoded ← payload ↦ json
    let string wire ← json.stringify(encoded)
    let json ∪ none tentativa ← json.try_parse(wire)
    print payload.name
    print wire
    print tentativa
    do {
        let json reparsed ← json.parse(wire)
        let Payload roundtrip ← reparsed ↦ Payload
        print roundtrip.name
    }
    catch err {
        print err
    }
}

radix/corpus/reference/reject/json/literal-duplicate-key-reject.fab (supporting · reject)#

A JSON object literal cannot repeat a key: the second occurrence rejects with SEM011:json_duplicate_key.

# =============================================================================
# json — A JSON object literal cannot repeat a key
# =============================================================================
#
# What this teaches:
#   • An object literal like `{ "a": 1, "a": 2 }` is ambiguous: which value wins?
#   • The typechecker rejects the second occurrence at its key, not the object as a whole
#   • Nested objects are checked the same way
#
# Common mistakes:
#   • Retyping an object and leaving an older key in place
#
# See also: json
# =============================================================================

# JSON duplicate-key reject case — expected compile failure
#
# This exemplum intentionally fails to compile.

main {
    let json a ← { "a": 1, "a": 2 }
    print a
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/json/literal-identifier-reject.fab (supporting · reject)#

JSON values are strings, numbers, objects, arrays or the wire words true/false/null: any other identifier value rejects with PARSE002:json_unknown_identifier_value.

# =============================================================================
# json — a bare identifier is not a JSON value
# =============================================================================
#
# What this teaches:
#   • JSON values are strings, numbers, objects, arrays and the wire words
#     `true`, `false` and `null`.
#   • A bare identifier such as `foo` is not JSON data and is rejected while
#     parsing the JSON literal.
#
# Common mistakes:
#   • Writing `{ "flag": foo }` when a JSON boolean was meant — write `true`.
#   • Expecting a Faber binding to be resolved inside a JSON literal.
#
# See also: json
# =============================================================================

main { let json a ← { "flag": foo } ; print a }

Expected: compilation rejects this example.

radix/corpus/reference/reject/json/literal-trailing-comma-reject.fab (supporting · reject)#

JSON follows RFC 8259: a comma before the closing brace rejects with PARSE001:item_list_trailing_comma.

# =============================================================================
# json — JSON follows RFC 8259: a comma before the closing brace rejects
# =============================================================================
#
# What this teaches:
#   • A JSON object literal is an RFC 8259 document: `{ "a": 1, }` is not one
#   • A comma directly before the closing brace rejects at parse time
#   • The same item-list law holds everywhere; JSON rows also name RFC 8259
#
# Common mistakes:
#   • Copying a pretty-printed object that leaves a dangling comma
#
# See also: json
# =============================================================================

# JSON trailing-comma reject case — expected compile failure
#
# This exemplum intentionally fails to compile.

main {
    let json a ← { "a": 1, }
    print a
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/json/literal-unquoted-key-reject.fab (supporting · reject)#

JSON object keys must be quoted strings: a bare identifier key rejects with PARSE001:json_quoted_key.

# =============================================================================
# json unquoted key — JSON object keys must be quoted strings.
# =============================================================================
#
# What this teaches:
#   • A bare `{` in expression position enters the JSON literal grammar
#   • JSON object keys are quoted strings: { "a": 1 }, never { a: 1 }
#   • A bare identifier key rejects with PARSE001:json_quoted_key
#
# Common mistakes:
#   • Writing { a: 1 } instead of { "a": 1 } — keys are JSON strings, not Faber names
#
# See also: json
# =============================================================================

# json unquoted-key reject case — expected compile failure
#
# This exemplum intentionally fails to compile.

main {
    let json a ← { a: 1 }
    print a
}

Expected: compilation rejects this example.