使用 Snake Yaml 的必填字段缺少双引号 [英] Missing double quotes for the required field using Snake Yaml

查看:74
本文介绍了使用 Snake Yaml 的必填字段缺少双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取 Yaml 模板并动态替换模板中的某些字段并创建一个新的 Yaml 文件.我生成的 yaml 文件应该在所有方面都反映模板,包括双引号.但是当我使用蛇 yaml 时,我缺少必填字段的双引号.任何人都可以建议解决这个问题吗?

i am trying to read a Yaml template and replace certain fields in the template dynamically and create a new Yaml file. My resultant yaml file should reflect the template in all aspects including the double quotes. But I am missing double quotes for the required fields when I use snake yaml. Can anyone please suggest to resolve this issue?

示例:

version: snapshot-01
kind: sample
metadata:
  name: abc
groups:
  id: "1000B"
  category: category1

<小时>

我正在阅读上述模板并动态替换所需字段,如下所示.


I am reading the above template and replacing the required fields dynamically as shown below.

 Yaml yaml = new Yaml();
 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(yamlTemplateLocation);
 Map<String, Object>yamlMap = yaml.load(inputStream);

现在我正在替换如下所示的必填字段

Now I am replacing the required fields as shown below

 yamlMap.put("version","v-1.0");
 Map<String, Object> metadata = (Map<String, Object>) yamlMap.get("metadata");
 metadata.put("name", "XYZ");

 Map<String, Object> groups = (Map<String, Object>) yamlMap.get("groups");
 groups.put("id","5000Z");
 groups.put("category","newCategory");

        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);

        Yaml yaml = new Yaml(options);
        String output = yaml.dump(map);
        System.out.println(output);

我期待如下所示的输出

version: v-1.0
kind: sample
metadata:
  name: XYZ
groups:
  id: "5000Z"
  category: newCategory

<小时>

但我实际上得到如下输出

version: v-1.0
kind: sample
metadata:
  name: XYZ
groups:
  id: 5000Z
  category: newCategory

<小时>

我的问题是,我在新的 yaml 文件中缺少id"节点的双引号.当我使用 options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED) 时,我得到了所有不需要的双引号字段.我只需要 id 字段的双引号.任何人都可以请建议解决此问题.


My problem here is, I am missing the double quotes for "id" node in the new yaml file. When I use, options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED), I am getting all fields double quoted which is not required. I need double quotes for id field only. Can anyone please advice to resolve this issue.

谢谢

推荐答案

如果您的输入是模板,最好使用模板引擎.作为一个简单的例子,MessageFormat 将允许您编写 id: "{0}" 然后将实际值插入其中,保留双引号.根据您的用例,您可以使用更复杂的模板.

If your input is a template, it might be better to use a templating engine. As simple example, MessageFormat would allow you to write id: "{0}" and then interpolate the actual value into it, keeping the double quotes. You could use more sophisticated templating depending on your use-case.

话虽如此,让我们来看看如何使用 SnakeYAML:

That being said, let's look at how to do it with SnakeYAML:

如果你想控制单个项目如何呈现为标量,你必须定义一个这样的类:

If you want to control how a single item is rendered as scalar, you have to define a class like this:

class QuotedString {
    public String value;

    public QuotedString(String value) {
        this.value = value;
    }
}

然后为它创建一个自定义代表:

And then create a custom representer for it:

class MyRepresenter extends Representer {
    public MyRepresenter() {
        this.representers.put(QuotedString.class, new RepresentQuotedString());
    }

    private class RepresentQuotedString implements Represent {
        public Node representData(Object data) {
            QuotedString str = (QuotedString) data;
            return representScalar(
                    Tag.STR, str.value, DumperOptions.ScalarStyle.DOUBLE_QUOTED);
        }
    }
}

修改您的代码以使用新类:

Modify your code to use the new class:

groups.put("id", new QuotedString("5000Z"));

最后,指示 SnakeYAML 使用您的代表:

And finally, instruct SnakeYAML to use your representer:

Yaml yaml = new Yaml(new MyRepresenter(), options);

应该这样做.

这篇关于使用 Snake Yaml 的必填字段缺少双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆