【问题标题】:How to parse into list of pairs from json with FasterXML如何使用 FasterXML 从 json 解析成对列表
【发布时间】:2014-12-12 15:08:35
【问题描述】:

假设我有以下 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 不是一个选项,因为我没有写Pair 类 ...)。

【问题讨论】:

    标签: java json jackson fasterxml


    【解决方案1】:

    您应该为 Pair 类编写一个自定义 serializer/deserializer

    这是一个例子:

    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]]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多