add compat mode for JSON (#2219)

This commit is contained in:
PSeitz
2023-10-17 10:00:55 +02:00
committed by GitHub
parent 5e06e504e6
commit ecb9a89a9f
2 changed files with 49 additions and 2 deletions

View File

@@ -18,6 +18,8 @@ use std::sync::Arc;
use columnar::MonotonicallyMappableToU128; use columnar::MonotonicallyMappableToU128;
use common::{u64_to_f64, BinarySerializable, DateTime, VInt}; 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::document::type_codes;
use crate::schema::{Facet, Field}; use crate::schema::{Facet, Field};
use crate::tokenizer::PreTokenizedString; use crate::tokenizer::PreTokenizedString;
@@ -157,6 +159,9 @@ pub enum ValueType {
Array, Array,
/// A dynamic object value. /// A dynamic object value.
Object, Object,
/// A JSON object value. Deprecated.
#[deprecated]
JSONObject,
} }
/// A value visitor for deserializing a document value. /// A value visitor for deserializing a document value.
@@ -376,6 +381,8 @@ where R: Read
type_codes::NULL_CODE => ValueType::Null, type_codes::NULL_CODE => ValueType::Null,
type_codes::ARRAY_CODE => ValueType::Array, type_codes::ARRAY_CODE => ValueType::Array,
type_codes::OBJECT_CODE => ValueType::Object, type_codes::OBJECT_CODE => ValueType::Object,
#[allow(deprecated)]
type_codes::JSON_OBJ_CODE => ValueType::JSONObject,
_ => { _ => {
return Err(DeserializeError::from(io::Error::new( return Err(DeserializeError::from(io::Error::new(
io::ErrorKind::InvalidData, io::ErrorKind::InvalidData,
@@ -514,6 +521,26 @@ where R: Read
let access = BinaryObjectDeserializer::from_reader(self.reader)?; let access = BinaryObjectDeserializer::from_reader(self.reader)?;
visitor.visit_object(access) 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 = <serde_json::Map::<String, serde_json::Value> 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)); 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] #[test]
fn test_nested_serialize() { fn test_nested_serialize() {
let mut object = serde_json::Map::new(); let mut object = serde_json::Map::new();

View File

@@ -254,8 +254,9 @@ pub(crate) mod type_codes {
pub const DATE_CODE: u8 = 5; pub const DATE_CODE: u8 = 5;
pub const F64_CODE: u8 = 6; pub const F64_CODE: u8 = 6;
pub const EXT_CODE: u8 = 7; 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 BOOL_CODE: u8 = 9;
pub const IP_CODE: u8 = 10; pub const IP_CODE: u8 = 10;
pub const NULL_CODE: u8 = 11; pub const NULL_CODE: u8 = 11;