11use crate :: {
2- serde :: type_fields , ArrayInfo , DynamicArray , DynamicEnum , DynamicList , DynamicMap ,
3- DynamicStruct , DynamicTuple , DynamicTupleStruct , EnumInfo , ListInfo , Map , MapInfo , NamedField ,
4- Reflect , ReflectDeserialize , StructInfo , StructVariantInfo , Tuple , TupleInfo , TupleStruct ,
5- TupleStructInfo , TupleVariantInfo , TypeInfo , TypeRegistry , UnnamedField , VariantInfo ,
2+ ArrayInfo , DynamicArray , DynamicEnum , DynamicList , DynamicMap , DynamicStruct , DynamicTuple ,
3+ DynamicTupleStruct , EnumInfo , ListInfo , Map , MapInfo , NamedField , Reflect , ReflectDeserialize ,
4+ StructInfo , StructVariantInfo , Tuple , TupleInfo , TupleStruct , TupleStructInfo ,
5+ TupleVariantInfo , TypeInfo , TypeRegistry , UnnamedField , VariantInfo ,
66} ;
77use erased_serde:: Deserializer ;
88use serde:: de:: { self , DeserializeSeed , Error , MapAccess , SeqAccess , Visitor } ;
@@ -137,42 +137,19 @@ impl<'a, 'de> Visitor<'de> for UntypedReflectDeserializerVisitor<'a> {
137137 where
138138 A : MapAccess < ' de > ,
139139 {
140- let type_name = match map. next_key :: < & str > ( ) ? {
141- Some ( type_fields:: TYPE ) => map. next_value :: < & str > ( ) ?,
142- Some ( type_fields:: VALUE ) => {
143- // `type` must come before `value`.
144- return Err ( de:: Error :: missing_field ( type_fields:: TYPE ) ) ;
145- }
146- Some ( field) => {
147- return Err ( de:: Error :: unknown_field ( field, & [ type_fields:: TYPE ] ) ) ;
148- }
149- None => {
150- return Err ( de:: Error :: invalid_length (
151- 0 ,
152- & "two entries: `type` and `value`" ,
153- ) ) ;
154- }
155- } ;
140+ let type_name = map
141+ . next_key :: < & str > ( ) ?
142+ . ok_or_else ( || Error :: invalid_length ( 0 , & "at least one entry" ) ) ?;
156143
157- match map. next_key :: < & str > ( ) ? {
158- Some ( type_fields:: VALUE ) => {
159- let registration = self . registry . get_with_name ( type_name) . ok_or_else ( || {
160- de:: Error :: custom ( format_args ! ( "No registration found for {}" , type_name) )
161- } ) ?;
162- let type_info = registration. type_info ( ) ;
163- let value = map. next_value_seed ( TypedReflectDeserializer {
164- type_info,
165- registry : self . registry ,
166- } ) ?;
167- Ok ( value)
168- }
169- Some ( type_fields:: TYPE ) => Err ( de:: Error :: duplicate_field ( type_fields:: TYPE ) ) ,
170- Some ( field) => Err ( de:: Error :: unknown_field ( field, & [ type_fields:: VALUE ] ) ) ,
171- None => Err ( de:: Error :: invalid_length (
172- 0 ,
173- & "two entries: `type` and `value`" ,
174- ) ) ,
175- }
144+ let registration = self . registry . get_with_name ( type_name) . ok_or_else ( || {
145+ Error :: custom ( format_args ! ( "No registration found for {}" , type_name) )
146+ } ) ?;
147+ let type_info = registration. type_info ( ) ;
148+ let value = map. next_value_seed ( TypedReflectDeserializer {
149+ type_info,
150+ registry : self . registry ,
151+ } ) ?;
152+ Ok ( value)
176153 }
177154}
178155
@@ -397,8 +374,8 @@ impl<'a, 'de> Visitor<'de> for TupleVisitor<'a> {
397374 }
398375
399376 fn visit_seq < V > ( self , mut seq : V ) -> Result < Self :: Value , V :: Error >
400- where
401- V : SeqAccess < ' de > ,
377+ where
378+ V : SeqAccess < ' de > ,
402379 {
403380 visit_tuple ( & mut seq, self . tuple_info , self . registry )
404381 }
@@ -819,8 +796,7 @@ mod tests {
819796
820797 // === Unit Variant === //
821798 let input = r#"{
822- "type": "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum",
823- "value": {
799+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
824800 "Unit": (),
825801 },
826802 }"# ;
@@ -833,8 +809,7 @@ mod tests {
833809
834810 // === NewType Variant === //
835811 let input = r#"{
836- "type": "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum",
837- "value": {
812+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
838813 "NewType": (123),
839814 },
840815 }"# ;
@@ -847,8 +822,7 @@ mod tests {
847822
848823 // === Tuple Variant === //
849824 let input = r#"{
850- "type": "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum",
851- "value": {
825+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
852826 "Tuple": (1.23, 3.21),
853827 },
854828 }"# ;
@@ -861,8 +835,7 @@ mod tests {
861835
862836 // === Struct Variant === //
863837 let input = r#"{
864- "type": "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum",
865- "value": {
838+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
866839 "Struct": {
867840 "value": "I <3 Enums",
868841 },
@@ -899,8 +872,7 @@ mod tests {
899872 } ;
900873
901874 let input = r#"{
902- "type": "bevy_reflect::serde::de::tests::MyStruct",
903- "value": {
875+ "bevy_reflect::serde::de::tests::MyStruct": {
904876 "primitive_value": 123,
905877 "option_value": Some("Hello world!"),
906878 "tuple_value": (
@@ -951,8 +923,7 @@ mod tests {
951923 #[ test]
952924 fn should_deserialize_value ( ) {
953925 let input = r#"{
954- "type": "f32",
955- "value": 1.23,
926+ "f32": 1.23,
956927 }"# ;
957928
958929 let registry = get_registry ( ) ;
0 commit comments