json_syntax/convert/
serde_json.rs1use crate::{object::Entry, Value};
2
3impl Value {
4 pub fn from_serde_json(value: serde_json::Value) -> Self {
22 match value {
23 serde_json::Value::Null => Self::Null,
24 serde_json::Value::Bool(b) => Self::Boolean(b),
25 serde_json::Value::Number(n) => Self::Number(n.into()),
26 serde_json::Value::String(s) => Self::String(s.into()),
27 serde_json::Value::Array(a) => {
28 Self::Array(a.into_iter().map(Self::from_serde_json).collect())
29 }
30 serde_json::Value::Object(o) => Self::Object(
31 o.into_iter()
32 .map(|(k, v)| Entry::new(k.into(), Self::from_serde_json(v)))
33 .collect(),
34 ),
35 }
36 }
37
38 pub fn into_serde_json(self) -> serde_json::Value {
56 match self {
57 Self::Null => serde_json::Value::Null,
58 Self::Boolean(b) => serde_json::Value::Bool(b),
59 Self::Number(n) => serde_json::Value::Number(n.into()),
60 Self::String(s) => serde_json::Value::String(s.into_string()),
61 Self::Array(a) => {
62 serde_json::Value::Array(a.into_iter().map(Value::into_serde_json).collect())
63 }
64 Self::Object(o) => serde_json::Value::Object(
65 o.into_iter()
66 .map(|Entry { key, value }| (key.into_string(), Value::into_serde_json(value)))
67 .collect(),
68 ),
69 }
70 }
71}
72
73impl From<serde_json::Value> for Value {
74 #[inline(always)]
75 fn from(value: serde_json::Value) -> Self {
76 Self::from_serde_json(value)
77 }
78}
79
80impl From<Value> for serde_json::Value {
81 fn from(value: Value) -> Self {
82 value.into_serde_json()
83 }
84}