Assuming I have the following JSON:

{
  "property": "123:1234"
}

How do I use Jackson annotations to ensure that the string value of "property" is de-serialized to a self-defined class rather than a String object? I went through their documentation and I was unable to find this particular feature.

Thanks in advance.

2 Answers

0
michalk On Best Solutions

You could create custom deserializer for your field. Assuming you want to map it to SomeClass object :

public class SomeClass {

    @JsonDeserialize(using = CustomPropertyDeserializer.class)
    private Properties property;

    public Properties getProperty() {
        return property;
    }

    public void setProperty(Properties property) {
        this.property = property;
    }
}

You annotate your field that you want to deserialize customly with @JsonDeserialize annotation passing custom deserializer. Your deserializer could look like this :

public class CustomPropertyDeserializer extends StdDeserializer<Properties> {

    public CustomPropertyDeserializer() {
        super(Properties.class);
    }

    @Override
    public Properties deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String valueAsString = p.getValueAsString();
        String[] split = valueAsString.split(":");

        return new Properties(split[0], split[1]);
    }
}

And custom property class :

public class Properties {
    private String first;
    private String second;

    public Properties(String first, String second) {
        this.first = first;
        this.second = second;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }
}

For testing it :

    public static void main(String[] args) throws IOException {
        String s = Files.lines(Paths.get("src/main/resources/data.json")).collect(Collectors.joining());

        ObjectMapper objectMapper = new ObjectMapper();

        SomeClass someClass = objectMapper.readValue(s, SomeClass.class);

        System.out.println(someClass.getProperty().getFirst());
        System.out.println(someClass.getProperty().getSecond());

    }

The output is then :

123
1234

So all the custom logic how to map your String to some class that you define could be placed in deserialize method of your custom deserializer.

0
vc73 On

First thing first define your class that needs to be used:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonTest{
@JsonProperty("property")
private String property;

    //define your getters and setters for the field

Then you can use the ObjectMapper class from jackson:

  public static <T> T extractObjectFromJson(String jsonText, Class<T> type) {
    try {
        return new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).reader().forType(type)
                .readValue(jsonText);
    } catch (Exception e) {
        //Manage your exception here
    }
    return null;
}

So you can just call the method extractobjectFromJson(//Your JSON String, JsonTest.class) to get your JSON deserialized.