使用javabean和字典列表创建yaml

xiozqbni  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(252)

如何创建字典列表,字典的值是列表或两者的混合
我试图使用JavaPOJO和snakeyaml库来构建下面的yaml

- name: this is test1
  ec2Group:
    name: example.1
    description: an example test1 group
- name: this is test2
  ec2Group:
    name: example.2
    description: an example test2 group

我运行完代码后得到的是

- ec2Group:
    description: an example EC2 group
    name: example
- ec2Group:
    description: an example EC22 group
    name: exampl2e

这是我的java定义

public class AWSSecurityGroupWrapper {

    private String name;
    private Ec2Group ec2Group;

}

public class Ec2Group {

    private String name;
    private String description;

}

这是我正在运行的java程序

Representer representer = new Representer() {
            @Override
            protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
                    Tag customTag) {
                if (propertyValue == null || propertyValue == "") {
                    return null;
                } 
                return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
            }
        };

        representer.addClassTag(AWSSecurityGroupWrapper.class, Tag.MAP);
        representer.getPropertyUtils().setSkipMissingProperties(true);
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

        List<AWSSecurityGroupWrapper> groupWrappersList = new ArrayList<>();
        AWSSecurityGroupWrapper awsSecurityGroupWrapper = new AWSSecurityGroupWrapper();
        Ec2Group ec2Group = new Ec2Group();
        ec2Group.setDescription("an example EC2 group");
        ec2Group.setName("example");
        awsSecurityGroupWrapper.setEc2Group(ec2Group);

        StringWriter writer = new StringWriter();
        Yaml yaml = new Yaml(representer, options);
        yaml.dump(awsSecurityGroupWrapper, writer);
        System.out.println(writer);

在本例中,什么是javapojo的正确表示,请给予帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题