react-native-code-push
react-native-code-push copied to clipboard
JsonObject converter unexpectedly crashes with "Unrecognized object: null"
Hello!
Recently I've encountered an issue with [email protected]
in my Android project.
I've tried to use the CodePushUtils :: convertJsonObjectToWritable()
method for easy conversion my objects before passing those data to React.
As I've noticed, there is a problem with conversion of the null
values.
The root cause is:
- The
JSONObject.get()
method is defined as@NonNull
, so never return anull
value. - Converter method still makes the
obj == null
comparison before puttingnull
into a map. - As
obj
is never null, this comparison is always false, so leads to throwing an exception "Unrecognized object: null"
A demo project to try on: https://github.com/vfedosieievdish/CodepushBugDemoJsonParser
The MainApplication :: testCodePushJSONParser()
will be triggered at startup to convert the {"intParam":5,"nullParam":null,"strParam":"my string"}
JSON string and crash on conversion the "nullParam":null
.
String jsonStr = "{\"intParam\":5,\"nullParam\":null,\"strParam\":\"my string\"}";
WritableMap m = CodePushUtils.convertJsonObjectToWritable(new JSONObject(jsonStr));
Apply a proposed patch (see below), and the conversion logic will be executed correctly (the obj
will remain null
for null values).
diff --git a/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java
index eb099bd..b0eb0c4 100644
--- a/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java
+++ b/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java
@@ -67,7 +67,9 @@ public class CodePushUtils {
String key = it.next();
Object obj = null;
try {
- obj = jsonObj.get(key);
+ if (!jsonObj.isNull(key)) {
+ obj = jsonObj.get(key);
+ }
} catch (JSONException jsonException) {
// Should not happen.
throw new CodePushUnknownException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException);
Hi! A pull request with a fix for this issue has been merged into the master branch. The fix will be included in the next release of react-native-code-push. Therefore, I'm closing this issue.
The fix has been released.