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

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

Introduction

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

Prototype

public abstract JsonToken getCurrentToken();

Source Link

Document

Accessor to find which token parser currently points to, if any; null will be returned if none.

Usage

From source file:com.microsoft.azure.storage.table.ODataUtilities.java

/***
 * Reserved for internal use. Asserts that the token type of the parser is the end of an array.
 * /*from  w w  w.j  a v  a 2s .  c om*/
 * @param parser
 *            The {@link JsonParser} whose current token to check.
 */
protected static void assertIsEndArrayJsonToken(final JsonParser parser) throws JsonParseException {
    if (!(parser.getCurrentToken() == JsonToken.END_ARRAY)) {
        throw new JsonParseException(SR.EXPECTED_END_ARRAY, parser.getCurrentLocation());
    }
}

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

/**
 * Helper method to ensure to get the current token.
 *//*  www  .  java 2  s . c  o m*/
public static JsonToken getToken(JsonParser parser) throws IOException {
    return parser.hasCurrentToken() ? parser.getCurrentToken() : parser.nextToken();
}

From source file:org.agorava.linkedin.jackson.DeserializationUtils.java

public static <T> T deserializeFromDataNode(JsonParser jp, DeserializationContext ctxt, String propertyName,
        TypeReference<T> typeReference) throws IOException, JsonProcessingException {
    if (jp.hasCurrentToken() && jp.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        JsonNode dataNode = jp.readValueAs(JsonNode.class);
        if (dataNode.has(propertyName)) {
            return OBJECT_MAPPER.reader(typeReference).<T>readValue(dataNode.get(propertyName));
        }/*from  ww w. j a va2 s.c om*/
        return null;
    }
    throw ctxt.mappingException("Expected JSON object");
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Returns {@code true} if the JSON Object is NOT finished.
 * Logic is inverted so this is used as a loop-end condition.
 *//*from   w  w  w  .j  a  va2 s. c om*/
public static boolean endObject(JsonParser par) {
    JsonToken token = par.getCurrentToken();
    return token != null && token != JsonToken.END_OBJECT;
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Returns {@code true} if the JSON Object is NOT finished.
 * Logic is inverted so this is used as a loop-end condition.
 *///w w w  .ja va  2 s  .  c  o m
public static boolean endArray(JsonParser par) {
    JsonToken token = par.getCurrentToken();
    return token != null && token != JsonToken.END_ARRAY;
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Skips a field name if necessary, returning the current token then.
 *//* w w w.  j  av  a  2  s  .  c  o m*/
public static JsonToken peekToken(JsonParser par) throws IOException {
    JsonToken token = par.getCurrentToken();
    if (token == null || token == JsonToken.FIELD_NAME) {
        token = par.nextToken();
    }
    return token;
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Starts an Object, skipping the '{' token, and if necessary a field name before it.
 *//*  w  w w  .  j  a  v  a  2 s  .  c om*/
public static void startObject(JsonParser par) throws IOException {
    JsonToken token = par.getCurrentToken();
    if (token == null || token == JsonToken.FIELD_NAME) {
        token = par.nextToken();
    }
    if (token == JsonToken.START_OBJECT) {
        par.nextToken();
    } else {
        throw new JsonParseException(par, "Expected start of object");
    }
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Starts an Array, skipping the '[' token, and if necessary a field name before it.
 */// w  w w .  ja  v a  2  s  . c o  m
public static void startArray(JsonParser par) throws IOException {
    JsonToken token = par.getCurrentToken();
    if (token == null || token == JsonToken.FIELD_NAME) {
        token = par.nextToken();
    }
    if (token == JsonToken.START_ARRAY) {
        par.nextToken();
    } else {
        throw new JsonParseException(par, "Expected start of array");
    }
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Reads from either a JSON Value String (containing CSV) or a JSON Array.
 * The dual input format is needed because some fields (e.g. keywords) were allowed
 * to be of either type in OpenRTB 2.2; now in 2.3 they are all CSV strings only.
 * TODO: Simplify this to only accept CSV strings after 2.2 compatibility is dropped.
 *//*from   w ww .java  2  s  .  c om*/
public static String readCsvString(JsonParser par) throws IOException {
    JsonToken currentToken = par.getCurrentToken();
    if (currentToken == JsonToken.START_ARRAY) {
        StringBuilder keywords = new StringBuilder();
        for (startArray(par); endArray(par); par.nextToken()) {
            if (keywords.length() != 0) {
                keywords.append(',');
            }
            keywords.append(par.getText());
        }
        return keywords.toString();
    } else if (currentToken == JsonToken.VALUE_STRING) {
        return par.getText();
    } else {
        throw new JsonParseException(par, "Expected string or array");
    }
}

From source file:com.boundary.zoocreeper.Restore.java

private static void expectCurrentToken(JsonParser jp, JsonToken expected) throws IOException {
    final JsonToken currentToken = jp.getCurrentToken();
    if (currentToken != expected) {
        throw new IOException(String.format("Expected: %s, Found: %s", expected, currentToken));
    }/*from  w ww. j  a  v a  2  s.c  om*/
}