Alexandr Shutko
Alexandr Shutko

Reputation: 1875

Android JSONObject api16/api22 copy from HashMap differs

I got a strange JSONObject behavior on api16 and api22 platforms when converting HashMap to JSONObject. So here is the test code:

HashMap<String, Object> hash1 = new HashMap<String, Object>() {{
    put("key1", "value1");
    put("key2", 2000);
}};

HashMap<String, Object> hash2 = new HashMap<String, Object>() {{
    put("key31", "value31");
    put("key32", 3200);
}};

hash1.put("hashKey", hash2);

JSONObject json = new JSONObject(hash1);
String jsonString = json.toString();

If I run this code on android 5.1 I got this string (it is good and correct):

{"key2":2000,"hashKey":{"key31":"value31","key32":3200},"key1":"value1"}

but when this code running on Android 4.1 it produces this:

{"hashKey":"{key31=value31, key32=3200}","key2":2000,"key1":"value1"}

Why this happens and is there any way to get JSONObject work correctly on all 4+ phones ?

Upvotes: 0

Views: 217

Answers (1)

Alexandr Shutko
Alexandr Shutko

Reputation: 1875

Ok. I fixed this. It was too expensive to migrate whole project to gson, so I borrowed some code from never JSONObject version and made a static copy method.

This is a static method:

public class JSONCopy extends JSONObject {

    public static JSONObject hashMap2JSONObject(Map copyFrom) throws JSONException {
        JSONObject newJson = new JSONObject();
        Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom;
        for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) {
            String key = (String) entry.getKey();
            if (key == null) {
                throw new NullPointerException("key == null");
            }
            newJson.put(key, wrap(entry.getValue()));
        }

        return newJson;
    }

    public static Object getJSONArray(Object o) throws JSONException {
        JSONArray newValue = new JSONArray();
        if (!o.getClass().isArray()) {
            throw new JSONException("Not a primitive array: " + o.getClass());
        }
        final int length = Array.getLength(o);
        for (int i = 0; i < length; ++i) {
            newValue.put(wrap(Array.get(o, i)));
        }

        return newValue;
    }

    public static Object wrap(Object o) {
        if (o == null) {
            return NULL;
        }
        if (o instanceof JSONArray || o instanceof JSONObject) {
            return o;
        }
        if (o.equals(NULL)) {
            return o;
        }
        try {
            if (o instanceof Collection) {
                return new JSONArray((Collection) o);
            } else if (o.getClass().isArray()) {
                return getJSONArray(o);
            }
            if (o instanceof Map) {
                return hashMap2JSONObject((Map) o);
            }
            if (o instanceof Boolean ||
                    o instanceof Byte ||
                    o instanceof Character ||
                    o instanceof Double ||
                    o instanceof Float ||
                    o instanceof Integer ||
                    o instanceof Long ||
                    o instanceof Short ||
                    o instanceof String) {
                return o;
            }
            if (o.getClass().getPackage().getName().startsWith("java.")) {
                return o.toString();
            }
        } catch (Exception ignored) {
        }
        return null;
    }
}

This is a test:

    public static void testCopy() {
        HashMap<String, Object> hash1 = new HashMap<String, Object>() {{
            put("key1", "value1");
            put("key2", 2000);
        }};

        HashMap<String, Object> hash2 = new HashMap<String, Object>() {{
            put("key31", "value31");
            put("key32", 3200);
        }};

        HashMap<String, Object> hash3 = new HashMap<String, Object>() {{
            put("key41", "value41");
            put("key42", 4200);
            put("key43", true);
        }};

        HashMap<String, Object> hash4 = new HashMap<String, Object>() {{
            put("key51", "value51");
            put("key52", 5200);
            put("key53", true);
        }};

        hash3.put("hashKey4", hash4);
        hash2.put("hashKey3", hash3);
        hash1.put("hashKey2", hash2);

        try {
            JSONObject json = hashMap2JSONObject(hash1);
            String jsonString = json.toString();
            Log.d("testCopy()", jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This is result (tested on 4.0, 4.1, 4.2, 5.1):

D/testCopy(): {"hashKey2":{"key31":"value31","key32":3200,
"hashKey3":{"key42":4200,"key41":"value41","key43":true,
"hashKey4":{"key51":"value51","key53":true,"key52":5200}}},
"key2":2000,"key1":"value1"}

Upvotes: 1

Related Questions