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

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

Introduction

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

Prototype

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

Source Link

Document

The main serialization method called by filter when property is to be written normally.

Usage

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

private void processFirstPass(Node parentNode, Object pojo, SerializerProvider provider, PropertyWriter writer,
        JsonGenerator generator) throws Exception {
    State state = this.state.get();
    Leaf leaf = Leaf.copyOf(state.propertyPath);

    // if this property _can_ contribute towards the path leading to a matching leaf then we have to check
    boolean matches = matcher.matches(leaf);
    if (matches || matcher.matchesParent(leaf)) {
        if (parentNode.isRoot()) {
            // make sure we don't actually write anything to the output, only replace if we are the root node, it will
            // be passed to other nodes as needed via recursive calls
            generator = new JsonFactory().createGenerator(CharStreams.nullWriter());
        }//www.  j av a  2  s  .  com

        // prepare a node for this property so child branches can add to it as needed
        state.currentNode = new Node(parentNode, writer.getName());
        writer.serializeAsField(pojo, generator, provider);

        // check the results of processing the children
        if (state.currentNode.isEmpty()) {
            // either we don't have any children or none of them are matching leaves.
            // in any case
            if (matches) {
                // it turns out we match anyway so add this node
                parentNode.addChild(state.currentNode);
            }
        } else {
            // child leafs match so we need to include this node as a parent of them
            parentNode.addChild(state.currentNode);
        }
    }
}

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);/*  w ww  .  ja v a2  s  .  co 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;
    }
}