无法使用 SnakeYaml 保持原始方式格式化 YAML。

huangapple go评论78阅读模式
英文:

Not able to format YAML using SnakeYaml keeping original way

问题

以下是翻译好的部分:

有以下的YAML格式

    image:
      repository: "test.com/test"
      pullPolicy: IfNotPresent
      tag: "abc"

用于修改YAML文件的JAVA代码

    public class SnakeYaml1 {
    
    	public static void main(String[] args) throws FileNotFoundException {
    		// TODO Auto-generated method stub
    		
    		InputStream inputStream = new FileInputStream(new File("C:\\yaml\\student1.yaml"));
    		
    		Yaml yaml = new Yaml(new Constructor(Values1.class));
    	
    		Values1 data = yaml.load(inputStream);
    		Image image = new Image();
    		image.setPullPolicy("update");
    		data.setImage(image);
    		
    		DumperOptions options = new DumperOptions();
    		options.setIndent(2);
    		options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    		options.setIndicatorIndent(2);
    		options.setIndentWithIndicator(true);
    		
    		PrintWriter writer = new PrintWriter(new File("C:\\yaml\\student1.yaml"));
    		Yaml yaml1 = new Yaml(new Constructor(Values1.class));
    		yaml1.dump(data, writer);
    
    	}
    }


    public class Values1 {
    	
    	private Image image;
    
    	public Image getImage() {
    		return image;
    	}
    
    	public void setImage(Image image) {
    		this.image = image;
    	}
    }

    public class Image {
    	
    	private String repository;
    	private String pullPolicy;
    	private String tag;
    	
    	public Image()
    	{	
    	}
    	
    	public Image (String repository, String pullPolicy, String tags)
    	{
    		super();
    		this.repository = repository;
    		this.pullPolicy = pullPolicy;
    		this.tag = tags;
    		
    	}
    	
    	public String getRepository() {
    		return repository;
    	}
    	public void setRepository(String repository) {
    		this.repository = repository;
    	}
    	public String getPullPolicy() {
    		return pullPolicy;
    	}
    	public void setPullPolicy(String pullPolicy) {
    		this.pullPolicy = pullPolicy;
    	}
    	public String getTag() {
    		return tag;
    	}
    	public void setTag(String tag) {
    		this.tag = tag;
    	}
    	
    
    }


执行Java代码后,YAML格式发生了变化

**执行Java代码后的YAML格式**

    !!oe.kubeapi.abc.Values1
    image: {pullPolicy: update, repository: null, tag: null}

**期望执行Java代码后的YAML格式**

    image:
          repository: "test.com/test"
          pullPolicy: update
          tag: "abc"

不明白为什么执行Java代码后YAML格式会发生变化。这是SnakeYaml的bug吗?

我尝试将属性`image`的格式更改为List,即`List<Image> image`,但仍然不起作用。

请建议应该采取什么措施。请提供帮助。
英文:

Have following YAML

image:
repository: "test.com/test"
pullPolicy: IfNotPresent
tag: "abc"

JAVA code to modify the YAKL file

public class SnakeYaml1 {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
InputStream inputStream = new FileInputStream(new File("C:\\yaml\\student1.yaml"));
Yaml yaml = new Yaml(new Constructor(Values1.class));
Values1 data = yaml.load(inputStream);
Image image = new Image();
image.setPullPolicy("update");
data.setImage(image);
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
options.setIndicatorIndent(2);
options.setIndentWithIndicator(true);
PrintWriter writer = new PrintWriter(new File("C:\\yaml\\student1.yaml"));
Yaml yaml1 = new Yaml(new Constructor(Values1.class));
yaml1.dump(data, writer);
}
}
public class Values1 {
private Image image;
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}
public class Image {
private String repository;
private String pullPolicy;
private String tag;
public Image()
{	
}
public Image (String repository, String pullPolicy, String tags)
{
super();
this.repository = repository;
this.pullPolicy = pullPolicy;
this.tag = tags;
}
public String getRepository() {
return repository;
}
public void setRepository(String repository) {
this.repository = repository;
}
public String getPullPolicy() {
return pullPolicy;
}
public void setPullPolicy(String pullPolicy) {
this.pullPolicy = pullPolicy;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
}

AFter executing the java code , the YAML format is getting changed

YAML format after executing JAVA code

!!oe.kubeapi.abc.Values1
image: {pullPolicy: update, repository: null, tag: null}

Expected YAML format after execution of java code

image:
repository: "test.com/test"
pullPolicy: update
tag: "abc"

Not getting why the YAML format is getting changed after executing java code. Is this the bug in SnakeYaml ??

I tried putting property image in List format as well , List<Image> image still it did not work

please suggest . what should be done . Any help please ?

答案1

得分: 1

code should Your.

options Yaml and

yaml = Yaml DumperOptions
Style FlowStyle.BLOCK(DumperOptions.FlowStyle.setDefaultFlowStyle(options);

should works as it code Your.

1:#markdown-header-dumping-yaml Documentation SnakeYaml its through looked ever you have you so lib, SnakeYaml it is mentioned you, it is that mentioned you, Well,

英文:

Well, you mentioned it is SnakeYaml lib, so I wonder have you ever looked through its documentation ?

Your code works as it should.

try:

 DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml  = new Yaml(options);

huangapple
  • 本文由 发表于 2023年6月2日 01:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定