|
| 1 | +package io.sentry.react; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import com.facebook.react.bridge.ReadableMap; |
| 5 | +import com.facebook.react.bridge.WritableMap; |
| 6 | +import io.sentry.ILogger; |
| 7 | +import io.sentry.SentryLevel; |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | +import org.json.JSONArray; |
| 19 | +import org.json.JSONException; |
| 20 | +import org.json.JSONObject; |
| 21 | + |
| 22 | +public final class RNSentryJsonUtils { |
| 23 | + private RNSentryJsonUtils() { |
| 24 | + throw new AssertionError("Utility class should not be instantiated"); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Read the configuration file in the Android assets folder and return the options as a |
| 29 | + * JSONObject. |
| 30 | + * |
| 31 | + * @param context Android Context |
| 32 | + * @param fileName configuration file name |
| 33 | + * @param logger Sentry logger |
| 34 | + * @return JSONObject with the configuration options |
| 35 | + */ |
| 36 | + public static @Nullable JSONObject getOptionsFromConfigurationFile( |
| 37 | + @NotNull Context context, @NotNull String fileName, @NotNull ILogger logger) { |
| 38 | + try (InputStream inputStream = context.getAssets().open(fileName); |
| 39 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { |
| 40 | + |
| 41 | + StringBuilder stringBuilder = new StringBuilder(); |
| 42 | + String line; |
| 43 | + while ((line = reader.readLine()) != null) { |
| 44 | + stringBuilder.append(line); |
| 45 | + } |
| 46 | + String configFileContent = stringBuilder.toString(); |
| 47 | + return new JSONObject(configFileContent); |
| 48 | + |
| 49 | + } catch (Exception e) { |
| 50 | + logger.log( |
| 51 | + SentryLevel.ERROR, |
| 52 | + "Failed to read configuration file. Please make sure " |
| 53 | + + fileName |
| 54 | + + " exists in the root of your project.", |
| 55 | + e); |
| 56 | + return null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static @NotNull Map<String, Object> jsonObjectToMap(@NotNull JSONObject jsonObject) { |
| 61 | + Map<String, Object> map = new HashMap<>(); |
| 62 | + Iterator<String> keys = jsonObject.keys(); |
| 63 | + while (keys.hasNext()) { |
| 64 | + String key = keys.next(); |
| 65 | + Object value = null; |
| 66 | + try { |
| 67 | + value = jsonObject.get(key); |
| 68 | + } catch (JSONException e) { |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + map.put(key, convertValue(value)); |
| 72 | + } |
| 73 | + return map; |
| 74 | + } |
| 75 | + |
| 76 | + private static @NotNull List<Object> jsonArrayToList(@NotNull JSONArray jsonArray) { |
| 77 | + List<Object> list = new ArrayList<>(); |
| 78 | + |
| 79 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 80 | + Object value = jsonArray.opt(i); |
| 81 | + list.add(convertValue(value)); |
| 82 | + } |
| 83 | + |
| 84 | + return list; |
| 85 | + } |
| 86 | + |
| 87 | + private static @Nullable Object convertValue(@Nullable Object value) { |
| 88 | + if (value instanceof JSONObject) { |
| 89 | + return jsonObjectToMap((JSONObject) value); |
| 90 | + } else if (value instanceof JSONArray) { |
| 91 | + return jsonArrayToList((JSONArray) value); |
| 92 | + } else { |
| 93 | + return value; // Primitive type or null |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Convert a JSONObject to a ReadableMap |
| 99 | + * |
| 100 | + * @param jsonObject JSONObject to convert |
| 101 | + * @return ReadableMap with the same data as the JSONObject |
| 102 | + */ |
| 103 | + public static @Nullable ReadableMap jsonObjectToReadableMap(@Nullable JSONObject jsonObject) { |
| 104 | + if (jsonObject == null) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + Map<String, Object> map = jsonObjectToMap(jsonObject); |
| 108 | + return (WritableMap) RNSentryMapConverter.convertToJavaWritable(map); |
| 109 | + } |
| 110 | +} |
0 commit comments