Example usage for com.fasterxml.jackson.databind DeserializationContext mappingException

List of usage examples for com.fasterxml.jackson.databind DeserializationContext mappingException

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind DeserializationContext mappingException.

Prototype

public JsonMappingException mappingException(String paramString) 

Source Link

Usage

From source file:com.github.jonpeterson.jackson.module.versioning.VersionedModelDeserializer.java

@Override
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode jsonNode = parser.readValueAsTree();

    if (!(jsonNode instanceof ObjectNode))
        throw context.mappingException("value must be a JSON object");

    ObjectNode modelData = (ObjectNode) jsonNode;

    JsonNode modelVersionNode = modelData.remove(jsonVersionedModel.propertyName());

    String modelVersion = null;/*from   w ww.  j  a  va  2 s.c  o  m*/
    if (modelVersionNode != null)
        modelVersion = modelVersionNode.asText();

    if (modelVersion == null)
        modelVersion = jsonVersionedModel.defaultDeserializeToVersion();

    if (modelVersion.isEmpty())
        throw context.mappingException("'" + jsonVersionedModel.propertyName()
                + "' property was null and defaultDeserializeToVersion was not set");

    // convert the model if converter specified and model needs converting
    if (converter != null && (jsonVersionedModel.alwaysConvert()
            || !modelVersion.equals(jsonVersionedModel.currentVersion())))
        modelData = converter.convert(modelData, modelVersion, jsonVersionedModel.currentVersion(),
                context.getNodeFactory());

    // set the serializeToVersionProperty value to the source model version if the defaultToSource property is true
    if (serializeToVersionAnnotation != null && serializeToVersionAnnotation.defaultToSource())
        modelData.put(serializeToVersionProperty.getName(), modelVersion);

    JsonParser postInterceptionParser = new TreeTraversingParser(modelData, parser.getCodec());
    postInterceptionParser.nextToken();
    return delegate.deserialize(postInterceptionParser, context);
}

From source file:fr.javatic.mongo.jacksonCodec.javaTime.deserializers.InstantDeserializer.java

@Override
public Instant deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        ctxt.mappingException(Date.class);
    }//from   w w  w  .  j  ava  2s.c  o m

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Date dt = (Date) obj;
    return Instant.ofEpochMilli(dt.getTime());
}

From source file:fr.javatic.mongo.jacksonCodec.javaTime.deserializers.OffsetDateTimeDeserializer.java

@Override
public OffsetDateTime deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        throw ctxt.mappingException(Date.class);
    }/* w  w w .  ja v a  2s. c  o m*/

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Date dt = (Date) obj;
    return Instant.ofEpochMilli(dt.getTime()).atOffset(ZoneOffset.UTC);
}

From source file:fr.javatic.mongo.jacksonCodec.javaTime.deserializers.ZonedDateTimeDeserializer.java

@Override
public ZonedDateTime deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        throw ctxt.mappingException(Date.class);
    }//  ww  w. j a  v a 2s  .  c o  m

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Date dt = (Date) obj;
    return Instant.ofEpochMilli(dt.getTime()).atZone(ZoneId.of("UTC"));
}

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 + "\"");
        }//from w ww.  j  av  a 2s  .  c o m
        return (DateTime) dateTimeDeserializer.deserialize(jp, ctxt);
    }
    throw ctxt.mappingException(dateTimeDeserializer.handledType(), token);
}

From source file:fr.javatic.mongo.jacksonCodec.objectId.IdDeserializer.java

public String deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_OBJECTID) {
        throw ctxt.mappingException(ObjectId.class);
    }/*from w w  w  .  jav a  2 s .c o m*/

    ObjectId parsedObjectId = (ObjectId) bsonParser.getEmbeddedObject();
    int timestamp = parsedObjectId.getTime();
    int machineAndProcessIdentifier = parsedObjectId.getMachine();
    int counter = parsedObjectId.getInc();

    byte[] bytes = new byte[12];
    bytes[0] = int3(timestamp);
    bytes[1] = int2(timestamp);
    bytes[2] = int1(timestamp);
    bytes[3] = int0(timestamp);
    bytes[4] = int3(machineAndProcessIdentifier);
    bytes[5] = int2(machineAndProcessIdentifier);
    bytes[6] = int1(machineAndProcessIdentifier);
    bytes[7] = int0(machineAndProcessIdentifier);
    bytes[8] = int3(counter);
    bytes[9] = int2(counter);
    bytes[10] = int1(counter);
    bytes[11] = int0(counter);

    StringBuilder buf = new StringBuilder(24);

    for (final byte b : bytes) {
        buf.append(String.format("%02x", b & 0xff));
    }

    return buf.toString();
}

From source file:org.hyperledger.jackson.BIDDeserializer.java

@Override
public BID deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();/*from   ww  w . j a v  a 2 s .  co m*/
    if (t == JsonToken.VALUE_STRING) {
        String hashString = jp.getText().trim();
        if (hashString.length() == 0) {
            return null;
        }

        return new BID(hashString);
    }

    throw ctxt.mappingException(handledType());
}

From source file:org.hyperledger.jackson.TIDDeserializer.java

@Override
public TID deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();/*from   ww  w  . j  av a 2s. c o  m*/
    if (t == JsonToken.VALUE_STRING) {
        String hashString = jp.getText().trim();
        if (hashString.length() == 0) {
            return null;
        }

        return new TID(hashString);
    }

    throw ctxt.mappingException(handledType());
}

From source file:org.hyperledger.jackson.HashDeserializer.java

@Override
public Hash deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();// www  .j a  va 2s .  co  m
    if (t == JsonToken.VALUE_STRING) {
        String hashString = jp.getText().trim();
        if (hashString.length() == 0) {
            return null;
        }

        return new Hash(hashString);
    }

    throw ctxt.mappingException(handledType());
}

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 . j  a  v  a  2  s  .c o m
    } 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);
}