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