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.jmingo.util.QueryUtils.java

/**
 * Checks what string has valid json format.
 *
 * @param json the json to validate//from ww w. java  2  s.co m
 * @return true if string is correct json otherwise - false
 */
public static boolean isValidJSON(final String json) {
    boolean valid = false;
    try {
        final JsonParser parser = new MongoMapper().getFactory().createParser(json);
        while (parser.nextToken() != null) {
        }
        valid = true;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return valid;
}

From source file:io.seldon.spark.actions.JobUtils.java

public static ActionData getActionDataFromActionLogLine(String actionLogLine) {
    ActionData actionData = new ActionData();

    String[] parts = actionLogLine.split("\\s+", 3);
    String json = parts[2];//w w  w .jav a  2 s .  c  o  m
    actionData.timestamp_utc = parts[0];

    JsonFactory jsonF = new JsonFactory();
    try {
        JsonParser jp = jsonF.createParser(json);
        if (jp.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // Let's move to value
            jp.nextToken();
            if (fieldName.equals("client")) {
                actionData.client = jp.getText();
            } else if (fieldName.equals("client_userid")) {
                actionData.client_userid = jp.getText();
            } else if (fieldName.equals("userid")) {
                actionData.userid = jp.getValueAsInt();
            } else if (fieldName.equals("itemid")) {
                actionData.itemid = jp.getValueAsInt();
            } else if (fieldName.equals("client_itemid")) {
                actionData.client_itemid = jp.getText();
            } else if (fieldName.equals("rectag")) {
                actionData.rectag = jp.getText();
            } else if (fieldName.equals("type")) {
                actionData.type = jp.getValueAsInt();
            } else if (fieldName.equals("value")) {
                actionData.value = jp.getValueAsDouble();
            }
        }
        jp.close();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return actionData;
}

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

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

    parser.nextToken();

    EncryptionAgent agent = new EncryptionAgent();
    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        parser.nextToken();/*from w w  w .  j a  v  a2  s  . com*/

        if (name.equals("Protocol")) {
            agent.setProtocol(parser.getValueAsString());
        } else if (name.equals("EncryptionAlgorithm")) {
            agent.setEncryptionAlgorithm(EncryptionAlgorithm.valueOf(parser.getValueAsString()));
        }
        parser.nextToken();
    }

    JsonUtilities.assertIsEndObjectJsonToken(parser);

    return agent;
}

From source file:org.nuxeo.client.test.marshallers.DocumentMarshaller.java

protected static List<Object> readArrayProperty(JsonParser jp) throws IOException {
    List<Object> list = new ArrayList<>();
    JsonToken tok = jp.nextToken();
    while (tok != JsonToken.END_ARRAY) {
        switch (tok) {
        case START_ARRAY:
            list.add(readArrayProperty(jp));
            break;
        case START_OBJECT:
            list.add(readObjectProperty(jp));
            break;
        default:/*from  w ww  .j  ava 2  s.  c o  m*/
            list.add(jp.getText());
        }
        tok = jp.nextToken();
    }
    return list;
}

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

static Lookup parseLookup(final JsonFactory jsonFactory, final InputStream inputStream) throws IOException {
    JsonParser parser = null;
    try {/*from   w w w.j  ava 2 s  .  co  m*/
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String id = null;
        String species = null;
        String type = null;
        String database = null;

        String locationName = null;
        String coordinateSystem = "chromosome"; // reasonable default?
        int start = -1;
        int end = -1;
        int strand = -1;

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String field = parser.getCurrentName();
            parser.nextToken();
            if ("id".equals(field)) {
                id = parser.getText();
            } else if ("species".equals(field)) {
                species = parser.getText();
            } else if ("object_type".equals(field)) {
                type = parser.getText();
            } else if ("db_type".equals(field)) {
                database = 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());
            }
        }
        return new Lookup(id, species, type, database,
                new Location(locationName, coordinateSystem, start, end, strand));
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:ai.susi.geo.GeoJsonReader.java

private static Map<String, String> parseMap(JsonParser parser) throws JsonParseException, IOException {
    Map<String, String> map = new HashMap<>();
    JsonToken token = parser.nextToken();
    if (!JsonToken.START_OBJECT.equals(token))
        return map;

    while (!parser.isClosed() && (token = parser.nextToken()) != null && token != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        token = parser.nextToken();//from   w  w  w  .j  a v  a 2  s .c  om
        String value = parser.getText();
        map.put(name.toLowerCase(), value);
    }
    return map;
}

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

static ArchivedSequence parseArchivedSequence(final JsonFactory jsonFactory, final InputStream inputStream)
        throws IOException {
    JsonParser parser = null;
    try {//from  ww  w. j a  va2 s. c om
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String id = null;
        String type = null;
        String assembly = null;
        String release = null;
        String version = null;
        String latest = null;
        String peptide = null;
        boolean current = false;
        List<String> possibleReplacement = new ArrayList<String>();

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

            if ("id".equals(field)) {
                id = parser.getText();
            } else if ("type".equals(field)) {
                type = parser.getText();
            } else if ("assembly".equals(field)) {
                assembly = parser.getText();
            } else if ("release".equals(field)) {
                release = parser.getText();
            } else if ("version".equals(field)) {
                version = parser.getText();
            } else if ("latest".equals(field)) {
                latest = parser.getText();
            } else if ("peptide".equals(field)) {
                peptide = parser.getText();
            } else if ("is_current".equals(field)) {
                current = (Integer.parseInt(parser.getText()) > 0);
            } else if ("possible_replacement".equals(field)) {
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    possibleReplacement.add(parser.getText());
                }
            }
        }
        return new ArchivedSequence(id, type, assembly, release, version, latest, peptide, current,
                possibleReplacement.toArray(new String[possibleReplacement.size()]));
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
}

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

/**
 * Expects start array.//from w w  w.  j av a  2s .  com
 *
 * @param parser the parser
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void expectStartArray(final JsonParser parser) throws IOException {

    if (parser.nextToken() != JsonToken.START_ARRAY) {
        throw new JsonParseException("expected start array", parser.getCurrentLocation());
    }
}

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#START_ARRAY}.
 * Throws {@link JsonParseException} if the token is not a {@link JsonToken#START_ARRAY}.
 *
 * @param parser the parser to read from
 * @throws IOException Signals that an I/O exception has occurred.
 *///from   www . j  a va2  s. c om
private static void expectEndArray(final JsonParser parser) throws IOException {

    if (parser.nextToken() != JsonToken.END_ARRAY) {
        throw new JsonParseException("expected end array", parser.getCurrentLocation());
    }
}

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#VALUE_NULL}.
 * Throws {@link JsonParseException} if the token is not a {@link JsonToken#VALUE_NULL}.
 *
 * @param parser the parser to read from
 * @throws IOException Signals that an I/O exception has occurred.
 *///from w ww  . j  a v a 2s .co m
private static void expectNullValue(final JsonParser parser) throws IOException {

    if (parser.nextToken() != JsonToken.VALUE_NULL) {
        throw new JsonParseException("expected null value", parser.getCurrentLocation());
    }
}