diff --git a/src/formats/serde.rs b/src/formats/serde.rs index 2c5473d..a86f8ec 100644 --- a/src/formats/serde.rs +++ b/src/formats/serde.rs @@ -1,8 +1,8 @@ -use serde::{Serialize, Serializer}; +use serde::ser::{Serialize, Serializer, SerializeMap}; use std::collections::HashMap; use zvariant::OwnedValue; //use std::hash::BuildHasher; -use serde::ser::SerializeMap; +//use serde::ser::SerializeMap; use serde_json::{Map,Value}; use zvariant::SerializeValue; @@ -29,52 +29,31 @@ pub fn serialize_hints( where S: Serializer, { - // You can customize the serialization logic here - let mut state = serializer.serialize_map(Some(hints.len()))?; - for (key, value) in hints { - state.serialize_entry(key, value)?; - } - state.end() -} - -/* -pub fn serialize_hints(hints: &HashMap, serializer: S) -> Result -where - S: serde::Serializer, -{ + // Start serializing the map let mut map = serializer.serialize_map(Some(hints.len()))?; - for (key, value) in hints { - // Try to cast the value to a string, otherwise fallback to debug format - let value_str = match value.downcast_ref::() { - Ok(s) => s.clone(), // Successfully cast to String - Err(_) => format!("{:?}", value), // Fallback for non-String values - }; - - map.serialize_entry(key, &value_str)?; + // Serialize each entry as desired + map.serialize_entry(key, &HintValueSerializer(value))?; } - map.end() } -*/ -/* -pub fn serialize_hints( - hints: &HashMap, - serializer: S, -) -> Result -where - S: Serializer, - H: BuildHasher, -{ - let mut map = serde_json::Map::new(); +// A custom struct to handle serialization of OwnedValue +struct HintValueSerializer<'a>(&'a OwnedValue); - for (key, value) in hints { - // Customize OwnedValue serialization as needed - map.insert(key.clone(), serde_json::Value::String(value.to_string())); +impl<'a> Serialize for HintValueSerializer<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + // Access the signature and value parts of the OwnedValue + let signature = self.0.value_signature().to_string(); + let value = &self.0; + + // Serialize them as a map with "signature" and "value" fields + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("signature", &signature)?; + map.serialize_entry("value", value)?; + map.end() } - - map.serialize(serializer) -} - -*/ +} \ No newline at end of file