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 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ where
/// "location": "Menlo Park, CA"
/// });
///
/// let u: User = serde_json::from_value(j).unwrap();
/// let u: User = serde_json::from_value(&j).unwrap();
/// println!("{:#?}", u);
/// }
/// ```
Expand All @@ -1001,7 +1001,7 @@ 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>(value: &Value) -> Result<T, Error>
where
T: DeserializeOwned,
{
Expand Down
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>(&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>(&j).is_err());
}
18 changes: 9 additions & 9 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ where
assert_eq!(v, value);

// Make sure we can deserialize from a `Value`.
let v: T = from_value(json_value.clone()).unwrap();
let v: T = from_value(&json_value).unwrap();
assert_eq!(v, value);

// Make sure we can round trip back to `Value`.
let json_value2: Value = from_value(json_value.clone()).unwrap();
let json_value2: Value = from_value(&json_value).unwrap();
assert_eq!(json_value2, json_value);

// Make sure we can fully ignore.
Expand Down Expand Up @@ -1413,10 +1413,10 @@ fn test_missing_option_field() {
let value: Foo = from_str("{\"x\": 5}").unwrap();
assert_eq!(value, Foo { x: Some(5) });

let value: Foo = from_value(json!({})).unwrap();
let value: Foo = from_value(&json!({})).unwrap();
assert_eq!(value, Foo { x: None });

let value: Foo = from_value(json!({"x": 5})).unwrap();
let value: Foo = from_value(&json!({"x": 5})).unwrap();
assert_eq!(value, Foo { x: Some(5) });
}

Expand Down Expand Up @@ -1444,10 +1444,10 @@ fn test_missing_renamed_field() {
let value: Foo = from_str("{\"y\": 5}").unwrap();
assert_eq!(value, Foo { x: Some(5) });

let value: Foo = from_value(json!({})).unwrap();
let value: Foo = from_value(&json!({})).unwrap();
assert_eq!(value, Foo { x: None });

let value: Foo = from_value(json!({"y": 5})).unwrap();
let value: Foo = from_value(&json!({"y": 5})).unwrap();
assert_eq!(value, Foo { x: Some(5) });
}

Expand Down 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, ()>>(&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, ()>>(&json!({"123 ":null})).unwrap_err();
assert_eq!(
err.to_string(),
"invalid value: expected key to be a number in quotes",
Expand Down Expand Up @@ -2385,7 +2385,7 @@ fn test_boxed_raw_value() {
assert_eq!(r#"{"foo": 2}"#, wrapper_from_reader.b.get());

let wrapper_from_value: Wrapper =
serde_json::from_value(json!({"a": 1, "b": {"foo": 2}, "c": 3})).unwrap();
serde_json::from_value(&json!({"a": 1, "b": {"foo": 2}, "c": 3})).unwrap();
assert_eq!(r#"{"foo":2}"#, wrapper_from_value.b.get());

let wrapper_to_string = serde_json::to_string(&wrapper_from_str).unwrap();
Expand Down