Example usage for com.fasterxml.jackson.core JsonToken START_ARRAY

List of usage examples for com.fasterxml.jackson.core JsonToken START_ARRAY

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonToken START_ARRAY.

Prototype

JsonToken START_ARRAY

To view the source code for com.fasterxml.jackson.core JsonToken START_ARRAY.

Click Source Link

Document

START_ARRAY is returned when encountering '[' which signals starting of an Array value

Usage

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

/**
 * Helper method to decode an object recursively.
 *
 * @param parser the JSON parser with the content.
 * @param target the target where the content should be stored.
 *
 * @throws IOException//from www  .j  av a 2s.  c o m
 * @returns the decoded object.
 */
private CouchbaseDocument decodeObject(final JsonParser parser, final CouchbaseDocument target)
        throws IOException {
    JsonToken currentToken = parser.nextToken();

    String fieldName = "";
    while (currentToken != null && currentToken != JsonToken.END_OBJECT) {
        if (currentToken == JsonToken.START_OBJECT) {
            target.put(fieldName, decodeObject(parser, new CouchbaseDocument()));
        } else if (currentToken == JsonToken.START_ARRAY) {
            target.put(fieldName, decodeArray(parser, new CouchbaseList()));
        } else if (currentToken == JsonToken.FIELD_NAME) {
            fieldName = parser.getCurrentName();
        } else {
            target.put(fieldName, decodePrimitive(currentToken, parser));
        }

        currentToken = parser.nextToken();
    }

    return target;
}

From source file:org.mongojack.internal.object.BsonObjectTraversingParser.java

@Override
public JsonToken nextToken() throws IOException {
    if (nextToken != null) {
        _currToken = nextToken;/*from  w w w.  java 2 s .c  om*/
        nextToken = null;
        return _currToken;
    }
    // are we to descend to a container child?
    if (startContainer) {
        startContainer = false;
        // minor optimization: empty containers can be skipped
        if (!nodeCursor.currentHasChildren()) {
            _currToken = (_currToken == JsonToken.START_OBJECT) ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
            return _currToken;
        }
        nodeCursor = nodeCursor.iterateChildren();
        _currToken = nodeCursor.nextToken();
        if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
            startContainer = true;
        }
        return _currToken;
    }
    // No more content?
    if (nodeCursor == null) {
        closed = true; // if not already set
        return null;
    }
    // Otherwise, next entry from currentFieldName cursor
    _currToken = nodeCursor.nextToken();
    if (_currToken != null) {
        if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
            startContainer = true;
        }
        return _currToken;
    }
    // null means no more children; need to return end marker
    _currToken = nodeCursor.endToken();
    nodeCursor = nodeCursor.getParent();
    return _currToken;
}

From source file:net.floodlightcontroller.cli.commands.ShowSwitchCmd.java

/**
 * Parses a JSON string and decomposes all JSON arrays and objects. Stores the
 * resulting strings in a nested Map of string objects.
 * /*w  w w  .j  a  va  2 s  .com*/
 * @param jsonString
 */
@SuppressWarnings("unchecked")
private List<Map<String, Object>> parseJson(String jsonString) throws IOException {
    /* The Jackson JSON parser. */
    JsonParser jp;
    /* The Jackson JSON factory. */
    JsonFactory f = new JsonFactory();
    /* The Jackson object mapper. */
    ObjectMapper mapper = new ObjectMapper();
    /* A list of JSON data objects retrieved by using the REST API. */
    List<Map<String, Object>> jsonData = new ArrayList<Map<String, Object>>();

    try {
        jp = f.createJsonParser(jsonString);
    } catch (JsonParseException e) {
        throw new IOException(e);
    }

    // Move to the first object in the array.
    jp.nextToken();
    if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
        throw new IOException("Expected START_ARRAY instead of " + jp.getCurrentToken());
    }

    // Retrieve the information from JSON
    while (jp.nextToken() == JsonToken.START_OBJECT) {
        jsonData.add(mapper.readValue(jp, Map.class));
    }

    // Close the JSON parser.
    jp.close();

    // Return.
    return jsonData;
}

From source file:com.sdl.odata.unmarshaller.json.core.JsonProcessor.java

/**
 * Process an embedded object./*from  w w w  .jav a  2  s . c o  m*/
 *
 * @param jsonParser the parser
 * @return map with embedded object key:values
 * @throws IOException If unable to read input parser
 */
private Object getEmbeddedObject(JsonParser jsonParser) throws IOException {
    LOG.info("Start parsing an embedded object.");
    Map<String, Object> embeddedMap = new HashMap<>();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        String key = jsonParser.getText();
        jsonParser.nextToken();
        JsonToken token = jsonParser.getCurrentToken();
        if (token == JsonToken.START_ARRAY) {
            Object embeddedArray = getCollectionValue(jsonParser);
            embeddedMap.put(key, embeddedArray);
        } else if (token == JsonToken.START_OBJECT) {
            Object embeddedObject = getEmbeddedObject(jsonParser);
            embeddedMap.put(key, embeddedObject);
        } else {
            if (token.equals(JsonToken.VALUE_NULL)) {
                embeddedMap.put(key, null);
            } else {
                embeddedMap.put(key, jsonParser.getText());
            }
        }
    }
    return embeddedMap;
}

From source file:org.emfjson.jackson.streaming.StreamReader.java

protected void readAttribute(JsonParser parser, EAttribute attribute, EObject owner) throws IOException {

    final JsonToken token = parser.nextToken();

    if (token == JsonToken.START_ARRAY) {
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            EObjects.setOrAdd(owner, attribute, parser.getText());
        }/* ww  w .  j  a v a 2 s. c om*/
    } else {
        EObjects.setOrAdd(owner, attribute, parser.getText());
    }
}

From source file:com.netflix.hollow.jsonadapter.HollowJsonAdapter.java

private int parseSubType(JsonParser parser, JsonToken currentToken, String subType) throws IOException {
    HollowSchema subTypeSchema = hollowSchemas.get(subType);
    switch (subTypeSchema.getSchemaType()) {
    case OBJECT:// w  w w  .  ja v  a2  s . c o m
        if (currentToken != JsonToken.START_OBJECT)
            throw new IOException("Expecting to parse a " + subType + ", which is a "
                    + subTypeSchema.getSchemaType() + ", expected JsonToken.START_OBJECT but instead found a "
                    + currentToken.toString());

        return addObject(parser, subType);

    case LIST:
    case SET:
        if (currentToken != JsonToken.START_ARRAY)
            throw new IOException("Expecting to parse a " + subType + ", which is a "
                    + subTypeSchema.getSchemaType() + ", expected JsonToken.START_ARRAY but instead found a "
                    + currentToken.toString());

        return addSubArray(parser, subType, getWriteRecord(subType));

    case MAP:
        switch (currentToken) {
        case START_ARRAY:
            return addStructuredMap(parser, subType, (HollowMapWriteRecord) getWriteRecord(subType));
        case START_OBJECT:
            return addUnstructuredMap(parser, subType, (HollowMapWriteRecord) getWriteRecord(subType));
        default:
            throw new IOException(
                    "Expecting to parse a " + subType + ", which is a " + subTypeSchema.getSchemaType()
                            + ", expected JsonToken.START_ARRAY or JsonToken.START_OBJECT but instead found a "
                            + currentToken.toString());
        }
    }
    throw new IOException();
}

From source file:com.floragunn.searchguard.dlic.rest.validation.AbstractConfigurationValidator.java

private boolean checkDatatypes() throws Exception {
    String contentAsJson = XContentHelper.convertToJson(content, false);
    JsonParser parser = factory.createParser(contentAsJson);
    JsonToken token = null;/*from  ww  w  .  j a  v a 2s  .co m*/
    while ((token = parser.nextToken()) != null) {
        if (token.equals(JsonToken.FIELD_NAME)) {
            String currentName = parser.getCurrentName();
            DataType dataType = allowedKeys.get(currentName);
            if (dataType != null) {
                JsonToken valueToken = parser.nextToken();
                switch (dataType) {
                case STRING:
                    if (!valueToken.equals(JsonToken.VALUE_STRING)) {
                        wrongDatatypes.put(currentName, "String expected");
                    }
                    break;
                case ARRAY:
                    if (!valueToken.equals(JsonToken.START_ARRAY) && !valueToken.equals(JsonToken.END_ARRAY)) {
                        wrongDatatypes.put(currentName, "Array expected");
                    }
                    break;
                case OBJECT:
                    if (!valueToken.equals(JsonToken.START_OBJECT)
                            && !valueToken.equals(JsonToken.END_OBJECT)) {
                        wrongDatatypes.put(currentName, "Object expected");
                    }
                    break;
                }
            }
        }
    }
    return wrongDatatypes.isEmpty();
}

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

/**
 * Reads from either a JSON Value String (containing CSV) or a JSON Array.
 * The dual input format is needed because some fields (e.g. keywords) were allowed
 * to be of either type in OpenRTB 2.2; now in 2.3 they are all CSV strings only.
 * TODO: Simplify this to only accept CSV strings after 2.2 compatibility is dropped.
 *///from   w w w.j a  v a2 s  .  co m
public static String readCsvString(JsonParser par) throws IOException {
    JsonToken currentToken = par.getCurrentToken();
    if (currentToken == JsonToken.START_ARRAY) {
        StringBuilder keywords = new StringBuilder();
        for (startArray(par); endArray(par); par.nextToken()) {
            if (keywords.length() != 0) {
                keywords.append(',');
            }
            keywords.append(par.getText());
        }
        return keywords.toString();
    } else if (currentToken == JsonToken.VALUE_STRING) {
        return par.getText();
    } else {
        throw new JsonParseException(par, "Expected string or array");
    }
}

From source file:io.debezium.document.JacksonReader.java

private Array parseArray(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the values in the array ...
    BasicArray array = new BasicArray();
    JsonToken token = null;//from  w ww .  j  a v  a  2 s .  c  om
    if (!nested) {
        // We expect the START_ARRAY token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_ARRAY) {
            throw new IOException("Expected data to start with an Array, but was " + token);
        }
    }
    token = parser.nextToken();
    while (token != JsonToken.END_ARRAY) {
        switch (token) {
        case START_OBJECT:
            array.add(parseDocument(parser, true));
            break;
        case START_ARRAY:
            array.add(parseArray(parser, true));
            break;
        case VALUE_STRING:
            array.add(parser.getValueAsString());
            break;
        case VALUE_TRUE:
            array.add(true);
            break;
        case VALUE_FALSE:
            array.add(false);
            break;
        case VALUE_NULL:
            array.addNull();
            break;
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            switch (parser.getNumberType()) {
            case FLOAT:
                array.add(parser.getFloatValue());
                break;
            case DOUBLE:
                array.add(parser.getDoubleValue());
                break;
            case BIG_DECIMAL:
                array.add(parser.getDecimalValue());
                break;
            case INT:
                array.add(parser.getIntValue());
                break;
            case LONG:
                array.add(parser.getLongValue());
                break;
            case BIG_INTEGER:
                array.add(parser.getBigIntegerValue());
                break;
            }
            break;
        case VALUE_EMBEDDED_OBJECT:
            // disregard this, since it's an extension ...
            break;
        case NOT_AVAILABLE:
            throw new JsonParseException("Non-blocking parsers are not supported", parser.getCurrentLocation());
        case FIELD_NAME:
            throw new JsonParseException("Not expecting a FIELD_NAME token", parser.getCurrentLocation());
        case END_ARRAY:
            throw new JsonParseException("Not expecting an END_ARRAY token", parser.getCurrentLocation());
        case END_OBJECT:
            throw new JsonParseException("Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return array;
}

From source file:com.streamsets.datacollector.json.JsonObjectReaderImpl.java

@SuppressWarnings("unchecked")
protected Object readObjectFromArray() throws IOException {
    Object value = null;/* w w  w.  j  a  va2s .  c o  m*/
    if (starting) {
        starting = false;
        JsonToken token = jsonParser.nextToken();
        rootContext = jsonParser.getParsingContext();
        if (token != JsonToken.START_ARRAY) {
            throw new JsonParseException(Utils.format("JSON array expected but stream starts with '{}'", token),
                    jsonParser.getTokenLocation());
        }
    }
    JsonToken token = jsonParser.nextToken();
    if (token != null && token != JsonToken.END_ARRAY) {
        value = jsonParser.readValueAs(Object.class);
    }
    return value;
}