11package xyz .victorolaitan .easyjson ;
22
33import java .lang .reflect .Field ;
4+ import java .lang .reflect .InvocationTargetException ;
45import java .util .ArrayList ;
6+ import java .util .List ;
57
68public class DatabaseHelper {
79
8- public static <T > T deserializeClass (Class <T > aClass , EasyJSON jsonStructure ) throws EasyJSONException {
9- return deserializeClass (aClass , jsonStructure .getRootNode ());
10+ public static <T > T deserializeToClass (Class <T > aClass , EasyJSON jsonStructure ) throws EasyJSONException {
11+ return deserializeToClass (aClass , jsonStructure .getRootNode ());
1012 }
1113
12- public static <T > T deserializeClass (Class <T > aClass , JSONElement jsonElement ) throws EasyJSONException {
13- T instance = null ;
14+ private static <T > T deserializeToClass (Class <T > aClass , JSONElement jsonElement ) throws EasyJSONException {
15+ T instance ;
1416 try {
15- instance = aClass .newInstance ();
16- } catch (InstantiationException e ) {
17+ instance = aClass .getDeclaredConstructor (). newInstance ();
18+ } catch (InstantiationException | NoSuchMethodException | InvocationTargetException e ) {
1719 throw new EasyJSONException (EasyJSONException .INSTANTIATION_ERROR , e );
1820 } catch (IllegalAccessException e ) {
1921 throw new EasyJSONException (EasyJSONException .ILLEGAL_ACCESS , e );
2022 }
21- deserializeInstance (instance , jsonElement );
23+ deserializeToInstance (instance , jsonElement );
2224 return instance ;
2325 }
2426
25- public static <T > void deserializeInstance (T instance , EasyJSON jsonStructure ) throws EasyJSONException {
26- deserializeInstance (instance , jsonStructure .getRootNode ());
27+ public static <T > void deserializeToInstance (T instance , EasyJSON jsonStructure ) throws EasyJSONException {
28+ deserializeToInstance (instance , jsonStructure .getRootNode ());
2729 }
2830
29- public static <T > void deserializeInstance (T instance , JSONElement jsonElement ) throws EasyJSONException {
30- for (int i = 0 ; i < jsonElement .getChildren ().size (); i ++) {
31- JSONElement child = (JSONElement ) jsonElement .getChildren ().get (i );
32-
31+ private static <T > void deserializeToInstance (T instance , JSONElement jsonElement ) throws EasyJSONException {
32+ for (JSONElement childElement : jsonElement ) {
3333 try {
34- if (child .getKey () != null ) {
35- Field field = instance .getClass ().getDeclaredField (child .getKey ());
36- field .setAccessible (true );
37- switch (child .getType ()) {
38- case PRIMITIVE :
39- field .set (instance , child .getValue ());
40- break ;
41- case ARRAY :
42- field .set (instance , extractArrayElements (child ));
43- break ;
44- case STRUCTURE :
45- field .set (instance , deserializeClass (field .getType (), child ));
46- break ;
47- case ROOT :
48- break ;
49- }
34+ Field field = instance .getClass ().getDeclaredField (childElement .getKey ());
35+ field .setAccessible (true );
36+ switch (childElement .getType ()) {
37+ case PRIMITIVE :
38+ field .set (instance , childElement .getValue ());
39+ break ;
40+ case ARRAY :
41+ if (field .getType ().isAssignableFrom (List .class )) {
42+ field .set (
43+ instance ,
44+ extractArrayElements (childElement )
45+ );
46+ }
47+ break ;
48+ case STRUCTURE :
49+ field .set (instance , deserializeToClass (field .getType (), childElement ));
50+ break ;
51+ case ROOT :
52+ break ;
5053 }
5154 } catch (IllegalAccessException e ) {
5255 throw new EasyJSONException (EasyJSONException .ILLEGAL_ACCESS , e );
5356 } catch (NoSuchFieldException e ) {
5457 throw new EasyJSONException (EasyJSONException .FIELD_NOT_FOUND , e );
58+ } catch (InstantiationException e ) {
59+ throw new EasyJSONException (EasyJSONException .INSTANTIATION_ERROR , e );
5560 }
5661 }
5762 }
5863
59- private static ArrayList extractArrayElements (JSONElement jsonElement ) {
60- ArrayList values = new ArrayList ();
61- for (int i = 0 ; i < jsonElement .getChildren ().size (); i ++) {
62- JSONElement e = (JSONElement ) jsonElement .getChildren ().get (i );
64+ private static List <?> extractArrayElements (JSONElement arrayElement ) throws IllegalAccessException , InstantiationException {
65+ List <Object > values = new ArrayList <>();
66+ for (JSONElement e : arrayElement ) {
6367 values .add (e .getValue ());
6468 }
6569 return values ;
@@ -81,36 +85,43 @@ public static <T> JSONElement serializeClass(Class<T> aClass, JSONElement source
8185 return serialize (null , aClass , sourceElement , excludeFields );
8286 }
8387
84- private static <T > JSONElement serialize (T instance , Class tClass , JSONElement sourceElement , Object ... excludeFields ) throws EasyJSONException {
88+ private static <T > JSONElement serialize (T instance , Class <? extends T > tClass , JSONElement sourceElement , Object ... excludeFields ) throws EasyJSONException {
8589 for (Field field : tClass .getDeclaredFields ()) {
86- if (! excludeContains ( field , instance , excludeFields )) {
87- try {
90+ try {
91+ if (! excludeContains ( field , instance , excludeFields )) {
8892 Object fieldValue = field .get (instance );
89- Class enclosingClass = fieldValue .getClass ().getEnclosingClass ();
93+ Class <?> enclosingClass = fieldValue .getClass ().getEnclosingClass ();
9094 if (enclosingClass != null && enclosingClass .equals (tClass )) {
91- sourceElement .putStructure (field .getName (),
92- serialize (fieldValue , fieldValue .getClass (), EasyJSON .create ().getRootNode (), excludeFields ));
93- } else if (fieldValue instanceof ArrayList ) {
94- sourceElement .putArray (field .getName (), ((ArrayList ) fieldValue ).toArray ());
95+ // typeof fieldValue == some subclass of tClass
96+ sourceElement .putStructure (
97+ field .getName (),
98+ serialize (fieldValue , fieldValue .getClass (), EasyJSON .create ().getRootNode (), excludeFields )
99+ );
100+ } else if (fieldValue instanceof List ) {
101+ // typeof fieldValue == List<?>
102+ sourceElement .putArray (field .getName (), ((List <?>) fieldValue ).toArray ());
95103 } else {
104+ // typeof fieldValue == some class implementing .toString()
96105 sourceElement .putPrimitive (field .getName (), fieldValue );
97106 }
98- } catch (Exception ignored ) {
99107 }
108+ } catch (IllegalAccessException e ) {
109+ throw new EasyJSONException (EasyJSONException .ILLEGAL_ACCESS , e );
100110 }
101111 }
102112 return sourceElement ;
103113 }
104114
105- private static boolean excludeContains (Field field , Object instance , Object ... excludeFields ) {
115+ private static boolean excludeContains (Field field , Object instance , Object ... excludeFields ) throws IllegalAccessException {
116+ Object fieldValue ;
117+ try {
118+ fieldValue = field .get (instance );
119+ } catch (NullPointerException ignored ) {
120+ return true ; // all instance fields in a static context are excluded
121+ }
106122 for (Object e : excludeFields ) {
107- try {
108- // fml
109- Object value = field .get (instance );
110- if (value == e ) {
111- return true ;
112- }
113- } catch (Exception ignored ) {
123+ if (fieldValue == e ) {
124+ return true ;
114125 }
115126 }
116127 return false ;
0 commit comments