1use super::to_value;
2use crate::{
3 cow::Cow,
4 stry,
5 value::borrowed::{Object, Value},
6 Error, ErrorType, Result,
7};
8use crate::{ObjectHasher, StaticNode};
9use serde_ext::ser::{
10 self, Serialize, SerializeMap as SerializeMapTrait, SerializeSeq as SerializeSeqTrait,
11};
12use std::marker::PhantomData;
13
14type Impossible<T> = ser::Impossible<T, Error>;
15
16impl<'value> Serialize for Value<'value> {
17 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
18 where
19 S: ser::Serializer,
20 {
21 match self {
22 Value::Static(StaticNode::Null) => serializer.serialize_unit(),
23 Value::Static(StaticNode::Bool(b)) => serializer.serialize_bool(*b),
24 #[allow(clippy::useless_conversion)] Value::Static(StaticNode::F64(f)) => serializer.serialize_f64((*f).into()),
26 Value::Static(StaticNode::U64(i)) => serializer.serialize_u64(*i),
27 #[cfg(feature = "128bit")]
28 Value::Static(StaticNode::U128(i)) => serializer.serialize_u128(*i),
29 Value::Static(StaticNode::I64(i)) => serializer.serialize_i64(*i),
30 #[cfg(feature = "128bit")]
31 Value::Static(StaticNode::I128(i)) => serializer.serialize_i128(*i),
32 Value::String(s) => serializer.serialize_str(s),
33 Value::Array(v) => {
34 let mut seq = serializer.serialize_seq(Some(v.len()))?;
35 for e in v.as_ref() {
36 seq.serialize_element(e)?;
37 }
38 seq.end()
39 }
40 Value::Object(m) => {
41 let mut map = serializer.serialize_map(Some(m.len()))?;
42 for (k, v) in m.iter() {
43 let k: &str = k;
44 map.serialize_entry(k, v)?;
45 }
46 map.end()
47 }
48 }
49 }
50}
51
52pub struct Serializer<'se> {
53 marker: PhantomData<&'se u8>,
54}
55
56impl<'se> Default for Serializer<'se> {
57 fn default() -> Self {
58 Self {
59 marker: PhantomData,
60 }
61 }
62}
63
64impl<'se> serde::Serializer for Serializer<'se> {
65 type Ok = Value<'se>;
66 type Error = Error;
67
68 type SerializeSeq = SerializeVec<'se>;
69 type SerializeTuple = SerializeVec<'se>;
70 type SerializeTupleStruct = SerializeVec<'se>;
71 type SerializeTupleVariant = SerializeTupleVariant<'se>;
72 type SerializeMap = SerializeMap<'se>;
73 type SerializeStruct = SerializeMap<'se>;
74 type SerializeStructVariant = SerializeStructVariant<'se>;
75
76 #[cfg_attr(not(feature = "no-inline"), inline)]
77 fn serialize_bool(self, value: bool) -> Result<Value<'se>> {
78 Ok(Value::Static(StaticNode::Bool(value)))
79 }
80
81 #[cfg_attr(not(feature = "no-inline"), inline)]
82 fn serialize_i8(self, value: i8) -> Result<Value<'se>> {
83 self.serialize_i64(i64::from(value))
84 }
85
86 #[cfg_attr(not(feature = "no-inline"), inline)]
87 fn serialize_i16(self, value: i16) -> Result<Value<'se>> {
88 self.serialize_i64(i64::from(value))
89 }
90
91 #[cfg_attr(not(feature = "no-inline"), inline)]
92 fn serialize_i32(self, value: i32) -> Result<Value<'se>> {
93 self.serialize_i64(i64::from(value))
94 }
95
96 fn serialize_i64(self, value: i64) -> Result<Value<'se>> {
97 Ok(Value::Static(StaticNode::I64(value)))
98 }
99
100 #[cfg(feature = "128bit")]
101 fn serialize_i128(self, value: i128) -> Result<Value<'se>> {
102 Ok(Value::Static(StaticNode::I128(value)))
103 }
104
105 #[cfg_attr(not(feature = "no-inline"), inline)]
106 fn serialize_u8(self, value: u8) -> Result<Value<'se>> {
107 self.serialize_u64(u64::from(value))
108 }
109
110 #[cfg_attr(not(feature = "no-inline"), inline)]
111 fn serialize_u16(self, value: u16) -> Result<Value<'se>> {
112 self.serialize_u64(u64::from(value))
113 }
114
115 #[cfg_attr(not(feature = "no-inline"), inline)]
116 fn serialize_u32(self, value: u32) -> Result<Value<'se>> {
117 self.serialize_u64(u64::from(value))
118 }
119
120 #[cfg_attr(not(feature = "no-inline"), inline)]
121 fn serialize_u64(self, value: u64) -> Result<Value<'se>> {
122 Ok(Value::Static(StaticNode::U64(value)))
123 }
124
125 #[cfg(feature = "128bit")]
126 fn serialize_u128(self, value: u128) -> Result<Value<'se>> {
127 Ok(Value::Static(StaticNode::U128(value)))
128 }
129
130 #[cfg_attr(not(feature = "no-inline"), inline)]
131 fn serialize_f32(self, value: f32) -> Result<Value<'se>> {
132 self.serialize_f64(f64::from(value))
133 }
134
135 #[cfg_attr(not(feature = "no-inline"), inline)]
136 fn serialize_f64(self, value: f64) -> Result<Value<'se>> {
137 Ok(Value::Static(StaticNode::from(value)))
138 }
139
140 #[cfg_attr(not(feature = "no-inline"), inline)]
141 fn serialize_char(self, value: char) -> Result<Value<'se>> {
142 let mut s = String::new();
143 s.push(value);
144 self.serialize_str(&s)
145 }
146
147 #[cfg_attr(not(feature = "no-inline"), inline)]
148 fn serialize_str(self, value: &str) -> Result<Value<'se>> {
149 Ok(Value::from(value.to_owned()))
150 }
151
152 #[cfg_attr(not(feature = "no-inline"), inline)]
153 fn serialize_bytes(self, value: &[u8]) -> Result<Value<'se>> {
154 Ok(value.iter().copied().collect())
155 }
156
157 #[cfg_attr(not(feature = "no-inline"), inline)]
158 fn serialize_unit(self) -> Result<Value<'se>> {
159 Ok(Value::Static(StaticNode::Null))
160 }
161
162 #[cfg_attr(not(feature = "no-inline"), inline)]
163 fn serialize_unit_struct(self, _name: &'static str) -> Result<Value<'se>> {
164 self.serialize_unit()
165 }
166
167 #[cfg_attr(not(feature = "no-inline"), inline)]
168 fn serialize_unit_variant(
169 self,
170 _name: &'static str,
171 _variant_index: u32,
172 variant: &'static str,
173 ) -> Result<Value<'se>> {
174 self.serialize_str(variant)
175 }
176
177 #[cfg_attr(not(feature = "no-inline"), inline)]
178 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value<'se>>
179 where
180 T: ?Sized + Serialize,
181 {
182 value.serialize(self)
183 }
184
185 fn serialize_newtype_variant<T>(
186 self,
187 _name: &'static str,
188 _variant_index: u32,
189 variant: &'static str,
190 value: &T,
191 ) -> Result<Value<'se>>
192 where
193 T: ?Sized + Serialize,
194 {
195 let mut values = Object::with_capacity_and_hasher(1, ObjectHasher::default());
196 let x = stry!(to_value(value));
197 values.insert_nocheck(variant.into(), x);
198 Ok(Value::from(values))
199 }
200
201 #[cfg_attr(not(feature = "no-inline"), inline)]
202 fn serialize_none(self) -> Result<Value<'se>> {
203 self.serialize_unit()
204 }
205
206 #[cfg_attr(not(feature = "no-inline"), inline)]
207 fn serialize_some<T>(self, value: &T) -> Result<Value<'se>>
208 where
209 T: ?Sized + Serialize,
210 {
211 value.serialize(self)
212 }
213
214 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
215 Ok(SerializeVec {
216 vec: Vec::with_capacity(len.unwrap_or(0)),
217 })
218 }
219
220 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
221 self.serialize_seq(Some(len))
222 }
223
224 fn serialize_tuple_struct(
225 self,
226 _name: &'static str,
227 len: usize,
228 ) -> Result<Self::SerializeTupleStruct> {
229 self.serialize_seq(Some(len))
230 }
231
232 fn serialize_tuple_variant(
233 self,
234 _name: &'static str,
235 _variant_index: u32,
236 variant: &'static str,
237 len: usize,
238 ) -> Result<Self::SerializeTupleVariant> {
239 Ok(SerializeTupleVariant {
240 name: variant,
241 vec: Vec::with_capacity(len),
242 })
243 }
244
245 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
246 Ok(SerializeMap {
247 map: Object::with_capacity_and_hasher(len.unwrap_or(0), ObjectHasher::default()),
248 next_key: None,
249 })
250 }
251
252 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
253 self.serialize_map(Some(len))
254 }
255
256 fn serialize_struct_variant(
257 self,
258 _name: &'static str,
259 _variant_index: u32,
260 variant: &'static str,
261 len: usize,
262 ) -> Result<Self::SerializeStructVariant> {
263 Ok(SerializeStructVariant {
264 name: variant,
265 map: Object::with_capacity_and_hasher(len, ObjectHasher::default()),
266 })
267 }
268}
269
270pub struct SerializeVec<'se> {
271 vec: Vec<Value<'se>>,
272}
273
274pub struct SerializeTupleVariant<'se> {
275 name: &'se str,
276 vec: Vec<Value<'se>>,
277}
278
279pub struct SerializeMap<'se> {
280 map: Object<'se>,
281 next_key: Option<Cow<'se, str>>,
282}
283
284pub struct SerializeStructVariant<'se> {
285 name: &'se str,
286 map: Object<'se>,
287}
288
289impl<'se> serde::ser::SerializeSeq for SerializeVec<'se> {
290 type Ok = Value<'se>;
291 type Error = Error;
292
293 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
294 where
295 T: ?Sized + Serialize,
296 {
297 self.vec.push(stry!(to_value(value)));
298 Ok(())
299 }
300
301 fn end(self) -> Result<Value<'se>> {
302 Ok(Value::Array(Box::new(self.vec)))
303 }
304}
305
306impl<'se> serde::ser::SerializeTuple for SerializeVec<'se> {
307 type Ok = Value<'se>;
308 type Error = Error;
309
310 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
311 where
312 T: ?Sized + Serialize,
313 {
314 serde::ser::SerializeSeq::serialize_element(self, value)
315 }
316
317 fn end(self) -> Result<Value<'se>> {
318 serde::ser::SerializeSeq::end(self)
319 }
320}
321
322impl<'se> serde::ser::SerializeTupleStruct for SerializeVec<'se> {
323 type Ok = Value<'se>;
324 type Error = Error;
325
326 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
327 where
328 T: ?Sized + Serialize,
329 {
330 serde::ser::SerializeSeq::serialize_element(self, value)
331 }
332
333 fn end(self) -> Result<Value<'se>> {
334 serde::ser::SerializeSeq::end(self)
335 }
336}
337
338impl<'se> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'se> {
339 type Ok = Value<'se>;
340 type Error = Error;
341
342 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
343 where
344 T: ?Sized + Serialize,
345 {
346 self.vec.push(stry!(to_value(value)));
347 Ok(())
348 }
349
350 fn end(self) -> Result<Value<'se>> {
351 let mut object = Object::with_capacity_and_hasher(1, ObjectHasher::default());
352 object.insert_nocheck(self.name.into(), Value::Array(Box::new(self.vec)));
353
354 Ok(Value::Object(Box::new(object)))
355 }
356}
357
358impl<'se> serde::ser::SerializeMap for SerializeMap<'se> {
359 type Ok = Value<'se>;
360 type Error = Error;
361
362 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
363 where
364 T: ?Sized + Serialize,
365 {
366 self.next_key = Some(stry!(key.serialize(MapKeySerializer {
367 marker: PhantomData
368 })));
369 Ok(())
370 }
371
372 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
373 where
374 T: ?Sized + Serialize,
375 {
376 let key = self.next_key.take();
377 let key = key.expect("serialize_value called before serialize_key");
380 self.map.insert(key, stry!(to_value(value)));
381 Ok(())
382 }
383
384 fn end(self) -> Result<Value<'se>> {
385 Ok(Value::Object(Box::new(self.map)))
386 }
387}
388
389struct MapKeySerializer<'se> {
390 marker: PhantomData<&'se u8>,
391}
392
393fn key_must_be_a_string() -> Error {
394 Error::generic(ErrorType::KeyMustBeAString)
395}
396
397impl<'se> serde_ext::Serializer for MapKeySerializer<'se> {
398 type Ok = Cow<'se, str>;
399 type Error = Error;
400
401 type SerializeSeq = Impossible<Cow<'se, str>>;
402 type SerializeTuple = Impossible<Cow<'se, str>>;
403 type SerializeTupleStruct = Impossible<Cow<'se, str>>;
404 type SerializeTupleVariant = Impossible<Cow<'se, str>>;
405 type SerializeMap = Impossible<Cow<'se, str>>;
406 type SerializeStruct = Impossible<Cow<'se, str>>;
407 type SerializeStructVariant = Impossible<Cow<'se, str>>;
408
409 #[cfg_attr(not(feature = "no-inline"), inline)]
410 fn serialize_unit_variant(
411 self,
412 _name: &'static str,
413 _variant_index: u32,
414 variant: &'static str,
415 ) -> Result<Self::Ok> {
416 Ok(Cow::from(variant))
417 }
418
419 #[cfg_attr(not(feature = "no-inline"), inline)]
420 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Self::Ok>
421 where
422 T: ?Sized + Serialize,
423 {
424 value.serialize(self)
425 }
426
427 fn serialize_bool(self, _value: bool) -> Result<Self::Ok> {
428 Err(key_must_be_a_string())
429 }
430
431 fn serialize_i8(self, value: i8) -> Result<Self::Ok> {
432 Ok(value.to_string().into())
433 }
434
435 fn serialize_i16(self, value: i16) -> Result<Self::Ok> {
436 Ok(value.to_string().into())
437 }
438
439 fn serialize_i32(self, value: i32) -> Result<Self::Ok> {
440 Ok(value.to_string().into())
441 }
442
443 fn serialize_i64(self, value: i64) -> Result<Self::Ok> {
444 Ok(value.to_string().into())
445 }
446
447 fn serialize_u8(self, value: u8) -> Result<Self::Ok> {
448 Ok(value.to_string().into())
449 }
450
451 fn serialize_u16(self, value: u16) -> Result<Self::Ok> {
452 Ok(value.to_string().into())
453 }
454
455 fn serialize_u32(self, value: u32) -> Result<Self::Ok> {
456 Ok(value.to_string().into())
457 }
458
459 fn serialize_u64(self, value: u64) -> Result<Self::Ok> {
460 Ok(value.to_string().into())
461 }
462
463 fn serialize_f32(self, _value: f32) -> Result<Self::Ok> {
464 Err(key_must_be_a_string())
465 }
466
467 fn serialize_f64(self, _value: f64) -> Result<Self::Ok> {
468 Err(key_must_be_a_string())
469 }
470
471 fn serialize_char(self, value: char) -> Result<Self::Ok> {
472 Ok({
473 let mut s = String::new();
474 s.push(value);
475 s.into()
476 })
477 }
478
479 #[cfg_attr(not(feature = "no-inline"), inline)]
480 fn serialize_str(self, value: &str) -> Result<Self::Ok> {
481 Ok(Cow::from(value.to_string()))
483 }
484
485 fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok> {
486 Err(key_must_be_a_string())
487 }
488
489 fn serialize_unit(self) -> Result<Self::Ok> {
490 Err(key_must_be_a_string())
491 }
492
493 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> {
494 Err(key_must_be_a_string())
495 }
496
497 fn serialize_newtype_variant<T>(
498 self,
499 _name: &'static str,
500 _variant_index: u32,
501 _variant: &'static str,
502 _value: &T,
503 ) -> Result<Self::Ok>
504 where
505 T: ?Sized + Serialize,
506 {
507 Err(key_must_be_a_string())
508 }
509
510 fn serialize_none(self) -> Result<Self::Ok> {
511 Err(key_must_be_a_string())
512 }
513
514 fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok>
515 where
516 T: ?Sized + Serialize,
517 {
518 Err(key_must_be_a_string())
519 }
520
521 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
522 Err(key_must_be_a_string())
523 }
524
525 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
526 Err(key_must_be_a_string())
527 }
528
529 fn serialize_tuple_struct(
530 self,
531 _name: &'static str,
532 _len: usize,
533 ) -> Result<Self::SerializeTupleStruct> {
534 Err(key_must_be_a_string())
535 }
536
537 fn serialize_tuple_variant(
538 self,
539 _name: &'static str,
540 _variant_index: u32,
541 _variant: &'static str,
542 _len: usize,
543 ) -> Result<Self::SerializeTupleVariant> {
544 Err(key_must_be_a_string())
545 }
546
547 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
548 Err(key_must_be_a_string())
549 }
550
551 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
552 Err(key_must_be_a_string())
553 }
554
555 fn serialize_struct_variant(
556 self,
557 _name: &'static str,
558 _variant_index: u32,
559 _variant: &'static str,
560 _len: usize,
561 ) -> Result<Self::SerializeStructVariant> {
562 Err(key_must_be_a_string())
563 }
564}
565
566impl<'se> serde::ser::SerializeStruct for SerializeMap<'se> {
567 type Ok = Value<'se>;
568 type Error = Error;
569
570 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
571 where
572 T: ?Sized + Serialize,
573 {
574 stry!(serde::ser::SerializeMap::serialize_key(self, key));
575 serde::ser::SerializeMap::serialize_value(self, value)
576 }
577
578 fn end(self) -> Result<Value<'se>> {
579 serde::ser::SerializeMap::end(self)
580 }
581}
582
583impl<'se> serde::ser::SerializeStructVariant for SerializeStructVariant<'se> {
584 type Ok = Value<'se>;
585 type Error = Error;
586
587 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
588 where
589 T: ?Sized + Serialize,
590 {
591 self.map.insert(key.into(), stry!(to_value(value)));
592 Ok(())
593 }
594
595 fn end(self) -> Result<Value<'se>> {
596 let mut object = Object::with_capacity_and_hasher(1, ObjectHasher::default());
597 object.insert_nocheck(self.name.into(), self.map.into());
598 Ok(Value::Object(Box::new(object)))
599 }
600}
601
602#[cfg(test)]
603mod test {
604 #![allow(clippy::ignored_unit_patterns)]
605 use super::Value;
606 use crate::{borrowed::Object, serde::from_slice, ObjectHasher};
607 use serde::{Deserialize, Serialize};
608 use value_trait::StaticNode;
609
610 #[test]
611 fn null() {
612 let v = Value::Static(crate::StaticNode::Null);
613 let s = serde_json::to_string(&v).expect("Failed to serialize");
614 assert_eq!(s, "null");
615 }
616
617 #[test]
618 fn bool_true() {
619 let v = Value::Static(StaticNode::Bool(true));
620 let s = serde_json::to_string(&v).expect("Failed to serialize");
621 assert_eq!(s, "true");
622 }
623
624 #[test]
625 fn bool_false() {
626 let v = Value::Static(StaticNode::Bool(false));
627 let s = serde_json::to_string(&v).expect("Failed to serialize");
628 assert_eq!(s, "false");
629 }
630
631 #[test]
632 fn float() {
633 let v = Value::Static(StaticNode::from(1.0));
634 let s = serde_json::to_string(&v).expect("Failed to serialize");
635 assert_eq!(s, "1.0");
636 }
637
638 #[test]
639 fn stringlike() {
640 let v = Value::from("snot".to_string());
641 let s = serde_json::to_string(&v).expect("Failed to serialize");
642 assert_eq!(s, "\"snot\"");
643
644 let v = Value::from("snot");
645 let s = serde_json::to_string(&v).expect("Failed to serialize");
646 assert_eq!(s, "\"snot\"");
647 }
648
649 #[test]
650 fn int() {
651 let v = Value::Static(StaticNode::I64(42));
652 let s = serde_json::to_string(&v).expect("Failed to serialize");
653 assert_eq!(s, "42");
654 }
655
656 #[test]
657 fn arr() {
658 let v = Value::Array(Box::new(vec![
659 Value::Static(StaticNode::I64(42)),
660 Value::Static(StaticNode::I64(23)),
661 ]));
662 let s = serde_json::to_string(&v).expect("Failed to serialize");
663 assert_eq!(s, "[42,23]");
664 }
665
666 #[test]
667 fn map() {
668 let mut m = Object::with_capacity_and_hasher(2, ObjectHasher::default());
669 m.insert("a".into(), Value::from(42));
670 m.insert("b".into(), Value::from(23));
671 let v = Value::Object(Box::new(m));
672 let mut s = serde_json::to_vec(&v).expect("Failed to serialize");
673 let v2: Value = from_slice(&mut s).expect("failed to deserialize");
674 assert_eq!(v, v2);
675 }
676
677 #[derive(Deserialize, Serialize, PartialEq, Debug, Default)]
678 struct Map {
679 key: u32,
680 }
681 #[allow(clippy::struct_field_names)]
682 #[derive(Deserialize, Serialize, PartialEq, Debug, Default)]
683 struct Obj {
684 v_i128: i128,
685 v_i64: i64,
686 v_i32: i32,
687 v_i16: i16,
688 v_i8: i8,
689 v_u128: u128,
690 v_u64: u64,
691 v_usize: usize,
692 v_u32: u32,
693 v_u16: u16,
694 v_u8: u8,
695 v_bool: bool,
696 v_str: String,
697 v_char: char,
698 v_map: Map,
700 v_arr: Vec<usize>,
701 v_null: (),
702 }
703
704 #[test]
705 fn from_slice_to_object() {
706 let o = Obj::default();
707 let mut vec = serde_json::to_vec(&o).expect("to_vec");
708 let vec2 = crate::serde::to_vec(&o).expect("to_vec");
709 assert_eq!(vec, vec2);
710
711 println!("{}", serde_json::to_string_pretty(&o).expect("json"));
712 let de: Obj = from_slice(&mut vec).expect("from_slice");
713 assert_eq!(o, de);
714 }
715
716 #[cfg(not(target_arch = "wasm32"))]
717 use proptest::prelude::*;
718 #[cfg(not(target_arch = "wasm32"))]
719 prop_compose! {
720 fn obj_case()(
721 v_i128 in any::<i64>().prop_map(i128::from),
722 v_i64 in any::<i64>(),
723 v_i32 in any::<i32>(),
724 v_i16 in any::<i16>(),
725 v_i8 in any::<i8>(),
726 v_u128 in any::<u64>().prop_map(u128::from),
727 v_u64 in any::<u64>(),
728 v_usize in any::<u32>().prop_map(|v| v as usize),
729 v_u32 in any::<u32>(),
730 v_u16 in any::<u16>(),
731 v_u8 in any::<u8>(),
732 v_bool in any::<bool>(),
733 v_str in ".*",
734 v_char in any::<char>(),
735 ) -> Obj {
736 Obj {
737 v_i128,
738 v_i64,
739 v_i32,
740 v_i16,
741 v_i8,
742 v_u128,
743 v_u64,
744 v_usize,
745 v_u32,
746 v_u16,
747 v_u8,
748 v_bool,
749 v_str,
750 v_char,
751 ..Obj::default()
752 }
753 }
754 }
755
756 #[cfg(not(target_arch = "wasm32"))]
757 proptest! {
758 #![proptest_config(ProptestConfig {
759 .. ProptestConfig::default()
760 })]
761
762 #[test]
763 fn prop_deserialize_obj(obj in obj_case()) {
764 let mut vec = serde_json::to_vec(&obj).expect("to_vec");
765 let vec1 = vec.clone();
766 let vec2 = vec.clone();
767 println!("{}", serde_json::to_string_pretty(&obj).expect("json"));
768 let de: Obj = from_slice(&mut vec).expect("from_slice");
769 prop_assert_eq!(&obj, &de);
770
771 let borrowed: crate::BorrowedValue = serde_json::from_slice(& vec1).expect("from_slice");
772 let owned: crate::OwnedValue = serde_json::from_slice(& vec2).expect("from_slice");
773 prop_assert_eq!(&borrowed, &owned);
774
775 let de: Obj = Obj::deserialize(borrowed).expect("deserialize");
776 prop_assert_eq!(&obj, &de);
777 let de: Obj = Obj::deserialize(owned).expect("deserialize");
778 prop_assert_eq!(&obj, &de);
779
780
781 }
782 }
783}