简体   繁体   English

YAML Jackson-数组的锚键

[英]YAML Jackson - Anchor keys of Array

I am trying to parse a YAML file into an object. 我正在尝试将YAML文件解析为一个对象。

Even though Online YAML Parser tells me that it is parsable the way I want, Jackson YAML parser refuses to give me what I want. 即使Online YAML解析器告诉我它可以按照我想要的方式进行解析,Jackson YAML解析器也拒绝提供我想要的内容。

Here is the YAML File : 这是YAML文件:

- nom: "service1"
  etats : &e1s1
    - nom: "e1"
      childs:
        - nom: "e2"
          childs:
            - nom: "e3"
              childs:
              - &a
                nom: "e5"
        - nom: "e4"
          childs:
            - <<: *a

Online YAML Parser tells me that "e4" and "e3" have "e5" as a child. 在线YAML分析器告诉我,“ e4”和“ e3”的孩子为“ e5”。

However when I try to parse this with Jackson, I get the following error : 但是,当我尝试用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["<<"])

So, I'd like to know if anybody have a way to do this where Jackson will accept it ? 所以,我想知道是否有人可以在杰克逊会接受的地方做到这一点?

Update 更新

I also tried this : 我也试过这个:

- nom: "service1"
  etats : &e1s1
    - nom: "e1"
      childs:
        - nom: "e2"
          childs:
            - nom: "e3"
              childs:
              - &a
                nom: "e5"
        - nom: "e4"
          childs:
            - *a

But get : 但是得到:

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])

<< is not a core YAML feature and has only been specified as an optional tag to outdated YAML 1.1 (see here ). <<不是YAML的核心功能,仅被指定为过时的YAML 1.1的可选标记(请参见此处 )。 Therefore, Jackson has every right not to support it. 因此,杰克逊完全有权不支持它。 Instead, it tries to parse << as a field name, which obviously fails because there is no such field. 相反,它尝试将<<解析为字段名称,这显然会失败,因为没有这样的字段。

Your second approach is more appropriate because it only uses core YAML features. 第二种方法更合适,因为它仅使用核心YAML功能。 The error you get points to a bug in Jackson as it incorrectly treats the alias *a as a String instead of resolving it. 您得到的错误指向Jackson中的错误,因为它错误地将别名*a视为字符串,而不是将其解析。 It may be a good idea to file a bug report. 提交错误报告可能是一个好主意。

Now you call Jackson a YAML parser in your question which it is not. 现在,您将问题称为Jackson而不是YAML解析器 It merely uses SnakeYaml as parser. 它仅使用SnakeYaml作为解析器。 SnakeYaml can load YAML to user-defined classes by itself, so you may be better of to directly use the SnakeYaml API . SnakeYaml可以自行将YAML加载到用户定义的类,因此最好直接使用SnakeYaml API Afaik it handles aliases correctly. Afaik它正确处理别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM