I have this simple enumeration which is generated by OpenAPI code generation using enumOuterClass.mustache.
@JsonbTypeSerializer(Address.Serializer.class)
@JsonbTypeDeserializer(Address.Deserializer.class)
public enum Address {
MAIN_ADDRESS("Main_Address"),
DELIVERY_ADDRESS("Delivery_Address");
private String value;
Address(String value) {
this.value = value;
}
public String value() {
return value;
}
@Override
public String toString() {
return value;
}
public static final class AddressDeserializer implements JsonbDeserializer<Address> {
@Override
public Address deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
String value;
try {
value = parser.getString();
} catch (java.lang.IllegalStateException ise) {
return null;
}
for (Address b : Address.values()) {
if (b.value().equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + parser.getString() + "'");
}
}
public static final class AddressSerializer implements JsonbSerializer<Address> {
@Override
public void serialize(Address obj, JsonGenerator generator, SerializationContext ctx) {
generator.write(obj.value);
}
}
}
Now I have written a simple test for serialization and deserialization of the enum values:
@Test
void addressEnumTest() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
for (Address a : Address.values()) {
String json = jsonb.toJson(a);
assertEquals(""" + a.value() + """, json);
Address result = jsonb.fromJson(json, Address.class);
assertEquals(a, result);
}
}
}
The serialization works perfectly while debugging and the breakpoints set are hit.
The deserialization is never called and my breakpoints there are not hit.
While continue the test I receive the following error:
jakarta.json.bind.JsonbException: Internal error: No enum constant Address.Main_Address.
If the value in JSON is written uppercase it works, but then my own deserializer is also never called.
How can I implement that my own deserializer is called?
I searched a lot on google but found only old stuff from 2020.
Some interesting links where
- https://github.com/eclipse-ee4j/yasson/issues/221
- JSON Binding @JsonbTypeDeserializer annotation ignored on enums?
Claudio Zesiger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4