///|
/// A decoded JSON object plus the path it was found at, so every field error
/// can name its exact location. Readers deliberately ignore unknown fields for
/// forward evolution; each concrete codec still names every field it consumes
/// and decides how `null` differs from absence.
priv struct JsonObject {
fields : Map[String, Json]
path : @json.JsonPath
}
///|
/// Require `json` to be an object. `expected` names the payload in the failure
/// message, e.g. "Git history payload" reads as "expected Git history payload
/// object".
fn JsonObject::decode(
json : Json,
path : @json.JsonPath,
expected : String,
) -> JsonObject raise @json.JsonDecodeError {
guard json is Object(fields) else {
raise JsonDecodeError((path, "expected \{expected} object"))
}
{ fields, path, }
}
///|
fn JsonObject::required_json(
self : JsonObject,
name : String,
) -> Json raise @json.JsonDecodeError {
match self.fields.get(name) {
Some(value) => value
None =>
raise JsonDecodeError((self.path.add_key(name), "missing required field"))
}
}