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