From 1721a4df9b59f4520bdaa6fa73c78c6bbf96bb0d Mon Sep 17 00:00:00 2001 From: Peyton Hammersley Date: Thu, 1 Aug 2024 09:48:51 +0100 Subject: [PATCH] fixed embedded json within decoding. Added support for Number Null and Array --- gremlin-client/src/io/macros.rs | 12 ++++++++ gremlin-client/src/io/serializer_v3.rs | 41 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/gremlin-client/src/io/macros.rs b/gremlin-client/src/io/macros.rs index 3829fd70..fab783cd 100644 --- a/gremlin-client/src/io/macros.rs +++ b/gremlin-client/src/io/macros.rs @@ -5,6 +5,18 @@ macro_rules! g_serializer { Ok(s.clone().into()) } else if let Value::Bool(b) = val { Ok((*b).into()) + } else if let Value::Null = val { + Ok(GValue::Null) + } else if let Value::Number(ref n) = val { + if let Some(i) = n.as_i64() { + Ok(i.into()) + } else if let Some(f) = n.as_f64() { + Ok(f.into()) + } else { + Err(GremlinError::Json("Expected number".to_string())) + } + } else if let Value::Array(ref l) = val { + Ok(l.iter().map($name).collect::>>()?.into()) } else { let _type = &val["@type"]; let _type = get_value!(_type,serde_json::Value::String)?.as_str(); diff --git a/gremlin-client/src/io/serializer_v3.rs b/gremlin-client/src/io/serializer_v3.rs index 8b5ea3bb..43523ff7 100644 --- a/gremlin-client/src/io/serializer_v3.rs +++ b/gremlin-client/src/io/serializer_v3.rs @@ -423,6 +423,7 @@ mod tests { use super::deserializer_v3; use serde_json::json; + use serde_json::Value; use crate::{edge, vertex}; @@ -734,4 +735,44 @@ mod tests { assert_eq!(result, GValue::Map(value_map)); } + + #[test] + fn test_direct_decode() { + let value = json!({ + "@type": "g:List", + "@value": [ + json!(0.2), + json!(0.1), + ] + }); + + let result = deserializer_v3(&value).expect("Failed to deserialize a List"); + + assert_eq!( + result, + GValue::List(vec![GValue::Double(0.2), GValue::Double(0.1)].into()) + ); + + let value = json!({ + "@type": "g:List", + "@value": [ + Value::Null, + ] + }); + + let result = deserializer_v3(&value).expect("Failed to deserialize a List"); + + assert_eq!(result, GValue::List(vec![GValue::Null].into())); + + let value = json!({ + "@type": "g:List", + "@value": [ + json!(1), + ] + }); + + let result = deserializer_v3(&value).expect("Failed to deserialize a List"); + + assert_eq!(result, GValue::List(vec![GValue::Int64(1)].into())); + } }