json_ld_syntax/
into_json.rs1use crate::{
2 context, Container, ContainerKind, ContextEntry, Direction, Keyword, LenientLangTagBuf,
3 Nullable,
4};
5use contextual::Contextual;
6use indexmap::IndexSet;
7
8impl<T: IntoJsonWithContext<N>, N> IntoJsonWithContext<N> for Vec<T> {
9 fn into_json_with(self, context: &N) -> json_syntax::Value {
10 json_syntax::Value::Array(
11 self.into_iter()
12 .map(|item| item.into_json_with(context))
13 .collect(),
14 )
15 }
16}
17
18impl<T: IntoJsonWithContext<N>, N> IntoJsonWithContext<N> for IndexSet<T> {
19 fn into_json_with(self, context: &N) -> json_syntax::Value {
20 json_syntax::Value::Array(
21 self.into_iter()
22 .map(|item| item.into_json_with(context))
23 .collect(),
24 )
25 }
26}
27
28pub trait IntoJsonWithContext<N>: Sized {
29 fn into_json_with(self, context: &N) -> json_syntax::Value;
30}
31
32pub trait IntoJson: Sized {
33 fn into_json(self) -> json_syntax::Value;
34}
35
36impl<'n, T: IntoJsonWithContext<N>, N> IntoJson for Contextual<T, &'n N> {
37 fn into_json(self) -> json_syntax::Value {
38 T::into_json_with(self.0, self.1)
39 }
40}
41
42impl<'n, T: IntoJsonWithContext<N>, N> IntoJson for Contextual<T, &'n mut N> {
43 fn into_json(self) -> json_syntax::Value {
44 T::into_json_with(self.0, self.1)
45 }
46}
47
48impl IntoJson for bool {
49 fn into_json(self) -> json_syntax::Value {
50 json_syntax::Value::Boolean(self)
51 }
52}
53
54impl<T: IntoJson> IntoJson for Box<T> {
55 fn into_json(self) -> json_syntax::Value {
56 T::into_json(*self)
57 }
58}
59
60impl IntoJson for iref::IriRefBuf {
61 fn into_json(self) -> json_syntax::Value {
62 json_syntax::Value::String(self.as_str().into())
63 }
64}
65
66impl<T: IntoJson> IntoJson for Nullable<T> {
67 fn into_json(self) -> json_syntax::Value {
68 match self {
69 Self::Null => json_syntax::Value::Null,
70 Self::Some(other) => T::into_json(other),
71 }
72 }
73}
74
75impl IntoJson for Keyword {
76 fn into_json(self) -> json_syntax::Value {
77 json_syntax::Value::String(self.into_str().into())
78 }
79}
80
81impl IntoJson for String {
82 fn into_json(self) -> json_syntax::Value {
83 json_syntax::Value::String(self.into())
84 }
85}
86
87impl IntoJson for LenientLangTagBuf {
88 fn into_json(self) -> json_syntax::Value {
89 json_syntax::Value::String(self.into_string().into())
90 }
91}
92
93impl IntoJson for Direction {
94 fn into_json(self) -> json_syntax::Value {
95 json_syntax::Value::String(self.as_str().into())
96 }
97}
98
99impl IntoJson for context::definition::TypeContainer {
100 fn into_json(self) -> json_syntax::Value {
101 json_syntax::Value::String(self.into_str().into())
102 }
103}
104
105impl IntoJson for context::definition::Type {
106 fn into_json(self) -> json_syntax::Value {
107 let mut object = json_syntax::Object::new();
108
109 object.insert("@container".into(), self.container.into_json());
110
111 if let Some(protected) = self.protected {
112 object.insert("@protected".into(), protected.into_json());
113 }
114
115 json_syntax::Value::Object(object)
116 }
117}
118
119impl IntoJson for context::definition::Version {
120 fn into_json(self) -> json_syntax::Value {
121 json_syntax::Value::Number(self.into())
122 }
123}
124
125impl IntoJson for context::definition::Vocab {
126 fn into_json(self) -> json_syntax::Value {
127 json_syntax::Value::String(self.into_string().into())
128 }
129}
130
131impl IntoJson for context::definition::Key {
132 fn into_json(self) -> json_syntax::Value {
133 json_syntax::Value::String(self.into_string().into())
134 }
135}
136
137impl IntoJson for context::term_definition::Index {
138 fn into_json(self) -> json_syntax::Value {
139 json_syntax::Value::String(self.into_string().into())
140 }
141}
142
143impl IntoJson for context::term_definition::Nest {
144 fn into_json(self) -> json_syntax::Value {
145 json_syntax::Value::String(self.into_string().into())
146 }
147}
148
149impl IntoJson for Container {
150 fn into_json(self) -> json_syntax::Value {
151 match self {
152 Self::One(c) => ContainerKind::into_json(c),
153 Self::Many(list) => {
154 json_syntax::Value::Array(list.into_iter().map(IntoJson::into_json).collect())
155 }
156 }
157 }
158}
159
160impl IntoJson for ContainerKind {
161 fn into_json(self) -> json_syntax::Value {
162 json_syntax::Value::String(self.as_str().into())
163 }
164}
165
166impl IntoJson for context::term_definition::Id {
167 fn into_json(self) -> json_syntax::Value {
168 match self {
169 Self::Keyword(k) => Keyword::into_json(k),
170 Self::Term(t) => String::into_json(t),
171 }
172 }
173}
174
175impl IntoJson for context::Context {
176 fn into_json(self) -> json_syntax::Value {
177 match self {
178 Self::One(c) => c.into_json(),
179 Self::Many(list) => {
180 json_syntax::Value::Array(list.into_iter().map(IntoJson::into_json).collect())
181 }
182 }
183 }
184}
185
186impl IntoJson for ContextEntry {
187 fn into_json(self) -> json_syntax::Value {
188 match self {
189 Self::Null => json_syntax::Value::Null,
190 Self::IriRef(iri) => iref::IriRefBuf::into_json(iri),
191 Self::Definition(def) => context::Definition::into_json(def),
192 }
193 }
194}
195
196impl IntoJson for context::Definition {
197 fn into_json(self) -> json_syntax::Value {
198 let mut object = json_syntax::Object::new();
199
200 if let Some(base) = self.base {
201 object.insert("@base".into(), base.into_json());
202 }
203
204 if let Some(import) = self.import {
205 object.insert("@import".into(), import.into_json());
206 }
207
208 if let Some(language) = self.language {
209 object.insert("@language".into(), language.into_json());
210 }
211
212 if let Some(direction) = self.direction {
213 object.insert("@direction".into(), direction.into_json());
214 }
215
216 if let Some(propagate) = self.propagate {
217 object.insert("@propagate".into(), propagate.into_json());
218 }
219
220 if let Some(protected) = self.protected {
221 object.insert("@protected".into(), protected.into_json());
222 }
223
224 if let Some(type_) = self.type_ {
225 object.insert("@type".into(), type_.into_json());
226 }
227
228 if let Some(version) = self.version {
229 object.insert("@version".into(), version.into_json());
230 }
231
232 if let Some(vocab) = self.vocab {
233 object.insert("@vocab".into(), vocab.into_json());
234 }
235
236 for (key, binding) in self.bindings {
237 object.insert(key.into_string().into(), binding.into_json());
238 }
239
240 json_syntax::Value::Object(object)
241 }
242}
243
244impl IntoJson for context::TermDefinition {
245 fn into_json(self) -> json_syntax::Value {
246 match self {
247 Self::Simple(s) => context::term_definition::Simple::into_json(s),
248 Self::Expanded(e) => context::term_definition::Expanded::into_json(*e),
249 }
250 }
251}
252
253impl IntoJson for context::term_definition::Simple {
254 fn into_json(self) -> json_syntax::Value {
255 json_syntax::Value::String(self.into_string().into())
256 }
257}
258
259impl IntoJson for context::term_definition::Type {
260 fn into_json(self) -> json_syntax::Value {
261 json_syntax::Value::String(self.into_string().into())
262 }
263}
264
265impl IntoJson for context::term_definition::TypeKeyword {
266 fn into_json(self) -> json_syntax::Value {
267 json_syntax::Value::String(self.into_str().into())
268 }
269}
270
271impl IntoJson for context::term_definition::Expanded {
272 fn into_json(self) -> json_syntax::Value {
273 let mut object = json_syntax::Object::new();
274
275 if let Some(id) = self.id {
276 object.insert("@id".into(), id.into_json());
277 }
278
279 if let Some(type_) = self.type_ {
280 object.insert("@type".into(), type_.into_json());
281 }
282
283 if let Some(context) = self.context {
284 object.insert("@context".into(), context.into_json());
285 }
286
287 if let Some(reverse) = self.reverse {
288 object.insert("@reverse".into(), reverse.into_json());
289 }
290
291 if let Some(index) = self.index {
292 object.insert("@index".into(), index.into_json());
293 }
294
295 if let Some(language) = self.language {
296 object.insert("@language".into(), language.into_json());
297 }
298
299 if let Some(direction) = self.direction {
300 object.insert("@direction".into(), direction.into_json());
301 }
302
303 if let Some(container) = self.container {
304 object.insert("@container".into(), container.into_json());
305 }
306
307 if let Some(nest) = self.nest {
308 object.insert("@nest".into(), nest.into_json());
309 }
310
311 if let Some(prefix) = self.prefix {
312 object.insert("@prefix".into(), prefix.into_json());
313 }
314
315 if let Some(propagate) = self.propagate {
316 object.insert("@propagate".into(), propagate.into_json());
317 }
318
319 if let Some(protected) = self.protected {
320 object.insert("@protected".into(), protected.into_json());
321 }
322
323 json_syntax::Value::Object(object)
324 }
325}