|
| 1 | +package io.sentry.react; |
| 2 | + |
| 3 | +import com.facebook.react.bridge.JavaOnlyArray; |
| 4 | +import com.facebook.react.bridge.JavaOnlyMap; |
| 5 | +import com.facebook.react.bridge.WritableArray; |
| 6 | +import com.facebook.react.bridge.WritableMap; |
| 7 | +import io.sentry.ILogger; |
| 8 | +import io.sentry.SentryLevel; |
| 9 | +import io.sentry.android.core.AndroidLogger; |
| 10 | +import java.util.Iterator; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import org.json.JSONArray; |
| 14 | +import org.json.JSONException; |
| 15 | +import org.json.JSONObject; |
| 16 | + |
| 17 | +final class RNSentryJsonConverter { |
| 18 | + public static final String NAME = "RNSentry.RNSentryJsonConverter"; |
| 19 | + |
| 20 | + private static final ILogger logger = new AndroidLogger(NAME); |
| 21 | + |
| 22 | + private RNSentryJsonConverter() { |
| 23 | + throw new AssertionError("Utility class should not be instantiated"); |
| 24 | + } |
| 25 | + |
| 26 | + @Nullable |
| 27 | + static WritableMap convertToWritable(@NotNull JSONObject jsonObject) { |
| 28 | + try { |
| 29 | + WritableMap writableMap = new JavaOnlyMap(); |
| 30 | + Iterator<String> iterator = jsonObject.keys(); |
| 31 | + while (iterator.hasNext()) { |
| 32 | + String key = iterator.next(); |
| 33 | + Object value = jsonObject.get(key); |
| 34 | + if (value instanceof Float || value instanceof Double) { |
| 35 | + writableMap.putDouble(key, jsonObject.getDouble(key)); |
| 36 | + } else if (value instanceof Number) { |
| 37 | + writableMap.putInt(key, jsonObject.getInt(key)); |
| 38 | + } else if (value instanceof String) { |
| 39 | + writableMap.putString(key, jsonObject.getString(key)); |
| 40 | + } else if (value instanceof JSONObject) { |
| 41 | + writableMap.putMap(key, convertToWritable(jsonObject.getJSONObject(key))); |
| 42 | + } else if (value instanceof JSONArray) { |
| 43 | + writableMap.putArray(key, convertToWritable(jsonObject.getJSONArray(key))); |
| 44 | + } else if (value == JSONObject.NULL) { |
| 45 | + writableMap.putNull(key); |
| 46 | + } |
| 47 | + } |
| 48 | + return writableMap; |
| 49 | + } catch (JSONException e) { |
| 50 | + logger.log(SentryLevel.ERROR, "Error parsing json object:" + e.getMessage()); |
| 51 | + return null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @NotNull |
| 56 | + static WritableArray convertToWritable(@NotNull JSONArray jsonArray) throws JSONException { |
| 57 | + WritableArray writableArray = new JavaOnlyArray(); |
| 58 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 59 | + Object value = jsonArray.get(i); |
| 60 | + if (value instanceof Float || value instanceof Double) { |
| 61 | + writableArray.pushDouble(jsonArray.getDouble(i)); |
| 62 | + } else if (value instanceof Number) { |
| 63 | + writableArray.pushInt(jsonArray.getInt(i)); |
| 64 | + } else if (value instanceof String) { |
| 65 | + writableArray.pushString(jsonArray.getString(i)); |
| 66 | + } else if (value instanceof JSONObject) { |
| 67 | + writableArray.pushMap(convertToWritable(jsonArray.getJSONObject(i))); |
| 68 | + } else if (value instanceof JSONArray) { |
| 69 | + writableArray.pushArray(convertToWritable(jsonArray.getJSONArray(i))); |
| 70 | + } else if (value == JSONObject.NULL) { |
| 71 | + writableArray.pushNull(); |
| 72 | + } |
| 73 | + } |
| 74 | + return writableArray; |
| 75 | + } |
| 76 | +} |
0 commit comments