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;
7#[cfg(not(feature = "arbitrary_precision"))]
8use core::convert::TryFrom;
9use core::fmt::Display;
10use core::result;
11use serde::ser::{Impossible, Serialize};
12
13impl Serialize for Value {
14 #[inline]
15 fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
16 where
17 S: ::serde::Serializer,
18 {
19 match self {
20 Value::Null => serializer.serialize_unit(),
21 Value::Bool(b: &bool) => serializer.serialize_bool(*b),
22 Value::Number(n: &Number) => n.serialize(serializer),
23 Value::String(s: &String) => serializer.serialize_str(s),
24 Value::Array(v: &Vec) => v.serialize(serializer),
25 #[cfg(any(feature = "std", feature = "alloc"))]
26 Value::Object(m: &Map) => {
27 use serde::ser::SerializeMap;
28 let mut map: ::SerializeMap = tri!(serializer.serialize_map(Some(m.len())));
29 for (k: &String, v: &Value) in m {
30 tri!(map.serialize_entry(k, v));
31 }
32 map.end()
33 }
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 = Map::new();
390
391 object.insert(self.name, v: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(code:ErrorCode::KeyMustBeAString, line:0, column:0)
452}
453
454impl serde::Serializer for MapKeySerializer {
455 type Ok = String;
456 type Error = Error;
457
458 type SerializeSeq = Impossible<String, Error>;
459 type SerializeTuple = Impossible<String, Error>;
460 type SerializeTupleStruct = Impossible<String, Error>;
461 type SerializeTupleVariant = Impossible<String, Error>;
462 type SerializeMap = Impossible<String, Error>;
463 type SerializeStruct = Impossible<String, Error>;
464 type SerializeStructVariant = Impossible<String, Error>;
465
466 #[inline]
467 fn serialize_unit_variant(
468 self,
469 _name: &'static str,
470 _variant_index: u32,
471 variant: &'static str,
472 ) -> Result<String> {
473 Ok(variant.to_owned())
474 }
475
476 #[inline]
477 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String>
478 where
479 T: ?Sized + Serialize,
480 {
481 value.serialize(self)
482 }
483
484 fn serialize_bool(self, _value: bool) -> Result<String> {
485 Err(key_must_be_a_string())
486 }
487
488 fn serialize_i8(self, value: i8) -> Result<String> {
489 Ok(value.to_string())
490 }
491
492 fn serialize_i16(self, value: i16) -> Result<String> {
493 Ok(value.to_string())
494 }
495
496 fn serialize_i32(self, value: i32) -> Result<String> {
497 Ok(value.to_string())
498 }
499
500 fn serialize_i64(self, value: i64) -> Result<String> {
501 Ok(value.to_string())
502 }
503
504 fn serialize_u8(self, value: u8) -> Result<String> {
505 Ok(value.to_string())
506 }
507
508 fn serialize_u16(self, value: u16) -> Result<String> {
509 Ok(value.to_string())
510 }
511
512 fn serialize_u32(self, value: u32) -> Result<String> {
513 Ok(value.to_string())
514 }
515
516 fn serialize_u64(self, value: u64) -> Result<String> {
517 Ok(value.to_string())
518 }
519
520 fn serialize_f32(self, _value: f32) -> Result<String> {
521 Err(key_must_be_a_string())
522 }
523
524 fn serialize_f64(self, _value: f64) -> Result<String> {
525 Err(key_must_be_a_string())
526 }
527
528 #[inline]
529 fn serialize_char(self, value: char) -> Result<String> {
530 Ok({
531 let mut s = String::new();
532 s.push(value);
533 s
534 })
535 }
536
537 #[inline]
538 fn serialize_str(self, value: &str) -> Result<String> {
539 Ok(value.to_owned())
540 }
541
542 fn serialize_bytes(self, _value: &[u8]) -> Result<String> {
543 Err(key_must_be_a_string())
544 }
545
546 fn serialize_unit(self) -> Result<String> {
547 Err(key_must_be_a_string())
548 }
549
550 fn serialize_unit_struct(self, _name: &'static str) -> Result<String> {
551 Err(key_must_be_a_string())
552 }
553
554 fn serialize_newtype_variant<T>(
555 self,
556 _name: &'static str,
557 _variant_index: u32,
558 _variant: &'static str,
559 _value: &T,
560 ) -> Result<String>
561 where
562 T: ?Sized + Serialize,
563 {
564 Err(key_must_be_a_string())
565 }
566
567 fn serialize_none(self) -> Result<String> {
568 Err(key_must_be_a_string())
569 }
570
571 fn serialize_some<T>(self, _value: &T) -> Result<String>
572 where
573 T: ?Sized + Serialize,
574 {
575 Err(key_must_be_a_string())
576 }
577
578 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
579 Err(key_must_be_a_string())
580 }
581
582 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
583 Err(key_must_be_a_string())
584 }
585
586 fn serialize_tuple_struct(
587 self,
588 _name: &'static str,
589 _len: usize,
590 ) -> Result<Self::SerializeTupleStruct> {
591 Err(key_must_be_a_string())
592 }
593
594 fn serialize_tuple_variant(
595 self,
596 _name: &'static str,
597 _variant_index: u32,
598 _variant: &'static str,
599 _len: usize,
600 ) -> Result<Self::SerializeTupleVariant> {
601 Err(key_must_be_a_string())
602 }
603
604 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
605 Err(key_must_be_a_string())
606 }
607
608 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
609 Err(key_must_be_a_string())
610 }
611
612 fn serialize_struct_variant(
613 self,
614 _name: &'static str,
615 _variant_index: u32,
616 _variant: &'static str,
617 _len: usize,
618 ) -> Result<Self::SerializeStructVariant> {
619 Err(key_must_be_a_string())
620 }
621
622 fn collect_str<T>(self, value: &T) -> Result<String>
623 where
624 T: ?Sized + Display,
625 {
626 Ok(value.to_string())
627 }
628}
629
630impl serde::ser::SerializeStruct for SerializeMap {
631 type Ok = Value;
632 type Error = Error;
633
634 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
635 where
636 T: ?Sized + Serialize,
637 {
638 match self {
639 SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value),
640 #[cfg(feature = "arbitrary_precision")]
641 SerializeMap::Number { out_value } => {
642 if key == crate::number::TOKEN {
643 *out_value = Some(value.serialize(NumberValueEmitter)?);
644 Ok(())
645 } else {
646 Err(invalid_number())
647 }
648 }
649 #[cfg(feature = "raw_value")]
650 SerializeMap::RawValue { out_value } => {
651 if key == crate::raw::TOKEN {
652 *out_value = Some(value.serialize(RawValueEmitter)?);
653 Ok(())
654 } else {
655 Err(invalid_raw_value())
656 }
657 }
658 }
659 }
660
661 fn end(self) -> Result<Value> {
662 match self {
663 SerializeMap::Map { .. } => serde::ser::SerializeMap::end(self),
664 #[cfg(feature = "arbitrary_precision")]
665 SerializeMap::Number { out_value, .. } => {
666 Ok(out_value.expect("number value was not emitted"))
667 }
668 #[cfg(feature = "raw_value")]
669 SerializeMap::RawValue { out_value, .. } => {
670 Ok(out_value.expect("raw value was not emitted"))
671 }
672 }
673 }
674}
675
676impl serde::ser::SerializeStructVariant for SerializeStructVariant {
677 type Ok = Value;
678 type Error = Error;
679
680 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
681 where
682 T: ?Sized + Serialize,
683 {
684 self.map.insert(k:String::from(key), v:tri!(to_value(value)));
685 Ok(())
686 }
687
688 fn end(self) -> Result<Value> {
689 let mut object: Map = Map::new();
690
691 object.insert(self.name, v:Value::Object(self.map));
692
693 Ok(Value::Object(object))
694 }
695}
696
697#[cfg(feature = "arbitrary_precision")]
698struct NumberValueEmitter;
699
700#[cfg(feature = "arbitrary_precision")]
701fn invalid_number() -> Error {
702 Error::syntax(ErrorCode::InvalidNumber, 0, 0)
703}
704
705#[cfg(feature = "arbitrary_precision")]
706impl serde::ser::Serializer for NumberValueEmitter {
707 type Ok = Value;
708 type Error = Error;
709
710 type SerializeSeq = Impossible<Value, Error>;
711 type SerializeTuple = Impossible<Value, Error>;
712 type SerializeTupleStruct = Impossible<Value, Error>;
713 type SerializeTupleVariant = Impossible<Value, Error>;
714 type SerializeMap = Impossible<Value, Error>;
715 type SerializeStruct = Impossible<Value, Error>;
716 type SerializeStructVariant = Impossible<Value, Error>;
717
718 fn serialize_bool(self, _v: bool) -> Result<Value> {
719 Err(invalid_number())
720 }
721
722 fn serialize_i8(self, _v: i8) -> Result<Value> {
723 Err(invalid_number())
724 }
725
726 fn serialize_i16(self, _v: i16) -> Result<Value> {
727 Err(invalid_number())
728 }
729
730 fn serialize_i32(self, _v: i32) -> Result<Value> {
731 Err(invalid_number())
732 }
733
734 fn serialize_i64(self, _v: i64) -> Result<Value> {
735 Err(invalid_number())
736 }
737
738 fn serialize_u8(self, _v: u8) -> Result<Value> {
739 Err(invalid_number())
740 }
741
742 fn serialize_u16(self, _v: u16) -> Result<Value> {
743 Err(invalid_number())
744 }
745
746 fn serialize_u32(self, _v: u32) -> Result<Value> {
747 Err(invalid_number())
748 }
749
750 fn serialize_u64(self, _v: u64) -> Result<Value> {
751 Err(invalid_number())
752 }
753
754 fn serialize_f32(self, _v: f32) -> Result<Value> {
755 Err(invalid_number())
756 }
757
758 fn serialize_f64(self, _v: f64) -> Result<Value> {
759 Err(invalid_number())
760 }
761
762 fn serialize_char(self, _v: char) -> Result<Value> {
763 Err(invalid_number())
764 }
765
766 fn serialize_str(self, value: &str) -> Result<Value> {
767 let n = tri!(value.to_owned().parse());
768 Ok(Value::Number(n))
769 }
770
771 fn serialize_bytes(self, _value: &[u8]) -> Result<Value> {
772 Err(invalid_number())
773 }
774
775 fn serialize_none(self) -> Result<Value> {
776 Err(invalid_number())
777 }
778
779 fn serialize_some<T>(self, _value: &T) -> Result<Value>
780 where
781 T: ?Sized + Serialize,
782 {
783 Err(invalid_number())
784 }
785
786 fn serialize_unit(self) -> Result<Value> {
787 Err(invalid_number())
788 }
789
790 fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
791 Err(invalid_number())
792 }
793
794 fn serialize_unit_variant(
795 self,
796 _name: &'static str,
797 _variant_index: u32,
798 _variant: &'static str,
799 ) -> Result<Value> {
800 Err(invalid_number())
801 }
802
803 fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value>
804 where
805 T: ?Sized + Serialize,
806 {
807 Err(invalid_number())
808 }
809
810 fn serialize_newtype_variant<T>(
811 self,
812 _name: &'static str,
813 _variant_index: u32,
814 _variant: &'static str,
815 _value: &T,
816 ) -> Result<Value>
817 where
818 T: ?Sized + Serialize,
819 {
820 Err(invalid_number())
821 }
822
823 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
824 Err(invalid_number())
825 }
826
827 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
828 Err(invalid_number())
829 }
830
831 fn serialize_tuple_struct(
832 self,
833 _name: &'static str,
834 _len: usize,
835 ) -> Result<Self::SerializeTupleStruct> {
836 Err(invalid_number())
837 }
838
839 fn serialize_tuple_variant(
840 self,
841 _name: &'static str,
842 _variant_index: u32,
843 _variant: &'static str,
844 _len: usize,
845 ) -> Result<Self::SerializeTupleVariant> {
846 Err(invalid_number())
847 }
848
849 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
850 Err(invalid_number())
851 }
852
853 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
854 Err(invalid_number())
855 }
856
857 fn serialize_struct_variant(
858 self,
859 _name: &'static str,
860 _variant_index: u32,
861 _variant: &'static str,
862 _len: usize,
863 ) -> Result<Self::SerializeStructVariant> {
864 Err(invalid_number())
865 }
866}
867
868#[cfg(feature = "raw_value")]
869struct RawValueEmitter;
870
871#[cfg(feature = "raw_value")]
872fn invalid_raw_value() -> Error {
873 Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0)
874}
875
876#[cfg(feature = "raw_value")]
877impl serde::ser::Serializer for RawValueEmitter {
878 type Ok = Value;
879 type Error = Error;
880
881 type SerializeSeq = Impossible<Value, Error>;
882 type SerializeTuple = Impossible<Value, Error>;
883 type SerializeTupleStruct = Impossible<Value, Error>;
884 type SerializeTupleVariant = Impossible<Value, Error>;
885 type SerializeMap = Impossible<Value, Error>;
886 type SerializeStruct = Impossible<Value, Error>;
887 type SerializeStructVariant = Impossible<Value, Error>;
888
889 fn serialize_bool(self, _v: bool) -> Result<Value> {
890 Err(invalid_raw_value())
891 }
892
893 fn serialize_i8(self, _v: i8) -> Result<Value> {
894 Err(invalid_raw_value())
895 }
896
897 fn serialize_i16(self, _v: i16) -> Result<Value> {
898 Err(invalid_raw_value())
899 }
900
901 fn serialize_i32(self, _v: i32) -> Result<Value> {
902 Err(invalid_raw_value())
903 }
904
905 fn serialize_i64(self, _v: i64) -> Result<Value> {
906 Err(invalid_raw_value())
907 }
908
909 fn serialize_u8(self, _v: u8) -> Result<Value> {
910 Err(invalid_raw_value())
911 }
912
913 fn serialize_u16(self, _v: u16) -> Result<Value> {
914 Err(invalid_raw_value())
915 }
916
917 fn serialize_u32(self, _v: u32) -> Result<Value> {
918 Err(invalid_raw_value())
919 }
920
921 fn serialize_u64(self, _v: u64) -> Result<Value> {
922 Err(invalid_raw_value())
923 }
924
925 fn serialize_f32(self, _v: f32) -> Result<Value> {
926 Err(invalid_raw_value())
927 }
928
929 fn serialize_f64(self, _v: f64) -> Result<Value> {
930 Err(invalid_raw_value())
931 }
932
933 fn serialize_char(self, _v: char) -> Result<Value> {
934 Err(invalid_raw_value())
935 }
936
937 fn serialize_str(self, value: &str) -> Result<Value> {
938 crate::from_str(value)
939 }
940
941 fn serialize_bytes(self, _value: &[u8]) -> Result<Value> {
942 Err(invalid_raw_value())
943 }
944
945 fn serialize_none(self) -> Result<Value> {
946 Err(invalid_raw_value())
947 }
948
949 fn serialize_some<T>(self, _value: &T) -> Result<Value>
950 where
951 T: ?Sized + Serialize,
952 {
953 Err(invalid_raw_value())
954 }
955
956 fn serialize_unit(self) -> Result<Value> {
957 Err(invalid_raw_value())
958 }
959
960 fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
961 Err(invalid_raw_value())
962 }
963
964 fn serialize_unit_variant(
965 self,
966 _name: &'static str,
967 _variant_index: u32,
968 _variant: &'static str,
969 ) -> Result<Value> {
970 Err(invalid_raw_value())
971 }
972
973 fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value>
974 where
975 T: ?Sized + Serialize,
976 {
977 Err(invalid_raw_value())
978 }
979
980 fn serialize_newtype_variant<T>(
981 self,
982 _name: &'static str,
983 _variant_index: u32,
984 _variant: &'static str,
985 _value: &T,
986 ) -> Result<Value>
987 where
988 T: ?Sized + Serialize,
989 {
990 Err(invalid_raw_value())
991 }
992
993 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
994 Err(invalid_raw_value())
995 }
996
997 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
998 Err(invalid_raw_value())
999 }
1000
1001 fn serialize_tuple_struct(
1002 self,
1003 _name: &'static str,
1004 _len: usize,
1005 ) -> Result<Self::SerializeTupleStruct> {
1006 Err(invalid_raw_value())
1007 }
1008
1009 fn serialize_tuple_variant(
1010 self,
1011 _name: &'static str,
1012 _variant_index: u32,
1013 _variant: &'static str,
1014 _len: usize,
1015 ) -> Result<Self::SerializeTupleVariant> {
1016 Err(invalid_raw_value())
1017 }
1018
1019 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
1020 Err(invalid_raw_value())
1021 }
1022
1023 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
1024 Err(invalid_raw_value())
1025 }
1026
1027 fn serialize_struct_variant(
1028 self,
1029 _name: &'static str,
1030 _variant_index: u32,
1031 _variant: &'static str,
1032 _len: usize,
1033 ) -> Result<Self::SerializeStructVariant> {
1034 Err(invalid_raw_value())
1035 }
1036
1037 fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
1038 where
1039 T: ?Sized + Display,
1040 {
1041 self.serialize_str(&value.to_string())
1042 }
1043}
1044