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.apache.lucene.server.handlers.AddDocumentHandler.java

/** Parse one value for a field, which is either an
 *  object matching the type of the field, or a {boost:
 *  ..., value: ...}. *///from  ww w .  j  av  a  2  s  .  com
private static boolean parseOneValue(FieldDef fd, JsonParser p, Document doc) throws IOException {

    Object o = null;
    float boost = 1.0f;

    JsonToken token = p.nextToken();
    if (token == JsonToken.START_ARRAY) {
        if ("hierarchy".equals(fd.faceted) || fd.valueType == FieldDef.FieldValueType.LAT_LON) {
            o = getNativeValue(fd, token, p);
        } else {
            if (fd.multiValued == false) {
                fail(fd.name, "expected single value, not array, since this field is not multiValued");
            }
            while (true) {
                if (!parseOneValue(fd, p, doc)) {
                    break;
                }
            }
            return true;
        }
    } else {

        if (token == JsonToken.END_ARRAY) {
            assert fd.multiValued;
            return false;
        }

        if (fd.fieldType.indexOptions() != IndexOptions.NONE && token == JsonToken.START_OBJECT) {
            // Parse a {boost: X, value: Y}
            while (true) {
                token = p.nextToken();
                if (token == JsonToken.END_OBJECT) {
                    break;
                }
                assert token == JsonToken.FIELD_NAME;
                String key = p.getText();
                if (key.equals("boost")) {
                    token = p.nextToken();
                    if (token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT) {
                        boost = p.getFloatValue();
                    } else {
                        fail(fd.name, "boost in inner object field value must have float or int value; got: "
                                + token);
                    }
                } else if (key.equals("value")) {
                    o = getNativeValue(fd, p.nextToken(), p);
                } else {
                    fail(fd.name, "unrecognized json key \"" + key
                            + "\" in inner object field value; must be boost or value");
                }
            }
            if (o == null) {
                fail(fd.name, "missing 'value' key");
            }
        } else {
            // Parse a native value:
            o = getNativeValue(fd, token, p);
        }
    }

    parseOneNativeValue(fd, doc, o, boost);
    return true;
}

From source file:org.apache.manifoldcf.agents.output.amazoncloudsearch.AmazonCloudSearchConnector.java

private String getStatusFromJsonResponse(String responsbody) throws ManifoldCFException {
    try {//from   w ww.j ava2 s. c  o  m
        JsonParser parser = new JsonFactory().createJsonParser(responsbody);
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            if ("status".equalsIgnoreCase(name)) {
                parser.nextToken();
                return parser.getText();
            }
        }
    } catch (JsonParseException e) {
        throw new ManifoldCFException(e);
    } catch (IOException e) {
        throw new ManifoldCFException(e);
    }
    return null;
}

From source file:com.tlongdev.bktf.interactor.TlongdevPriceListInteractor.java

private int parseJson(InputStream inputStream) throws IOException {
    //Create a parser from the input stream for fast parsing and low impact on memory
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(inputStream);

    Vector<ContentValues> cVVector = new Vector<>();
    int retVal = 0;
    int count = 0;

    //Not a JSON if it doesn't start with START OBJECT
    if (parser.nextToken() != JsonToken.START_OBJECT) {
        return -1;
    }/*  w w  w  . ja  v a  2  s .c om*/

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

        switch (name) {
        case "success":
            if (parser.getIntValue() == 0) {
                retVal = 1;
            }
            break;
        case "message":
            errorMessage = parser.getText();
            break;
        case "count":
            count = parser.getIntValue();
            break;
        case "prices":

            while (parser.nextToken() != JsonToken.END_ARRAY) {
                ContentValues values = buildContentValues(parser);
                cVVector.add(values);
            }

            if (cVVector.size() > 0) {
                ContentValues[] cvArray = new ContentValues[cVVector.size()];
                cVVector.toArray(cvArray);
                //Insert all the data into the database
                rowsInserted = mContext.getContentResolver().bulkInsert(PriceEntry.CONTENT_URI, cvArray);
                Log.v(LOG_TAG, "inserted " + rowsInserted + " rows into prices table");
            }
            break;
        }
    }

    parser.close();

    return retVal;
}

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

/**
 * Parses one position/*from  w w w. j a va2s.c o m*/
 *
 * Syntax:
 *
 * { "type": "Point", "coordinates": [100.0, 0.0] }
 *
 * @param jsParser
 * @throws IOException
 * @return Point
 */
private Point parsePoint(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME coordinates        
    String coordinatesField = jp.getText();
    if (coordinatesField.equalsIgnoreCase(GeoJsonField.COORDINATES)) {
        jp.nextToken(); // START_ARRAY [ to parse the coordinate
        Point point = GF.createPoint(parseCoordinate(jp));
        return point;
    } else {
        throw new SQLException(
                "Malformed GeoJSON file. Expected 'coordinates', found '" + coordinatesField + "'");
    }
}

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

/**
 * Features in GeoJSON contain a geometry object and additional properties
 *
 * Syntax:/*from  w ww . ja va  2s  .  c o  m*/
 *
 * { "type": "Feature", "geometry":{"type": "Point", "coordinates": [102.0,
 * 0.5]}, "properties": {"prop0": "value0"} }
 *
 * @param jsParser
 */
private void parseFeature(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME geometry
    String firstField = jp.getText();
    fieldIndex = 1;
    if (firstField.equalsIgnoreCase(GeoJsonField.GEOMETRY)) {
        jp.nextToken(); //START_OBJECT {
        getPreparedStatement().setObject(fieldIndex, parseGeometry(jp));
        fieldIndex++;
    } else if (firstField.equalsIgnoreCase(GeoJsonField.PROPERTIES)) {
        parseProperties(jp, fieldIndex);
    }
    //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();// field name
        if (secondParam.equalsIgnoreCase(GeoJsonField.GEOMETRY)) {
            jp.nextToken(); //START_OBJECT {
            getPreparedStatement().setObject(fieldIndex, parseGeometry(jp));
            fieldIndex++;
        } else if (secondParam.equalsIgnoreCase(GeoJsonField.PROPERTIES)) {
            parseProperties(jp, fieldIndex);
        }
        jp.nextToken(); //END_OBJECT } feature
    }
    if (!hasProperties) {
        getPreparedStatement().setObject(fieldIndex, featureCounter);
    }
    getPreparedStatement().execute();
}

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

/**
 * Parses an array of positions/*from   ww  w.  j  a va  2 s  . c  o  m*/
 *
 * Syntax:
 *
 * { "type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
 *
 * @param jsParser
 * @throws IOException
 * @return MultiPoint
 */
private MultiPoint parseMultiPoint(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME coordinates        
    String coordinatesField = jp.getText();
    if (coordinatesField.equalsIgnoreCase(GeoJsonField.COORDINATES)) {
        jp.nextToken(); // START_ARRAY [ coordinates
        MultiPoint mPoint = GF.createMultiPoint(parseCoordinates(jp));
        jp.nextToken();//END_OBJECT } geometry
        return mPoint;
    } else {
        throw new SQLException(
                "Malformed GeoJSON file. Expected 'coordinates', found '" + coordinatesField + "'");
    }
}

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

/**
 *
 * Parse the array of positions.// ww w.jav a  2  s .c  o  m
 *
 * Syntax:
 *
 * { "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
 *
 * @param jsParser
 */
private LineString parseLinestring(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME coordinates        
    String coordinatesField = jp.getText();
    if (coordinatesField.equalsIgnoreCase(GeoJsonField.COORDINATES)) {
        jp.nextToken(); // START_ARRAY [ coordinates
        LineString line = GF.createLineString(parseCoordinates(jp));
        jp.nextToken();//END_OBJECT } geometry
        return line;
    } else {
        throw new SQLException(
                "Malformed GeoJSON file. Expected 'coordinates', found '" + coordinatesField + "'");
    }
}

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

/**
 * Each element in the geometries array of a GeometryCollection is one of
 * the geometry objects described above:
 *
 * { "type": "GeometryCollection", "geometries": [ { "type": "Point",
 * "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [
 * [101.0, 0.0], [102.0, 1.0] ] } ]//  w  w w .  j  ava2  s .c  o  m
 *
 * @param jp
 *
 * @throws IOException
 * @throws SQLException
 * @return GeometryCollection
 */
private GeometryCollection parseGeometryCollection(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME geometries        
    String coordinatesField = jp.getText();
    if (coordinatesField.equalsIgnoreCase(GeoJsonField.GEOMETRIES)) {
        jp.nextToken();//START array
        jp.nextToken();//START object
        ArrayList<Geometry> geometries = new ArrayList<Geometry>();
        while (jp.getCurrentToken() != JsonToken.END_ARRAY) {
            geometries.add(parseGeometry(jp));
            jp.nextToken();
        }
        jp.nextToken();//END_OBJECT } geometry
        return GF.createGeometryCollection(geometries.toArray(new Geometry[geometries.size()]));
    } else {
        throw new SQLException(
                "Malformed GeoJSON file. Expected 'geometries', found '" + coordinatesField + "'");
    }

}

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

/**
 * Parses the featureCollection/*from w w w  .j ava  2 s .c om*/
 *
 * @param jp
 * @throws IOException
 * @throws SQLException
 */
private void parseFeatures(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME features
    String firstParam = jp.getText();
    if (firstParam.equalsIgnoreCase(GeoJsonField.CRS)) {
        firstParam = skipCRS(jp);
    }
    if (firstParam.equalsIgnoreCase(GeoJsonField.FEATURES)) {
        jp.nextToken(); // START_ARRAY [
        JsonToken token = jp.nextToken(); // START_OBJECT {
        while (token != JsonToken.END_ARRAY) {
            jp.nextToken(); // FIELD_NAME type
            jp.nextToken(); // VALUE_STRING Feature
            String geomType = jp.getText();
            if (geomType.equalsIgnoreCase(GeoJsonField.FEATURE)) {
                if (progress.isCanceled()) {
                    throw new SQLException("Canceled by user");
                }
                parseFeature(jp);
                token = jp.nextToken(); //START_OBJECT new feature                    
                featureCounter++;
                if (nodeCountProgress++ % readFileSizeEachNode == 0) {
                    // Update Progress
                    try {
                        progress.setStep((int) (((double) fc.position() / fileSize) * 100));
                    } catch (IOException ex) {
                        // Ignore
                    }
                }
            } else {
                throw new SQLException("Malformed GeoJSON file. Expected 'Feature', found '" + geomType + "'");
            }
        }
        //LOOP END_ARRAY ]
    } else {
        throw new SQLException("Malformed GeoJSON file. Expected 'features', found '" + firstParam + "'");
    }
}

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

/**
 * Parses an array of positions defined as:
 *
 * { "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0,
 * 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }
 *
 * @param jsParser/*w  w  w . j  a  v  a 2s  .  com*/
 * @return MultiLineString
 */
private MultiLineString parseMultiLinestring(JsonParser jp) throws IOException, SQLException {
    jp.nextToken(); // FIELD_NAME coordinates        
    String coordinatesField = jp.getText();
    if (coordinatesField.equalsIgnoreCase(GeoJsonField.COORDINATES)) {
        ArrayList<LineString> lineStrings = new ArrayList<LineString>();
        jp.nextToken();//START_ARRAY [ coordinates
        jp.nextToken(); // START_ARRAY [ coordinates line
        while (jp.getCurrentToken() != JsonToken.END_ARRAY) {
            lineStrings.add(GF.createLineString(parseCoordinates(jp)));
            jp.nextToken();
        }
        MultiLineString line = GF
                .createMultiLineString(lineStrings.toArray(new LineString[lineStrings.size()]));
        jp.nextToken();//END_OBJECT } geometry
        return line;
    } else {
        throw new SQLException(
                "Malformed GeoJSON file. Expected 'coordinates', found '" + coordinatesField + "'");
    }

}