Jackson json property (string) to instance

久未见 提交于 2021-01-29 13:30:33

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/56171539/jackson-json-property-string-to-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!