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.mayocat.jackson.OptionalStringListDeserializer.java

@Override
public Optional<List<String>> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (jp.getCurrentToken().isScalarValue()) {
        // value is only one string
        return Optional.of(Arrays.asList(jp.getValueAsString()));
    } else {// w w w  . j  a  va2  s  . com
        // Value is a list of strings
        List<String> result = Lists.newArrayList();
        while (jp.nextToken().isScalarValue()) {
            String value = jp.getValueAsString();
            result.add(value);
        }
        return Optional.of(result);
    }
}

From source file:com.cedarsoft.couchdb.io.ActionFailedExceptionSerializer.java

@Nonnull
public ActionFailedException deserialize(int status, @Nonnull InputStream in)
        throws VersionException, IOException {
    try (MaxLengthByteArrayOutputStream teedOut = new MaxLengthByteArrayOutputStream();
            TeeInputStream teeInputStream = new TeeInputStream(in, teedOut)) {

        JsonFactory jsonFactory = JacksonSupport.getJsonFactory();

        JsonParser parser = jsonFactory.createJsonParser(teeInputStream);
        JacksonParserWrapper parserWrapper = new JacksonParserWrapper(parser);

        parserWrapper.nextToken(JsonToken.START_OBJECT);

        String error = null;//from  w w  w  .  j a  v  a2  s. com
        String reason = null;

        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            String currentName = parser.getCurrentName();

            if (currentName.equals(PROPERTY_ERROR)) {
                parserWrapper.nextToken(JsonToken.VALUE_STRING);
                error = parser.getText();
                continue;
            }

            if (currentName.equals(PROPERTY_REASON)) {
                parserWrapper.nextToken(JsonToken.VALUE_STRING);
                reason = parser.getText();
                continue;
            }

            throw new IllegalStateException("Unexpected field reached <" + currentName + ">");
        }

        parserWrapper.verifyDeserialized(error, PROPERTY_ERROR);
        parserWrapper.verifyDeserialized(reason, PROPERTY_REASON);
        assert reason != null;
        assert error != null;

        parserWrapper.ensureObjectClosed();

        return new ActionFailedException(status, error, reason, teedOut.toByteArray());
    }
}

From source file:com.cinnober.msgcodec.json.JsonCodec.java

@Override
public Object decode(InputStream in) throws IOException {
    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(in);
    JsonToken token = p.nextToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new DecodeException("Expected {");
    }/*from  w ww .  ja  v  a2s .  co m*/
    return dynamicGroupHandler.readValue(p);
}

From source file:org.oscim.tiling.source.geojson.GeoJsonTileDecoder.java

private void parseMulti(JsonParser jp, GeometryType type) throws JsonParseException, IOException {

    for (JsonToken t; (t = jp.nextToken()) != null;) {
        if (t == END_ARRAY)
            break;

        if (t == START_ARRAY) {
            if (type == GeometryType.POLY)
                parsePolygon(jp);//w w w  .  ja v a2 s .  co  m

            else if (type == GeometryType.LINE)
                parseLineString(jp);

            else if (type == GeometryType.POINT)
                parseCoordinate(jp);
            ;

        } else {
            //....
        }
    }
}

From source file:net.floodlightcontroller.loadbalancer.MonitorsResource.java

protected LBMonitor jsonToMonitor(String json) throws IOException {
    MappingJsonFactory f = new MappingJsonFactory();
    JsonParser jp;
    LBMonitor monitor = new LBMonitor();

    try {/*from   ww w . j a v  a2 s. c  o m*/
        jp = f.createJsonParser(json);
    } catch (JsonParseException e) {
        throw new IOException(e);
    }

    jp.nextToken();
    if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
        throw new IOException("Expected START_OBJECT");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
            throw new IOException("Expected FIELD_NAME");
        }

        String n = jp.getCurrentName();
        jp.nextToken();
        if (jp.getText().equals(""))
            continue;
        else if (n.equals("monitor")) {
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String field = jp.getCurrentName();

                if (field.equals("id")) {
                    monitor.id = jp.getText();
                    continue;
                }
                if (field.equals("name")) {
                    monitor.name = jp.getText();
                    continue;
                }
                if (field.equals("type")) {
                    monitor.type = Short.parseShort(jp.getText());
                    continue;
                }
                if (field.equals("delay")) {
                    monitor.delay = Short.parseShort(jp.getText());
                    continue;
                }
                if (field.equals("timeout")) {
                    monitor.timeout = Short.parseShort(jp.getText());
                    continue;
                }
                if (field.equals("attempts_before_deactivation")) {
                    monitor.attemptsBeforeDeactivation = Short.parseShort(jp.getText());
                    continue;
                }
                if (field.equals("network_id")) {
                    monitor.netId = jp.getText();
                    continue;
                }
                if (field.equals("address")) {
                    monitor.address = Integer.parseInt(jp.getText());
                    continue;
                }
                if (field.equals("protocol")) {
                    monitor.protocol = Byte.parseByte(jp.getText());
                    continue;
                }
                if (field.equals("port")) {
                    monitor.port = Short.parseShort(jp.getText());
                    continue;
                }
                if (field.equals("admin_state")) {
                    monitor.adminState = Short.parseShort(jp.getText());
                    continue;
                }
                if (field.equals("status")) {
                    monitor.status = Short.parseShort(jp.getText());
                    continue;
                }

                log.warn("Unrecognized field {} in " + "parsing Vips", jp.getText());
            }
        }
    }
    jp.close();

    return monitor;
}

From source file:org.oscim.tiling.source.geojson.GeoJsonTileDecoder.java

@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is) throws IOException {
    mTileDataSink = sink;/*from   ww w. j  av  a  2s  .c  om*/
    mTileScale = 1 << tile.zoomLevel;
    mTileX = tile.tileX / mTileScale;
    mTileY = tile.tileY / mTileScale;
    mTileScale *= Tile.SIZE;

    JsonParser jp = mJsonFactory.createParser(new InputStreamReader(is));

    for (JsonToken t; (t = jp.nextToken()) != null;) {
        if (t == FIELD_NAME) {
            if (match(jp, FIELD_FEATURES)) {
                if (jp.nextToken() != START_ARRAY)
                    continue;

                while ((t = jp.nextToken()) != null) {
                    if (t == START_OBJECT)
                        parseFeature(jp);

                    if (t == END_ARRAY)
                        break;
                }
            }
        }
    }
    return true;
}

From source file:org.oscim.tiling.source.geojson.GeoJsonTileDecoder.java

private void parsePolygon(JsonParser jp) throws JsonParseException, IOException {
    int ring = 0;

    for (JsonToken t; (t = jp.nextToken()) != null;) {
        if (t == START_ARRAY) {
            if (ring == 0)
                mMapElement.startPolygon();
            else// w  w w  .ja v a  2  s  .  com
                mMapElement.startHole();

            ring++;
            parseCoordSequence(jp);
            removeLastPoint();
            continue;
        }

        if (t == END_ARRAY)
            break;
    }
}

From source file:com.cinnober.msgcodec.json.JsonCodec.java

/**
 * Read a static group from the specified stream, when the JSON does not contain the '$type' field.
 *
 * @param groupType the expected group type, not null.
 * @param in the stream to read from, not null.
 * @return the decoded value.//from   www  .j a  va2  s  .  c  o m
 * @throws IOException if the underlying stream throws an exception.
 * @throws DecodeException if the value could not be decoded, or if a required field is missing.
 */
public Object decodeStatic(Object groupType, InputStream in) throws IOException {
    StaticGroupHandler groupHandler = staticGroupsByGroupType.get(groupType);
    if (groupHandler == null) {
        throw new IllegalArgumentException("Unknown group type");
    }

    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(in);
    JsonToken token = p.nextToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new DecodeException("Expected {");
    }
    return groupHandler.readValue(p);
}

From source file:com.cinnober.msgcodec.json.JsonCodec.java

/**
 * Read a static group from the specified stream, when the JSON does not contain the '$type' field.
 *
 * @param groupName the expected group name, not null.
 * @param in the stream to read from, not null.
 * @return the decoded value.//from  w ww.  j  a  v a  2 s.c om
 * @throws IOException if the underlying stream throws an exception.
 * @throws DecodeException if the value could not be decoded, or if a required field is missing.
 */
public Object decodeStatic(String groupName, InputStream in) throws IOException {
    StaticGroupHandler groupHandler = lookupGroupByName(groupName);
    if (groupHandler == null) {
        throw new IllegalArgumentException("Unknown group name");
    }

    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(in);
    JsonToken token = p.nextToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new DecodeException("Expected {");
    }
    return groupHandler.readValue(p);
}

From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java

/** Parses the current json token into the corresponding
 *  java object. *//* w  w w  . j  av a 2s  .  c o  m*/
private static Object getNativeValue(FieldDef fd, JsonToken token, JsonParser p) throws IOException {
    Object o;
    if (token == JsonToken.VALUE_STRING) {
        o = p.getText();
    } else if (token == JsonToken.VALUE_NUMBER_INT) {
        o = Long.valueOf(p.getLongValue());
    } else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
        o = Double.valueOf(p.getDoubleValue());
    } else if (token == JsonToken.VALUE_TRUE) {
        o = Boolean.TRUE;
    } else if (token == JsonToken.VALUE_FALSE) {
        o = Boolean.FALSE;
    } else if (fd.faceted.equals("hierarchy") && token == JsonToken.START_ARRAY) {
        if (fd.multiValued == false) {
            List<String> values = new ArrayList<>();
            while (true) {
                token = p.nextToken();
                if (token == JsonToken.END_ARRAY) {
                    break;
                } else if (token != JsonToken.VALUE_STRING) {
                    if (token == JsonToken.START_ARRAY) {
                        fail(fd.name, "expected array of strings, but saw array inside array");
                    } else {
                        fail(fd.name, "expected array of strings, but saw " + token + " inside array");
                    }
                }
                values.add(p.getText());
            }
            o = values;
        } else {
            List<List<String>> values = new ArrayList<>();
            while (true) {
                token = p.nextToken();
                if (token == JsonToken.END_ARRAY) {
                    break;
                } else if (token == JsonToken.START_ARRAY) {
                    List<String> sub = new ArrayList<>();
                    values.add(sub);
                    while (true) {
                        token = p.nextToken();
                        if (token == JsonToken.VALUE_STRING) {
                            sub.add(p.getText());
                        } else if (token == JsonToken.END_ARRAY) {
                            break;
                        } else {
                            fail(fd.name, "expected array of strings or array of array of strings, but saw "
                                    + token + " inside inner array");
                        }
                    }
                } else if (token == JsonToken.VALUE_STRING) {
                    List<String> sub = new ArrayList<>();
                    values.add(sub);
                    sub.add(p.getText());
                } else if (token == JsonToken.START_ARRAY) {
                    fail(fd.name, "expected array of strings, but saw array inside array");
                } else {
                    fail(fd.name, "expected array of strings, but saw " + token + " inside array");
                }
            }
            o = values;
        }
    } else if (fd.valueType == FieldDef.FieldValueType.LAT_LON) {
        if (token != JsonToken.START_ARRAY) {
            fail(fd.name, "latlon field must be [lat, lon] value; got " + token);
        }
        double[] latLon = new double[2];
        token = p.nextToken();
        if (token != JsonToken.VALUE_NUMBER_FLOAT) {
            fail(fd.name, "latlon field must be [lat, lon] value; got " + token);
        }
        latLon[0] = p.getDoubleValue();
        token = p.nextToken();
        if (token != JsonToken.VALUE_NUMBER_FLOAT) {
            fail(fd.name, "latlon field must be [lat, lon] value; got " + token);
        }
        latLon[1] = p.getDoubleValue();
        token = p.nextToken();
        if (token != JsonToken.END_ARRAY) {
            fail(fd.name, "latlon field must be [lat, lon] value; got " + token);
        }
        o = latLon;
    } else {
        String message;
        if (token == JsonToken.VALUE_NULL) {
            message = "null field value not supported; just omit this field from the document instead";
        } else {
            message = "value in inner object field value should be string, int/long, float/double or boolean; got "
                    + token;
        }

        fail(fd.name, message);

        // Dead code but compiler disagrees:
        o = null;
    }
    return o;
}