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

Change serde_json::from_value to use pass-by-reference #1128

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ impl Display for Value {
}
}

impl AsRef<Value> for Value {
fn as_ref(&self) -> &Value {
self
}
}

fn parse_index(s: &str) -> Option<usize> {
if s.starts_with('+') || (s.starts_with('0') && s.len() != 1) {
return None;
Expand Down Expand Up @@ -987,8 +993,13 @@ where
/// "location": "Menlo Park, CA"
/// });
///
/// let u: User = serde_json::from_value(j).unwrap();
/// // from_value accepts both reference ...
/// let u: User = serde_json::from_value(&j).unwrap();
/// println!("{:#?}", u);
///
/// // ... and moved value
/// let u2: User = serde_json::from_value(j).unwrap();
/// println!("{:#?}", u2);
/// }
/// ```
///
Expand All @@ -1001,9 +1012,9 @@ where
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
pub fn from_value<T>(value: Value) -> Result<T, Error>
pub fn from_value<T, V: AsRef<Value>>(value: V) -> Result<T, Error>
where
T: DeserializeOwned,
{
T::deserialize(value)
T::deserialize(value.as_ref())
}
2 changes: 1 addition & 1 deletion tests/regression/issue520.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum E {
fn test() {
let e = E::Float(159.1);
let v = serde_json::to_value(e).unwrap();
let e = serde_json::from_value::<E>(v).unwrap();
let e = serde_json::from_value::<E, serde_json::Value>(v).unwrap();

match e {
E::Float(f) => assert_eq!(f, 159.1),
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/issue795.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ fn test() {
assert!(serde_json::from_str::<Enum>(s).is_err());

let j = json!({"Variant":{"x":0,"y":0}});
assert!(serde_json::from_value::<Enum>(j).is_err());
assert!(serde_json::from_value::<Enum, serde_json::Value>(j).is_err());
}
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,13 +1899,13 @@ fn test_integer_key() {
(r#"{"123 ":null}"#, "expected `\"` at line 1 column 6"),
]);

let err = from_value::<BTreeMap<i32, ()>>(json!({" 123":null})).unwrap_err();
let err = from_value::<BTreeMap<i32, ()>, Value>(json!({" 123":null})).unwrap_err();
assert_eq!(
err.to_string(),
"invalid value: expected key to be a number in quotes",
);

let err = from_value::<BTreeMap<i32, ()>>(json!({"123 ":null})).unwrap_err();
let err = from_value::<BTreeMap<i32, ()>, Value>(json!({"123 ":null})).unwrap_err();
assert_eq!(
err.to_string(),
"invalid value: expected key to be a number in quotes",
Expand Down