Example usage for com.fasterxml.jackson.core JsonParser getCurrentToken

List of usage examples for com.fasterxml.jackson.core JsonParser getCurrentToken

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getCurrentToken.

Prototype

public abstract JsonToken getCurrentToken();

Source Link

Document

Accessor to find which token parser currently points to, if any; null will be returned if none.

Usage

From source file:com.microsoft.azure.storage.queue.CloudQueueEncryptedMessage.java

public static CloudQueueEncryptedMessage deserialize(String inputMessage)
        throws JsonProcessingException, IOException {
    JsonParser parser = Utility.getJsonParser(inputMessage);
    CloudQueueEncryptedMessage message = new CloudQueueEncryptedMessage();
    try {// w  w w  .j  a va  2  s  .  co  m
        if (!parser.hasCurrentToken()) {
            parser.nextToken();
        }

        JsonUtilities.assertIsStartObjectJsonToken(parser);

        parser.nextToken();

        while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            parser.nextToken();

            if (name.equals("EncryptedMessageContents")) {
                message.setEncryptedMessageContents(parser.getValueAsString());
            } else if (name.equals("EncryptionData")) {
                message.setEncryptionData(EncryptionData.deserialize(parser));
            }
            parser.nextToken();
        }

        JsonUtilities.assertIsEndObjectJsonToken(parser);
    } finally {
        parser.close();
    }

    return message;
}

From source file:com.microsoft.azure.storage.table.TableStorageErrorDeserializer.java

/**
 * Parses the error exception details from the Json-formatted response.
 * //  www. ja v a 2s . com
 * @param parser
 *            the {@link JsonParser} to use for parsing
 * @throws IOException
 *             if an error occurs while accessing the stream with Json.
 * @throws JsonParseException
 *             if an error occurs while parsing the stream.
 */
private static HashMap<String, String[]> parseJsonErrorException(JsonParser parser)
        throws JsonParseException, IOException {
    HashMap<String, String[]> additionalDetails = new HashMap<String, String[]>();

    parser.nextToken();
    JsonUtilities.assertIsStartObjectJsonToken(parser);

    parser.nextToken();
    JsonUtilities.assertIsFieldNameJsonToken(parser);

    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentName().equals(TableConstants.ErrorConstants.ERROR_MESSAGE)) {
            parser.nextToken();
            additionalDetails.put(TableConstants.ErrorConstants.ERROR_MESSAGE,
                    new String[] { parser.getValueAsString() });
        } else if (parser.getCurrentName().equals(TableConstants.ErrorConstants.ERROR_EXCEPTION_TYPE)) {
            parser.nextToken();
            additionalDetails.put(TableConstants.ErrorConstants.ERROR_EXCEPTION_TYPE,
                    new String[] { parser.getValueAsString() });
        } else if (parser.getCurrentName().equals(TableConstants.ErrorConstants.ERROR_EXCEPTION_STACK_TRACE)) {
            parser.nextToken();
            additionalDetails.put(Constants.ERROR_EXCEPTION_STACK_TRACE,
                    new String[] { parser.getValueAsString() });
        }
        parser.nextToken();
    }

    return additionalDetails;
}

From source file:org.elasticsearch.client.sniff.ElasticsearchNodesSniffer.java

static List<Node> readHosts(HttpEntity entity, Scheme scheme, JsonFactory jsonFactory) throws IOException {
    try (InputStream inputStream = entity.getContent()) {
        JsonParser parser = jsonFactory.createParser(inputStream);
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("expected data to start with an object");
        }//from   w w  w  . j a v  a 2  s . c o  m
        List<Node> nodes = new ArrayList<>();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                if ("nodes".equals(parser.getCurrentName())) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        JsonToken token = parser.nextToken();
                        assert token == JsonToken.START_OBJECT;
                        String nodeId = parser.getCurrentName();
                        Node node = readNode(nodeId, parser, scheme);
                        if (node != null) {
                            nodes.add(node);
                        }
                    }
                } else {
                    parser.skipChildren();
                }
            }
        }
        return nodes;
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Merges the {@code message} from the JsonParser using the given {@code schema}.
 *///from  w  w  w .  j  ava2 s  . c om
public static <T> void mergeFrom(JsonParser parser, T message, Schema<T> schema, boolean numeric)
        throws IOException {
    if (parser.nextToken() != JsonToken.START_OBJECT) {
        throw new JsonInputException("Expected token: { but was " + parser.getCurrentToken() + " on message "
                + schema.messageFullName());
    }

    schema.mergeFrom(new JsonInput(parser, numeric), message);

    if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        throw new JsonInputException("Expected token: } but was " + parser.getCurrentToken() + " on message "
                + schema.messageFullName());
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Parses the {@code messages} from the parser using the given {@code schema}.
 */// w  w w.  ja va  2  s.co  m
public static <T> List<T> parseListFrom(JsonParser parser, Schema<T> schema, boolean numeric)
        throws IOException {
    if (parser.nextToken() != JsonToken.START_ARRAY) {
        throw new JsonInputException("Expected token: [ but was " + parser.getCurrentToken() + " on message: "
                + schema.messageFullName());
    }

    final JsonInput input = new JsonInput(parser, numeric);
    final List<T> list = new ArrayList<>();
    for (JsonToken t = parser.nextToken(); t != JsonToken.END_ARRAY; t = parser.nextToken()) {
        if (t != JsonToken.START_OBJECT) {
            throw new JsonInputException("Expected token: { but was " + parser.getCurrentToken()
                    + " on message " + schema.messageFullName());
        }

        final T message = schema.newMessage();
        schema.mergeFrom(input, message);

        if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
            throw new JsonInputException("Expected token: } but was " + parser.getCurrentToken()
                    + " on message " + schema.messageFullName());
        }

        list.add(message);
        input.reset();
    }
    return list;
}

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

/**
 * Private routine to deserialize JSON containing monomer categorization data.
 * This is done manually to give more freedom regarding data returned by the
 * webservice.//w  w  w .  j a  v  a  2s.  co m
 *
 * @param parser the JSONParser containing JSONData.
 * @return List containing the monomer categorization
 *
 * @throws JsonParseException
 * @throws IOException
 */
private static List<CategorizedMonomer> deserializeEditorCategorizationConfig(JsonParser parser)
        throws JsonParseException, IOException {
    List<CategorizedMonomer> config = new LinkedList<CategorizedMonomer>();
    CategorizedMonomer currentMonomer = null;

    parser.nextToken();
    while (parser.hasCurrentToken()) {
        String fieldName = parser.getCurrentName();
        JsonToken token = parser.getCurrentToken();

        if (JsonToken.START_OBJECT.equals(token)) {
            currentMonomer = new CategorizedMonomer();
        } else if (JsonToken.END_OBJECT.equals(token)) {
            config.add(currentMonomer);
        }

        if (fieldName != null) {
            switch (fieldName) {
            // id is first field
            case "monomerID":
                parser.nextToken();
                currentMonomer.setMonomerID(parser.getText());
                break;
            case "monomerName":
                parser.nextToken();
                currentMonomer.setMonomerName(parser.getText());
                break;
            case "naturalAnalogon":
                parser.nextToken();
                currentMonomer.setNaturalAnalogon(parser.getText());
                break;
            case "monomerType":
                parser.nextToken();
                currentMonomer.setMonomerType(parser.getText());
                break;
            case "polymerType":
                parser.nextToken();
                currentMonomer.setPolymerType(parser.getText());
                break;
            case "category":
                parser.nextToken();
                currentMonomer.setCategory(parser.getText());
                break;
            case "shape":
                parser.nextToken();
                currentMonomer.setShape(parser.getText());
                break;
            case "fontColor":
                parser.nextToken();
                currentMonomer.setFontColor(parser.getText());
                break;
            case "backgroundColor":
                parser.nextToken();
                currentMonomer.setBackgroundColor(parser.getText());
                break;
            default:
                break;
            }
        }
        parser.nextToken();
    }

    return config;
}

From source file:org.n52.movingcode.runtime.processors.config.ProcessorConfig.java

/**
 * Properties reader//from  www  . j av a 2s  . c o m
 * 
 * @return
 */
private static final HashMap<String, ProcessorDescription> readProperties() {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream is = classLoader.getResourceAsStream(configFile);

    HashMap<String, ProcessorDescription> processorMap = new HashMap<String, ProcessorDescription>();

    JsonFactory f = new JsonFactory();
    JsonParser jp;

    try {
        jp = f.createParser(is);
        jp.nextToken(); // will return JsonToken.START_OBJECT
        while (jp.nextToken() != END_OBJECT) {
            String field = jp.getCurrentName();

            if (field.equalsIgnoreCase(KEY_PROCESSORS)) {
                // get next token, make sure it is the beginning of an array
                if (jp.nextToken() != START_ARRAY) {
                    break;
                }

                while (jp.nextToken() != END_ARRAY) {
                    // do the parsing
                    if (jp.getCurrentToken() == START_OBJECT) {
                        ProcessorDescription p = parseProcessorJSON(jp);
                        // only add those processor that have a valid ID
                        if (p.getId() != null) {
                            processorMap.put(p.getId(), p);
                        }
                    }

                }

            } else {
                if (field.equalsIgnoreCase(KEY_DEFAULTS)) {
                    // parse defaults
                    ProcessorDescription p = parseProcessorJSON(jp);
                    p.setId(DEFAULT_PROCESSOR_CONFIG_ID);
                    processorMap.put(p.getId(), p);
                }
            }

        }
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return processorMap;
}

From source file:com.boundary.zoocreeper.Restore.java

private static BackupZNode readZNode(JsonParser jp, String path) throws IOException {
    expectNextToken(jp, JsonToken.START_OBJECT);
    long ephemeralOwner = 0;
    byte[] data = null;
    final List<ACL> acls = Lists.newArrayList();
    final Set<String> seenFields = Sets.newHashSet();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        jp.nextValue();/*w w  w . j a  v  a 2 s. c om*/
        final String fieldName = jp.getCurrentName();
        seenFields.add(fieldName);
        if (Backup.FIELD_EPHEMERAL_OWNER.equals(fieldName)) {
            ephemeralOwner = jp.getLongValue();
        } else if (Backup.FIELD_DATA.equals(fieldName)) {
            if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                data = null;
            } else {
                data = jp.getBinaryValue();
            }
        } else if (Backup.FIELD_ACLS.equals(fieldName)) {
            readACLs(jp, acls);
        } else {
            LOGGER.debug("Ignored field: {}", fieldName);
        }
    }
    if (!seenFields.containsAll(REQUIRED_ZNODE_FIELDS)) {
        throw new IOException("Missing required fields: " + REQUIRED_ZNODE_FIELDS);
    }
    return new BackupZNode(path, ephemeralOwner, data, acls);
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Creates a json pipe from a {@link JsonParser}.
 *//*from  w  ww.j a  va  2  s.  co m*/
public static Pipe newPipe(final JsonParser parser, boolean numeric) throws IOException {
    final JsonInput jsonInput = new JsonInput(parser, numeric);
    return new Pipe() {
        @Override
        protected Input begin(Pipe.Schema<?> pipeSchema) throws IOException {
            if (parser.nextToken() != JsonToken.START_OBJECT) {
                throw new JsonInputException("Expected token: { but was " + parser.getCurrentToken()
                        + " on message " + pipeSchema.wrappedSchema.messageFullName());
            }

            return jsonInput;
        }

        @Override
        protected void end(Pipe.Schema<?> pipeSchema, Input input, boolean cleanupOnly) throws IOException {
            if (cleanupOnly) {
                parser.close();
                return;
            }

            assert input == jsonInput;
            final JsonToken token = parser.getCurrentToken();

            parser.close();

            if (token != JsonToken.END_OBJECT) {
                throw new JsonInputException("Expected token: } but was " + token + " on message "
                        + pipeSchema.wrappedSchema.messageFullName());
            }
        }
    };
}

From source file:timezra.dropbox.maven.plugin.client.DbxClientWrapper.java

private static <C> WithChildrenC<C> readWithNulls(final JsonParser parser,
        final Collector<DbxEntry, ? extends C> collector) throws IOException, JsonReadException {
    final JsonLocation top = JsonReader.expectObjectStart(parser);

    String size = null;/*from ww w  . j  a va2s . c om*/
    long bytes = -1;
    String path = null;
    Boolean is_dir = null;
    Boolean is_deleted = null;
    String rev = null;
    Boolean thumb_exists = null;
    String icon = null;
    Date modified = null;
    Date client_mtime = null;
    String hash = null;
    C contents = null;

    while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
        final String fieldName = parser.getCurrentName();
        JsonReader.nextToken(parser);

        final int fi = FM.get(fieldName);
        try {
            switch (fi) {
            case -1:
                JsonReader.skipValue(parser);
                break;
            case FM_size:
                size = JsonReader.StringReader.readField(parser, fieldName, size);
                break;
            case FM_bytes:
                bytes = JsonReader.readUnsignedLongField(parser, fieldName, bytes);
                break;
            case FM_path:
                path = JsonReader.StringReader.readField(parser, fieldName, path);
                break;
            case FM_is_dir:
                is_dir = JsonReader.BooleanReader.readField(parser, fieldName, is_dir);
                break;
            case FM_is_deleted:
                is_deleted = JsonReader.BooleanReader.readField(parser, fieldName, is_deleted);
                break;
            case FM_rev:
                rev = JsonReader.StringReader.readField(parser, fieldName, rev);
                break;
            case FM_thumb_exists:
                thumb_exists = JsonReader.BooleanReader.readField(parser, fieldName, thumb_exists);
                break;
            case FM_icon:
                icon = JsonReader.StringReader.readField(parser, fieldName, icon);
                break;
            case FM_modified:
                modified = JsonDateReader.Dropbox.readField(parser, fieldName, modified);
                break;
            case FM_client_mtime:
                client_mtime = JsonDateReader.Dropbox.readField(parser, fieldName, client_mtime);
                break;
            case FM_hash:
                if (collector == null) {
                    throw new JsonReadException(
                            "not expecting \"hash\" field, since we didn't ask for children",
                            parser.getCurrentLocation());
                }
                hash = JsonReader.StringReader.readField(parser, fieldName, hash);
                break;
            case FM_contents:
                if (collector == null) {
                    throw new JsonReadException(
                            "not expecting \"contents\" field, since we didn't ask for children",
                            parser.getCurrentLocation());
                }
                contents = JsonArrayReader.mk(Reader, collector).readField(parser, fieldName, contents);
                break;
            default:
                throw new AssertionError("bad index: " + fi + ", field = \"" + fieldName + "\"");
            }
        } catch (final JsonReadException ex) {
            throw ex.addFieldContext(fieldName);
        }
    }

    JsonReader.expectObjectEnd(parser);

    if (path == null) {
        throw new JsonReadException("missing field \"path\"", top);
    }
    if (icon == null) {
        throw new JsonReadException("missing field \"icon\"", top);
    }
    if (is_deleted == null) {
        is_deleted = Boolean.FALSE;
    }
    if (is_dir == null) {
        is_dir = Boolean.FALSE;
    }
    if (thumb_exists == null) {
        thumb_exists = Boolean.FALSE;
    }

    if (is_dir && (contents != null || hash != null)) {
        if (hash == null) {
            throw new JsonReadException("missing \"hash\", when we asked for children", top);
        }
        if (contents == null) {
            throw new JsonReadException("missing \"contents\", when we asked for children", top);
        }
    }

    DbxEntry e;
    if (is_dir) {
        e = new Folder(path, icon, thumb_exists);
    } else {
        // Normal File
        if (size == null) {
            throw new JsonReadException("missing \"size\" for a file entry", top);
        }
        if (bytes == -1) {
            throw new JsonReadException("missing \"bytes\" for a file entry", top);
        }
        if (modified == null) {
            throw new JsonReadException("missing \"modified\" for a file entry", top);
        }
        if (client_mtime == null) {
            throw new JsonReadException("missing \"client_mtime\" for a file entry", top);
        }
        if (rev == null) {
            throw new JsonReadException("missing \"rev\" for a file entry", top);
        }
        e = new File(path, icon, thumb_exists, bytes, size, modified, client_mtime, rev);
    }

    return new WithChildrenC<C>(e, hash, contents);
}