Java Code Examples for com.fasterxml.jackson.databind.node.ObjectNode#traverse()

The following examples show how to use com.fasterxml.jackson.databind.node.ObjectNode#traverse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MappingTest3.java    From jolt with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates how to do recursive polymorphic JSON deserialization in Jackson 2.2.
 *
 * Aka specify a Deserializer and "catch" some input, determine what type of Class it
 *  should be parsed too, and then reuse the Jackson infrastructure to recursively do so.
 */
@Override
public QueryFilter deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {

    ObjectNode root = jp.readValueAsTree();

    // pass in our objectCodec so that the subJsonParser knows about our configured Modules and Annotations
    JsonParser subJsonParser = root.traverse( jp.getCodec() );

    // Check if it is a "RealFilter"
    JsonNode queryParam = root.get("queryParam");
    if ( queryParam != null && queryParam.isValueNode() ) {
        return subJsonParser.readValueAs( RealFilter.class );
    }
    else {
        return subJsonParser.readValueAs( LogicalFilter3.class );
    }
}
 
Example 2
Source File: MappingTest5.java    From jolt with Apache License 2.0 6 votes vote down vote up
/**
 * I tried moving this logic to be an @JsonDeserialize on the QueryFilter5 interface
 *  but I could not get it to work.
 *
 * When this logic was moved there (and other logic was messed with), I would either get
 *  A) Deserializaion error on the List<QueryFilter5> in the LogicalFilter5 or
 *  B) stack overflow from bad recursion
 *
 * The problem with the List<QueryFilter5> in the LogicalFilter5, seemed like it
 *  looked at the type of the first QueryFilter5, and assumed that all the elements in
 *  the Array/List would be the same time, which totally breaks the goal of mixing
 *  Real and Logical QueryFilter subclasses.
 */
@Override
public QueryFilter5 deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {

    ObjectNode root = jp.readValueAsTree();

    // pass in our objectCodec so that the subJsonParser knows about our configured Modules and Annotations
    JsonParser subJsonParser = root.traverse( jp.getCodec() );

    // Check if it is a "RealFilter"
    JsonNode valuesParam = root.get("values");

    if ( valuesParam == null ) {
         return subJsonParser.readValueAs( LogicalFilter5.class );
    }
    if ( ! valuesParam.isArray() ) {
       throw new RuntimeException( "Expected an Array");
    }

    return subJsonParser.readValueAs( RealFilter5.class );
}
 
Example 3
Source File: MappingTest1.java    From jolt with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates how to do recursive polymorphic JSON deserialization in Jackson 2.2.
 *
 * Aka specify a Deserializer and "catch" some input, determine what type of Class it
 *  should be parsed too, and then reuse the Jackson infrastructure to recursively do so.
 */
@Override
public QueryFilter deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {

    ObjectNode root = jp.readValueAsTree();

    JsonNode queryParam = root.get("queryParam");
    String value = queryParam.asText();

    // pass in our objectCodec so that the subJsonParser knows about our configured Modules and Annotations
    JsonParser subJsonParser = root.traverse( jp.getCodec() );

    // Determine the "type" of filter we are dealing with Real or Logical and specify type
    if ( "OR".equals( value ) || "AND".equals( value ) ) {
        return subJsonParser.readValueAs( LogicalFilter1.class );
    }
    else {
        return subJsonParser.readValueAs( RealFilter.class );
    }
}
 
Example 4
Source File: MappingTest4.java    From jolt with Apache License 2.0 5 votes vote down vote up
/**
 * Demonstrates how to do recursive polymorphic JSON deserialization in Jackson 2.2.
 *
 * Aka specify a Deserializer and "catch" some input, determine what type of Class it
 *  should be parsed too, and then reuse the Jackson infrastructure to recursively do so.
 */
@Override
public QueryFilter4 deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {

    ObjectNode root = jp.readValueAsTree();

    // pass in our objectCodec so that the subJsonParser knows about our configured Modules and Annotations
    JsonParser subJsonParser = root.traverse( jp.getCodec() );

    // Check if it is a "RealFilter"
    JsonNode valueParam = root.get("value");

    if ( valueParam == null ) {
         return subJsonParser.readValueAs( LogicalFilter4.class );
    }
    if ( valueParam.isBoolean() ) {
        return subJsonParser.readValueAs( BooleanRealFilter4.class );
    }
    else if ( valueParam.isTextual() ) {
        return subJsonParser.readValueAs( StringRealFilter4.class );
    }
    else if ( valueParam.isIntegralNumber() ) {
        return subJsonParser.readValueAs( IntegerRealFilter4.class );
    }
    else {
        throw new RuntimeException("Unknown type");
    }
}