英文:
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<String>
ITEMS:
- item1
- item2
- item3:
- item4
- item5
- item6
Can not be serialized in to List<String>
but Map<String, List<Object>
.
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("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);
}
Outputs:
---
ITEMS:
- "item1"
- "item2"
- item3:
- "item4"
- "item5"
- "item6"
答案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:
{
"ITEMS": [
"item1",
"item2",
{
"item3": [
"item4",
"item5"
]
},
"item6"
]
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论