Debian Codegraph @

third_party/rust/serde_with/src/json.rs

raw
1 //! De/Serialization of JSON
2 //!
3 //! This modules is only available when using the `json` feature of the crate.
4
5 use crate::prelude::*;
6
7 /// Serialize value as string containing JSON
8 ///
9 /// *Note*: This type is not necessary for normal usage of serde with JSON.
10 /// It is only required if the serialized format contains a string, which itself contains JSON.
11 ///
12 /// # Errors
13 ///
14 /// Serialization can fail if `T`'s implementation of `Serialize` decides to
15 /// fail, or if `T` contains a map with non-string keys.
16 ///
17 /// # Examples
18 ///
19 /// ```
20 /// # #[cfg(feature = "macros")] {
21 /// # use serde::{Deserialize, Serialize};
22 /// # use serde_with::{serde_as, json::JsonString};
23 /// #
24 /// #[serde_as]
25 /// #[derive(Deserialize, Serialize)]
26 /// struct A {
27 /// #[serde_as(as = "JsonString")]
28 /// other_struct: B,
29 /// }
30 /// #[derive(Deserialize, Serialize)]
31 /// struct B {
32 /// value: usize,
33 /// }
34 ///
35 /// let v: A = serde_json::from_str(r#"{"other_struct":"{\"value\":5}"}"#).unwrap();
36 /// assert_eq!(5, v.other_struct.value);
37 ///
38 /// let x = A {
39 /// other_struct: B { value: 10 },
40 /// };
41 /// assert_eq!(
42 /// r#"{"other_struct":"{\"value\":10}"}"#,
43 /// serde_json::to_string(&x).unwrap()
44 /// );
45 /// # }
46 /// ```
47 ///
48 /// The `JsonString` converter takes a type argument, which allows altering the serialization behavior of the inner value, before it gets turned into a JSON string.
49 ///
50 /// ```
51 /// # #[cfg(feature = "macros")] {
52 /// # use serde::{Deserialize, Serialize};
53 /// # use serde_with::{serde_as, json::JsonString};
54 /// # use std::collections::BTreeMap;
55 /// #
56 /// #[serde_as]
57 /// #[derive(Debug, Serialize, Deserialize, PartialEq)]
58 /// struct Struct {
59 /// #[serde_as(as = "JsonString<Vec<(JsonString, _)>>")]
60 /// value: BTreeMap<[u8; 2], u32>,
61 /// }
62 ///
63 /// let value = Struct {
64 /// value: BTreeMap::from([([1, 2], 3), ([4, 5], 6)]),
65 /// };
66 /// assert_eq!(
67 /// r#"{"value":"[[\"[1,2]\",3],[\"[4,5]\",6]]"}"#,
68 /// serde_json::to_string(&value).unwrap()
69 /// );
70 /// # }
71 /// ```
72 pub struct JsonString<T = Same>(PhantomData<T>);
73
74 impl<T, TAs> SerializeAs<T> for JsonString<TAs>
75 where
76 TAs: SerializeAs<T>,
77 {
78 fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
79 where
80 S: Serializer,
81 {
82 serializer.serialize_str(
83 &serde_json::to_string(&SerializeAsWrap::<T, TAs>::new(source))
84 .map_err(SerError::custom)?,
85 )
86 }
87 }
88
89 impl<'de, T, TAs> DeserializeAs<'de, T> for JsonString<TAs>
90 where
91 TAs: for<'a> DeserializeAs<'a, T>,
92 {
93 fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
94 where
95 D: Deserializer<'de>,
96 {
97 struct Helper<S, SAs>(PhantomData<(S, SAs)>);
98
99 impl<S, SAs> Visitor<'_> for Helper<S, SAs>
100 where
101 SAs: for<'a> DeserializeAs<'a, S>,
102 {
103 type Value = S;
104
105 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
106 formatter.write_str("valid json object")
107 }
108
109 fn visit_str<E>(self, value: &str) -> Result<S, E>
110 where
111 E: DeError,
112 {
113 serde_json::from_str(value)
114 .map(DeserializeAsWrap::<S, SAs>::into_inner)
115 .map_err(DeError::custom)
116 }
117 }
118
119 deserializer.deserialize_str(Helper::<T, TAs>(PhantomData))
120 }
121 }