项目作者: poblish

项目描述 :
Demonstrates Jackson ser/deserialization of enums
高级语言: Java
项目地址: git://github.com/poblish/JacksonSerializedEnumsDemo.git
创建时间: 2017-02-02T22:10:26Z
项目社区:https://github.com/poblish/JacksonSerializedEnumsDemo

开源协议:Apache License 2.0

下载


Jackson Serialized Enums

This project demonstrates the seamless conversion of Java enums to and from custom String representations, with the minimum of extra configuration, using Jackson 2.x.


Our code maintains the following three statuses:

  1. Status.UNKNOWN, Status.ACTIVE, Status.CANCELLED_EXPIRED

And we need to synchronise with a JSON-consuming REST service that accepts only the following:

  1. "???", "Active state", "CANCELLED_EXPIRED"

This demonstration shows that no additional logic is required, and that the mapping of enum <-> String can be achieved entirely using @JsonProperty("<value>") mappings:

  1. public enum Status {
  2. @JsonProperty("???") UNKNOWN,
  3. @JsonProperty("Active state") ACTIVE,
  4. CANCELLED_EXPIRED
  5. }

No custom setters or getters, and no special constructors, are required.

Type-safety is maintained: you don’t have to replace your enum type with a String inside your DTO, and any attempt to deserialise an invalid String value will fail, rather than being defaulted etc.