All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openapitools.jackson.nullable.JsonNullableSerializer Maven / Gradle / Ivy

Go to download

JsonNullable wrapper class and Jackson module to support fields with meaningful null values.

There is a newer version: 0.2.6
Show newest version
package org.openapitools.jackson.nullable;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.std.ReferenceTypeSerializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
import com.fasterxml.jackson.databind.util.NameTransformer;

public class JsonNullableSerializer extends ReferenceTypeSerializer> {

    private static final long serialVersionUID = 1L;

    /*
    /**********************************************************
    /* Constructors, factory methods
    /**********************************************************
     */

    protected JsonNullableSerializer(ReferenceType fullType, boolean staticTyping,
                                     TypeSerializer vts, JsonSerializer ser) {
        super(fullType, staticTyping, vts, ser);
    }

    protected JsonNullableSerializer(JsonNullableSerializer base, BeanProperty property,
                                     TypeSerializer vts, JsonSerializer valueSer, NameTransformer unwrapper,
                                     Object suppressableValue)
    {
        // Keep suppressNulls to false to always serialize JsonNullable[null]
        super(base, property, vts, valueSer, unwrapper,
                suppressableValue, false);
    }

    @Override
    protected ReferenceTypeSerializer> withResolved(BeanProperty prop,
                                                                    TypeSerializer vts, JsonSerializer valueSer,
                                                                    NameTransformer unwrapper)
    {
        return new JsonNullableSerializer(this, prop, vts, valueSer, unwrapper,
                _suppressableValue);
    }

    @Override
    public ReferenceTypeSerializer> withContentInclusion(Object suppressableValue,
                                                                         boolean suppressNulls)
    {
        return new JsonNullableSerializer(this, _property, _valueTypeSerializer,
                _valueSerializer, _unwrapper,
                suppressableValue);
    }

    /*
    /**********************************************************
    /* Abstract method impls
    /**********************************************************
     */

    @Override
    protected boolean _isValuePresent(JsonNullable value) {
        return value.isPresent();
    }

    @Override
    protected Object _getReferenced(JsonNullable value) {
        return value.get();
    }

    @Override
    protected Object _getReferencedIfPresent(JsonNullable value) {
        return value.isPresent() ? value.get() : null;
    }
}