
1. preface
inJavaIn order to avoid too many magic values during development, we used enumeration classes to encapsulate some static state code. However, returning the meaning of these enumerations correctly and comprehensively to the front end is not so smooth. We usually use theJacksonClass library serialization objects areJSONToday, let's talk about usingJacksonUniversal techniques for serialized enumerations.
2. universal enumeration normal form
In order to facilitate unified processing and standardize a unified style, it is recommended to specify a unified abstract interface, such as:
/**
* The interface Enumerator.
*/
public interface Enumerator {
/**
* Code integer.
*
* @return the integer
*/
Integer code();
/**
* Description string.
*
* @return the string
*/
String description();
}
Let's write an implementation to identify gender:
public enum GenderEnum implements Enumerator {
UNKNOWN(0, "Unknown"),
MALE(1, "Male"),
FEMALE(2, "Female");
private final Integer code;
private final String description;
GenderEnum(Integer code, String description) {
this.code = code;
this.description = description;
}
@Override
public Integer code() {
return code;
}
@Override
public String description() {
return description;
}
}
3. serialized enumeration
If we use it directlyJacksonSerializing an enumeration will only simply output the enumeratedString
Name:
@Resource
private ObjectMapper objectMapper;
@Test
void enumTest() {
try {
String s = objectMapper.writeValueAsString(GenderEnum.MALE);
//Output string MALE
System.out.println(s);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
We expect toGenderEnum.MALE
serialized as {"code":1,"description":"male"}
。we can askObjectMapper
Customize oneModule
To achieve this personalized need:
//Declare a simple Module object
SimpleModule module = new SimpleModule();
//Add a serializer to the Module
module.addSerializer(Enumerator.class, new JsonSerializer<Enumerator>() {
@Override
public void serialize(Enumerator value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
//Start writing object
gen.writeStartObject();
//Specify k v respectively code description
gen.writeNumberField("code",value.code());
gen.writeStringField("description",value.description());
//Explicit end operation
gen.writeEndObject();
}
});
//Register Module
objectMapper.registerModule(module);
Then executing it again will get the results we expect. However, this is not reasonable.
4. Automatic global configuration in Spring Boot
inSpring BootIn the application, we want to configure it globally.Spring BootAutomatic configuration of provides us with a personalized customizationObjectMapper
Of the possibilities, you just need to declare oneJackson2ObjectMapperBuilderCustomizer
and injectSpring IoC:
@Bean
public Jackson2ObjectMapperBuilderCustomizer enumCustomizer(){
return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.serializerByType(Enumerator.class, new JsonSerializer<Enumerator>() {
@Override
public void serialize(Enumerator value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("code",value.code());
gen.writeStringField("description",value.description());
gen.writeEndObject();
}
});
}
This achieves global configuration.
5. summary
Here we introduce how to customizeJacksonLibrary for the purpose of more friendly serialization of enumerations. In fact, not just enumerating, you can also implement other serialization, deserialization, and customization of time output formats.
Comments0