-
-
Save contfedorov/87b80771786eeb8fc6e92ab7ae4c89bc to your computer and use it in GitHub Desktop.
React-Native Module, ReadableMap/Array to Java JSON objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static WritableMap convertJsonToMap(@NonNull JSONObject jsonObject) throws JSONException { | |
WritableMap map = Arguments.createMap(); | |
Iterator<String> iterator = jsonObject.keys(); | |
while (iterator.hasNext()) { | |
String key = iterator.next(); | |
Object value = jsonObject.get(key); | |
if (value instanceof JSONObject) { | |
map.putMap(key, convertJsonToMap((JSONObject) value)); | |
} else if (value instanceof JSONArray) { | |
map.putArray(key, convertJsonToArray((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 Long) { | |
map.putDouble(key, ((Long) value).doubleValue()); | |
} else if (value instanceof Double) { | |
map.putDouble(key, (Double) value); | |
} else if (value instanceof String) { | |
map.putString(key, (String) value); | |
} else if (value == JSONObject.NULL) { | |
map.putNull(key); | |
} else { | |
// value != null since jsonObject.put("key", null) removes value for "key" | |
map.putString(key, value.toString()); | |
} | |
} | |
return map; | |
} | |
public static WritableArray convertJsonToArray(@NonNull JSONArray jsonArray) throws JSONException { | |
WritableArray array = Arguments.createArray(); | |
for (int i = 0; i < jsonArray.length(); i++) { | |
Object value = jsonArray.get(i); | |
if (value instanceof JSONObject) { | |
array.pushMap(convertJsonToMap((JSONObject) value)); | |
} else if (value instanceof JSONArray) { | |
array.pushArray(convertJsonToArray((JSONArray) value)); | |
} else if (value instanceof Boolean) { | |
array.pushBoolean((Boolean) value); | |
} else if (value instanceof Integer) { | |
array.pushInt((Integer) value); | |
} else if (value instanceof Long) { | |
array.pushDouble(((Long) value).doubleValue()); | |
} else if (value instanceof Double) { | |
array.pushDouble((Double) value); | |
} else if (value instanceof String) { | |
array.pushString((String) value); | |
} else if (value == JSONObject.NULL) { | |
array.pushNull(); | |
} else { | |
// value != null since jsonArray.get() throws in this case | |
array.pushString(value.toString()); | |
} | |
} | |
return array; | |
} | |
public static JSONObject convertMapToJson(@NonNull ReadableMap readableMap) throws JSONException { | |
JSONObject object = new JSONObject(); | |
ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); | |
while (iterator.hasNextKey()) { | |
String key = iterator.nextKey(); | |
switch (readableMap.getType(key)) { | |
case Null: | |
object.put(key, JSONObject.NULL); | |
break; | |
case Boolean: | |
object.put(key, readableMap.getBoolean(key)); | |
break; | |
case Number: | |
object.put(key, readableMap.getDouble(key)); | |
break; | |
case String: | |
object.put(key, readableMap.getString(key)); | |
break; | |
case Map: | |
//noinspection ConstantConditions | |
object.put(key, convertMapToJson(readableMap.getMap(key))); | |
break; | |
case Array: | |
//noinspection ConstantConditions | |
object.put(key, convertArrayToJson(readableMap.getArray(key))); | |
break; | |
} | |
} | |
return object; | |
} | |
public static JSONArray convertArrayToJson(@NonNull ReadableArray readableArray) throws JSONException { | |
JSONArray array = new JSONArray(); | |
for (int i = 0; i < readableArray.size(); i++) { | |
switch (readableArray.getType(i)) { | |
case Null: | |
array.put(JSONObject.NULL); | |
break; | |
case Boolean: | |
array.put(readableArray.getBoolean(i)); | |
break; | |
case Number: | |
array.put(readableArray.getDouble(i)); | |
break; | |
case String: | |
array.put(readableArray.getString(i)); | |
break; | |
case Map: | |
array.put(convertMapToJson(readableArray.getMap(i))); | |
break; | |
case Array: | |
array.put(convertArrayToJson(readableArray.getArray(i))); | |
break; | |
} | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Null
type /JSONObject.NULL
handlingLong
type handling