Consider the following enum and class:
public enum State {
OFF,
ON,
UNKNOWN
}
public class Machine {
String name;
int numCores;
State state;
public Machine(String name, int numCores, State state) {
this.name = name;
this.numCores = numCores;
this.state = state;
}
}
And consider the following main function:
public static void main(String args[]) {
Machine m = new Machine("Machine 1", 8, State.OFF);
ObjectMapper mapper = new ObjectMapper();
String machineAsJsonString = mapper.writeValueAsString(m);
System.out.println(machineAsJsonString);
}
Currently, the output of this main is:
{"name" : "Machine 1", "numCores" : 8, "state" : "OFF"}
This output is not good for me, as instead of the string "OFF"
for state
, I would like it to be 0
, which is the ordinal value of OFF
in the enum State
.
So the actual result I want to get is:
{"name" : "Machine 1", "numCores" : 8, "state" : 0}
Is there some elegant way to make it behave this way?
It should work by specifying JsonValue
mapper.
public enum State {
OFF,
ON,
UNKNOWN;
@JsonValue
public int toValue() {
return ordinal();
}
}
This works for deserialization also, as noted in Javadoc of @JsonValue
annotation:
NOTE: when use for Java enums, one additional feature is that value returned by annotated method is also considered to be the value to deserialize from, not just JSON String to serialize as. This is possible since set of Enum values is constant and it is possible to define mapping, but can not be done in general for POJO types; as such, this is not used for POJO deserialization
You can use setting
objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
See https://github.com/FasterXML/jackson-databind/blob/master/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java for complete test cases
Thanks to tip at https://righele.it/2016/01/30/jackson-deserialization-from-json-to-java-enums/
You can use in this way
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
public enum State {
OFF,
ON,
UNKNOWN
}
Yet another way:
public enum State {
@JsonProperty("0")
OFF,
@JsonProperty("1")
ON,
@JsonProperty("2")
UNKNOWN
}
However, this will produce {"state" : "1"}
instead of {"state" : 1}
(string, not numeric). In most cases it's OK
For completion I post another way: custom serializer:
public class StateSerializer extends JsonSerializer<State> {
public void serialize(State value, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
generator.writeStartObject();
generator.writeFieldName("id");
generator.writeNumber(value.getId());
generator.writeEndObject();
}
}
@JsonSerialize(using = StateSerializer.class)
public enum State {
...
public int getId(){...}
}
If you want to print the ordinal of the enum you can change your constructor to accept an int
instead of State
and then in your call to Machine
you can structure it in the following way:
Machine m = new Machine("Machine 1", 8, State.OFF.ordinal());
This will get the enum ordinal value of the passed in state and print the following
{name='Machine 1', numCores=8, state=0}
For integer enums, in kotlin, this works well. In java as well. If you don't want to use ordinals.
enum class State(@get:JsonValue val state: Int) {
OFF(0),
ON(1),
UNKNOWN(-1)
}
Byte code generated is:
@JsonValue
public final int getState() {
return this.state;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.