@@ -597,6 +597,64 @@ where
597597 }
598598}
599599
600+ #[ cfg( feature = "serde" ) ]
601+ pub ( super ) mod serde {
602+ use super :: * ;
603+ use :: serde:: {
604+ de:: { MapAccess , Visitor } ,
605+ ser:: SerializeMap ,
606+ Deserialize , Deserializer , Serialize , Serializer ,
607+ } ;
608+ use std:: fmt:: Formatter ;
609+
610+ impl Serialize for Dictionary {
611+ #[ inline]
612+ fn serialize < S > ( & self , ser : S ) -> Result < S :: Ok , S :: Error >
613+ where
614+ S : Serializer ,
615+ {
616+ let mut ser = ser. serialize_map ( Some ( self . len ( ) as usize ) ) ?;
617+ for ( key, value) in self . iter ( ) {
618+ ser. serialize_entry ( & key, & value) ?
619+ }
620+ ser. end ( )
621+ }
622+ }
623+
624+ pub ( in super :: super ) struct DictionaryVisitor ;
625+
626+ impl < ' de > Visitor < ' de > for DictionaryVisitor {
627+ type Value = Dictionary < Unique > ;
628+
629+ fn expecting ( & self , formatter : & mut Formatter ) -> fmt:: Result {
630+ formatter. write_str ( "a Dictionary" )
631+ }
632+
633+ fn visit_map < A > ( self , mut map : A ) -> Result < Self :: Value , A :: Error >
634+ where
635+ A : MapAccess < ' de > ,
636+ {
637+ let dict = Dictionary :: new ( ) ;
638+ while let Some ( ( key, value) ) = map. next_entry :: < Variant , Variant > ( ) ? {
639+ dict. insert ( key, value)
640+ }
641+ Ok ( dict)
642+ }
643+ }
644+
645+ impl < ' de , Access : ThreadAccess > Deserialize < ' de > for Dictionary < Access > {
646+ #[ inline]
647+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
648+ where
649+ D : Deserializer < ' de > ,
650+ {
651+ deserializer
652+ . deserialize_map ( DictionaryVisitor )
653+ . map ( |dict| unsafe { dict. cast_access ( ) } )
654+ }
655+ }
656+ }
657+
600658godot_test ! ( test_dictionary {
601659 use std:: collections:: HashSet ;
602660
0 commit comments