@@ -16,9 +16,21 @@ impl ValuesMapBuilder {
1616 Self :: default ( )
1717 }
1818
19+ /// Returns a ValuesMapBuilder populated from the given map-serializable
20+ /// value.
21+ pub fn try_from < S : Serialize > ( s : S ) -> serde_json:: Result < Self > {
22+ let value = serde_json:: to_value ( s) ?;
23+ let map = serde_json:: from_value ( value) ?;
24+ Ok ( Self ( map) )
25+ }
26+
1927 /// Inserts a string value into the map.
2028 pub fn string ( & mut self , key : impl Into < String > , value : impl Into < String > ) -> & mut Self {
21- self . entry ( key, value. into ( ) )
29+ let value = value. into ( ) ;
30+ if value. is_empty ( ) {
31+ return self ;
32+ }
33+ self . entry ( key, value)
2234 }
2335
2436 /// Inserts a string value into the map only if the given Option is Some.
@@ -39,7 +51,11 @@ impl ValuesMapBuilder {
3951 key : impl Into < String > ,
4052 iter : impl IntoIterator < Item = T > ,
4153 ) -> & mut Self {
42- self . entry ( key, iter. into_iter ( ) . map ( |s| s. into ( ) ) . collect :: < Vec < _ > > ( ) )
54+ let entries = iter. into_iter ( ) . map ( |s| s. into ( ) ) . collect :: < Vec < _ > > ( ) ;
55+ if entries. is_empty ( ) {
56+ return self ;
57+ }
58+ self . entry ( key, entries)
4359 }
4460
4561 /// Inserts an entry into the map using the value's `impl Into<Value>`.
@@ -55,7 +71,9 @@ impl ValuesMapBuilder {
5571 value : impl Serialize ,
5672 ) -> serde_json:: Result < & mut Self > {
5773 let value = serde_json:: to_value ( value) ?;
58- self . 0 . insert ( key. into ( ) , value) ;
74+ if !value. is_null ( ) {
75+ self . 0 . insert ( key. into ( ) , value) ;
76+ }
5977 Ok ( self )
6078 }
6179
0 commit comments