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:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.wdtk.ModifiedMapDeserializer.java

@SuppressWarnings("unchecked")
@Override/*from  w  w  w  .  j a v  a2  s .  c  o m*/
public Map<K, V> deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    Map<K, V> result = null;
    if (p.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        result = new HashMap<>();
        p.nextToken();
        if (!p.getCurrentToken().equals(JsonToken.END_ARRAY)) {
            logger.warn("Array was not empty. Current token: " + p.getCurrentToken());
        }
    } else {
        Object tmp = defaultDeserializer.deserialize(p, ctxt);
        result = (Map<K, V>) tmp;
    }
    return result;
}

From source file:net.nullschool.grains.jackson.datatype.AbstractBasicConstMapDeserializer.java

@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.START_OBJECT) {
        token = jp.nextToken();//from w ww  .  ja va  2  s . c  om
    } else if (token != JsonToken.FIELD_NAME) {
        throw ctxt.mappingException(mapType.getRawClass());
    }

    if (token == JsonToken.END_OBJECT) {
        return emptyResult();
    }

    KeyDeserializer kd = keyDeserializer;
    JsonDeserializer<?> vd = valueDeserializer;
    TypeDeserializer vtd = valueTypeDeserializer;

    List<Object> keys = null;
    List<Object> values = null;
    Object key;
    Object value;
    do {
        String name = jp.getCurrentName();
        key = kd == null ? name : kd.deserializeKey(name, ctxt);
        value = jp.nextToken() != JsonToken.VALUE_NULL
                ? vtd == null ? vd.deserialize(jp, ctxt) : vd.deserializeWithType(jp, ctxt, vtd)
                : null;

        token = jp.nextToken();
        if (keys == null) {
            if (token == JsonToken.END_OBJECT) {
                return resultOf(key, value);
            }
            keys = new ArrayList<>();
            values = new ArrayList<>();
        }
        keys.add(key);
        values.add(value);
    } while (token != JsonToken.END_OBJECT);

    return asResult(keys, values);
}

From source file:net.nullschool.grains.jackson.datatype.GrainDeserializer.java

@Override
public Grain deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.START_OBJECT) {
        token = jp.nextToken();/*from   www.jav a2s  . c  om*/
    } else if (token != JsonToken.FIELD_NAME) {
        throw ctxt.mappingException(getValueClass());
    }

    if (token == JsonToken.END_OBJECT) {
        return factory.getDefaultValue();
    }

    GrainBuilder builder = factory.getNewBuilder();

    do {
        String key = jp.getCurrentName();
        PropertyReader reader = readers.get(key);
        token = jp.nextToken();
        Object value;
        if (token == JsonToken.VALUE_NULL) {
            value = null;
        } else if (reader != null) {
            value = reader.deserializer.deserialize(jp, ctxt);
        } else {
            switch (token) {
            case START_ARRAY:
                value = extArrayDeserializer.deserialize(jp, ctxt);
                break;
            case START_OBJECT:
                value = extObjectDeserializer.deserialize(jp, ctxt);
                break;
            default:
                value = extValueDeserializer.deserialize(jp, ctxt);
            }
        }

        builder.put(key, value);
    } while (jp.nextToken() == JsonToken.FIELD_NAME);

    return builder.build();
}

From source file:com.boxedfolder.carrot.domain.util.DateTimeDeserializer.java

@Override
public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
    DateTime dateTime = null;//  ww w .  jav a2  s  . c  o m
    try {
        JsonToken currentToken = jsonParser.getCurrentToken();
        System.out.println(jsonParser.getText());
        if (currentToken == JsonToken.VALUE_STRING) {
            String dateTimeAsString = jsonParser.getText().trim();
            DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZoneUTC();
            dateTime = formatter.parseDateTime(dateTimeAsString);
        }
    } catch (Exception e) {
        throw deserializationContext.mappingException(getClass());
    }

    return dateTime;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.v4.ActionDeserializer.java

@Override
protected Action doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final Action action = new Action();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Name".equals(jp.getCurrentName())) {
                action.setName(jp.nextTextValue());
            } else if ("IsBound".equals(jp.getCurrentName())) {
                action.setBound(BooleanUtils.toBoolean(jp.nextTextValue()));
            } else if ("EntitySetPath".equals(jp.getCurrentName())) {
                action.setEntitySetPath(jp.nextTextValue());
            } else if ("Parameter".equals(jp.getCurrentName())) {
                jp.nextToken();//from  w ww.j  av a2  s .  com
                action.getParameters().add(jp.getCodec().readValue(jp, Parameter.class));
            } else if ("ReturnType".equals(jp.getCurrentName())) {
                action.setReturnType(parseReturnType(jp, "Action"));
            } else if ("Annotation".equals(jp.getCurrentName())) {
                jp.nextToken();
                action.setAnnotation(jp.getCodec().readValue(jp, Annotation.class));
            }
        }
    }

    return action;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.AssociationDeserializer.java

@Override
public Association deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final Association association = new Association();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Name".equals(jp.getCurrentName())) {
                association.setName(jp.nextTextValue());
            } else if ("ReferentialConstraint".equals(jp.getCurrentName())) {
                jp.nextToken();//from  ww w .j a  va 2s.  co m
                association.setReferentialConstraint(jp.getCodec().readValue(jp, ReferentialConstraint.class));
            } else if ("End".equals(jp.getCurrentName())) {
                jp.nextToken();
                association.getEnds().add(jp.getCodec().readValue(jp, AssociationEnd.class));
            }
        }
    }

    return association;
}

From source file:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.OldAliasListDeserializer.java

@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    List<String> result;

    ObjectCodec codec = p.getCodec();/*from   w w w.  jav  a2 s  .  co  m*/

    if (p.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        result = codec.readValue(p, new TypeReference<List<String>>() {
        });
    } else {
        LinkedHashMap<Integer, String> map = codec.readValue(p,
                new TypeReference<LinkedHashMap<Integer, String>>() {
                });

        result = new ArrayList<String>(map.values());
    }
    return result;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.DataServicesDeserializer.java

@Override
public DataServices deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final DataServices dataServices = new DataServices();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("DataServiceVersion".equals(jp.getCurrentName())) {
                dataServices.setDataServiceVersion(jp.nextTextValue());
            } else if ("MaxDataServiceVersion".equals(jp.getCurrentName())) {
                dataServices.setMaxDataServiceVersion(jp.nextTextValue());
            } else if ("Schema".equals(jp.getCurrentName())) {
                jp.nextToken();/*from   w w  w  .j  av  a 2  s.  c om*/
                dataServices.getSchemas().add(jp.getCodec().readValue(jp, Schema.class));
            }
        }
    }

    return dataServices;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.AssociationSetDeserializer.java

@Override
public AssociationSet deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final AssociationSet associationSet = new AssociationSet();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Name".equals(jp.getCurrentName())) {
                associationSet.setName(jp.nextTextValue());
            } else if ("Association".equals(jp.getCurrentName())) {
                associationSet.setAssociation(jp.nextTextValue());
            } else if ("End".equals(jp.getCurrentName())) {
                jp.nextToken();//from w w w. j a  va  2 s.  com
                associationSet.getEnds().add(jp.getCodec().readValue(jp, AssociationSetEnd.class));
            }
        }
    }

    return associationSet;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.TypeAnnotationDeserializer.java

@Override
public TypeAnnotation deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final TypeAnnotation typeAnnot = new TypeAnnotation();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Term".equals(jp.getCurrentName())) {
                typeAnnot.setTerm(jp.nextTextValue());
            } else if ("Qualifier".equals(jp.getCurrentName())) {
                typeAnnot.setQualifier(jp.nextTextValue());
            } else if ("Documentation".equals(jp.getCurrentName())) {
                jp.nextToken();//from  www .j a  v  a 2 s  .com
                typeAnnot.setDocumentation(jp.getCodec().readValue(jp, Documentation.class));
            } else if ("PropertyValue".equals(jp.getCurrentName())) {
                jp.nextToken();
                typeAnnot.getPropertyValues().add(jp.getCodec().readValue(jp, PropertyValue.class));
            }
        }
    }

    return typeAnnot;
}