push: Collect empty objects in FlattenedJson

This commit is contained in:
Kévin Commaille 2023-05-24 12:32:08 +02:00 committed by Kévin Commaille
parent 3f28f2a6f1
commit f8ed83aa53

View File

@ -27,10 +27,16 @@ impl FlattenedJson {
fn flatten_value(&mut self, value: JsonValue, path: String) { fn flatten_value(&mut self, value: JsonValue, path: String) {
match value { match value {
JsonValue::Object(fields) => { JsonValue::Object(fields) => {
for (key, value) in fields { if fields.is_empty() {
let key = escape_key(&key); if self.map.insert(path.clone(), FlattenedJsonValue::EmptyObject).is_some() {
let path = if path.is_empty() { key } else { format!("{path}.{key}") }; warn!("Duplicate path in flattened JSON: {path}");
self.flatten_value(value, path); }
} else {
for (key, value) in fields {
let key = escape_key(&key);
let path = if path.is_empty() { key } else { format!("{path}.{key}") };
self.flatten_value(value, path);
}
} }
} }
value => { value => {
@ -211,6 +217,9 @@ pub enum FlattenedJsonValue {
/// Represents an array. /// Represents an array.
Array(Vec<ScalarJsonValue>), Array(Vec<ScalarJsonValue>),
/// Represents an empty object.
EmptyObject,
} }
impl FlattenedJsonValue { impl FlattenedJsonValue {
@ -300,7 +309,7 @@ impl PartialEq<ScalarJsonValue> for FlattenedJsonValue {
Self::Bool(b) => other.as_bool() == Some(*b), Self::Bool(b) => other.as_bool() == Some(*b),
Self::Integer(i) => other.as_integer() == Some(*i), Self::Integer(i) => other.as_integer() == Some(*i),
Self::String(s) => other.as_str() == Some(s), Self::String(s) => other.as_str() == Some(s),
Self::Array(_) => false, Self::Array(_) | Self::EmptyObject => false,
} }
} }
} }
@ -322,7 +331,8 @@ mod tests {
"number": 10, "number": 10,
"array": [1, 2], "array": [1, 2],
"boolean": true, "boolean": true,
"null": null "null": null,
"empty_object": {}
}"#, }"#,
) )
.unwrap(); .unwrap();
@ -336,6 +346,7 @@ mod tests {
"array".into() => vec![int!(1).into(), int!(2).into()].into(), "array".into() => vec![int!(1).into(), int!(2).into()].into(),
"boolean".into() => true.into(), "boolean".into() => true.into(),
"null".into() => FlattenedJsonValue::Null, "null".into() => FlattenedJsonValue::Null,
"empty_object".into() => FlattenedJsonValue::EmptyObject,
} }
); );
} }