Files
launcher/jsonx/jsonx.mbt
SpriteandClaude Sonnet 5.5 17f81c60c8 moon fmt
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 01:08:53 +00:00

53 lines
950 B
MoonBit

///|
/// Small accessors over `Json`, so decoding code reads as lookups with defaults.
///|
pub fn get(j : Json, key : String) -> Json? {
match j {
Object(o) => o.get(key)
_ => None
}
}
///|
pub fn str(j : Json, key : String, default~ : String = "") -> String {
match get(j, key) {
Some(String(s)) => s
_ => default
}
}
///|
pub fn int(j : Json, key : String, default~ : Int32 = 0) -> Int32 {
match get(j, key) {
Some(Number(n, ..)) => n.to_int()
Some(True) => 1
_ => default
}
}
///|
pub fn bool(j : Json, key : String, default~ : Bool = false) -> Bool {
match get(j, key) {
Some(True) => true
Some(False) => false
Some(Number(n, ..)) => n != 0.0
_ => default
}
}
///|
pub fn arr(j : Json, key : String) -> Array[Json] {
match get(j, key) {
Some(Array(a)) => a
_ => []
}
}
///|
pub fn parse(text : String) -> Json? {
Some(@json.parse(text)) catch {
_ => None
}
}