New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
readlearncode opened this Issue Sep 16, 2017 · 0 comments

Comments

Projects
None yet
3 participants
@readlearncode

readlearncode commented Sep 16, 2017

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

@m0mus m0mus added the bug label Sep 17, 2017

@m0mus m0mus added this to the 1.0.1 milestone Sep 17, 2017

bravehorsie added a commit that referenced this issue Sep 18, 2017

Fixes #48, fixes #47
Serializers and deserializers for anonymous enum value classes are now properly resolved.
Serializer is now using value.name()
Deserializer is using Enum#valueOf()

Signed-off-by: Roman Grigoriadi <roman.grigoriadi@oracle.com>

@m0mus m0mus closed this in #50 Sep 25, 2017

m0mus added a commit that referenced this issue Sep 25, 2017

Fixes #48, fixes #47 (#50)
Serializers and deserializers for anonymous enum value classes are now properly resolved.
Serializer is now using value.name()
Deserializer is using Enum#valueOf()

Signed-off-by: Roman Grigoriadi <roman.grigoriadi@oracle.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment