Java Code Examples for com.fasterxml.jackson.core.JsonToken#isStructStart()

The following examples show how to use com.fasterxml.jackson.core.JsonToken#isStructStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonWeatherParser.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Iterates through the JSON structure and collects weather data.
 */
private void handleToken(JsonParser jp, String property, Weather weather) throws Exception {
    JsonToken token = jp.getCurrentToken();
    String prop = PropertyResolver.add(property, jp.getCurrentName());

    if (token.isStructStart()) {
        boolean isObject = token == JsonToken.START_OBJECT || token == JsonToken.END_OBJECT;

        Weather forecast = !isObject ? weather : startIfForecast(weather, prop);
        while (!jp.nextValue().isStructEnd()) {
            handleToken(jp, prop, forecast);
        }
        if (isObject) {
            endIfForecast(weather, forecast, prop);
        }
    } else {
        try {
            setValue(weather, prop, jp.getValueAsString());
        } catch (Exception ex) {
            logger.error("Error setting property '{}' with value '{}' ({})", prop, jp.getValueAsString(),
                    ex.getMessage());
        }
    }
}
 
Example 2
Source File: BaseDeserializer.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Helper used to skip to the end of the current object.  Useful for forwards compatibility.
 *
 * @param jparser The parser to use for extraction.
 * @throws IOException If there is an error parsing.
 */
protected void ignoreRestOfObject(JsonParser jparser)
        throws IOException
{
    if (jparser.getCurrentToken().isStructEnd()) {
        return;
    }

    int open = 1;
    /* Since proper matching of start/end markers is handled
     * by nextToken(), we'll just count nesting levels here
     */
    while (true) {
        JsonToken t = jparser.nextToken();
        if (t == null) {
            throw new IllegalStateException("Expected " + JsonToken.END_OBJECT + " found " + jparser.getText());
        }
        if (t.isStructStart()) {
            ++open;
        }
        else if (t.isStructEnd()) {
            if (--open == 0) {
                return;
            }
        }
    }
}
 
Example 3
Source File: TokenBuffer.java    From vespa with Apache License 2.0 5 votes vote down vote up
private int nestingOffset(JsonToken t) {
    if (t.isStructStart()) {
        return 1;
    } else if (t.isStructEnd()) {
        return -1;
    } else {
        return 0;
    }
}
 
Example 4
Source File: Session.java    From jlibs with Apache License 2.0 5 votes vote down vote up
private boolean needsAcknowledgement(JsonParser parser) throws IOException{
    boolean acknowledge = false;
    boolean readAcknowledge = false;

    int open = 1;
    while(true){
        JsonToken t = parser.nextToken();
        if(readAcknowledge){
            readAcknowledge = false;
            if(t==JsonToken.VALUE_TRUE)
                acknowledge = true;
            else if(t==JsonToken.VALUE_FALSE)
                acknowledge = false;
        }

        if(t==null)
            throw new JsonParseException("unexpected EOF", parser.getCurrentLocation());
        else if(t.isStructStart())
            open++;
        else if(t.isStructEnd()){
            if(--open==0)
                return acknowledge;
        }else if(open==1 && t==JsonToken.FIELD_NAME && parser.getText().equals("acknowledge")){
            readAcknowledge = true;
        }
    }
}
 
Example 5
Source File: OpenRtbJsonUtils.java    From openrtb with Apache License 2.0 5 votes vote down vote up
/**
 * Skips a field name if necessary, returning the current token then, which must be
 * the start of an Array or Object: '{' or '['.
 */
public static JsonToken peekStructStart(JsonParser par) throws IOException {
  JsonToken token = peekToken(par);
  if (token.isStructStart()) {
    return token;
  } else {
    throw new JsonParseException(par, "Expected start of array or object");
  }
}
 
Example 6
Source File: RestExecutor.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Override public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonToken startTok = p.getCurrentToken();

    if (startTok.isStructStart()) {
        StringWriter wrt = new StringWriter(4096);

        JsonGenerator gen = factory.createGenerator(wrt);

        JsonToken tok = startTok, endTok = startTok == START_ARRAY ? END_ARRAY : END_OBJECT;

        int cnt = 1;

        while (cnt > 0) {
            writeToken(tok, p, gen);

            tok = p.nextToken();

            if (tok == startTok)
                cnt++;
            else if (tok == endTok)
                cnt--;
        }

        gen.close();

        return wrt.toString();
    }

    return p.getValueAsString();
}