Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Enums while parsing JSON with GSON

Tags:

java

json

gson

This is related to a previous question that I asked here earlier

JSON parsing using Gson

I am trying to parse the same JSON, but now I have changed my classes a little bit.

{     "lower": 20,     "upper": 40,     "delimiter": " ",     "scope": ["${title}"] } 

My class now looks like:

public class TruncateElement {     private int lower;    private int upper;    private String delimiter;    private List<AttributeScope> scope;     // getters and setters }   public enum AttributeScope {      TITLE("${title}"),     DESCRIPTION("${description}"),      private String scope;      AttributeScope(String scope) {         this.scope = scope;     }      public String getScope() {         return this.scope;     } } 

This code throws an exception,

com.google.gson.JsonParseException: The JsonDeserializer EnumTypeAdapter failed to deserialized json object "${title}" given the type class com.amazon.seo.attribute.template.parse.data.AttributeScope at  

The exception is understandable, because as per the solution to my previous question, GSON is expecting the Enum objects to be actually be created as

${title}("${title}"), ${description}("${description}"); 

But since this is syntactically impossible, what are the recommended solutions, workarounds?

like image 823
Sachin Kulkarni Avatar asked Nov 21 '11 11:11

Sachin Kulkarni


People also ask

How does GSON serialize enums?

By default, Gson serializes enums by name in lowercase. If this is not sufficient and you want some other string or an integer value, then have a look at the @SerializedName annotation. Gson can serialize and deserialize enums using the @SerializedName annotation.

Can we pass Enum in JSON?

All you have to do is create a static method annotated with @JsonCreator in your enum. This should accept a parameter (the enum value) and return the corresponding enum. This method overrides the default mapping of Enum name to a json attribute .

How are enums represented in JSON?

So how will I represent Enum type in JSON data? An enum in any language represents a set of possible values but in an object it only takes 1 value. In JSON is the same, you can define a set of values in a JSON Schema, but then in JSON data it takes only one of those values.

Is GSON better than JSON?

Conclusion: 2021 The obvious elephant in the room is that java and the json libraries got faster. Like way faster then back when the first benchmarks were run. It is also obvious that GSON stepped up big and won both benchmarks for Big and small files. In both cases very clearly.


1 Answers

I want to expand a bit NAZIK/user2724653 answer (for my case). Here is a Java code:

public class Item {     @SerializedName("status")     private Status currentState = null;      // other fields, getters, setters, constructor and other code...      public enum Status {         @SerializedName("0")         BUY,         @SerializedName("1")         DOWNLOAD,         @SerializedName("2")         DOWNLOADING,         @SerializedName("3")         OPEN      } } 

in the json file you have just a field "status": "N",, where N=0,1,2,3 - depend on the Status values. So that's all, GSON works fine with the values for the nested enum class. In my case i've parsed a list of Items from json array:

List<Item> items = new Gson().<List<Item>>fromJson(json,                                           new TypeToken<List<Item>>(){}.getType()); 
like image 51
validcat Avatar answered Sep 29 '22 14:09

validcat