serde_json/value/
ser.rs

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