Why can't I unwrap the root node and deserialize an array of objects?

Mridang Agarwalla

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\":\"[email protected]\"},{\"email\":\"[email protected]\"}]}";
        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.

araqnid

Create an ObjectReader to configure the root name explicitly:

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

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

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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I can't deserialize json array

From Dev

Why can't I delete this file as root?

From Dev

Why can't I array_combine an array with keys with an array of objects?

From Dev

Can't deserialize json array

From Dev

GSON can't deserialize an array

From Dev

Why can't I pass methods as objects?

From Dev

Why can't I change objects in a vector?

From Dev

Why can't I dereference pointer to objects?

From Dev

Why can't I pass methods as objects?

From Dev

Why do I ever need to point the Array constructor from another source and why can't I call toString() directly on array objects?

From Dev

I can't seem to post an array of objects

From Dev

Trouble to understand why I can't have an array of objects with only unique object

From Dev

I can't modify an array in Node

From Dev

I can't modify an array in Node

From Dev

Why does it return nil when I unwrap the array of strings in localNotification?

From Dev

How can I deserialize an array of JSON objects using JSON.NET?

From Dev

Why can't I open the "root" folder in Xubuntu?

From Dev

Why I can't erase a hidden file with root privileges?

From Dev

How can I unwrap an array from JSON file with python?

From Dev

Can't send an array of Javascript objects in node.js

From Dev

Can't send an array of Javascript objects in node.js

From Dev

Why can' t I Convert SparseArray to Array?

From Dev

Why can't I iterate over this array?

From Dev

why can't I initialize the array like this?

From Dev

Why can't I iterate over this array?

From Dev

why I can't accessing this array elements

From Dev

Why can't I append to array?

From Dev

Why can't I increment an array?

From Dev

Why I can't sort this array?

Related Related

HotTag

Archive