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