Snake yaml dumper options generating unnecessary escape spaces character (“\ ”) for a String with spaces when converting from map to yaml

自闭症网瘾萝莉.ら 提交于 2020-06-01 05:08:40

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/62047880/snake-yaml-dumper-options-generating-unnecessary-escape-spaces-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!