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:org.mongojack.internal.CalendarDeserializer.java

@Override
public Calendar deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken token = jp.getCurrentToken();
    Date date;/*from  w w w  .j  a  v  a  2  s  . c  o m*/
    if (token == JsonToken.VALUE_EMBEDDED_OBJECT) {
        // See if it's a date
        Object object = jp.getEmbeddedObject();
        if (object instanceof Date) {
            date = (Date) object;
        } else {
            throw ctxt.mappingException(Calendar.class);
        }
    } else {
        date = _parseDate(jp, ctxt);
    }
    if (date == null) {
        return null;
    }
    return ctxt.constructCalendar(date);
}

From source file:webby.commons.io.jackson.OffsetDateTimeDeserializer.java

@Override
public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch (parser.getCurrentToken()) {
    case VALUE_STRING:
        String string = parser.getText().trim();
        if (prepareFn != null)
            string = prepareFn.apply(string);
        if (string == null || string.length() == 0)
            return null;
        return OffsetDateTime.parse(string, formatter);
    }/*www . j  a  va 2  s . c  o m*/

    throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}

From source file:io.swagger.inflector.config.DirectionDeserializer.java

@Override
public Set<Configuration.Direction> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    final JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.VALUE_FALSE) {
        return EnumSet.noneOf(Configuration.Direction.class);
    } else if (token == JsonToken.VALUE_TRUE) {
        return EnumSet.allOf(Configuration.Direction.class);
    } else if (token == JsonToken.START_ARRAY) {
        final Set<Configuration.Direction> items = EnumSet.noneOf(Configuration.Direction.class);
        while (true) {
            final JsonToken next = jp.nextToken();
            if (next == JsonToken.VALUE_STRING) {
                final String name = jp.getText();
                items.add(Configuration.Direction.valueOf(name));
            } else if (next == JsonToken.END_ARRAY) {
                return items;
            } else {
                break;
            }/*from w  ww . j  ava2s. com*/
        }
    }
    throw ctxt.mappingException(Configuration.Direction.class);
}

From source file:org.springframework.data.convert.JsonBooleanDeserializer.java

@Override
public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken currentToken = jp.getCurrentToken();

    if (currentToken.equals(JsonToken.VALUE_STRING)) {
        String text = jp.getText().trim();

        if (YES.equalsIgnoreCase(text)) {
            return Boolean.TRUE;
        } else if (NO.equalsIgnoreCase(text)) {
            return Boolean.FALSE;
        }// w w  w .  j a va2s.com

        throw ctxt.weirdStringException(text, Boolean.class,
                "Only \"" + YES + "\" or \"" + NO + "\" values supported");
    } else if (currentToken.equals(JsonToken.VALUE_NULL)) {
        return getNullValue();
    }

    throw ctxt.mappingException(Boolean.class);
}

From source file:org.mayocat.jackson.OptionalStringListDeserializer.java

@Override
public Optional<List<String>> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (jp.getCurrentToken().isScalarValue()) {
        // value is only one string
        return Optional.of(Arrays.asList(jp.getValueAsString()));
    } else {//from www .  j a v  a  2  s  .co m
        // Value is a list of strings
        List<String> result = Lists.newArrayList();
        while (jp.nextToken().isScalarValue()) {
            String value = jp.getValueAsString();
            result.add(value);
        }
        return Optional.of(result);
    }
}

From source file:com.clicktravel.infrastructure.persistence.aws.cloudsearch.client.JodaDateTimeDeserializer.java

@Override
public DateTime deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (jp.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw ctxt.mappingException("Expected JSON string");
    }/*from www. ja  v a  2s. c om*/
    return formatter.parseDateTime(jp.getText());
}

From source file:de.taimos.dvalin.interconnect.model.DateTimeDeserializerWithTZ.java

@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new DateTime(jp.getLongValue(), DateTimeZone.forTimeZone(ctxt.getTimeZone()));
    }//from  ww  w . j  av a  2  s  .  co  m
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (str.length() == 0) { // [JACKSON-360]
            return null;
        }
        // catch serialized time zones
        if ((str.charAt(str.length() - 6) == '+') || (str.charAt(str.length() - 1) == 'Z')
                || (str.charAt(str.length() - 6) == '-')) {
            return new DateTime(str);
        }
        return new DateTime(str, DateTimeZone.forTimeZone(ctxt.getTimeZone()));
    }
    ctxt.handleUnexpectedToken(this.handledType(), jp);
    // never reached
    return null;
}

From source file:fi.hsl.parkandride.core.domain.StrictIsoDateTimeDeserializer.java

@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (!isValid(str)) {
            throw ctxt.mappingException("expected ISO 8601 date time with timezone, " + "for example \""
                    + DateTime.now() + "\", but got \"" + str + "\"");
        }/* w  w  w  .ja  v  a 2 s .  c  o  m*/
        return (DateTime) dateTimeDeserializer.deserialize(jp, ctxt);
    }
    throw ctxt.mappingException(dateTimeDeserializer.handledType(), token);
}

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

@Override
public Locale deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (str.length() == 0) {
            return null;
        }/*from   w  w w  .  j a  va2s  . com*/

        return toLocale(str);
    }

    throw ctxt.mappingException(handledType());
}

From source file:org.springframework.social.linkedin.api.impl.json.CodeDeserializer.java

@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (jp.hasCurrentToken() && jp.getCurrentToken().equals(JsonToken.START_OBJECT)) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.reader(JsonNode.class).readValue(jp);
        return node.has(VALUE) ? node.get(VALUE).textValue() : null;
    }/*from w ww.  j a v  a  2s.co  m*/

    throw ctxt.mappingException("Expected JSON object");
}