1

I have an Android app which in some places generates JSON, serialises it, and then at a later time de-serialises it and uses the data. I'm using the builtin JSONObject On Android 5 and up which looks the org.json package.

My app runs fine on all Android 5.0 and newer devices, but on Android 4.x it fails in some places. Looking in the debugger the de-serialised JSONObject looks somewhat broken.

This seems like some kind of bug in the JSON library that ships with older android, and I'd like to simply use a newer up to date version from MavenCentral or JCenter

How do I do this? I've added

compile 'org.json:json:20160212'

To my app's build.gradle dependencies section, but it doesn't seem to make any difference.

Is this possible or does the old busted android system library always win?


Update: It turns out not to be a bug in JSON parsing, but in JSON generation. The app was generating JSON from Java Map and List objects - which in Android 4 results in incorrect string output: More details here: http://fupeg.blogspot.co.nz/2011/07/android-json-bug.html

I've worked around the problem by writing the following two functions:

public static JSONObject mapToJSON(Map<String,Object> map){
    HashMap<String,Object> fixed = new HashMap<>();
    for (String key : map.keySet()){
        Object value = map.get(key);
        if (value instanceof Map){
            value = mapToJSON((Map<String,Object>) value);
        } else if (value instanceof List) {
            value = listToJSON((List<Object>)value);
        }
        fixed.put(key,value);
    }
    return new JSONObject(fixed);
}

public static JSONArray listToJSON(List<Object> list) {
    JSONArray result = new JSONArray();
    for (Object value : list){
        if (value instanceof Map){
            value = mapToJSON((Map<String,Object>) value);
        } else if (value instanceof List) {
            value = listToJSON((List<Object>)value);
        }
        result.put(value);
    }
    return result;
}

And replacing all calls in the app

  • new JSONObject(someList) replaced with listToJSON(someList)
  • new JSONObject(someMap) replaced with mapToJSON(someMap)

The question still stands though. It'd be much better if I didn't have to implement this workaround, and could instead bundle a newer version of the org.json library for use on Android 4.0. Does anyone know how I might do this on Android? Or if it's not possible?

4
  • Could you paste the differences in both parsed objects (The Android 5 and the 4 ones?) Commented May 19, 2016 at 8:23
  • Never heard of such issues. How and what is it failing ? The request or the parsing ? Commented May 19, 2016 at 8:24
  • It turned out not to be bugs in the JSON parser, but in generating JSON from Java Maps and Lists. The App was generating JSON in one place and parsing it in another place (yeah that doesn't make sense at face value, but there's a good reason) The bug is mentioned here: fupeg.blogspot.co.nz/2011/07/android-json-bug.html Commented May 22, 2016 at 21:46
  • Possible duplicate of changing or upgrading built-in org.json libraries, is possible and a good idea? Commented Nov 25, 2017 at 5:55

0

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.