反序列化具有嵌套数组的 Java 中的 YAML 文件。

huangapple go评论65阅读模式
英文:

Deserialisation of YAML file with nested arrays in java

问题

我正在尝试在Java中对Yaml文件进行反序列化,但由于它具有嵌套的数组格式,就像下面的示例中所示,所以我遇到了一些问题。

items.yml

ITEMS:
  - item1
  - item2
  - item3:
    - item4
    - item5
  - item6

我在这里使用的是Jackson进行反序列化,对于item2之前的部分都正常工作,但在反序列化item3时,它会抛出以下异常:

com.fasterxml.jackson.databind.JsonMappingException: 找不到适合的构造函数,用于类型 [简单类型,类 com.ymalParser.io.Item]:无法从JSON对象实例化(需要添加/启用类型信息吗?)

我对这种反序列化的事情还不太了解。有人可以帮我看看我漏掉了什么吗?

提前感谢您的帮助。

英文:

I'm trying to deserialise a Yaml file in Java but facing issues as it has nested array format in it like in below example.

items.yml

ITEMS:
  - item1
  - item2
  - item3:
    - item4
    - item5
  - item6

I'm using Jackson here to deserialise it and it is working fine till item2 but while deserialising item3 it is throwing below exception:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.ymalParser.io.Item]: can not instantiate from JSON object (need to add/enable type information?)

I'm new to this deserialisation thing.
Can anyone help me with what I'm missing here.

Thanks in advance..

答案1

得分: 0

你正在创建一个简单类型的数组(item1,item2,item6)。然而,item3 是一个复杂类型。

```yaml
ITEMS:
  - item1
  - item2
...

可以序列化为 List<String>

ITEMS:
  - item1
  - item2
  - item3:
    - item4
    - item5
  - item6

无法序列化为 List<String>,但可以序列化为 Map<String, List<Object>>

尝试这个示例:

ITEMS:
  - item1: []
  - item2: []
  - item3:
    - item4
    - item5
  - item6: []

第二次编辑:

public void test() {
	ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
	Resource resource = new ClassPathResource("test.yaml");
	InputStream input = resource.getInputStream();
	Map<String, List<Object>> list = mapper.readValue(input, new TypeReference<Map<String, List<Object>>>() {});
	mapper.writeValue(System.out, list);
}

输出:

---
ITEMS:
- "item1"
- "item2"
- item3:
  - "item4"
  - "item5"
- "item6"
英文:

You are creating an array of simple types (item1, item2, item6). However item3 is a complex type.

ITEMS:
  - item1
  - item2
...

Can be serialized in to List&lt;String&gt;

ITEMS:
  - item1
  - item2
  - item3:
    - item4
    - item5
  - item6

Can not be serialized in to List&lt;String&gt; but Map&lt;String, List&lt;Object&gt;.

Try this one:

ITEMS:
  - item1: []
  - item2: []
  - item3:
    - item4
    - item5
  - item6: []

SECOND EDIT:

public void test() {
	ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
	Resource resource = new ClassPathResource(&quot;test.yaml&quot;);
	InputStream input = resource.getInputStream();
	Map&lt;String, List&lt;Object&gt;&gt; list = mapper.readValue(input, new TypeReference&lt;Map&lt;String, List&lt;Object&gt;&gt;&gt;() {});
	mapper.writeValue(System.out, list);
}

Outputs:

---
ITEMS:
- &quot;item1&quot;
- &quot;item2&quot;
- item3:
  - &quot;item4&quot;
  - &quot;item5&quot;
- &quot;item6&quot;

答案2

得分: 0

我不会提供答案,只给一个小提示:看一下当你的数据转换为JSON后是什么样子:

{
   "ITEMS": [
      "item1",
      "item2",
      {
         "item3": [
            "item4",
            "item5"
         ]
      },
      "item6"
   ]
}

问题在于ITEMS数组的元素是不同类型的:item1、item2和item6是字符串,第三个元素是一个对象。从理论上讲,这是有效的YAML/JSON,但将其反序列化为Java对象会有问题。

英文:

I'm not going to provide an answer, just a small hint: see how your data looks like when converted to JSON:

{
   &quot;ITEMS&quot;: [
      &quot;item1&quot;,
      &quot;item2&quot;,
      {
         &quot;item3&quot;: [
            &quot;item4&quot;,
            &quot;item5&quot;
         ]
      },
      &quot;item6&quot;
   ]
}

The problem is that the elements of the ITEMS array are of different types: item1, item2 and item6 are strings, the third item is an object. In theory it's valid YAML/JSON, but deserializing it to a java object is problematic.

huangapple
  • 本文由 发表于 2020年9月30日 00:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64123829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定