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

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

Introduction

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

Prototype

public JsonMappingException weirdStringException(String paramString1, Class<?> paramClass,
            String paramString2) 

Source Link

Usage

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  w w.  ja  v 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: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 .ja  v a2 s  .c  o m*/

        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:com.basistech.rosette.dm.jackson.VersionCheckDeserializer.java

@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "The value of 'version' must be a string");
    }//ww  w .  ja  v a 2 s  .c om
    String version = p.readValueAs(String.class);
    String[] bits = version.split("\\.");
    if (bits.length < 3) { // allow for a fourth digit for some reason some day.
        throw ctxt.weirdStringException(version, String.class, "Versions must be of the form x.y.z");
    }
    if (!"1".equals(bits[0])) {
        throw ctxt.weirdStringException(version, String.class,
                String.format("Incompatible ADM version %s", version));
    }
    return version;
}

From source file:com.tikinou.schedulesdirect.core.jackson.deser.BooleanYNDeserializer.java

@Override
public Boolean deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();//from ww w . j av a2  s . c om
    if (t == JsonToken.VALUE_TRUE) {
        return Boolean.TRUE;
    }
    if (t == JsonToken.VALUE_FALSE) {
        return Boolean.FALSE;
    }
    if (t == JsonToken.VALUE_NULL) {
        return null;
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return (jp.getIntValue() != 0);
    }
    if (t == JsonToken.VALUE_STRING) {
        String text = jp.getText().trim();
        if ("true".equals(text)) {
            return Boolean.TRUE;
        }
        if ("false".equals(text) || text.length() == 0) {
            return Boolean.FALSE;
        }

        if ("N".equalsIgnoreCase(text) || text.length() == 0) {
            return Boolean.FALSE;
        }

        if ("Y".equalsIgnoreCase(text)) {
            return Boolean.TRUE;
        }
        throw ctxt.weirdStringException(text, Boolean.class, "only \"true\" or \"false\" recognized");
    }
    // Otherwise, no can do:
    throw ctxt.mappingException(Boolean.class);
}