diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-02-16 17:50:36 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-02-16 17:50:36 +0100 |
| commit | bb1822e3e68fe6f699102bfc1659731bdbac1a40 (patch) | |
| tree | a45621f7f033384760f0423af7f5482e26d48a2d /common/object/src/json.rs | |
| parent | 13835c447e400d8fd3d5641552e96fadd63e87dc (diff) | |
| download | jellything-bb1822e3e68fe6f699102bfc1659731bdbac1a40.tar jellything-bb1822e3e68fe6f699102bfc1659731bdbac1a40.tar.bz2 jellything-bb1822e3e68fe6f699102bfc1659731bdbac1a40.tar.zst | |
object to json conversion
Diffstat (limited to 'common/object/src/json.rs')
| -rw-r--r-- | common/object/src/json.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/common/object/src/json.rs b/common/object/src/json.rs new file mode 100644 index 0000000..e8b04ea --- /dev/null +++ b/common/object/src/json.rs @@ -0,0 +1,52 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin <metamuffin.org> +*/ + +use crate::{Object, Registry, types::*}; +use serde_json::{Map, Value}; + +pub fn object_to_json(reg: &Registry, ob: Object<'_>) -> Value { + eprintln!("{ob:#?}"); + 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(); + + match ty { + x if x == STR => { + o.insert(key, ob.get_typed::<&str>(i).unwrap().to_string().into()); + } + x if x == OBJECT => { + eprintln!("{key}"); + o.insert(key, object_to_json(reg, ob.get_typed::<Object>(i).unwrap())); + } + x if x == U32 => { + o.insert(key, ob.get_typed::<u32>(i).unwrap().to_string().into()); + } + x if x == U64 => { + o.insert(key, ob.get_typed::<u64>(i).unwrap().to_string().into()); + } + x if x == F64 => { + o.insert(key, ob.get_typed::<f64>(i).unwrap().to_string().into()); + } + _ => { + nonexhaustive = true; + } + }; + } + if nonexhaustive { + o.insert("_nonexhaustive".to_owned(), Value::Bool(true)); + } + + Value::Object(o) +} |