actolap / json-io

Automatically exported from code.google.com/p/json-io
0 stars 0 forks source link

json for enum includes private fields, causes error when parsing #21

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I have an enum like:

public enum AssetType {
    ABSTRACT(AssetEncoding.CONTENT_ENCODING), 
    AUTOCOMPLETE(AssetEncoding.CONTENT_ENCODING),
    ...
    private EnumSet<AssetEncoding> encoding;
    private AssetType(EnumSet<AssetEncoding> encoding) { 
        this.encoding = encoding;
    }
    ...
}

2. The emitted JSON for this enum includes the "encoding" value as follows:
   "x" : {
        "@id": 1548,
        "@type": "com.uptodate.web.api.AssetType",
        "encoding": {
            "@id": 60,
            "@type": "java.util.RegularEnumSet",
            "@items": [
                {
                    "@type": "com.uptodate.web.api.AssetEncoding",
                    "name": "COMPRESSED",
                    "ordinal": 0
                },
                {
                    "@type": "com.uptodate.web.api.AssetEncoding",
                    "name": "ENCRYPTED",
                    "ordinal": 1
                },
                {
                    "@type": "com.uptodate.web.api.AssetEncoding",
                    "name": "BASE64",
                    "ordinal": 2
                },
                {
                    "@type": "com.uptodate.web.api.AssetEncoding",
                    "name": "JSON",
                    "ordinal": 3
                }
            ]
        },
        "name": "TOPIC",
        "ordinal": 10
    },

3. This causes the following exception
    java.lang.ClassCastException: class com.uptodate.web.api.AssetEncoding != null
    at java.util.EnumSet.typeCheck(Unknown Source)
    at java.util.RegularEnumSet.add(Unknown Source)
    at java.util.RegularEnumSet.add(Unknown Source)
    at com.cedarsoftware.util.io.JsonReader.traverseCollection(JsonReader.java:1587)
    at com.cedarsoftware.util.io.JsonReader.convertMapsToObjects(JsonReader.java:1286)
    at com.cedarsoftware.util.io.JsonReader.traverseCollection(JsonReader.java:1585)
    at com.cedarsoftware.util.io.JsonReader.convertMapsToObjects(JsonReader.java:1286)
    at com.cedarsoftware.util.io.JsonReader.traverseCollection(JsonReader.java:1585)
    at com.cedarsoftware.util.io.JsonReader.convertMapsToObjects(JsonReader.java:1286)
    at com.cedarsoftware.util.io.JsonReader.convertParsedMapsToJava(JsonReader.java:1233)
    at com.cedarsoftware.util.io.JsonReader.readObject(JsonReader.java:1193)
    at com.cedarsoftware.util.io.JsonReader.jsonToJava(JsonReader.java:1085)

What is the expected output? What do you see instead?

I am surprised to see the emitted JSON contain the enum's "ordinal" value and 
the enum's private fields.  I would expect to see just the name:

   "x" : {
        "@id": 1548,
        "@type": "com.uptodate.web.api.AssetType",
        "name": "TOPIC",
    },

In fact, other JSON serializers (GSON for example) emits just the name:

   "x" : "TOPIC"

What version of the product are you using? On what operating system?

2.7.0 on Linux

Please provide any additional information below.

I can work around the problem by declaring my enum fields "transient".  That 
would not work for third-party software obviously, so I think the tool would be 
better if it handled this situation properly.

That's it.  I've had no other problems, it is a great library!  Thank you.

Original issue reported on code.google.com by jro...@uptodate.com on 2 Sep 2014 at 10:01