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:com.unboundid.scim2.client.requests.SearchRequestBuilder.java

/**
 * Invoke the SCIM retrieve request.//from w w w.  j ava 2  s .  com
 *
 * @param post {@code true} to send the request using POST or {@code false}
 *             to send the request using GET.
 * @param <T> The type of objects to return.
 * @param resultHandler The search result handler that should be used to
 *                      process the resources.
 * @param cls The Java class object used to determine the type to return.
 * @throws ScimException If an error occurred.
 */
private <T> void invoke(final boolean post, final SearchResultHandler<T> resultHandler, final Class<T> cls)
        throws ScimException {
    Response response;
    if (post) {
        Set<String> attributeSet = null;
        Set<String> excludedAttributeSet = null;
        if (attributes != null && attributes.size() > 0) {
            if (!excluded) {
                attributeSet = attributes;
            } else {
                excludedAttributeSet = attributes;
            }
        }

        SearchRequest searchRequest = new SearchRequest(attributeSet, excludedAttributeSet, filter, sortBy,
                sortOrder, startIndex, count);

        Invocation.Builder builder = target().path(ApiConstants.SEARCH_WITH_POST_PATH_EXTENSION)
                .request(ScimService.MEDIA_TYPE_SCIM_TYPE, MediaType.APPLICATION_JSON_TYPE);
        for (Map.Entry<String, List<Object>> header : headers.entrySet()) {
            builder = builder.header(header.getKey(), StaticUtils.listToString(header.getValue(), ", "));
        }
        response = builder.post(Entity.entity(searchRequest, getContentType()));
    } else {
        response = buildRequest().get();
    }

    try {
        if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
            InputStream inputStream = response.readEntity(InputStream.class);
            try {
                JsonParser parser = JsonUtils.getObjectReader().getFactory().createParser(inputStream);
                try {
                    parser.nextToken();
                    boolean stop = false;
                    while (!stop && parser.nextToken() != JsonToken.END_OBJECT) {
                        String field = parser.getCurrentName();
                        parser.nextToken();
                        if (field.equals("schemas")) {
                            parser.skipChildren();
                        } else if (field.equals("totalResults")) {
                            resultHandler.totalResults(parser.getIntValue());
                        } else if (field.equals("startIndex")) {
                            resultHandler.startIndex(parser.getIntValue());
                        } else if (field.equals("itemsPerPage")) {
                            resultHandler.itemsPerPage(parser.getIntValue());
                        } else if (field.equals("Resources")) {
                            while (parser.nextToken() != JsonToken.END_ARRAY) {
                                if (!resultHandler.resource(parser.readValueAs(cls))) {
                                    stop = true;
                                    break;
                                }
                            }
                        } else if (SchemaUtils.isUrn(field)) {
                            resultHandler.extension(field, parser.<ObjectNode>readValueAsTree());
                        } else {
                            // Just skip this field
                            parser.nextToken();
                        }
                    }
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    parser.close();
                }
            } catch (IOException e) {
                throw new ResponseProcessingException(response, e);
            }
        } else {
            throw toScimException(response);
        }
    } finally {
        response.close();
    }
}

From source file:io.apiman.manager.api.exportimport.json.JsonImportReader.java

public void readPlanVersions() throws Exception {
    current = nextToken();/*w  ww.jav a2 s.  c  om*/
    if (current == JsonToken.END_ARRAY) {
        return;
    }
    while (nextToken() != JsonToken.END_ARRAY) {
        // Traverse each plan definition
        while (nextToken() != JsonToken.END_OBJECT) {
            if (jp.getCurrentName().equals(PlanVersionBean.class.getSimpleName())) {
                current = nextToken();
                PlanVersionBean planBean = jp.readValueAs(PlanVersionBean.class);
                dispatcher.planVersion(planBean);
            } else {
                OrgElementsEnum fieldName = OrgElementsEnum.valueOf(jp.getCurrentName());
                current = nextToken();
                switch (fieldName) {
                case Policies:
                    processEntities(PolicyBean.class, new EntityHandler<PolicyBean>() {
                        @Override
                        public void handleEntity(PolicyBean policy) throws Exception {
                            dispatcher.planPolicy(policy);
                        }
                    });
                    break;
                default:
                    throw new RuntimeException("Unhandled entity " + fieldName + " with token " + current);
                }
            }
        }
    }
}

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  ww w.ja va 2  s .co m
        } else {
            objects.add(jsonParser.getText());
        }
    }
    return objects;
}

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

/**
 * Read the first feature to create the table
 * @param jp/*w  ww  . j a  v a 2 s  .c o m*/
 */
private void readFeatures(JsonParser jp, String geomType, StringBuilder metadataBuilder)
        throws IOException, SQLException {
    jp.nextToken(); // START_ARRAY [
    JsonToken token = jp.nextToken(); // START_OBJECT {
    if (token != JsonToken.END_ARRAY) {
        jp.nextToken(); // FIELD_NAME type"name"
        jp.nextToken(); // VALUE_STRING Feature
        geomType = jp.getText();
        if (geomType.equalsIgnoreCase(GeoJsonField.FEATURE)) {
            jp.nextToken(); // FIELD_NAME geometry
            String firstField = jp.getText();
            if (firstField.equalsIgnoreCase(GeoJsonField.GEOMETRY)) {
                parseGeometryMetadata(jp, metadataBuilder);
                hasGeometryField = true;
                fieldIndex++;
                jp.nextToken();//END_OBJECT } geometry
            } else if (firstField.equalsIgnoreCase(GeoJsonField.PROPERTIES)) {
                fieldIndex = parseMetadataProperties(jp, metadataBuilder, fieldIndex);
                hasProperties = true;
            }
            // 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();
                if (secondParam.equalsIgnoreCase(GeoJsonField.GEOMETRY)) {
                    parseGeometryMetadata(jp, metadataBuilder);
                    hasGeometryField = true;
                    fieldIndex++;
                    jp.nextToken();//END_OBJECT } geometry;
                } else if (secondParam.equalsIgnoreCase(GeoJsonField.PROPERTIES)) {
                    fieldIndex = parseMetadataProperties(jp, metadataBuilder, fieldIndex);
                    hasProperties = true;
                }
                jp.nextToken(); //END_OBJECT } feature
            }
            if (!hasProperties) {
                metadataBuilder.append("ID INT, PRIMARY KEY (ID)");
                fieldIndex++;
            }
            metadataBuilder.append(")");
        } else {
            throw new SQLException("Malformed GeoJSON file. Expected 'Feature', found '" + geomType + "'");
        }
    }
}

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

@Override
public void copyCurrentStructure(JsonParser jp) throws IOException {
    JsonToken t = jp.getCurrentToken();//from  w  w  w .  j ava  2 s .c  o m

    // Let'string handle field-name separately first
    if (t == JsonToken.FIELD_NAME) {
        writeFieldName(jp.getCurrentName());
        t = jp.nextToken();
        // fall-through to copy the associated value
    }

    switch (t) {
    case START_ARRAY:
        writeStartArray();
        while (jp.nextToken() != JsonToken.END_ARRAY) {
            copyCurrentStructure(jp);
        }
        writeEndArray();
        break;
    case START_OBJECT:
        writeStartObject();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            copyCurrentStructure(jp);
        }
        writeEndObject();
        break;
    default: // others are simple:
        copyCurrentEvent(jp);
    }
}

From source file:io.pdef.json.JsonJacksonFormat.java

private List<?> readArray(final JsonParser parser) throws IOException {
    JsonToken current = parser.getCurrentToken();
    if (current != JsonToken.START_ARRAY) {
        throw new JsonFormatException("Bad JSON string, failed to read an array");
    }//from   w  ww. java  2s. co  m

    List<Object> list = new ArrayList<Object>();
    while (true) {
        JsonToken next = parser.nextToken();
        if (next == null) {
            throw new JsonFormatException("End of file");
        } else if (next == JsonToken.END_ARRAY) {
            break;
        }

        Object element = read(parser);
        list.add(element);
    }

    return list;
}

From source file:com.bazaarvoice.jsonpps.PrettyPrintJson.java

private void copyCurrentStructure(JsonParser parser, ObjectMapper mapper, int depth, JsonGenerator generator)
        throws IOException {
    // Avoid using the mapper to parse the entire input until we absolutely must.  This allows pretty
    // printing huge top-level arrays (that wouldn't fit in memory) containing smaller objects (that
    // individually do fit in memory) where the objects are printed with sorted keys.
    JsonToken t = parser.getCurrentToken();
    if (t == null) {
        generator.copyCurrentStructure(parser); // Will report the error of a null token.
        return;//from  w  w w .  ja  v a2s .  co  m
    }
    int id = t.id();
    if (id == ID_FIELD_NAME) {
        if (depth > flatten) {
            generator.writeFieldName(parser.getCurrentName());
        }
        t = parser.nextToken();
        id = t.id();
    }
    switch (id) {
    case ID_START_OBJECT:
        if (sortKeys && depth >= flatten) {
            // Load the entire object in memory so we can sort its keys and serialize it back out.
            mapper.writeValue(generator, parser.readValueAs(Map.class));
        } else {
            // Don't load the whole object into memory.  Copy it in a memory-efficient streaming fashion.
            if (depth >= flatten) {
                generator.writeStartObject();
            }
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                copyCurrentStructure(parser, mapper, depth + 1, generator);
            }
            if (depth >= flatten) {
                generator.writeEndObject();
            }
        }
        break;
    case ID_START_ARRAY:
        // Don't load the whole array into memory.  Copy it in a memory-efficient streaming fashion.
        if (depth >= flatten) {
            generator.writeStartArray();
        }
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            copyCurrentStructure(parser, mapper, depth + 1, generator);
        }
        if (depth >= flatten) {
            generator.writeEndArray();
        }
        break;
    default:
        generator.copyCurrentEvent(parser);
        break;
    }
}

From source file:com.quinsoft.zeidon.standardoe.ActivateOisFromJsonStream.java

private boolean readSimpleOi() throws Exception {
    JsonToken token = jp.getCurrentToken();

    // If we find the end of the OI array then that's the end of OIs.
    if (token == JsonToken.END_ARRAY || token == JsonToken.END_OBJECT)
        return false; // No more OIs in the stream.

    String fieldName = jp.getCurrentName();

    assert token == JsonToken.FIELD_NAME;
    assert lodDef.getRoot().getName().equalsIgnoreCase(fieldName);

    // If the token after reading the .oimeta is END_OBJECT then the OI is empty.
    if (token != JsonToken.END_OBJECT) {
        // readEntity expects the current token to be the opening { or [.
        // Skip over the field name.
        token = jp.nextToken();/*from   w  w  w. j a  va  2 s.  c o  m*/
        readEntity(fieldName);
        token = jp.nextToken();
    }

    if (token != JsonToken.END_OBJECT)
        throw new ZeidonException("OI JSON stream doesn't end with object.");

    return true; // Keep looking for OIs in the stream.
}

From source file:data.DefaultExchanger.java

private void importDataFromArray(JsonParser parser, JdbcTemplate jdbcTemplate, int batchSize)
        throws IOException {
    int importedNodesCount = 0;
    final List<JsonNode> nodes = new ArrayList<>();
    while (parser.nextToken() != JsonToken.END_ARRAY) {
        final JsonNode node = parser.readValueAsTree();
        nodes.add(node);/* www  .  j av a  2 s  . c  om*/
        if (nodes.size() == batchSize) {
            importedNodesCount += batchUpdate(jdbcTemplate, nodes).length;
            nodes.clear();
        }
    }
    if (nodes.size() > 0) {
        importedNodesCount += batchUpdate(jdbcTemplate, nodes).length;
    }
    play.Logger.info("imported {{}} {}", importedNodesCount, getTable());
}

From source file:org.helm.notation2.wsadapter.MonomerWSLoader.java

/**
 * Private routine to deserialize a JSON containing attachment data. This is
 * done manually to give more freedom regarding data returned by the
 * webservice.//from   ww  w .  ja  v a 2  s .  co m
 *
 * @param parser the JSONParser containing JSONData.
 * @param attachmentDB the attachments stored in the Toolkit
 * @return List containing attachments
 *
 * @throws JsonParseException
 * @throws IOException
 */
private List<Attachment> deserializeAttachmentList(JsonParser parser, Map<String, Attachment> attachmentDB)
        throws JsonParseException, IOException {
    List<Attachment> attachments = new ArrayList<Attachment>();
    Attachment currentAttachment = null;

    while (!JsonToken.END_ARRAY.equals(parser.nextToken())) {

        String fieldName = parser.getCurrentName();
        JsonToken token = parser.getCurrentToken();

        if (JsonToken.START_OBJECT.equals(token)) {
            currentAttachment = new Attachment();
        } else if (JsonToken.END_OBJECT.equals(token)) {
            currentAttachment.setCapGroupSMILES(
                    attachmentDB.get(currentAttachment.getAlternateId()).getCapGroupSMILES());
            attachments.add(currentAttachment);
        }

        if (fieldName != null) {
            switch (fieldName) {
            case "id":
                parser.nextToken();
                currentAttachment.setId(Integer.parseInt(parser.getText()));
                break;
            case "alternateId":
                parser.nextToken();
                currentAttachment.setAlternateId(parser.getText());
                break;
            case "label":
                parser.nextToken();
                currentAttachment.setLabel(parser.getText());
                break;
            case "capGroupName":
                parser.nextToken();
                currentAttachment.setCapGroupName(parser.getText());
                break;
            case "capGroupSMILES":
                parser.nextToken();
                currentAttachment.setCapGroupSMILES(parser.getText());
                break;
            default:
                break;
            }
        }

    }

    return attachments;
}