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

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

Introduction

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

Prototype

public abstract JsonToken nextToken() throws IOException, JsonParseException;

Source Link

Document

Main iteration method, which will advance stream enough to determine type of the next token, if any.

Usage

From source file:org.mashti.jetson.util.JsonParserUtil.java

/**
 * Consumes the next token from a JSON stream and checks that the token is a {@link JsonToken#FIELD_NAME}.
 * Throws {@link JsonParseException} if the token is not a {@link JsonToken#FIELD_NAME}.
 *
 * @param parser the parser to read from
 * @return the field name/* w  w  w  .j  a  va 2 s.c o m*/
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static String nextFieldName(final JsonParser parser) throws IOException {

    if (parser.nextToken() == JsonToken.FIELD_NAME) {
        return parser.getCurrentName();
    }
    throw new JsonParseException("expected some field name", parser.getCurrentLocation());
}

From source file:com.microsoft.azure.storage.core.WrappedContentKey.java

public static WrappedContentKey deserialize(JsonParser parser) throws JsonParseException, IOException {
    JsonUtilities.assertIsStartObjectJsonToken(parser);

    parser.nextToken();

    WrappedContentKey wrappedContentKey = new WrappedContentKey();
    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        parser.nextToken();//w w w.  j  a  v a2s.c  om

        if (name.equals("KeyId")) {
            wrappedContentKey.setKeyId(parser.getValueAsString());
        } else if (name.equals("EncryptedKey")) {
            wrappedContentKey.setEncryptedKey(parser.getBinaryValue());
        } else if (name.equals("Algorithm")) {
            wrappedContentKey.setAlgorithm(parser.getValueAsString());
        }
        parser.nextToken();
    }

    JsonUtilities.assertIsEndObjectJsonToken(parser);

    return wrappedContentKey;
}

From source file:org.talend.components.jira.datum.EntityParser.java

/**
 * Rewinds {@link JsonParser} to the value of specified field
 * //from   ww  w.j a  v a  2  s. c om
 * @param parser JSON parser
 * @param fieldName name of field rewind to
 * @return true if field was found, false otherwise
 * @throws IOException in case of exception during JSON parsing
 */
private static boolean rewindToField(JsonParser parser, final String fieldName) throws IOException {

    JsonToken currentToken = parser.nextToken();
    /*
     * There is no special token, which denotes end of file, in Jackson.
     * This counter is used to define the end of file.
     * The counter counts '{' and '}'. It is increased, when meets '{' and
     * decreased, when meets '}'. When braceCounter == 0 it means the end
     * of file was met
     */
    int braceCounter = 0;
    String currentField = null;
    do {
        if (JsonToken.START_OBJECT == currentToken) {
            braceCounter++;
        }
        if (JsonToken.END_OBJECT == currentToken) {
            braceCounter--;
        }
        if (JsonToken.FIELD_NAME == currentToken) {
            currentField = parser.getCurrentName();
        }
        currentToken = parser.nextToken();
    } while (!fieldName.equals(currentField) && braceCounter != END_JSON);

    return braceCounter != END_JSON;
}

From source file:com.github.heuermh.ensemblrestclient.JacksonOverlapConverter.java

static List<Variation> parseOverlap(final JsonFactory jsonFactory, final InputStream inputStream)
        throws IOException {
    JsonParser parser = null;
    try {//from   www.  jav  a2s  .c o m
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String id = null;
        String reference = null;
        List<String> alternateAlleles = new ArrayList<String>();
        String locationName = null;
        String coordinateSystem = "chromosome";
        int start = -1;
        int end = -1;
        int strand = -1;
        List<Variation> variationFeatures = new ArrayList<Variation>();
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                String field = parser.getCurrentName();
                parser.nextToken();
                if ("id".equals(field)) {
                    id = parser.getText();
                } else if ("seq_region_name".equals(field)) {
                    locationName = parser.getText();
                } else if ("start".equals(field)) {
                    start = Integer.parseInt(parser.getText());
                } else if ("end".equals(field)) {
                    end = Integer.parseInt(parser.getText());
                } else if ("strand".equals(field)) {
                    strand = Integer.parseInt(parser.getText());
                } else if ("alt_alleles".equals(field)) {
                    int index = 0;
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        if (index == 0) {
                            reference = parser.getText();
                        } else if (index == 1) {
                            alternateAlleles.add(parser.getText());
                        }
                        index++;
                    }
                }
            }
            variationFeatures.add(new Variation(id, reference, alternateAlleles,
                    new Location(locationName, coordinateSystem, start, end, strand)));
            id = null;
            reference = null;
            alternateAlleles.clear();
            locationName = null;
            start = -1;
            end = -1;
            strand = -1;
        }
        return variationFeatures;
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
}

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

private static void expectNextToken(JsonParser jp, JsonToken expected) throws IOException {
    if (jp.nextToken() != expected) {
        throw new IOException(String.format("Expected: %s, Found: %s", expected, jp.getCurrentToken()));
    }//from  w  w  w .jav  a 2 s .c o m
}

From source file:org.elasticsearch.client.sniff.ElasticsearchHostsSniffer.java

private static HttpHost readHost(String nodeId, JsonParser parser, Scheme scheme) throws IOException {
    HttpHost httpHost = null;//from  ww w.j  a  v a 2s .  c o m
    String fieldName = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            fieldName = parser.getCurrentName();
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            if ("http".equals(fieldName)) {
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JsonToken.VALUE_STRING
                            && "publish_address".equals(parser.getCurrentName())) {
                        URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
                        httpHost = new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(),
                                boundAddressAsURI.getScheme());
                    } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                        parser.skipChildren();
                    }
                }
            } else {
                parser.skipChildren();
            }
        }
    }
    //http section is not present if http is not enabled on the node, ignore such nodes
    if (httpHost == null) {
        logger.debug("skipping node [" + nodeId + "] with http disabled");
        return null;
    }
    return httpHost;
}

From source file:com.microsoft.azure.storage.blob.BlobEncryptionData.java

public static BlobEncryptionData deserialize(String inputData) throws JsonProcessingException, IOException {
    JsonParser parser = Utility.getJsonParser(inputData);
    BlobEncryptionData data = new BlobEncryptionData();
    try {// w  w w  .j  a  v a 2s. co  m
        if (!parser.hasCurrentToken()) {
            parser.nextToken();
        }

        JsonUtilities.assertIsStartObjectJsonToken(parser);

        parser.nextToken();

        while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            parser.nextToken();

            if (name.equals(Constants.EncryptionConstants.ENCRYPTION_MODE)) {
                data.setEncryptionMode(parser.getValueAsString());
            } else if (name.equals(Constants.EncryptionConstants.WRAPPED_CONTENT_KEY)) {
                data.setWrappedContentKey(WrappedContentKey.deserialize(parser));
            } else if (name.equals(Constants.EncryptionConstants.ENCRYPTION_AGENT)) {
                data.setEncryptionAgent(EncryptionAgent.deserialize(parser));
            } else if (name.equals(Constants.EncryptionConstants.CONTENT_ENCRYPTION_IV)) {
                data.setContentEncryptionIV(parser.getBinaryValue());
            } else if (name.equals(Constants.EncryptionConstants.KEY_WRAPPING_METADATA)) {
                data.setKeyWrappingMetadata(deserializeKeyWrappingMetadata(parser));
            } else {
                consumeJsonObject(parser);
            }
            parser.nextToken();
        }

        JsonUtilities.assertIsEndObjectJsonToken(parser);
    } finally {
        parser.close();
    }

    return data;
}

From source file:com.microsoft.azure.storage.core.EncryptionData.java

public static void consumeJsonObject(JsonParser parser) throws IOException {
    JsonUtilities.assertIsStartObjectJsonToken(parser);
    parser.nextToken();
    if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        consumeJsonObject(parser);// w  w w .ja va  2  s . c  o  m
    }
}

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

/**
 * Parses the error exception details from the Json-formatted response.
 * /*from w w  w.j a  v  a2  s .co m*/
 * @param parser
 *            the {@link JsonParser} to use for parsing
 * @throws IOException
 *             if an error occurs while accessing the stream with Json.
 * @throws JsonParseException
 *             if an error occurs while parsing the stream.
 */
private static HashMap<String, String[]> parseJsonErrorException(JsonParser parser)
        throws JsonParseException, IOException {
    HashMap<String, String[]> additionalDetails = new HashMap<String, String[]>();

    parser.nextToken();
    JsonUtilities.assertIsStartObjectJsonToken(parser);

    parser.nextToken();
    JsonUtilities.assertIsFieldNameJsonToken(parser);

    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentName().equals(TableConstants.ErrorConstants.ERROR_MESSAGE)) {
            parser.nextToken();
            additionalDetails.put(TableConstants.ErrorConstants.ERROR_MESSAGE,
                    new String[] { parser.getValueAsString() });
        } else if (parser.getCurrentName().equals(TableConstants.ErrorConstants.ERROR_EXCEPTION_TYPE)) {
            parser.nextToken();
            additionalDetails.put(TableConstants.ErrorConstants.ERROR_EXCEPTION_TYPE,
                    new String[] { parser.getValueAsString() });
        } else if (parser.getCurrentName().equals(TableConstants.ErrorConstants.ERROR_EXCEPTION_STACK_TRACE)) {
            parser.nextToken();
            additionalDetails.put(Constants.ERROR_EXCEPTION_STACK_TRACE,
                    new String[] { parser.getValueAsString() });
        }
        parser.nextToken();
    }

    return additionalDetails;
}

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

private static void readACLs(JsonParser jp, List<ACL> acls) throws IOException {
    expectCurrentToken(jp, JsonToken.START_ARRAY);
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        acls.add(readACL(jp));/*from   w  ww.j ava  2 s  .com*/
    }
}