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

@Override
public Calendar deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken token = jp.getCurrentToken();
    Date date;/*from  w ww  .  j a va  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:javaslang.jackson.datatype.deserialize.TupleDeserializer.java

@Override
public Tuple deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    List<Object> list = new ArrayList<>();
    int ptr = 0;//from w w  w  .  j a v  a2s .  com

    for (JsonToken jsonToken = p.nextToken(); jsonToken != END_ARRAY; jsonToken = p.nextToken()) {
        if (ptr >= deserializersCount()) {
            throw ctxt.mappingException(javaType.getRawClass());
        }
        JsonDeserializer<?> deserializer = deserializer(ptr++);
        Object value = (jsonToken != VALUE_NULL) ? deserializer.deserialize(p, ctxt)
                : deserializer.getNullValue(ctxt);
        list.add(value);
    }
    return create(list, ctxt);
}

From source file:io.gravitee.definition.jackson.datatype.services.healthcheck.deser.HealthCheckDeserializer.java

@Override
public HealthCheck deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);

    HealthCheck healthCheck = new HealthCheck();

    final JsonNode intervalNode = node.get("interval");
    if (intervalNode != null) {
        healthCheck.setInterval(intervalNode.asLong());
    } else {//from www.  j av a  2 s  .  com
        throw ctxt.mappingException("[healthcheck] Interval is required");
    }

    final JsonNode unitNode = node.get("unit");
    if (unitNode != null) {
        healthCheck.setUnit(TimeUnit.valueOf(unitNode.asText().toUpperCase()));
    } else {
        throw ctxt.mappingException("[healthcheck] Unit is required");
    }

    final JsonNode requestNode = node.get("request");
    if (requestNode != null) {
        healthCheck.setRequest(requestNode.traverse(jp.getCodec()).readValueAs(Request.class));
    } else {
        throw ctxt.mappingException("[healthcheck] Request is required");
    }

    final JsonNode expectationNode = node.get("expectation");
    if (expectationNode != null) {
        healthCheck.setExpectation(expectationNode.traverse(jp.getCodec()).readValueAs(Expectation.class));
    } else {
        Expectation expectation = new Expectation();
        expectation.setAssertions(Collections.singletonList(Expectation.DEFAULT_ASSERTION));
        healthCheck.setExpectation(expectation);
    }

    final JsonNode healthCheckEnabledNode = node.get("enabled");
    if (healthCheckEnabledNode != null) {
        healthCheck.setEnabled(healthCheckEnabledNode.asBoolean(true));
    } else {
        healthCheck.setEnabled(true);
    }

    return healthCheck;
}

From source file:com.kaaprotech.satu.jackson.SatuDeserializers.java

@Override
public JsonDeserializer<?> findMapDeserializer(final MapType type, final DeserializationConfig config,
        BeanDescription beanDesc, final KeyDeserializer keyDeserializer,
        final TypeDeserializer elementTypeDeserializer, final JsonDeserializer<?> elementDeserializer)
        throws JsonMappingException {

    if (ImmutableMap.class.isAssignableFrom(type.getRawClass())) {
        return new StdDeserializer<Object>(type) {
            private static final long serialVersionUID = 1L;

            @Override/* w w w .j a  va2  s  . c om*/
            public Object deserialize(JsonParser jp, DeserializationContext context) throws IOException {

                JsonToken t = jp.getCurrentToken();
                if (t == JsonToken.START_OBJECT) {
                    t = jp.nextToken();
                }
                if (t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
                    throw context.mappingException(type.getRawClass());
                }

                MutableMap<Object, Object> m = Maps.mutable.of();

                for (; jp.getCurrentToken() == JsonToken.FIELD_NAME; jp.nextToken()) {
                    // Pointing to field name
                    String fieldName = jp.getCurrentName();
                    Object key = (keyDeserializer == null) ? fieldName
                            : keyDeserializer.deserializeKey(fieldName, context);
                    t = jp.nextToken();

                    Object value;
                    if (t == JsonToken.VALUE_NULL) {
                        value = null;
                    } else if (elementDeserializer == null) {
                        value = jp.readValueAs(type.getContentType().getRawClass());
                    } else if (elementTypeDeserializer == null) {
                        value = elementDeserializer.deserialize(jp, context);
                    } else {
                        value = elementDeserializer.deserializeWithType(jp, context, elementTypeDeserializer);
                    }
                    m.put(key, value);
                }
                return m.toImmutable();
            }
        };
    }

    return super.findMapDeserializer(type, config, beanDesc, keyDeserializer, elementTypeDeserializer,
            elementDeserializer);
}

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()));
    }/*w  w w .  ja v a 2  s.c  o  m*/
    case VALUE_NUMBER_INT: {
        return new Quorum(jp.getIntValue());
    }
    case VALUE_NULL: {
        return null;
    }
    default:
        break;
    }
    throw ctxt.mappingException(Quorum.class);
}

From source file:io.gravitee.definition.jackson.datatype.api.deser.ProxyDeserializer.java

@Override
public Proxy deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);

    Proxy proxy = new Proxy();
    final JsonNode contextPath = node.get("context_path");
    if (contextPath != null) {
        String sContextPath = formatContextPath(contextPath.asText());
        proxy.setContextPath(sContextPath);
    } else {/*from   w w  w .  j  av  a 2  s  . c  o m*/
        throw ctxt.mappingException("[api] API must have a valid context path");
    }

    final JsonNode nodeEndpoints = node.get("endpoints");

    if (nodeEndpoints != null && nodeEndpoints.isArray()) {
        nodeEndpoints.elements().forEachRemaining(jsonNode -> {
            try {
                Endpoint endpoint = jsonNode.traverse(jp.getCodec()).readValueAs(Endpoint.class);
                proxy.getEndpoints().add(endpoint);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    JsonNode stripContextNode = node.get("strip_context_path");
    if (stripContextNode != null) {
        proxy.setStripContextPath(stripContextNode.asBoolean(false));
    }

    JsonNode loadBalancingNode = node.get("load_balancing");
    if (loadBalancingNode != null) {
        LoadBalancer loadBalancer = loadBalancingNode.traverse(jp.getCodec()).readValueAs(LoadBalancer.class);
        proxy.setLoadBalancer(loadBalancer);
    }

    JsonNode failoverNode = node.get("failover");
    if (failoverNode != null) {
        Failover failover = failoverNode.traverse(jp.getCodec()).readValueAs(Failover.class);
        proxy.setFailover(failover);
    }

    JsonNode dumpRequestNode = node.get("dumpRequest");
    if (dumpRequestNode != null) {
        boolean dumpRequest = dumpRequestNode.asBoolean(Proxy.DEFAULT_DUMP_REQUEST);
        proxy.setDumpRequest(dumpRequest);
    } else {
        proxy.setDumpRequest(Proxy.DEFAULT_DUMP_REQUEST);
    }

    return proxy;
}

From source file:io.gravitee.definition.jackson.datatype.api.deser.EndpointDeserializer.java

@Override
public Endpoint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);

    Endpoint endpoint = new Endpoint();
    endpoint.setTarget(node.get("target").asText());

    JsonNode nameNode = node.get("name");
    if (nameNode != null) {
        String name = nameNode.asText();
        endpoint.setName(name);//from   w ww .  ja va  2s.com
    } else {
        throw ctxt.mappingException("Endpoint name is required");
    }

    JsonNode weightNode = node.get("weight");
    if (weightNode != null) {
        int weight = weightNode.asInt(Endpoint.DEFAULT_WEIGHT);
        endpoint.setWeight(weight);
    } else {
        endpoint.setWeight(Endpoint.DEFAULT_WEIGHT);
    }

    JsonNode backupNode = node.get("backup");
    if (backupNode != null) {
        boolean backup = backupNode.asBoolean(false);
        endpoint.setBackup(backup);
    } else {
        endpoint.setBackup(false);
    }

    JsonNode healthcheckNode = node.get("healthcheck");
    if (healthcheckNode != null) {
        boolean healthcheck = healthcheckNode.asBoolean(true);
        endpoint.setHealthcheck(healthcheck);
    } else {
        endpoint.setHealthcheck(true);
    }

    JsonNode httpProxyNode = node.get("proxy");
    if (httpProxyNode != null) {
        HttpProxy httpProxy = httpProxyNode.traverse(jp.getCodec()).readValueAs(HttpProxy.class);
        endpoint.setHttpProxy(httpProxy);
    }

    JsonNode httpClientOptionsNode = node.get("http");
    if (httpClientOptionsNode != null) {
        HttpClientOptions httpClientOptions = httpClientOptionsNode.traverse(jp.getCodec())
                .readValueAs(HttpClientOptions.class);
        endpoint.setHttpClientOptions(httpClientOptions);
    } else {
        endpoint.setHttpClientOptions(new HttpClientOptions());
    }

    JsonNode httpClientSslOptionsNode = node.get("ssl");
    if (httpClientSslOptionsNode != null) {
        HttpClientSslOptions httpClientSslOptions = httpClientSslOptionsNode.traverse(jp.getCodec())
                .readValueAs(HttpClientSslOptions.class);
        endpoint.setHttpClientSslOptions(httpClientSslOptions);
    }

    return endpoint;
}

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

@Override
public MasterPublicKey deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();/*from   www  . j a v a 2  s. com*/
    if (t == JsonToken.VALUE_STRING) {
        try {
            String keyString = jp.getText().trim();
            if (keyString.length() == 0) {
                return null;
            }

            return MasterPublicKey.parse(keyString);
        } catch (HyperLedgerException e) {
            throw JsonMappingException.from(jp, "Error deserializing extended key", e);
        }
    }

    throw ctxt.mappingException(handledType());
}

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

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

            return MasterPrivateKey.parse(keyString);
        } catch (HyperLedgerException e) {
            throw JsonMappingException.from(jp, "Error deserializing extended key", e);
        }
    }

    throw ctxt.mappingException(handledType());
}

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

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

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

    throw ctxt.mappingException(handledType());
}