@@ -62,7 +62,9 @@ impl ser::Error for Error {
62
62
}
63
63
}
64
64
65
- struct Serializer ;
65
+ struct Serializer {
66
+ named_structs : bool
67
+ }
66
68
67
69
/// Convert a `T` into `rmpv::Value` which is an enum that can represent any valid MessagePack data.
68
70
///
@@ -77,7 +79,27 @@ struct Serializer;
77
79
/// ```
78
80
#[ inline]
79
81
pub fn to_value < T : Serialize > ( value : T ) -> Result < Value , Error > {
80
- value. serialize ( Serializer )
82
+ value. serialize ( Serializer {
83
+ named_structs : false
84
+ } )
85
+ }
86
+
87
+ /// The same as `rmpv::ext::to_value` with the variation that Structs are encoded as Value::Map
88
+ ///
89
+ /// ```rust
90
+ /// # use rmpv::Value;
91
+ ///
92
+ /// struct Book { name: String }
93
+ ///
94
+ /// let val = rmpv::ext::to_value_named(Book { name: "James".into() }).unwrap();
95
+ ///
96
+ /// assert_eq!(Value::Map(vec![(Value::String("name"), Value::String("John Smith".into()))]), val);
97
+ /// ```
98
+ #[ inline]
99
+ pub fn to_value_named < T : Serialize > ( value : T ) -> Result < Value , Error > {
100
+ value. serialize ( Serializer {
101
+ named_structs : true
102
+ } )
81
103
}
82
104
83
105
impl ser:: Serializer for Serializer {
@@ -89,7 +111,7 @@ impl ser::Serializer for Serializer {
89
111
type SerializeTupleStruct = SerializeVec ;
90
112
type SerializeTupleVariant = SerializeTupleVariant ;
91
113
type SerializeMap = DefaultSerializeMap ;
92
- type SerializeStruct = SerializeVec ;
114
+ type SerializeStruct = DefaultSerializeStruct ;
93
115
type SerializeStructVariant = SerializeStructVariant ;
94
116
95
117
#[ inline]
@@ -251,8 +273,12 @@ impl ser::Serializer for Serializer {
251
273
}
252
274
253
275
#[ inline]
254
- fn serialize_struct ( self , name : & ' static str , len : usize ) -> Result < Self :: SerializeStruct , Error > {
255
- self . serialize_tuple_struct ( name, len)
276
+ fn serialize_struct ( self , _name : & ' static str , len : usize ) -> Result < Self :: SerializeStruct , Error > {
277
+ let se = DefaultSerializeStruct {
278
+ named : self . named_structs ,
279
+ map : Vec :: with_capacity ( len) ,
280
+ } ;
281
+ return Ok ( se)
256
282
}
257
283
258
284
#[ inline]
@@ -682,6 +708,12 @@ pub struct DefaultSerializeMap {
682
708
next_key : Option < Value > ,
683
709
}
684
710
711
+ #[ doc( hidden) ]
712
+ pub struct DefaultSerializeStruct {
713
+ named : bool ,
714
+ map : Vec < ( Value , Value ) > ,
715
+ }
716
+
685
717
#[ doc( hidden) ]
686
718
pub struct SerializeStructVariant {
687
719
idx : u32 ,
@@ -787,20 +819,26 @@ impl ser::SerializeMap for DefaultSerializeMap {
787
819
}
788
820
}
789
821
790
- impl SerializeStruct for SerializeVec {
822
+ impl SerializeStruct for DefaultSerializeStruct {
791
823
type Ok = Value ;
792
824
type Error = Error ;
793
825
794
826
#[ inline]
795
- fn serialize_field < T : ?Sized > ( & mut self , _key : & ' static str , value : & T ) -> Result < ( ) , Error >
827
+ fn serialize_field < T : ?Sized > ( & mut self , key : & ' static str , value : & T ) -> Result < ( ) , Error >
796
828
where T : Serialize
797
829
{
798
- ser:: SerializeSeq :: serialize_element ( self , value)
830
+ self . map . push ( ( to_value ( key) ?, to_value ( value) ?) ) ;
831
+ Ok ( ( ) )
799
832
}
800
833
801
834
#[ inline]
802
835
fn end ( self ) -> Result < Value , Error > {
803
- ser:: SerializeSeq :: end ( self )
836
+ if self . named {
837
+ return Ok ( Value :: Map ( self . map ) )
838
+ }
839
+
840
+ let stripped_keys: Vec < Value > = self . map . into_iter ( ) . map ( |( _, val) | val) . collect ( ) ;
841
+ Ok ( Value :: Array ( stripped_keys) )
804
842
}
805
843
}
806
844
0 commit comments