55import com .google .gson .JsonDeserializationContext ;
66import com .google .gson .JsonDeserializer ;
77import com .google .gson .JsonElement ;
8+ import com .google .gson .JsonObject ;
89import com .google .gson .JsonParseException ;
910import com .google .gson .JsonPrimitive ;
1011import com .google .gson .JsonSerializationContext ;
1112import com .google .gson .JsonSerializer ;
13+ import org .apache .commons .lang3 .ArrayUtils ;
1214
1315import java .lang .reflect .Type ;
1416import java .util .Arrays ;
@@ -33,26 +35,47 @@ default T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
3335
3436 default <R > JsonArray serializeArray (R [] objects , JsonSerializationContext context ) {
3537 JsonArray array = new JsonArray ();
38+ if (ArrayUtils .isEmpty (objects )) return array ;
3639 Type arrayType = objects .getClass ().getComponentType ();
3740 for (R t : objects ) {
38- array .add (context .serialize (t , arrayType ));
41+ JsonElement element = context .serialize (t , arrayType );
42+ if (element .isJsonPrimitive () && element .getAsJsonPrimitive ().isNumber ()) {
43+ JsonObject typed = new JsonObject ();
44+ typed .addProperty ("typed" , t .getClass ().getSimpleName ());
45+ typed .add ("element" , element );
46+ array .add (typed );
47+ } else {
48+ array .add (element );
49+ }
3950 }
4051 return array ;
4152 }
4253
43- default <R > R [] deserializeArray (JsonArray jsonElements , JsonDeserializationContext context ,
54+ default <R > R [] deserializeArray (JsonArray jsonArray , JsonDeserializationContext context ,
4455 IntFunction <R []> function ) {
45- if (jsonElements == null ) return function .apply (0 );
46- R [] array2 = function .apply (jsonElements .size ());
47- Type arrayType = array2 .getClass ().getComponentType ();
48- Arrays .setAll (array2 , i -> handleArg (jsonElements .get (i ), context , arrayType ));
49- return array2 ;
56+ if (jsonArray == null || jsonArray . size () == 0 ) return function .apply (0 );
57+ R [] array = function .apply (jsonArray .size ());
58+ Type arrayType = array .getClass ().getComponentType ();
59+ Arrays .setAll (array , i -> handleArg (jsonArray .get (i ), context , arrayType ));
60+ return array ;
5061 }
5162
5263 static Object handleArg (JsonElement element , JsonDeserializationContext context , Type arrayType ) {
5364 // args can sometimes be keys
5465 if (element .isJsonObject ()) {
55- return context .deserialize (element .getAsJsonObject (), IDrawable .class );
66+ JsonObject object = element .getAsJsonObject ();
67+ if (!object .has ("typed" ))
68+ return context .deserialize (object , IDrawable .class );
69+
70+ JsonElement value = object .get ("element" );
71+ return switch (object .get ("typed" ).getAsString ()) {
72+ case "Integer" -> value .getAsInt ();
73+ case "Long" -> value .getAsLong ();
74+ case "Double" -> value .getAsDouble ();
75+ case "Float" -> value .getAsFloat ();
76+ case "Byte" -> value .getAsByte ();
77+ default -> value .getAsNumber ();
78+ };
5679 } else if (element instanceof JsonPrimitive primitive && primitive .isNumber ()) {
5780 return primitive .getAsNumber ();
5881 } else {
0 commit comments