【发布时间】:2018-03-23 05:19:53
【问题描述】:
我正在尝试将 YAML 文件解析为对象。
尽管 Online YAML Parser 告诉我它可以按照我想要的方式进行解析,但 Jackson 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”作为一个孩子。
但是,当我尝试使用 Jackson 解析此内容时,出现以下错误:
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])
【问题讨论】: