我想解析一个YAML文件到一个对象。YAML杰克逊 - 阵列锚键
即使在线YAML解析器告诉我,它可以解析我想要的方式,杰克逊YAML解析器拒绝给我我想要的东西。
这里是YAML文件:
- nom: "service1"
etats : &e1s1
- nom: "e1"
childs:
- nom: "e2"
childs:
- nom: "e3"
childs:
- &a
nom: "e5"
- nom: "e4"
childs:
- <<: *a
在线YAML解析器告诉我, “E4” 和 “E3” 有 “E5” 作为一个孩子。
然而,当我尝试与杰克逊解析这个,我得到以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "<<" (class Etat), not marked as ignorable (4 known properties: "dependsOnAnotherService", "nom", "hasToken", "childs"])
at [Source: (File); line: 13, column: 21] (through reference chain: java.lang.Object[][0]->Service["etats"]->java.util.ArrayList[0]->Etat["childs"]->java.util.ArrayList[1]->Etat["childs"]->java.util.ArrayList[0]->Etat["<<"])
所以,我想知道,如果有人有办法做到这一点,其中杰克逊会接受吗?
更新
我也试过这样:
- nom: "service1"
etats : &e1s1
- nom: "e1"
childs:
- nom: "e2"
childs:
- nom: "e3"
childs:
- &a
nom: "e5"
- nom: "e4"
childs:
- *a
但得到:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `Etat` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('a')
at [Source: (File); line: 13, column: 15] (through reference chain: java.lang.Object[][0]->Service["etats"]->java.util.ArrayList[0]->Etat["childs"]->java.util.ArrayList[1]->Etat["childs"]->java.util.ArrayList[0])
我虽然它会工作,因为杰克逊YAML模块似乎使用SnakeYAML根据https://github.com/FasterXML/jackson-dataformats-text/tree/master/yaml – Namoz
是的,但它可能会使用低级别界面不能解析别名。 – flyx
我认为你是对的,所以我用YAMLBeans来代替,并且它工作正常。 – Namoz