Example usage for com.fasterxml.jackson.core JsonParser clearCurrentToken

List of usage examples for com.fasterxml.jackson.core JsonParser clearCurrentToken

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser clearCurrentToken.

Prototype

public abstract void clearCurrentToken();

Source Link

Document

Method called to "consume" the current token by effectively removing it so that #hasCurrentToken returns false, and #getCurrentToken null).

Usage

From source file:org.dbrain.data.jackson.serializers.JacksonSerializationUtils.java

public static Value parseValue(JsonParser parser, DeserializationContext ctxt) throws IOException {
    JsonToken token = getToken(parser);//  ww  w  .j  av  a 2 s .  c o  m
    if (token != null) {
        Value result;
        switch (token) {
        case VALUE_STRING:
            result = Value.of(parser.getValueAsString());
            break;
        case VALUE_NUMBER_FLOAT:
            result = Value.of(parser.getDoubleValue());
            break;
        case VALUE_NUMBER_INT:
            result = Value.of(parser.getBigIntegerValue());
            break;
        case VALUE_NULL:
            result = NullValueImpl.NULL;
            break;
        case VALUE_TRUE:
            result = Value.of(Boolean.TRUE);
            break;
        case VALUE_FALSE:
            result = Value.of(Boolean.FALSE);
            break;
        case START_OBJECT: {
            ValueMap values = ValueMap.newInstance();
            while (parser.nextToken() == JsonToken.FIELD_NAME) {
                String key = parser.getCurrentName();
                parser.nextToken();
                Value v = parseValue(parser, ctxt);
                if (v == null) {
                    throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
                }
                values.put(key, v);
            }
            if (getToken(parser) == JsonToken.END_OBJECT) {
                parser.clearCurrentToken();
            } else {
                throw ctxt.wrongTokenException(parser, JsonToken.END_OBJECT, null);
            }
            result = values;
        }
            break;
        case START_ARRAY: {
            ValueList values = ValueList.newInstance();
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                Value v = parseValue(parser, ctxt);
                if (v == null) {
                    throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
                }
                values.add(v);
            }
            if (getToken(parser) == JsonToken.END_ARRAY) {
                parser.clearCurrentToken();
            } else {
                throw ctxt.wrongTokenException(parser, JsonToken.END_ARRAY, null);
            }
            result = values;
        }
            break;
        default:
            throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
        }
        return result;
    } else {
        return null;
    }
}

From source file:com.opentable.jaxrs.StreamedJsonResponseConverter.java

private <T> void doRead(Callback<T> callback, TypeReference<T> type, final JsonParser jp) throws IOException {
    expect(jp, jp.nextToken(), JsonToken.START_OBJECT);
    expect(jp, jp.nextToken(), JsonToken.FIELD_NAME);
    if (!"results".equals(jp.getCurrentName())) {
        throw new JsonParseException("expecting results field", jp.getCurrentLocation());
    }//ww  w.  ja v a2s.  c o  m
    expect(jp, jp.nextToken(), JsonToken.START_ARRAY);
    // As noted in a well-hidden comment in the MappingIterator constructor,
    // readValuesAs requires the parser to be positioned after the START_ARRAY
    // token with an empty current token
    jp.clearCurrentToken();

    Iterator<T> iter = jp.readValuesAs(type);

    while (iter.hasNext()) {
        try {
            callback.call(iter.next());
        } catch (CallbackRefusedException e) {
            LOG.debug("callback refused execution, finishing.", e);
            return;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException("Callback interrupted", e);
        } catch (Exception e) {
            Throwables.propagateIfPossible(e, IOException.class);
            throw new IOException("Callback failure", e);
        }
    }
    if (jp.nextValue() != JsonToken.VALUE_TRUE || !jp.getCurrentName().equals("success")) {
        throw new IOException("Streamed receive did not terminate normally; inspect server logs for cause.");
    }
}

From source file:nl.talsmasoftware.enumerables.support.json.jackson2.EnumerableDeserializer.java

private Enumerable parseObject(JsonParser jp, Class<? extends Enumerable> type) throws IOException {
    Enumerable value = null;//from   w w  w.j  a  v  a2  s .  com
    while (true) {
        final JsonToken nextToken = jp.nextToken();
        if (nextToken == null)
            throw new IllegalStateException("JSON stream ended while parsing an Enumerable object.");

        switch (nextToken) {
        case VALUE_NULL:
        case VALUE_STRING:
            if (value == null && "value".equals(jp.getCurrentName())) {
                value = Enumerable.parse(type, jp.getText());
            }
            break;
        case END_OBJECT:
            jp.clearCurrentToken();
            if (value == null)
                throw new IllegalStateException(
                        "Attribute \"value\" is required to parse an Enumerable JSON object.");
            return value;
        case START_ARRAY:
        case START_OBJECT:
            jp.skipChildren();
            jp.clearCurrentToken();
            break;
        }
    }
}