forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONSerializer.java
More file actions
114 lines (103 loc) · 4.04 KB
/
JSONSerializer.java
File metadata and controls
114 lines (103 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.json;
/*
By: Alexander Forselius <drsounds@gmail.com>
Copyright (c) 2002-2012 JSON.org (contribution by Alexander Forselius)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/***
* A simple JSON serializer
* @author drsou_000
*
*/
public class JSONSerializer {
/**
Strip away get from accessor
**/
private String stripAccessor(String name) {
return name.substring(3).toLowerCase(Locale.US);
}
/**
Make the first letters uppercase. from {@link http://stackoverflow.com/questions/1149855/how-to-upper-case-every-first-letter-of-word-in-a-string}
**/
private String radiate(String s) {
final StringBuilder result = new StringBuilder(s.length());
String[] words = s.split("\\s");
for(int i=0,l=words.length;i<l;++i) {
if(i>0) result.append(" ");
result.append(Character.toUpperCase(words[i].charAt(0)))
.append(words[i].substring(1));
}
return result.toString();
}
/***
Serialize Any class into a JSON object
***/
public JSONObject serialize(Object object) {
JSONObject newObject = new JSONObject();
Field[] fields = object.getClass().getDeclaredFields();
for(int i = 0; i < fields.length; i++) {
Field field = fields[i];
try {
String name = field.getName();
if(name.startsWith("this"))
continue;
String newName = radiate(field.getName());
//newObject.put(method.getName(), method.invoke(object));
Class<?> _class = field.getType();
Method method = object.getClass().getMethod("get" + newName);
Object member = method.invoke(object);
if(member.getClass().isArray()) {
JSONArray array = new JSONArray();
Object[] collection = (Object[])member;
for(Object __object: collection) {
array.put(serialize(__object));
}
newObject.put(newName, array);
} else if(member instanceof String || member instanceof Float || member instanceof Float || member instanceof Integer) {
newObject.put(newName, member);
} else if(member instanceof Object) {
newObject.put(newName, serialize(member));
}
// newObject.put(name, this.serialize(member));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return newObject;
}
}