diff options
| -rw-r--r-- | Cargo.lock | 13 | ||||
| -rw-r--r-- | common/Cargo.toml | 2 | ||||
| -rw-r--r-- | common/object/Cargo.toml | 4 | ||||
| -rw-r--r-- | common/object/src/json.rs | 52 | ||||
| -rw-r--r-- | common/object/src/lib.rs | 3 | ||||
| -rw-r--r-- | common/object/src/registry.rs | 1 |
6 files changed, 71 insertions, 4 deletions
@@ -1872,6 +1872,7 @@ version = "0.1.0" dependencies = [ "bytemuck", "log", + "serde_json", ] [[package]] @@ -3442,15 +3443,15 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.145" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "itoa", "memchr", - "ryu", "serde", "serde_core", + "zmij", ] [[package]] @@ -4814,6 +4815,12 @@ dependencies = [ ] [[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" + +[[package]] name = "zstd-sys" version = "2.0.16+zstd.1.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/common/Cargo.toml b/common/Cargo.toml index 1d7d0a8..4eae9f8 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -5,4 +5,4 @@ edition = "2024" [dependencies] jellystream-types = { path = "../stream/types" } -jellyobject = { path = "object" } +jellyobject = { path = "object", features = ["json"] } diff --git a/common/object/Cargo.toml b/common/object/Cargo.toml index a5825ee..2c27c5e 100644 --- a/common/object/Cargo.toml +++ b/common/object/Cargo.toml @@ -6,3 +6,7 @@ edition = "2024" [dependencies] bytemuck = { version = "1.24.0", features = ["extern_crate_std"] } log = "0.4.29" +serde_json = { version = "1.0.149", optional = true } + +[features] +json = ["dep:serde_json"] 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) +} diff --git a/common/object/src/lib.rs b/common/object/src/lib.rs index 0eb11fd..9af4f85 100644 --- a/common/object/src/lib.rs +++ b/common/object/src/lib.rs @@ -7,11 +7,14 @@ mod buffer; pub mod inspect; +#[cfg(feature = "json")] +pub mod json; mod path; mod registry; #[cfg(test)] mod tests; mod value; + pub use buffer::*; pub use path::*; pub use registry::*; diff --git a/common/object/src/registry.rs b/common/object/src/registry.rs index 85d3ff2..f3ed7df 100644 --- a/common/object/src/registry.rs +++ b/common/object/src/registry.rs @@ -16,6 +16,7 @@ pub mod types { pub const BINARY: TypeId = TypeId::of::<&[u8]>(); pub const U32: TypeId = TypeId::of::<u32>(); pub const U64: TypeId = TypeId::of::<u64>(); + pub const F64: TypeId = TypeId::of::<f64>(); } #[derive(Default, Clone)] |