Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter out null fields in snakeyaml

Tags:

null

snakeyaml

I am using snakeyaml to print my object in a YAML file. There are some fields which could be null. How can I prevent these field, when they are null from being printed in the file?

like image 242
warrior107 Avatar asked Sep 28 '16 10:09

warrior107


People also ask

How do you ignore null fields in JSON response?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL.

How do I ignore null values in post request body in spring boot?

Just use this @JsonSerialize(include = Inclusion. NON_NULL) instead of @JsonInclude(Include. NON_NULL) and it works..!!

How do I ignore null values in Jackson?

In Jackson, we can use @JsonInclude(JsonInclude. Include. NON_NULL) to ignore the null fields.


1 Answers

After some research I have finally found the solution. One needs to change how null fields have to be represented in Representer Here is the code

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);
        }
    }
};

Use this representer in YAML object.

like image 107
warrior107 Avatar answered Sep 18 '22 02:09

warrior107