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:com.github.heuermh.ensemblrestclient.JacksonVariationConverter.java

static VariationConsequences parseVariationConsequences(final JsonFactory jsonFactory,
        final InputStream inputStream) throws IOException {
    JsonParser parser = null;
    try {/*from  w  w w .  ja va2  s  . c o m*/
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String identifier = null;
        String referenceAllele = null;
        List<String> alternateAlleles = new ArrayList<String>();

        String locationName = null;
        String coordinateSystem = "chromosome";
        int start = -1;
        int end = -1;
        int strand = -1;

        List<TranscriptConsequences> transcriptConsequences = new ArrayList<TranscriptConsequences>();

        String alternateAllele = null;
        int transcriptStrand = -1;
        boolean canonical = false;
        String geneId = null;
        String transcriptId = null;
        String translationId = null;
        String transcriptAlleleString = null;
        String codons = null;
        String hgvsc = null;
        String aminoAcids = null;
        String hgvsp = null;
        List<String> consequenceTerms = new ArrayList<String>();

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

                if ("id".equals(field)) {
                    identifier = parser.getText();
                } else if ("seq_region_name".equals(field)) {
                    locationName = parser.getText();
                } else if ("start".equals(field)) {
                    start = parser.getIntValue();
                } else if ("end".equals(field)) {
                    end = parser.getIntValue();
                } else if ("strand".equals(field)) {
                    strand = parser.getIntValue();
                } else if ("allele_string".equals(field)) {
                    String[] tokens = parser.getText().split("/");
                    referenceAllele = tokens[0];
                    for (int i = 1; i < tokens.length; i++) {
                        alternateAlleles.add(tokens[i]);
                    }
                } else if ("transcript_consequences".equals(field)) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            String transcriptField = parser.getCurrentName();
                            parser.nextToken();

                            if ("variant_allele".equals(transcriptField)) {
                                alternateAllele = parser.getText();
                            } else if ("strand".equals(transcriptField)) {
                                transcriptStrand = parser.getIntValue();
                            } else if ("canonical".equals(transcriptField)) {
                                canonical = (Integer.parseInt(parser.getText()) > 0);
                            } else if ("gene_id".equals(transcriptField)) {
                                geneId = parser.getText();
                            } else if ("transcript_id".equals(transcriptField)) {
                                transcriptId = parser.getText();
                            } else if ("protein_id".equals(transcriptField)) {
                                translationId = parser.getText();
                            } else if ("codons".equals(transcriptField)) {
                                codons = parser.getText();
                            } else if ("hgvsc".equals(transcriptField)) {
                                hgvsc = parser.getText();
                            } else if ("amino_acids".equals(transcriptField)) {
                                aminoAcids = parser.getText();
                            } else if ("hgvsp".equals(transcriptField)) {
                                hgvsp = parser.getText();
                            } else if ("consequence_terms".equals(transcriptField)) {
                                while (parser.nextToken() != JsonToken.END_ARRAY) {
                                    consequenceTerms.add(parser.getText());
                                }
                            }
                        }

                        transcriptConsequences.add(new TranscriptConsequences(alternateAllele, transcriptStrand,
                                canonical, geneId, transcriptId, translationId, codons, hgvsc, aminoAcids,
                                hgvsp, consequenceTerms));

                        alternateAllele = null;
                        transcriptStrand = -1;
                        canonical = false;
                        geneId = null;
                        transcriptId = null;
                        translationId = null;
                        transcriptAlleleString = null;
                        codons = null;
                        hgvsc = null;
                        aminoAcids = null;
                        hgvsp = null;
                        consequenceTerms.clear();
                    }
                } else if ("colocated_variants".equals(field)) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            // ignore
                        }
                    }
                }
            }
        }
        Location location = new Location(locationName, coordinateSystem, start, end, strand);
        return new VariationConsequences(identifier, referenceAllele, alternateAlleles, location,
                transcriptConsequences);
    } 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 ACL readACL(JsonParser jp) throws IOException {
    expectCurrentToken(jp, JsonToken.START_OBJECT);
    String scheme = null;//w w  w .j  a  va2s .c  o  m
    String id = null;
    int perms = -1;
    final Set<String> seenFields = Sets.newHashSet();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        jp.nextValue();
        final String fieldName = jp.getCurrentName();
        seenFields.add(fieldName);
        if (Backup.FIELD_ACL_SCHEME.equals(fieldName)) {
            scheme = jp.getValueAsString();
        } else if (Backup.FIELD_ACL_ID.equals(fieldName)) {
            id = jp.getValueAsString();
        } else if (Backup.FIELD_ACL_PERMS.equals(fieldName)) {
            perms = jp.getIntValue();
        } else {
            throw new IOException("Unexpected field: " + fieldName);
        }
    }
    if (!seenFields.containsAll(REQUIRED_ACL_FIELDS)) {
        throw new IOException("Missing required ACL fields: " + REQUIRED_ACL_FIELDS);
    }
    final Id zkId;
    if (Ids.ANYONE_ID_UNSAFE.getScheme().equals(scheme) && Ids.ANYONE_ID_UNSAFE.getId().equals(id)) {
        zkId = Ids.ANYONE_ID_UNSAFE;
    } else {
        zkId = new Id(scheme, id);
    }
    return new ACL(perms, zkId);
}

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

private static BackupZNode readZNode(JsonParser jp, String path) throws IOException {
    expectNextToken(jp, JsonToken.START_OBJECT);
    long ephemeralOwner = 0;
    byte[] data = null;
    final List<ACL> acls = Lists.newArrayList();
    final Set<String> seenFields = Sets.newHashSet();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        jp.nextValue();/*from w ww . j av a  2 s. c o  m*/
        final String fieldName = jp.getCurrentName();
        seenFields.add(fieldName);
        if (Backup.FIELD_EPHEMERAL_OWNER.equals(fieldName)) {
            ephemeralOwner = jp.getLongValue();
        } else if (Backup.FIELD_DATA.equals(fieldName)) {
            if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                data = null;
            } else {
                data = jp.getBinaryValue();
            }
        } else if (Backup.FIELD_ACLS.equals(fieldName)) {
            readACLs(jp, acls);
        } else {
            LOGGER.debug("Ignored field: {}", fieldName);
        }
    }
    if (!seenFields.containsAll(REQUIRED_ZNODE_FIELDS)) {
        throw new IOException("Missing required fields: " + REQUIRED_ZNODE_FIELDS);
    }
    return new BackupZNode(path, ephemeralOwner, data, acls);
}

From source file:com.netflix.hollow.jsonadapter.util.JsonUtil.java

private static void print(JsonParser parser, JsonToken token, int index, PrintStream out) throws Exception {
    if (index == 0)
        System.out.println("\n\n -----");
    try {/*from  ww w  .  j av a  2 s  .  co m*/
        while (token != null && token != JsonToken.END_OBJECT) {
            switch (token) {
            case START_ARRAY:
                print(index, String.format("fieldname=%s, token=%s", parser.getCurrentName(), token), out);
                print(parser, parser.nextToken(), index + 1, out);
                break;
            case START_OBJECT:
                print(index, String.format("fieldname=%s, token=%s", parser.getCurrentName(), token), out);
                print(parser, parser.nextToken(), index + 1, out);
                break;
            case VALUE_NUMBER_INT:
                print(index, String.format("fieldname=%s, token=%s, value=%s", parser.getCurrentName(), token,
                        parser.getLongValue()), out);
                break;
            case VALUE_NUMBER_FLOAT:
                print(index, String.format("fieldname=%s, token=%s, value=%s", parser.getCurrentName(), token,
                        parser.getDoubleValue()), out);
                break;
            case VALUE_NULL:
                print(index,
                        String.format("fieldname=%s, token=%s, value=NULL", parser.getCurrentName(), token),
                        out);
                break;
            case VALUE_STRING:
                print(index, String.format("fieldname=%s, token=%s, value=%s", parser.getCurrentName(), token,
                        parser.getValueAsString()), out);
                break;
            case VALUE_FALSE:
            case VALUE_TRUE:
                print(index, String.format("fieldname=%s, token=%s, value=%s", parser.getCurrentName(), token,
                        parser.getBooleanValue()), out);
                break;
            case FIELD_NAME:
                //print(index, String.format("fieldname=%s, token=%s", parser.getCurrentName(), token));
                break;
            case END_ARRAY:
            case END_OBJECT:
                index--;
                break;
            default:
            }
            token = parser.nextToken();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}

From source file:org.datagator.api.client.MatrixDeserializer.java

private static ArrayList<ArrayList<Object>> parseRows(JsonParser jp, int bodyRow, int bodyColumn)
        throws IOException, JsonProcessingException {
    ArrayList<ArrayList<Object>> columnHeaders = new ArrayList<ArrayList<Object>>();

    int rowIndex = 0;

    JsonToken token = jp.getCurrentToken(); // START_ARRAY
    if (!token.equals(JsonToken.START_ARRAY)) {
        throw new RuntimeException(String.format("Unexpected token %s", token));
    }/*w  ww  .j  a v a 2  s.  c o m*/

    token = jp.nextToken(); // START_ARRAY
    while (token.equals(JsonToken.START_ARRAY)) {
        int columnIndex = 0;
        if (rowIndex < bodyRow) {
            ArrayList<Object> rowBuffer = new ArrayList<Object>();
            token = jp.nextToken();
            while (!token.equals(JsonToken.END_ARRAY)) {
                rowBuffer.add(jp.getText());
                columnIndex += 1;
                token = jp.nextToken();
            }
            columnHeaders.add(rowBuffer);
        } else {
            token = jp.nextToken();
            while (!token.equals(JsonToken.END_ARRAY)) {
                columnIndex += 1;
                token = jp.nextToken();
            }
        }
        rowIndex += 1;
        token = jp.nextToken(); // START_ARRAY
    }

    return columnHeaders;
}

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

protected static Document readDocument(JsonParser jp) throws IOException {
    String uid = null;//from w w  w .  j a  v a 2  s . c  om
    String type = null;
    String path = null;
    String state = null;
    String versionLabel = null;
    String isCheckedOut = null;
    String lockCreated = null;
    String lockOwner = null;
    String repository = null;
    String changeToken = null;
    boolean isProxy = false;
    JsonToken tok = jp.nextToken();
    Map<String, Object> properties = new HashMap<>();
    while (tok != null && tok != JsonToken.END_OBJECT) {
        tok = jp.nextToken();
        String key = jp.getText();
        tok = jp.nextToken();
        if ("uid".equals(key)) {
            uid = jp.getText();
        } else if ("path".equals(key)) {
            path = jp.getText();
        } else if ("type".equals(key)) {
            type = jp.getText();
        } else if ("state".equals(key)) {
            state = jp.getText();
        } else if ("versionLabel".equals(key)) {
            versionLabel = jp.getText();
        } else if ("isCheckedOut".equals(key)) {
            isCheckedOut = jp.getText();
        } else if ("lock".equals(key)) {
            if (!JsonToken.VALUE_NULL.equals(jp.getCurrentToken())) {
                String[] lock = jp.getText().split(":");
                lockOwner = lock[0];
                lockCreated = lock[1];
            }
        } else if ("lockCreated".equals(key)) {
            lockCreated = jp.getText();
        } else if ("lockOwner".equals(key)) {
            lockOwner = jp.getText();
        } else if ("repository".equals(key)) {
            repository = jp.getText();
        } else if ("properties".equals(key)) {
            readProperties(jp, properties);
        } else if ("changeToken".equals(key)) {
            changeToken = jp.getText();
        } else if ("isProxy".equals(key)) {
            isProxy = jp.getBooleanValue();
        }
    }
    return new Document(uid, type, null, changeToken, path, state, lockOwner, lockCreated, repository,
            versionLabel, isCheckedOut, isProxy, properties, null);
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Merges the {@code message} from the JsonParser using the given {@code schema}.
 *///  w  ww .j  a va  2 s.c  o  m
public static <T> void mergeFrom(JsonParser parser, T message, Schema<T> schema, boolean numeric)
        throws IOException {
    if (parser.nextToken() != JsonToken.START_OBJECT) {
        throw new JsonInputException("Expected token: { but was " + parser.getCurrentToken() + " on message "
                + schema.messageFullName());
    }

    schema.mergeFrom(new JsonInput(parser, numeric), message);

    if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        throw new JsonInputException("Expected token: } but was " + parser.getCurrentToken() + " on message "
                + schema.messageFullName());
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Parses the {@code messages} from the parser using the given {@code schema}.
 */// www .  j av  a  2  s  .  c o  m
public static <T> List<T> parseListFrom(JsonParser parser, Schema<T> schema, boolean numeric)
        throws IOException {
    if (parser.nextToken() != JsonToken.START_ARRAY) {
        throw new JsonInputException("Expected token: [ but was " + parser.getCurrentToken() + " on message: "
                + schema.messageFullName());
    }

    final JsonInput input = new JsonInput(parser, numeric);
    final List<T> list = new ArrayList<>();
    for (JsonToken t = parser.nextToken(); t != JsonToken.END_ARRAY; t = parser.nextToken()) {
        if (t != JsonToken.START_OBJECT) {
            throw new JsonInputException("Expected token: { but was " + parser.getCurrentToken()
                    + " on message " + schema.messageFullName());
        }

        final T message = schema.newMessage();
        schema.mergeFrom(input, message);

        if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
            throw new JsonInputException("Expected token: } but was " + parser.getCurrentToken()
                    + " on message " + schema.messageFullName());
        }

        list.add(message);
        input.reset();
    }
    return list;
}

From source file:org.helm.notation2.wsadapter.MonomerWSLoader.java

/**
 * Private routine to deserialize JSON containing monomer categorization data.
 * This is done manually to give more freedom regarding data returned by the
 * webservice./* w  w w .j a va2s.c o  m*/
 *
 * @param parser the JSONParser containing JSONData.
 * @return List containing the monomer categorization
 *
 * @throws JsonParseException
 * @throws IOException
 */
private static List<CategorizedMonomer> deserializeEditorCategorizationConfig(JsonParser parser)
        throws JsonParseException, IOException {
    List<CategorizedMonomer> config = new LinkedList<CategorizedMonomer>();
    CategorizedMonomer currentMonomer = null;

    parser.nextToken();
    while (parser.hasCurrentToken()) {
        String fieldName = parser.getCurrentName();
        JsonToken token = parser.getCurrentToken();

        if (JsonToken.START_OBJECT.equals(token)) {
            currentMonomer = new CategorizedMonomer();
        } else if (JsonToken.END_OBJECT.equals(token)) {
            config.add(currentMonomer);
        }

        if (fieldName != null) {
            switch (fieldName) {
            // id is first field
            case "monomerID":
                parser.nextToken();
                currentMonomer.setMonomerID(parser.getText());
                break;
            case "monomerName":
                parser.nextToken();
                currentMonomer.setMonomerName(parser.getText());
                break;
            case "naturalAnalogon":
                parser.nextToken();
                currentMonomer.setNaturalAnalogon(parser.getText());
                break;
            case "monomerType":
                parser.nextToken();
                currentMonomer.setMonomerType(parser.getText());
                break;
            case "polymerType":
                parser.nextToken();
                currentMonomer.setPolymerType(parser.getText());
                break;
            case "category":
                parser.nextToken();
                currentMonomer.setCategory(parser.getText());
                break;
            case "shape":
                parser.nextToken();
                currentMonomer.setShape(parser.getText());
                break;
            case "fontColor":
                parser.nextToken();
                currentMonomer.setFontColor(parser.getText());
                break;
            case "backgroundColor":
                parser.nextToken();
                currentMonomer.setBackgroundColor(parser.getText());
                break;
            default:
                break;
            }
        }
        parser.nextToken();
    }

    return config;
}

From source file:io.syndesis.jsondb.impl.JsonRecordSupport.java

public static void jsonStreamToRecords(JsonParser jp, String path, Consumer<JsonRecord> consumer)
        throws IOException {
    boolean inArray = false;
    int arrayIndex = 0;
    while (true) {
        JsonToken nextToken = jp.nextToken();

        String currentPath = path;

        if (nextToken == FIELD_NAME) {
            if (inArray) {
                currentPath = path + toArrayIndexPath(arrayIndex) + "/";
            }//  w  w  w.  jav  a 2s  .  co  m
            jsonStreamToRecords(jp, currentPath + validateKey(jp.getCurrentName()) + "/", consumer);
        } else if (nextToken == VALUE_NULL) {
            if (inArray) {
                currentPath = path + toArrayIndexPath(arrayIndex) + "/";
            }
            consumer.accept(JsonRecord.of(currentPath, "", nextToken.id()));
            if (inArray) {
                arrayIndex++;
            } else {
                return;
            }
        } else if (nextToken.isScalarValue()) {
            if (inArray) {
                currentPath = path + toArrayIndexPath(arrayIndex) + "/";
            }
            consumer.accept(JsonRecord.of(currentPath, jp.getValueAsString(), nextToken.id()));
            if (inArray) {
                arrayIndex++;
            } else {
                return;
            }
        } else if (nextToken == END_OBJECT) {
            if (inArray) {
                arrayIndex++;
            } else {
                return;
            }
        } else if (nextToken == START_ARRAY) {
            inArray = true;
        } else if (nextToken == END_ARRAY) {
            return;
        }
    }
}