如何在SnakeYaml中省略数据类型标签?

时间:2014-08-25 21:22:52

标签: parsing yaml snakeyaml

我有SnakeYaml生成的以下1.1 YAML

'test_jbgrp1':
  'tags': []
  'jobs':
  - 'test_job1'
  'reserve': []
  'cancel':
  - 'max_duration': !!int '1200'

!!int标签打破了另一个(较旧的)软件,我要求在写入文件之前删除标签。我不想回复到一个愚蠢的解决方案,比如将内容写入String并在转储文件之前对其进行后处理,所以问题是 - 在Snakeyaml中是否有一个设置会从上面的代码中删除!!int? / p>

1 个答案:

答案 0 :(得分:2)

假设您必须删除!!int

的所有出现

您可以查看How to skip a property以跳过该属性或使用Flexible Scalar Type Customization进行转换

简而言之,您可以配置Yaml实例,如下所示

Yaml yaml = new Yaml(new MyRepresenter());
String output = yaml.dump(new MyJavaBean());

其中MyRepresenter表示如下

@Override
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
                        Object propertyValue, Tag customTag) {
       if (int.class.equals(property.getType())) {//some better condition
          //construct NodeTupe as you wish - i.e. keep the element and remove the type
          return null;//this will skip the property
       } else {
          return super
                     .representJavaBeanProperty(javaBean, property, propertyValue, customTag);
       }
}