syn_serde/json.rs
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! A module to provide functions for JSON <-> Rust serialize and deserialize.
4
5use std::io;
6
7use serde_json::Result;
8
9use super::*;
10
11// Serialize [`Syn`] type into JSON data.
12
13/// Serialize the given [`Syn`] type as JSON into the I/O stream.
14///
15/// This function is equivalent to the following code:
16///
17/// ```rust
18/// # use std::io;
19/// # fn to_writer<W>(writer: W, syn_file: &syn::File) -> serde_json::Result<()>
20/// # where
21/// # W: io::Write,
22/// # {
23/// use syn_serde::Syn;
24///
25/// let adapter = syn_file.to_adapter();
26/// serde_json::to_writer(writer, &adapter)
27/// # }
28/// ```
29pub fn to_writer<S, W>(writer: W, syn: &S) -> Result<()>
30where
31 S: Syn,
32 W: io::Write,
33{
34 let adapter = syn.to_adapter();
35 serde_json::to_writer(writer, &adapter)
36}
37
38/// Serialize the given [`Syn`] type as pretty-printed JSON into the IO
39/// stream.
40///
41/// This function is equivalent to the following code:
42///
43/// ```rust
44/// # use std::io;
45/// # fn to_writer_pretty<W: io::Write>(writer: W, syn_file: &syn::File) -> serde_json::Result<()> {
46/// use syn_serde::Syn;
47///
48/// let adapter = syn_file.to_adapter();
49/// serde_json::to_writer_pretty(writer, &adapter)
50/// # }
51/// ```
52pub fn to_writer_pretty<S, W>(writer: W, syn: &S) -> Result<()>
53where
54 S: Syn,
55 W: io::Write,
56{
57 let adapter = syn.to_adapter();
58 serde_json::to_writer_pretty(writer, &adapter)
59}
60
61/// Serialize the given [`Syn`] type as a JSON byte vector.
62///
63/// This function is equivalent to the following code:
64///
65/// ```rust
66/// # fn to_vec(syn_file: &syn::File) -> Vec<u8> {
67/// use syn_serde::Syn;
68///
69/// let adapter = syn_file.to_adapter();
70/// serde_json::to_vec(&adapter).unwrap()
71/// # }
72/// ```
73// All of the data structures in syn-serde are compatible with JSON so unwrap will never fail.
74#[allow(clippy::missing_panics_doc)]
75pub fn to_vec<S>(syn: &S) -> Vec<u8>
76where
77 S: Syn,
78{
79 let adapter = syn.to_adapter();
80 serde_json::to_vec(&adapter).unwrap()
81}
82
83/// Serialize the given [`Syn`] type as a pretty-printed JSON byte vector.
84///
85/// This function is equivalent to the following code:
86///
87/// ```rust
88/// # fn to_vec_pretty(syn_file: &syn::File) -> Vec<u8> {
89/// use syn_serde::Syn;
90///
91/// let adapter = syn_file.to_adapter();
92/// serde_json::to_vec_pretty(&adapter).unwrap()
93/// # }
94/// ```
95// All of the data structures in syn-serde are compatible with JSON so unwrap will never fail.
96#[allow(clippy::missing_panics_doc)]
97pub fn to_vec_pretty<S>(syn: &S) -> Vec<u8>
98where
99 S: Syn,
100{
101 let adapter = syn.to_adapter();
102 serde_json::to_vec_pretty(&adapter).unwrap()
103}
104
105/// Serialize the given [`Syn`] type as a String of JSON.
106///
107/// This function is equivalent to the following code:
108///
109/// ```rust
110/// # fn to_string(syn_file: &syn::File) -> String {
111/// use syn_serde::Syn;
112///
113/// let adapter = syn_file.to_adapter();
114/// serde_json::to_string(&adapter).unwrap()
115/// # }
116/// ```
117// All of the data structures in syn-serde are compatible with JSON so unwrap will never fail.
118#[allow(clippy::missing_panics_doc)]
119pub fn to_string<S>(syn: &S) -> String
120where
121 S: Syn,
122{
123 let adapter = syn.to_adapter();
124 serde_json::to_string(&adapter).unwrap()
125}
126
127/// Serialize the given [`Syn`] type as a pretty-printed String of JSON.
128///
129/// This function is equivalent to the following code:
130///
131/// ```rust
132/// # fn to_string_pretty(syn_file: &syn::File) -> String {
133/// use syn_serde::Syn;
134///
135/// let adapter = syn_file.to_adapter();
136/// serde_json::to_string_pretty(&adapter).unwrap()
137/// # }
138/// ```
139// All of the data structures in syn-serde are compatible with JSON so unwrap will never fail.
140#[allow(clippy::missing_panics_doc)]
141pub fn to_string_pretty<S>(syn: &S) -> String
142where
143 S: Syn,
144{
145 let adapter = syn.to_adapter();
146 serde_json::to_string_pretty(&adapter).unwrap()
147}
148
149// Deserialize JSON data to [`Syn`] type.
150
151/// Deserialize an instance of [`Syn`] type from an I/O stream of JSON.
152///
153/// This function is equivalent to the following code:
154///
155/// ```rust
156/// # use std::io;
157/// # fn from_reader<R: io::Read>(reader: R) -> serde_json::Result<syn::File> {
158/// use syn_serde::Syn;
159///
160/// let adapter: <syn::File as Syn>::Adapter = serde_json::from_reader(reader)?;
161/// let syn_file = syn::File::from_adapter(&adapter);
162/// Ok(syn_file)
163/// # }
164/// ```
165pub fn from_reader<S, R>(reader: R) -> Result<S>
166where
167 S: Syn,
168 R: io::Read,
169{
170 let adapter: S::Adapter = serde_json::from_reader(reader)?;
171 Ok(S::from_adapter(&adapter))
172}
173
174/// Deserialize an instance of [`Syn`] type from bytes of JSON text.
175///
176/// This function is equivalent to the following code:
177///
178/// ```rust
179/// # fn from_reader(v: &[u8]) -> serde_json::Result<syn::File> {
180/// use syn_serde::Syn;
181///
182/// let adapter: <syn::File as Syn>::Adapter = serde_json::from_slice(v)?;
183/// let syn_file = syn::File::from_adapter(&adapter);
184/// Ok(syn_file)
185/// # }
186/// ```
187pub fn from_slice<S>(v: &[u8]) -> Result<S>
188where
189 S: Syn,
190{
191 let adapter: S::Adapter = serde_json::from_slice(v)?;
192 Ok(S::from_adapter(&adapter))
193}
194
195/// Deserialize an instance of [`Syn`] type from a string of JSON text.
196///
197/// This function is equivalent to the following code:
198///
199/// ```rust
200/// # fn from_str(s: &str) -> serde_json::Result<syn::File> {
201/// use syn_serde::Syn;
202///
203/// let adapter: <syn::File as Syn>::Adapter = serde_json::from_str(s)?;
204/// let syn_file = syn::File::from_adapter(&adapter);
205/// Ok(syn_file)
206/// # }
207/// ```
208pub fn from_str<S>(s: &str) -> Result<S>
209where
210 S: Syn,
211{
212 let adapter: S::Adapter = serde_json::from_str(s)?;
213 Ok(S::from_adapter(&adapter))
214}