为什么我不能打开根节点并反序列化一个对象数组? [英] Why can't I unwrap the root node and deserialize an array of objects?

查看:108
本文介绍了为什么我不能打开根节点并反序列化一个对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我无法通过展开根节点来反序列化对象数组?

Why am I not able to deserialize an array of objects by unwrapping the root node?

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonRootName;
import org.junit.Assert;
import org.junit.Test;

public class RootNodeTest extends Assert {

    @JsonRootName("customers")
    public static class Customer {
        public String email;
    }

    @Test
    public void testUnwrapping() throws IOException {
        String json = "{\"customers\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
        List<Customer> customers = Arrays.asList(mapper.readValue(json, Customer[].class));
        System.out.println(customers);
    }
}

我一直在挖掘杰克逊的文档,这个我可以弄清楚,但在运行它时,我收到以下错误:

I've been digging through the Jackson documentation and this is what I could figure out but upon running it, I get the following error:

A org.codehaus.jackson.map.JsonMappingException has been caught, Root name 'customers' does not match expected ('Customer[]') for type [array type, component type: [simple type, class tests.RootNodeTest$Customer]] at [Source: java.io.StringReader@49921538; line: 1, column: 2]

我想在不创建包装类的情况下完成此操作。虽然这是一个示例,但我不想仅为展开根节点创建不必要的包装类。

I would like to accomplish this without creating a wrapper class. While this is an example, I don't want to create unnecessary wrapper classes only for unwrapping the root node.

推荐答案

创建一个ObjectReader以明确配置根名称:

Create an ObjectReader to configure the root name explicitly:

@Test
public void testUnwrapping() throws IOException {
    String json = "{\"customers\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";
    ObjectReader objectReader = mapper.reader(new TypeReference<List<Customer>>() {})
                                      .withRootName("customers");
    List<Customer> customers = objectReader.readValue(json);
    assertThat(customers, contains(customer("hello@world.com"), customer("john.doe@example.com")));
}

(顺便说一下这是Jackson 2.5,你有不同的版本吗?我有DeserializationFeature而不是DeserializationConfig.Feature)

(btw this is with Jackson 2.5, do you have a different version? I have DeserializationFeature rather than DeserializationConfig.Feature)

似乎通过以这种方式使用对象阅读器,您不需要全局配置展开根值功能,也不要使用 @JsonRootName 注释。

It seems that by using an object reader in this fashion, you don't need to globally configure the "unwrap root value" feature, nor use the @JsonRootName annotation.

另请注意,您可以直接申请列表< Customer> 而不是通过数组 - 给予 ObjectMapper.reader 的类型就像 ObjectMapper.readValue

Note also that you can directly request a List<Customer> rather than going through an array- the type given to ObjectMapper.reader works just like the second parameter to ObjectMapper.readValue

这篇关于为什么我不能打开根节点并反序列化一个对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆