如何将数组反序列化为对象?

时间:2015-11-28 17:23:35

标签: java json jackson

我想要投射的对象看起来像

public class AdDimension {
    private int width;
    private int height;

    public AdDimension() {
        // needed by Jackson
    }

    public AdDimension(final int width, final int height) {
        this.width = width;
        this.height = height;
    }
..
}

并在

中使用
public class Campaign {
    private String id;
    private List<AdDimension> adDimensions;

    public Campaign() {
        // needed by Jackson
    }
 ...
}

我尝试在测试中反序列化

@Test
public void testLoadCampaigns() throws IOException {
    final File campaignsFile = new File(getClass().getClassLoader().getResource("campaigns.json").getFile());
    final List<Campaign> campaigns = new JsonReader().loadCampaigns(campaignsFile);
    System.out.println(campaigns);
}

我的广告系列文件看起来像

[
  {
    "id": 1,
    "targetedCountries": ["CA", "IT"],
    "targetedDomain": "apple.com",
    "adDimensions": [[300,250], [600,200]]
  }
]

当我进行测试时,我看到了

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.org.site.messages.AdDimension out of START_ARRAY token
 at [Source: /Users/harit/IdeaProjects/site/target/test-classes/campaigns.json; line: 6, column: 22] (through reference chain: java.util.ArrayList[0]->com.org.scout.messages.Campaign["adDimensions"]->java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:853)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1257)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:157)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2639)
    at com.org.harness.JsonReader.loadCampaigns(JsonReader.java:15)
    at com.org.harness.JsonReaderTest.testLoadCampaigns(JsonReaderTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error: 
  testLoadCampaigns(com.org.harness.JsonReaderTest): Can not deserialize instance of com.org.site.messages.AdDimension out of START_ARRAY token(..)

如何将[[300,250], [600,200]]读入List<AdDimension>

2 个答案:

答案 0 :(得分:3)

默认情况下,Jackson会尝试将JSON数组值反序列化为Java数组类型或Collection类型(例如ArrayList)。但是您试图将[600,200]之类的JSON数组反序列化为AdDimension对象。这不会开箱即用。

您可以像建议的其他答案一样更改您的JSON,或者使用自定义反序列化策略。

您的第一个选择是定义自定义JsonDeserializer(尽管您应该使用其sublcass StdDeserializer

class AdDimensionDeserializer extends JsonDeserializer<AdDimension> {
    @Override
    public AdDimension deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        // you have complete control over the tokens in the JSON, so validate as you wish
        ArrayNode arrayNode = parser.readValueAsTree();
        return new AdDimension(arrayNode.get(0).asInt(), arrayNode.get(1).asInt());
    }
}

并将其注册为

@JsonDeserialize(using = AdDimensionDeserializer.class)
class AdDimension {

Jackson将实例化AdDimensionDeserializer并使用该实例将JSON数组元素反序列化为AdDimension的实例。

另一种选择是使用@JsonCreater

  

标记注释,可用于定义构造函数和工厂   方法作为一个用于实例化新实例的方法   相关课程。

例如

@JsonCreator
public AdDimension(int[] dimensions) {
    // again, validate as required
    this.width = dimensions[0];
    this.height = dimensions[1];
}

杰克逊会找到这个构造函数并用它来反序列化你的JSON。它首先会尝试将文本[300,250]反序列化为int[],它显然可以,然后将其传递给您的构造函数。

答案 1 :(得分:1)

也许是一个糟糕的json格式?

我认为应该是这样的

[
  {
    "id": 1,
    "targetedCountries": ["CA", "IT"],
    "targetedDomain": "apple.com",
    "adDimensions": [{"width":300, "height":250}, {"width":600, "height":200}]
  }
]

我现在无法测试,但似乎不行。