jansupol / jsonbapi

0 stars 0 forks source link

Serialising enum uses toString() method not name() as specified in section 3.9 of specification #55

Closed jansupol closed 6 years ago

jansupol commented 6 years ago

Given the enum:

public enum Binding {
    HARD_BACK {
        public String toString() {
            return "Hard Back";
        }
    }, SOFT_BACK {
        public String toString() {
            return "Soft Back";
        }
    }
}

JSON is:

{
  "binding": "Hard Back"
}

The binding value is the value returned by the overridden toString() method rather than the name() method. This is in contrast to my reading of the specification, which says:

3.9 Enum Serialization of an Enum instance to a JSON String value MUST follow the conversion process defined in javadoc specification for their name(). Deserialization of a JSON value into an enum instance MUST be done by calling the enum’s valueOf(String) method.

The name method returns the identifier rather than the toString() value.

Dependecies: org.eclipse/yasson: 1.0 javax.json.bind-api: 1.0 org.glassfish/javax.json: 1.1 javax.json/javax.json-api: 1.1

CODE: https://github.com/readlearncode/Java-EE-8-Sampler/blob/master/json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/EnumExample.java TEST: https://github.com/readlearncode/Java-EE-8-Sampler/blob/master/json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/EnumExampleTest.java

jansupol commented 6 years ago
jansupol commented 6 years ago

@m0mus Commented @readlearncode It looks like a bug in Yasson. Can I ask you to move it into Yasson issues tracker here: https://github.com/eclipse/yasson/issues

jansupol commented 6 years ago

@readlearncode Commented Moved to https://github.com/eclipse/yasson/issues/47