Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Jackson to deserialize JSON string or object into a String field

Tags:

java

json

jackson

I am using Jackson 2 library and I am trying to read a JSON response, which looks like:

{ "value":"Hello" }

When value is empty, JSON response looks like:

{ "value":{} }

My model POJO class looks like this

public class Hello {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

The problem is that when response looks like {value:{}}, Jackson is trying to read an Object, but my model class field is a string, so it throws an Exception:

JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token. 

My question is how Jackson can successfully read JSONs who look like:

 {"value":"something"} 

and at the same time if response looks like this {"value":{}} (empty response for me), pass null to value field of my Hello model class.

I am using the code below in order to read JSON string:

String myJsonAsString = "{...}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(myJsonAsString , Hello.class);
like image 305
Konstantinos Raptis Avatar asked Mar 11 '17 20:03

Konstantinos Raptis


People also ask

Which methods does Jackson rely upon to deserialize a JSON formatted string?

The readValue() method is used to parse (deserialize) JSON from a String, Stream, or File into POJOs. On the other hand, the writeValue() method is used to turn POJOs into JSON (serialize).

What is Jackson deserialization?

Deserialization annotations are used when we deserialize JSON string into an Object. Jackson library provides several deserialization annotations such as @JsonCreator, @JacksonInject, @JsonAnySetter, etc. These annotations are mostly used in setter.

How do I convert JSON objects to Jackson?

Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.


1 Answers

You can use a custom deserializer for this feld. Here is one that returns the string if it's there, or null in any other case:

public class Hello {

    @JsonDeserialize(using = StupidValueDeserializer.class)
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public class StupidValueDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonToken jsonToken = p.getCurrentToken();
        if (jsonToken == JsonToken.VALUE_STRING) {
            return p.getValueAsString();
        }
        return null;
    }
}
like image 108
JB Nizet Avatar answered Oct 06 '22 00:10

JB Nizet