hushaojie04 / libgdx

Automatically exported from code.google.com/p/libgdx
0 stars 0 forks source link

Json serializes java.util.Map<> in an invalid way #1097

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is the test:

public class JsonTestMain {

    public static class MyClassWithMap {

        Map<String, Integer> myMap = new HashMap<String, Integer>();

        public MyClassWithMap() {
            myMap.put("pipote", 56);
        }

    }

    public static class MyClassWithList {

        List<Integer> myList = new ArrayList<Integer>();

        public MyClassWithList() {
            myList.add(56);
        }

    }

    public static void main(String[] args) {
        mapToJson();
        listToJson();
    }

    private static void mapToJson() {
        Json json = new Json();
        json.setUsePrototypes(false);
        String jsonValue = json.toJson(new MyClassWithMap());
        System.out.println(jsonValue);
    }

    private static void listToJson() {
        Json json = new Json();
        json.setUsePrototypes(false);
        String jsonValue = json.toJson(new MyClassWithList());
        System.out.println(jsonValue);
    }

}

The output is:

{myMap:{class:java.util.HashMap,pipote:56}}
{myList:[56]}

As you can see, the list is correctly serialized but for the map, it is 
exporting the class, which is not part of the JSON protocol if I am not 
mistaken. Tried to load that from other JSON parsers and it fails.

expected output:

{"myMap":{"pipote":56}}

Original issue reported on code.google.com by ariel.co...@gmail.com on 22 Oct 2012 at 1:46

GoogleCodeExporter commented 9 years ago
Most other JSON parsers want quoted keys...

{"myMap":{"class":"java.util.HashMap","pipote":56}}

Is the valid JSON that any parser would understand.

The Json class includes the class name so that it knows what type of Map to 
instantiate while deserializing. It doesn't write the class name for the list 
because it only knows ArrayList.

Or did I misunderstand?

Original comment by email.ne...@gmail.com on 22 Oct 2012 at 3:09

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I just happened to bump into this "bug" tonight myself. It is solvable by 
instantiating json by: new Json(OutputType.json). From the docs:
"/** Like JSON, but names and values are only quoted if necessary. */ 
minimal;", which is the default behavior. I think that the default behavior 
should be regular json, and that users may specify minimal if they really need 
it.

Original comment by kjetil...@gmail.com on 23 Oct 2012 at 11:15

GoogleCodeExporter commented 9 years ago
Well, it seems there is a way to export normal JSON by using 
        json.setOutputType(OutputType.json), but it is not enable by default, I 
believe the default output should be normal JSON and the others should be 
optional.

Also, is there a way to not export the Map class as one entry of the map?, Just 
to know, maybe you want to load from a json exported by other parser or auto 
generated by a server where you don't have libgdx as the json exporter. Even 
though, I know it is useful at loading time to determine the class of the map.

Original comment by ariel.co...@gmail.com on 23 Oct 2012 at 11:58

GoogleCodeExporter commented 9 years ago
Well, it seems using setTypeName(null); you avoid exporting the data, so in the 
end you could reach the normal behavior, still don't like it is not default 
behavior but closing the issue as invalid.

Original comment by ariel.co...@gmail.com on 25 Oct 2012 at 3:12

GoogleCodeExporter commented 9 years ago
Hello guys,

    May I ask how do I force the parser to ALWAYS use double quote on keys? We have a server that accepts the following format:

{"key":"mystring", "key": 4}

Thank you
~Zek

Original comment by ezekielb...@gmail.com on 8 Oct 2013 at 5:11