ClickSmile / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

A wrapper for converting A Java HashMap<Object, Object> containing Arrays to a correctly represented JSONObject #87

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The Arrays are not converted while initializing a new JSONObject from a HashMap.
Eg:
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("arg0", "test");
map.put("arg1", Arrays.asList(new String[]{"a", "b"}));
map.put("arg2", new String[]{"a", "b"});
map.put("arg3", new int[][][]{{{1}, {5}}, {{2}, {6}}});
Map<String, String[][]> m = new HashMap<String,String[][]>();
m.put("t0", new String[][]{{"a", "b"}, {"c", "d"}});
map.put("arg4", m);
Map<String, Map<String,String[][]>> m1 = new 
HashMap<String,Map<String,String[][]>>();
m1.put("d0", new HashMap<String, String[][]>(m));
map.put(5, m1);
System.out.println(new JSONObject(map));
/*
 * Output:
 * {"arg4":{"t0":[[Ljava.lang.String;@724356},"arg3":[[[I@1a2d3be,"arg2":[Ljava.lang.String;@fb765a,"arg1":["a","b"],"arg0":"test","5":{"d0":{"t0":[[Ljava.lang.String;@724356}}}
 */

Output with new Wrapper makeJSONObject(map);
/*
 * Output:
 * {"arg4":{"t0":[["a","b"],["c","d"]]},"arg3":[[[1],[5]],[[2],[6]]],"arg2":["a","b"],"arg1":["a","b"],"arg0":"test","5":{"d0":{"t0":[["a","b"],["c","d"]]}}}
 */

Function:
    private static Object makeJSONObject(Object data) {
        String cName = data.getClass().getName();

        if (data instanceof List) {
            JSONArray reply = new JSONArray();
            for (Object o : (List) data) {
                reply.add(makeJSONObject(o));
            }
            return reply;
        }
        else if(data instanceof HashMap) {
            JSONObject reply  = new JSONObject();
            Iterator it = ((Map<Object, Object>)data).entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                String key = String.valueOf(pairs.getKey());
                reply.put(key, makeJSONObject(pairs.getValue()));
                it.remove();
            }
            return reply;
        }
        else if (cName.equals("[Ljava.lang.String")) {
            JSONArray reply = new JSONArray();
            for (String s : (String[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[C")) {
            JSONArray reply = new JSONArray();
            for (char s : (char[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[F")) {
            JSONArray reply = new JSONArray();
            for (float s : (float[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[I")) {
            JSONArray reply = new JSONArray();
            for (int s : (int[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[J")) {
            JSONArray reply = new JSONArray();
            for (long s : (long[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[S")) {
            JSONArray reply = new JSONArray();
            for (short s : (short[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[Z")) {
            JSONArray reply = new JSONArray();
            for (boolean s : (boolean[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("[B")) {
            JSONArray reply = new JSONArray();
            for (byte s : (byte[]) data) {
                reply.add(s);
            }
            return reply;
        }
        else if (cName.equals("java.lang.String")
            || cName.equals("C") || cName.equals("F") || cName.equals("I")
            || cName.equals("J") || cName.equals("S") || cName.equals("Z")
            || cName.equals("B")) {
            return String.valueOf(data);
        }
        else {
            return makeJSONObject(Arrays.asList((Object[]) data));
        }
    }

Original issue reported on code.google.com by deepansh...@gmail.com on 9 Jul 2013 at 8:40

GoogleCodeExporter commented 8 years ago
This was fixed in r211, but I added an extra test in r215 to cover nested 
arrays.

Original comment by jon.cham...@gmail.com on 10 Aug 2013 at 4:03