{
"name": "Sample item",
"required": "1"
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class SampleItem {
private String name;
@JsonDeserialize(using=NumericBooleanDeserializer.class) //Deserializer class created below
private boolean required; //This can be boolean. If there is no corresponding value in JSON, it will be FALSE.
}
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
/**
*Settings for JSON string conversion (Boolean).
* - 0 → false
* - 1 → true
*/
public static class NumericBooleanDeserializer extends JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return !"0".equals(parser.getText());
}
}
Recommended Posts