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