Snakeyaml似乎不必要地在列表中包装简单的值

Snakeyaml似乎不必要地在列表中包装简单的值

本文介绍了Snakeyaml似乎不必要地在列表中包装简单的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Groovy和Snakeyaml解析下面的YAML文件(显然我已经清理了数据,但它足以证明这个问题):

  --- 
info:
汇总:Snakeyaml发行
示例:
- 第一个示例:
名称:示例1
sublist:
- 0.1:
foo:bar

我期望以下语句:

  println resource.info.summary 
println resource.examples。1st example.name
println resource.examples。1st example.sublist。0.1

p>

  Snakeyaml发行
示例1
[foo:bar]

和:

  println resource.examples。第一个例子.sublist。0.1.foo 

得到:

  bar 

然而, 来自:

的输出>

  println resource.info.summary 
println resource.examples。1st example.name
println resource.examples 。1st example.sublist。0.1
println resource.examples。1st example.sublist。0.1.foo

是:

  Snakeyaml发行
[示例1]
[[[foo:bar]]]
[[bar]]

我可以通过包含列表标记来获得期望的输出:

  println resource.info.summary 
println resource.examples [ 0]1st example.name
println resource.examples [0]。1st example.sublist [0]。0.1
println resource.examples [0]。1st example .sublist [0]。0.1.foo

这似乎不必要。也许我误解了数据的结构?为了完整起见,我用来说明这个问题的groovy代码如下所示(我从客户的代码中取出CustomerResolver代码以便它将浮动字符串):

  import org.yaml.snakeyaml.DumperOptions 
导入组织。 yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import org.yaml.snakeyaml.nodes.Tag
import org.yaml.snakeyaml.representer.Representer
import org.yaml.snakeyaml.resolver.Resolver
$ b def fileName =example.yaml
def Yaml yaml = new Yaml(new Constructor(),new Representer(),new DumperOptions (),
new CustomResolver())
def resource = yaml.load(new File(fileName).newInputStream())

println resource.info.summary
println resource.examples。1st example.name
println resource.examples。1st example.sublist。0.1
println resource.examples。1st example.sublist。0.1 .foo

类CustomResolver扩展解析器{

/ *
*不解析浮点数和时间戳
* /

protected void addImplicitResolvers (){
addImplicitResolver(Tag.BOOL,BOOL,yYnNtTfFoO);
addImplicitResolver(Tag.INT,INT, - + 0123456789);
addImplicitResolver(Tag.MERGE,MERGE,<);
addImplicitResolver(Tag.NULL,NULL,〜nN\0);
addImplicitResolver(Tag.NULL,EMPTY,null);


$ b $ / code>

任何想法?

解决方案

问题在于如何访问yaml



例子首先包含 list ,然后只包含 object 与键第一个示例



您的情况请尝试以下访问:

  println resource.info.summary 
println resource.examples [0]。1st example
println resource.examples [0]。1st example .name
println resource.examples [0]。1st example.sublist [0]
println resource.examples [0]。1st example.sublist [0]。0.1
println resource.examples [0]。1st example.sublist [0]。0.1.foo

了解list访问器如何工作检查这个例子:

  @Grab(group ='org.yaml',module ='snakeyaml',version ='1.18')
import org.yaml.snakeyaml.Yaml
$ b $ def Yaml yaml = new Yaml()
def resource = yaml.load新的StringReader('''---
info:
总结ry:Snakeyaml发行
示例:
- 第一个示例:
名称:示例1
- 第一个示例:
名称:示例2
- 第一个示例:
xname:示例3
- 第二个示例:
名称:示例4
'''))

println resource.examples。1st example
//打印> [[name:Example 1],[name:Example 2],[xname:Example 3],null]

println resource.examples。1st example.name
//打印> ; [例1,例2,空]


I am attempting to parse the following YAML file using Groovy and Snakeyaml (clearly I have sanitised the data but it is sufficient to demonstrate the issue):

---
info:
  summary: Snakeyaml Issue
examples:
  - 1st example:
      name: Example 1
      sublist:
        - 0.1:
           foo: bar

I would expect the following statements:

println resource.info.summary
println resource.examples."1st example".name
println resource.examples."1st example".sublist."0.1"

to yield:

Snakeyaml Issue
Example 1
[foo:bar]

and:

println resource.examples."1st example".sublist."0.1".foo

to yield:

bar

However, the actual output from:

println resource.info.summary
println resource.examples."1st example".name
println resource.examples."1st example".sublist."0.1"
println resource.examples."1st example".sublist."0.1".foo

is:

Snakeyaml Issue
[Example 1]
[[[foo:bar]]]
[[bar]]

I can only get the desired output by including the list indicies:

println resource.info.summary
println resource.examples[0]."1st example".name
println resource.examples[0]."1st example".sublist[0]."0.1"
println resource.examples[0]."1st example".sublist[0]."0.1".foo

which seems unnecessary. Perhaps I am misunderstanding the structure of the data?

For completeness, the groovy code that I am using to illustrate the issue is shown below (I grabbed the CustomerResolver code off the web so that it would keep floats as strings):

import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import org.yaml.snakeyaml.nodes.Tag
import org.yaml.snakeyaml.representer.Representer
import org.yaml.snakeyaml.resolver.Resolver

def fileName = "example.yaml"
def Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(),
    new CustomResolver())
def resource = yaml.load(new File(fileName).newInputStream())

println resource.info.summary
println resource.examples."1st example".name
println resource.examples."1st example".sublist."0.1"
println resource.examples."1st example".sublist."0.1".foo

class CustomResolver extends Resolver {

    /*
     * Do not resolve float and timestamp
     */

    protected void addImplicitResolvers() {
        addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
        addImplicitResolver(Tag.INT, INT, "-+0123456789");
        addImplicitResolver(Tag.MERGE, MERGE, "<");
        addImplicitResolver(Tag.NULL, NULL, "~nN\0");
        addImplicitResolver(Tag.NULL, EMPTY, null);

    }
}

Any ideas?

解决方案

the problem in a way how you access the yaml

the examples first contains list and only then contains object with key 1st example

for your case try this access:

println resource.info.summary
println resource.examples[0]."1st example"
println resource.examples[0]."1st example".name
println resource.examples[0]."1st example".sublist[0]
println resource.examples[0]."1st example".sublist[0]."0.1"
println resource.examples[0]."1st example".sublist[0]."0.1".foo

to understand how list accessor works check this example:

@Grab(group='org.yaml', module='snakeyaml', version='1.18')
import org.yaml.snakeyaml.Yaml

def Yaml yaml = new Yaml()
def resource = yaml.load(new StringReader('''---
info:
  summary: Snakeyaml Issue
examples:
  - 1st example:
      name: Example 1
  - 1st example:
      name: Example 2
  - 1st example:
      xname: Example 3
  - 2nd example:
      name: Example 4
'''))

println resource.examples."1st example"
//prints>  [[name:Example 1], [name:Example 2], [xname:Example 3], null]

println resource.examples."1st example".name
//prints> [Example 1, Example 2, null]

这篇关于Snakeyaml似乎不必要地在列表中包装简单的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:31