air_interpreter_value/value/
ser.rs

1/*
2 * Copyright 2024 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * This file is based on serde_json crate by Erick Tryzelaar and David Tolnay
19 * licensed under conditions of MIT License and Apache License, Version 2.0.
20 */
21
22use crate::value::JValue;
23use core::result;
24use serde::ser::Serialize;
25
26impl Serialize for JValue {
27    #[inline]
28    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
29    where
30        S: ::serde::Serializer,
31    {
32        match self {
33            JValue::Null => serializer.serialize_unit(),
34            JValue::Bool(b) => serializer.serialize_bool(*b),
35            JValue::Number(n) => n.serialize(serializer),
36            JValue::String(s) => serializer.serialize_str(s),
37            JValue::Array(v) => v.serialize(serializer),
38            JValue::Object(m) => {
39                use serde::ser::SerializeMap;
40                let mut map = tri!(serializer.serialize_map(Some(m.len())));
41                for (k, v) in &**m {
42                    tri!(map.serialize_entry(k, v));
43                }
44                map.end()
45            }
46        }
47    }
48}