--Convert values in Java Enum enums and JSON --Use the abstract classes JsonSerializer and JsonDeserializer provided by Jackson
package com.example.humansex;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
 * ISO 5218 (Code for notation of human gender)An enumeration type that represents.
 *Gender code:unknown=0,male=1,Female=2,Not applicable=9
 */
public enum HumanSex {
  /**
   *unknown=0,male=1,Female=2,Not applicable=9
   */
  NOT_KNOWN(0), MALE(1), FEMALE(2), NOT_APPLICABLE(9);
  private final int code;
  private HumanSex(int code) {
    this.code = code;
  }
  /**
   *Returns the gender code.
   */
  public int getCode() {
    return code;
  }
  /**
   *Returns a HumanSex enumeration constant that matches the gender code.
   *
   * @param code Gender code
   * @return HumanSex enumeration constant
   */
  public static HumanSex codeOf(int code) {
    for (HumanSex value : HumanSex.values()) {
      if (code == value.getCode()) {
        return value;
      }
    }
    return null; //Does not match
  }
  /**
   *A class that serializes a HumanSex object to JSON.
   */
  public static class Serializer extends JsonSerializer<HumanSex> {
    /**
     *Gender code the HumanSex object when generating JSON(Integer value)Convert to.
     */
    @Override
    public void serialize(HumanSex value, JsonGenerator generator, SerializerProvider serializers) throws IOException {
      generator.writeNumber(value.getCode());
    }
  }
  /**
   *A class that deserializes a HumanSex object from JSON.
   */
  public static class Deserializer extends JsonDeserializer<HumanSex> {
    /**
     *Gender code when parsing JSON(Integer value)To a HumanSex object.
     */
    @Override
    public HumanSex deserialize(JsonParser parser, DeserializationContext context) throws IOException {
      return HumanSex.codeOf(parser.getIntValue());
    }
  }
}
package com.example;
import com.example.humansex.HumanSex;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.ArrayList;
import java.util.List;
public class MyData {
  public List<Person> personList = new ArrayList<>();
  @Override
  public String toString() {
    StringBuilder buf = new StringBuilder();
    for (Person person : personList) {
      buf.append(person + System.lineSeparator());
    }
    return buf.toString();
  }
  public static class Person {
    public String name;
    //Specify the class to be used when JSON generation / analysis by Jackson with annotation
    @JsonSerialize(using = HumanSex.Serializer.class)
    @JsonDeserialize(using = HumanSex.Deserializer.class)
    public HumanSex sex;
    public Person() {
    }
    public Person(String name, HumanSex sex) {
      this.name = name;
      this.sex = sex;
    }
    @Override
    public String toString() {
      return "name=[" + name + "], sex=[" + sex.toString() + "]";
    }
  }
}
package com.example;
import com.example.humansex.HumanSex;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.StringWriter;
public class App {
  public static void main(String[] args) throws IOException {
    //Generate data object
    MyData inputData = new MyData();
    inputData.personList.add(new MyData.Person("Peko",   HumanSex.NOT_KNOWN));
    inputData.personList.add(new MyData.Person("Anubis", HumanSex.MALE));
    inputData.personList.add(new MyData.Person("Isis",   HumanSex.FEMALE));
    inputData.personList.add(new MyData.Person("Robot",  HumanSex.NOT_APPLICABLE));
    //Generate JSON string from data object in Jackson
    StringWriter out = new StringWriter();
    new ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(out, inputData);
    String json = out.toString();
    //Output JSON string
    System.out.println("***** Mydata → JSON *****");
    System.out.println(json);
    //Parse JSON string in Jackson and convert it to a data object
    MyData outputData = new ObjectMapper().readValue(json, MyData.class);
    //Output string representation of object
    System.out.println("***** JSON → Mydata *****");
    System.out.println(outputData);
  }
}
Prepare a configuration file build.gradle for building and running with Gradle.
build.gradle 
plugins {
  id 'java'
  id 'application'
}
group 'org.example'
version '0.0.1'
sourceCompatibility = 11
repositories {
  mavenCentral()
}
dependencies {
  implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.3'
}
application {
  mainClassName = 'com.example.App'
}
***** Mydata → JSON *****
{
  "personList" : [ {
    "name" : "Peko",
    "sex" : 0
  }, {
    "name" : "Anubis",
    "sex" : 1
  }, {
    "name" : "Isis",
    "sex" : 2
  }, {
    "name" : "Robot",
    "sex" : 9
  } ]
}
***** JSON → Mydata *****
name=[Peko], sex=[NOT_KNOWN]
name=[Anubis], sex=[MALE]
name=[Isis], sex=[FEMALE]
name=[Robot], sex=[NOT_APPLICABLE]
Recommended Posts