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

前端 未结 3 1375
野的像风
野的像风 2021-01-07 15:20

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         


        
相关标签:
3条回答
  • 2021-01-07 15:34

    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")));
    }
    

    (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

    0 讨论(0)
  • 2021-01-07 15:55

    This code worked for me:

    import org.codehaus.jackson.map.ObjectMapper;
    import org.junit.Assert;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.util.List;
    
    public class RootNodeTest extends Assert {
    
    public static class CustomerMapping {
        public List<Customer> customer;
    
        public List<Customer> getCustomer() {
            return customer;
        }
    
        public static class Customer {
            public String email;
    
            public String getEmail() {
                return email;
            }
        }
    
    }
    
    @Test
    public void testUnwrapping() throws IOException {
        String json = "{\"customer\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";
        ObjectMapper mapper = new ObjectMapper();
        CustomerMapping customerMapping = mapper.readValue(json, CustomerMapping.class);
        List<CustomerMapping.Customer> customers = customerMapping.getCustomer();
        for (CustomerMapping.Customer customer : customers) {
            System.out.println(customer.getEmail());
        }
      }
    }
    

    First of all you need a java object for the whole json object. In my case this is CustomerMapping. Then you need a java object for your customer key. In my case this is the inner class CustomerMapping.Customer. Because customer is a json array you need a list of CustomerMapping.Customer objects. Also, you do not need to map the json array to a java array and convert it then to a list. Jackson already does it for you. Finally, you just specify the variable email of type String and print it to the console.

    0 讨论(0)
  • 2021-01-07 15:58

    it seems you can't escape a wrapper class. according to this, the @JsonRootName annotation will only allow you to unwrap a json that contains a single instance of your pojo: so it will work for a String like this: "{\"customer\":{\"email\":\"hello@world.com\"}}";

    0 讨论(0)
提交回复
热议问题