feat: add JSON parsing and manipulation support

Add comprehensive JSON support via the Json module:
- Parse JSON strings with Json.parse() returning Result<Json, String>
- Stringify with Json.stringify() and Json.prettyPrint()
- Extract values with Json.get(), getIndex(), asString(), asInt(), etc.
- Build JSON with constructors: Json.null(), bool(), int(), string(), array(), object()
- Query with Json.isNull() and Json.keys()

Includes example at examples/json.lux demonstrating building, parsing,
and extracting JSON data with file I/O integration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 16:22:35 -05:00
parent b0f6756411
commit ef9746c2fe
3 changed files with 580 additions and 0 deletions

112
examples/json.lux Normal file
View File

@@ -0,0 +1,112 @@
// JSON example - demonstrates JSON parsing and manipulation
//
// This script parses JSON, extracts values, and builds new JSON structures
fn main(): Unit with {Console, File} = {
Console.print("=== Lux JSON Example ===")
Console.print("")
// First, build some JSON programmatically
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("")
// Stringify to a compact string
let jsonStr = Json.stringify(person)
Console.print("Compact: " + jsonStr)
Console.print("")
// Write to file and read back to test parsing
File.write("/tmp/test.json", jsonStr)
Console.print("Written to /tmp/test.json")
Console.print("")
// Read and parse from file
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("")
// Get string field
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)")
}
// Get int field
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)")
}
// Get bool field
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)")
}
// Get array field
match Json.get(json, "scores") {
Some(scoresJson) => match Json.asArray(scoresJson) {
Some(arr) => {
Console.print(" scores: " + toString(List.length(arr)) + " items")
// Get first score
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("")
// Get the keys
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 {}