|
| 1 | +package org.json; |
| 2 | + |
| 3 | +import java.lang.reflect.Array; |
| 4 | +import java.lang.reflect.Constructor; |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +public abstract class JSONMappedObject { |
| 12 | + |
| 13 | + private static boolean debug = false; |
| 14 | + |
| 15 | + protected Object getValue(Class<?> type, Object obj) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, JSONException |
| 16 | + { |
| 17 | + Object value; |
| 18 | + |
| 19 | + if(JSONMappedObject.class.isAssignableFrom(type)) { |
| 20 | + // Recursion |
| 21 | + if(debug) System.out.println("Object found"); |
| 22 | + Constructor<?> constructor = type.getConstructor(JSONObject.class); |
| 23 | + if(debug) System.out.println("Constructor created"); |
| 24 | + value = constructor.newInstance((JSONObject) obj); |
| 25 | + } else if(type.isEnum()) { |
| 26 | + // Enums |
| 27 | + if(debug) System.out.println("Enum found"); |
| 28 | + value = Enum.valueOf((Class<Enum>) type, (String) obj); |
| 29 | + } else if(type.isArray()) { |
| 30 | + JSONArray array = (JSONArray) obj; |
| 31 | + Object[] objects = new Object[array.length()]; |
| 32 | + for(int i = 0; i < array.length(); i++) |
| 33 | + objects[i] = getValue(type.getComponentType(), array.get(i)); |
| 34 | + value = objects; |
| 35 | + } else { |
| 36 | + // Basic type. Or error :( |
| 37 | + if(debug) System.out.println("Basic found"); |
| 38 | + value = obj; |
| 39 | + } |
| 40 | + |
| 41 | + return value; |
| 42 | + } |
| 43 | + |
| 44 | + protected Object getValue(Class<?> type, String key, JSONObject obj) throws NoSuchMethodException, SecurityException, JSONException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException |
| 45 | + { |
| 46 | + Object value; |
| 47 | + |
| 48 | + if(JSONMappedObject.class.isAssignableFrom(type)) { |
| 49 | + // Recursion |
| 50 | + if(debug) System.out.println("Object found"); |
| 51 | + Constructor<?> constructor = type.getConstructor(JSONObject.class); |
| 52 | + if(debug) System.out.println("Constructor created"); |
| 53 | + value = constructor.newInstance(obj.getJSONObject(key)); |
| 54 | + } else if(type.isEnum()) { |
| 55 | + // Enums |
| 56 | + if(debug) System.out.println("Enum found"); |
| 57 | + value = Enum.valueOf((Class<Enum>) type, obj.getString(key)); |
| 58 | + } else if(type.isArray()) { |
| 59 | + JSONArray array = obj.getJSONArray(key); |
| 60 | + Object objects = type.cast(Array.newInstance(type.getComponentType(), array.length())); |
| 61 | + for(int i = 0; i < array.length(); i++) |
| 62 | + Array.set(objects, i, type.getComponentType().cast(getValue(type.getComponentType(), array.get(i)))); |
| 63 | + value = objects; |
| 64 | + } else { |
| 65 | + // Basic type. Or error :( |
| 66 | + if(debug) System.out.println("Basic found"); |
| 67 | + value = obj.get(key); |
| 68 | + } |
| 69 | + |
| 70 | + return value; |
| 71 | + } |
| 72 | + |
| 73 | + protected void parseJson(JSONObject obj) throws JSONException, IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchMethodException, SecurityException, InvocationTargetException { |
| 74 | + if(debug) System.out.println("parseJson called on " + this.getClass().getName()); |
| 75 | + Field[] fields = this.getClass().getDeclaredFields(); |
| 76 | + |
| 77 | + for(Field field : fields) { |
| 78 | + if(debug) System.out.println("Checking " + field.getName()); |
| 79 | + if(!field.isAnnotationPresent(JSONObj.class)) |
| 80 | + continue; |
| 81 | + |
| 82 | + String key = field.getName(); |
| 83 | + Class<?> type = field.getType(); |
| 84 | + if(debug) System.out.println("Annotation found on " + key + " type: " + type.getName()); |
| 85 | + |
| 86 | + Object value = getValue(type, key, obj); |
| 87 | + |
| 88 | + field.set(this, value); |
| 89 | + if(debug) System.out.println("Field set to " + value); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public JSONMappedObject(JSONObject obj) throws JSONException |
| 94 | + { |
| 95 | + try { |
| 96 | + parseJson(obj); |
| 97 | + } catch (IllegalArgumentException | IllegalAccessException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException e) { |
| 98 | + throw new JSONException(e); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments