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?
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.
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 .
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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With