use std::collections::BTreeMap;
use crate::reexports::JsonValue;
pub fn serde_to_tcb(v: serde_json::Value) -> JsonValue {
match v {
serde_json::Value::Null => JsonValue::Null,
serde_json::Value::Bool(b) => JsonValue::Bool(b),
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
JsonValue::Integer(i)
} else {
JsonValue::String(n.to_string().into())
}
}
serde_json::Value::String(s) => JsonValue::String(s.into()),
serde_json::Value::Array(arr) => {
JsonValue::Array(arr.into_iter().map(serde_to_tcb).collect())
}
serde_json::Value::Object(obj) => {
let mut map = BTreeMap::new();
for (k, val) in obj {
map.insert(k, serde_to_tcb(val));
}
JsonValue::Object(map)
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use serde_json::json;
#[test]
fn scalars_map_one_to_one() {
assert!(matches!(serde_to_tcb(json!(null)), JsonValue::Null));
assert!(matches!(serde_to_tcb(json!(true)), JsonValue::Bool(true)));
assert!(matches!(serde_to_tcb(json!(false)), JsonValue::Bool(false)));
match serde_to_tcb(json!("hello")) {
JsonValue::String(s) => assert_eq!(s.as_ref(), "hello"),
other => panic!("expected String, got {other:?}"),
}
}
#[test]
fn integers_stay_integer() {
match serde_to_tcb(json!(42)) {
JsonValue::Integer(i) => assert_eq!(i, 42),
other => panic!("expected Integer, got {other:?}"),
}
match serde_to_tcb(json!(-7)) {
JsonValue::Integer(i) => assert_eq!(i, -7),
other => panic!("expected Integer, got {other:?}"),
}
}
#[test]
fn floats_become_domain_decimal_strings() {
match serde_to_tcb(json!(2.5)) {
JsonValue::String(s) => assert_eq!(s.as_ref(), "2.5"),
other => panic!("expected String decimal form, got {other:?}"),
}
}
#[test]
fn arrays_convert_recursively() {
match serde_to_tcb(json!([1, "a", true, null])) {
JsonValue::Array(items) => {
assert_eq!(items.len(), 4);
assert!(matches!(items[0], JsonValue::Integer(1)));
assert!(matches!(items[1], JsonValue::String(_)));
assert!(matches!(items[2], JsonValue::Bool(true)));
assert!(matches!(items[3], JsonValue::Null));
}
other => panic!("expected Array, got {other:?}"),
}
}
#[test]
fn objects_convert_recursively_and_normalize_key_order() {
let out = serde_to_tcb(json!({
"zeta": 1,
"nested": {"beta": [2.5, "x"], "alpha": null},
"alpha": "first"
}));
match out {
JsonValue::Object(map) => {
assert_eq!(map.len(), 3);
let keys: Vec<_> = map.keys().cloned().collect();
assert_eq!(keys, vec!["alpha", "nested", "zeta"]);
match &map["nested"] {
JsonValue::Object(inner) => {
let inner_keys: Vec<_> = inner.keys().cloned().collect();
assert_eq!(inner_keys, vec!["alpha", "beta"]);
match &inner["beta"] {
JsonValue::Array(items) => {
match &items[0] {
JsonValue::String(s) => assert_eq!(s.as_ref(), "2.5"),
other => panic!("expected String decimal, got {other:?}"),
}
assert!(
matches!(items[1], JsonValue::String(ref s) if s.as_ref() == "x")
);
}
other => panic!("expected Array, got {other:?}"),
}
}
other => panic!("expected Object, got {other:?}"),
}
}
other => panic!("expected Object, got {other:?}"),
}
}
#[test]
fn round_trip_shape_matches_loader_semantics() {
let out = serde_to_tcb(json!({
"metadata": {
"constants": [
{ "constant_id": "c1", "version": "2018", "value_hash": "h1", "domain_lo": 0, "domain_hi": 1 }
]
}
}));
match out {
JsonValue::Object(map) => match &map["metadata"] {
JsonValue::Object(meta) => match &meta["constants"] {
JsonValue::Array(items) => {
assert_eq!(items.len(), 1);
match &items[0] {
JsonValue::Object(entry) => {
match &entry["domain_lo"] {
JsonValue::Integer(i) => assert_eq!(*i, 0),
other => panic!("expected Integer, got {other:?}"),
}
match &entry["version"] {
JsonValue::String(s) => assert_eq!(s.as_ref(), "2018"),
other => panic!("expected String, got {other:?}"),
}
}
other => panic!("expected Object entry, got {other:?}"),
}
}
other => panic!("expected Array, got {other:?}"),
},
other => panic!("expected Object metadata, got {other:?}"),
},
other => panic!("expected Object, got {other:?}"),
}
}
}