diff options
Diffstat (limited to 'common/object/src/json.rs')
| -rw-r--r-- | common/object/src/json.rs | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/common/object/src/json.rs b/common/object/src/json.rs index 0aa4cbd..cbb8bda 100644 --- a/common/object/src/json.rs +++ b/common/object/src/json.rs @@ -4,36 +4,30 @@ Copyright (C) 2026 metamuffin <metamuffin.org> */ -use crate::{Object, Registry, types::*}; +use crate::{Object, ValueType}; use serde_json::{Map, Value}; -pub fn object_to_json(reg: &Registry, ob: Object<'_>) -> Value { +pub fn object_to_json(ob: Object<'_>) -> Value { let mut o = Map::new(); let mut nonexhaustive = false; for (i, tag) in ob.keys().enumerate() { - let Some(info) = reg.info(tag) else { - nonexhaustive = true; - continue; - }; - let Some(ty) = info.r#type else { - nonexhaustive = true; - continue; - }; - let key = info.name.to_string(); - - let val = match ty { - x if x == STR => ob.get_typed::<&str>(i).unwrap().to_string().into(), - x if x == OBJECT => object_to_json(reg, ob.get_typed::<Object>(i).unwrap()), - x if x == U32 => ob.get_typed::<u32>(i).unwrap().into(), - x if x == U64 => ob.get_typed::<u64>(i).unwrap().into(), - x if x == I64 => ob.get_typed::<i64>(i).unwrap().into(), - x if x == F64 => ob.get_typed::<f64>(i).unwrap().into(), + let kbytes = tag.0.to_be_bytes(); + let k = str::from_utf8(&kbytes).unwrap().to_string(); + let ty = ob.offset_type(i); + let sz = ob.size(i); + let val = match (ty, sz) { + (ValueType::String, _) => ob.get_typed::<&str>(i).unwrap().to_string().into(), + (ValueType::Object, _) => object_to_json(ob.get_typed::<Object>(i).unwrap()), + (ValueType::UInt, 4) => ob.get_typed::<u32>(i).unwrap().into(), + (ValueType::UInt, 8) => ob.get_typed::<u64>(i).unwrap().into(), + (ValueType::Int, 8) => ob.get_typed::<i64>(i).unwrap().into(), + (ValueType::Float, 8) => ob.get_typed::<f64>(i).unwrap().into(), _ => { nonexhaustive = true; continue; } }; - multi_insert(&mut o, key, val); + multi_insert(&mut o, k, val); } if nonexhaustive { o.insert("_nonexhaustive".to_owned(), Value::Bool(true)); |