Skip to content

Commit 1e5dbde

Browse files
authored
Adds utility class for converting JsonObject to WritableMap (#4479)
* Convert json object to writable map * Make class/methods package-private(default)
1 parent 7850677 commit 1e5dbde

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.sentry.react
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import com.facebook.react.bridge.WritableArray
5+
import com.facebook.react.bridge.WritableMap
6+
import io.sentry.react.RNSentryJsonConverter.convertToWritable
7+
import org.json.JSONArray
8+
import org.json.JSONObject
9+
import org.junit.Assert.assertEquals
10+
import org.junit.Assert.assertNotNull
11+
import org.junit.Assert.assertNull
12+
import org.junit.Test
13+
import org.junit.runner.RunWith
14+
15+
@RunWith(AndroidJUnit4::class)
16+
class RNSentryJsonConverterTest {
17+
@Test
18+
fun testConvertToWritableWithSimpleJsonObject() {
19+
val jsonObject =
20+
JSONObject().apply {
21+
put("floatKey", 12.3f)
22+
put("doubleKey", 12.3)
23+
put("intKey", 123)
24+
put("stringKey", "test")
25+
put("nullKey", JSONObject.NULL)
26+
}
27+
28+
val result: WritableMap? = convertToWritable(jsonObject)
29+
30+
assertNotNull(result)
31+
assertEquals(12.3, result!!.getDouble("floatKey"), 0.0001)
32+
assertEquals(12.3, result.getDouble("doubleKey"), 0.0)
33+
assertEquals(123, result.getInt("intKey"))
34+
assertEquals("test", result.getString("stringKey"))
35+
assertNull(result.getString("nullKey"))
36+
}
37+
38+
@Test
39+
fun testConvertToWritableWithNestedJsonObject() {
40+
val jsonObject =
41+
JSONObject().apply {
42+
put(
43+
"nested",
44+
JSONObject().apply {
45+
put("key", "value")
46+
},
47+
)
48+
}
49+
50+
val result: WritableMap? = convertToWritable(jsonObject)
51+
52+
assertNotNull(result)
53+
val nestedMap = result!!.getMap("nested")
54+
assertNotNull(nestedMap)
55+
assertEquals("value", nestedMap!!.getString("key"))
56+
}
57+
58+
@Test
59+
fun testConvertToWritableWithJsonArray() {
60+
val jsonArray =
61+
JSONArray().apply {
62+
put(1)
63+
put(2.5)
64+
put("string")
65+
put(JSONObject.NULL)
66+
}
67+
68+
val result: WritableArray = convertToWritable(jsonArray)
69+
70+
assertEquals(1, result.getInt(0))
71+
assertEquals(2.5, result.getDouble(1), 0.0)
72+
assertEquals("string", result.getString(2))
73+
assertNull(result.getString(3))
74+
}
75+
76+
@Test
77+
fun testConvertToWritableWithNestedJsonArray() {
78+
val jsonObject =
79+
JSONObject().apply {
80+
put(
81+
"array",
82+
JSONArray().apply {
83+
put(
84+
JSONObject().apply {
85+
put("key1", "value1")
86+
},
87+
)
88+
put(
89+
JSONObject().apply {
90+
put("key2", "value2")
91+
},
92+
)
93+
},
94+
)
95+
}
96+
97+
val result: WritableMap? = convertToWritable(jsonObject)
98+
99+
val array = result?.getArray("array")
100+
assertEquals("value1", array?.getMap(0)?.getString("key1"))
101+
assertEquals("value2", array?.getMap(1)?.getString("key2"))
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)