Example usage for com.fasterxml.jackson.databind.ser PropertyWriter serializeAsOmittedField

List of usage examples for com.fasterxml.jackson.databind.ser PropertyWriter serializeAsOmittedField

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.ser PropertyWriter serializeAsOmittedField.

Prototype

public abstract void serializeAsOmittedField(Object pojo, JsonGenerator jgen, SerializerProvider prov)
        throws Exception;

Source Link

Document

Serialization method that filter needs to call in cases where property is to be filtered, but the underlying data format requires a placeholder of some kind.

Usage

From source file:com.pressassociation.pr.filter.json.jackson.JacksonMatcherFilter.java

@Override
public void serializeAsField(Object pojo, JsonGenerator jGen, SerializerProvider provider,
        PropertyWriter writer) throws Exception {
    checkNotNull(pojo);/*from ww w  .java  2  s. c  o  m*/
    checkNotNull(jGen);
    checkNotNull(provider);
    checkNotNull(writer);

    // remember the node that triggered this method call
    State state = this.state.get();
    if (state == null) {
        state = new State();
        this.state.set(state);
    }
    Node parentNode = state.currentNode;
    state.propertyPath.addLast(writer.getName());
    try {
        if (!state.serializationMode) {
            processFirstPass(parentNode, pojo, provider, writer, jGen);
        } else {
            // do the actual writing of the field
            // if the first child in the current node matches this field then we want to write it and it's children
            if (state.currentNode.matchesFirstChild(writer)) {
                state.currentNode = state.currentNode.popChild();
                writer.serializeAsField(pojo, jGen, provider);
            } else {
                // write missing fields.
                writer.serializeAsOmittedField(pojo, jGen, provider);
            }
        }

        if (parentNode.isRoot()) {
            try {
                // about to enter the _real_ serialization pass, mark it as such so the above logic isn't repeated
                state.serializationMode = true;
                if (parentNode.isEmpty()) {
                    // write out that we will be omitting data
                    writer.serializeAsOmittedField(pojo, jGen, provider);
                } else {
                    // in this case we've found a root property that needs to be output
                    // as we go through the child nodes again we check against the Node tree instead of the Matcher, we've
                    // already done those checks on the first pass
                    state.currentNode = parentNode.popChild();
                    checkState(state.currentNode.matchesName(writer), "Unexpected root node: %s",
                            state.currentNode);
                    writer.serializeAsField(pojo, jGen, provider);
                }
            } finally {
                state.serializationMode = false;
                this.state.remove();
            }
        }
    } finally {
        state.propertyPath.removeLast();
        state.currentNode = parentNode;
    }
}