Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover raw values after losing the type #1148

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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