|
| 1 | +package io.sentry; |
| 2 | + |
| 3 | +import com.facebook.react.bridge.Arguments; |
| 4 | +import com.facebook.react.bridge.ReadableMap; |
| 5 | +import com.facebook.react.bridge.ReadableMapKeySetIterator; |
| 6 | +import com.facebook.react.bridge.ReadableType; |
| 7 | +import com.facebook.react.bridge.WritableMap; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Iterator; |
| 12 | + |
| 13 | +import org.json.JSONArray; |
| 14 | +import org.json.JSONObject; |
| 15 | +import org.json.JSONException; |
| 16 | + |
| 17 | +public class MapUtil { |
| 18 | + |
| 19 | + public static JSONObject toJSONObject(ReadableMap readableMap) throws JSONException { |
| 20 | + JSONObject jsonObject = new JSONObject(); |
| 21 | + |
| 22 | + ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); |
| 23 | + |
| 24 | + while (iterator.hasNextKey()) { |
| 25 | + String key = iterator.nextKey(); |
| 26 | + ReadableType type = readableMap.getType(key); |
| 27 | + |
| 28 | + switch (type) { |
| 29 | + case Null: |
| 30 | + jsonObject.put(key, null); |
| 31 | + break; |
| 32 | + case Boolean: |
| 33 | + jsonObject.put(key, readableMap.getBoolean(key)); |
| 34 | + break; |
| 35 | + case Number: |
| 36 | + jsonObject.put(key, readableMap.getDouble(key)); |
| 37 | + break; |
| 38 | + case String: |
| 39 | + jsonObject.put(key, readableMap.getString(key)); |
| 40 | + break; |
| 41 | + case Map: |
| 42 | + jsonObject.put(key, MapUtil.toJSONObject(readableMap.getMap(key))); |
| 43 | + break; |
| 44 | + case Array: |
| 45 | + jsonObject.put(key, ArrayUtil.toJSONArray(readableMap.getArray(key))); |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return jsonObject; |
| 51 | + } |
| 52 | + |
| 53 | + public static Map<String, Object> toMap(JSONObject jsonObject) throws JSONException { |
| 54 | + Map<String, Object> map = new HashMap<>(); |
| 55 | + Iterator<String> iterator = jsonObject.keys(); |
| 56 | + |
| 57 | + while (iterator.hasNext()) { |
| 58 | + String key = iterator.next(); |
| 59 | + Object value = jsonObject.get(key); |
| 60 | + |
| 61 | + if (value instanceof JSONObject) { |
| 62 | + value = MapUtil.toMap((JSONObject) value); |
| 63 | + } |
| 64 | + if (value instanceof JSONArray) { |
| 65 | + value = ArrayUtil.toArray((JSONArray) value); |
| 66 | + } |
| 67 | + |
| 68 | + map.put(key, value); |
| 69 | + } |
| 70 | + |
| 71 | + return map; |
| 72 | + } |
| 73 | + |
| 74 | + public static Map<String, Object> toMap(ReadableMap readableMap) { |
| 75 | + Map<String, Object> map = new HashMap<>(); |
| 76 | + ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); |
| 77 | + |
| 78 | + while (iterator.hasNextKey()) { |
| 79 | + String key = iterator.nextKey(); |
| 80 | + ReadableType type = readableMap.getType(key); |
| 81 | + |
| 82 | + switch (type) { |
| 83 | + case Null: |
| 84 | + map.put(key, null); |
| 85 | + break; |
| 86 | + case Boolean: |
| 87 | + map.put(key, readableMap.getBoolean(key)); |
| 88 | + break; |
| 89 | + case Number: |
| 90 | + map.put(key, readableMap.getDouble(key)); |
| 91 | + break; |
| 92 | + case String: |
| 93 | + map.put(key, readableMap.getString(key)); |
| 94 | + break; |
| 95 | + case Map: |
| 96 | + map.put(key, MapUtil.toMap(readableMap.getMap(key))); |
| 97 | + break; |
| 98 | + case Array: |
| 99 | + map.put(key, ArrayUtil.toArray(readableMap.getArray(key))); |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return map; |
| 105 | + } |
| 106 | + |
| 107 | + public static WritableMap toWritableMap(Map<String, Object> map) { |
| 108 | + WritableMap writableMap = Arguments.createMap(); |
| 109 | + Iterator iterator = map.entrySet().iterator(); |
| 110 | + |
| 111 | + while (iterator.hasNext()) { |
| 112 | + Map.Entry pair = (Map.Entry)iterator.next(); |
| 113 | + Object value = pair.getValue(); |
| 114 | + |
| 115 | + if (value == null) { |
| 116 | + writableMap.putNull((String) pair.getKey()); |
| 117 | + } else if (value instanceof Boolean) { |
| 118 | + writableMap.putBoolean((String) pair.getKey(), (Boolean) value); |
| 119 | + } else if (value instanceof Double) { |
| 120 | + writableMap.putDouble((String) pair.getKey(), (Double) value); |
| 121 | + } else if (value instanceof Integer) { |
| 122 | + writableMap.putInt((String) pair.getKey(), (Integer) value); |
| 123 | + } else if (value instanceof String) { |
| 124 | + writableMap.putString((String) pair.getKey(), (String) value); |
| 125 | + } else if (value instanceof Map) { |
| 126 | + writableMap.putMap((String) pair.getKey(), MapUtil.toWritableMap((Map<String, Object>) value)); |
| 127 | + } else if (value.getClass() != null && value.getClass().isArray()) { |
| 128 | + writableMap.putArray((String) pair.getKey(), ArrayUtil.toWritableArray((Object[]) value)); |
| 129 | + } |
| 130 | + |
| 131 | + iterator.remove(); |
| 132 | + } |
| 133 | + |
| 134 | + return writableMap; |
| 135 | + } |
| 136 | +} |
0 commit comments