Skip to content

Commit 5fb03ef

Browse files
committed
Adds Javadoc and null annotations in json utils
1 parent ad460d5 commit 5fb03ef

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/rnsentryandroidtester/RNSentryJsonUtilsTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class RNSentryJsonUtilsTest {
3636

3737
assertNotNull(result)
3838
assertTrue(result is JavaOnlyMap)
39-
assertEquals("stringValue", result.getString("stringKey"))
40-
assertEquals(true, result.getBoolean("booleanKey"))
41-
assertEquals(123, result.getInt("intKey"))
39+
assertEquals("stringValue", result?.getString("stringKey"))
40+
assertEquals(true, result?.getBoolean("booleanKey"))
41+
assertEquals(123, result?.getInt("intKey"))
4242
}
4343

4444
@Test
@@ -80,17 +80,17 @@ class RNSentryJsonUtilsTest {
8080

8181
assertNotNull(result)
8282
assertTrue(result is JavaOnlyMap)
83-
assertEquals("stringValue", result.getString("stringKey"))
84-
assertEquals(true, result.getBoolean("booleanKey"))
85-
assertEquals(123, result.getInt("intKey"))
86-
val nested = result.getMap("nestedKey")
83+
assertEquals("stringValue", result?.getString("stringKey"))
84+
assertEquals(true, result?.getBoolean("booleanKey"))
85+
assertEquals(123, result?.getInt("intKey"))
86+
val nested = result?.getMap("nestedKey")
8787
assertNotNull(nested)
8888
assertEquals("nestedStringValue", nested?.getString("nestedStringKey"))
8989
assertEquals(false, nested?.getBoolean("nestedBooleanKey"))
9090
val deepNestedArray = nested?.getArray("deepNestedArrayKey")
9191
assertNotNull(deepNestedArray)
9292
assertEquals("deepNestedArrayValue", deepNestedArray?.getString(0))
93-
val array = result.getArray("arrayKey")
93+
val array = result?.getArray("arrayKey")
9494
assertNotNull(array)
9595
assertEquals("arrayStringValue", array?.getString(0))
9696
assertEquals(789, array?.getInt(1))

packages/core/android/src/main/java/io/sentry/react/RNSentryJsonUtils.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.Iterator;
1414
import java.util.List;
1515
import java.util.Map;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
1618
import org.json.JSONArray;
1719
import org.json.JSONException;
1820
import org.json.JSONObject;
@@ -22,8 +24,17 @@ private RNSentryJsonUtils() {
2224
throw new AssertionError("Utility class should not be instantiated");
2325
}
2426

25-
public static JSONObject getOptionsFromConfigurationFile(
26-
Context context, String fileName, ILogger logger) {
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) {
2738
try (InputStream inputStream = context.getAssets().open(fileName);
2839
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
2940

@@ -46,7 +57,7 @@ public static JSONObject getOptionsFromConfigurationFile(
4657
}
4758
}
4859

49-
private static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) {
60+
private static @NotNull Map<String, Object> jsonObjectToMap(@NotNull JSONObject jsonObject) {
5061
Map<String, Object> map = new HashMap<>();
5162
Iterator<String> keys = jsonObject.keys();
5263
while (keys.hasNext()) {
@@ -62,7 +73,7 @@ private static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) {
6273
return map;
6374
}
6475

65-
private static List<Object> jsonArrayToList(JSONArray jsonArray) {
76+
private static @NotNull List<Object> jsonArrayToList(@NotNull JSONArray jsonArray) {
6677
List<Object> list = new ArrayList<>();
6778

6879
for (int i = 0; i < jsonArray.length(); i++) {
@@ -73,7 +84,7 @@ private static List<Object> jsonArrayToList(JSONArray jsonArray) {
7384
return list;
7485
}
7586

76-
private static Object convertValue(Object value) {
87+
private static @Nullable Object convertValue(@Nullable Object value) {
7788
if (value instanceof JSONObject) {
7889
return jsonObjectToMap((JSONObject) value);
7990
} else if (value instanceof JSONArray) {
@@ -83,7 +94,16 @@ private static Object convertValue(Object value) {
8394
}
8495
}
8596

86-
public static ReadableMap jsonObjectToReadableMap(JSONObject jsonObject) {
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+
}
87107
Map<String, Object> map = jsonObjectToMap(jsonObject);
88108
return (WritableMap) RNSentryMapConverter.convertToJavaWritable(map);
89109
}

0 commit comments

Comments
 (0)