85 lines
2.9 KiB
Plaintext
85 lines
2.9 KiB
Plaintext
fn main(): Unit with {Console, File} = {
|
|
Console.print("=== Lux JSON Example ===")
|
|
Console.print("")
|
|
Console.print("=== Building JSON ===")
|
|
Console.print("")
|
|
let name = Json.string("Alice")
|
|
let age = Json.int(30)
|
|
let active = Json.bool(true)
|
|
let scores = Json.array([Json.int(95), Json.int(87), Json.int(92)])
|
|
let person = Json.object([("name", name), ("age", age), ("active", active), ("scores", scores)])
|
|
Console.print("Built JSON:")
|
|
let pretty = Json.prettyPrint(person)
|
|
Console.print(pretty)
|
|
Console.print("")
|
|
let jsonStr = Json.stringify(person)
|
|
Console.print("Compact: " + jsonStr)
|
|
Console.print("")
|
|
File.write("/tmp/test.json", jsonStr)
|
|
Console.print("Written to /tmp/test.json")
|
|
Console.print("")
|
|
Console.print("=== Parsing JSON ===")
|
|
Console.print("")
|
|
let content = File.read("/tmp/test.json")
|
|
Console.print("Read from file: " + content)
|
|
Console.print("")
|
|
match Json.parse(content) {
|
|
Ok(json) => {
|
|
Console.print("Parse succeeded!")
|
|
Console.print("")
|
|
Console.print("Extracting fields:")
|
|
match Json.get(json, "name") {
|
|
Some(nameJson) => match Json.asString(nameJson) {
|
|
Some(n) => Console.print(" name: " + n),
|
|
None => Console.print(" name: (not a string)"),
|
|
},
|
|
None => Console.print(" name: (not found)"),
|
|
}
|
|
match Json.get(json, "age") {
|
|
Some(ageJson) => match Json.asInt(ageJson) {
|
|
Some(a) => Console.print(" age: " + toString(a)),
|
|
None => Console.print(" age: (not an int)"),
|
|
},
|
|
None => Console.print(" age: (not found)"),
|
|
}
|
|
match Json.get(json, "active") {
|
|
Some(activeJson) => match Json.asBool(activeJson) {
|
|
Some(a) => Console.print(" active: " + toString(a)),
|
|
None => Console.print(" active: (not a bool)"),
|
|
},
|
|
None => Console.print(" active: (not found)"),
|
|
}
|
|
match Json.get(json, "scores") {
|
|
Some(scoresJson) => match Json.asArray(scoresJson) {
|
|
Some(arr) => {
|
|
Console.print(" scores: " + toString(List.length(arr)) + " items")
|
|
match Json.getIndex(scoresJson, 0) {
|
|
Some(firstJson) => match Json.asInt(firstJson) {
|
|
Some(first) => Console.print(" first score: " + toString(first)),
|
|
None => Console.print(" first score: (not an int)"),
|
|
},
|
|
None => Console.print(" (no first element)"),
|
|
}
|
|
},
|
|
None => Console.print(" scores: (not an array)"),
|
|
},
|
|
None => Console.print(" scores: (not found)"),
|
|
}
|
|
Console.print("")
|
|
Console.print("Object keys:")
|
|
match Json.keys(json) {
|
|
Some(ks) => Console.print(" " + String.join(ks, ", ")),
|
|
None => Console.print(" (not an object)"),
|
|
}
|
|
},
|
|
Err(e) => Console.print("Parse error: " + e),
|
|
}
|
|
Console.print("")
|
|
Console.print("=== JSON Null Check ===")
|
|
let nullVal = Json.null()
|
|
Console.print("Is null value null? " + toString(Json.isNull(nullVal)))
|
|
Console.print("Is person null? " + toString(Json.isNull(person)))
|
|
}
|
|
|
|
let result = run main() with {}
|