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

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

Introduction

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

Prototype

JsonToken START_OBJECT

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

Click Source Link

Document

START_OBJECT is returned when encountering '{' which signals starting of an Object value.

Usage

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.
 * //from w  w  w  . j  a  v a 2  s  .c  om
 * @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:de.konqi.fitapi.auth.PublicKeysManager.java

/**
 * Forces a refresh of the public certificates downloaded from {@link #getPublicCertsEncodedUrl}.
 *
 * <p>/*  www  . j a va 2  s .c  o m*/
 * This method is automatically called from {@link #getPublicKeys()} if the public keys have not
 * yet been initialized or if the expiration time is very close, so normally this doesn't need to
 * be called. Only call this method to explicitly force the public keys to be updated.
 * </p>
 */
public PublicKeysManager refresh() throws GeneralSecurityException, IOException {
    lock.lock();
    try {
        publicKeys = new ArrayList<PublicKey>();
        // HTTP request to public endpoint
        CertificateFactory factory = SecurityUtils.getX509CertificateFactory();
        HttpResponse certsResponse = transport.createRequestFactory()
                .buildGetRequest(new GenericUrl(publicCertsEncodedUrl)).execute();
        expirationTimeMilliseconds = clock.currentTimeMillis()
                + getCacheTimeInSec(certsResponse.getHeaders()) * 1000;
        // parse each public key in the JSON response
        JsonParser parser = jsonFactory.createJsonParser(certsResponse.getContent());
        JsonToken currentToken = parser.getCurrentToken();
        // token is null at start, so get next token
        if (currentToken == null) {
            currentToken = parser.nextToken();
        }
        Preconditions.checkArgument(currentToken == JsonToken.START_OBJECT);
        try {
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                parser.nextToken();
                String certValue = parser.getText();
                X509Certificate x509Cert = (X509Certificate) factory
                        .generateCertificate(new ByteArrayInputStream(StringUtils.getBytesUtf8(certValue)));
                publicKeys.add(x509Cert.getPublicKey());
            }
            publicKeys = Collections.unmodifiableList(publicKeys);
        } finally {
            parser.close();
        }
        return this;
    } finally {
        lock.unlock();
    }
}

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 w ww. ja v  a 2  s  .  c o 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:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java

private ImmutableMap<String, String> parseIndex(JsonParser p, DeserializationContext ctxt) throws IOException {
    // Index can either be an array or object with id values. Here we transform
    // both to array.
    ImmutableMap<String, String> index = null;
    JsonToken token = p.currentToken();/*from   w  w w .  j a  v a  2  s .  c  om*/
    if (token == JsonToken.START_ARRAY)
        index = parseIndexAsArray(p);
    else if (token == JsonToken.START_OBJECT)
        index = parseIndexAsMap(p);
    else
        ctxt.reportMappingException(
                "could not deserialize category index, need either an array " + "or an object, got %s", token);

    return checkNotNull(index, "could not parse index");

}

From source file:com.adobe.communities.ugc.migration.importer.ScoresImportServlet.java

private void importFile(final JsonParser jsonParser, final UserPropertiesManager userManager,
        final ResourceResolver resolver) throws ServletException, IOException, RepositoryException {
    Map<String, Boolean> scoreTypes = getScoreTypes(resolver);
    JsonToken token = jsonParser.nextToken();

    while (!token.equals(JsonToken.END_OBJECT)) {
        final String authId = jsonParser.getCurrentName();
        token = jsonParser.nextToken();//from  w ww . j av  a2 s  .c  o m
        if (!token.equals(JsonToken.START_OBJECT)) {
            throw new ServletException("Expected to see start object, got " + token);
        }
        final Map<String, Long> scores = new HashMap<String, Long>();
        token = jsonParser.nextToken();
        while (!token.equals(JsonToken.END_OBJECT)) {
            final String scoreName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            final Long scoreValue = jsonParser.getLongValue();
            scores.put(scoreName, scoreValue);
            if (!scoreTypes.containsKey(scoreName)) {
                LOG.warn(
                        "A score of type [{}] was imported for [{}], but that score type hasn't been configured "
                                + "on this server",
                        scoreName, authId);
            }
            token = jsonParser.nextToken();
        }
        updateProfileScore(authId, scores, userManager, resolver);
        token = jsonParser.nextToken();
    }
}

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

protected void readReference(JsonParser parser, EReference reference, EObject owner) throws IOException {

    JsonToken token = parser.nextToken();

    if (reference.isContainment()) {

        if (isMapEntry(reference.getEReferenceType())) {
            if (token == JsonToken.START_OBJECT) {
                token = parseEntry(parser, reference, owner);
            }//from  w w  w.  ja  va 2 s  .  com
        }

        if (token == JsonToken.START_ARRAY) {
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                parseObject(parser, reference, owner, null);
            }
        } else if (token == JsonToken.START_OBJECT) {
            parseObject(parser, reference, owner, null);
        }

    } else {

        if (token == JsonToken.START_ARRAY) {
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                entries.add(createReferenceEntry(parser, reference, owner));
            }
        } else {
            entries.add(createReferenceEntry(parser, reference, owner));
        }
    }

}

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

@Nonnull
private static List<? extends CouchDoc.Attachment> deserializeAttachments(
        @Nonnull JacksonParserWrapper parserWrapper) throws IOException {
    List<CouchDoc.Attachment> attachments = new ArrayList<>();

    //check for attachments
    if (parserWrapper.getCurrentToken() == JsonToken.FIELD_NAME
            && parserWrapper.getCurrentName().equals(PROPERTY_ATTACHMENTS)) {
        parserWrapper.nextToken(JsonToken.START_OBJECT);

        while (parserWrapper.nextToken() != JsonToken.END_OBJECT) {
            String attachmentId = parserWrapper.getCurrentName();

            parserWrapper.nextToken(JsonToken.START_OBJECT);
            parserWrapper.nextFieldValue(PROPERTY_CONTENT_TYPE);
            String contentType = parserWrapper.getText();
            parserWrapper.nextFieldValue("revpos");
            parserWrapper.nextFieldValue("digest");
            parserWrapper.nextFieldValue("length");
            long length = parserWrapper.getNumberValue().longValue();
            parserWrapper.nextFieldValue("stub");

            attachments.add(new CouchDoc.StubbedAttachment(new AttachmentId(attachmentId),
                    MediaType.valueOf(contentType), length));

            parserWrapper.nextToken(JsonToken.END_OBJECT);
        }/* w  ww  . j ava  2s  . c  o m*/
        parserWrapper.nextToken(JsonToken.END_OBJECT);
    }

    return attachments;
}

From source file:org.oscim.utils.overpass.OverpassAPIReader.java

public void parse(InputStream in) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    try {/*from   w  ww.ja va  2  s . c o  m*/
        JsonParser jp = jsonFactory.createJsonParser(in);

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

                String name = jp.getCurrentName();
                jp.nextToken();

                if ("type".equals(name)) {
                    String type = jp.getText();

                    if ("node".equals(type))
                        parseNode(jp);

                    else if ("way".equals(type))
                        parseWay(jp);

                    else if ("relation".equals(type))
                        parseRelation(jp);
                }
            }
        }
    } catch (JsonParseException e) {
        e.printStackTrace();
    }
}

From source file:com.addthis.codec.config.ConfigTraversingParser.java

@Override
public JsonParser skipChildren() throws IOException, JsonParseException {
    if (_currToken == JsonToken.START_OBJECT) {
        _startContainer = false;//from w w w  . j a  v  a  2  s  .c om
        _currToken = JsonToken.END_OBJECT;
    } else if (_currToken == JsonToken.START_ARRAY) {
        _startContainer = false;
        _currToken = JsonToken.END_ARRAY;
    }
    return this;
}

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

@Override
public JsonParser skipChildren() throws IOException {
    if (_currToken == JsonToken.START_OBJECT) {
        startContainer = false;//w  w  w. j  a v a2  s  .co m
        _currToken = JsonToken.END_OBJECT;
    } else if (_currToken == JsonToken.START_ARRAY) {
        startContainer = false;
        _currToken = JsonToken.END_ARRAY;
    }
    return this;
}