Renderingen-US

json

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

Syntax:

Category#

type

None.

Examples#

radix/corpus/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" }
    var string name
}

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