11use crate :: { DynamicEntity , DynamicScene } ;
22use anyhow:: Result ;
3- use bevy_reflect:: {
4- serde :: { ReflectSerializer , UntypedReflectDeserializer } ,
5- Reflect , TypeRegistry , TypeRegistryArc ,
6- } ;
3+ use bevy_reflect:: serde :: { TypedReflectDeserializer , TypedReflectSerializer } ;
4+ use bevy_reflect :: { serde :: UntypedReflectDeserializer , Reflect , TypeRegistry , TypeRegistryArc } ;
5+ use bevy_utils :: HashSet ;
6+ use serde :: ser :: SerializeMap ;
77use serde:: {
88 de:: { DeserializeSeed , Error , MapAccess , SeqAccess , Visitor } ,
99 ser:: { SerializeSeq , SerializeStruct } ,
@@ -70,12 +70,12 @@ impl<'a> Serialize for ComponentsSerializer<'a> {
7070 where
7171 S : serde:: Serializer ,
7272 {
73- let mut state = serializer. serialize_seq ( Some ( self . components . len ( ) ) ) ?;
73+ let mut state = serializer. serialize_map ( Some ( self . components . len ( ) ) ) ?;
7474 for component in self . components {
75- state. serialize_element ( & ReflectSerializer :: new (
76- & * * component,
77- & * self . registry . read ( ) ,
78- ) ) ?;
75+ state. serialize_entry (
76+ component. type_name ( ) ,
77+ & TypedReflectSerializer :: new ( & * * component , & * self . registry . read ( ) ) ,
78+ ) ?;
7979 }
8080 state. end ( )
8181 }
@@ -188,7 +188,7 @@ impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> {
188188 return Err ( Error :: duplicate_field ( ENTITY_FIELD_COMPONENTS ) ) ;
189189 }
190190
191- components = Some ( map. next_value_seed ( ComponentVecDeserializer {
191+ components = Some ( map. next_value_seed ( ComponentDeserializer {
192192 registry : self . registry ,
193193 } ) ?) ;
194194 }
@@ -209,32 +209,55 @@ impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> {
209209 }
210210}
211211
212- pub struct ComponentVecDeserializer < ' a > {
212+ pub struct ComponentDeserializer < ' a > {
213213 pub registry : & ' a TypeRegistry ,
214214}
215215
216- impl < ' a , ' de > DeserializeSeed < ' de > for ComponentVecDeserializer < ' a > {
216+ impl < ' a , ' de > DeserializeSeed < ' de > for ComponentDeserializer < ' a > {
217217 type Value = Vec < Box < dyn Reflect > > ;
218218
219219 fn deserialize < D > ( self , deserializer : D ) -> Result < Self :: Value , D :: Error >
220220 where
221221 D : serde:: Deserializer < ' de > ,
222222 {
223- deserializer. deserialize_seq ( ComponentSeqVisitor {
223+ deserializer. deserialize_map ( ComponentVisitor {
224224 registry : self . registry ,
225225 } )
226226 }
227227}
228228
229- struct ComponentSeqVisitor < ' a > {
229+ struct ComponentVisitor < ' a > {
230230 pub registry : & ' a TypeRegistry ,
231231}
232232
233- impl < ' a , ' de > Visitor < ' de > for ComponentSeqVisitor < ' a > {
233+ impl < ' a , ' de > Visitor < ' de > for ComponentVisitor < ' a > {
234234 type Value = Vec < Box < dyn Reflect > > ;
235235
236236 fn expecting ( & self , formatter : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
237- formatter. write_str ( "list of components" )
237+ formatter. write_str ( "map of components" )
238+ }
239+
240+ fn visit_map < A > ( self , mut map : A ) -> std:: result:: Result < Self :: Value , A :: Error >
241+ where
242+ A : MapAccess < ' de > ,
243+ {
244+ let mut added = HashSet :: new ( ) ;
245+ let mut components = Vec :: new ( ) ;
246+ while let Some ( key) = map. next_key :: < & str > ( ) ? {
247+ if !added. insert ( key) {
248+ return Err ( Error :: custom ( format ! ( "duplicate component: `{}`" , key) ) ) ;
249+ }
250+
251+ let registration = self
252+ . registry
253+ . get_with_name ( key)
254+ . ok_or_else ( || Error :: custom ( format ! ( "no registration found for `{}`" , key) ) ) ?;
255+ components. push (
256+ map. next_value_seed ( TypedReflectDeserializer :: new ( registration, self . registry ) ) ?,
257+ ) ;
258+ }
259+
260+ Ok ( components)
238261 }
239262
240263 fn visit_seq < A > ( self , mut seq : A ) -> Result < Self :: Value , A :: Error >
0 commit comments