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