55
66import com .applozic .mobicomkit .uiwidgets .conversation .richmessaging .models .v2 .KmFormPayloadModel ;
77import com .applozic .mobicommons .json .JsonMarker ;
8+ import com .google .gson .Gson ;
9+ import com .google .gson .GsonBuilder ;
10+ import com .google .gson .JsonArray ;
11+ import com .google .gson .JsonDeserializationContext ;
12+ import com .google .gson .JsonDeserializer ;
13+ import com .google .gson .JsonElement ;
14+ import com .google .gson .JsonObject ;
15+ import com .google .gson .JsonParseException ;
16+ import com .google .gson .JsonSerializationContext ;
17+ import com .google .gson .JsonSerializer ;
18+ import com .google .gson .reflect .TypeToken ;
819
20+ import com .applozic .mobicommons .json .GsonUtils ;
21+
22+
23+ import java .lang .reflect .Type ;
924import java .util .HashSet ;
1025import java .util .Map ;
1126
@@ -19,7 +34,7 @@ public class KmFormStateModel extends JsonMarker {
1934 private Map <String , String > hiddenFields ;
2035 private SparseIntArray validationArray ;
2136 private SparseArray <Long > dateFieldArray ;
22- private SparseArray <KmFormPayloadModel . Options > dropdownFieldArray ;
37+ private SparseArray <String > dropdownFieldArray ;
2338
2439 public SparseArray <String > getTextFields () {
2540 return textFields ;
@@ -86,10 +101,172 @@ public void setDateFieldArray(SparseArray<Long> dateFieldArray) {
86101 }
87102
88103 public SparseArray <KmFormPayloadModel .Options > getDropdownFieldArray () {
89- return dropdownFieldArray ;
104+ if (dropdownFieldArray == null ) {
105+ return null ;
106+ }
107+ SparseArray <KmFormPayloadModel .Options > result = new SparseArray <>();
108+ for (int i =0 ; i <dropdownFieldArray .size (); i ++) {
109+ result .put (dropdownFieldArray .keyAt (i ), GsonUtils .getObjectFromJson (dropdownFieldArray .get (i ), KmFormPayloadModel .Options .class ));
110+ }
111+ return result ;
90112 }
91113
92114 public void setDropdownFieldArray (SparseArray <KmFormPayloadModel .Options > dropdownFieldArray ) {
93- this .dropdownFieldArray = dropdownFieldArray ;
115+ SparseArray <String > result = new SparseArray <>();
116+ for (int i =0 ; i <dropdownFieldArray .size (); i ++) {
117+ result .put (dropdownFieldArray .keyAt (i ), GsonUtils .getJsonFromObject (dropdownFieldArray .get (i ), KmFormPayloadModel .Options .class ));
118+ }
119+ this .dropdownFieldArray = result ;
120+ }
121+
122+ public static class SparseArrayStringAdapter implements JsonSerializer <SparseArray <String >>, JsonDeserializer <SparseArray <String >> {
123+ @ Override
124+ public JsonElement serialize (SparseArray <String > src , Type typeOfSrc , JsonSerializationContext context ) {
125+ JsonObject jsonObject = new JsonObject ();
126+ for (int i = 0 ; i < src .size (); i ++) {
127+ int key = src .keyAt (i );
128+ String value = src .valueAt (i );
129+ jsonObject .addProperty (String .valueOf (key ), value );
130+ }
131+ return jsonObject ;
132+ }
133+
134+ @ Override
135+ public SparseArray <String > deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
136+ SparseArray <String > result = new SparseArray <>();
137+ JsonObject jsonObject = json .getAsJsonObject ();
138+ for (Map .Entry <String , JsonElement > entry : jsonObject .entrySet ()) {
139+ int key = Integer .parseInt (entry .getKey ());
140+ String value = entry .getValue ().getAsString ();
141+ result .put (key , value );
142+ }
143+ return result ;
144+ }
145+ }
146+
147+ public static class SparseIntArrayAdapter implements JsonSerializer <SparseIntArray >, JsonDeserializer <SparseIntArray > {
148+ @ Override
149+ public JsonElement serialize (SparseIntArray src , Type typeOfSrc , JsonSerializationContext context ) {
150+ JsonObject jsonObject = new JsonObject ();
151+ for (int i = 0 ; i < src .size (); i ++) {
152+ int key = src .keyAt (i );
153+ int value = src .valueAt (i );
154+ jsonObject .addProperty (String .valueOf (key ), value );
155+ }
156+ return jsonObject ;
157+ }
158+
159+ @ Override
160+ public SparseIntArray deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
161+ SparseIntArray result = new SparseIntArray ();
162+ JsonObject jsonObject = json .getAsJsonObject ();
163+ for (Map .Entry <String , JsonElement > entry : jsonObject .entrySet ()) {
164+ int key = Integer .parseInt (entry .getKey ());
165+ int value = entry .getValue ().getAsInt ();
166+ result .put (key , value );
167+ }
168+ return result ;
169+ }
170+ }
171+
172+ public static class SparseArrayHashSetAdapter implements JsonSerializer <SparseArray <HashSet <Integer >>>, JsonDeserializer <SparseArray <HashSet <Integer >>> {
173+ @ Override
174+ public JsonElement serialize (SparseArray <HashSet <Integer >> src , Type typeOfSrc , JsonSerializationContext context ) {
175+ JsonObject jsonObject = new JsonObject ();
176+ for (int i = 0 ; i < src .size (); i ++) {
177+ int key = src .keyAt (i );
178+ HashSet <Integer > value = src .valueAt (i );
179+ JsonArray jsonArray = new JsonArray ();
180+ for (Integer item : value ) {
181+ jsonArray .add (item );
182+ }
183+ jsonObject .add (String .valueOf (key ), jsonArray );
184+ }
185+ return jsonObject ;
186+ }
187+
188+ @ Override
189+ public SparseArray <HashSet <Integer >> deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
190+ SparseArray <HashSet <Integer >> result = new SparseArray <>();
191+ JsonObject jsonObject = json .getAsJsonObject ();
192+ for (Map .Entry <String , JsonElement > entry : jsonObject .entrySet ()) {
193+ int key = Integer .parseInt (entry .getKey ());
194+ JsonArray jsonArray = entry .getValue ().getAsJsonArray ();
195+ HashSet <Integer > hashSet = new HashSet <>();
196+ for (JsonElement element : jsonArray ) {
197+ hashSet .add (element .getAsInt ());
198+ }
199+ result .put (key , hashSet );
200+ }
201+ return result ;
202+ }
203+ }
204+
205+ public static class SparseArrayLongAdapter implements JsonSerializer <SparseArray <Long >>, JsonDeserializer <SparseArray <Long >> {
206+ @ Override
207+ public JsonElement serialize (SparseArray <Long > src , Type typeOfSrc , JsonSerializationContext context ) {
208+ JsonObject jsonObject = new JsonObject ();
209+ for (int i = 0 ; i < src .size (); i ++) {
210+ int key = src .keyAt (i );
211+ Long value = src .valueAt (i );
212+ jsonObject .addProperty (String .valueOf (key ), value );
213+ }
214+ return jsonObject ;
215+ }
216+
217+ @ Override
218+ public SparseArray <Long > deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
219+ SparseArray <Long > result = new SparseArray <>();
220+ JsonObject jsonObject = json .getAsJsonObject ();
221+ for (Map .Entry <String , JsonElement > entry : jsonObject .entrySet ()) {
222+ int key = Integer .parseInt (entry .getKey ());
223+ Long value = entry .getValue ().getAsLong ();
224+ result .put (key , value );
225+ }
226+ return result ;
227+ }
228+ }
229+
230+ public static class SparseArrayOptionsAdapter implements JsonSerializer <SparseArray <KmFormPayloadModel .Options >>, JsonDeserializer <SparseArray <KmFormPayloadModel .Options >> {
231+ @ Override
232+ public JsonElement serialize (SparseArray <KmFormPayloadModel .Options > src , Type typeOfSrc , JsonSerializationContext context ) {
233+ JsonObject jsonObject = new JsonObject ();
234+ for (int i = 0 ; i < src .size (); i ++) {
235+ int key = src .keyAt (i );
236+ KmFormPayloadModel .Options value = src .valueAt (i );
237+ jsonObject .add (String .valueOf (key ), context .serialize (value ));
238+ }
239+ return jsonObject ;
240+ }
241+
242+ @ Override
243+ public SparseArray <KmFormPayloadModel .Options > deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
244+ SparseArray <KmFormPayloadModel .Options > result = new SparseArray <>();
245+ JsonObject jsonObject = json .getAsJsonObject ();
246+ for (Map .Entry <String , JsonElement > entry : jsonObject .entrySet ()) {
247+ int key = Integer .parseInt (entry .getKey ());
248+ KmFormPayloadModel .Options value = context .deserialize (entry .getValue (), KmFormPayloadModel .Options .class );
249+ result .put (key , value );
250+ }
251+ return result ;
252+ }
253+ }
254+
255+ public static Gson getGson () {
256+ return new GsonBuilder ()
257+ .registerTypeAdapter (new TypeToken <SparseArray <String >>() {}.getType (), new SparseArrayStringAdapter ())
258+ .registerTypeAdapter (new TypeToken <SparseIntArray >() {}.getType (), new SparseIntArrayAdapter ())
259+ .registerTypeAdapter (new TypeToken <SparseArray <HashSet <Integer >>>() {}.getType (), new SparseArrayHashSetAdapter ())
260+ .registerTypeAdapter (new TypeToken <SparseArray <Long >>() {}.getType (), new SparseArrayLongAdapter ())
261+ .registerTypeAdapter (new TypeToken <SparseArray <KmFormPayloadModel .Options >>() {}.getType (), new SparseArrayOptionsAdapter ())
262+ .create ();
263+ }
264+
265+ public String toJson () {
266+ return getGson ().toJson (this );
267+ }
268+
269+ public static KmFormStateModel fromJson (String json ) {
270+ return getGson ().fromJson (json , KmFormStateModel .class );
94271 }
95- }
272+ }
0 commit comments