///|
fn required_string(
object : Map[String, Json],
field : String,
location : String,
) -> String raise AuditError {
guard object.get(field) is Some(String(value)) else {
raise InvalidInput("\{location}: \{field} must be a string")
}
value
}
///|
/// Read the project's version 1 raw JSON answer without deduplicating or
/// rewriting records. JSON null denotes an explicitly unmeasured value.
pub fn parse_json_answer(text : String) -> Array[Row] raise AuditError {
let input = @json.parse(
text,
reject_duplicate_keys=true,
preserve_number_repr=true,
) catch {
_ => raise InvalidInput("invalid answer JSON or repeated object member")
}
guard input is Object(root) &&
root.length() == 2 &&
is_raw_answer_version_one(root.get("schema_version")) &&
root.get("records") is Some(Array(records)) else {
raise InvalidInput(
"answer JSON must contain only schema_version 1 and a records array",
)
}
let rows : Array[Row] = []
for i in 0.. Some((v, repr.unwrap_or(v.to_string())))
Some(Null) => None
_ => raise InvalidInput("\{location}: value must be a number or null")
}
rows.push(make_row(part, operator, repeat, value, unit, 0, location))
}
rows
}
///|
fn is_raw_answer_version_one(value : Json?) -> Bool {
guard value is Some(Number(1.0, repr~)) else { return false }
let decimal = parse_decimal_reading(repr.unwrap_or("1")) catch {
_ => return false
}
let one : DecimalReading = {
coefficient: @bigint.BigInt::from_int(1),
exponent: @bigint.BigInt::from_int(0),
}
decimal_abs_compare(decimal, one) == 0
}