Fine tune serde.

This commit is contained in:
Stephen Chung 2022-11-22 15:38:16 +08:00
parent be040287dc
commit b2e4efd872
4 changed files with 29 additions and 7 deletions

View File

@ -38,6 +38,7 @@ rustyline = { version = "10", optional = true }
[dev-dependencies] [dev-dependencies]
serde_bytes = "0.11" serde_bytes = "0.11"
rmp-serde = "1.1"
serde_json = { version = "1.0", default-features = false, features = ["alloc"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
[features] [features]

View File

@ -127,20 +127,20 @@ impl<'de> Visitor<'de> for DynamicVisitor {
#[inline(always)] #[inline(always)]
fn visit_char<E: Error>(self, v: char) -> Result<Self::Value, E> { fn visit_char<E: Error>(self, v: char) -> Result<Self::Value, E> {
self.visit_string(v.to_string()) Ok(v.into())
} }
#[inline(always)] #[inline(always)]
fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> { fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(v.into()) Ok(v.into())
} }
#[inline(always)] #[inline(always)]
fn visit_borrowed_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
self.visit_str(v)
}
#[inline(always)]
fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> { fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
Ok(v.into()) Ok(v.into())
} }
#[inline(always)]
fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
Ok(Dynamic::from_blob(v.to_vec()))
}
#[inline(always)] #[inline(always)]
fn visit_unit<E: Error>(self) -> Result<Self::Value, E> { fn visit_unit<E: Error>(self) -> Result<Self::Value, E> {

View File

@ -20,7 +20,7 @@ impl Serialize for Dynamic {
Union::Unit(..) => ser.serialize_unit(), Union::Unit(..) => ser.serialize_unit(),
Union::Bool(x, ..) => ser.serialize_bool(x), Union::Bool(x, ..) => ser.serialize_bool(x),
Union::Str(ref s, ..) => ser.serialize_str(s.as_str()), Union::Str(ref s, ..) => ser.serialize_str(s.as_str()),
Union::Char(c, ..) => ser.serialize_str(&c.to_string()), Union::Char(c, ..) => ser.serialize_char(c),
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
Union::Int(x, ..) => ser.serialize_i64(x), Union::Int(x, ..) => ser.serialize_i64(x),
@ -58,7 +58,7 @@ impl Serialize for Dynamic {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Array(ref a, ..) => (**a).serialize(ser), Union::Array(ref a, ..) => (**a).serialize(ser),
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Blob(ref a, ..) => (**a).serialize(ser), Union::Blob(ref a, ..) => ser.serialize_bytes(&**a),
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Union::Map(ref m, ..) => { Union::Map(ref m, ..) => {
let mut map = ser.serialize_map(Some(m.len()))?; let mut map = ser.serialize_map(Some(m.len()))?;

View File

@ -809,10 +809,31 @@ fn test_serde_blob() -> Result<(), Box<EvalAltResult>> {
", ",
)?; )?;
let json = serde_json::to_string(&r).unwrap();
assert_eq!(json, "[0,1,2,3,4,5,6,7,8,9]");
let r = from_dynamic::<serde_bytes::ByteBuf>(&r)?; let r = from_dynamic::<serde_bytes::ByteBuf>(&r)?;
assert_eq!(r.to_vec(), vec![0_u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]); assert_eq!(r.to_vec(), vec![0_u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
#[cfg(not(feature = "no_object"))]
{
let r = engine.eval::<Dynamic>(
"
let x = blob(10);
for i in 0..10 { x[i] = i; }
#{ x: x }
",
)?;
let data = format!("{r:?}");
let encoded = rmp_serde::to_vec(&r).unwrap();
let decoded: Dynamic = rmp_serde::from_slice(&encoded).unwrap();
assert_eq!(format!("{decoded:?}"), data);
}
Ok(()) Ok(())
} }