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.tanrabad.survey.service.json.MultiPolygonTypeConverter.java

private List<Location> getChildPolygon(JsonParser jsonParser) throws IOException {
    List<Location> eachChildPolygon;
    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
        eachChildPolygon = new ArrayList<>();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            eachChildPolygon.add(getLocation(jsonParser));
        }/*from w  w  w . j  a  va  2 s .c  o m*/
    } else {
        eachChildPolygon = null;
    }
    return eachChildPolygon;
}

From source file:com.reprezen.swagedit.model.NodeDeserializer.java

@Override
public AbstractNode deserialize(JsonParser p, DeserializationContext context)
        throws IOException, JsonProcessingException {

    JsonLocation startLocation = p.getTokenLocation();
    if (p.getCurrentToken() == JsonToken.FIELD_NAME) {
        p.nextToken();
    }//  ww w  .  j  a va 2s .  co  m

    switch (p.getCurrentToken()) {
    case START_OBJECT:
        return deserializeObjectNode(p, context, startLocation);
    case START_ARRAY:
        return deserializeArrayNode(p, context, startLocation);
    default:
        return deserializeValueNode(p, context, startLocation);
    }
}

From source file:org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService.java

/**
 * Helper method to decode an array recusrively.
 *
 * @param parser the JSON parser with the content.
 * @param target the target where the content should be stored.
 *
 * @throws IOException//  w w  w . j  av  a 2s. c om
 * @returns the decoded list.
 */
private CouchbaseList decodeArray(final JsonParser parser, final CouchbaseList target) throws IOException {
    JsonToken currentToken = parser.nextToken();

    while (currentToken != null && currentToken != JsonToken.END_ARRAY) {
        if (currentToken == JsonToken.START_OBJECT) {
            target.put(decodeObject(parser, new CouchbaseDocument()));
        } else if (currentToken == JsonToken.START_ARRAY) {
            target.put(decodeArray(parser, new CouchbaseList()));
        } else {
            target.put(decodePrimitive(currentToken, parser));
        }

        currentToken = parser.nextToken();
    }

    return target;
}

From source file:com.joliciel.jochre.search.highlight.Snippet.java

public Snippet(String json) {
    try {/*w  ww.  j av  a 2 s.  c om*/
        Reader reader = new StringReader(json);
        JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory 
        JsonParser jsonParser = jsonFactory.createJsonParser(reader);
        jsonParser.nextToken();
        this.read(jsonParser);
    } catch (IOException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    }
}

From source file:com.sdl.odata.renderer.json.writer.JsonPropertyWriterTest.java

private Map<String, String> getJsonObject(JsonParser jsonParser) throws IOException {
    Map<String, String> map = new HashMap<>();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        String key = jsonParser.getText();
        jsonParser.nextToken();//  ww w. ja v a  2s . com
        map.put(key, jsonParser.getText());
    }
    return map;
}

From source file:com.sdl.odata.renderer.json.writer.JsonPropertyWriterTest.java

private List<Object> getJsonArray(JsonParser jsonParser) throws IOException {
    List<Object> objects = new ArrayList<>();
    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
            Map<String, String> jsonObject = getJsonObject(jsonParser);
            objects.add(jsonObject);//from  w  w w. j av  a 2  s . c  o  m
        } else {
            objects.add(jsonParser.getText());
        }
    }
    return objects;
}

From source file:invar.lib.data.DataParserJson.java

private void parse(JsonParser parser, DataNode root) throws IOException {
    String fieldName = null;//from   ww  w.ja va2s . co  m
    DataNode parent = root;
    while (!parser.isClosed()) {
        JsonToken token = parser.nextToken();
        if (token == null) {
            continue;
        }
        switch (token) {
        case START_ARRAY:
            parent.addChild(parent = DataNode.createArray().setFieldName(fieldName));
            fieldName = null;
            break;
        case END_ARRAY:
            parent = parent.getParent();
            fieldName = null;
            break;
        case START_OBJECT:
            parent.addChild(parent = DataNode.createObject().setFieldName(fieldName));
            fieldName = null;
            break;
        case END_OBJECT:
            parent = parent.getParent();
            fieldName = null;
            break;
        case VALUE_TRUE:
            parent.addChild(DataNode.createBoolean().setValue(true).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_FALSE:
            parent.addChild(DataNode.createBoolean().setValue(false).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_NULL:
            parent.addChild(DataNode.createNull().setValue(null).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_STRING:
            parent.addChild(
                    DataNode.createString().setFieldName(fieldName).setValue(parser.getValueAsString()));
            fieldName = null;
            break;
        case VALUE_NUMBER_INT:
            try {
                Long v = parser.getValueAsLong();
                parent.addChild(DataNode.createLong().setFieldName(fieldName).setValue(v));
            } catch (JsonParseException e) {
                BigInteger v = parser.getBigIntegerValue();
                parent.addChild(DataNode.createBigInt().setFieldName(fieldName).setValue(v));
            }
            fieldName = null;
            break;
        case VALUE_NUMBER_FLOAT:
            parent.addChild(
                    DataNode.createDouble().setFieldName(fieldName).setValue(parser.getValueAsDouble()));
            fieldName = null;
            break;
        case FIELD_NAME:
            fieldName = parser.getValueAsString();
            break;
        default:
            fieldName = null;
            break;
        }
    }
}

From source file:com.cedarsoft.serialization.jackson.IgnoringSerializerTest.java

@Theory
public void testIt(@Nonnull String json) throws Exception {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    JsonParser parser = jsonFactory.createParser(new ByteArrayInputStream(json.getBytes()));

    Void result = serializer.deserialize(parser, Version.valueOf(1, 0, 0)); //we use the override stuff to avoid version/type check
    assertThat(result).isNull();//  w  w w. j  a  va  2s. co  m

    JsonToken nextToken = parser.nextToken();
    assertThat(nextToken).isNull();
}

From source file:org.fluentd.jvmwatcher.JvmWatcher.java

/**
 * @param parser/*  www .  ja  v  a  2  s  .  c o  m*/
 * @throws JsonParseException
 * @throws IOException
 */
private void loadTarget(JsonParser parser) throws JsonParseException, IOException {
    if (parser.nextToken() == JsonToken.START_ARRAY) {
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                String shortName = null;
                String pattern = null;
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if ((parser.getCurrentToken() == JsonToken.FIELD_NAME)
                            && (parser.getText().compareTo("shortname") == 0)) {
                        if (parser.nextToken() == JsonToken.VALUE_STRING) {
                            shortName = parser.getText();
                        }
                    }
                    if ((parser.getCurrentToken() == JsonToken.FIELD_NAME)
                            && (parser.getText().compareTo("pattern") == 0)) {
                        if (parser.nextToken() == JsonToken.VALUE_STRING) {
                            pattern = parser.getText();
                        }
                    }
                }
                // add target pattern
                Pattern regexPattern = Pattern.compile(pattern);
                LocalJvmInfo.addTargetProcessPattern(shortName, regexPattern);
            }
        }
    }
}