从地图转换为Yaml时,Snake Yaml Dumper选项会为带有空格的字符串生成不必要的转义符("\") [英] Snake yaml dumper options generating unnecessary escape spaces character ("\ ") for a String with spaces when converting from map to yaml

查看:417
本文介绍了从地图转换为Yaml时,Snake Yaml Dumper选项会为带有空格的字符串生成不必要的转义符("\")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取Yaml模板并动态替换模板中的某些字段,并使用Snake Yaml创建一个新的Yaml文件.但是,当我使用蛇yaml时,如果字符串包含必需字段的空格,我将得到转义空格字符.有人可以建议解决此问题吗?

从地图到带空格的yaml转换我又遇到了一个问题.

示例:

--------------------------------
version: snapshot-01
kind: sample
metadata:
  name: abc
options: "<placeholder>"
--------------------------------

我正在阅读上面的模板,并如下所示动态替换必填字段.

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

当我用包含空格Ex的字符串替换占位符时:"abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd"

yamlMap.put("version","v-1.0");
yamlMap.put("options","abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd");

我得到的输出为

--------------------------------
version: "v-1.0"
kind: sample
metadata:
  name: abc
options:  "abc  sfksajfkl  jfajfkjakj  
      \ jqjlkkalkl  kajklfjalkd "
--------------------------------

注意:它正在生成转义符(即"\") 在"abc sfksajfkl jfajfkjakj \ jqjlkkalkl kajklfjalkd"中

但是我的要求如下所示-它不应生成任何转义符

--------------------------------
version: "v-1.0"
kind: sample
metadata:
  name: abc
options:  "abc  sfksajfkl  jfajfkjakj  jqjlkkalkl  kajklfjalkd"
--------------------------------

有人可以帮我吗?预先感谢!

解决方案

您显示的代码与YAML输出不匹配,因为该代码只有单个空格.我将假设YAML中的内容是实际放在地图中的字符串,因为您显示的代码不会产生此YAML,并且您的要求还包括双精度空格.

转义字符只是换行的副作用.在多行上用引号分隔的标量时,将换行符折叠到单个空间中.但是,单词之间有多个空格,因此YAML必须插入一个转义空格以将第二个空格标记为内容(第二行开头的所有非转义空格都视为缩进,而不是内容的一部分).

因此,您需要解决的实际问题是换行.您可以这样禁用它:

 DumperOptions options = new DumperOptions();
options.setSplitLines(false);
Yaml yaml = new Yaml(options);
System.out.println(yaml.dump(yamlMap));
 

请注意,带有换行符&的YAML转义空间确实可以正确加载.尽量不要对YAML表示形式提出过多的要求,因为您无法完全控制它.

i am trying to read a Yaml template and replace certain fields in the template dynamically and create a new Yaml file using Snake Yaml. But I am getting escape space character if a String contains spacess for the required fields when I use snake yaml. Can anyone please suggest to resolve this issue?

I am getting one more issue from map to yaml conversion with spaces.

Example :

--------------------------------
version: snapshot-01
kind: sample
metadata:
  name: abc
options: "<placeholder>"
--------------------------------

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

When i replace the place holders with a string which contain spaces Ex : "abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd"

yamlMap.put("version","v-1.0");
yamlMap.put("options","abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd");

I am getting output as

--------------------------------
version: "v-1.0"
kind: sample
metadata:
  name: abc
options:  "abc  sfksajfkl  jfajfkjakj  
      \ jqjlkkalkl  kajklfjalkd "
--------------------------------

Note : It is generating escape space character (i.e "\ ") in "abc sfksajfkl jfajfkjakj \ jqjlkkalkl kajklfjalkd "

But my requirement is like below - which should not generate any escape space character

--------------------------------
version: "v-1.0"
kind: sample
metadata:
  name: abc
options:  "abc  sfksajfkl  jfajfkjakj  jqjlkkalkl  kajklfjalkd"
--------------------------------

Could anyone please help me with this? Thanks in advance!

解决方案

The code you show doesn't match the YAML output since the code only has single spaces. I will assume the content in the YAML is the actual string being put in the map since the code you show would not produce this YAML and your requirement also include the double spaces.

The escape character is just a side effect of line breaking. When breaking a quoted scalar over multiple lines, line breaks are folded into single spaces. However, you have multiple spaces between the words, so YAML must insert an escaped space to mark the second space as content (all non-escaped spaces at the beginning of the second line is considered indentation and not part of the content).

So the actual problem you need to solve is line breaking. You disable it like this:

DumperOptions options = new DumperOptions();
options.setSplitLines(false);
Yaml yaml = new Yaml(options);
System.out.println(yaml.dump(yamlMap));

As a side note, the YAML with line breaks & escaped space does load correctly. Try not to put too specific requirements on YAML representation since you cannot completely control it.

这篇关于从地图转换为Yaml时,Snake Yaml Dumper选项会为带有空格的字符串生成不必要的转义符("\")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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