attempt to fix serialize hints
This commit is contained in:
parent
08ac40ae85
commit
27e0d3550d
@ -1,27 +1,40 @@
|
|||||||
use serde::ser::{Serialize, Serializer, SerializeMap};
|
/*use serde::ser::{Serialize, Serializer, SerializeMap};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use zvariant::OwnedValue;
|
//use zvariant::OwnedValue;
|
||||||
//use std::hash::BuildHasher;
|
//use std::hash::BuildHasher;
|
||||||
//use serde::ser::SerializeMap;
|
//use serde::ser::SerializeMap;
|
||||||
use serde_json::{Map,Value};
|
use serde_json::{Map,Value};
|
||||||
use zvariant::SerializeValue;
|
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<S>(actions: &[String], serializer: S) -> Result<S::Ok, S::Error>
|
pub fn serialize_actions<S>(actions: &[String], serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
let mut map = serde_json::Map::new();
|
let mut map = Map::new();
|
||||||
|
|
||||||
// Actions are in pairs: [id, label, id, label, ...]
|
// Actions are in pairs: [id, label, id, label, ...]
|
||||||
for pair in actions.chunks(2) {
|
for pair in actions.chunks(2) {
|
||||||
if let [id, label] = pair {
|
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)
|
map.serialize(serializer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn serialize_hints<S>(
|
pub fn serialize_hints<S>(
|
||||||
hints: &HashMap<String, OwnedValue>,
|
hints: &HashMap<String, OwnedValue>,
|
||||||
serializer: S,
|
serializer: S,
|
||||||
@ -40,7 +53,7 @@ where
|
|||||||
|
|
||||||
// A custom struct to handle serialization of OwnedValue
|
// A custom struct to handle serialization of OwnedValue
|
||||||
struct HintValueSerializer<'a>(&'a OwnedValue);
|
struct HintValueSerializer<'a>(&'a OwnedValue);
|
||||||
|
/*
|
||||||
impl<'a> Serialize for HintValueSerializer<'a> {
|
impl<'a> Serialize for HintValueSerializer<'a> {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@ -56,4 +69,35 @@ impl<'a> Serialize for HintValueSerializer<'a> {
|
|||||||
map.serialize_entry("value", value)?;
|
map.serialize_entry("value", value)?;
|
||||||
map.end()
|
map.end()
|
||||||
}
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
impl<'a> Serialize for HintValueSerializer<'a> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
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::<bool>() {
|
||||||
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user