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.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public void importQnaContent(final JsonParser jsonParser, final Resource resource,
        final ResourceResolver resolver) throws ServletException, IOException {
    if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        jsonParser.nextToken(); // advance to first key in the object - should be the id value of the old post
        while (jsonParser.getCurrentToken().equals(JsonToken.FIELD_NAME)) {
            extractTopic(jsonParser, resource, resolver, qnaForumOperations);
            jsonParser.nextToken(); // get the next token - presumably a field name for the next post
        }//ww  w .  jav  a  2 s  .  c  o  m
        jsonParser.nextToken(); // skip end token
    } else {
        throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public void importForumContent(final JsonParser jsonParser, final Resource resource,
        final ResourceResolver resolver) throws ServletException, IOException {
    if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        jsonParser.nextToken(); // advance to first key in the object - should be the id value of the old post
        while (jsonParser.getCurrentToken().equals(JsonToken.FIELD_NAME)) {
            extractTopic(jsonParser, resource, resolver, forumOperations);
            jsonParser.nextToken(); // get the next token - presumably a field name for the next post
        }//from  ww  w.  j av a 2 s .  c o m
        jsonParser.nextToken(); // skip end token
    } else {
        throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public void importCommentsContent(final JsonParser jsonParser, final Resource resource,
        final ResourceResolver resolver) throws ServletException, IOException {
    if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        jsonParser.nextToken(); // advance to first key in the object - should be the id value of the old post
        while (jsonParser.getCurrentToken().equals(JsonToken.FIELD_NAME)) {
            extractTopic(jsonParser, resource, resolver, commentOperations);
            jsonParser.nextToken(); // get the next token - presumably a field name for the next post
        }/*  w  w  w .  ja  va 2s  .c o  m*/
        jsonParser.nextToken(); // skip end token
    } else {
        throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

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

/**
 * Parses the metadata properties.//from  w w w  . ja  v a  2  s  . c om
 *
 * @param jp
 * @return index
 */
private int parseMetadataProperties(JsonParser jp, StringBuilder metadataBuilder, int fieldIndex)
        throws IOException {
    jp.nextToken();//START_OBJECT {
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getText().toUpperCase(); //FIELD_NAME columnName            
        JsonToken value = jp.nextToken();
        if (value == JsonToken.VALUE_STRING) {
            metadataBuilder.append(fieldName).append(" VARCHAR");
            fieldIndex++;
        } else if (value == JsonToken.VALUE_TRUE) {
            metadataBuilder.append(fieldName).append(" BOOLEAN");
            fieldIndex++;
        } else if (value == JsonToken.VALUE_FALSE) {
            metadataBuilder.append(fieldName).append(" BOOLEAN");
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NUMBER_FLOAT) {
            metadataBuilder.append(fieldName).append(" DOUBLE");
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NUMBER_INT) {
            metadataBuilder.append(fieldName).append(" INT");
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NULL) {
            metadataBuilder.append(fieldName).append(" VARCHAR");
            fieldIndex++;
        } else {
            // TODO: ignore value.
        }
        metadataBuilder.append(",");
    }
    return fieldIndex;
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public void importJournalContent(final JsonParser jsonParser, final Resource resource,
        final ResourceResolver resolver) throws IOException, ServletException {

    if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        jsonParser.nextToken(); // advance to first key in the object - should be the id value of the old post
        while (jsonParser.getCurrentToken().equals(JsonToken.FIELD_NAME)) {
            extractTopic(jsonParser, resource, resolver, journalOperations);
            jsonParser.nextToken(); // get the next token - presumably a field name for the next post
        }//ww  w.  j  av a 2s .c  o m
        jsonParser.nextToken(); // skip end token
    } else {
        throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got "
                + jsonParser.getCurrentToken().toString());
    }
}

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

/**
 * Parses geometry metadata.//from   w  w  w. j a  va  2  s  .c  o  m
 *
 * @param jp
 * @param metadataBuilder
 */
private void parseGeometryMetadata(JsonParser jp, StringBuilder metadataBuilder)
        throws IOException, SQLException {
    jp.nextToken(); //START_OBJECT {
    jp.nextToken(); // FIELD_NAME type     
    jp.nextToken(); //VALUE_STRING Point
    String geomType = jp.getText();
    if (geomType.equalsIgnoreCase(GeoJsonField.POINT)) {
        checkCoordinates(jp, metadataBuilder, GeoJsonField.POINT);
    } else if (geomType.equalsIgnoreCase(GeoJsonField.LINESTRING)) {
        checkCoordinates(jp, metadataBuilder, GeoJsonField.LINESTRING);
    } else if (geomType.equalsIgnoreCase(GeoJsonField.POLYGON)) {
        checkCoordinates(jp, metadataBuilder, GeoJsonField.POLYGON);
    } else if (geomType.equalsIgnoreCase(GeoJsonField.MULTIPOINT)) {
        checkCoordinates(jp, metadataBuilder, GeoJsonField.MULTIPOINT);
    } else if (geomType.equalsIgnoreCase(GeoJsonField.MULTILINESTRING)) {
        checkCoordinates(jp, metadataBuilder, GeoJsonField.MULTILINESTRING);
    } else if (geomType.equalsIgnoreCase(GeoJsonField.MULTIPOLYGON)) {
        checkCoordinates(jp, metadataBuilder, GeoJsonField.MULTIPOLYGON);
    } else if (geomType.equalsIgnoreCase(GeoJsonField.GEOMETRYCOLLECTION)) {
        jp.nextToken();//START geometries array
        if (jp.getText().equalsIgnoreCase(GeoJsonField.GEOMETRIES)) {
            jp.skipChildren();
            if (isH2) {
                metadataBuilder.append("THE_GEOM GEOMETRY,");
            } else {
                metadataBuilder.append("THE_GEOM GEOMETRY(geometry,").append(parsedSRID).append("),");
            }
        } else {
            throw new SQLException(
                    "Malformed GeoJSON file. Expected 'geometries', found '" + jp.getText() + "'");
        }
    } else {
        throw new SQLException("Unsupported geometry : " + geomType);
    }
}

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

/**
 * Check if the geometry is well formatted and contained an array of coordinates.
 * //from   www.  ja v  a2s  . c om
 * @param jp
 * @param metadataBuilder
 * @throws SQLException
 * @throws IOException 
 */
private void checkCoordinates(JsonParser jp, StringBuilder metadataBuilder, String geometryType)
        throws SQLException, IOException {
    jp.nextToken(); // FIELD_NAME coordinates
    if (jp.getText().equalsIgnoreCase(GeoJsonField.COORDINATES)) {
        jp.nextToken();//START coordinates array
        jp.skipChildren();
        if (isH2) {
            metadataBuilder.append("THE_GEOM ").append(geometryType).append(",");
        } else {
            metadataBuilder.append("THE_GEOM GEOMETRY(").append(geometryType).append(",").append(parsedSRID)
                    .append("),");
        }
    } else {
        throw new SQLException("Malformed GeoJSON file. Expected 'coordinates', found '" + jp.getText() + "'");
    }
}

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  w  w. j  a v  a 2  s  . c o  m*/
    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;
}