X Tutup
package com.tethik.json; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; public abstract class JSONMappedObject { private static boolean debug = false; protected Object getValue(Class type, Object obj) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, JSONException { Object value; if(JSONMappedObject.class.isAssignableFrom(type)) { // Recursion if(debug) System.out.println("Object found"); Constructor constructor = type.getConstructor(JSONObject.class); if(debug) System.out.println("Constructor created"); value = constructor.newInstance((JSONObject) obj); } else if(type.isEnum()) { // Enums if(debug) System.out.println("Enum found"); value = Enum.valueOf((Class) type, (String) obj); } else if(type.isArray()) { JSONArray array = (JSONArray) obj; Object[] objects = new Object[array.length()]; for(int i = 0; i < array.length(); i++) objects[i] = getValue(type.getComponentType(), array.get(i)); value = objects; } else { // Basic type. Or error :( if(debug) System.out.println("Basic found"); value = obj; } return value; } protected Object getValue(Class type, String key, JSONObject obj) throws NoSuchMethodException, SecurityException, JSONException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Object value; if(JSONMappedObject.class.isAssignableFrom(type)) { // Recursion if(debug) System.out.println("Object found"); Constructor constructor = type.getConstructor(JSONObject.class); if(debug) System.out.println("Constructor created"); value = constructor.newInstance(obj.getJSONObject(key)); } else if(type.isEnum()) { // Enums if(debug) System.out.println("Enum found"); value = Enum.valueOf((Class) type, obj.getString(key)); } else if(type.isArray()) { JSONArray array = obj.getJSONArray(key); Object objects = type.cast(Array.newInstance(type.getComponentType(), array.length())); for(int i = 0; i < array.length(); i++) Array.set(objects, i, type.getComponentType().cast(getValue(type.getComponentType(), array.get(i)))); value = objects; } else { // Basic type. Or error :( if(debug) System.out.println("Basic found"); value = obj.get(key); } return value; } protected void parseJson(JSONObject obj) throws JSONException, IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchMethodException, SecurityException, InvocationTargetException { if(debug) System.out.println("parseJson called on " + this.getClass().getName()); Field[] fields = this.getClass().getDeclaredFields(); for(Field field : fields) { field.setAccessible(true); if(debug) System.out.println("Checking " + field.getName()); if(!field.isAnnotationPresent(JSONObj.class)) continue; String key = field.getName(); Class type = field.getType(); if(debug) System.out.println("Annotation found on " + key + " type: " + type.getName()); Object value = getValue(type, key, obj); field.set(this, value); if(debug) System.out.println("Field set to " + value); } } public JSONMappedObject(JSONObject obj) throws JSONException { try { parseJson(obj); } catch (IllegalArgumentException | IllegalAccessException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException e) { throw new JSONException(e); } } }
X Tutup