在使用GSON解析JSON时使用Enums

Sac*_*rni 102 java json gson

这与我之前在此问过的上一个问题有关

使用Gson进行JSON解析

我试图解析相同的JSON,但现在我已经改变了我的类.

{
    "lower": 20,
    "upper": 40,
    "delimiter": " ",
    "scope": ["${title}"]
}
Run Code Online (Sandbox Code Playgroud)

我的课现在看起来像:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码抛出异常,

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 
Run Code Online (Sandbox Code Playgroud)

异常是可以理解的,因为根据我之前的问题的解决方案,GSON期望实际上将Enum对象创建为

${title}("${title}"),
${description}("${description}");
Run Code Online (Sandbox Code Playgroud)

但由于这在语法上是不可能的,推荐的解决方案是什么,解决方法呢?

val*_*cat 285

我想扩展一点NAZIK/user2724653答案(对我而言).这是一个Java代码:

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
     }
}
Run Code Online (Sandbox Code Playgroud)

在json文件中,您只有一个字段"status": "N",,其中N = 0,1,2,3 - 取决于Status值.所以这一切,GSON适用于嵌套enum类的值.在我的情况我已经分析的列表Items,从json数组:

List<Item> items = new Gson().<List<Item>>fromJson(json,
                                          new TypeToken<List<Item>>(){}.getType());
Run Code Online (Sandbox Code Playgroud)

  • 这个答案完美地解决了一切,不需要类型适配器! (23认同)
  • 如果状态为5的json将到达,会发生什么?有没有办法定义默认值? (15认同)
  • @DmitryBorodin如果来自JSON的值与任何`SerializedName`都不匹配,那么枚举将默认为'null`.可以在包装类中处理未知状态的默认行为.但是,如果您需要"null"以外的"unknown"表示,那么您将需要编写自定义反序列化器或类型适配器. (6认同)
  • 当我这样做时,使用 Retrofit/Gson,枚举值的 SerializedName 添加了额外的引号。例如,服务器实际上接收的是 ```"1"```,而不是简单的 ```1```... (4认同)

Pro*_*uce 51

Gson的文档:

Gson为Enums提供默认序列化和反序列化...如果您希望更改默认表示,可以通过GsonBuilder.registerTypeAdapter(Type,Object)注册类型适配器来实现.

以下是一种这样的方法.

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<AttributeScope>
{
  @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<AttributeScope> scope;
}

enum AttributeScope
{
  TITLE("${title}"), DESCRIPTION("${description}");

  String scope;

  AttributeScope(String scope)
  {
    this.scope = scope;
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 31

使用注释@SerializedName:

@SerializedName("${title}")
TITLE,
@SerializedName("${description}")
DESCRIPTION
Run Code Online (Sandbox Code Playgroud)


use*_*995 9

使用GSON 2.2.2版枚举将很容易编组和解组.

import com.google.gson.annotations.SerializedName;

enum AttributeScope
{
  @SerializedName("${title}")
  TITLE("${title}"),

  @SerializedName("${description}")
  DESCRIPTION("${description}");

  private String scope;

  AttributeScope(String scope)
  {
    this.scope = scope;
  }

  public String getScope() {
    return scope;
  }
}
Run Code Online (Sandbox Code Playgroud)


Wou*_*out 7

以下代码片段消除了Gson.registerTypeAdapter(...)使用@JsonAdapter(class)自Gson 2.3以来可用的注释对explicit的需要(请参阅注释pm_labs)。

@JsonAdapter(Level.Serializer.class)
public enum Level {
    WTF(0),
    ERROR(1),
    WARNING(2),
    INFO(3),
    DEBUG(4),
    VERBOSE(5);

    int levelCode;

    Level(int levelCode) {
        this.levelCode = levelCode;
    }

    static Level getLevelByCode(int levelCode) {
        for (Level level : values())
            if (level.levelCode == levelCode) return level;
        return INFO;
    }

    static class Serializer implements JsonSerializer<Level>, JsonDeserializer<Level> {
        @Override
        public JsonElement serialize(Level src, Type typeOfSrc, JsonSerializationContext context) {
            return context.serialize(src.levelCode);
        }

        @Override
        public Level deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
            try {
                return getLevelByCode(json.getAsNumber().intValue());
            } catch (JsonParseException e) {
                return INFO;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 小心地将序列化器/反序列化器类添加到您的proguard配置中,因为它们可能会被删除(对我而言这是发生了的) (2认同)