SHOW:
|
|
- or go back to the newest paste.
1 | - | package com.android.example; |
1 | + | package com.android4; |
2 | ||
3 | import java.lang.reflect.Array; | |
4 | import java.lang.reflect.Field; | |
5 | import java.lang.reflect.Modifier; | |
6 | ||
7 | import org.json.JSONArray; | |
8 | import org.json.JSONException; | |
9 | import org.json.JSONObject; | |
10 | ||
11 | /** | |
12 | - | Object obj = null; |
12 | + | * Parses any class to JSON and populates any class from JSON using Android native JSON, without any third-party libraries. |
13 | - | System.out.println("Populating " + classname.getSimpleName()+ " -with- " + js.toString()); |
13 | + | * |
14 | - | try { |
14 | + | * @author Ender Muab'Dib |
15 | - | obj = classname.newInstance(); |
15 | + | * |
16 | - | } catch (InstantiationException e1) { |
16 | + | */ |
17 | - | System.err.println(e1.getMessage()); |
17 | + | |
18 | - | return null; |
18 | + | private static int privacy = 2; |
19 | - | } catch (IllegalAccessException e1) { |
19 | + | |
20 | - | System.err.println(e1.getMessage()); |
20 | + | /** |
21 | - | return null; |
21 | + | * p = 0 --> Only for public fields |
22 | * p = 1 --> Only for public + protected fields | |
23 | * p = 2 --> All fields: public + protected + private. Use only under your own responsability. | |
24 | - | Field[] fields = classname.getFields(); |
24 | + | */ |
25 | - | for (Field f : fields) { |
25 | + | public static void setPrivacyLevel(int p) { |
26 | - | try { |
26 | + | if ((p >= 0) && (p <= 2)) { |
27 | - | if (js.has(f.getName())) { |
27 | + | privacy = p; |
28 | - | String type = f.getType().getSimpleName(); |
28 | + | |
29 | - | if (type.equalsIgnoreCase("boolean")) { |
29 | + | |
30 | - | f.setBoolean(obj, js.getBoolean(f.getName())); |
30 | + | |
31 | - | } else if (type.equalsIgnoreCase("int")) { |
31 | + | /** |
32 | - | f.setInt(obj, js.getInt(f.getName())); |
32 | + | * privacy = 0 --> Only for public fields |
33 | - | } else if (type.equalsIgnoreCase("double")) { |
33 | + | * privacy = 1 --> Only for public + protected fields |
34 | - | f.setDouble(obj, js.getDouble(f.getName())); |
34 | + | * privacy = 2 --> All fields: public + protected + private. Use only under your own responsability. |
35 | - | } else if (type.equalsIgnoreCase("float")) { |
35 | + | */ |
36 | - | f.setFloat(obj, (float) js.getDouble(f.getName())); |
36 | + | public static int getPrivacyLevel() { |
37 | - | } else if (type.equalsIgnoreCase("string")) { |
37 | + | return privacy; |
38 | - | f.set(obj, js.getString(f.getName())); |
38 | + | |
39 | - | } else if (f.getType().isArray()) { |
39 | + | |
40 | - | // System.out.println("This is an ARRAY: " + f.getName()); |
40 | + | /** |
41 | - | insertArrayFromJSON(obj, f, js.getJSONArray(f.getName())); |
41 | + | * @return an object of type classname with its PUBLIC fields full with the js info |
42 | - | } else { |
42 | + | */ |
43 | - | f.set(obj,populateObjectFromJSON(f.getType(),js.getJSONObject(f.getName()))); |
43 | + | |
44 | - | } |
44 | + | Object obj = null; |
45 | - | } |
45 | + | System.out.println("Populating " + classname.getSimpleName() + " -with- " + js.toString()); |
46 | - | } catch (IllegalArgumentException e) { |
46 | + | try { |
47 | - | System.err.println(e.getMessage()); |
47 | + | obj = classname.newInstance(); |
48 | - | } catch (IllegalAccessException e) { |
48 | + | } catch (InstantiationException e1) { |
49 | - | System.err.println(e.getMessage()); |
49 | + | System.err.println(e1.getMessage()); |
50 | - | } catch (JSONException e) { |
50 | + | return null; |
51 | - | System.err.println(e.getMessage()); |
51 | + | } catch (IllegalAccessException e1) { |
52 | - | } |
52 | + | System.err.println(e1.getMessage()); |
53 | return null; | |
54 | } | |
55 | - | return obj; |
55 | + | |
56 | Field[] fields; | |
57 | ||
58 | - | public static void insertArrayFromJSON(Object obj, Field f, JSONArray js, int pos) throws IllegalArgumentException, NegativeArraySizeException, IllegalAccessException, JSONException { |
58 | + | if (privacy == 0) { |
59 | - | f.set(obj,Array.newInstance(f.getType().getComponentType(), js.length())); |
59 | + | fields = classname.getFields(); |
60 | - | String type = f.getType().getComponentType().getSimpleName(); |
60 | + | } else { |
61 | - | |
61 | + | fields = classname.getDeclaredFields(); |
62 | - | for (int i = 0; i < js.length(); i++) { |
62 | + | |
63 | - | if (f.getType().getComponentType().isArray()) { |
63 | + | |
64 | - | System.out.println("An array!! " + f.getName()); |
64 | + | for (Field f : fields) { |
65 | - | //TO-DO |
65 | + | // System.out.println("Declared " + f.getName()); |
66 | - | } else if (!f.getType().getComponentType().isPrimitive()&& (!type.equalsIgnoreCase("string"))) { |
66 | + | |
67 | - | //System.out.println(f.getType().getSimpleName() + " isn't primitive " + f.getName()); |
67 | + | if ((privacy == 2) && (Modifier.isPrivate(f.getModifiers()))) { |
68 | - | Array.set(f.get(obj),i,populateObjectFromJSON(f.getType().getComponentType(),js.getJSONObject(i))); |
68 | + | f.setAccessible(true); |
69 | - | //Array.set(f.get(obj),i,populateObjectFromJSON(f.getType().getComponentType(), new JSONObject(js.getJSONArray(i).toString()))); |
69 | + | } |
70 | - | } else { |
70 | + | |
71 | - | //Primitive |
71 | + | try { |
72 | - | Array.set(f.get(obj), i, js.get(i)); |
72 | + | if (js.has(f.getName())) { |
73 | - | } |
73 | + | String type = f.getType().getSimpleName(); |
74 | if (type.equalsIgnoreCase("boolean")) { | |
75 | f.setBoolean(obj, js.getBoolean(f.getName())); | |
76 | - | |
76 | + | } else if (type.equalsIgnoreCase("int")) { |
77 | - | public static JSONObject parseObjectToJSONClass(Object obj) {JSONObject js = new JSONObject(); |
77 | + | f.setInt(obj, js.getInt(f.getName())); |
78 | - | Class<?> c = obj.getClass(); |
78 | + | } else if (type.equalsIgnoreCase("double")) { |
79 | - | Field[] fields = c.getFields(); |
79 | + | f.setDouble(obj, js.getDouble(f.getName())); |
80 | } else if (type.equalsIgnoreCase("float")) { | |
81 | - | for (Field f : fields) { |
81 | + | f.setFloat(obj, (float) js.getDouble(f.getName())); |
82 | - | try { |
82 | + | } else if (type.equalsIgnoreCase("string")) { |
83 | - | System.out.println(f.getName() + " - "+ f.getType().getSimpleName() + " - " + f.get(obj)); |
83 | + | f.set(obj, js.getString(f.getName())); |
84 | } else if (f.getType().isArray()) { | |
85 | - | String name = f.getName(); |
85 | + | f.set(obj, Array.newInstance(f.getType().getComponentType(), js.getJSONArray(f.getName()).length())); |
86 | - | String type = f.getType().getSimpleName(); |
86 | + | insertArrayFromJSON(f.get(obj), js.getJSONArray(f.getName())); |
87 | - | if (f.get(obj) != null) { |
87 | + | } else { |
88 | - | if (type.equalsIgnoreCase("boolean")) { |
88 | + | f.set(obj, populateObjectFromJSON(f.getType(), js.getJSONObject(f.getName()))); |
89 | - | js.put(name, f.getBoolean(obj)); |
89 | + | } |
90 | - | } else if (type.equalsIgnoreCase("int")) { |
90 | + | |
91 | - | js.put(name, f.getInt(obj)); |
91 | + | } catch (IllegalArgumentException e) { |
92 | - | } else if (type.equalsIgnoreCase("double")) { |
92 | + | System.err.println(e.getMessage()); |
93 | - | js.put(name, f.getDouble(obj)); |
93 | + | } catch (IllegalAccessException e) { |
94 | - | } else if (type.equalsIgnoreCase("float")) { |
94 | + | System.err.println(e.getMessage()); |
95 | - | js.put(name, f.getFloat(obj)); |
95 | + | } catch (JSONException e) { |
96 | - | } else if (type.equalsIgnoreCase("string")) { |
96 | + | System.err.println(e.getMessage()); |
97 | - | js.put(name, (String) f.get(obj)); |
97 | + | } |
98 | - | } else if (type.endsWith("]")) { |
98 | + | |
99 | - | JSONArray ar = new JSONArray(); |
99 | + | if ((privacy == 2) && (Modifier.isPrivate(f.getModifiers()))) { |
100 | - | for (int i = 0; i < Array.getLength(f.get(obj)); i++) { |
100 | + | f.setAccessible(false); |
101 | - | ar.put(i, Array.get(f.get(obj), i)); |
101 | + | } |
102 | - | } |
102 | + | |
103 | - | js.put(name, ar); |
103 | + | |
104 | - | } else { |
104 | + | return obj; |
105 | - | js.put(name, parseObjectToJSONClass(f.get(obj))); |
105 | + | |
106 | - | } |
106 | + | |
107 | - | } |
107 | + | /** |
108 | - | } catch (IllegalArgumentException e) { |
108 | + | * @param o |
109 | - | System.err.println(e.getMessage()); |
109 | + | * This object will be fill up with the JSONArray js data |
110 | - | } catch (IllegalAccessException e) { |
110 | + | */ |
111 | - | System.err.println(e.getMessage()); |
111 | + | public static void insertArrayFromJSON(Object o, JSONArray js) throws IllegalArgumentException, NegativeArraySizeException, IllegalAccessException, JSONException { |
112 | - | } catch (JSONException e) { |
112 | + | Class<?> c = o.getClass(); |
113 | - | System.err.println(e.getMessage()); |
113 | + | |
114 | - | } |
114 | + | String type = c.getComponentType().getSimpleName(); |
115 | ||
116 | - | return js; |
116 | + | for (int i = 0; i < js.length(); i++) { |
117 | if (c.getComponentType().isArray()) { | |
118 | Array.set(o, i, Array.newInstance(c.getComponentType().getComponentType(), js.getJSONArray(i).length())); | |
119 | insertArrayFromJSON(Array.get(o, i), js.getJSONArray(i)); | |
120 | } else if (!c.getComponentType().isPrimitive() && (!type.equalsIgnoreCase("string"))) { | |
121 | Array.set(o, i, populateObjectFromJSON(c.getComponentType(), js.getJSONObject(i))); | |
122 | } else { | |
123 | Array.set(o, i, js.get(i)); | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | /** | |
129 | * @param obj Object to convert in JSON format | |
130 | * @return JSONOBject with all obj initialited AND PUBLIC fields. | |
131 | */ | |
132 | public static JSONObject parseObjectToJSONClass(Object obj) { | |
133 | JSONObject js = new JSONObject(); | |
134 | ||
135 | Class<?> c = obj.getClass(); | |
136 | ||
137 | Field[] fields; | |
138 | ||
139 | if (privacy == 0) { | |
140 | fields = c.getFields(); | |
141 | } else { | |
142 | fields = c.getDeclaredFields(); | |
143 | } | |
144 | ||
145 | for (Field f : fields) { | |
146 | if ((privacy == 2) && (Modifier.isPrivate(f.getModifiers()))) { | |
147 | f.setAccessible(true); | |
148 | } | |
149 | ||
150 | try { | |
151 | System.out.println(f.getName() + " - " + f.getType().getSimpleName() + " - " + f.get(obj)); | |
152 | ||
153 | String name = f.getName(); | |
154 | String type = f.getType().getSimpleName(); | |
155 | if (f.get(obj) != null) { | |
156 | if (type.equalsIgnoreCase("boolean")) { | |
157 | js.put(name, f.getBoolean(obj)); | |
158 | } else if (type.equalsIgnoreCase("int")) { | |
159 | js.put(name, f.getInt(obj)); | |
160 | } else if (type.equalsIgnoreCase("double")) { | |
161 | js.put(name, f.getDouble(obj)); | |
162 | } else if (type.equalsIgnoreCase("float")) { | |
163 | js.put(name, f.getFloat(obj)); | |
164 | } else if (type.equalsIgnoreCase("string")) { | |
165 | js.put(name, (String) f.get(obj)); | |
166 | } else if (type.endsWith("]")) { | |
167 | js.put(name, generateJSONArray(f.get(obj))); | |
168 | } else { | |
169 | js.put(name, parseObjectToJSONClass(f.get(obj))); | |
170 | } | |
171 | } | |
172 | } catch (IllegalArgumentException e) { | |
173 | System.err.println(e.getMessage()); | |
174 | } catch (IllegalAccessException e) { | |
175 | System.err.println(e.getMessage()); | |
176 | } catch (JSONException e) { | |
177 | System.err.println(e.getMessage()); | |
178 | } | |
179 | ||
180 | if ((privacy == 2) && (Modifier.isPrivate(f.getModifiers()))) { | |
181 | f.setAccessible(false); | |
182 | } | |
183 | } | |
184 | ||
185 | return js; | |
186 | } | |
187 | ||
188 | /** | |
189 | * | |
190 | * @param o Array object to convert in JSONArray format | |
191 | * @return a JSONArray with all the o components. | |
192 | */ | |
193 | public static JSONArray generateJSONArray(Object o) throws ArrayIndexOutOfBoundsException, IllegalArgumentException, JSONException { | |
194 | JSONArray ar = new JSONArray(); | |
195 | for (int i = 0; i < Array.getLength(o); i++) { | |
196 | if (Array.get(o, i).getClass().isArray()) { | |
197 | ar.put(i, generateJSONArray(Array.get(o, i))); | |
198 | } else { | |
199 | ar.put(i, Array.get(o, i)); | |
200 | } | |
201 | } | |
202 | return ar; | |
203 | } | |
204 | } |