From f25f2ce2ca7e6e340859bc1c79dce24b37d4e460 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 21 Oct 2024 10:10:53 +0530 Subject: [PATCH] another unsuccessful attempt to ser hints --- src/formats/serde.rs | 65 +++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 43 deletions(-) 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