diff --git a/src/formats/serde.rs b/src/formats/serde.rs index a86f8ec..40ad3db 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,27 +1,40 @@ -use serde::ser::{Serialize, Serializer, SerializeMap}; +/*use serde::ser::{Serialize, Serializer, SerializeMap}; use std::collections::HashMap; -use zvariant::OwnedValue; +//use zvariant::OwnedValue; //use std::hash::BuildHasher; //use serde::ser::SerializeMap; use serde_json::{Map,Value}; use zvariant::SerializeValue; +use std::convert::TryInto; +use zvariant::{OwnedValue, Value};*/ + + +use serde::ser::{Serialize, Serializer, SerializeMap}; +use std::collections::HashMap; +use std::convert::TryInto; // Import TryInto +use serde_json::{Map, Value as JsonValue}; // Alias serde_json::Value to JsonValue +use zvariant::{OwnedValue, Value as ZValue}; // Alias zvariant::Value to ZValue + + + pub fn serialize_actions(actions: &[String], serializer: S) -> Result where S: Serializer, { - let mut map = serde_json::Map::new(); + let mut map = Map::new(); // Actions are in pairs: [id, label, id, label, ...] for pair in actions.chunks(2) { if let [id, label] = pair { - map.insert(id.clone(), serde_json::Value::String(label.clone())); + map.insert(id.clone(), JsonValue::String(label.clone())); } } map.serialize(serializer) } + pub fn serialize_hints( hints: &HashMap, serializer: S, @@ -40,7 +53,7 @@ where // A custom struct to handle serialization of OwnedValue struct HintValueSerializer<'a>(&'a OwnedValue); - +/* impl<'a> Serialize for HintValueSerializer<'a> { fn serialize(&self, serializer: S) -> Result where @@ -56,4 +69,35 @@ impl<'a> Serialize for HintValueSerializer<'a> { map.serialize_entry("value", value)?; map.end() } -} \ No newline at end of file +}*/ + +impl<'a> Serialize for HintValueSerializer<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let signature = self.0.value_signature().to_string(); + + // Extract the raw value using downcast_ref for common types + let raw_value = if let Ok(v) = self.0.downcast_ref::<&u8>() { + serde_json::Value::Number(serde_json::Number::from(*v)) + } else if let Ok(v) = self.0.downcast_ref::<&i32>() { + serde_json::Value::Number(serde_json::Number::from(*v)) + } else if let Ok(v) = self.0.downcast_ref::<&u64>() { + serde_json::Value::Number(serde_json::Number::from(*v)) + } else if let Ok(v) = self.0.downcast_ref::() { + serde_json::Value::Bool(v) + } else if let Ok(v) = self.0.downcast_ref::<&String>() { + serde_json::Value::String(v.to_string()) + } else { + serde_json::Value::Null // Unsupported types fallback to Null + }; + + // Serialize the final structure as a map with "signature" and "value" + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("signature", &signature)?; + map.serialize_entry("value", &raw_value)?; + map.end() + } +} +