Skip to content

Commit

Permalink
Recover raw values after losing the type
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Jun 30, 2024
1 parent a9e089a commit 049e7ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ impl Serialize for Value {
#[cfg(any(feature = "std", feature = "alloc"))]
Value::Object(m) => {
use serde::ser::SerializeMap;
#[cfg(feature = "raw_value")]
if m.len() == 1 {
if let Some(Value::String(value)) = m.get(crate::raw::TOKEN) {
if let Ok(value) = crate::raw::RawValue::from_string(value.clone()) {
return value.serialize(serializer);
}
}
}
let mut map = tri!(serializer.serialize_map(Some(m.len())));
for (k, v) in m {
tri!(map.serialize_entry(k, v));
Expand Down
15 changes: 15 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,21 @@ fn test_boxed_raw_value() {
assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string);
}

#[cfg(feature = "raw_value")]
#[test]
fn test_lost_raw_value() {
let value = Value::Object({
let mut value = serde_json::Map::default();
value.insert(
"$serde_json::private::RawValue".into(),
Value::String("0".into()),
);
value
});

assert_eq!(value.to_string(), "0");
}

#[cfg(feature = "raw_value")]
#[test]
fn test_raw_invalid_utf8() {
Expand Down

0 comments on commit 049e7ee

Please sign in to comment.