java 在Jackson的单个文件中反序列化来自多个YAML文档的POJO

xjreopfe  于 2023-01-16  发布在  Java
关注(0)|答案(3)|浏览(163)

我有一个YAML文件,看起来像这样:

---
name: Sam
tags:
    -   Dev
    -   Java
----
name: Bob
tags:
    -   PM

我想使用Jackson来反序列化文件中的所有文档,但我不知道如何使用普通的ObjectMapper来实现这一点。如果我使用YAMLFactory来创建文件的解析器,我可以单步执行所有标记,因此解析器显然能够处理多个文档-但是如何将它们联系在一起呢?看起来我的YAMLFactory创建的解析器只从文件中解析出一个文档。
我还尝试过直接创建YAMLParser并使用ObjectMapper#readValue(JsonParser, Class),但是ObjectMapper会耗尽整个YAMLParser来反序列化单个示例。

qgelzfjb

qgelzfjb1#

这是多年以后的事了,但值得指出的是,这是受支持的。Jackson语义略有不同,可能是由于它的JSON起源。这可以通过使用ObjectMapper中的MappingIterator来实现。

YAMLFactory yamlFactory;
ObjectMapper mapper;

YAMLParser yamlParser = yamlFactory.createParser("file-with-multiple-docs.yaml")
List<ObjectNode> docs = mapper
      .readValues(yamlParser, new TypeReference<ObjectNode>(){})
      .readAll();

如果需要,用您自己的POJO替换ObjectNode

pgky5nke

pgky5nke2#

您可以直接使用SnakeYaml(JacksonYAML解析器正在内部使用它):

try (InputStream input = new FileInputStream(file)) {
    Yaml yaml = new Yaml(new SafeConstructor());
    yaml.loadAll(input).forEach( System.out::println );
} catch (Throwable e) {
    System.out.println("ERROR: " + e.getMessage());
}

将产生:

{name=Sam, tags=[Dev, Java]}
{name=Bob, tags=[PM]}
8dtrkrch

8dtrkrch3#

这似乎在这一点上不受支持。这里是YAMLParser源代码的链接。
如果输入的YAML包含多个文档,则Jackson失败。下面是一个示例:

public class JacksonYAML {
    public static final String YAML = "---\n" +
            "name: Sam\n" +
            "tags:\n" +
            "    -   Dev\n" +
            "    -   Java\n" +
            "----\n" +
            "name: Bob\n" +
            "tags:\n" +
            "    -   PM";

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        System.out.println(mapper.readValue(YAML, Object.class));
    }
}

异常错误指向输入源中的第二项:

Exception in thread "main" while scanning a simple key
 in 'reader', line 6, column 1:
    ----
    ^
could not found expected ':'
 in 'reader', line 7, column 1:
    name: Bob
    ^

    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.stalePossibleSimpleKeys(ScannerImpl.java:465)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.needMoreTokens(ScannerImpl.java:280)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:225)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl$ParseBlockSequenceEntry.produce(ParserImpl.java:502)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl.getEvent(ParserImpl.java:168)
    at com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken(YAMLParser.java:331)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapArray(UntypedObjectDeserializer.java:529)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:449)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:572)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:435)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)
    at stackoverflow.JacksonYAML.main(JacksonYAML.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

相关问题