0

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);
    }
}
HtmlMan
  • 45
  • 2
  • 9
  • I don't quite understand the concern around why it doesn't fail. The `{}` is technically a valid (empty) object. It will still deserialize to an instance of `Response` and will have that instance's properties as null. – nmina Aug 16 '21 at 18:03
  • I'm not sure, but when we talk about an object in any programming language, whether it's JavaScript or Java, we usually mean at least one property. The {} entry has no properties and is doomed to fail serialization. Correct me if I'm wrong. – HtmlMan Aug 16 '21 at 19:51
  • You're right, conceptually, an object is composed of properties and behaviors. To be clearer, perhaps I should have mentioned: A `{}` is technically a valid JSON representation of an object. In which case, the deserializer will still be able to parse. – nmina Aug 16 '21 at 20:27
  • Person with the same problem - https://stackoverflow.com/a/63030594/10074763 – HtmlMan Aug 16 '21 at 21:27
  • Could you give us more info about an app and how it works? How do you want to use it? Is it `Spring` app? Do you use this model in controller? – Michał Ziober Aug 17 '21 at 09:45
  • This app work on Spring Framework v3 + Java 8. This model uses as an entity for response for outer API. Model Response work when I get usual JSON, but sometimes I may get not correct response `{}` and this broke my code. – HtmlMan Aug 17 '21 at 09:52
  • If you use `Spring` you need to use [FasterXML](https://github.com/FasterXML/jackson-databind) version of `Jackson`. `codehouse` is not supported anymore. In your case, you just need to replaces imports and use these from `FasterXML` libs – Michał Ziober Aug 17 '21 at 14:29

0 Answers0