Example usage for com.fasterxml.jackson.databind.util TokenBuffer asParser

List of usage examples for com.fasterxml.jackson.databind.util TokenBuffer asParser

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.util TokenBuffer asParser.

Prototype

public JsonParser asParser(ObjectCodec paramObjectCodec) 

Source Link

Usage

From source file:com.basistech.rosette.dm.jackson.ListAttributeDeserializer.java

@SuppressWarnings("unchecked")
private ListAttribute deserialize(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb)
        throws IOException {
    jp.nextToken();//from  w  w  w  .  j a  v a 2s.  co  m
    String keyName = jp.getText();

    if (tb != null) { // need to put back skipped properties?
        jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
    }
    // Must point to the next value; tb had no current, jp pointed to VALUE_STRING:

    KnownAttribute attribute = KnownAttribute.getAttributeForKey(keyName);
    if (attribute == null) {
        attribute = KnownAttribute.UNKNOWN;
    }
    Class<? extends BaseAttribute> itemClass = attribute.attributeClass();

    ListAttribute.Builder<BaseAttribute> builder = new ListAttribute.Builder<>(attribute.attributeClass());
    List<BaseAttribute> items = Lists.newArrayList();

    JsonToken nextToken;
    while ((nextToken = jp.nextToken()) != JsonToken.END_OBJECT) {
        if (nextToken != JsonToken.FIELD_NAME) {
            throw ctxt.wrongTokenException(jp, JsonToken.END_OBJECT, "Expected field name.");
        } else {
            String name = jp.getCurrentName();
            if ("items".equals(name)) {
                // the actual list items.
                nextToken = jp.nextToken();
                if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
                    Object o = jp.getEmbeddedObject();
                    if (o instanceof List) { // could it be an array, also?!?
                        // when using JsonTree, sometimes Jackson just sticks the entire Java object in here.
                        items.addAll((List) o);
                    } else {
                        throw ctxt.mappingException(
                                "List contains VALUE_EMBEDDED_OBJECT for items, but it wasn't a list.");
                    }
                } else if (nextToken != JsonToken.START_ARRAY) { // what about nothing?
                    throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "Expected array of items");
                } else {
                    // the START_ARRAY case, which is _normal_. Read the elements.
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        items.add(jp.readValueAs(itemClass));
                    }
                }
            } else {
                nextToken = jp.nextToken();
                Object value;
                if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
                    value = jp.getEmbeddedObject();
                } else {
                    value = jp.readValueAs(Object.class);
                }
                builder.extendedProperty(name, value);
            }
        }
    }
    builder.setItems(items);
    return builder.build();
}