Java Code Examples for com.fasterxml.jackson.core.JsonParser#getTokenLocation()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getTokenLocation() . 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: JsonReader.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
/**
 * Helper to read and parse the optional ".tag" field. If one is found, positions the parser
 * at the next field (or the closing brace); otherwise leaves the parser position unchanged.
 * Returns null if there isn't a ".tag" field; otherwise an array of strings (the tags).
 * Initially the parser must be positioned right after the opening brace.
 */
public static String[] readTags(JsonParser parser)
    throws IOException, JsonReadException
{
    if (parser.getCurrentToken() != JsonToken.FIELD_NAME) {
        return null;
    }
    if (!".tag".equals(parser.getCurrentName())) {
        return null;
    }
    parser.nextToken();
    if (parser.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw new JsonReadException("expected a string value for .tag field", parser.getTokenLocation());
    }
    String tag = parser.getText();
    parser.nextToken();
    return tag.split("\\.");
}
 
Example 2
Source File: DbxAppInfo.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
@Override
public String read(JsonParser parser) throws IOException, JsonReadException
{
    try {
        String v = parser.getText();
        String error = getKeyFormatError(v);
        if (error != null) {
            throw new JsonReadException("bad format for app secret: " + error, parser.getTokenLocation());
        }
        parser.nextToken();
        return v;
    }
    catch (JsonParseException ex) {
        throw JsonReadException.fromJackson(ex);
    }
}
 
Example 3
Source File: DbxAppInfo.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
@Override
public String read(JsonParser parser) throws IOException, JsonReadException
{
    try {
        String v = parser.getText();
        String error = getKeyFormatError(v);
        if (error != null) {
            throw new JsonReadException("bad format for app key: " + error, parser.getTokenLocation());
        }
        parser.nextToken();
        return v;
    }
    catch (JsonParseException ex) {
        throw JsonReadException.fromJackson(ex);
    }
}
 
Example 4
Source File: DbxEntry.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
@Override
public T read(JsonParser parser)
    throws IOException, JsonReadException
{
    JsonToken token = parser.getCurrentToken();
    if (token == JsonToken.VALUE_STRING) {
        String s = parser.getText();
        if (!s.equals("pending")) {
            throw new JsonReadException("got a string, but the value wasn't \"pending\"", parser.getTokenLocation());
        }
        parser.nextToken();
        return pendingValue;
    } else {
        return reader.read(parser);
    }
}
 
Example 5
Source File: NodeDeserializer.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public AbstractNode deserialize(JsonParser p, DeserializationContext context)
        throws IOException, JsonProcessingException {

    JsonLocation startLocation = p.getTokenLocation();
    if (p.getCurrentToken() == JsonToken.FIELD_NAME) {
        p.nextToken();
    }

    switch (p.getCurrentToken()) {
    case START_OBJECT:
        return deserializeObjectNode(p, context, startLocation);
    case START_ARRAY:
        return deserializeArrayNode(p, context, startLocation);
    default:
        return deserializeValueNode(p, context, startLocation);
    }
}
 
Example 6
Source File: JSONObjectException.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
public static JSONObjectException from(JsonParser p, Throwable problem,
        String msg, Object... args)
{
    if (args.length > 0) {
        msg = String.format(msg, args);
    }
    return new JSONObjectException(msg, ((p == null) ? null : p.getTokenLocation()), problem);
}
 
Example 7
Source File: DbxAuthFinish.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
@Override
public String read(JsonParser parser) throws IOException, JsonReadException {
    try {
        String v = parser.getText();
        if (!v.equals("Bearer") && !v.equals("bearer")) {
            throw new JsonReadException("expecting \"Bearer\": got " + jq(v), parser.getTokenLocation());
        }
        parser.nextToken();
        return v;
    } catch (JsonParseException ex) {
        throw JsonReadException.fromJackson(ex);
    }
}
 
Example 8
Source File: DbxAuthFinish.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
@Override
public String read(JsonParser parser) throws IOException, JsonReadException {
    try {
        String v = parser.getText();
        String error = DbxAppInfo.getTokenPartError(v);
        if (error != null) {
            throw new JsonReadException(error, parser.getTokenLocation());
        }
        parser.nextToken();
        return v;
    } catch (JsonParseException ex) {
        throw JsonReadException.fromJackson(ex);
    }
}
 
Example 9
Source File: JSONOptions.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
    public JSONOptions deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
        JsonProcessingException {
      JsonLocation l = jp.getTokenLocation();
//      logger.debug("Reading tree.");
      TreeNode n = jp.readValueAsTree();
//      logger.debug("Tree {}", n);
      if (n instanceof JsonNode) {
        return new JSONOptions( (JsonNode) n, l);
      } else {
        throw new IllegalArgumentException(String.format("Received something other than a JsonNode %s", n));
      }
    }
 
Example 10
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static JsonLocation expectObjectStart(JsonParser parser)
    throws IOException, JsonReadException
{
    if (parser.getCurrentToken() != JsonToken.START_OBJECT) {
        throw new JsonReadException("expecting the start of an object (\"{\")", parser.getTokenLocation());
    }
    JsonLocation loc = parser.getTokenLocation();
    nextToken(parser);
    return loc;
}
 
Example 11
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static void expectObjectEnd(JsonParser parser)
    throws IOException, JsonReadException
{
    if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        throw new JsonReadException("expecting the end of an object (\"}\")", parser.getTokenLocation());
    }
    nextToken(parser);
}
 
Example 12
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static JsonLocation expectArrayStart(JsonParser parser)
    throws IOException, JsonReadException
{
    if (parser.getCurrentToken() != JsonToken.START_ARRAY) {
        throw new JsonReadException("expecting the start of an array (\"[\")", parser.getTokenLocation());
    }
    JsonLocation loc = parser.getTokenLocation();
    nextToken(parser);
    return loc;
}
 
Example 13
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static JsonLocation expectArrayEnd(JsonParser parser)
    throws IOException, JsonReadException
{
    if (parser.getCurrentToken() != JsonToken.END_ARRAY) {
        throw new JsonReadException("expecting the end of an array (\"[\")", parser.getTokenLocation());
    }
    JsonLocation loc = parser.getTokenLocation();
    nextToken(parser);
    return loc;
}
 
Example 14
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public Long read(JsonParser parser)
        throws IOException, JsonReadException
{
    long v = readUnsignedLong(parser);
    if (v >= 4294967296L) {
        throw new JsonReadException("expecting a 32-bit unsigned integer, got: " + v, parser.getTokenLocation());
    }
    return v;
}
 
Example 15
Source File: JSONObjectException.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
public static JSONObjectException from(JsonParser p, String msg, Object... args) {
    if (args.length > 0) {
        msg = String.format(msg, args);
    }
    return new JSONObjectException(msg, ((p == null) ? null : p.getTokenLocation()));
}
 
Example 16
Source File: JSONObjectException.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
public static JSONObjectException from(JsonParser p, String msg) {
    return new JSONObjectException(msg, ((p == null) ? null : p.getTokenLocation()));
}
 
Example 17
Source File: ProjectDeserializer.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private void validate( JsonParser jsonParser, String input, String expected ) throws JsonProcessingException {
    if ( ! input.equals( expected ) ) {
        throw new JsonParseException( "Unexpected token: " + input, jsonParser.getTokenLocation() );
    }
}
 
Example 18
Source File: CoordinatedClusterDeserializer.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private void validate( JsonParser jsonParser, String input, String expected ) throws JsonProcessingException {
    if ( ! input.equals( expected ) ) {
        throw new JsonParseException( "Unexpected token: " + input, jsonParser.getTokenLocation() );
    }
}
 
Example 19
Source File: DbxHost.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public DbxHost read(JsonParser parser) throws IOException, JsonReadException {
    JsonToken t = parser.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String s = parser.getText();
        JsonReader.nextToken(parser);
        return DbxHost.fromBaseHost(s);
    } else if (t == JsonToken.START_OBJECT) {
        JsonLocation top = parser.getTokenLocation();
        nextToken(parser);

        String api = null;
        String content = null;
        String web = null;
        String notify = null;

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

            try {
                if (fieldName.equals("api")) {
                    api = JsonReader.StringReader.readField(parser, fieldName, api);
                }
                else if (fieldName.equals("content")) {
                    content = JsonReader.StringReader.readField(parser, fieldName, content);
                }
                else if (fieldName.equals("web")) {
                    web = JsonReader.StringReader.readField(parser, fieldName, web);
                }
                else if (fieldName.equals("notify")) {
                    notify = JsonReader.StringReader.readField(parser, fieldName, notify);
                }
                else {
                    throw new JsonReadException("unknown field", parser.getCurrentLocation());
                }
            }
            catch (JsonReadException ex) {
                throw ex.addFieldContext(fieldName);
            }
        }

        JsonReader.expectObjectEnd(parser);

        if (api == null) throw new JsonReadException("missing field \"api\"", top);
        if (content == null) throw new JsonReadException("missing field \"content\"", top);
        if (web == null) throw new JsonReadException("missing field \"web\"", top);
        if (notify == null) throw new JsonReadException("missing field \"notify\"", top);

        return new DbxHost(api, content, web, notify);
    } else {
        throw new JsonReadException("expecting a string or an object", parser.getTokenLocation());
    }
}
 
Example 20
Source File: RunnerDeserializer.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private void validate( JsonParser jsonParser, String input, String expected ) throws JsonProcessingException {
    if ( ! input.equals( expected ) ) {
        throw new JsonParseException( "Unexpected token: " + input, jsonParser.getTokenLocation() );
    }
}