Example usage for com.fasterxml.jackson.databind JsonMappingException prependPath

List of usage examples for com.fasterxml.jackson.databind JsonMappingException prependPath

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonMappingException prependPath.

Prototype

public void prependPath(Object paramObject, String paramString) 

Source Link

Usage

From source file:com.addthis.codec.jackson.CodecTypeDeserializer.java

@Nullable
private Object _deserializeObjectFromInlinedType(ObjectNode objectNode, ObjectCodec objectCodec,
        DeserializationContext ctxt) throws IOException {
    String matched = null;//w w  w  .j  a  v a  2  s  . c o m
    for (String alias : pluginMap.inlinedAliases()) {
        if (objectNode.get(alias) != null) {
            if (matched != null) {
                String message = String.format(
                        "no type specified, more than one key, and both %s and %s match for inlined types.",
                        matched, alias);
                JsonMappingException exception = ctxt.instantiationException(_baseType.getRawClass(), message);
                exception.prependPath(_baseType, matched);
                throw exception;
            }
            matched = alias;
        }
    }
    if (matched != null) {
        ConfigObject aliasDefaults = pluginMap.aliasDefaults(matched);
        JsonNode configValue = objectNode.get(matched);
        String primaryField = (String) aliasDefaults.get("_primary").unwrapped();
        objectNode.remove(matched);
        Jackson.setAt(objectNode, configValue, primaryField);
        Jackson.merge(objectNode, Jackson.configConverter(aliasDefaults));
        if (_typeIdVisible) {
            objectNode.put(_typePropertyName, matched);
        }
        try {
            JsonDeserializer<Object> deser = _findDeserializer(ctxt, matched);
            JsonParser treeParser = objectCodec.treeAsTokens(objectNode);
            treeParser.nextToken();
            return deser.deserialize(treeParser, ctxt);
        } catch (IOException cause) {
            IOException unwrapped = Jackson.maybeUnwrapPath(primaryField, cause);
            if (unwrapped != cause) {
                throw wrapWithPath(unwrapped, idRes.typeFromId(ctxt, matched), matched);
            } else {
                throw unwrapped;
            }
        }
    } else {
        return null;
    }
}

From source file:com.addthis.codec.jackson.CodecTypeDeserializer.java

private boolean handleDefaultsAndImplicitPrimary(ObjectNode fieldValues, ConfigObject aliasDefaults,
        JsonDeserializer<?> deserializer, DeserializationContext ctxt) throws JsonMappingException {
    if (!aliasDefaults.isEmpty()) {
        if (deserializer instanceof DelegatingDeserializer) {
            deserializer = ((DelegatingDeserializer) deserializer).getDelegatee();
        }/*from  w  ww .  ja v a  2s . com*/
        if ((deserializer instanceof BeanDeserializerBase) && (aliasDefaults.get("_primary") != null)) {
            BeanDeserializerBase beanDeserializer = (BeanDeserializerBase) deserializer;
            String primaryField = (String) aliasDefaults.get("_primary").unwrapped();
            if (!fieldValues.has(primaryField)) {
                // user has not explicitly set a value where _primary points, see if _primary is a plugin type
                SettableBeanProperty primaryProperty = beanDeserializer.findProperty(primaryField);
                if ((primaryProperty != null) && primaryProperty.hasValueTypeDeserializer()) {
                    TypeDeserializer primaryTypeDeserializer = primaryProperty.getValueTypeDeserializer();
                    if (primaryTypeDeserializer instanceof CodecTypeDeserializer) {
                        CodecTypeIdResolver primaryPropertyTypeIdResolver = ((CodecTypeDeserializer) primaryTypeDeserializer).idRes;
                        String possibleInlinedPrimary = null;
                        Iterator<String> fieldNames = fieldValues.fieldNames();
                        while (fieldNames.hasNext()) {
                            String fieldName = fieldNames.next();
                            if ((fieldName.charAt(0) != '_') && !beanDeserializer.hasProperty(fieldName)) {
                                if (primaryPropertyTypeIdResolver.isValidTypeId(fieldName)) {
                                    if (possibleInlinedPrimary == null) {
                                        possibleInlinedPrimary = fieldName;
                                    } else {
                                        String message = String.format(
                                                "%s and %s are both otherwise unknown properties that "
                                                        + "could be types for the _primary property %s whose category is "
                                                        + "%s. This is too ambiguous to resolve.",
                                                possibleInlinedPrimary, fieldName, primaryField,
                                                ((CodecTypeDeserializer) primaryTypeDeserializer).pluginMap
                                                        .category());
                                        JsonMappingException ex = ctxt
                                                .instantiationException(_baseType.getRawClass(), message);
                                        ex.prependPath(beanDeserializer.getValueType(), fieldName);
                                        throw ex;
                                    }
                                }
                            }
                        }
                        // did we find a good candidate?
                        if (possibleInlinedPrimary != null) {
                            // then wrap the value with its key (its type), and stash it in our primary field
                            JsonNode inlinedPrimaryValue = fieldValues.remove(possibleInlinedPrimary);
                            fieldValues.with(primaryField).set(possibleInlinedPrimary, inlinedPrimaryValue);
                            Jackson.merge(fieldValues, Jackson.configConverter(aliasDefaults));
                            return true;
                        }
                    }
                }
            }
        }
        // merge alias defaults here since we check for empty etc anyway
        Jackson.merge(fieldValues, Jackson.configConverter(aliasDefaults));
    }
    return false;
}