22 public static Object
unwrap(@Nullable Object o) {
23 if (JSONObject.NULL.equals(o) || o ==
null) {
26 if (o instanceof Boolean
28 || o instanceof Character
29 || o instanceof Double
31 || o instanceof Integer
34 || o instanceof String) {
38 if (o instanceof JSONArray) {
40 JSONArray array = (JSONArray) o;
41 for (
int i = 0; i < array.length(); i++) {
46 if (o instanceof JSONObject) {
47 Map<String, Object> map =
new HashMap<>();
48 JSONObject jsonObject = (JSONObject) o;
49 Iterator<String> keyIterator = jsonObject.keys();
50 while (keyIterator.hasNext()) {
51 String
key = keyIterator.next();
56 }
catch (Exception ignored) {
63 public static Object
wrap(@Nullable Object o) {
65 return JSONObject.NULL;
67 if (o instanceof JSONArray || o instanceof JSONObject) {
70 if (o.equals(JSONObject.NULL)) {
74 if (o instanceof Collection) {
75 JSONArray
result =
new JSONArray();
76 for (Object e : (Collection) o)
result.put(
wrap(e));
78 }
else if (o.getClass().isArray()) {
79 JSONArray
result =
new JSONArray();
80 int length = Array.getLength(o);
84 if (o instanceof Map) {
85 JSONObject
result =
new JSONObject();
86 for (Map.Entry<?, ?> entry : ((Map<?, ?>) o).entrySet())
87 result.put((String) entry.getKey(),
wrap(entry.getValue()));
90 if (o instanceof Boolean
92 || o instanceof Character
93 || o instanceof Double
95 || o instanceof Integer
98 || o instanceof String) {
101 if (o.getClass().getPackage().getName().startsWith(
"java.")) {
104 }
catch (Exception ignored) {