@@ -573,6 +573,64 @@ where
573573 }
574574}
575575
576+ #[ cfg( feature = "serde" ) ]
577+ pub ( super ) mod serde {
578+ use super :: * ;
579+ use :: serde:: {
580+ de:: { MapAccess , Visitor } ,
581+ ser:: SerializeMap ,
582+ Deserialize , Deserializer , Serialize , Serializer ,
583+ } ;
584+ use std:: fmt:: Formatter ;
585+
586+ impl Serialize for Dictionary {
587+ #[ inline]
588+ fn serialize < S > ( & self , ser : S ) -> Result < S :: Ok , S :: Error >
589+ where
590+ S : Serializer ,
591+ {
592+ let mut ser = ser. serialize_map ( Some ( self . len ( ) as usize ) ) ?;
593+ for ( key, value) in self . iter ( ) {
594+ ser. serialize_entry ( & key, & value) ?
595+ }
596+ ser. end ( )
597+ }
598+ }
599+
600+ pub ( in super :: super ) struct DictionaryVisitor ;
601+
602+ impl < ' de > Visitor < ' de > for DictionaryVisitor {
603+ type Value = Dictionary < Unique > ;
604+
605+ fn expecting ( & self , formatter : & mut Formatter ) -> fmt:: Result {
606+ formatter. write_str ( "a Dictionary" )
607+ }
608+
609+ fn visit_map < A > ( self , mut map : A ) -> Result < Self :: Value , A :: Error >
610+ where
611+ A : MapAccess < ' de > ,
612+ {
613+ let dict = Dictionary :: new ( ) ;
614+ while let Some ( ( key, value) ) = map. next_entry :: < Variant , Variant > ( ) ? {
615+ dict. insert ( key, value)
616+ }
617+ Ok ( dict)
618+ }
619+ }
620+
621+ impl < ' de , Access : ThreadAccess > Deserialize < ' de > for Dictionary < Access > {
622+ #[ inline]
623+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
624+ where
625+ D : Deserializer < ' de > ,
626+ {
627+ deserializer
628+ . deserialize_map ( DictionaryVisitor )
629+ . map ( |dict| unsafe { dict. cast_access ( ) } )
630+ }
631+ }
632+ }
633+
576634godot_test ! ( test_dictionary {
577635 use std:: collections:: HashSet ;
578636
0 commit comments