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 994192a
Show file tree
Hide file tree
Showing 2 changed files with 22 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
14 changes: 14 additions & 0 deletions tests/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ fn test_retain() {
let keys: Vec<_> = val.keys().collect();
assert_eq!(keys, &["a", "c"]);
}

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

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

0 comments on commit 994192a

Please sign in to comment.