From ecb9a89a9ff0573fba5c2cf8b83f353f35022310 Mon Sep 17 00:00:00 2001 From: PSeitz Date: Tue, 17 Oct 2023 10:00:55 +0200 Subject: [PATCH] add compat mode for JSON (#2219) --- src/schema/document/de.rs | 46 ++++++++++++++++++++++++++++++++++++++ src/schema/document/mod.rs | 5 +++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/schema/document/de.rs b/src/schema/document/de.rs index 1fb192d139..706f3b768e 100644 --- a/src/schema/document/de.rs +++ b/src/schema/document/de.rs @@ -18,6 +18,8 @@ use std::sync::Arc; use columnar::MonotonicallyMappableToU128; use common::{u64_to_f64, BinarySerializable, DateTime, VInt}; +use super::se::BinaryObjectSerializer; +use super::{OwnedValue, Value}; use crate::schema::document::type_codes; use crate::schema::{Facet, Field}; use crate::tokenizer::PreTokenizedString; @@ -157,6 +159,9 @@ pub enum ValueType { Array, /// A dynamic object value. Object, + /// A JSON object value. Deprecated. + #[deprecated] + JSONObject, } /// A value visitor for deserializing a document value. @@ -376,6 +381,8 @@ where R: Read type_codes::NULL_CODE => ValueType::Null, type_codes::ARRAY_CODE => ValueType::Array, type_codes::OBJECT_CODE => ValueType::Object, + #[allow(deprecated)] + type_codes::JSON_OBJ_CODE => ValueType::JSONObject, _ => { return Err(DeserializeError::from(io::Error::new( io::ErrorKind::InvalidData, @@ -514,6 +521,26 @@ where R: Read let access = BinaryObjectDeserializer::from_reader(self.reader)?; visitor.visit_object(access) } + #[allow(deprecated)] + ValueType::JSONObject => { + // This is a compatibility layer + // The implementation is slow, but is temporary anyways + let mut de = serde_json::Deserializer::from_reader(self.reader); + let json_map = as serde::Deserialize>::deserialize(&mut de).map_err(|err| DeserializeError::Custom(err.to_string()))?; + let mut out = Vec::new(); + let mut serializer = BinaryObjectSerializer::begin(json_map.len(), &mut out)?; + for (key, val) in json_map { + let val: OwnedValue = val.into(); + serializer.serialize_entry(&key, (&val).as_value())?; + } + serializer.end()?; + + let out_rc = std::rc::Rc::new(out); + let mut slice: &[u8] = &out_rc; + let access = BinaryObjectDeserializer::from_reader(&mut slice)?; + + visitor.visit_object(access) + } } } } @@ -955,6 +982,25 @@ mod tests { assert_eq!(value, crate::schema::OwnedValue::Object(expected_object)); } + #[test] + fn test_json_compat() { + let data = [ + 8, 123, 34, 107, 101, 121, 97, 58, 34, 58, 34, 98, 108, 117, 98, 34, 44, 34, 118, 97, + 108, 115, 34, 58, 123, 34, 104, 101, 121, 34, 58, 34, 104, 111, 34, 125, 125, + ] + .to_vec(); + let expected = json!({ + "keya:": "blub", + "vals": { + "hey": "ho" + } + }); + let expected_val: OwnedValue = expected.clone().into(); + + let value = deserialize_value(data); + assert_eq!(value, expected_val); + } + #[test] fn test_nested_serialize() { let mut object = serde_json::Map::new(); diff --git a/src/schema/document/mod.rs b/src/schema/document/mod.rs index 0e09242f7e..941ecbd2ac 100644 --- a/src/schema/document/mod.rs +++ b/src/schema/document/mod.rs @@ -254,8 +254,9 @@ pub(crate) mod type_codes { pub const DATE_CODE: u8 = 5; pub const F64_CODE: u8 = 6; pub const EXT_CODE: u8 = 7; - // Replaced by the `OBJECT_CODE`. - // -- pub const JSON_OBJ_CODE: u8 = 8; + + #[deprecated] + pub const JSON_OBJ_CODE: u8 = 8; // Replaced by the `OBJECT_CODE`. pub const BOOL_CODE: u8 = 9; pub const IP_CODE: u8 = 10; pub const NULL_CODE: u8 = 11;