@@ -4,7 +4,6 @@ namespace AltV.Net.Elements.Args
44{
55 public static class DefaultMValueAdapters
66 {
7- //TODO: create DefaultDictionaryAdapter
87 public class DefaultArrayAdapter < T > : IMValueAdapter < List < T > >
98 {
109 private readonly IMValueAdapter < T > elementAdapter ;
@@ -57,9 +56,68 @@ public void ToMValue(object obj, IMValueWriter writer)
5756 }
5857 }
5958
59+ public class DefaultDictionaryAdapter < T > : IMValueAdapter < IDictionary < string , T > >
60+ {
61+ private readonly IMValueAdapter < T > elementAdapter ;
62+
63+ public DefaultDictionaryAdapter ( IMValueAdapter < T > elementAdapter )
64+ {
65+ this . elementAdapter = elementAdapter ;
66+ }
67+
68+ public IDictionary < string , T > FromMValue ( IMValueReader reader )
69+ {
70+ var dictionary = new Dictionary < string , T > ( ) ;
71+ reader . BeginObject ( ) ;
72+ while ( reader . HasNext ( ) )
73+ {
74+ var key = reader . NextName ( ) ;
75+ dictionary [ key ] = elementAdapter . FromMValue ( reader ) ;
76+ }
77+
78+ reader . EndObject ( ) ;
79+ return dictionary ;
80+ }
81+
82+ public void ToMValue ( IDictionary < string , T > dictionary , IMValueWriter writer )
83+ {
84+ writer . BeginObject ( ) ;
85+ foreach ( var ( key , value ) in dictionary )
86+ {
87+ writer . Name ( key ) ;
88+ elementAdapter . ToMValue ( value , writer ) ;
89+ }
90+
91+ writer . EndObject ( ) ;
92+ }
93+
94+ object IMValueBaseAdapter . FromMValue ( IMValueReader reader )
95+ {
96+ return FromMValue ( reader ) ;
97+ }
98+
99+ public void ToMValue ( object obj , IMValueWriter writer )
100+ {
101+ if ( obj is IDictionary < string , T > list )
102+ {
103+ ToMValue ( list , writer ) ;
104+ }
105+ else
106+ {
107+ writer . BeginObject ( ) ;
108+ writer . EndObject ( ) ;
109+ }
110+ }
111+ }
112+
60113 public static IMValueAdapter < List < T > > GetArrayAdapter < T > ( IMValueAdapter < T > elementAdapter )
61114 {
62115 return new DefaultArrayAdapter < T > ( elementAdapter ) ;
63116 }
117+
118+ public static IMValueAdapter < IDictionary < string , T > > GetDictionaryAdapter < T > ( IMValueAdapter < T > elementAdapter )
119+ {
120+ return new DefaultDictionaryAdapter < T > ( elementAdapter ) ;
121+ }
64122 }
65123}
0 commit comments