2016-09-28 1 views
1

snakeyaml을 사용하여 YAML 파일에서 내 개체를 인쇄하고 있습니다. null 일 수있는 필드가 있습니다. null이 파일에 인쇄되는 것을 방지하려면 어떻게해야합니까?snakeyaml의 null 필드 필터링

답변

2

일부 연구 끝에 마침내 해결책을 찾았습니다. 하나는 여기에 코드

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 개체에서이 representer가 Representer 표현 될 필요가 얼마나 널 (null) 필드를 변경해야합니다.