Skip to content

Commit 1e334be

Browse files
Implemented from_json for InputValue.
1 parent 494628a commit 1e334be

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/integrations/serde.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
use serde::{de, ser};
22
use serde::ser::SerializeMap;
3-
use std::collections::HashMap;
43
use std::fmt;
4+
use std::collections::HashMap;
55

66
use ::{GraphQLError, Value};
77
use ast::InputValue;
88
use executor::ExecutionError;
99
use parser::{ParseError, Spanning, SourcePosition};
1010
use validation::RuleError;
11+
use serde_json::Value as Json;
12+
13+
14+
impl InputValue {
15+
/// Converts serde_json::Value to juniper::InputValue
16+
pub fn from_json(json: Json) -> InputValue {
17+
match json {
18+
Json::Number(num) => {
19+
if let Some(number) = num.as_i64() {
20+
InputValue::int(number)
21+
}
22+
else if let Some(number) = num.as_f64() {
23+
InputValue::float(number)
24+
}
25+
else if let Some(number) = num.as_u64() {
26+
InputValue::float(number as f64)
27+
}
28+
else {
29+
panic!("Invalid number data type was found.");
30+
}
31+
}
32+
Json::String(s) => InputValue::string(s),
33+
Json::Bool(b) => InputValue::boolean(b),
34+
Json::Array(a) => InputValue::list(a.into_iter().map(InputValue::from_json).collect()),
35+
Json::Object(o) => InputValue::object(o.into_iter().map(|(k, v)| (k, InputValue::from_json(v))).collect()),
36+
Json::Null => InputValue::null(),
37+
}
38+
}
39+
}
1140

1241
impl ser::Serialize for ExecutionError {
1342
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>

0 commit comments

Comments
 (0)