react-native-map4d-services 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +24 -24
- package/android/build.gradle +58 -60
- package/android/src/main/AndroidManifest.xml +4 -4
- package/android/src/main/java/com/reactnativemap4dservices/Map4dServicesModule.java +78 -91
- package/android/src/main/java/com/reactnativemap4dservices/Map4dServicesPackage.java +28 -28
- package/android/src/main/java/com/reactnativemap4dservices/SClient.java +50 -50
- package/android/src/main/java/com/reactnativemap4dservices/SConverter.java +166 -166
- package/android/src/main/java/com/reactnativemap4dservices/SJsonUtils.java +130 -130
- package/android/src/main/java/com/reactnativemap4dservices/SRequest.java +280 -376
- package/ios/Map4dServices.h +5 -5
- package/ios/Map4dServices.m +67 -81
- package/ios/Map4dServices.xcodeproj/project.pbxproj +2 -2
- package/ios/SClient.h +23 -23
- package/ios/SClient.m +47 -47
- package/ios/SParamConvert.h +44 -44
- package/ios/SParamConvert.m +229 -229
- package/ios/SRequest.h +36 -40
- package/ios/SRequest.m +112 -142
- package/lib/commonjs/index.js +21 -17
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/index.js +22 -14
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/index.d.ts +217 -111
- package/package.json +151 -150
- package/react-native-map4d-services.podspec +27 -20
- package/src/index.tsx +268 -175
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
package com.reactnativemap4dservices;
|
|
2
|
-
|
|
3
|
-
import androidx.annotation.NonNull;
|
|
4
|
-
|
|
5
|
-
import com.facebook.react.bridge.ReadableArray;
|
|
6
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
7
|
-
|
|
8
|
-
import org.json.JSONArray;
|
|
9
|
-
import org.json.JSONException;
|
|
10
|
-
import org.json.JSONObject;
|
|
11
|
-
|
|
12
|
-
class SConverter {
|
|
13
|
-
private static String toAvoidPath(ReadableArray array) {
|
|
14
|
-
if (array == null) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
StringBuilder resultBuilder = new StringBuilder();
|
|
18
|
-
for (int i = 0; i < array.size(); ++i) {
|
|
19
|
-
resultBuilder.append(toLocation(array.getMap(i)));
|
|
20
|
-
if (i < array.size() - 1) {
|
|
21
|
-
resultBuilder.append(",");
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return resultBuilder.toString();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
static String toLocation(ReadableMap map) {
|
|
28
|
-
if (map == null) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
double lat = map.getDouble("latitude");
|
|
32
|
-
double lng = map.getDouble("longitude");
|
|
33
|
-
return lat + "," + lng;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
static String toViewbox(ReadableMap map) {
|
|
37
|
-
if (map == null) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
String southWest = toLocation(map.getMap("southwest"));
|
|
41
|
-
String northEast = toLocation(map.getMap("northeast"));
|
|
42
|
-
return southWest + "," + northEast;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
static String toDatetime(double time) {
|
|
46
|
-
long datetime = (long) time;
|
|
47
|
-
return String.valueOf(datetime);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
static String toTypes(@NonNull ReadableArray types) {
|
|
51
|
-
StringBuilder result = new StringBuilder();
|
|
52
|
-
for (int i = 0; i < types.size(); ++i) {
|
|
53
|
-
if (i > 0) {
|
|
54
|
-
result.append("&types=");
|
|
55
|
-
}
|
|
56
|
-
result.append(types.getString(i));
|
|
57
|
-
}
|
|
58
|
-
return result.toString();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
static String toTags(@NonNull ReadableArray tags) {
|
|
62
|
-
StringBuilder result = new StringBuilder();
|
|
63
|
-
for (int i = 0; i < tags.size(); ++i) {
|
|
64
|
-
if (i > 0) {
|
|
65
|
-
result.append("&tags=");
|
|
66
|
-
}
|
|
67
|
-
result.append(tags.getString(i));
|
|
68
|
-
}
|
|
69
|
-
return result.toString();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
static int toWeighting(@NonNull String weighting) {
|
|
73
|
-
switch (weighting) {
|
|
74
|
-
case "shortest":
|
|
75
|
-
return 0;
|
|
76
|
-
case "fastest":
|
|
77
|
-
return 1;
|
|
78
|
-
case "balance":
|
|
79
|
-
return 2;
|
|
80
|
-
default:
|
|
81
|
-
return 3;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
static String toLocationList(ReadableArray map) {
|
|
86
|
-
if (map == null) {
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
StringBuilder result = new StringBuilder();
|
|
91
|
-
for (int i = 0; i < map.size(); ++i) {
|
|
92
|
-
result.append(toLocation(map.getMap(i)));
|
|
93
|
-
if (i < map.size() - 1) {
|
|
94
|
-
result.append(";");
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return result.toString();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
static JSONArray toOriginJsonArray(ReadableArray origins) {
|
|
101
|
-
if (origins == null) {
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
JSONArray originsJsonArray = null;
|
|
105
|
-
if (origins != null && origins.size() > 0) {
|
|
106
|
-
originsJsonArray = new JSONArray();
|
|
107
|
-
}
|
|
108
|
-
for (int i = 0; i < origins.size(); ++i) {
|
|
109
|
-
ReadableMap origin = origins.getMap(i);
|
|
110
|
-
try {
|
|
111
|
-
JSONObject originJsonObject = new JSONObject();
|
|
112
|
-
String alias = origin.getString("alias");
|
|
113
|
-
originJsonObject.put("alias", alias);
|
|
114
|
-
originJsonObject.put("location", toLocation(origin));
|
|
115
|
-
originsJsonArray.put(originJsonObject);
|
|
116
|
-
} catch (JSONException e) {
|
|
117
|
-
e.printStackTrace();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return originsJsonArray;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
static String toAvoid(ReadableMap map) {
|
|
124
|
-
if (map == null) {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
StringBuilder resultBuilder = new StringBuilder();
|
|
128
|
-
if (map.hasKey("location")) {
|
|
129
|
-
String location = toLocation(map.getMap("location"));
|
|
130
|
-
if (location != null) {
|
|
131
|
-
resultBuilder.append(location);
|
|
132
|
-
if (map.hasKey("radius")) {
|
|
133
|
-
int radius = map.getInt("radius");
|
|
134
|
-
resultBuilder.append(",").append(radius);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
else if (map.hasKey("viewbox")) {
|
|
139
|
-
resultBuilder.append(toViewbox(map.getMap("viewbox")));
|
|
140
|
-
}
|
|
141
|
-
else if (map.hasKey("path")) {
|
|
142
|
-
resultBuilder.append(toAvoidPath(map.getArray("path")));
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return resultBuilder.toString();
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static String toAvoidRoads(ReadableMap map) {
|
|
149
|
-
if (map == null) {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
if (map.hasKey("types")) {
|
|
153
|
-
StringBuilder resultBuilder = new StringBuilder();
|
|
154
|
-
ReadableArray array = map.getArray("types");
|
|
155
|
-
for (int i = 0; i < array.size(); ++i) {
|
|
156
|
-
String type = array.getString(i);
|
|
157
|
-
resultBuilder.append(type);
|
|
158
|
-
if (i < array.size() - 1) {
|
|
159
|
-
resultBuilder.append(",");
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return resultBuilder.toString();
|
|
163
|
-
}
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
1
|
+
package com.reactnativemap4dservices;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
6
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
7
|
+
|
|
8
|
+
import org.json.JSONArray;
|
|
9
|
+
import org.json.JSONException;
|
|
10
|
+
import org.json.JSONObject;
|
|
11
|
+
|
|
12
|
+
class SConverter {
|
|
13
|
+
private static String toAvoidPath(ReadableArray array) {
|
|
14
|
+
if (array == null) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
StringBuilder resultBuilder = new StringBuilder();
|
|
18
|
+
for (int i = 0; i < array.size(); ++i) {
|
|
19
|
+
resultBuilder.append(toLocation(array.getMap(i)));
|
|
20
|
+
if (i < array.size() - 1) {
|
|
21
|
+
resultBuilder.append(",");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return resultBuilder.toString();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static String toLocation(ReadableMap map) {
|
|
28
|
+
if (map == null) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
double lat = map.getDouble("latitude");
|
|
32
|
+
double lng = map.getDouble("longitude");
|
|
33
|
+
return lat + "," + lng;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static String toViewbox(ReadableMap map) {
|
|
37
|
+
if (map == null) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
String southWest = toLocation(map.getMap("southwest"));
|
|
41
|
+
String northEast = toLocation(map.getMap("northeast"));
|
|
42
|
+
return southWest + "," + northEast;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static String toDatetime(double time) {
|
|
46
|
+
long datetime = (long) time;
|
|
47
|
+
return String.valueOf(datetime);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static String toTypes(@NonNull ReadableArray types) {
|
|
51
|
+
StringBuilder result = new StringBuilder();
|
|
52
|
+
for (int i = 0; i < types.size(); ++i) {
|
|
53
|
+
if (i > 0) {
|
|
54
|
+
result.append("&types=");
|
|
55
|
+
}
|
|
56
|
+
result.append(types.getString(i));
|
|
57
|
+
}
|
|
58
|
+
return result.toString();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static String toTags(@NonNull ReadableArray tags) {
|
|
62
|
+
StringBuilder result = new StringBuilder();
|
|
63
|
+
for (int i = 0; i < tags.size(); ++i) {
|
|
64
|
+
if (i > 0) {
|
|
65
|
+
result.append("&tags=");
|
|
66
|
+
}
|
|
67
|
+
result.append(tags.getString(i));
|
|
68
|
+
}
|
|
69
|
+
return result.toString();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static int toWeighting(@NonNull String weighting) {
|
|
73
|
+
switch (weighting) {
|
|
74
|
+
case "shortest":
|
|
75
|
+
return 0;
|
|
76
|
+
case "fastest":
|
|
77
|
+
return 1;
|
|
78
|
+
case "balance":
|
|
79
|
+
return 2;
|
|
80
|
+
default:
|
|
81
|
+
return 3;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static String toLocationList(ReadableArray map) {
|
|
86
|
+
if (map == null) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
StringBuilder result = new StringBuilder();
|
|
91
|
+
for (int i = 0; i < map.size(); ++i) {
|
|
92
|
+
result.append(toLocation(map.getMap(i)));
|
|
93
|
+
if (i < map.size() - 1) {
|
|
94
|
+
result.append(";");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return result.toString();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static JSONArray toOriginJsonArray(ReadableArray origins) {
|
|
101
|
+
if (origins == null) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
JSONArray originsJsonArray = null;
|
|
105
|
+
if (origins != null && origins.size() > 0) {
|
|
106
|
+
originsJsonArray = new JSONArray();
|
|
107
|
+
}
|
|
108
|
+
for (int i = 0; i < origins.size(); ++i) {
|
|
109
|
+
ReadableMap origin = origins.getMap(i);
|
|
110
|
+
try {
|
|
111
|
+
JSONObject originJsonObject = new JSONObject();
|
|
112
|
+
String alias = origin.getString("alias");
|
|
113
|
+
originJsonObject.put("alias", alias);
|
|
114
|
+
originJsonObject.put("location", toLocation(origin));
|
|
115
|
+
originsJsonArray.put(originJsonObject);
|
|
116
|
+
} catch (JSONException e) {
|
|
117
|
+
e.printStackTrace();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return originsJsonArray;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static String toAvoid(ReadableMap map) {
|
|
124
|
+
if (map == null) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
StringBuilder resultBuilder = new StringBuilder();
|
|
128
|
+
if (map.hasKey("location")) {
|
|
129
|
+
String location = toLocation(map.getMap("location"));
|
|
130
|
+
if (location != null) {
|
|
131
|
+
resultBuilder.append(location);
|
|
132
|
+
if (map.hasKey("radius")) {
|
|
133
|
+
int radius = map.getInt("radius");
|
|
134
|
+
resultBuilder.append(",").append(radius);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if (map.hasKey("viewbox")) {
|
|
139
|
+
resultBuilder.append(toViewbox(map.getMap("viewbox")));
|
|
140
|
+
}
|
|
141
|
+
else if (map.hasKey("path")) {
|
|
142
|
+
resultBuilder.append(toAvoidPath(map.getArray("path")));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return resultBuilder.toString();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static String toAvoidRoads(ReadableMap map) {
|
|
149
|
+
if (map == null) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if (map.hasKey("types")) {
|
|
153
|
+
StringBuilder resultBuilder = new StringBuilder();
|
|
154
|
+
ReadableArray array = map.getArray("types");
|
|
155
|
+
for (int i = 0; i < array.size(); ++i) {
|
|
156
|
+
String type = array.getString(i);
|
|
157
|
+
resultBuilder.append(type);
|
|
158
|
+
if (i < array.size() - 1) {
|
|
159
|
+
resultBuilder.append(",");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return resultBuilder.toString();
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
package com.reactnativemap4dservices;
|
|
2
|
-
|
|
3
|
-
import androidx.annotation.NonNull;
|
|
4
|
-
|
|
5
|
-
import com.facebook.react.bridge.ReadableArray;
|
|
6
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
7
|
-
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
8
|
-
import com.facebook.react.bridge.WritableArray;
|
|
9
|
-
import com.facebook.react.bridge.WritableMap;
|
|
10
|
-
import com.facebook.react.bridge.WritableNativeArray;
|
|
11
|
-
import com.facebook.react.bridge.WritableNativeMap;
|
|
12
|
-
|
|
13
|
-
import org.json.JSONArray;
|
|
14
|
-
import org.json.JSONException;
|
|
15
|
-
import org.json.JSONObject;
|
|
16
|
-
|
|
17
|
-
import java.util.Iterator;
|
|
18
|
-
|
|
19
|
-
class SJsonUtils {
|
|
20
|
-
@NonNull
|
|
21
|
-
static WritableMap convertJsonToMap(@NonNull JSONObject jsonObject) throws JSONException {
|
|
22
|
-
WritableMap map = new WritableNativeMap();
|
|
23
|
-
|
|
24
|
-
Iterator<String> iterator = jsonObject.keys();
|
|
25
|
-
while (iterator.hasNext()) {
|
|
26
|
-
String key = iterator.next();
|
|
27
|
-
Object value = jsonObject.get(key);
|
|
28
|
-
if (value instanceof JSONObject) {
|
|
29
|
-
map.putMap(key, convertJsonToMap((JSONObject) value));
|
|
30
|
-
} else if (value instanceof JSONArray) {
|
|
31
|
-
map.putArray(key, convertJsonToArray((JSONArray) value));
|
|
32
|
-
if (("option_values").equals(key)) {
|
|
33
|
-
map.putArray("options", convertJsonToArray((JSONArray) value));
|
|
34
|
-
}
|
|
35
|
-
} else if (value instanceof Boolean) {
|
|
36
|
-
map.putBoolean(key, (Boolean) value);
|
|
37
|
-
} else if (value instanceof Integer) {
|
|
38
|
-
map.putInt(key, (Integer) value);
|
|
39
|
-
} else if (value instanceof Double) {
|
|
40
|
-
map.putDouble(key, (Double) value);
|
|
41
|
-
} else if (value instanceof String) {
|
|
42
|
-
map.putString(key, (String) value);
|
|
43
|
-
} else {
|
|
44
|
-
map.putString(key, value.toString());
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return map;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
@NonNull
|
|
51
|
-
static WritableArray convertJsonToArray(@NonNull JSONArray jsonArray) throws JSONException {
|
|
52
|
-
WritableArray array = new WritableNativeArray();
|
|
53
|
-
for (int i = 0; i < jsonArray.length(); i++) {
|
|
54
|
-
Object value = jsonArray.get(i);
|
|
55
|
-
if (value instanceof JSONObject) {
|
|
56
|
-
array.pushMap(convertJsonToMap((JSONObject) value));
|
|
57
|
-
} else if (value instanceof JSONArray) {
|
|
58
|
-
array.pushArray(convertJsonToArray((JSONArray) value));
|
|
59
|
-
} else if (value instanceof Boolean) {
|
|
60
|
-
array.pushBoolean((Boolean) value);
|
|
61
|
-
} else if (value instanceof Integer) {
|
|
62
|
-
array.pushInt((Integer) value);
|
|
63
|
-
} else if (value instanceof Double) {
|
|
64
|
-
array.pushDouble((Double) value);
|
|
65
|
-
} else if (value instanceof String) {
|
|
66
|
-
array.pushString((String) value);
|
|
67
|
-
} else {
|
|
68
|
-
array.pushString(value.toString());
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return array;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
@NonNull
|
|
75
|
-
static JSONObject convertMapToJson(@NonNull ReadableMap readableMap) throws JSONException {
|
|
76
|
-
JSONObject object = new JSONObject();
|
|
77
|
-
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
|
|
78
|
-
while (iterator.hasNextKey()) {
|
|
79
|
-
String key = iterator.nextKey();
|
|
80
|
-
switch (readableMap.getType(key)) {
|
|
81
|
-
case Null:
|
|
82
|
-
object.put(key, JSONObject.NULL);
|
|
83
|
-
break;
|
|
84
|
-
case Boolean:
|
|
85
|
-
object.put(key, readableMap.getBoolean(key));
|
|
86
|
-
break;
|
|
87
|
-
case Number:
|
|
88
|
-
object.put(key, readableMap.getDouble(key));
|
|
89
|
-
break;
|
|
90
|
-
case String:
|
|
91
|
-
object.put(key, readableMap.getString(key));
|
|
92
|
-
break;
|
|
93
|
-
case Map:
|
|
94
|
-
object.put(key, convertMapToJson(readableMap.getMap(key)));
|
|
95
|
-
break;
|
|
96
|
-
case Array:
|
|
97
|
-
object.put(key, convertArrayToJson(readableMap.getArray(key)));
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return object;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
@NonNull
|
|
105
|
-
static JSONArray convertArrayToJson(@NonNull ReadableArray readableArray) throws JSONException {
|
|
106
|
-
JSONArray array = new JSONArray();
|
|
107
|
-
for (int i = 0; i < readableArray.size(); i++) {
|
|
108
|
-
switch (readableArray.getType(i)) {
|
|
109
|
-
case Null:
|
|
110
|
-
break;
|
|
111
|
-
case Boolean:
|
|
112
|
-
array.put(readableArray.getBoolean(i));
|
|
113
|
-
break;
|
|
114
|
-
case Number:
|
|
115
|
-
array.put(readableArray.getDouble(i));
|
|
116
|
-
break;
|
|
117
|
-
case String:
|
|
118
|
-
array.put(readableArray.getString(i));
|
|
119
|
-
break;
|
|
120
|
-
case Map:
|
|
121
|
-
array.put(convertMapToJson(readableArray.getMap(i)));
|
|
122
|
-
break;
|
|
123
|
-
case Array:
|
|
124
|
-
array.put(convertArrayToJson(readableArray.getArray(i)));
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return array;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
1
|
+
package com.reactnativemap4dservices;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
6
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
7
|
+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
8
|
+
import com.facebook.react.bridge.WritableArray;
|
|
9
|
+
import com.facebook.react.bridge.WritableMap;
|
|
10
|
+
import com.facebook.react.bridge.WritableNativeArray;
|
|
11
|
+
import com.facebook.react.bridge.WritableNativeMap;
|
|
12
|
+
|
|
13
|
+
import org.json.JSONArray;
|
|
14
|
+
import org.json.JSONException;
|
|
15
|
+
import org.json.JSONObject;
|
|
16
|
+
|
|
17
|
+
import java.util.Iterator;
|
|
18
|
+
|
|
19
|
+
class SJsonUtils {
|
|
20
|
+
@NonNull
|
|
21
|
+
static WritableMap convertJsonToMap(@NonNull JSONObject jsonObject) throws JSONException {
|
|
22
|
+
WritableMap map = new WritableNativeMap();
|
|
23
|
+
|
|
24
|
+
Iterator<String> iterator = jsonObject.keys();
|
|
25
|
+
while (iterator.hasNext()) {
|
|
26
|
+
String key = iterator.next();
|
|
27
|
+
Object value = jsonObject.get(key);
|
|
28
|
+
if (value instanceof JSONObject) {
|
|
29
|
+
map.putMap(key, convertJsonToMap((JSONObject) value));
|
|
30
|
+
} else if (value instanceof JSONArray) {
|
|
31
|
+
map.putArray(key, convertJsonToArray((JSONArray) value));
|
|
32
|
+
if (("option_values").equals(key)) {
|
|
33
|
+
map.putArray("options", convertJsonToArray((JSONArray) value));
|
|
34
|
+
}
|
|
35
|
+
} else if (value instanceof Boolean) {
|
|
36
|
+
map.putBoolean(key, (Boolean) value);
|
|
37
|
+
} else if (value instanceof Integer) {
|
|
38
|
+
map.putInt(key, (Integer) value);
|
|
39
|
+
} else if (value instanceof Double) {
|
|
40
|
+
map.putDouble(key, (Double) value);
|
|
41
|
+
} else if (value instanceof String) {
|
|
42
|
+
map.putString(key, (String) value);
|
|
43
|
+
} else {
|
|
44
|
+
map.putString(key, value.toString());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return map;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@NonNull
|
|
51
|
+
static WritableArray convertJsonToArray(@NonNull JSONArray jsonArray) throws JSONException {
|
|
52
|
+
WritableArray array = new WritableNativeArray();
|
|
53
|
+
for (int i = 0; i < jsonArray.length(); i++) {
|
|
54
|
+
Object value = jsonArray.get(i);
|
|
55
|
+
if (value instanceof JSONObject) {
|
|
56
|
+
array.pushMap(convertJsonToMap((JSONObject) value));
|
|
57
|
+
} else if (value instanceof JSONArray) {
|
|
58
|
+
array.pushArray(convertJsonToArray((JSONArray) value));
|
|
59
|
+
} else if (value instanceof Boolean) {
|
|
60
|
+
array.pushBoolean((Boolean) value);
|
|
61
|
+
} else if (value instanceof Integer) {
|
|
62
|
+
array.pushInt((Integer) value);
|
|
63
|
+
} else if (value instanceof Double) {
|
|
64
|
+
array.pushDouble((Double) value);
|
|
65
|
+
} else if (value instanceof String) {
|
|
66
|
+
array.pushString((String) value);
|
|
67
|
+
} else {
|
|
68
|
+
array.pushString(value.toString());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return array;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@NonNull
|
|
75
|
+
static JSONObject convertMapToJson(@NonNull ReadableMap readableMap) throws JSONException {
|
|
76
|
+
JSONObject object = new JSONObject();
|
|
77
|
+
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
|
|
78
|
+
while (iterator.hasNextKey()) {
|
|
79
|
+
String key = iterator.nextKey();
|
|
80
|
+
switch (readableMap.getType(key)) {
|
|
81
|
+
case Null:
|
|
82
|
+
object.put(key, JSONObject.NULL);
|
|
83
|
+
break;
|
|
84
|
+
case Boolean:
|
|
85
|
+
object.put(key, readableMap.getBoolean(key));
|
|
86
|
+
break;
|
|
87
|
+
case Number:
|
|
88
|
+
object.put(key, readableMap.getDouble(key));
|
|
89
|
+
break;
|
|
90
|
+
case String:
|
|
91
|
+
object.put(key, readableMap.getString(key));
|
|
92
|
+
break;
|
|
93
|
+
case Map:
|
|
94
|
+
object.put(key, convertMapToJson(readableMap.getMap(key)));
|
|
95
|
+
break;
|
|
96
|
+
case Array:
|
|
97
|
+
object.put(key, convertArrayToJson(readableMap.getArray(key)));
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return object;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@NonNull
|
|
105
|
+
static JSONArray convertArrayToJson(@NonNull ReadableArray readableArray) throws JSONException {
|
|
106
|
+
JSONArray array = new JSONArray();
|
|
107
|
+
for (int i = 0; i < readableArray.size(); i++) {
|
|
108
|
+
switch (readableArray.getType(i)) {
|
|
109
|
+
case Null:
|
|
110
|
+
break;
|
|
111
|
+
case Boolean:
|
|
112
|
+
array.put(readableArray.getBoolean(i));
|
|
113
|
+
break;
|
|
114
|
+
case Number:
|
|
115
|
+
array.put(readableArray.getDouble(i));
|
|
116
|
+
break;
|
|
117
|
+
case String:
|
|
118
|
+
array.put(readableArray.getString(i));
|
|
119
|
+
break;
|
|
120
|
+
case Map:
|
|
121
|
+
array.put(convertMapToJson(readableArray.getMap(i)));
|
|
122
|
+
break;
|
|
123
|
+
case Array:
|
|
124
|
+
array.put(convertArrayToJson(readableArray.getArray(i)));
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return array;
|
|
129
|
+
}
|
|
130
|
+
}
|