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

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

Introduction

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

Prototype

public abstract JsonToken nextToken() throws IOException, JsonParseException;

Source Link

Document

Main iteration method, which will advance stream enough to determine type of the next token, if any.

Usage

From source file:com.erigir.maven.plugin.processor.JSONValidator.java

@Override
public void validate(File input) throws MojoExecutionException {
    try {/*from w w w  .  ja va2 s  . c  o  m*/
        final JsonParser parser = new ObjectMapper().getFactory().createParser(input);

        while (parser.nextToken() != null) {
        }

    } catch (IOException e) {
        throw new MojoExecutionException("JSON validation failed for: " + input, e);
    }
}

From source file:org.bonitasoft.web.designer.model.JacksonObjectMapper.java

public void checkValidJson(byte[] bytes) throws IOException, JsonProcessingException {
    JsonParser parser = objectMapper.getFactory().createParser(bytes);
    while (parser.nextToken() != null) {
        // do nothing, will throw JsonProcessingException if error occurs
    }/*from   w ww. j  a v  a 2 s.  c  o  m*/
}

From source file:monasca.log.api.app.validation.LogApplicationTypeValidationTest.java

private String getMessage(String json) throws JsonParseException, IOException {
    JsonFactory factory = new JsonFactory();
    JsonParser jp = factory.createParser(json);
    jp.nextToken();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        jp.nextToken();/*from w w w.  j  av  a 2  s .  c o m*/
        if ("message".equals(fieldname)) {

            return jp.getText();
        }
    }
    jp.close();
    return null;
}

From source file:com.unboundid.scim2.common.utils.Parser.java

/**
 * Read a filter from the reader.//  www  . j a va 2 s  . com
 *
 * @param reader The reader to read the filter from.
 * @param isValueFilter Whether to read the filter as a value filter.
 * @return The parsed filter.
 * @throws BadRequestException If the filter string could not be parsed.
 */
private static Filter readFilter(final StringReader reader, final boolean isValueFilter)
        throws BadRequestException {
    final Stack<Filter> outputStack = new Stack<Filter>();
    final Stack<String> precedenceStack = new Stack<String>();

    String token;
    String previousToken = null;

    while ((token = readFilterToken(reader, isValueFilter)) != null) {
        if (token.equals("(") && expectsNewFilter(previousToken)) {
            precedenceStack.push(token);
        } else if (token.equalsIgnoreCase(FilterType.NOT.getStringValue()) && expectsNewFilter(previousToken)) {
            // "not" should be followed by an (
            String nextToken = readFilterToken(reader, isValueFilter);
            if (nextToken == null) {
                throw BadRequestException.invalidFilter("Unexpected end of filter string");
            }
            if (!nextToken.equals("(")) {
                final String msg = String.format("Expected '(' at position %d", reader.mark);
                throw BadRequestException.invalidFilter(msg);
            }
            precedenceStack.push(token);
        } else if (token.equals(")") && !expectsNewFilter(previousToken)) {
            String operator = closeGrouping(precedenceStack, outputStack, false);
            if (operator == null) {
                final String msg = String.format(
                        "No opening parenthesis matching closing " + "parenthesis at position %d", reader.mark);
                throw BadRequestException.invalidFilter(msg);
            }
            if (operator.equalsIgnoreCase(FilterType.NOT.getStringValue())) {
                // Treat "not" the same as "(" except wrap everything in a not filter.
                outputStack.push(Filter.not(outputStack.pop()));
            }
        } else if (token.equalsIgnoreCase(FilterType.AND.getStringValue())
                && !expectsNewFilter(previousToken)) {
            // and has higher precedence than or.
            precedenceStack.push(token);
        } else if (token.equalsIgnoreCase(FilterType.OR.getStringValue()) && !expectsNewFilter(previousToken)) {
            // pop all the pending ands first before pushing or.
            LinkedList<Filter> andComponents = new LinkedList<Filter>();
            while (!precedenceStack.isEmpty()) {
                if (precedenceStack.peek().equalsIgnoreCase(FilterType.AND.getStringValue())) {
                    precedenceStack.pop();
                    andComponents.addFirst(outputStack.pop());
                } else {
                    break;
                }
                if (!andComponents.isEmpty()) {
                    andComponents.addFirst(outputStack.pop());
                    outputStack.push(Filter.and(andComponents));
                }
            }

            precedenceStack.push(token);
        } else if (token.endsWith("[") && expectsNewFilter(previousToken)) {
            // This is a complex value filter.
            final Path filterAttribute;
            try {
                filterAttribute = parsePath(token.substring(0, token.length() - 1));
            } catch (final BadRequestException e) {
                Debug.debugException(e);
                final String msg = String.format("Invalid attribute path at position %d: %s", reader.mark,
                        e.getMessage());
                throw BadRequestException.invalidFilter(msg);
            }

            if (filterAttribute.isRoot()) {
                final String msg = String.format("Attribute path expected at position %d", reader.mark);
                throw BadRequestException.invalidFilter(msg);
            }

            outputStack.push(Filter.hasComplexValue(filterAttribute, readFilter(reader, true)));
        } else if (isValueFilter && token.equals("]") && !expectsNewFilter(previousToken)) {
            break;
        } else if (expectsNewFilter(previousToken)) {
            // This must be an attribute path followed by operator and maybe value.
            final Path filterAttribute;
            try {
                filterAttribute = parsePath(token);
            } catch (final BadRequestException e) {
                Debug.debugException(e);
                final String msg = String.format("Invalid attribute path at position %d: %s", reader.mark,
                        e.getMessage());
                throw BadRequestException.invalidFilter(msg);
            }

            if (filterAttribute.isRoot()) {
                final String msg = String.format("Attribute path expected at position %d", reader.mark);
                throw BadRequestException.invalidFilter(msg);
            }

            String op = readFilterToken(reader, isValueFilter);

            if (op == null) {
                throw BadRequestException.invalidFilter("Unexpected end of filter string");
            }

            if (op.equalsIgnoreCase(FilterType.PRESENT.getStringValue())) {
                outputStack.push(Filter.pr(filterAttribute));
            } else {
                ValueNode valueNode;
                try {
                    // Mark the beginning of the JSON value so we can later reset back
                    // to this position and skip the actual chars that were consumed
                    // by Jackson. The Jackson parser is buffered and reads everything
                    // until the end of string.
                    reader.mark(0);
                    ScimJsonFactory scimJsonFactory = (ScimJsonFactory) JsonUtils.getObjectReader()
                            .getFactory();
                    JsonParser parser = scimJsonFactory.createScimFilterParser(reader);
                    // The object mapper will return a Java null for JSON null.
                    // Have to distinguish between reading a JSON null and encountering
                    // the end of string.
                    if (parser.getCurrentToken() == null && parser.nextToken() == null) {
                        // End of string.
                        valueNode = null;
                    } else {
                        valueNode = parser.readValueAsTree();

                        // This is actually a JSON null. Use NullNode.
                        if (valueNode == null) {
                            valueNode = JsonUtils.getJsonNodeFactory().nullNode();
                        }
                    }
                    // Reset back to the beginning of the JSON value.
                    reader.reset();
                    // Skip the number of chars consumed by JSON parser.
                    reader.skip(parser.getCurrentLocation().getCharOffset());
                } catch (IOException e) {
                    final String msg = String.format("Invalid comparison value at position %d: %s", reader.mark,
                            e.getMessage());
                    throw BadRequestException.invalidFilter(msg);
                }

                if (valueNode == null) {
                    throw BadRequestException.invalidFilter("Unexpected end of filter string");
                }

                if (op.equalsIgnoreCase(FilterType.EQUAL.getStringValue())) {
                    outputStack.push(Filter.eq(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.NOT_EQUAL.getStringValue())) {
                    outputStack.push(Filter.ne(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.CONTAINS.getStringValue())) {
                    outputStack.push(Filter.co(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.STARTS_WITH.getStringValue())) {
                    outputStack.push(Filter.sw(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.ENDS_WITH.getStringValue())) {
                    outputStack.push(Filter.ew(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.GREATER_THAN.getStringValue())) {
                    outputStack.push(Filter.gt(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.GREATER_OR_EQUAL.getStringValue())) {
                    outputStack.push(Filter.ge(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.LESS_THAN.getStringValue())) {
                    outputStack.push(Filter.lt(filterAttribute, valueNode));
                } else if (op.equalsIgnoreCase(FilterType.LESS_OR_EQUAL.getStringValue())) {
                    outputStack.push(Filter.le(filterAttribute, valueNode));
                } else {
                    final String msg = String.format("Unrecognized attribute operator '%s' at position %d. "
                            + "Expected: eq,ne,co,sw,ew,pr,gt,ge,lt,le", op, reader.mark);
                    throw BadRequestException.invalidFilter(msg);
                }
            }
        } else {
            final String msg = String.format("Unexpected character '%s' at position %d", token, reader.mark);
            throw BadRequestException.invalidFilter(msg);
        }
        previousToken = token;
    }

    closeGrouping(precedenceStack, outputStack, true);

    if (outputStack.isEmpty()) {
        throw BadRequestException.invalidFilter("Unexpected end of filter string");
    }
    return outputStack.pop();
}

From source file:com.github.jonpeterson.jackson.module.interceptor.JsonInterceptingDeserializer.java

@Override
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode jsonNode = parser.readValueAsTree();

    for (JsonInterceptor interceptor : interceptors)
        jsonNode = interceptor.intercept(jsonNode, context.getNodeFactory());

    JsonParser postInterceptionParser = new TreeTraversingParser(jsonNode, parser.getCodec());
    postInterceptionParser.nextToken();
    return delegate.deserialize(postInterceptionParser, context);
}

From source file:ch.rasc.wampspring.message.CallResultMessage.java

public CallResultMessage(JsonParser jp) throws IOException {
    super(WampMessageType.CALLRESULT);

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }/*from  ww  w.j av a 2 s.c  o m*/
    this.callID = jp.getValueAsString();

    jp.nextToken();
    this.result = jp.readValueAs(Object.class);
}

From source file:ch.rasc.wampspring.message.PrefixMessage.java

public PrefixMessage(JsonParser jp) throws IOException {
    super(WampMessageType.PREFIX);

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }//w  w  w.ja va 2  s  . c o m
    this.prefix = jp.getValueAsString();

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }
    this.uri = jp.getValueAsString();
}

From source file:KV78Tester.java

public void checkLines(BufferedReader in) throws JsonParseException, IOException {
    JsonFactory f = new JsonFactory();
    JsonParser jp = f.createJsonParser(in);
    String line = "";
    jp.nextToken();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String namefield = jp.getCurrentName();
        if (namefield != null && namefield.contains("_")) {
            line = namefield;/*from ww  w . ja  v a 2  s.  c om*/
        }
        jp.nextToken();
        if ("Actuals".equals(namefield)) {
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                jp.getCurrentName();
                jp.nextToken();
                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    jp.nextToken();
                }
            }
        } else if ("Network".equals(namefield)) {
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                int userstop = Integer.parseInt(jp.getCurrentName());
                jp.nextToken();
                checkStop(jp, line, userstop);
            }
        }
    }
}

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

@Override
protected void read(BidResponse.Builder msg, JsonParser par) throws IOException {
    if ("test4arr".equals(getCurrentName(par))) {
        for (startArray(par); endArray(par); par.nextToken()) {
            msg.addExtension(TestExt.testResponse4, par.getIntValue());
        }//from w w  w  .j  av  a  2s  .  co  m
    }
}

From source file:ch.rasc.wampspring.message.SubscribeMessage.java

public SubscribeMessage(JsonParser jp, WampSession wampSession) throws IOException {
    super(WampMessageType.SUBSCRIBE);

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }//w  w w .ja va2  s.c  om
    setTopicURI(replacePrefix(jp.getValueAsString(), wampSession));
}