cosmwasm_std/
serde.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// This file simply re-exports some methods from serde_json
// The reason is two fold:
// 1. To easily ensure that all calling libraries use the same version (minimize code size)
// 2. To allow us to switch out to eg. serde-json-core more easily

use core::any::type_name;
use serde::{de::DeserializeOwned, Serialize};

use crate::Binary;
use crate::{StdError, StdResult};

#[deprecated = "use from_json instead"]
pub fn from_slice<T: DeserializeOwned>(value: &[u8]) -> StdResult<T> {
    from_json(value)
}

#[deprecated = "use from_json instead"]
pub fn from_binary<T: DeserializeOwned>(value: &Binary) -> StdResult<T> {
    from_json(value)
}

/// Deserializes the given JSON bytes to a data structure.
///
/// Errors if the input is not valid JSON or cannot be deserialized to the given type.
pub fn from_json<T: DeserializeOwned>(value: impl AsRef<[u8]>) -> StdResult<T> {
    serde_json_wasm::from_slice(value.as_ref())
        .map_err(|e| StdError::parse_err(type_name::<T>(), e))
}

#[deprecated = "use to_json_vec instead"]
pub fn to_vec<T>(data: &T) -> StdResult<Vec<u8>>
where
    T: Serialize + ?Sized,
{
    to_json_vec(data)
}

#[deprecated = "use to_json_binary instead"]
pub fn to_binary<T>(data: &T) -> StdResult<Binary>
where
    T: Serialize + ?Sized,
{
    to_json_binary(data)
}

/// Serializes the given data structure as a JSON byte vector.
pub fn to_json_vec<T>(data: &T) -> StdResult<Vec<u8>>
where
    T: Serialize + ?Sized,
{
    serde_json_wasm::to_vec(data).map_err(|e| StdError::serialize_err(type_name::<T>(), e))
}

/// Serializes the given data structure as a JSON string.
pub fn to_json_string<T>(data: &T) -> StdResult<String>
where
    T: Serialize + ?Sized,
{
    serde_json_wasm::to_string(data).map_err(|e| StdError::serialize_err(type_name::<T>(), e))
}

/// Serializes the given data structure as JSON bytes.
pub fn to_json_binary<T>(data: &T) -> StdResult<Binary>
where
    T: Serialize + ?Sized,
{
    to_json_vec(data).map(Binary::new)
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::num::{
        NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
        NonZeroU32, NonZeroU64, NonZeroU8,
    };
    use proptest::{prop_assert_eq, property_test};
    use serde::Deserialize;

    use crate::msgpack::{from_msgpack, to_msgpack_vec};

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    #[serde(rename_all = "snake_case")]
    enum SomeMsg {
        Refund {},
        ReleaseAll {
            image: String,
            amount: u32,
            time: u64,
            karma: i32,
        },
        Cowsay {
            text: String,
        },
    }

    #[test]
    fn to_json_vec_works() {
        let msg = SomeMsg::Refund {};
        let serialized = to_json_vec(&msg).unwrap();
        assert_eq!(serialized, br#"{"refund":{}}"#);

        let msg = SomeMsg::ReleaseAll {
            image: "foo".to_string(),
            amount: 42,
            time: 9007199254740999, // Number.MAX_SAFE_INTEGER + 7
            karma: -17,
        };
        let serialized = String::from_utf8(to_json_vec(&msg).unwrap()).unwrap();
        assert_eq!(
            serialized,
            r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
        );
    }

    #[test]
    fn from_json_works() {
        let deserialized: SomeMsg = from_json(br#"{"refund":{}}"#).unwrap();
        assert_eq!(deserialized, SomeMsg::Refund {});

        let expected = SomeMsg::ReleaseAll {
            image: "foo".to_string(),
            amount: 42,
            time: 18446744073709551615,
            karma: -17,
        };
        // &[u8]
        let deserialized: SomeMsg = from_json(
            br#"{"release_all":{"image":"foo","amount":42,"time":18446744073709551615,"karma":-17}}"#,
        )
        .unwrap();
        assert_eq!(deserialized, expected);

        // &str
        let deserialized: SomeMsg = from_json(
            r#"{"release_all":{"image":"foo","amount":42,"time":18446744073709551615,"karma":-17}}"#,
        )
        .unwrap();
        assert_eq!(deserialized, expected);
    }

    #[test]
    fn from_json_or_binary() {
        let msg = SomeMsg::Refund {};
        let serialized: Binary = to_json_binary(&msg).unwrap();

        let parse_binary: SomeMsg = from_json(&serialized).unwrap();
        assert_eq!(parse_binary, msg);

        let parse_slice: SomeMsg = from_json(serialized.as_slice()).unwrap();
        assert_eq!(parse_slice, msg);
    }

    #[test]
    fn to_json_vec_works_for_special_chars() {
        let msg = SomeMsg::Cowsay {
            text: "foo\"bar\\\"bla".to_string(),
        };
        let serialized = String::from_utf8(to_json_vec(&msg).unwrap()).unwrap();
        assert_eq!(serialized, r#"{"cowsay":{"text":"foo\"bar\\\"bla"}}"#);
    }

    #[test]
    fn from_json_works_for_special_chars() {
        let deserialized: SomeMsg = from_json(br#"{"cowsay":{"text":"foo\"bar\\\"bla"}}"#).unwrap();
        assert_eq!(
            deserialized,
            SomeMsg::Cowsay {
                text: "foo\"bar\\\"bla".to_string(),
            }
        );
    }

    #[test]
    fn to_json_string_works() {
        let msg = SomeMsg::Refund {};
        let serialized = to_json_string(&msg).unwrap();
        assert_eq!(serialized, r#"{"refund":{}}"#);

        let msg = SomeMsg::ReleaseAll {
            image: "foo".to_string(),
            amount: 42,
            time: 9007199254740999, // Number.MAX_SAFE_INTEGER + 7
            karma: -17,
        };
        let serialized = to_json_string(&msg).unwrap();
        assert_eq!(
            serialized,
            r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
        );
    }

    macro_rules! test_integer {
        ($($ty:ty),+$(,)?) => {
            $(
                ::paste::paste! {
                    #[property_test]
                    fn [<test_ $ty:snake:lower _encoding>](input: $ty) {
                        let primitive = input.get();

                        // Verify that the serialization is the same as the primitive
                        let serialized = to_json_string(&input).unwrap();
                        let serialized_primitive = to_json_string(&primitive).unwrap();
                        prop_assert_eq!(serialized.as_str(), serialized_primitive.as_str());

                        // Verify that the serialized primitive can be deserialized
                        let deserialized: $ty = from_json(serialized_primitive).unwrap();
                        assert_eq!(deserialized, input);

                        // Verify that zero is not allowed
                        assert!(from_json::<$ty>("0").is_err());

                        // Verify that the msgpack encoding is the same as the primitive
                        let serialized = to_msgpack_vec(&input).unwrap();
                        let serialized_primitive = to_msgpack_vec(&primitive).unwrap();
                        prop_assert_eq!(serialized.as_slice(), serialized_primitive.as_slice());

                        // Verify that the serialized primitive can be deserialized
                        let deserialized: $ty = from_msgpack(&serialized_primitive).unwrap();
                        prop_assert_eq!(deserialized, input);
                    }
                }
            )+
        };
    }

    test_integer! {
        NonZeroU8,
        NonZeroU16,
        NonZeroU32,
        NonZeroU64,
        NonZeroU128,
    }

    test_integer! {
        NonZeroI8,
        NonZeroI16,
        NonZeroI32,
        NonZeroI64,
        NonZeroI128,
    }
}