Skip to main content

conjure_serde/json/de/
server.rs

1// Copyright 2018 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use crate::de::null_collections_behavior::NullCollectionsBehavior;
15use crate::de::unknown_fields_behavior::UnknownFieldsBehavior;
16use crate::json::de::client::ValueBehavior;
17use serde::de;
18use serde_json::de::{IoRead, Read, SliceRead, StrRead};
19use serde_json::Error;
20use std::io;
21
22/// Deserializes a value from a reader of JSON data.
23pub fn server_from_reader<R, T>(reader: R) -> Result<T, Error>
24where
25    R: io::Read,
26    T: de::DeserializeOwned,
27{
28    let mut de = ServerDeserializer::from_reader(reader);
29    let value = T::deserialize(&mut de)?;
30    de.end()?;
31    Ok(value)
32}
33
34/// Deserializes a value from a string of JSON data.
35pub fn server_from_str<'a, T>(s: &'a str) -> Result<T, Error>
36where
37    T: de::Deserialize<'a>,
38{
39    let mut de = ServerDeserializer::from_str(s);
40    let value = T::deserialize(&mut de)?;
41    de.end()?;
42    Ok(value)
43}
44
45/// Deserializes a value from a slice of JSON data.
46pub fn server_from_slice<'a, T>(s: &'a [u8]) -> Result<T, Error>
47where
48    T: de::Deserialize<'a>,
49{
50    let mut de = ServerDeserializer::from_slice(s);
51    let value = T::deserialize(&mut de)?;
52    de.end()?;
53    Ok(value)
54}
55
56/// A serde JSON deserializer appropriate for use by Conjure servers.
57///
58/// In contrast to serde_json, the f32 and f64 types can be deserialized from the strings `"Infinity"`, `"-Infinity"`,
59/// and `"NaN"`, and bytes are deserialized from base64 encoded strings. Unknown object fields trigger errors.
60pub struct ServerDeserializer<R>(serde_json::Deserializer<R>);
61
62impl<R> ServerDeserializer<IoRead<R>>
63where
64    R: io::Read,
65{
66    /// Creates a Conjure JSON server deserializer from an `io::Read`.
67    pub fn from_reader(reader: R) -> ServerDeserializer<IoRead<R>> {
68        ServerDeserializer(serde_json::Deserializer::from_reader(reader))
69    }
70}
71
72impl<'a> ServerDeserializer<SliceRead<'a>> {
73    /// Creates a Conjure JSON server deserializer from a `&[u8]`.
74    pub fn from_slice(bytes: &'a [u8]) -> ServerDeserializer<SliceRead<'a>> {
75        ServerDeserializer(serde_json::Deserializer::from_slice(bytes))
76    }
77}
78
79impl<'a> ServerDeserializer<StrRead<'a>> {
80    /// Creates a Conjure JSON server deserializer from a `&str`.
81    #[allow(clippy::should_implement_trait)] // match serde_json's API
82    pub fn from_str(s: &'a str) -> ServerDeserializer<StrRead<'a>> {
83        ServerDeserializer(serde_json::Deserializer::from_str(s))
84    }
85}
86
87impl<'de, R> ServerDeserializer<R>
88where
89    R: Read<'de>,
90{
91    /// Validates that the input stream is at the end or that it only has trailing whitespace.
92    pub fn end(&mut self) -> Result<(), Error> {
93        self.0.end()
94    }
95}
96
97impl<'a, 'de, R> de::Deserializer<'de> for &'a mut ServerDeserializer<R>
98where
99    R: Read<'de>,
100{
101    impl_deserialize_body!(
102        &'a mut serde_json::Deserializer<R>,
103        UnknownFieldsBehavior<NullCollectionsBehavior<ValueBehavior>>
104    );
105
106    // we can't delegate this due to the signature, but luckily we know the answer
107    fn is_human_readable(&self) -> bool {
108        true
109    }
110}