我想解析一個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