Recently I've encountered an issue with react-native-code-push@8.2.1 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 a null value.
Converter method still makes the obj == null comparison before putting null into a map.
As obj is never null, this comparison is always false, so leads to throwing an exception "Unrecognized object: null"
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.
Hello!
Recently I've encountered an issue with
react-native-code-push@8.2.1
in my Android project. I've tried to use theCodePushUtils :: 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:JSONObject.get()
method is defined as@NonNull
, so never return anull
value.obj == null
comparison before puttingnull
into a map.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
.Apply a proposed patch (see below), and the conversion logic will be executed correctly (the
obj
will remainnull
for null values).