json_api/value/mod.rs
1//! Represent and interact with JSON API values.
2
3pub(crate) mod convert;
4mod stringify;
5
6pub mod collections;
7pub mod fields;
8
9use std::cmp::PartialEq;
10use std::fmt::{self, Formatter};
11use std::iter::FromIterator;
12use std::str::FromStr;
13
14use serde::de::{Deserialize, Deserializer, Visitor};
15use serde::ser::{Serialize, Serializer};
16
17use error::Error;
18
19pub use serde_json::value::Number;
20
21pub use self::collections::{Map, Set};
22pub use self::convert::{from_value, to_value};
23#[doc(no_inline)]
24pub use self::fields::{Key, Path};
25pub use self::stringify::Stringify;
26
27/// Represents any valid JSON API value.
28///
29/// Like [`serde_json::Value`], but with spec compliance baked into the type
30/// system.
31///
32/// [`serde_json::Value`]: https://docs.serde.rs/serde_json/enum.Value.html
33#[derive(Clone, Debug, PartialEq)]
34pub enum Value {
35 /// A null value.
36 Null,
37
38 /// An array of values.
39 Array(Vec<Value>),
40
41 /// A boolean value.
42 Bool(bool),
43
44 /// An integer or floating point value.
45 Number(Number),
46
47 /// A JSON object as a hash table with consistent order. Keys are
48 /// guarenteed to be a valid [member name].
49 ///
50 /// [member name]: http://jsonapi.org/format/#document-member-names
51 Object(Map),
52
53 /// A string value.
54 String(String),
55}
56
57impl Value {
58 /// Optionally get the underlying vector as a slice. Returns `None` if the
59 /// `Value` is not an array.
60 ///
61 /// # Example
62 ///
63 /// ```
64 /// # extern crate json_api;
65 /// #
66 /// # use json_api::Value;
67 /// #
68 /// # fn main() {
69 /// let data = vec![true.into(), false.into()];
70 /// let array = Value::Array(data.clone());
71 /// let boolean = Value::Bool(true);
72 ///
73 /// assert_eq!(array.as_array(), Some(data.as_slice()));
74 /// assert_eq!(boolean.as_array(), None);
75 /// # }
76 /// ```
77 pub fn as_array(&self) -> Option<&[Value]> {
78 match *self {
79 Value::Array(ref inner) => Some(inner),
80 _ => None,
81 }
82 }
83
84 /// Optionally get the underlying vector as a mutable slice. Returns `None`
85 /// if the `Value` is not an array.
86 ///
87 /// # Example
88 ///
89 /// ```
90 /// # extern crate json_api;
91 /// #
92 /// # use json_api::Value;
93 /// #
94 /// # fn main() {
95 /// let mut data = vec![true.into(), false.into()];
96 /// let mut array = Value::Array(data.clone());
97 /// let mut boolean = Value::Bool(true);
98 ///
99 /// assert_eq!(array.as_array_mut(), Some(data.as_mut_slice()));
100 /// assert_eq!(boolean.as_array_mut(), None);
101 /// # }
102 /// ```
103 pub fn as_array_mut(&mut self) -> Option<&mut [Value]> {
104 match *self {
105 Value::Array(ref mut inner) => Some(inner),
106 _ => None,
107 }
108 }
109
110 /// Optionally get the inner boolean value. Returns `None` if the `Value` is
111 /// not a boolean.
112 ///
113 /// # Example
114 ///
115 /// ```
116 /// # extern crate json_api;
117 /// #
118 /// # use json_api::Value;
119 /// #
120 /// # fn main() {
121 /// let boolean = Value::Bool(true);
122 /// let number = Value::from(3.14);
123 ///
124 /// assert_eq!(boolean.as_bool(), Some(true));
125 /// assert_eq!(number.as_bool(), None);
126 /// # }
127 /// ```
128 pub fn as_bool(&self) -> Option<bool> {
129 match *self {
130 Value::Bool(inner) => Some(inner),
131 _ => None,
132 }
133 }
134
135 /// Returns `Some(())` if the `Value` is null.
136 ///
137 /// # Example
138 ///
139 /// ```
140 /// # extern crate json_api;
141 /// #
142 /// # use json_api::Value;
143 /// #
144 /// # fn main() {
145 /// let null = Value::Null;
146 /// let text = Value::String("Hello, World!".to_owned());
147 ///
148 /// assert_eq!(null.as_null(), Some(()));
149 /// assert_eq!(text.as_null(), None);
150 /// # }
151 /// ```
152 pub fn as_null(&self) -> Option<()> {
153 match *self {
154 Value::Null => Some(()),
155 _ => None,
156 }
157 }
158
159 /// Optionally get a reference to the inner map. Returns `None` if the
160 /// `Value` is not an object.
161 ///
162 /// # Example
163 ///
164 /// ```
165 /// # extern crate json_api;
166 /// #
167 /// # use json_api::value::{Map, Value};
168 /// #
169 /// # fn main() {
170 /// let data = Map::new();
171 /// let object = Value::Object(data.clone());
172 /// let number = Value::from(3.14);
173 ///
174 /// assert_eq!(object.as_object(), Some(&data));
175 /// assert_eq!(number.as_object(), None);
176 /// # }
177 /// ```
178 pub fn as_object(&self) -> Option<&Map> {
179 match *self {
180 Value::Object(ref inner) => Some(inner),
181 _ => None,
182 }
183 }
184
185 /// Optionally get a mutable reference to the inner map. Returns `None` if
186 /// the `Value` is not an object.
187 ///
188 /// # Example
189 ///
190 /// ```
191 /// # extern crate json_api;
192 /// #
193 /// # use json_api::value::{Map, Value};
194 /// #
195 /// # fn main() {
196 /// let mut data = Map::new();
197 /// let mut object = Value::Object(data.clone());
198 /// let mut number = Value::from(3.14);
199 ///
200 /// assert_eq!(object.as_object_mut(), Some(&mut data));
201 /// assert_eq!(number.as_object_mut(), None);
202 /// # }
203 /// ```
204 pub fn as_object_mut(&mut self) -> Option<&mut Map> {
205 match *self {
206 Value::Object(ref mut inner) => Some(inner),
207 _ => None,
208 }
209 }
210
211 /// Optionally get the underlying string as a string slice. Returns `None`
212 /// if the `Value` is not a string.
213 ///
214 /// # Example
215 ///
216 /// ```
217 /// # extern crate json_api;
218 /// #
219 /// # use json_api::Value;
220 /// #
221 /// # fn main() {
222 /// let data = "Hello, World!";
223 /// let string = Value::String(data.to_owned());
224 /// let number = Value::from(3.14);
225 ///
226 /// assert_eq!(string.as_str(), Some(data));
227 /// assert_eq!(number.as_str(), None);
228 /// # }
229 /// ```
230 pub fn as_str(&self) -> Option<&str> {
231 match *self {
232 Value::String(ref inner) => Some(inner),
233 _ => None,
234 }
235 }
236
237 /// Optionally get the underlying number as an `f64`. Returns `None` if the
238 /// `Value` cannot be represented as an `f64`.
239 ///
240 /// # Example
241 ///
242 /// ```
243 /// # extern crate json_api;
244 /// #
245 /// # use json_api::Value;
246 /// #
247 /// # fn main() {
248 /// let number = Value::from(3.14);
249 /// let string = Value::String("Hello, World!".to_owned());
250 ///
251 /// assert_eq!(number.as_f64(), Some(3.14));
252 /// assert_eq!(string.as_f64(), None);
253 /// # }
254 /// ```
255 pub fn as_f64(&self) -> Option<f64> {
256 match *self {
257 Value::Number(ref n) => n.as_f64(),
258 _ => None,
259 }
260 }
261
262 /// Optionally get the underlying number as an `i64`. Returns `None` if the
263 /// `Value` cannot be represented as an `i64`.
264 ///
265 /// # Example
266 ///
267 /// ```
268 /// # extern crate json_api;
269 /// #
270 /// # use json_api::Value;
271 /// #
272 /// # fn main() {
273 /// let integer = Value::from(10);
274 /// let float = Value::from(3.14);
275 ///
276 /// assert_eq!(integer.as_i64(), Some(10));
277 /// assert_eq!(float.as_i64(), None);
278 /// # }
279 /// ```
280 pub fn as_i64(&self) -> Option<i64> {
281 match *self {
282 Value::Number(ref n) => n.as_i64(),
283 _ => None,
284 }
285 }
286
287 /// Optionally get the underlying number as an `u64`. Returns `None` if the
288 /// `Value` cannot be represented as an `u64`.
289 ///
290 /// # Example
291 ///
292 /// ```
293 /// # extern crate json_api;
294 /// #
295 /// # use json_api::Value;
296 /// #
297 /// # fn main() {
298 /// let positive = Value::from(10);
299 /// let negative = Value::from(-10);
300 ///
301 /// assert_eq!(positive.as_u64(), Some(10));
302 /// assert_eq!(negative.as_u64(), None);
303 /// # }
304 /// ```
305 pub fn as_u64(&self) -> Option<u64> {
306 match *self {
307 Value::Number(ref n) => n.as_u64(),
308 _ => None,
309 }
310 }
311
312 /// Returns true if the `Value` is an array.
313 ///
314 /// For any `Value` on which `is_array` returns true, [`as_array`] and
315 /// [`as_array_mut`] are guaranteed to return a reference to the vector
316 /// representing the array.
317 ///
318 /// # Example
319 ///
320 /// ```
321 /// # extern crate json_api;
322 /// #
323 /// # use json_api::Value;
324 /// #
325 /// # fn main() {
326 /// let mut value = Value::from(vec![1, 2, 3]);
327 ///
328 /// assert!(value.is_array());
329 ///
330 /// value.as_array().unwrap();
331 /// value.as_array_mut().unwrap();
332 /// # }
333 /// ```
334 ///
335 /// [`as_array`]: #method.as_array
336 /// [`as_array_mut`]: #method.as_array_mut
337 pub fn is_array(&self) -> bool {
338 match *self {
339 Value::Array(_) => true,
340 _ => false,
341 }
342 }
343
344 /// Returns true if the `Value` is a boolean.
345 ///
346 /// For any `Value` on which `is_boolean` returns true, [`as_bool`] is
347 /// guaranteed to return the boolean value.
348 ///
349 /// # Example
350 ///
351 /// ```
352 /// # extern crate json_api;
353 /// #
354 /// # use json_api::Value;
355 /// #
356 /// # fn main() {
357 /// let value = Value::Bool(true);
358 ///
359 /// assert!(value.is_boolean());
360 /// value.as_bool().unwrap();
361 /// # }
362 /// ```
363 ///
364 /// [`as_bool`]: #method.as_bool
365 pub fn is_boolean(&self) -> bool {
366 match *self {
367 Value::Bool(_) => true,
368 _ => false,
369 }
370 }
371
372 /// Returns true if the `Value` is null.
373 ///
374 /// For any `Value` on which `is_null` returns true, [`as_null`] is
375 /// guaranteed to return `Some(())`.
376 ///
377 /// # Example
378 ///
379 /// ```
380 /// # extern crate json_api;
381 /// #
382 /// # use json_api::Value;
383 /// #
384 /// # fn main() {
385 /// let value = Value::Null;
386 ///
387 /// assert!(value.is_null());
388 /// value.as_null().unwrap();
389 /// # }
390 /// ```
391 ///
392 /// [`as_null`]: #method.as_null
393 pub fn is_null(&self) -> bool {
394 match *self {
395 Value::Null => true,
396 _ => false,
397 }
398 }
399
400 /// Returns true if the `Value` is a number.
401 ///
402 /// # Example
403 ///
404 /// ```
405 /// # extern crate json_api;
406 /// #
407 /// # use json_api::Value;
408 /// #
409 /// # fn main() {
410 /// assert!(Value::from(3.14).is_number());
411 /// # }
412 /// ```
413 pub fn is_number(&self) -> bool {
414 match *self {
415 Value::Number(_) => true,
416 _ => false,
417 }
418 }
419
420 /// Returns true if the `Value` is an object.
421 ///
422 /// For any `Value` on which `is_array` returns true, [`as_object`] and
423 /// [`as_object_mut`] are guaranteed to return a reference to the map
424 /// representing the object.
425 ///
426 /// # Example
427 ///
428 /// ```
429 /// # extern crate json_api;
430 /// #
431 /// # use json_api::Value;
432 /// #
433 /// # fn main() {
434 /// let mut value = Value::Object(Default::default());
435 ///
436 /// assert!(value.is_object());
437 ///
438 /// value.as_object().unwrap();
439 /// value.as_object_mut().unwrap();
440 /// # }
441 /// ```
442 ///
443 /// [`as_object`]: #method.as_object
444 /// [`as_object_mut`]: #method.as_object_mut
445 pub fn is_object(&self) -> bool {
446 match *self {
447 Value::Object(_) => true,
448 _ => false,
449 }
450 }
451
452 /// Returns true if the `Value` is a string.
453 ///
454 /// For any `Value` on which `is_string` returns true, [`as_str`] is
455 /// guaranteed to return the string slice.
456 ///
457 /// # Example
458 ///
459 /// ```
460 /// # extern crate json_api;
461 /// #
462 /// # use json_api::Value;
463 /// #
464 /// # fn main() {
465 /// let value = Value::String("Hello, world!".to_owned());
466 ///
467 /// assert!(value.is_string());
468 /// value.as_str().unwrap();
469 /// # }
470 /// ```
471 ///
472 /// [`as_str`]: #method.as_str
473 pub fn is_string(&self) -> bool {
474 match *self {
475 Value::String(_) => true,
476 _ => false,
477 }
478 }
479
480 /// Returns true if the `Value` is a number that can be represented as an
481 /// `f64`.
482 ///
483 /// For any `Value` on which `is_f64` returns true, [`as_f64`] is
484 /// guaranteed to return the floating point value.
485 ///
486 /// Currently this function returns true if and only if both [`is_i64`] and
487 /// [`is_u64`] return false. This behavior is not a guarantee in the future.
488 ///
489 /// # Example
490 ///
491 /// ```
492 /// # extern crate json_api;
493 /// #
494 /// # use json_api::Value;
495 /// #
496 /// # fn main() {
497 /// let value = Value::from(3.14);
498 ///
499 /// assert!(value.is_f64());
500 /// value.as_f64().unwrap();
501 /// # }
502 /// ```
503 ///
504 /// [`as_f64`]: #method.as_f64
505 /// [`is_i64`]: #method.is_i64
506 /// [`is_u64`]: #method.is_u64
507 pub fn is_f64(&self) -> bool {
508 match *self {
509 Value::Number(ref n) => n.is_f64(),
510 _ => false,
511 }
512 }
513
514 /// Returns true if the `Value` is an integer between `i64::MIN` and
515 /// `i64::MAX`.
516 ///
517 /// For any Value on which `is_i64` returns true, [`as_i64`] is guaranteed
518 /// to return the integer value.
519 ///
520 /// # Example
521 ///
522 /// ```
523 /// # extern crate json_api;
524 /// #
525 /// # use json_api::Value;
526 /// #
527 /// # fn main() {
528 /// let pos = Value::from(3);
529 /// let neg = Value::from(-3);
530 ///
531 /// assert!(pos.is_i64());
532 /// assert!(neg.is_i64());
533 ///
534 /// pos.as_i64().unwrap();
535 /// neg.as_i64().unwrap();
536 /// # }
537 /// ```
538 ///
539 /// [`as_i64`]: #method.as_i64
540 pub fn is_i64(&self) -> bool {
541 match *self {
542 Value::Number(ref n) => n.is_i64(),
543 _ => false,
544 }
545 }
546
547 /// Returns true if the `Value` is an integer between `0` and `u64::MAX`.
548 ///
549 /// For any Value on which `is_u64` returns true, [`as_u64`] is guaranteed
550 /// to return the integer value.
551 ///
552 /// # Example
553 ///
554 /// ```
555 /// # extern crate json_api;
556 /// #
557 /// # use json_api::Value;
558 /// #
559 /// # fn main() {
560 /// let value = Value::from(3);
561 ///
562 /// assert!(value.is_u64());
563 /// value.as_u64().unwrap();
564 /// # }
565 /// ```
566 ///
567 /// [`as_u64`]: #method.as_u64
568 pub fn is_u64(&self) -> bool {
569 match *self {
570 Value::Number(ref n) => n.is_u64(),
571 _ => false,
572 }
573 }
574}
575
576/// Returns the `Value::Null`. This allows for better composition with `Option`
577/// types.
578///
579/// # Example
580///
581/// ```
582/// # extern crate json_api;
583/// #
584/// # use json_api::Value;
585/// #
586/// # fn main() {
587/// const MSG: &'static str = "Hello, World!";
588///
589/// let opt = None;
590/// let value = opt.map(Value::String).unwrap_or_default();
591/// assert_eq!(value, Value::Null);
592///
593/// let opt = Some(MSG.to_owned());
594/// let value = opt.map(Value::String).unwrap_or_default();
595/// assert_eq!(value, Value::String(MSG.to_owned()));
596/// # }
597/// ```
598impl Default for Value {
599 fn default() -> Self {
600 Value::Null
601 }
602}
603
604impl From<bool> for Value {
605 fn from(inner: bool) -> Self {
606 Value::Bool(inner)
607 }
608}
609
610impl From<f32> for Value {
611 fn from(n: f32) -> Self {
612 Value::from(f64::from(n))
613 }
614}
615
616impl From<f64> for Value {
617 fn from(n: f64) -> Self {
618 Number::from_f64(n).map(Value::Number).unwrap_or_default()
619 }
620}
621
622impl From<i8> for Value {
623 fn from(n: i8) -> Self {
624 Value::from(i64::from(n))
625 }
626}
627
628impl From<i16> for Value {
629 fn from(n: i16) -> Self {
630 Value::from(i64::from(n))
631 }
632}
633
634impl From<i32> for Value {
635 fn from(n: i32) -> Self {
636 Value::from(i64::from(n))
637 }
638}
639
640impl From<i64> for Value {
641 fn from(n: i64) -> Self {
642 Value::Number(n.into())
643 }
644}
645
646impl From<u8> for Value {
647 fn from(n: u8) -> Self {
648 Value::from(u64::from(n))
649 }
650}
651
652impl From<u16> for Value {
653 fn from(n: u16) -> Self {
654 Value::from(u64::from(n))
655 }
656}
657
658impl From<u32> for Value {
659 fn from(n: u32) -> Self {
660 Value::from(u64::from(n))
661 }
662}
663
664impl From<u64> for Value {
665 fn from(n: u64) -> Self {
666 Value::Number(n.into())
667 }
668}
669
670impl From<String> for Value {
671 fn from(s: String) -> Self {
672 Value::String(s)
673 }
674}
675
676impl From<Map> for Value {
677 fn from(data: Map) -> Self {
678 Value::Object(data)
679 }
680}
681
682impl<T> From<Option<T>> for Value
683where
684 T: Into<Value>,
685{
686 fn from(data: Option<T>) -> Self {
687 data.map(T::into).unwrap_or_default()
688 }
689}
690
691impl<T> From<Vec<T>> for Value
692where
693 T: Into<Value>,
694{
695 fn from(data: Vec<T>) -> Self {
696 Value::Array(data.into_iter().map(|i| i.into()).collect())
697 }
698}
699
700impl<'a> From<&'a str> for Value {
701 fn from(s: &'a str) -> Self {
702 Value::String(s.to_owned())
703 }
704}
705
706impl<'a, T> From<&'a [T]> for Value
707where
708 T: Clone + Into<Value>,
709{
710 fn from(data: &'a [T]) -> Self {
711 Value::Array(data.iter().cloned().map(|i| i.into()).collect())
712 }
713}
714
715impl<T> FromIterator<T> for Value
716where
717 T: Into<Value>,
718{
719 fn from_iter<I>(iter: I) -> Self
720 where
721 I: IntoIterator<Item = T>,
722 {
723 Value::Array(iter.into_iter().map(|i| i.into()).collect())
724 }
725}
726
727impl FromIterator<(Key, Value)> for Value {
728 fn from_iter<I>(iter: I) -> Self
729 where
730 I: IntoIterator<Item = (Key, Value)>,
731 {
732 Value::Object(Map::from_iter(iter))
733 }
734}
735
736impl FromStr for Value {
737 type Err = Error;
738
739 fn from_str(src: &str) -> Result<Self, Self::Err> {
740 convert::from_json(src.parse()?)
741 }
742}
743
744impl PartialEq<bool> for Value {
745 fn eq(&self, rhs: &bool) -> bool {
746 self.as_bool().map_or(false, |lhs| lhs == *rhs)
747 }
748}
749
750impl PartialEq<f32> for Value {
751 fn eq(&self, rhs: &f32) -> bool {
752 *self == f64::from(*rhs)
753 }
754}
755
756impl PartialEq<f64> for Value {
757 fn eq(&self, rhs: &f64) -> bool {
758 self.as_f64().map_or(false, |lhs| lhs == *rhs)
759 }
760}
761
762impl PartialEq<i8> for Value {
763 fn eq(&self, rhs: &i8) -> bool {
764 *self == i64::from(*rhs)
765 }
766}
767
768impl PartialEq<i16> for Value {
769 fn eq(&self, rhs: &i16) -> bool {
770 *self == i64::from(*rhs)
771 }
772}
773
774impl PartialEq<i32> for Value {
775 fn eq(&self, rhs: &i32) -> bool {
776 *self == i64::from(*rhs)
777 }
778}
779
780impl PartialEq<i64> for Value {
781 fn eq(&self, rhs: &i64) -> bool {
782 self.as_i64().map_or(false, |lhs| lhs == *rhs)
783 }
784}
785
786impl PartialEq<isize> for Value {
787 fn eq(&self, rhs: &isize) -> bool {
788 *self == (*rhs as i64)
789 }
790}
791
792impl PartialEq<u8> for Value {
793 fn eq(&self, rhs: &u8) -> bool {
794 *self == u64::from(*rhs)
795 }
796}
797
798impl PartialEq<u16> for Value {
799 fn eq(&self, rhs: &u16) -> bool {
800 *self == u64::from(*rhs)
801 }
802}
803
804impl PartialEq<u32> for Value {
805 fn eq(&self, rhs: &u32) -> bool {
806 *self == u64::from(*rhs)
807 }
808}
809
810impl PartialEq<u64> for Value {
811 fn eq(&self, rhs: &u64) -> bool {
812 self.as_u64().map_or(false, |lhs| lhs == *rhs)
813 }
814}
815
816impl PartialEq<usize> for Value {
817 fn eq(&self, rhs: &usize) -> bool {
818 *self == (*rhs as u64)
819 }
820}
821
822impl PartialEq<str> for Value {
823 fn eq(&self, rhs: &str) -> bool {
824 self.as_str().map_or(false, |lhs| lhs == rhs)
825 }
826}
827
828impl<'de> Deserialize<'de> for Value {
829 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
830 where
831 D: Deserializer<'de>,
832 {
833 use serde::de::{Error, MapAccess, SeqAccess};
834
835 struct ValueVisitor;
836
837 impl<'de> Visitor<'de> for ValueVisitor {
838 type Value = Value;
839
840 fn expecting(&self, f: &mut Formatter) -> fmt::Result {
841 f.write_str("any valid JSON API value")
842 }
843
844 fn visit_bool<E>(self, value: bool) -> Result<Value, E> {
845 Ok(Value::Bool(value))
846 }
847
848 fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
849 Ok(Value::from(value))
850 }
851
852 fn visit_i64<E>(self, value: i64) -> Result<Value, E> {
853 Ok(Value::Number(value.into()))
854 }
855
856 fn visit_u64<E>(self, value: u64) -> Result<Value, E> {
857 Ok(Value::Number(value.into()))
858 }
859
860 fn visit_str<E: Error>(self, value: &str) -> Result<Value, E> {
861 self.visit_string(String::from(value))
862 }
863
864 fn visit_string<E>(self, value: String) -> Result<Value, E> {
865 Ok(Value::String(value))
866 }
867
868 fn visit_none<E>(self) -> Result<Value, E> {
869 Ok(Value::Null)
870 }
871
872 fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error>
873 where
874 D: Deserializer<'de>,
875 {
876 Deserialize::deserialize(deserializer)
877 }
878
879 fn visit_unit<E>(self) -> Result<Value, E> {
880 Ok(Value::Null)
881 }
882
883 fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
884 where
885 A: MapAccess<'de>,
886 {
887 let mut map = Map::with_capacity(access.size_hint().unwrap_or(0));
888
889 while let Some(key) = access.next_key::<String>()? {
890 let key = key.parse().map_err(Error::custom)?;
891 let value = access.next_value()?;
892
893 map.insert(key, value);
894 }
895
896 Ok(Value::Object(map))
897 }
898
899 fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error>
900 where
901 A: SeqAccess<'de>,
902 {
903 let mut array = Vec::with_capacity(access.size_hint().unwrap_or(0));
904
905 while let Some(value) = access.next_element()? {
906 array.push(value);
907 }
908
909 Ok(Value::Array(array))
910 }
911 }
912
913 deserializer.deserialize_any(ValueVisitor)
914 }
915}
916
917impl Serialize for Value {
918 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
919 where
920 S: Serializer,
921 {
922 match *self {
923 Value::Null => serializer.serialize_none(),
924 Value::Array(ref value) => value.serialize(serializer),
925 Value::Bool(value) => serializer.serialize_bool(value),
926 Value::Number(ref value) => value.serialize(serializer),
927 Value::Object(ref value) => value.serialize(serializer),
928 Value::String(ref value) => serializer.serialize_str(value),
929 }
930 }
931}