-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathJSONParser.java
More file actions
152 lines (139 loc) · 5.17 KB
/
JSONParser.java
File metadata and controls
152 lines (139 loc) · 5.17 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.reactnativenavigation.options.parsers;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
public class JSONParser {
public JSONObject parse(ReadableMap map) {
try {
ReadableMapKeySetIterator it = map.keySetIterator();
JSONObject result = new JSONObject();
while (it.hasNextKey()) {
String key = it.nextKey();
switch (map.getType(key)) {
case String:
result.put(key, map.getString(key));
break;
case Number:
result.put(key, parseNumber(map, key));
break;
case Boolean:
result.put(key, map.getBoolean(key));
break;
case Array:
result.put(key, parse(map.getArray(key)));
break;
case Map:
result.put(key, parse(map.getMap(key)));
break;
default:
break;
}
}
return result;
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public JSONArray parse(ReadableArray arr) {
JSONArray result = new JSONArray();
for (int i = 0; i < arr.size(); i++) {
switch (arr.getType(i)) {
case String:
result.put(arr.getString(i));
break;
case Number:
result.put(parseNumber(arr, i));
break;
case Boolean:
result.put(arr.getBoolean(i));
break;
case Array:
result.put(parse(arr.getArray(i)));
break;
case Map:
result.put(parse(arr.getMap(i)));
break;
default:
break;
}
}
return result;
}
private static Object parseNumber(ReadableMap map, String key) {
try {
Double doubleValue = map.getDouble(key);
if(doubleValue % 1 == 0){
return map.getInt(key);
}
return doubleValue;
} catch (Exception e) {
return map.getInt(key);
}
}
private static Object parseNumber(ReadableArray arr, int index) {
try {
Double doubleValue = arr.getDouble(index);
if(doubleValue % 1 == 0){
return arr.getInt(index);
}
return doubleValue;
} catch (Exception e) {
return arr.getInt(index);
}
}
public static WritableMap convert(JSONObject jsonObject) {
WritableMap map = Arguments.createMap();
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject.opt(key);
if (value instanceof JSONObject) {
map.putMap(key, convert((JSONObject) value));
} else if (value instanceof JSONArray) {
map.putArray(key, convert((JSONArray) value));
} else if (value instanceof Boolean) {
map.putBoolean(key, (Boolean) value);
} else if (value instanceof Integer) {
map.putInt(key, (Integer) value);
} else if (value instanceof Double) {
map.putDouble(key, (Double) value);
} else if (value instanceof String) {
map.putString(key, (String) value);
} else {
map.putString(key, value.toString());
}
}
return map;
}
public static WritableArray convert(JSONArray jsonArray) {
WritableArray array = Arguments.createArray();
for (int i = 0; i < jsonArray.length(); i++) {
Object value = jsonArray.opt(i);
if (value instanceof JSONObject) {
array.pushMap(convert((JSONObject) value));
} else if (value instanceof JSONArray) {
array.pushArray(convert((JSONArray) value));
} else if (value instanceof Boolean) {
array.pushBoolean((Boolean) value);
} else if (value instanceof Integer) {
array.pushInt((Integer) value);
} else if (value instanceof Double) {
array.pushDouble((Double) value);
} else if (value instanceof String) {
array.pushString((String) value);
} else {
array.pushString(value.toString());
}
}
return array;
}
}