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

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

Deserialisation of YAML file with nested arrays in java

问题

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

items.yml

  1. ITEMS:
  2. - item1
  3. - item2
  4. - item3:
  5. - item4
  6. - item5
  7. - item6

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

  1. 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

  1. ITEMS:
  2. - item1
  3. - item2
  4. - item3:
  5. - item4
  6. - item5
  7. - 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:

  1. 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

  1. 你正在创建一个简单类型的数组(item1item2item6)。然而,item3 是一个复杂类型。
  2. ```yaml
  3. ITEMS:
  4. - item1
  5. - item2
  6. ...

可以序列化为 List<String>

  1. ITEMS:
  2. - item1
  3. - item2
  4. - item3:
  5. - item4
  6. - item5
  7. - item6

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

尝试这个示例:

  1. ITEMS:
  2. - item1: []
  3. - item2: []
  4. - item3:
  5. - item4
  6. - item5
  7. - item6: []

第二次编辑:

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

输出:

  1. ---
  2. ITEMS:
  3. - "item1"
  4. - "item2"
  5. - item3:
  6. - "item4"
  7. - "item5"
  8. - "item6"
英文:

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

  1. ITEMS:
  2. - item1
  3. - item2
  4. ...

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

  1. ITEMS:
  2. - item1
  3. - item2
  4. - item3:
  5. - item4
  6. - item5
  7. - item6

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

Try this one:

  1. ITEMS:
  2. - item1: []
  3. - item2: []
  4. - item3:
  5. - item4
  6. - item5
  7. - item6: []

SECOND EDIT:

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

Outputs:

  1. ---
  2. ITEMS:
  3. - &quot;item1&quot;
  4. - &quot;item2&quot;
  5. - item3:
  6. - &quot;item4&quot;
  7. - &quot;item5&quot;
  8. - &quot;item6&quot;

答案2

得分: 0

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

  1. {
  2. "ITEMS": [
  3. "item1",
  4. "item2",
  5. {
  6. "item3": [
  7. "item4",
  8. "item5"
  9. ]
  10. },
  11. "item6"
  12. ]
  13. }

问题在于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:

  1. {
  2. &quot;ITEMS&quot;: [
  3. &quot;item1&quot;,
  4. &quot;item2&quot;,
  5. {
  6. &quot;item3&quot;: [
  7. &quot;item4&quot;,
  8. &quot;item5&quot;
  9. ]
  10. },
  11. &quot;item6&quot;
  12. ]
  13. }

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:

确定