List of usage examples for com.fasterxml.jackson.databind DeserializationContext instantiationException
public JsonMappingException instantiationException(Class<?> paramClass, Throwable paramThrowable)
From source file:com.addthis.codec.jackson.CodecTypeDeserializer.java
@Nullable private Object _deserializeObjectFromInlinedType(ObjectNode objectNode, ObjectCodec objectCodec, DeserializationContext ctxt) throws IOException { String matched = null;/*from ww 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:org.mongojack.internal.DBRefDeserializer.java
@Override public DBRef deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // First of all, make sure that we can get a copy of the DBCollection if (jp instanceof JacksonDBCollectionProvider) { K id = null;//from www . j a va 2s. c o m String collectionName = null; JsonToken token = jp.getCurrentToken(); if (token == JsonToken.VALUE_NULL) { return null; } if (token == JsonToken.VALUE_EMBEDDED_OBJECT) { // Someones already kindly decoded it for us Object object = jp.getEmbeddedObject(); if (object instanceof com.mongodb.DBRef) { if (keyDeserializer != null) { id = keyDeserializer.deserialize(jp, ctxt); } else { id = (K) ((com.mongodb.DBRef) object).getId(); } collectionName = ((com.mongodb.DBRef) object).getRef(); } else { throw ctxt.instantiationException(DBRef.class, "Don't know what to do with embedded object: " + object); } } else if (token == JsonToken.START_OBJECT) { token = jp.nextValue(); while (token != JsonToken.END_OBJECT) { if (jp.getCurrentName().equals("$id")) { if (keyDeserializer != null) { id = keyDeserializer.deserialize(jp, ctxt); } else { id = (K) jp.getEmbeddedObject(); } } else if (jp.getCurrentName().equals("$ref")) { collectionName = jp.getText(); } else { // Ignore the rest } token = jp.nextValue(); } } if (id == null) { return null; } if (collectionName == null) { throw ctxt.instantiationException(DBRef.class, "DBRef contains no collection name"); } JacksonDBCollection coll = ((JacksonDBCollectionProvider) jp).getDBCollection(); JacksonDBCollection<T, K> refColl = coll.getReferenceCollection(collectionName, type, keyType); return new FetchableDBRef<T, K>(id, refColl); } else { throw ctxt.instantiationException(DBRef.class, "DBRef can only be deserialised by this deserializer if parser implements " + JacksonDBCollectionProvider.class.getName() + " parser is actually " + jp.getClass().getName()); } }
From source file:org.apache.ode.jacob.soup.jackson.ChannelProxyDeserializer.java
@Override public Channel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String type = null;//from w ww . j a va 2 s .c o m int id = -1; while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldname = jp.getCurrentName(); if (jp.getCurrentToken() == JsonToken.FIELD_NAME) { // if we're not already on the field, advance by one. jp.nextToken(); } if ("channelType".equals(fieldname)) { type = jp.getText(); } else if ("channelId".equals(fieldname)) { id = jp.getIntValue(); } } if (type == null) { throw ctxt.mappingException(Channel.class); } if (id < 0) { throw ctxt.mappingException(Channel.class); } try { CommChannel cchannel = new CommChannel(ctxt.findClass(type)); cchannel.setId(id); return (Channel) ChannelFactory.createChannel(cchannel, cchannel.getType()); } catch (ClassNotFoundException e) { throw ctxt.instantiationException(Channel.class, e); } }
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 w w. jav a 2s.c om 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; }