5

假设我有以下 Java 类:

import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Demo {
    public int x;
    public int y;
    public List<Pair<Integer, Integer>> the_list;
}

我想从例如以下 json 格式填充它:

{ "x" : 1,  
  "y" : 2, 
  "the_list" : [[1,2],[3,4]]}

使用更快的xml

ObjectMapper mapper = new ObjectMapper();

我可能可以调用 mapper.readTree(json) 并填写我需要的所有内容。问题是我拥有的实际类(不是 Demo)包含很多参数,我想从数据绑定功能中受益。

尝试一个简单的:

mapper.readValue(json, Demo.class)

给出以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of     
org.apache.commons.lang3.tuple.Pair, problem: abstract types either need to be mapped to 
concrete types, have custom deserializer, or be instantiated with additional type   
information

有没有办法将自定义解析与数据绑定混合在一起?我查看了注释,但没有找到任何适合目的的东西(我无法让 mixins 与泛型一起使用,未调用 the_list 的自定义设置器可能是因为它是一个列表,JsonCreator 不是一个选项,因为我没有写对类...)。

4

1 回答 1

9

您应该为 Pair 类编写自定义序列化器/反序列化器。

这是一个例子:

public class JacksonPair {
    static final String JSON = "{ \"x\" : 1,  \n" +
            "  \"y\" : 2, \n" +
            "  \"the_list\" : [[1,2],[3,4]]}";

    static class Demo {
        public int x;
        public int y;
        public List<Pair<Integer, Integer>> the_list;

        @Override
        public String toString() {
            return "Demo{" +
                    "x=" + x +
                    ", y=" + y +
                    ", the_list=" + the_list +
                    '}';
        }
    }

    static class PairSerializer extends JsonSerializer<Pair> {

        @Override
        public void serialize(
                Pair pair,
                JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeStartArray(2);
            jsonGenerator.writeObject(pair.getLeft());
            jsonGenerator.writeObject(pair.getRight());
            jsonGenerator.writeEndArray();
        }
    }

    static class PairDeserializer extends JsonDeserializer<Pair> {

        @Override
        public Pair deserialize(
                JsonParser jsonParser,
                DeserializationContext deserializationContext) throws IOException {
            final Object[] array = jsonParser.readValueAs(Object[].class);
            return Pair.of(array[0], array[1]);
        }
    }

    public static void main(String[] args) throws IOException {
        final SimpleModule module = new SimpleModule();
        module.addSerializer(Pair.class, new PairSerializer());
        module.addDeserializer(Pair.class, new PairDeserializer());
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(module);
        final Demo demo = objectMapper.readValue(JSON, Demo.class);
        System.out.println("toString: " + demo);
        System.out.println("Input: " + JSON);
        System.out.println("Output: " + objectMapper.writeValueAsString(demo));
    }
}

输出:

toString: Demo{x=1, y=2, the_list=[(1,2), (3,4)]}
Input: { "x" : 1,  
  "y" : 2, 
  "the_list" : [[1,2],[3,4]]}
Output: {"x":1,"y":2,"the_list":[[1,2],[3,4]]}
于 2014-12-14T08:46:22.577 回答