我使用JAVA 1.6和Jackson 1.9.9,我有一个enum

public enum Event {
    FORGOT_PASSWORD("forgot password");

    private final String value;

    private Event(final String description) {
        this.value = description;
    }

    @JsonValue
    final String value() {
        return this.value;
    }
}

我已经添加了一个@JsonValue,这似乎做的工作,它序列化对象:

{"event":"forgot password"}

但当我尝试反序列化时,我得到

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.globalrelay.gas.appsjson.authportal.Event from String value 'forgot password': value not one of declared Enum instance names

我错过了什么?


当前回答

我一直在寻找枚举序列化的解决方案,我终于做出了一个解决方案。

https://github.com/sirgilligan/EnumerationSerialization

https://digerati-illuminatus.blogspot.com/2022/10/java-enum-generic-serializer-and.html

它使用了一个新的注释和两个新类EnumerationSerializer和EnumerationDeserializer。您可以子类化EnumerationDeserializer,并创建一个设置枚举class的类(典型方法),或者您可以注释枚举,并且不必拥有EnumerationDeserializer的子类。

@JsonSerialize(using = EnumerationSerializer.class)
@JsonDeserialize(using = EnumerationDeserializer.class)
@EnumJson(serializeProjection = Projection.NAME, deserializationClass = RGB.class)
enum RGB {
    RED,
    GREEN,
    BLUE
}

注意ContextualDeserializer的实现如何从注释中提取类。

https://github.com/sirgilligan/EnumerationSerialization/blob/main/src/main/java/org/example/EnumerationDeserializer.java

这里有很多很好的代码,可以提供一些见解。

对于你的具体问题,你可以这样做:

@JsonSerialize(using = EnumerationSerializer.class)
@JsonDeserialize(using = EnumerationDeserializer.class)
@EnumJson(serializeProjection = Projection.NAME, deserializationClass = Event.class)
public enum Event {
    FORGOT_PASSWORD("forgot password");

    //This annotation is optional because the code looks for value or alias.
    @EnumJson(serializeProjection = Projection.VALUE)
    private final String value;

    private Event(final String description) {
        this.value = description;
    }

}

或者你可以这样做:

@JsonSerialize(using = EnumerationSerializer.class)
@JsonDeserialize(using = EnumerationDeserializer.class)
@EnumJson(serializeProjection = Projection.NAME, deserializationClass = Event.class)
public enum Event {
    FORGOT_PASSWORD("forgot password");

    private final String value;

    private Event(final String description) {
        this.value = description;
    }

}

这就是你要做的。

然后,如果您有一个“有”事件的类,您可以注释每个事件以您想要的方式序列化。

class EventHolder {
    @EnumJson(serializeProjection = Projection.NAME)
    Event someEvent;

    @EnumJson(serializeProjection = Projection.ORDINAL)
    Event someOtherEvent;

    @EnumJson(serializeProjection = Projection.VALUE)
    Event yetAnotherEvent;
}

其他回答

您可以自定义任何属性的反序列化。

使用将要处理的属性的annotationJsonDeserialize (import com.fasterxml.jackson.databin .annotation. jsondeserialize)声明反序列化类。如果这是一个Enum:

@JsonDeserialize(using = MyEnumDeserialize.class)
private MyEnum myEnum;

通过这种方式,您的类将被用于反序列化属性。这是一个完整的例子:

public class MyEnumDeserialize extends JsonDeserializer<MyEnum> {

    @Override
    public MyEnum deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        MyEnum type = null;
        try{
            if(node.get("attr") != null){
                type = MyEnum.get(Long.parseLong(node.get("attr").asText()));
                if (type != null) {
                    return type;
                }
            }
        }catch(Exception e){
            type = null;
        }
        return type;
    }
}

我发现最简单的方法是使用@JsonFormat.Shape。枚举的OBJECT注释。

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum MyEnum{
    ....
}

如果您希望将枚举类与其JSON表示完全解耦,@xbakesx指出的序列化器/反序列化器解决方案是一个很好的解决方案。

或者,如果您更喜欢自包含的解决方案,那么基于@JsonCreator和@JsonValue注释的实现将更方便。

因此,利用@Stanley的例子,下面是一个完整的自包含解决方案(Java 6, Jackson 1.9):

public enum DeviceScheduleFormat {

    Weekday,
    EvenOdd,
    Interval;

    private static Map<String, DeviceScheduleFormat> namesMap = new HashMap<String, DeviceScheduleFormat>(3);

    static {
        namesMap.put("weekday", Weekday);
        namesMap.put("even-odd", EvenOdd);
        namesMap.put("interval", Interval);
    }

    @JsonCreator
    public static DeviceScheduleFormat forValue(String value) {
        return namesMap.get(StringUtils.lowerCase(value));
    }

    @JsonValue
    public String toValue() {
        for (Entry<String, DeviceScheduleFormat> entry : namesMap.entrySet()) {
            if (entry.getValue() == this)
                return entry.getKey();
        }

        return null; // or fail
    }
}

可以采用多种方法将JSON对象反序列化为枚举。我最喜欢的风格是创建一个内部类:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.fasterxml.jackson.annotation.JsonFormat.Shape.OBJECT;

@JsonFormat(shape = OBJECT)
public enum FinancialAccountSubAccountType {
  MAIN("Main"),
  MAIN_DISCOUNT("Main Discount");

  private final static Map<String, FinancialAccountSubAccountType> ENUM_NAME_MAP;
  static {
    ENUM_NAME_MAP = Arrays.stream(FinancialAccountSubAccountType.values())
      .collect(Collectors.toMap(
        Enum::name,
        Function.identity()));
  }

  private final String displayName;

  FinancialAccountSubAccountType(String displayName) {
    this.displayName = displayName;
  }

  @JsonCreator
  public static FinancialAccountSubAccountType fromJson(Request request) {
    return ENUM_NAME_MAP.get(request.getCode());
  }

  @JsonProperty("name")
  public String getDisplayName() {
    return displayName;
  }

  private static class Request {
    @NotEmpty(message = "Financial account sub-account type code is required")
    private final String code;
    private final String displayName;

    @JsonCreator
    private Request(@JsonProperty("code") String code,
                    @JsonProperty("name") String displayName) {
      this.code = code;
      this.displayName = displayName;
    }

    public String getCode() {
      return code;
    }

    @JsonProperty("name")
    public String getDisplayName() {
      return displayName;
    }
  }
}

这里,'value'充当反序列化器,'namespace'充当序列化器。因此,你可以在保存时将值“Student Absent”传递给API,在DB中它将被保存为“STUDENT_ABSENT”。另一方面,在检索类中的数据时,API将返回“Student Absent”。

import com.fasterxml.jackson.annotation.JsonProperty;
public enum AttendanceEnums {
    STUDENT_PRESENT,
    @JsonProperty(value = "Student Absent", namespace = "Student Absent")
    STUDENT_ABSENT;
}