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

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

Introduction

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

Prototype

JsonToken END_ARRAY

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

Click Source Link

Document

END_ARRAY is returned when encountering ']' which signals ending of an Array value

Usage

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

@Override
public JsonToken nextToken() throws IOException {
    if (nextToken != null) {
        _currToken = nextToken;//ww w. ja  v a 2  s  .  co  m
        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:com.cinnober.msgcodec.json.TypeScannerJsonParser.java

private void skipValue(Iterator<Token> it) throws NoSuchElementException, DecodeException {
    Token token = it.next();/*  w ww .  jav a2s.c om*/
    switch (token.getType()) {
    case START_ARRAY:
        skipValuesUntil(it, JsonToken.END_ARRAY);
        break;
    case START_OBJECT:
        skipValuesUntil(it, JsonToken.END_OBJECT);
        break;
    case END_ARRAY:
    case END_OBJECT:
        throw new DecodeException("Unexpected JSON token " + token.getType());
    default:
        break;
    }
}

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

@Override
public String handleStreamed(Reader reader, Map<String, List<String>> params) throws Exception {

    JsonFactory jfactory = new JsonFactory();

    JsonParser parser = jfactory.createJsonParser(reader);

    if (parser.nextToken() != JsonToken.START_OBJECT) {
        throw new IllegalArgumentException("expected JSON object");
    }/*  w ww .j  a  va2  s  .  c  o m*/
    if (parser.nextToken() != JsonToken.FIELD_NAME) {
        throw new IllegalArgumentException("expected indexName first");
    }
    if (!parser.getText().equals("indexName")) {
        throw new IllegalArgumentException("expected indexName first");
    }
    if (parser.nextToken() != JsonToken.VALUE_STRING) {
        throw new IllegalArgumentException("indexName should be string");
    }

    IndexState indexState = globalState.get(parser.getText());
    indexState.verifyStarted(null);
    if (parser.nextToken() != JsonToken.FIELD_NAME) {
        throw new IllegalArgumentException("expected documents next");
    }
    if (!parser.getText().equals("documents")) {
        throw new IllegalArgumentException("expected documents after indexName");
    }

    if (parser.nextToken() != JsonToken.START_ARRAY) {
        throw new IllegalArgumentException("documents should be a list");
    }
    ShardState shardState = indexState.getShard(0);

    int count = 0;
    IndexingContext ctx = new IndexingContext();

    AddDocumentHandler addDocHandler = (AddDocumentHandler) globalState.getHandler("addDocument");

    // Parse as many doc blocks as there are:
    while (true) {

        List<Document> children = null;
        Document parent = null;
        Term updateTerm = null;

        JsonToken token = parser.nextToken();
        if (token == JsonToken.END_ARRAY) {
            break;
        }
        if (token != JsonToken.START_OBJECT) {
            throw new IllegalArgumentException("expected object");
        }

        // Parse term + parent + children for this one doc block:
        while (true) {
            token = parser.nextToken();
            if (token == JsonToken.END_OBJECT) {
                // Done with parent + child in this block
                break;
            }
            if (token != JsonToken.FIELD_NAME) {
                throw new IllegalArgumentException("missing field name: " + token);
            }
            String f = parser.getText();
            if (f.equals("term")) {
                if (parser.nextToken() != JsonToken.START_OBJECT) {
                    throw new IllegalArgumentException("missing object");
                }

                // TODO: allow field to be specified only once, then
                // only text per document

                String field = null, term = null;

                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    String f2 = parser.getText();
                    if (f2.equals("field")) {
                        if (parser.nextToken() != JsonToken.VALUE_STRING) {
                            throw new IllegalArgumentException("missing string value");
                        }
                        field = parser.getText();
                        // Ensure field is valid:
                        indexState.getField(field);
                    } else if (f2.equals("term")) {
                        if (parser.nextToken() != JsonToken.VALUE_STRING) {
                            throw new IllegalArgumentException("missing string value");
                        }
                        term = parser.getText();
                    } else {
                        throw new IllegalArgumentException("unexpected field " + f);
                    }
                }
                updateTerm = new Term(field, term);
            } else if (f.equals("children")) {
                token = parser.nextToken();
                if (token != JsonToken.START_ARRAY) {
                    throw new IllegalArgumentException("expected array for children");
                }

                children = new ArrayList<Document>();

                // Parse each child:
                while (true) {
                    Document doc = addDocHandler.parseDocument(indexState, parser);
                    if (doc == null) {
                        break;
                    }
                    children.add(doc);
                }
            } else if (f.equals("parent")) {
                parent = addDocHandler.parseDocument(indexState, parser);
            } else {
                throw new IllegalArgumentException("unrecognized field name \"" + f + "\"");
            }
        }

        if (parent == null) {
            throw new IllegalArgumentException("missing parent");
        }
        if (children == null) {
            throw new IllegalArgumentException("missing children");
        }

        // Parent is last:
        children.add(parent);

        globalState.submitIndexingTask(shardState.getAddDocumentsJob(count, updateTerm, children, ctx));
        count++;
    }

    // nocommit this is ... lameish:
    while (true) {
        if (ctx.addCount.get() == count) {
            break;
        }
        Thread.sleep(1);
    }

    Throwable t = ctx.getError();
    if (t != null) {
        IOUtils.reThrow(t);
    }

    JSONObject o = new JSONObject();
    o.put("indexGen", shardState.writer.getMaxCompletedSequenceNumber());
    o.put("indexedDocumentBlockCount", count);

    return o.toString();
}

From source file:org.hyperledger.dropwizard.hocon.HoconDeserializer.java

protected ConfigList deserializeArray(JsonParser jp, DeserializationContext ctxt) throws IOException {

    List<Object> values = new ArrayList<>();

    JsonToken t;// w ww  .j  a v a  2 s  .com
    while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
        switch (t) {
        case START_ARRAY:
            values.add(deserializeArray(jp, ctxt).unwrapped());
            break;
        case START_OBJECT:
            values.add(deserializeObject(jp, ctxt).unwrapped());
            break;
        case VALUE_FALSE:
            values.add(false);
            break;
        case VALUE_TRUE:
            values.add(true);
            break;
        case VALUE_NULL:
            values.add(null);
            break;
        case VALUE_NUMBER_FLOAT:
            if (jp.getNumberType() == JsonParser.NumberType.BIG_DECIMAL) {
                values.add(jp.getDecimalValue());
            } else {
                values.add(jp.getDoubleValue());
            }
            break;
        case VALUE_NUMBER_INT:
            // very cumbersome... but has to be done
            switch (jp.getNumberType()) {
            case LONG:
                values.add(jp.getLongValue());
                break;
            case INT:
                values.add(jp.getIntValue());
                break;
            default:
                values.add(jp.getBigIntegerValue());
            }
            break;
        case VALUE_STRING:
            values.add(jp.getText());
            break;
        default:
            throw ctxt.mappingException(_valueClass);
        }
    }
    return ConfigValueFactory.fromIterable(values);
}

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());
        }//www . j a  v a 2  s . c  om
    } else {
        EObjects.setOrAdd(owner, attribute, parser.getText());
    }
}

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.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;
    }/*from   w  ww.j  a  v a2s  .co  m*/

    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.lobid.lodmill.JsonDecoder.java

private JsonToken handleValuesOfArrays(final JsonToken currentToken, final String key)
        throws JsonParseException, IOException {
    int i = 0;// w  ww.  ja  va2  s .com
    JsonToken jtoken = currentToken;
    while (JsonToken.END_ARRAY != jtoken) {
        final String value = this.jsonParser.getText();
        LOG.debug("key=" + key + i + " valueArray=" + value);
        getReceiver().literal(key + i, value);
        jtoken = this.jsonParser.nextToken();
        i++;
    }
    return jtoken;
}

From source file:com.github.heuermh.ensemblrestclient.JacksonVariationConverter.java

static VariationConsequences parseVariationConsequences(final JsonFactory jsonFactory,
        final InputStream inputStream) throws IOException {
    JsonParser parser = null;//from  w  ww.  ja va 2  s  .  co m
    try {
        parser = jsonFactory.createParser(inputStream);
        parser.nextToken();

        String identifier = null;
        String referenceAllele = null;
        List<String> alternateAlleles = new ArrayList<String>();

        String locationName = null;
        String coordinateSystem = "chromosome";
        int start = -1;
        int end = -1;
        int strand = -1;

        List<TranscriptConsequences> transcriptConsequences = new ArrayList<TranscriptConsequences>();

        String alternateAllele = null;
        int transcriptStrand = -1;
        boolean canonical = false;
        String geneId = null;
        String transcriptId = null;
        String translationId = null;
        String transcriptAlleleString = null;
        String codons = null;
        String hgvsc = null;
        String aminoAcids = null;
        String hgvsp = null;
        List<String> consequenceTerms = new ArrayList<String>();

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

                if ("id".equals(field)) {
                    identifier = parser.getText();
                } else if ("seq_region_name".equals(field)) {
                    locationName = parser.getText();
                } else if ("start".equals(field)) {
                    start = parser.getIntValue();
                } else if ("end".equals(field)) {
                    end = parser.getIntValue();
                } else if ("strand".equals(field)) {
                    strand = parser.getIntValue();
                } else if ("allele_string".equals(field)) {
                    String[] tokens = parser.getText().split("/");
                    referenceAllele = tokens[0];
                    for (int i = 1; i < tokens.length; i++) {
                        alternateAlleles.add(tokens[i]);
                    }
                } else if ("transcript_consequences".equals(field)) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            String transcriptField = parser.getCurrentName();
                            parser.nextToken();

                            if ("variant_allele".equals(transcriptField)) {
                                alternateAllele = parser.getText();
                            } else if ("strand".equals(transcriptField)) {
                                transcriptStrand = parser.getIntValue();
                            } else if ("canonical".equals(transcriptField)) {
                                canonical = (Integer.parseInt(parser.getText()) > 0);
                            } else if ("gene_id".equals(transcriptField)) {
                                geneId = parser.getText();
                            } else if ("transcript_id".equals(transcriptField)) {
                                transcriptId = parser.getText();
                            } else if ("protein_id".equals(transcriptField)) {
                                translationId = parser.getText();
                            } else if ("codons".equals(transcriptField)) {
                                codons = parser.getText();
                            } else if ("hgvsc".equals(transcriptField)) {
                                hgvsc = parser.getText();
                            } else if ("amino_acids".equals(transcriptField)) {
                                aminoAcids = parser.getText();
                            } else if ("hgvsp".equals(transcriptField)) {
                                hgvsp = parser.getText();
                            } else if ("consequence_terms".equals(transcriptField)) {
                                while (parser.nextToken() != JsonToken.END_ARRAY) {
                                    consequenceTerms.add(parser.getText());
                                }
                            }
                        }

                        transcriptConsequences.add(new TranscriptConsequences(alternateAllele, transcriptStrand,
                                canonical, geneId, transcriptId, translationId, codons, hgvsc, aminoAcids,
                                hgvsp, consequenceTerms));

                        alternateAllele = null;
                        transcriptStrand = -1;
                        canonical = false;
                        geneId = null;
                        transcriptId = null;
                        translationId = null;
                        transcriptAlleleString = null;
                        codons = null;
                        hgvsc = null;
                        aminoAcids = null;
                        hgvsp = null;
                        consequenceTerms.clear();
                    }
                } else if ("colocated_variants".equals(field)) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            // ignore
                        }
                    }
                }
            }
        }
        Location location = new Location(locationName, coordinateSystem, start, end, strand);
        return new VariationConsequences(identifier, referenceAllele, alternateAlleles, location,
                transcriptConsequences);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            parser.close();
        } catch (Exception e) {
            // ignored
        }
    }
}

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;/* w w  w .  j  a v  a2  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;
}