在反序列化Json时,如何在jackson中确定POJO的setter方法调用的顺序?

Anu*_*thi 5 java json jackson

我有一个像

{
    "key" : ["key1", "key2", "key3"],
    "value" : "v1";
}
Run Code Online (Sandbox Code Playgroud)

我正在Pojo使用杰克逊将其反序列化到我的班级,而反序列化时我希望变量的value类型List<String>取决于变量的大小key。这样最后一个对象将代表这个Json。

{
     "key" : ["key1", "key2", "key3"],
     "value" : ["v1", "v1", "v1"];
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的Pojo课是这样的

public class Pojo {

@JsonProperty("key")
private List<String> key;
@JsonProperty("value")
private List<String> value;

@JsonProperty("key")
public List<String> getKey() {
    return key;
}

@JsonProperty("key")
public void setKey(List<String> key) {
    this.key = key;
}

@JsonProperty("value")
public List<String> getValue() {
    return value;
}

@JsonProperty("value")
public void setValue(String val) {
    List<String> arr = new ArrayList<String>();
    for (int i=0; i<key.size(); i++) {
        arr.add(val);
    }
    this.value = arr;
}

}
Run Code Online (Sandbox Code Playgroud)

但是我得到了JsonMappingException。在调试时,我发现setValue方法变量内部keynull。有没有办法先设置变量的值key?(在变量之前value

Mic*_*ber 1

您应该在自定义反序列化器类中隐藏这种丑陋的转换。它可能看起来像这样:

class PojoJsonDeserializer extends JsonDeserializer<Pojo> {

    @Override
    public Pojo deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        InnerPojo innerPojo = jp.readValueAs(InnerPojo.class);

        return innerPojo.toPojo();
    }

    private static class InnerPojo {
        public List<String> key;
        public String value;

        Pojo toPojo() {
            Pojo pojo = new Pojo();
            pojo.setKey(new ArrayList<String>(key));
            pojo.setValue(valueNTimes(value, key.size()));

            return pojo;
        }

        private List<String> valueNTimes(String value, int nTimes) {
            List<String> result = new ArrayList<String>(nTimes);
            for (int index = 0; index < nTimes; index++) {
                result.add(value);
            }

            return result;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您的 POJO 类现在看起来“自然”:

@JsonDeserialize(using = PojoJsonDeserializer.class)
class Pojo {

    private List<String> key;
    private List<String> value;

    public List<String> getKey() {
        return key;
    }

    public void setKey(List<String> key) {
        this.key = key;
    }

    public List<String> getValue() {
        return value;
    }

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

    @Override
    public String toString() {
        return "Pojo [key=" + key + ", value=" + value + "]";
    }
}
Run Code Online (Sandbox Code Playgroud)

简单测试程序:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.readValue(json, Pojo.class));
    }
}
Run Code Online (Sandbox Code Playgroud)

印刷:

Pojo [key=[key1, key2, key3], value=[v1, v1, v1]]
Run Code Online (Sandbox Code Playgroud)