-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Convert json object to writable map * Make class/methods package-private(default)
- Loading branch information
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...SentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryJsonConverterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.sentry.react | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.facebook.react.bridge.WritableArray | ||
import com.facebook.react.bridge.WritableMap | ||
import io.sentry.react.RNSentryJsonConverter.convertToWritable | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class RNSentryJsonConverterTest { | ||
@Test | ||
fun testConvertToWritableWithSimpleJsonObject() { | ||
val jsonObject = | ||
JSONObject().apply { | ||
put("floatKey", 12.3f) | ||
put("doubleKey", 12.3) | ||
put("intKey", 123) | ||
put("stringKey", "test") | ||
put("nullKey", JSONObject.NULL) | ||
} | ||
|
||
val result: WritableMap? = convertToWritable(jsonObject) | ||
|
||
assertNotNull(result) | ||
assertEquals(12.3, result!!.getDouble("floatKey"), 0.0001) | ||
assertEquals(12.3, result.getDouble("doubleKey"), 0.0) | ||
assertEquals(123, result.getInt("intKey")) | ||
assertEquals("test", result.getString("stringKey")) | ||
assertNull(result.getString("nullKey")) | ||
} | ||
|
||
@Test | ||
fun testConvertToWritableWithNestedJsonObject() { | ||
val jsonObject = | ||
JSONObject().apply { | ||
put( | ||
"nested", | ||
JSONObject().apply { | ||
put("key", "value") | ||
}, | ||
) | ||
} | ||
|
||
val result: WritableMap? = convertToWritable(jsonObject) | ||
|
||
assertNotNull(result) | ||
val nestedMap = result!!.getMap("nested") | ||
assertNotNull(nestedMap) | ||
assertEquals("value", nestedMap!!.getString("key")) | ||
} | ||
|
||
@Test | ||
fun testConvertToWritableWithJsonArray() { | ||
val jsonArray = | ||
JSONArray().apply { | ||
put(1) | ||
put(2.5) | ||
put("string") | ||
put(JSONObject.NULL) | ||
} | ||
|
||
val result: WritableArray = convertToWritable(jsonArray) | ||
|
||
assertEquals(1, result.getInt(0)) | ||
assertEquals(2.5, result.getDouble(1), 0.0) | ||
assertEquals("string", result.getString(2)) | ||
assertNull(result.getString(3)) | ||
} | ||
|
||
@Test | ||
fun testConvertToWritableWithNestedJsonArray() { | ||
val jsonObject = | ||
JSONObject().apply { | ||
put( | ||
"array", | ||
JSONArray().apply { | ||
put( | ||
JSONObject().apply { | ||
put("key1", "value1") | ||
}, | ||
) | ||
put( | ||
JSONObject().apply { | ||
put("key2", "value2") | ||
}, | ||
) | ||
}, | ||
) | ||
} | ||
|
||
val result: WritableMap? = convertToWritable(jsonObject) | ||
|
||
val array = result?.getArray("array") | ||
assertEquals("value1", array?.getMap(0)?.getString("key1")) | ||
assertEquals("value2", array?.getMap(1)?.getString("key2")) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/core/android/src/main/java/io/sentry/react/RNSentryJsonConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.sentry.react; | ||
|
||
import com.facebook.react.bridge.JavaOnlyArray; | ||
import com.facebook.react.bridge.JavaOnlyMap; | ||
import com.facebook.react.bridge.WritableArray; | ||
import com.facebook.react.bridge.WritableMap; | ||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
import io.sentry.android.core.AndroidLogger; | ||
import java.util.Iterator; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
final class RNSentryJsonConverter { | ||
public static final String NAME = "RNSentry.RNSentryJsonConverter"; | ||
|
||
private static final ILogger logger = new AndroidLogger(NAME); | ||
|
||
private RNSentryJsonConverter() { | ||
throw new AssertionError("Utility class should not be instantiated"); | ||
} | ||
|
||
@Nullable | ||
static WritableMap convertToWritable(@NotNull JSONObject jsonObject) { | ||
try { | ||
WritableMap writableMap = new JavaOnlyMap(); | ||
Iterator<String> iterator = jsonObject.keys(); | ||
while (iterator.hasNext()) { | ||
String key = iterator.next(); | ||
Object value = jsonObject.get(key); | ||
if (value instanceof Float || value instanceof Double) { | ||
writableMap.putDouble(key, jsonObject.getDouble(key)); | ||
} else if (value instanceof Number) { | ||
writableMap.putInt(key, jsonObject.getInt(key)); | ||
} else if (value instanceof String) { | ||
writableMap.putString(key, jsonObject.getString(key)); | ||
} else if (value instanceof JSONObject) { | ||
writableMap.putMap(key, convertToWritable(jsonObject.getJSONObject(key))); | ||
} else if (value instanceof JSONArray) { | ||
writableMap.putArray(key, convertToWritable(jsonObject.getJSONArray(key))); | ||
} else if (value == JSONObject.NULL) { | ||
writableMap.putNull(key); | ||
} | ||
} | ||
return writableMap; | ||
} catch (JSONException e) { | ||
logger.log(SentryLevel.ERROR, "Error parsing json object:" + e.getMessage()); | ||
return null; | ||
} | ||
} | ||
|
||
@NotNull | ||
static WritableArray convertToWritable(@NotNull JSONArray jsonArray) throws JSONException { | ||
WritableArray writableArray = new JavaOnlyArray(); | ||
for (int i = 0; i < jsonArray.length(); i++) { | ||
Object value = jsonArray.get(i); | ||
if (value instanceof Float || value instanceof Double) { | ||
writableArray.pushDouble(jsonArray.getDouble(i)); | ||
} else if (value instanceof Number) { | ||
writableArray.pushInt(jsonArray.getInt(i)); | ||
} else if (value instanceof String) { | ||
writableArray.pushString(jsonArray.getString(i)); | ||
} else if (value instanceof JSONObject) { | ||
writableArray.pushMap(convertToWritable(jsonArray.getJSONObject(i))); | ||
} else if (value instanceof JSONArray) { | ||
writableArray.pushArray(convertToWritable(jsonArray.getJSONArray(i))); | ||
} else if (value == JSONObject.NULL) { | ||
writableArray.pushNull(); | ||
} | ||
} | ||
return writableArray; | ||
} | ||
} |