cosmwasm_std/
serde.rs

1// This file simply re-exports some methods from serde_json
2// The reason is two fold:
3// 1. To easily ensure that all calling libraries use the same version (minimize code size)
4// 2. To allow us to switch out to eg. serde-json-core more easily
5
6use core::any::type_name;
7use serde::{de::DeserializeOwned, Serialize};
8
9use crate::Binary;
10use crate::{StdError, StdResult};
11
12#[deprecated = "use from_json instead"]
13pub fn from_slice<T: DeserializeOwned>(value: &[u8]) -> StdResult<T> {
14    from_json(value)
15}
16
17#[deprecated = "use from_json instead"]
18pub fn from_binary<T: DeserializeOwned>(value: &Binary) -> StdResult<T> {
19    from_json(value)
20}
21
22/// Deserializes the given JSON bytes to a data structure.
23///
24/// Errors if the input is not valid JSON or cannot be deserialized to the given type.
25pub fn from_json<T: DeserializeOwned>(value: impl AsRef<[u8]>) -> StdResult<T> {
26    serde_json_wasm::from_slice(value.as_ref())
27        .map_err(|e| StdError::parse_err(type_name::<T>(), e))
28}
29
30#[deprecated = "use to_json_vec instead"]
31pub fn to_vec<T>(data: &T) -> StdResult<Vec<u8>>
32where
33    T: Serialize + ?Sized,
34{
35    to_json_vec(data)
36}
37
38#[deprecated = "use to_json_binary instead"]
39pub fn to_binary<T>(data: &T) -> StdResult<Binary>
40where
41    T: Serialize + ?Sized,
42{
43    to_json_binary(data)
44}
45
46/// Serializes the given data structure as a JSON byte vector.
47pub fn to_json_vec<T>(data: &T) -> StdResult<Vec<u8>>
48where
49    T: Serialize + ?Sized,
50{
51    serde_json_wasm::to_vec(data).map_err(|e| StdError::serialize_err(type_name::<T>(), e))
52}
53
54/// Serializes the given data structure as a JSON string.
55pub fn to_json_string<T>(data: &T) -> StdResult<String>
56where
57    T: Serialize + ?Sized,
58{
59    serde_json_wasm::to_string(data).map_err(|e| StdError::serialize_err(type_name::<T>(), e))
60}
61
62/// Serializes the given data structure as JSON bytes.
63pub fn to_json_binary<T>(data: &T) -> StdResult<Binary>
64where
65    T: Serialize + ?Sized,
66{
67    to_json_vec(data).map(Binary::new)
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use core::num::{
74        NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
75        NonZeroU32, NonZeroU64, NonZeroU8,
76    };
77    use proptest::{prop_assert_eq, property_test};
78    use serde::Deserialize;
79
80    use crate::msgpack::{from_msgpack, to_msgpack_vec};
81
82    #[derive(Serialize, Deserialize, Debug, PartialEq)]
83    #[serde(rename_all = "snake_case")]
84    enum SomeMsg {
85        Refund {},
86        ReleaseAll {
87            image: String,
88            amount: u32,
89            time: u64,
90            karma: i32,
91        },
92        Cowsay {
93            text: String,
94        },
95    }
96
97    #[test]
98    fn to_json_vec_works() {
99        let msg = SomeMsg::Refund {};
100        let serialized = to_json_vec(&msg).unwrap();
101        assert_eq!(serialized, br#"{"refund":{}}"#);
102
103        let msg = SomeMsg::ReleaseAll {
104            image: "foo".to_string(),
105            amount: 42,
106            time: 9007199254740999, // Number.MAX_SAFE_INTEGER + 7
107            karma: -17,
108        };
109        let serialized = String::from_utf8(to_json_vec(&msg).unwrap()).unwrap();
110        assert_eq!(
111            serialized,
112            r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
113        );
114    }
115
116    #[test]
117    fn from_json_works() {
118        let deserialized: SomeMsg = from_json(br#"{"refund":{}}"#).unwrap();
119        assert_eq!(deserialized, SomeMsg::Refund {});
120
121        let expected = SomeMsg::ReleaseAll {
122            image: "foo".to_string(),
123            amount: 42,
124            time: 18446744073709551615,
125            karma: -17,
126        };
127        // &[u8]
128        let deserialized: SomeMsg = from_json(
129            br#"{"release_all":{"image":"foo","amount":42,"time":18446744073709551615,"karma":-17}}"#,
130        )
131        .unwrap();
132        assert_eq!(deserialized, expected);
133
134        // &str
135        let deserialized: SomeMsg = from_json(
136            r#"{"release_all":{"image":"foo","amount":42,"time":18446744073709551615,"karma":-17}}"#,
137        )
138        .unwrap();
139        assert_eq!(deserialized, expected);
140    }
141
142    #[test]
143    fn from_json_or_binary() {
144        let msg = SomeMsg::Refund {};
145        let serialized: Binary = to_json_binary(&msg).unwrap();
146
147        let parse_binary: SomeMsg = from_json(&serialized).unwrap();
148        assert_eq!(parse_binary, msg);
149
150        let parse_slice: SomeMsg = from_json(serialized.as_slice()).unwrap();
151        assert_eq!(parse_slice, msg);
152    }
153
154    #[test]
155    fn to_json_vec_works_for_special_chars() {
156        let msg = SomeMsg::Cowsay {
157            text: "foo\"bar\\\"bla".to_string(),
158        };
159        let serialized = String::from_utf8(to_json_vec(&msg).unwrap()).unwrap();
160        assert_eq!(serialized, r#"{"cowsay":{"text":"foo\"bar\\\"bla"}}"#);
161    }
162
163    #[test]
164    fn from_json_works_for_special_chars() {
165        let deserialized: SomeMsg = from_json(br#"{"cowsay":{"text":"foo\"bar\\\"bla"}}"#).unwrap();
166        assert_eq!(
167            deserialized,
168            SomeMsg::Cowsay {
169                text: "foo\"bar\\\"bla".to_string(),
170            }
171        );
172    }
173
174    #[test]
175    fn to_json_string_works() {
176        let msg = SomeMsg::Refund {};
177        let serialized = to_json_string(&msg).unwrap();
178        assert_eq!(serialized, r#"{"refund":{}}"#);
179
180        let msg = SomeMsg::ReleaseAll {
181            image: "foo".to_string(),
182            amount: 42,
183            time: 9007199254740999, // Number.MAX_SAFE_INTEGER + 7
184            karma: -17,
185        };
186        let serialized = to_json_string(&msg).unwrap();
187        assert_eq!(
188            serialized,
189            r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
190        );
191    }
192
193    macro_rules! test_integer {
194        ($($ty:ty),+$(,)?) => {
195            $(
196                ::paste::paste! {
197                    #[property_test]
198                    fn [<test_ $ty:snake:lower _encoding>](input: $ty) {
199                        let primitive = input.get();
200
201                        // Verify that the serialization is the same as the primitive
202                        let serialized = to_json_string(&input).unwrap();
203                        let serialized_primitive = to_json_string(&primitive).unwrap();
204                        prop_assert_eq!(serialized.as_str(), serialized_primitive.as_str());
205
206                        // Verify that the serialized primitive can be deserialized
207                        let deserialized: $ty = from_json(serialized_primitive).unwrap();
208                        assert_eq!(deserialized, input);
209
210                        // Verify that zero is not allowed
211                        assert!(from_json::<$ty>("0").is_err());
212
213                        // Verify that the msgpack encoding is the same as the primitive
214                        let serialized = to_msgpack_vec(&input).unwrap();
215                        let serialized_primitive = to_msgpack_vec(&primitive).unwrap();
216                        prop_assert_eq!(serialized.as_slice(), serialized_primitive.as_slice());
217
218                        // Verify that the serialized primitive can be deserialized
219                        let deserialized: $ty = from_msgpack(&serialized_primitive).unwrap();
220                        prop_assert_eq!(deserialized, input);
221                    }
222                }
223            )+
224        };
225    }
226
227    test_integer! {
228        NonZeroU8,
229        NonZeroU16,
230        NonZeroU32,
231        NonZeroU64,
232        NonZeroU128,
233    }
234
235    test_integer! {
236        NonZeroI8,
237        NonZeroI16,
238        NonZeroI32,
239        NonZeroI64,
240        NonZeroI128,
241    }
242}