Skip to content
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

Add GSON annotation '@SerializedName' to Enums #267

Closed
dj-at-work opened this issue Nov 4, 2014 · 3 comments
Closed

Add GSON annotation '@SerializedName' to Enums #267

dj-at-work opened this issue Nov 4, 2014 · 3 comments
Milestone

Comments

@dj-at-work
Copy link

I've experienced a strange behavior of GSON when it deserialize a json string value to Enum. I have multiple Enums of the same kind in my schema. But one resists to be deserialized correctly.

Finally I found a solution. If I add @SerializedName to each value, deserialization works correct.

@Generated("org.jsonschema2pojo")
    public static enum Alignment {
        @SerializedName("left")
        LEFT("left"),
        @SerializedName("center")
        CENTER("center"),
        @SerializedName("right")
        RIGHT("right");
    // ...
    }

So far so good. My issue is, that I have to do this by hand, and can't find a way that it's done by jsonschema2pojo automatically.

@joelittlejohn joelittlejohn added this to the 0.4.7 milestone Nov 4, 2014
@joelittlejohn
Copy link
Owner

Thanks for reporting this. It's definitely the goal of this project to be able to serialize enums correctly no matter what the actual values are. I think this was missed when the Gson support was added. I'll look to include this in the next release.

@dj-at-work
Copy link
Author

You're welcome. It's seems not to be mentioned in the official Gson docs when they added the SerializedName annotation. But as someone mentioned at SO:
"Note: this does not work with older versions of GSON. It works with version 2.1 and greater".

@joelittlejohn
Copy link
Owner

Okay thanks.

Gson 2.1 was released Dec 31, 2011 (the party release!) so hopefully most people have upgraded. It certainly can't hurt to have the annotation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants