Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/Gson.html" rel="noreferrer">the documentation for Gson</a>:</p> <blockquote> <p>Gson provides default serialization and deserialization for Enums... If you would prefer to change the default representation, you can do so by registering a type adapter through GsonBuilder.registerTypeAdapter(Type, Object).</p> </blockquote> <p>Following is one such approach.</p> <pre><code>import java.io.FileReader; import java.lang.reflect.Type; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; public class GsonFoo { public static void main(String[] args) throws Exception { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(AttributeScope.class, new AttributeScopeDeserializer()); Gson gson = gsonBuilder.create(); TruncateElement element = gson.fromJson(new FileReader("input.json"), TruncateElement.class); System.out.println(element.lower); System.out.println(element.upper); System.out.println(element.delimiter); System.out.println(element.scope.get(0)); } } class AttributeScopeDeserializer implements JsonDeserializer&lt;AttributeScope&gt; { @Override public AttributeScope deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { AttributeScope[] scopes = AttributeScope.values(); for (AttributeScope scope : scopes) { if (scope.scope.equals(json.getAsString())) return scope; } return null; } } class TruncateElement { int lower; int upper; String delimiter; List&lt;AttributeScope&gt; scope; } enum AttributeScope { TITLE("${title}"), DESCRIPTION("${description}"); String scope; AttributeScope(String scope) { this.scope = scope; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload