I have access to an external API that can in some cases send JSON (a structure in the first class), and maybe in some cases not a valid object of this type - {}. I want to convert this invalid object to the first one, as in the class below, and use a custom deserialization method. But for some reason, my application does not fail if you put a debugging point there. Why is that? As an instruction, I used this example - https://gist.github.com/sghill/2395319
import com.example.MyDeserializer;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import java.util.List;
@JsonDeserialize(using = MyDeserializer.class)
public class Response {
public List<Error> errors;
public class Error{
public int status;
public String code;
public String title;
public String detail;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
public List<Error> getErrors() {
return errors;
}
public void setErrors(List<Error> errors) {
this.errors = errors;
}
}
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
public class ResponseDeserializer extends JsonDeserializer<Response> {
@Override
public Response deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
JsonNode node = jsonParser.readValueAsTree();
ObjectMapper mapper = new ObjectMapper();
if (node.asText().isEmpty()) {
return new Response();
}
return mapper.readValue(node, Response.class);
}
}