英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论