代码之家  ›  专栏  ›  技术社区  ›  warrior107

筛选出snakeyaml中的空字段

  •  9
  • warrior107  · 技术社区  · 9 年前

    我正在使用snakeyaml在YAML文件中打印我的对象。有些字段可能为空。

    1 回复  |  直到 9 年前
        1
  •  17
  •   warrior107    9 年前

    这是密码

    Representer representer = new Representer() {
        @Override
        protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,Tag customTag) {
            // if value of property is null, ignore it.
            if (propertyValue == null) {
                return null;
            }  
            else {
                return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
            }
        }
    };
    

    在YAML对象中使用此表示器。