Appcelerator Titanium KrollDict fails to sanitize JSONObject.NULL values

If you’re developing an Appcelerator Titanium module for Android it’s possible that you’ll run into this exception (see bottom of post for full exception trace):

!!! Unable to convert unknown Java object class 'org.json.JSONObject$1' to Js value !!!

It took me a long time to track down the cause of this error, but essentially the issue is in the JNI layer, which passes native Java objects to the Javascript layer does not know how to handle a JSONObject.NULL value. The Titanium wrapper object KrollDict attempts to convert all non-native Java objects into native ones. JSONObject becomes a simple Map, JSONArray becomes an ArrayList and so on. However, the constructor fails to account for instances of the JSONObject.NULL object, which is not a simple primitive.

An easy work around would be to ensure the JSON objects that you’re passing around never have any true JSON null values in them. You can replace them with empty strings, or simply remove the key from the object altogether. This is ultimately the fix we went with. However, a simple two-line patch should solve this issue in the core Titanium code:

From 57868ea5b822468563d4a1be2adb901cce383f11 Mon Sep 17 00:00:00 2001
From: Tristan Waddington <tristan.waddington@gmail.com>
Date: Mon, 4 Jun 2012 14:24:15 -0700
Subject: [PATCH] Update the 'fromJSON' method of 'KrollDict' to map the
 'JSONObject.NULL' Object to a real Java null
 representation.
 
---
 android/titanium/src/java/org/appcelerator/kroll/KrollDict.java |    2 ++
 1 file changed, 2 insertions(+)
 
diff --git a/android/titanium/src/java/org/appcelerator/kroll/KrollDict.java b/android/titanium/src/java/org/appcelerator/kroll/KrollDict.java
index da29217..96017d6 100644
--- a/android/titanium/src/java/org/appcelerator/kroll/KrollDict.java
+++ b/android/titanium/src/java/org/appcelerator/kroll/KrollDict.java
@@ -56,6 +56,8 @@ public static Object fromJSON(Object value) {
 					values[i] = fromJSON(array.get(i));
 				}
 				return values;
+			} else if (value == JSONObject.NULL) {
+				return null;
 			}
 		} catch (JSONException e) {
 			Log.e(TAG, "Error parsing JSON", e);
-- 
1.7.10

I submitted a pull request to the titanium_mobile repository on GitHub, but all contributors are required to sign a Contributor License Agreement (CLA). Not a big deal, but I don’t expect to ever submit another patch to this project, so I’m not inclined to spend a day getting their dev environment bootstrapped and sign my life away for a two-line bug fix (yes, I wrote a passive-aggressive blog post instead).

Unfortunately, they don’t even seem interested in accepting the patch as a simple bug report so someone else can fix it:

If you’re trying to build a platform that’s meant to be developer focused, willful disinterest is not the best response. If you’re curious, here’s the raw patch file and the full exception:

5 thoughts on “Appcelerator Titanium KrollDict fails to sanitize JSONObject.NULL values

  1. I’m experiencing the same issue.
    Could you post the link with the bug report, so we can all make pressure to fix this?

  2. Tristan,

    Thank you for taking the time to provide a detailed report of this bug.
    I am sorry if our previous communication gave the impression of
    being “disinterest” in the contribution and resolving the issue.

    I personally value working with the community. I know first
    hand how important this can be for the platform’s success.
    So I wanted to take time out of my weekend to correct this wrong.

    I have filed a bug report (11055) and created a patch
    to resolve the problem and add some extra regression testing.
    The problem should be resolved in our next major release.

    Thank you again for your contribution. I sincerely apologize for
    how this was originally handled and hope our actions today show
    we are willing to admit our wrongs and correct them.

    I hope this isn’t the end of your Titanium adventures.
    I think with the help from developers like yourself the platform
    has great potential.

    Josh Roesslein
    Titanium Platform Engineer

    • Thanks Josh, I dug up the bug report you mentioned here:

      https://jira.appcelerator.org/browse/TIMOB-11055

      I appreciate you taking the time to write up a proper bug report for this issue. I probably should have done that myself originally, but it’s way harder than it should be to find the right venue to report these kinds of issues. I’m not working with Titanium much these days, but I’ll be glad to see this issue get resolved for others.

      You may want to update the README for the titanium_mobile project to include a prominent link to your bug tracker. The contributing section mentions this, but provides no way to get there directly:

      https://github.com/appcelerator/titanium_mobile#contributing

      Again, thanks for digging up this old issue and getting it some attention.

  3. It solved an anoying problem with my app. Just patched Titanium source and compile it. Many thanks for your work!

Comments are closed.