/build/cargo-vendor-dir/serde_json-1.0.140/src/value/ser.rs
Line | Count | Source |
1 | | use crate::error::{Error, ErrorCode, Result}; |
2 | | use crate::map::Map; |
3 | | use crate::value::{to_value, Value}; |
4 | | use alloc::borrow::ToOwned; |
5 | | use alloc::string::{String, ToString}; |
6 | | use alloc::vec::Vec; |
7 | | use core::fmt::Display; |
8 | | use core::result; |
9 | | use serde::ser::{Impossible, Serialize}; |
10 | | |
11 | | impl Serialize for Value { |
12 | | #[inline] |
13 | 0 | fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error> |
14 | 0 | where |
15 | 0 | S: ::serde::Serializer, |
16 | 0 | { |
17 | 0 | match self { |
18 | 0 | Value::Null => serializer.serialize_unit(), |
19 | 0 | Value::Bool(b) => serializer.serialize_bool(*b), |
20 | 0 | Value::Number(n) => n.serialize(serializer), |
21 | 0 | Value::String(s) => serializer.serialize_str(s), |
22 | 0 | Value::Array(v) => v.serialize(serializer), |
23 | | #[cfg(any(feature = "std", feature = "alloc"))] |
24 | 0 | Value::Object(m) => { |
25 | | use serde::ser::SerializeMap; |
26 | 0 | let mut map = tri!(serializer.serialize_map(Some(m.len()))); |
27 | 0 | for (k, v) in m { |
28 | 0 | tri!(map.serialize_entry(k, v)); |
29 | | } |
30 | 0 | map.end() |
31 | | } |
32 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
33 | | Value::Object(_) => unreachable!(), |
34 | | } |
35 | 0 | } |
36 | | } |
37 | | |
38 | | /// Serializer whose output is a `Value`. |
39 | | /// |
40 | | /// This is the serializer that backs [`serde_json::to_value`][crate::to_value]. |
41 | | /// Unlike the main serde_json serializer which goes from some serializable |
42 | | /// value of type `T` to JSON text, this one goes from `T` to |
43 | | /// `serde_json::Value`. |
44 | | /// |
45 | | /// The `to_value` function is implementable as: |
46 | | /// |
47 | | /// ``` |
48 | | /// use serde::Serialize; |
49 | | /// use serde_json::{Error, Value}; |
50 | | /// |
51 | | /// pub fn to_value<T>(input: T) -> Result<Value, Error> |
52 | | /// where |
53 | | /// T: Serialize, |
54 | | /// { |
55 | | /// input.serialize(serde_json::value::Serializer) |
56 | | /// } |
57 | | /// ``` |
58 | | pub struct Serializer; |
59 | | |
60 | | impl serde::Serializer for Serializer { |
61 | | type Ok = Value; |
62 | | type Error = Error; |
63 | | |
64 | | type SerializeSeq = SerializeVec; |
65 | | type SerializeTuple = SerializeVec; |
66 | | type SerializeTupleStruct = SerializeVec; |
67 | | type SerializeTupleVariant = SerializeTupleVariant; |
68 | | type SerializeMap = SerializeMap; |
69 | | type SerializeStruct = SerializeMap; |
70 | | type SerializeStructVariant = SerializeStructVariant; |
71 | | |
72 | | #[inline] |
73 | 0 | fn serialize_bool(self, value: bool) -> Result<Value> { |
74 | 0 | Ok(Value::Bool(value)) |
75 | 0 | } |
76 | | |
77 | | #[inline] |
78 | 0 | fn serialize_i8(self, value: i8) -> Result<Value> { |
79 | 0 | self.serialize_i64(value as i64) |
80 | 0 | } |
81 | | |
82 | | #[inline] |
83 | 0 | fn serialize_i16(self, value: i16) -> Result<Value> { |
84 | 0 | self.serialize_i64(value as i64) |
85 | 0 | } |
86 | | |
87 | | #[inline] |
88 | 0 | fn serialize_i32(self, value: i32) -> Result<Value> { |
89 | 0 | self.serialize_i64(value as i64) |
90 | 0 | } |
91 | | |
92 | 0 | fn serialize_i64(self, value: i64) -> Result<Value> { |
93 | 0 | Ok(Value::Number(value.into())) |
94 | 0 | } |
95 | | |
96 | 0 | fn serialize_i128(self, value: i128) -> Result<Value> { |
97 | | #[cfg(feature = "arbitrary_precision")] |
98 | | { |
99 | | Ok(Value::Number(value.into())) |
100 | | } |
101 | | |
102 | | #[cfg(not(feature = "arbitrary_precision"))] |
103 | | { |
104 | 0 | if let Ok(value) = u64::try_from(value) { |
105 | 0 | Ok(Value::Number(value.into())) |
106 | 0 | } else if let Ok(value) = i64::try_from(value) { |
107 | 0 | Ok(Value::Number(value.into())) |
108 | | } else { |
109 | 0 | Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0)) |
110 | | } |
111 | | } |
112 | 0 | } |
113 | | |
114 | | #[inline] |
115 | 0 | fn serialize_u8(self, value: u8) -> Result<Value> { |
116 | 0 | self.serialize_u64(value as u64) |
117 | 0 | } |
118 | | |
119 | | #[inline] |
120 | 0 | fn serialize_u16(self, value: u16) -> Result<Value> { |
121 | 0 | self.serialize_u64(value as u64) |
122 | 0 | } |
123 | | |
124 | | #[inline] |
125 | 0 | fn serialize_u32(self, value: u32) -> Result<Value> { |
126 | 0 | self.serialize_u64(value as u64) |
127 | 0 | } |
128 | | |
129 | | #[inline] |
130 | 0 | fn serialize_u64(self, value: u64) -> Result<Value> { |
131 | 0 | Ok(Value::Number(value.into())) |
132 | 0 | } |
133 | | |
134 | 0 | fn serialize_u128(self, value: u128) -> Result<Value> { |
135 | | #[cfg(feature = "arbitrary_precision")] |
136 | | { |
137 | | Ok(Value::Number(value.into())) |
138 | | } |
139 | | |
140 | | #[cfg(not(feature = "arbitrary_precision"))] |
141 | | { |
142 | 0 | if let Ok(value) = u64::try_from(value) { |
143 | 0 | Ok(Value::Number(value.into())) |
144 | | } else { |
145 | 0 | Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0)) |
146 | | } |
147 | | } |
148 | 0 | } |
149 | | |
150 | | #[inline] |
151 | 0 | fn serialize_f32(self, float: f32) -> Result<Value> { |
152 | 0 | Ok(Value::from(float)) |
153 | 0 | } |
154 | | |
155 | | #[inline] |
156 | 0 | fn serialize_f64(self, float: f64) -> Result<Value> { |
157 | 0 | Ok(Value::from(float)) |
158 | 0 | } |
159 | | |
160 | | #[inline] |
161 | 0 | fn serialize_char(self, value: char) -> Result<Value> { |
162 | 0 | let mut s = String::new(); |
163 | 0 | s.push(value); |
164 | 0 | Ok(Value::String(s)) |
165 | 0 | } |
166 | | |
167 | | #[inline] |
168 | 0 | fn serialize_str(self, value: &str) -> Result<Value> { |
169 | 0 | Ok(Value::String(value.to_owned())) |
170 | 0 | } |
171 | | |
172 | 0 | fn serialize_bytes(self, value: &[u8]) -> Result<Value> { |
173 | 0 | let vec = value.iter().map(|&b| Value::Number(b.into())).collect(); |
174 | 0 | Ok(Value::Array(vec)) |
175 | 0 | } |
176 | | |
177 | | #[inline] |
178 | 0 | fn serialize_unit(self) -> Result<Value> { |
179 | 0 | Ok(Value::Null) |
180 | 0 | } |
181 | | |
182 | | #[inline] |
183 | 0 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> { |
184 | 0 | self.serialize_unit() |
185 | 0 | } |
186 | | |
187 | | #[inline] |
188 | 0 | fn serialize_unit_variant( |
189 | 0 | self, |
190 | 0 | _name: &'static str, |
191 | 0 | _variant_index: u32, |
192 | 0 | variant: &'static str, |
193 | 0 | ) -> Result<Value> { |
194 | 0 | self.serialize_str(variant) |
195 | 0 | } |
196 | | |
197 | | #[inline] |
198 | 0 | fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value> |
199 | 0 | where |
200 | 0 | T: ?Sized + Serialize, |
201 | 0 | { |
202 | 0 | value.serialize(self) |
203 | 0 | } |
204 | | |
205 | 0 | fn serialize_newtype_variant<T>( |
206 | 0 | self, |
207 | 0 | _name: &'static str, |
208 | 0 | _variant_index: u32, |
209 | 0 | variant: &'static str, |
210 | 0 | value: &T, |
211 | 0 | ) -> Result<Value> |
212 | 0 | where |
213 | 0 | T: ?Sized + Serialize, |
214 | 0 | { |
215 | 0 | let mut values = Map::new(); |
216 | 0 | values.insert(String::from(variant), tri!(to_value(value))); |
217 | 0 | Ok(Value::Object(values)) |
218 | 0 | } |
219 | | |
220 | | #[inline] |
221 | 0 | fn serialize_none(self) -> Result<Value> { |
222 | 0 | self.serialize_unit() |
223 | 0 | } |
224 | | |
225 | | #[inline] |
226 | 0 | fn serialize_some<T>(self, value: &T) -> Result<Value> |
227 | 0 | where |
228 | 0 | T: ?Sized + Serialize, |
229 | 0 | { |
230 | 0 | value.serialize(self) |
231 | 0 | } |
232 | | |
233 | 0 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> { |
234 | 0 | Ok(SerializeVec { |
235 | 0 | vec: Vec::with_capacity(len.unwrap_or(0)), |
236 | 0 | }) |
237 | 0 | } |
238 | | |
239 | 0 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> { |
240 | 0 | self.serialize_seq(Some(len)) |
241 | 0 | } |
242 | | |
243 | 0 | fn serialize_tuple_struct( |
244 | 0 | self, |
245 | 0 | _name: &'static str, |
246 | 0 | len: usize, |
247 | 0 | ) -> Result<Self::SerializeTupleStruct> { |
248 | 0 | self.serialize_seq(Some(len)) |
249 | 0 | } |
250 | | |
251 | 0 | fn serialize_tuple_variant( |
252 | 0 | self, |
253 | 0 | _name: &'static str, |
254 | 0 | _variant_index: u32, |
255 | 0 | variant: &'static str, |
256 | 0 | len: usize, |
257 | 0 | ) -> Result<Self::SerializeTupleVariant> { |
258 | 0 | Ok(SerializeTupleVariant { |
259 | 0 | name: String::from(variant), |
260 | 0 | vec: Vec::with_capacity(len), |
261 | 0 | }) |
262 | 0 | } |
263 | | |
264 | 0 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> { |
265 | 0 | Ok(SerializeMap::Map { |
266 | 0 | map: Map::with_capacity(len.unwrap_or(0)), |
267 | 0 | next_key: None, |
268 | 0 | }) |
269 | 0 | } |
270 | | |
271 | 0 | fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> { |
272 | 0 | match name { |
273 | 0 | #[cfg(feature = "arbitrary_precision")] |
274 | 0 | crate::number::TOKEN => Ok(SerializeMap::Number { out_value: None }), |
275 | 0 | #[cfg(feature = "raw_value")] |
276 | 0 | crate::raw::TOKEN => Ok(SerializeMap::RawValue { out_value: None }), |
277 | 0 | _ => self.serialize_map(Some(len)), |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | 0 | fn serialize_struct_variant( |
282 | 0 | self, |
283 | 0 | _name: &'static str, |
284 | 0 | _variant_index: u32, |
285 | 0 | variant: &'static str, |
286 | 0 | _len: usize, |
287 | 0 | ) -> Result<Self::SerializeStructVariant> { |
288 | 0 | Ok(SerializeStructVariant { |
289 | 0 | name: String::from(variant), |
290 | 0 | map: Map::new(), |
291 | 0 | }) |
292 | 0 | } |
293 | | |
294 | 0 | fn collect_str<T>(self, value: &T) -> Result<Value> |
295 | 0 | where |
296 | 0 | T: ?Sized + Display, |
297 | 0 | { |
298 | 0 | Ok(Value::String(value.to_string())) |
299 | 0 | } |
300 | | } |
301 | | |
302 | | pub struct SerializeVec { |
303 | | vec: Vec<Value>, |
304 | | } |
305 | | |
306 | | pub struct SerializeTupleVariant { |
307 | | name: String, |
308 | | vec: Vec<Value>, |
309 | | } |
310 | | |
311 | | pub enum SerializeMap { |
312 | | Map { |
313 | | map: Map<String, Value>, |
314 | | next_key: Option<String>, |
315 | | }, |
316 | | #[cfg(feature = "arbitrary_precision")] |
317 | | Number { out_value: Option<Value> }, |
318 | | #[cfg(feature = "raw_value")] |
319 | | RawValue { out_value: Option<Value> }, |
320 | | } |
321 | | |
322 | | pub struct SerializeStructVariant { |
323 | | name: String, |
324 | | map: Map<String, Value>, |
325 | | } |
326 | | |
327 | | impl serde::ser::SerializeSeq for SerializeVec { |
328 | | type Ok = Value; |
329 | | type Error = Error; |
330 | | |
331 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<()> |
332 | 0 | where |
333 | 0 | T: ?Sized + Serialize, |
334 | 0 | { |
335 | 0 | self.vec.push(tri!(to_value(value))); |
336 | 0 | Ok(()) |
337 | 0 | } |
338 | | |
339 | 0 | fn end(self) -> Result<Value> { |
340 | 0 | Ok(Value::Array(self.vec)) |
341 | 0 | } |
342 | | } |
343 | | |
344 | | impl serde::ser::SerializeTuple for SerializeVec { |
345 | | type Ok = Value; |
346 | | type Error = Error; |
347 | | |
348 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<()> |
349 | 0 | where |
350 | 0 | T: ?Sized + Serialize, |
351 | 0 | { |
352 | 0 | serde::ser::SerializeSeq::serialize_element(self, value) |
353 | 0 | } |
354 | | |
355 | 0 | fn end(self) -> Result<Value> { |
356 | 0 | serde::ser::SerializeSeq::end(self) |
357 | 0 | } |
358 | | } |
359 | | |
360 | | impl serde::ser::SerializeTupleStruct for SerializeVec { |
361 | | type Ok = Value; |
362 | | type Error = Error; |
363 | | |
364 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<()> |
365 | 0 | where |
366 | 0 | T: ?Sized + Serialize, |
367 | 0 | { |
368 | 0 | serde::ser::SerializeSeq::serialize_element(self, value) |
369 | 0 | } |
370 | | |
371 | 0 | fn end(self) -> Result<Value> { |
372 | 0 | serde::ser::SerializeSeq::end(self) |
373 | 0 | } |
374 | | } |
375 | | |
376 | | impl serde::ser::SerializeTupleVariant for SerializeTupleVariant { |
377 | | type Ok = Value; |
378 | | type Error = Error; |
379 | | |
380 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<()> |
381 | 0 | where |
382 | 0 | T: ?Sized + Serialize, |
383 | 0 | { |
384 | 0 | self.vec.push(tri!(to_value(value))); |
385 | 0 | Ok(()) |
386 | 0 | } |
387 | | |
388 | 0 | fn end(self) -> Result<Value> { |
389 | 0 | let mut object = Map::new(); |
390 | 0 |
|
391 | 0 | object.insert(self.name, Value::Array(self.vec)); |
392 | 0 |
|
393 | 0 | Ok(Value::Object(object)) |
394 | 0 | } |
395 | | } |
396 | | |
397 | | impl serde::ser::SerializeMap for SerializeMap { |
398 | | type Ok = Value; |
399 | | type Error = Error; |
400 | | |
401 | 0 | fn serialize_key<T>(&mut self, key: &T) -> Result<()> |
402 | 0 | where |
403 | 0 | T: ?Sized + Serialize, |
404 | 0 | { |
405 | 0 | match self { |
406 | 0 | SerializeMap::Map { next_key, .. } => { |
407 | 0 | *next_key = Some(tri!(key.serialize(MapKeySerializer))); |
408 | 0 | Ok(()) |
409 | | } |
410 | | #[cfg(feature = "arbitrary_precision")] |
411 | | SerializeMap::Number { .. } => unreachable!(), |
412 | | #[cfg(feature = "raw_value")] |
413 | | SerializeMap::RawValue { .. } => unreachable!(), |
414 | | } |
415 | 0 | } |
416 | | |
417 | 0 | fn serialize_value<T>(&mut self, value: &T) -> Result<()> |
418 | 0 | where |
419 | 0 | T: ?Sized + Serialize, |
420 | 0 | { |
421 | 0 | match self { |
422 | 0 | SerializeMap::Map { map, next_key } => { |
423 | 0 | let key = next_key.take(); |
424 | 0 | // Panic because this indicates a bug in the program rather than an |
425 | 0 | // expected failure. |
426 | 0 | let key = key.expect("serialize_value called before serialize_key"); |
427 | 0 | map.insert(key, tri!(to_value(value))); |
428 | 0 | Ok(()) |
429 | | } |
430 | | #[cfg(feature = "arbitrary_precision")] |
431 | | SerializeMap::Number { .. } => unreachable!(), |
432 | | #[cfg(feature = "raw_value")] |
433 | | SerializeMap::RawValue { .. } => unreachable!(), |
434 | | } |
435 | 0 | } |
436 | | |
437 | 0 | fn end(self) -> Result<Value> { |
438 | 0 | match self { |
439 | 0 | SerializeMap::Map { map, .. } => Ok(Value::Object(map)), |
440 | 0 | #[cfg(feature = "arbitrary_precision")] |
441 | 0 | SerializeMap::Number { .. } => unreachable!(), |
442 | 0 | #[cfg(feature = "raw_value")] |
443 | 0 | SerializeMap::RawValue { .. } => unreachable!(), |
444 | 0 | } |
445 | 0 | } |
446 | | } |
447 | | |
448 | | struct MapKeySerializer; |
449 | | |
450 | 0 | fn key_must_be_a_string() -> Error { |
451 | 0 | Error::syntax(ErrorCode::KeyMustBeAString, 0, 0) |
452 | 0 | } |
453 | | |
454 | 0 | fn float_key_must_be_finite() -> Error { |
455 | 0 | Error::syntax(ErrorCode::FloatKeyMustBeFinite, 0, 0) |
456 | 0 | } |
457 | | |
458 | | impl serde::Serializer for MapKeySerializer { |
459 | | type Ok = String; |
460 | | type Error = Error; |
461 | | |
462 | | type SerializeSeq = Impossible<String, Error>; |
463 | | type SerializeTuple = Impossible<String, Error>; |
464 | | type SerializeTupleStruct = Impossible<String, Error>; |
465 | | type SerializeTupleVariant = Impossible<String, Error>; |
466 | | type SerializeMap = Impossible<String, Error>; |
467 | | type SerializeStruct = Impossible<String, Error>; |
468 | | type SerializeStructVariant = Impossible<String, Error>; |
469 | | |
470 | | #[inline] |
471 | 0 | fn serialize_unit_variant( |
472 | 0 | self, |
473 | 0 | _name: &'static str, |
474 | 0 | _variant_index: u32, |
475 | 0 | variant: &'static str, |
476 | 0 | ) -> Result<String> { |
477 | 0 | Ok(variant.to_owned()) |
478 | 0 | } |
479 | | |
480 | | #[inline] |
481 | 0 | fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String> |
482 | 0 | where |
483 | 0 | T: ?Sized + Serialize, |
484 | 0 | { |
485 | 0 | value.serialize(self) |
486 | 0 | } |
487 | | |
488 | 0 | fn serialize_bool(self, value: bool) -> Result<String> { |
489 | 0 | Ok(if value { "true" } else { "false" }.to_owned()) |
490 | 0 | } |
491 | | |
492 | 0 | fn serialize_i8(self, value: i8) -> Result<String> { |
493 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
494 | 0 | } |
495 | | |
496 | 0 | fn serialize_i16(self, value: i16) -> Result<String> { |
497 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
498 | 0 | } |
499 | | |
500 | 0 | fn serialize_i32(self, value: i32) -> Result<String> { |
501 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
502 | 0 | } |
503 | | |
504 | 0 | fn serialize_i64(self, value: i64) -> Result<String> { |
505 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
506 | 0 | } |
507 | | |
508 | 0 | fn serialize_i128(self, value: i128) -> Result<String> { |
509 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
510 | 0 | } |
511 | | |
512 | 0 | fn serialize_u8(self, value: u8) -> Result<String> { |
513 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
514 | 0 | } |
515 | | |
516 | 0 | fn serialize_u16(self, value: u16) -> Result<String> { |
517 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
518 | 0 | } |
519 | | |
520 | 0 | fn serialize_u32(self, value: u32) -> Result<String> { |
521 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
522 | 0 | } |
523 | | |
524 | 0 | fn serialize_u64(self, value: u64) -> Result<String> { |
525 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
526 | 0 | } |
527 | | |
528 | 0 | fn serialize_u128(self, value: u128) -> Result<String> { |
529 | 0 | Ok(itoa::Buffer::new().format(value).to_owned()) |
530 | 0 | } |
531 | | |
532 | 0 | fn serialize_f32(self, value: f32) -> Result<String> { |
533 | 0 | if value.is_finite() { |
534 | 0 | Ok(ryu::Buffer::new().format_finite(value).to_owned()) |
535 | | } else { |
536 | 0 | Err(float_key_must_be_finite()) |
537 | | } |
538 | 0 | } |
539 | | |
540 | 0 | fn serialize_f64(self, value: f64) -> Result<String> { |
541 | 0 | if value.is_finite() { |
542 | 0 | Ok(ryu::Buffer::new().format_finite(value).to_owned()) |
543 | | } else { |
544 | 0 | Err(float_key_must_be_finite()) |
545 | | } |
546 | 0 | } |
547 | | |
548 | | #[inline] |
549 | 0 | fn serialize_char(self, value: char) -> Result<String> { |
550 | 0 | Ok({ |
551 | 0 | let mut s = String::new(); |
552 | 0 | s.push(value); |
553 | 0 | s |
554 | 0 | }) |
555 | 0 | } |
556 | | |
557 | | #[inline] |
558 | 0 | fn serialize_str(self, value: &str) -> Result<String> { |
559 | 0 | Ok(value.to_owned()) |
560 | 0 | } |
561 | | |
562 | 0 | fn serialize_bytes(self, _value: &[u8]) -> Result<String> { |
563 | 0 | Err(key_must_be_a_string()) |
564 | 0 | } |
565 | | |
566 | 0 | fn serialize_unit(self) -> Result<String> { |
567 | 0 | Err(key_must_be_a_string()) |
568 | 0 | } |
569 | | |
570 | 0 | fn serialize_unit_struct(self, _name: &'static str) -> Result<String> { |
571 | 0 | Err(key_must_be_a_string()) |
572 | 0 | } |
573 | | |
574 | 0 | fn serialize_newtype_variant<T>( |
575 | 0 | self, |
576 | 0 | _name: &'static str, |
577 | 0 | _variant_index: u32, |
578 | 0 | _variant: &'static str, |
579 | 0 | _value: &T, |
580 | 0 | ) -> Result<String> |
581 | 0 | where |
582 | 0 | T: ?Sized + Serialize, |
583 | 0 | { |
584 | 0 | Err(key_must_be_a_string()) |
585 | 0 | } |
586 | | |
587 | 0 | fn serialize_none(self) -> Result<String> { |
588 | 0 | Err(key_must_be_a_string()) |
589 | 0 | } |
590 | | |
591 | 0 | fn serialize_some<T>(self, _value: &T) -> Result<String> |
592 | 0 | where |
593 | 0 | T: ?Sized + Serialize, |
594 | 0 | { |
595 | 0 | Err(key_must_be_a_string()) |
596 | 0 | } |
597 | | |
598 | 0 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
599 | 0 | Err(key_must_be_a_string()) |
600 | 0 | } |
601 | | |
602 | 0 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
603 | 0 | Err(key_must_be_a_string()) |
604 | 0 | } |
605 | | |
606 | 0 | fn serialize_tuple_struct( |
607 | 0 | self, |
608 | 0 | _name: &'static str, |
609 | 0 | _len: usize, |
610 | 0 | ) -> Result<Self::SerializeTupleStruct> { |
611 | 0 | Err(key_must_be_a_string()) |
612 | 0 | } |
613 | | |
614 | 0 | fn serialize_tuple_variant( |
615 | 0 | self, |
616 | 0 | _name: &'static str, |
617 | 0 | _variant_index: u32, |
618 | 0 | _variant: &'static str, |
619 | 0 | _len: usize, |
620 | 0 | ) -> Result<Self::SerializeTupleVariant> { |
621 | 0 | Err(key_must_be_a_string()) |
622 | 0 | } |
623 | | |
624 | 0 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
625 | 0 | Err(key_must_be_a_string()) |
626 | 0 | } |
627 | | |
628 | 0 | fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { |
629 | 0 | Err(key_must_be_a_string()) |
630 | 0 | } |
631 | | |
632 | 0 | fn serialize_struct_variant( |
633 | 0 | self, |
634 | 0 | _name: &'static str, |
635 | 0 | _variant_index: u32, |
636 | 0 | _variant: &'static str, |
637 | 0 | _len: usize, |
638 | 0 | ) -> Result<Self::SerializeStructVariant> { |
639 | 0 | Err(key_must_be_a_string()) |
640 | 0 | } |
641 | | |
642 | 0 | fn collect_str<T>(self, value: &T) -> Result<String> |
643 | 0 | where |
644 | 0 | T: ?Sized + Display, |
645 | 0 | { |
646 | 0 | Ok(value.to_string()) |
647 | 0 | } |
648 | | } |
649 | | |
650 | | impl serde::ser::SerializeStruct for SerializeMap { |
651 | | type Ok = Value; |
652 | | type Error = Error; |
653 | | |
654 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> |
655 | 0 | where |
656 | 0 | T: ?Sized + Serialize, |
657 | 0 | { |
658 | 0 | match self { |
659 | 0 | SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value), |
660 | 0 | #[cfg(feature = "arbitrary_precision")] |
661 | 0 | SerializeMap::Number { out_value } => { |
662 | 0 | if key == crate::number::TOKEN { |
663 | 0 | *out_value = Some(tri!(value.serialize(NumberValueEmitter))); |
664 | 0 | Ok(()) |
665 | 0 | } else { |
666 | 0 | Err(invalid_number()) |
667 | 0 | } |
668 | 0 | } |
669 | 0 | #[cfg(feature = "raw_value")] |
670 | 0 | SerializeMap::RawValue { out_value } => { |
671 | 0 | if key == crate::raw::TOKEN { |
672 | 0 | *out_value = Some(tri!(value.serialize(RawValueEmitter))); |
673 | 0 | Ok(()) |
674 | 0 | } else { |
675 | 0 | Err(invalid_raw_value()) |
676 | 0 | } |
677 | 0 | } |
678 | 0 | } |
679 | 0 | } |
680 | | |
681 | 0 | fn end(self) -> Result<Value> { |
682 | 0 | match self { |
683 | 0 | SerializeMap::Map { .. } => serde::ser::SerializeMap::end(self), |
684 | 0 | #[cfg(feature = "arbitrary_precision")] |
685 | 0 | SerializeMap::Number { out_value, .. } => { |
686 | 0 | Ok(out_value.expect("number value was not emitted")) |
687 | 0 | } |
688 | 0 | #[cfg(feature = "raw_value")] |
689 | 0 | SerializeMap::RawValue { out_value, .. } => { |
690 | 0 | Ok(out_value.expect("raw value was not emitted")) |
691 | 0 | } |
692 | 0 | } |
693 | 0 | } |
694 | | } |
695 | | |
696 | | impl serde::ser::SerializeStructVariant for SerializeStructVariant { |
697 | | type Ok = Value; |
698 | | type Error = Error; |
699 | | |
700 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> |
701 | 0 | where |
702 | 0 | T: ?Sized + Serialize, |
703 | 0 | { |
704 | 0 | self.map.insert(String::from(key), tri!(to_value(value))); |
705 | 0 | Ok(()) |
706 | 0 | } |
707 | | |
708 | 0 | fn end(self) -> Result<Value> { |
709 | 0 | let mut object = Map::new(); |
710 | 0 |
|
711 | 0 | object.insert(self.name, Value::Object(self.map)); |
712 | 0 |
|
713 | 0 | Ok(Value::Object(object)) |
714 | 0 | } |
715 | | } |
716 | | |
717 | | #[cfg(feature = "arbitrary_precision")] |
718 | | struct NumberValueEmitter; |
719 | | |
720 | | #[cfg(feature = "arbitrary_precision")] |
721 | | fn invalid_number() -> Error { |
722 | | Error::syntax(ErrorCode::InvalidNumber, 0, 0) |
723 | | } |
724 | | |
725 | | #[cfg(feature = "arbitrary_precision")] |
726 | | impl serde::ser::Serializer for NumberValueEmitter { |
727 | | type Ok = Value; |
728 | | type Error = Error; |
729 | | |
730 | | type SerializeSeq = Impossible<Value, Error>; |
731 | | type SerializeTuple = Impossible<Value, Error>; |
732 | | type SerializeTupleStruct = Impossible<Value, Error>; |
733 | | type SerializeTupleVariant = Impossible<Value, Error>; |
734 | | type SerializeMap = Impossible<Value, Error>; |
735 | | type SerializeStruct = Impossible<Value, Error>; |
736 | | type SerializeStructVariant = Impossible<Value, Error>; |
737 | | |
738 | | fn serialize_bool(self, _v: bool) -> Result<Value> { |
739 | | Err(invalid_number()) |
740 | | } |
741 | | |
742 | | fn serialize_i8(self, _v: i8) -> Result<Value> { |
743 | | Err(invalid_number()) |
744 | | } |
745 | | |
746 | | fn serialize_i16(self, _v: i16) -> Result<Value> { |
747 | | Err(invalid_number()) |
748 | | } |
749 | | |
750 | | fn serialize_i32(self, _v: i32) -> Result<Value> { |
751 | | Err(invalid_number()) |
752 | | } |
753 | | |
754 | | fn serialize_i64(self, _v: i64) -> Result<Value> { |
755 | | Err(invalid_number()) |
756 | | } |
757 | | |
758 | | fn serialize_u8(self, _v: u8) -> Result<Value> { |
759 | | Err(invalid_number()) |
760 | | } |
761 | | |
762 | | fn serialize_u16(self, _v: u16) -> Result<Value> { |
763 | | Err(invalid_number()) |
764 | | } |
765 | | |
766 | | fn serialize_u32(self, _v: u32) -> Result<Value> { |
767 | | Err(invalid_number()) |
768 | | } |
769 | | |
770 | | fn serialize_u64(self, _v: u64) -> Result<Value> { |
771 | | Err(invalid_number()) |
772 | | } |
773 | | |
774 | | fn serialize_f32(self, _v: f32) -> Result<Value> { |
775 | | Err(invalid_number()) |
776 | | } |
777 | | |
778 | | fn serialize_f64(self, _v: f64) -> Result<Value> { |
779 | | Err(invalid_number()) |
780 | | } |
781 | | |
782 | | fn serialize_char(self, _v: char) -> Result<Value> { |
783 | | Err(invalid_number()) |
784 | | } |
785 | | |
786 | | fn serialize_str(self, value: &str) -> Result<Value> { |
787 | | let n = tri!(value.to_owned().parse()); |
788 | | Ok(Value::Number(n)) |
789 | | } |
790 | | |
791 | | fn serialize_bytes(self, _value: &[u8]) -> Result<Value> { |
792 | | Err(invalid_number()) |
793 | | } |
794 | | |
795 | | fn serialize_none(self) -> Result<Value> { |
796 | | Err(invalid_number()) |
797 | | } |
798 | | |
799 | | fn serialize_some<T>(self, _value: &T) -> Result<Value> |
800 | | where |
801 | | T: ?Sized + Serialize, |
802 | | { |
803 | | Err(invalid_number()) |
804 | | } |
805 | | |
806 | | fn serialize_unit(self) -> Result<Value> { |
807 | | Err(invalid_number()) |
808 | | } |
809 | | |
810 | | fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> { |
811 | | Err(invalid_number()) |
812 | | } |
813 | | |
814 | | fn serialize_unit_variant( |
815 | | self, |
816 | | _name: &'static str, |
817 | | _variant_index: u32, |
818 | | _variant: &'static str, |
819 | | ) -> Result<Value> { |
820 | | Err(invalid_number()) |
821 | | } |
822 | | |
823 | | fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value> |
824 | | where |
825 | | T: ?Sized + Serialize, |
826 | | { |
827 | | Err(invalid_number()) |
828 | | } |
829 | | |
830 | | fn serialize_newtype_variant<T>( |
831 | | self, |
832 | | _name: &'static str, |
833 | | _variant_index: u32, |
834 | | _variant: &'static str, |
835 | | _value: &T, |
836 | | ) -> Result<Value> |
837 | | where |
838 | | T: ?Sized + Serialize, |
839 | | { |
840 | | Err(invalid_number()) |
841 | | } |
842 | | |
843 | | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
844 | | Err(invalid_number()) |
845 | | } |
846 | | |
847 | | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
848 | | Err(invalid_number()) |
849 | | } |
850 | | |
851 | | fn serialize_tuple_struct( |
852 | | self, |
853 | | _name: &'static str, |
854 | | _len: usize, |
855 | | ) -> Result<Self::SerializeTupleStruct> { |
856 | | Err(invalid_number()) |
857 | | } |
858 | | |
859 | | fn serialize_tuple_variant( |
860 | | self, |
861 | | _name: &'static str, |
862 | | _variant_index: u32, |
863 | | _variant: &'static str, |
864 | | _len: usize, |
865 | | ) -> Result<Self::SerializeTupleVariant> { |
866 | | Err(invalid_number()) |
867 | | } |
868 | | |
869 | | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
870 | | Err(invalid_number()) |
871 | | } |
872 | | |
873 | | fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { |
874 | | Err(invalid_number()) |
875 | | } |
876 | | |
877 | | fn serialize_struct_variant( |
878 | | self, |
879 | | _name: &'static str, |
880 | | _variant_index: u32, |
881 | | _variant: &'static str, |
882 | | _len: usize, |
883 | | ) -> Result<Self::SerializeStructVariant> { |
884 | | Err(invalid_number()) |
885 | | } |
886 | | } |
887 | | |
888 | | #[cfg(feature = "raw_value")] |
889 | | struct RawValueEmitter; |
890 | | |
891 | | #[cfg(feature = "raw_value")] |
892 | | fn invalid_raw_value() -> Error { |
893 | | Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0) |
894 | | } |
895 | | |
896 | | #[cfg(feature = "raw_value")] |
897 | | impl serde::ser::Serializer for RawValueEmitter { |
898 | | type Ok = Value; |
899 | | type Error = Error; |
900 | | |
901 | | type SerializeSeq = Impossible<Value, Error>; |
902 | | type SerializeTuple = Impossible<Value, Error>; |
903 | | type SerializeTupleStruct = Impossible<Value, Error>; |
904 | | type SerializeTupleVariant = Impossible<Value, Error>; |
905 | | type SerializeMap = Impossible<Value, Error>; |
906 | | type SerializeStruct = Impossible<Value, Error>; |
907 | | type SerializeStructVariant = Impossible<Value, Error>; |
908 | | |
909 | | fn serialize_bool(self, _v: bool) -> Result<Value> { |
910 | | Err(invalid_raw_value()) |
911 | | } |
912 | | |
913 | | fn serialize_i8(self, _v: i8) -> Result<Value> { |
914 | | Err(invalid_raw_value()) |
915 | | } |
916 | | |
917 | | fn serialize_i16(self, _v: i16) -> Result<Value> { |
918 | | Err(invalid_raw_value()) |
919 | | } |
920 | | |
921 | | fn serialize_i32(self, _v: i32) -> Result<Value> { |
922 | | Err(invalid_raw_value()) |
923 | | } |
924 | | |
925 | | fn serialize_i64(self, _v: i64) -> Result<Value> { |
926 | | Err(invalid_raw_value()) |
927 | | } |
928 | | |
929 | | fn serialize_u8(self, _v: u8) -> Result<Value> { |
930 | | Err(invalid_raw_value()) |
931 | | } |
932 | | |
933 | | fn serialize_u16(self, _v: u16) -> Result<Value> { |
934 | | Err(invalid_raw_value()) |
935 | | } |
936 | | |
937 | | fn serialize_u32(self, _v: u32) -> Result<Value> { |
938 | | Err(invalid_raw_value()) |
939 | | } |
940 | | |
941 | | fn serialize_u64(self, _v: u64) -> Result<Value> { |
942 | | Err(invalid_raw_value()) |
943 | | } |
944 | | |
945 | | fn serialize_f32(self, _v: f32) -> Result<Value> { |
946 | | Err(invalid_raw_value()) |
947 | | } |
948 | | |
949 | | fn serialize_f64(self, _v: f64) -> Result<Value> { |
950 | | Err(invalid_raw_value()) |
951 | | } |
952 | | |
953 | | fn serialize_char(self, _v: char) -> Result<Value> { |
954 | | Err(invalid_raw_value()) |
955 | | } |
956 | | |
957 | | fn serialize_str(self, value: &str) -> Result<Value> { |
958 | | crate::from_str(value) |
959 | | } |
960 | | |
961 | | fn serialize_bytes(self, _value: &[u8]) -> Result<Value> { |
962 | | Err(invalid_raw_value()) |
963 | | } |
964 | | |
965 | | fn serialize_none(self) -> Result<Value> { |
966 | | Err(invalid_raw_value()) |
967 | | } |
968 | | |
969 | | fn serialize_some<T>(self, _value: &T) -> Result<Value> |
970 | | where |
971 | | T: ?Sized + Serialize, |
972 | | { |
973 | | Err(invalid_raw_value()) |
974 | | } |
975 | | |
976 | | fn serialize_unit(self) -> Result<Value> { |
977 | | Err(invalid_raw_value()) |
978 | | } |
979 | | |
980 | | fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> { |
981 | | Err(invalid_raw_value()) |
982 | | } |
983 | | |
984 | | fn serialize_unit_variant( |
985 | | self, |
986 | | _name: &'static str, |
987 | | _variant_index: u32, |
988 | | _variant: &'static str, |
989 | | ) -> Result<Value> { |
990 | | Err(invalid_raw_value()) |
991 | | } |
992 | | |
993 | | fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value> |
994 | | where |
995 | | T: ?Sized + Serialize, |
996 | | { |
997 | | Err(invalid_raw_value()) |
998 | | } |
999 | | |
1000 | | fn serialize_newtype_variant<T>( |
1001 | | self, |
1002 | | _name: &'static str, |
1003 | | _variant_index: u32, |
1004 | | _variant: &'static str, |
1005 | | _value: &T, |
1006 | | ) -> Result<Value> |
1007 | | where |
1008 | | T: ?Sized + Serialize, |
1009 | | { |
1010 | | Err(invalid_raw_value()) |
1011 | | } |
1012 | | |
1013 | | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
1014 | | Err(invalid_raw_value()) |
1015 | | } |
1016 | | |
1017 | | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
1018 | | Err(invalid_raw_value()) |
1019 | | } |
1020 | | |
1021 | | fn serialize_tuple_struct( |
1022 | | self, |
1023 | | _name: &'static str, |
1024 | | _len: usize, |
1025 | | ) -> Result<Self::SerializeTupleStruct> { |
1026 | | Err(invalid_raw_value()) |
1027 | | } |
1028 | | |
1029 | | fn serialize_tuple_variant( |
1030 | | self, |
1031 | | _name: &'static str, |
1032 | | _variant_index: u32, |
1033 | | _variant: &'static str, |
1034 | | _len: usize, |
1035 | | ) -> Result<Self::SerializeTupleVariant> { |
1036 | | Err(invalid_raw_value()) |
1037 | | } |
1038 | | |
1039 | | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
1040 | | Err(invalid_raw_value()) |
1041 | | } |
1042 | | |
1043 | | fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { |
1044 | | Err(invalid_raw_value()) |
1045 | | } |
1046 | | |
1047 | | fn serialize_struct_variant( |
1048 | | self, |
1049 | | _name: &'static str, |
1050 | | _variant_index: u32, |
1051 | | _variant: &'static str, |
1052 | | _len: usize, |
1053 | | ) -> Result<Self::SerializeStructVariant> { |
1054 | | Err(invalid_raw_value()) |
1055 | | } |
1056 | | |
1057 | | fn collect_str<T>(self, value: &T) -> Result<Self::Ok> |
1058 | | where |
1059 | | T: ?Sized + Display, |
1060 | | { |
1061 | | self.serialize_str(&value.to_string()) |
1062 | | } |
1063 | | } |