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

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

Introduction

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

Prototype

public abstract String getText() throws IOException, JsonParseException;

Source Link

Document

Method for accessing textual representation of the current token; if no current token (before first call to #nextToken , or after encountering end-of-input), returns null.

Usage

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

/**
 * Private routine to deserialize monomer Store JSON. This is done manually to
 * give more freedom regarding data returned by the webservice.
 *
 * @param parser the JSONParser containing JSONData.
 * @param attachmentDB the attachments stored in the Toolkit
 * @return Map containing monomers//from   www .  ja v  a 2  s .  com
 *
 * @throws JsonParseException
 * @throws IOException
 * @throws EncoderException
 */
private Map<String, Monomer> deserializeMonomerStore(JsonParser parser, Map<String, Attachment> attachmentDB)
        throws JsonParseException, IOException, EncoderException {
    Map<String, Monomer> monomers = new HashMap<String, Monomer>();
    Monomer currentMonomer = null;

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

        if (JsonToken.START_OBJECT.equals(token)) {
            currentMonomer = new Monomer();
        } else if (JsonToken.END_OBJECT.equals(token)) {
            monomers.put(currentMonomer.getAlternateId(), currentMonomer);
        }

        if (fieldName != null) {
            switch (fieldName) {
            // id is first field
            case "id":
                parser.nextToken();
                currentMonomer.setId(Integer.parseInt(parser.getText()));
                break;
            case "alternateId":
                parser.nextToken();
                currentMonomer.setAlternateId(parser.getText());
                break;
            case "naturalAnalog":
                parser.nextToken();
                currentMonomer.setNaturalAnalog(parser.getText());
                break;
            case "name":
                parser.nextToken();
                currentMonomer.setName(parser.getText());
                break;
            case "canSMILES":
                parser.nextToken();
                currentMonomer.setCanSMILES(parser.getText());
                break;
            case "molfile":
                parser.nextToken();
                currentMonomer.setMolfile(MolfileEncoder.decode(parser.getText()));
                break;
            case "monomerType":
                parser.nextToken();
                currentMonomer.setMonomerType(parser.getText());
                break;
            case "polymerType":
                parser.nextToken();
                currentMonomer.setPolymerType(parser.getText());
                break;
            case "attachmentList":
                currentMonomer.setAttachmentList(deserializeAttachmentList(parser, attachmentDB));
                break;
            case "newMonomer":
                parser.nextToken();
                currentMonomer.setNewMonomer(Boolean.parseBoolean(parser.getText()));
                break;
            case "adHocMonomer":
                parser.nextToken();
                currentMonomer.setAdHocMonomer(Boolean.parseBoolean(parser.getText()));
                break;
            default:
                break;
            }
        }
        parser.nextToken();
    }

    return monomers;
}

From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java

List<Ancestry> parseSubPopulation(final String id, final List<Ancestry> ancestries, final JsonParser parser)
        throws IOException {
    String label = null;//from w ww  .  j av a 2  s .  com
    double proportion = 0.0d;
    double unassigned = 0.0d;
    List<Ancestry> subPopulations = new ArrayList<Ancestry>();
    while (parser.nextToken() != JsonToken.END_ARRAY) {
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String field = parser.getCurrentName();
            parser.nextToken();

            if ("label".equals(field)) {
                label = parser.getText();
            } else if ("proportion".equals(field)) {
                proportion = Double.parseDouble(parser.getText());
            } else if ("unassigned".equals(field)) {
                unassigned = Double.parseDouble(parser.getText());
            } else if ("sub_populations".equals(field)) {
                subPopulations = parseSubPopulation(id, subPopulations, parser);
            }
        }
        Ancestry ancestry = new Ancestry(id, label, proportion, unassigned, subPopulations);
        ancestries.add(ancestry);
        label = null;
        proportion = 0.0d;
        unassigned = 0.0d;
        subPopulations.clear();
    }
    return ancestries;
}

From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java

/**
 * Read the first feature to create the table
 * @param jp/*w  ww  . j  a v a  2  s  .  com*/
 */
private void readFeatures(JsonParser jp, String geomType, StringBuilder metadataBuilder)
        throws IOException, SQLException {
    jp.nextToken(); // START_ARRAY [
    JsonToken token = jp.nextToken(); // START_OBJECT {
    if (token != JsonToken.END_ARRAY) {
        jp.nextToken(); // FIELD_NAME type"name"
        jp.nextToken(); // VALUE_STRING Feature
        geomType = jp.getText();
        if (geomType.equalsIgnoreCase(GeoJsonField.FEATURE)) {
            jp.nextToken(); // FIELD_NAME geometry
            String firstField = jp.getText();
            if (firstField.equalsIgnoreCase(GeoJsonField.GEOMETRY)) {
                parseGeometryMetadata(jp, metadataBuilder);
                hasGeometryField = true;
                fieldIndex++;
                jp.nextToken();//END_OBJECT } geometry
            } else if (firstField.equalsIgnoreCase(GeoJsonField.PROPERTIES)) {
                fieldIndex = parseMetadataProperties(jp, metadataBuilder, fieldIndex);
                hasProperties = true;
            }
            // If there is only one geometry field in the feature them the next
            // token corresponds to the end object of the feature.
            jp.nextToken();
            if (jp.getCurrentToken() != JsonToken.END_OBJECT) {
                String secondParam = jp.getText();
                if (secondParam.equalsIgnoreCase(GeoJsonField.GEOMETRY)) {
                    parseGeometryMetadata(jp, metadataBuilder);
                    hasGeometryField = true;
                    fieldIndex++;
                    jp.nextToken();//END_OBJECT } geometry;
                } else if (secondParam.equalsIgnoreCase(GeoJsonField.PROPERTIES)) {
                    fieldIndex = parseMetadataProperties(jp, metadataBuilder, fieldIndex);
                    hasProperties = true;
                }
                jp.nextToken(); //END_OBJECT } feature
            }
            if (!hasProperties) {
                metadataBuilder.append("ID INT, PRIMARY KEY (ID)");
                fieldIndex++;
            }
            metadataBuilder.append(")");
        } else {
            throw new SQLException("Malformed GeoJSON file. Expected 'Feature', found '" + geomType + "'");
        }
    }
}

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

protected void readReqImageField(JsonParser par, NativeRequest.Asset.Image.Builder image, String fieldName)
        throws IOException {
    switch (fieldName) {
    case "type": {
        ImageAssetType value = ImageAssetType.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            image.setType(value);/*from  w ww .  j a  v a2s .  co m*/
        }
    }
        break;
    case "w":
        image.setW(par.getIntValue());
        break;
    case "h":
        image.setH(par.getIntValue());
        break;
    case "wmin":
        image.setWmin(par.getIntValue());
        break;
    case "hmin":
        image.setHmin(par.getIntValue());
        break;
    case "mimes":
        for (startArray(par); endArray(par); par.nextToken()) {
            image.addMimes(par.getText());
        }
        break;
    default:
        readOther(image, par, fieldName);
    }
}

From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java

/**
 * Parses the first GeoJSON feature to create the PreparedStatement.
 *
 * @throws SQLException/* ww  w  . j  a  v  a  2  s. c  om*/
 * @throws IOException
 */
private boolean parseMetadata() throws SQLException, IOException {
    FileInputStream fis = null;
    StringBuilder metadataBuilder = new StringBuilder();
    try {
        fis = new FileInputStream(fileName);
        this.fc = fis.getChannel();
        this.fileSize = fc.size();
        // Given the file size and an average node file size.
        // Skip how many nodes in order to update progression at a step of 1%
        readFileSizeEachNode = Math.max(1, (this.fileSize / AVERAGE_NODE_SIZE) / 100);
        nodeCountProgress = 0;

        JsonParser jp = jsFactory.createParser(fis);
        metadataBuilder.append("CREATE TABLE ");
        metadataBuilder.append(tableLocation);
        metadataBuilder.append(" (");

        jp.nextToken();//START_OBJECT
        jp.nextToken(); // field_name (type)
        jp.nextToken(); // value_string (FeatureCollection)
        String geomType = jp.getText();

        if (geomType.equalsIgnoreCase(GeoJsonField.FEATURECOLLECTION)) {
            jp.nextToken(); // FIELD_NAME features
            String firstParam = jp.getText();
            //Read the CRS
            if (firstParam.equalsIgnoreCase(GeoJsonField.CRS)) {
                parsedSRID = readCRS(jp);
                readFeatures(jp, geomType, metadataBuilder);
            } else if (firstParam.equalsIgnoreCase(GeoJsonField.FEATURES)) {
                readFeatures(jp, geomType, metadataBuilder);
            } else {
                throw new SQLException(
                        "Malformed GeoJSON file. Expected 'features', found '" + firstParam + "'");
            }
        } else {
            throw new SQLException(
                    "Malformed GeoJSON file. Expected 'FeatureCollection', found '" + geomType + "'");
        }
        jp.close();
    } catch (FileNotFoundException ex) {
        throw new SQLException(ex);

    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            throw new IOException(ex);
        }
    }

    // Now we create the table if there is at leat one geometry field.
    if (hasGeometryField) {
        Statement stmt = connection.createStatement();
        stmt.execute(metadataBuilder.toString());
        stmt.close();

        if (fieldIndex > 0) {
            StringBuilder insert = new StringBuilder("INSERT INTO ").append(tableLocation)
                    .append(" VALUES ( ?");
            for (int i = 1; i < fieldIndex; i++) {
                insert.append(",?");
            }
            insert.append(");");
            preparedStatement = connection.prepareStatement(insert.toString());
            return true;
        }
    } else {
        throw new SQLException("The first feature must contains a geometry field.");
    }
    return false;
}

From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java

/**
 * Parse the specified input stream and return a user.
 *
 * @param inputStream input stream/*from   w  ww.j  a  va 2 s .com*/
 * @return the specified input stream parsed into a user
 */
@Override
public User parseUser(final InputStream inputStream) {
    checkNotNull(inputStream);
    JsonParser parser = null;
    try {
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String id = null;
        String profileId = null;
        boolean genotyped = false;
        List<Profile> profiles = new ArrayList<Profile>();

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String field = parser.getCurrentName();
            parser.nextToken();
            if ("id".equals(field)) {
                id = parser.getText();
            } else if ("profiles".equals(field)) {
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        String profileField = parser.getCurrentName();
                        parser.nextToken();
                        if ("id".equals(profileField)) {
                            profileId = parser.getText();
                        } else if ("genotyped".equals(profileField)) {
                            genotyped = parser.getBooleanValue();
                        }
                    }
                    profiles.add(new Profile(profileId, genotyped));
                }
            }
        }
        return new User(id, profiles);
    } catch (IOException e) {
        logger.warn("could not parse user", e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
    return null;
}

From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java

@Override
public Genotype parseGenotypes(final InputStream inputStream) {
    checkNotNull(inputStream);/*from  w  w w. ja  va 2 s  . c  o m*/
    JsonParser parser = null;
    try {
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String id = null;
        String location = null;
        String interpretation = null;
        Map<String, String> values = new HashMap<String, String>();

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

            if ("id".equals(field)) {
                id = parser.getText();
            } else {
                location = field;
                interpretation = parser.getText();
                values.put(location, interpretation);
            }
        }
        return new Genotype(id, values);
    } catch (IOException e) {
        logger.warn("could not parse genotypes");
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
    return null;
}

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

/**
 * Private routine to deserialize a JSON containing attachment data. This is
 * done manually to give more freedom regarding data returned by the
 * webservice.//from   w  ww . j a  v a 2s.c o  m
 *
 * @param parser the JSONParser containing JSONData.
 * @param attachmentDB the attachments stored in the Toolkit
 * @return List containing attachments
 *
 * @throws JsonParseException
 * @throws IOException
 */
private List<Attachment> deserializeAttachmentList(JsonParser parser, Map<String, Attachment> attachmentDB)
        throws JsonParseException, IOException {
    List<Attachment> attachments = new ArrayList<Attachment>();
    Attachment currentAttachment = null;

    while (!JsonToken.END_ARRAY.equals(parser.nextToken())) {

        String fieldName = parser.getCurrentName();
        JsonToken token = parser.getCurrentToken();

        if (JsonToken.START_OBJECT.equals(token)) {
            currentAttachment = new Attachment();
        } else if (JsonToken.END_OBJECT.equals(token)) {
            currentAttachment.setCapGroupSMILES(
                    attachmentDB.get(currentAttachment.getAlternateId()).getCapGroupSMILES());
            attachments.add(currentAttachment);
        }

        if (fieldName != null) {
            switch (fieldName) {
            case "id":
                parser.nextToken();
                currentAttachment.setId(Integer.parseInt(parser.getText()));
                break;
            case "alternateId":
                parser.nextToken();
                currentAttachment.setAlternateId(parser.getText());
                break;
            case "label":
                parser.nextToken();
                currentAttachment.setLabel(parser.getText());
                break;
            case "capGroupName":
                parser.nextToken();
                currentAttachment.setCapGroupName(parser.getText());
                break;
            case "capGroupSMILES":
                parser.nextToken();
                currentAttachment.setCapGroupSMILES(parser.getText());
                break;
            default:
                break;
            }
        }

    }

    return attachments;
}

From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java

/**
 * We skip the CRS because it has been already parsed.
 * /* www  . j a v  a2 s  .  c o m*/
 *
 * @param jp
 */
private String skipCRS(JsonParser jp) throws IOException {
    jp.nextToken(); //START_OBJECT {
    jp.skipChildren();
    /* jp.nextToken();// crs type
     jp.nextToken(); // crs name
     jp.nextToken(); // crs properties
     jp.nextToken(); //START_OBJECT {
     jp.nextToken(); // crs name
     jp.nextToken(); // crs value
     jp.nextToken(); //END_OBJECT }
     jp.nextToken(); //END_OBJECT }*/
    jp.nextToken(); //Go to features
    return jp.getText();
}

From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java

@Override
public List<Carrier> parseCarriers(final InputStream inputStream) {
    checkNotNull(inputStream);/*from  w  w w .  j a  va 2 s .  co m*/
    JsonParser parser = null;
    try {
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String id = null;
        String reportId = null;
        String description = null;
        int mutations = 0;
        List<Carrier> carriers = new ArrayList<Carrier>();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String field = parser.getCurrentName();
            parser.nextToken();

            if ("id".equals(field)) {
                id = parser.getText();
            } else if ("carriers".equals(field)) {
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        String carrierField = parser.getCurrentName();
                        parser.nextToken();

                        if ("report_id".equals(carrierField)) {
                            reportId = parser.getText();
                        } else if ("description".equals(carrierField)) {
                            description = parser.getText();
                        } else if ("mutations".equals(carrierField)) {
                            mutations = Integer.parseInt(parser.getText());
                        }
                    }
                    carriers.add(new Carrier(id, reportId, description, mutations));
                    reportId = null;
                    description = null;
                    mutations = 0;
                }
            }
        }
        return carriers;
    } catch (IOException e) {
        logger.warn("could not parse carriers", e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
    return null;
}