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:json.internal.BaseJValueObjectDeserializer.java

public JValue deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
    switch (jp.getCurrentToken()) {
    case START_ARRAY:
        return parseArray(jp, ctx);
    case START_OBJECT:
        return parseObject(jp, ctx);
    case VALUE_STRING:
        return jString(jp.getText());
    case VALUE_NUMBER_INT:
    case VALUE_NUMBER_FLOAT:
        return JNumber$.MODULE$.apply(_parseDouble(jp, ctx));
    case VALUE_TRUE:
        return JTrue$.MODULE$;
    case VALUE_FALSE:
        return JFalse$.MODULE$;
    case VALUE_NULL:
        return JNull$.MODULE$;
    case VALUE_EMBEDDED_OBJECT:
        throwException("JSON parser - unexpected embedded object");
        return null;
    case END_OBJECT:
        throwException("JSON parser - unexpected end of object");
        return null;
    case FIELD_NAME:
        throwException("JSON parser - unexpected field name");
        return null;
    case END_ARRAY:
        throwException("JSON parser - unexpected end of array");
        return null;
    case NOT_AVAILABLE:
        throwException("JSON parser - unexpected end of token");
        return null;
    default://from w  ww .j  a va  2  s  .  com
        throwException("JSON parser - unexpected token " + jp.getCurrentToken());
        return null;
    }
}

From source file:com.bitsofproof.dropwizard.supernode.jackson.AddressDeserializer.java

@Override
public Address deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        try {/*from   w w w . ja va 2 s .c  om*/
            String satoshiStyle = jp.getText().trim();
            if (satoshiStyle.length() == 0) {
                return null;
            }

            return Address.fromSatoshiStyle(satoshiStyle);
        } catch (ValidationException e) {
            throw JsonMappingException.from(jp, "Error deserializing bitcoin address", e);
        }
    }

    throw ctxt.mappingException(getValueClass());
}

From source file:com.animedetour.api.sched.deserialization.PanelDateDeserializer.java

@Override
public ReadableInstant deserialize(JsonParser parser, DeserializationContext context)
        throws IOException, JsonProcessingException {
    JsonToken token = parser.getCurrentToken();

    if (token != JsonToken.VALUE_STRING) {
        throw context.mappingException("Expected string value for date");
    }//from   w w w  .  j av  a2 s.  c o m

    String value = parser.getText().trim();

    if (value.length() == 0) {
        return null;
    }

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    return formatter.parseDateTime(value);
}

From source file:com.basho.riak.client.raw.http.QuorumDeserializer.java

@Override
public Quorum deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken token = jp.getCurrentToken();
    switch (token) {
    case VALUE_STRING: {
        return new Quorum(Quora.fromString(jp.getText()));
    }//from   ww w .  j a  v a 2 s  . c om
    case VALUE_NUMBER_INT: {
        return new Quorum(jp.getIntValue());
    }
    case VALUE_NULL: {
        return null;
    }
    default:
        break;
    }
    throw ctxt.mappingException(Quorum.class);
}

From source file:eu.trentorise.opendata.jackan.ckan.CkanDateDeserializer.java

@Override
public ReadableDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();

    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (NONE.equals(str)) {
            return null;
        }//from w w w.  ja v a 2s .c  o m
    }

    return super.deserialize(jp, ctxt);
}

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

public static void extractTally(final Resource post, final JsonParser jsonParser,
        final ModifyingResourceProvider srp, final TallyOperationsService tallyOperationsService)
        throws IOException {
    jsonParser.nextToken(); // should be start object, but would be end array if no objects were present
    while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) {
        Long timestamp = null;/*  w w w  .j  a va 2  s  .  c o m*/
        String userIdentifier = null;
        String response = null;
        String tallyType = null;
        jsonParser.nextToken(); // should make current token by "FIELD_NAME" but could be END_OBJECT if this were
        // an empty object
        while (!jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) {
            final String label = jsonParser.getCurrentName();
            jsonParser.nextToken(); // should be FIELD_VALUE
            if (label.equals(TallyConstants.TIMESTAMP_PROPERTY)) {
                timestamp = jsonParser.getValueAsLong();
            } else {
                final String responseValue = jsonParser.getValueAsString();
                if (label.equals("response")) {
                    response = URLDecoder.decode(responseValue, "UTF-8");
                } else if (label.equals("userIdentifier")) {
                    userIdentifier = URLDecoder.decode(responseValue, "UTF-8");
                } else if (label.equals("tallyType")) {
                    tallyType = responseValue;
                }
            }
            jsonParser.nextToken(); // should make current token be "FIELD_NAME" unless we're at the end of our
            // loop and it's now "END_OBJECT" instead
        }
        if (timestamp != null && userIdentifier != null && response != null && tallyType != null) {
            createTally(srp, post, tallyType, userIdentifier, timestamp, response, tallyOperationsService);
        }
        jsonParser.nextToken(); // may advance to "START_OBJECT" if we're not finished yet, but might be
        // "END_ARRAY" now
    }
}

From source file:com.spotify.ffwd.filter.FilterDeserializer.java

@Override
public Filter deserialize(JsonParser p, DeserializationContext ctx)
        throws IOException, JsonProcessingException {
    if (p.getCurrentToken() != JsonToken.START_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.START_ARRAY, null);
    }//from   w  ww . j  av  a2  s.c  o m

    final String id = p.nextTextValue();

    final PartialDeserializer d = suppliers.get(id);

    if (d == null) {
        throw ctx.weirdStringException(id, Filter.class,
                String.format("Expected one of %s", suppliers.keySet()));
    }

    final Filter instance = d.deserialize(p, ctx);

    if (p.getCurrentToken() != JsonToken.END_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.END_ARRAY, null);
    }

    return instance;
}

From source file:com.basistech.rosette.dm.jackson.array.ListAttributeArrayDeserializer.java

@Override
@SuppressWarnings("unchecked")
public ListAttribute deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp.getCurrentToken() == JsonToken.START_ARRAY) { // this is what we expect.
        // we advance to be in the same place the 'else' will be -- the first FIELD_NAME.
        jp.nextToken();//from ww  w.  j  a  v  a 2s  .co  m
    } else {
        throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY,
                "ListAttributeDeserializer called not array start.");
    }

    if (jp.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw ctxt.mappingException("Expected VALUE_STRING for item type.");
    }
    String itemTypeKeyName = jp.getText();

    KnownAttribute attribute = KnownAttribute.getAttributeForKey(itemTypeKeyName);
    if (attribute == null) {
        attribute = KnownAttribute.UNKNOWN;
    }
    Class<? extends BaseAttribute> itemClass = attribute.attributeClass();

    ListAttribute.Builder<BaseAttribute> builder = new ListAttribute.Builder<>(attribute.attributeClass());
    List<BaseAttribute> items = Lists.newArrayList();

    if (jp.nextToken() != JsonToken.START_ARRAY) {
        throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "No array of values for list.");
    }

    // we just read the elements as we see them,
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        // the START_ARRAY case, which is _normal_. Read the elements.
        items.add(jp.readValueAs(itemClass));
    }
    builder.setItems(items);
    // we are still in the top-level array ...
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        throw ctxt.wrongTokenException(jp, JsonToken.START_OBJECT, "No extended properties for list.");
    }
    Map<String, Object> props = jp.readValueAs(new TypeReference<Map<String, Object>>() {
    });
    for (Map.Entry<String, Object> me : props.entrySet()) {
        builder.extendedProperty(me.getKey(), me.getValue());
    }
    jp.nextToken(); // consume the END_OBJECT of the extended props
    return builder.build();
}

From source file:com.inversoft.json.ZonedDateTimeDeserializer.java

@Override
public ZonedDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();
    long value;/* ww w  .j av  a 2  s  .c  o m*/
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
        value = jp.getLongValue();
    } else if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (str.length() == 0) {
            return null;
        }

        try {
            value = Long.parseLong(str);
        } catch (NumberFormatException e) {
            throw ctxt.mappingException(handledType());
        }
    } else {
        throw ctxt.mappingException(handledType());
    }

    return ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.UTC);
}

From source file:com.addthis.bundle.value.ValueDeserializer.java

@Override
public ValueObject deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();
    switch (t) {//from ww  w.  j  av a 2  s  .  c  o m
    case VALUE_TRUE:
    case VALUE_FALSE:
    case VALUE_STRING:
        return jp.readValueAs(ValueString.class);
    case VALUE_NUMBER_INT:
        return jp.readValueAs(ValueLong.class);
    case VALUE_NUMBER_FLOAT:
        return jp.readValueAs(ValueDouble.class);
    case START_ARRAY:
        return jp.readValueAs(ValueArray.class);
    case START_OBJECT:
    case FIELD_NAME:
        return jp.readValueAs(ValueMap.class);
    case END_OBJECT:
        // calling jp.readValueAs here will return null rather than an empty map, so make empty map in tokens
        jp.nextToken();
        ObjectNode objectNode = ctxt.getNodeFactory().objectNode();
        JsonParser emptyObjectParser = jp.getCodec().treeAsTokens(objectNode);
        emptyObjectParser.nextToken();
        return emptyObjectParser.readValueAs(ValueMap.class);
    case VALUE_EMBEDDED_OBJECT:
    default:
        throw ctxt.mappingException(handledType());
    }
}