@Override public Any deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode root = p.getCodec().readTree(p); JsonNode type = root.get(JSON_TYPE); JsonNode value = root.get(JSON_VALUE); if (type == null || !type.isTextual()) { throw ctxt.reportMappingException("invalid '@type' field when deserializing ProtoBuf Any object"); } if (value == null || !value.isTextual()) { throw ctxt.reportMappingException("invalid '@value' field when deserializing ProtoBuf Any object"); } return Any.newBuilder() .setTypeUrl(type.textValue()) .setValue(ByteString.copyFrom(value.binaryValue())) .build(); } }
@Override public Requirement deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException { switch (p.getCurrentTokenId()) { case JsonTokenId.ID_STRING: final String str = p.getText().trim(); try { return buildRequirement(str); } catch (SemverException e) { ctxt.reportMappingException(e.getMessage()); } default: throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, null); } }
@Override public Semver deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException { switch (p.getCurrentTokenId()) { case JsonTokenId.ID_STRING: case JsonTokenId.ID_NUMBER_INT: final String str = p.getText().trim(); try { return buildSemver(str); } catch (SemverException e) { ctxt.reportMappingException(e.getMessage()); } default: throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "expected String or Number"); } }
protected KeyDeserializer _handleUnknownKeyDeserializer(DeserializationContext ctxt, JavaType type) throws JsonMappingException { ctxt.reportMappingException("Can not find a (Map) Key deserializer for type %s", type); return null; } }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws JsonMappingException{ ctxt.reportMappingException(_message); return null; } }
protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt, String type) throws IOException { ctxt.reportMappingException("Can not coerce a floating-point value ('%s') into %s; enable `DeserializationFeature.ACCEPT_FLOAT_AS_INT` to allow", p.getValueAsString(), type); } }
protected T _deserializeEmbedded(Object ob, DeserializationContext ctxt) throws IOException { // default impl: error out ctxt.reportMappingException("Don't know how to convert embedded Object of type %s into %s", ob.getClass().getName(), _valueClass.getName()); return null; }
protected JsonDeserializer<Object> _handleUnknownValueDeserializer(DeserializationContext ctxt, JavaType type) throws JsonMappingException { /* Let's try to figure out the reason, to give better error * messages */ Class<?> rawClass = type.getRawClass(); if (!ClassUtil.isConcrete(rawClass)) { ctxt.reportMappingException("Can not find a Value deserializer for abstract type %s", type); } ctxt.reportMappingException("Can not find a Value deserializer for type %s", type); return null; }
/** * Method called to locate deserializer for the passed root-level value. */ protected JsonDeserializer<Object> _findRootDeserializer(DeserializationContext ctxt) throws JsonMappingException { if (_rootDeserializer != null) { return _rootDeserializer; } // Sanity check: must have actual type... JavaType t = _valueType; if (t == null) { ctxt.reportMappingException("No value type configured for ObjectReader"); } // First: have we already seen it? JsonDeserializer<Object> deser = _rootDeserializers.get(t); if (deser != null) { return deser; } // Nope: need to ask provider to resolve it deser = ctxt.findRootValueDeserializer(t); if (deser == null) { // can this happen? ctxt.reportMappingException("Can not find a deserializer for type %s", t); } _rootDeserializers.put(t, deser); return deser; }
/** * @since 2.6 */ protected JsonDeserializer<Object> _findTreeDeserializer(DeserializationContext ctxt) throws JsonMappingException { JsonDeserializer<Object> deser = _rootDeserializers.get(JSON_NODE_TYPE); if (deser == null) { // Nope: need to ask provider to resolve it deser = ctxt.findRootValueDeserializer(JSON_NODE_TYPE); if (deser == null) { // can this happen? ctxt.reportMappingException("Can not find a deserializer for type %s", JSON_NODE_TYPE); } _rootDeserializers.put(JSON_NODE_TYPE, deser); } return deser; }
@Override public final T getNullValue(DeserializationContext ctxt) throws JsonMappingException { if (_primitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) { ctxt.reportMappingException( "Can not map JSON null into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)", handledType().toString()); } return _nullValue; }
@Override public T getEmptyValue(DeserializationContext ctxt) throws JsonMappingException { // [databind#1095]: Should not allow coercion from into null from Empty String // either, if `null` not allowed if (_primitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) { ctxt.reportMappingException( "Can not map Empty String as null into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)", handledType().toString()); } return _nullValue; } }
/** * Method called when there is a duplicate value for a field. * By default we don't care, and the last value is used. * Can be overridden to provide alternate handling, such as throwing * an exception, or choosing different strategy for combining values * or choosing which one to keep. * * @param fieldName Name of the field for which duplicate value was found * @param objectNode Object node that contains values * @param oldValue Value that existed for the object node before newValue * was added * @param newValue Newly added value just added to the object node */ protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt, JsonNodeFactory nodeFactory, String fieldName, ObjectNode objectNode, JsonNode oldValue, JsonNode newValue) throws JsonProcessingException { // [databind#237]: Report an error if asked to do so: if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)) { ctxt.reportMappingException("Duplicate field '%s' for ObjectNode: not allowed when FAIL_ON_READING_DUP_TREE_KEY enabled", fieldName); } }
/** * @since 2.4 */ @SuppressWarnings("unchecked") public <T> T readValue(JsonParser p, JavaType type) throws IOException { JsonDeserializer<Object> deser = findRootValueDeserializer(type); if (deser == null) { reportMappingException("Could not find JsonDeserializer for type %s", type); } return (T) deser.deserialize(p, this); }
@Override public Any deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode root = p.getCodec().readTree(p); JsonNode type = root.get(JSON_TYPE); JsonNode value = root.get(JSON_VALUE); if (type == null || !type.isTextual()) { throw ctxt.reportMappingException("invalid '@type' field when deserializing ProtoBuf Any object"); } if (value == null || !value.isTextual()) { throw ctxt.reportMappingException("invalid '@value' field when deserializing ProtoBuf Any object"); } return Any.newBuilder() .setTypeUrl(type.textValue()) .setValue(ByteString.copyFrom(value.binaryValue())) .build(); } }
@Override public void handleResolvedForwardReference(Object id, Object value) throws IOException { if (_bean == null) { _context.reportMappingException( "Can not resolve ObjectId forward reference using property '%s' (of type %s): Bean not yet resolved", _prop.getName(), _prop.getDeclaringClass().getName()); } _prop.set(_bean, value); } }
private ImmutableMap<String, String> parseIndex(JsonParser p, DeserializationContext ctxt) throws IOException { // Index can either be an array or object with id values. Here we transform // both to array. ImmutableMap<String, String> index = null; JsonToken token = p.currentToken(); if (token == JsonToken.START_ARRAY) index = parseIndexAsArray(p); else if (token == JsonToken.START_OBJECT) index = parseIndexAsMap(p); else ctxt.reportMappingException("could not deserialize category index, need either an array " + "or an object, got %s", token); return checkNotNull(index, "could not parse index"); }
/** * Helper method called when {@link JsonParser} indicates that it can use * so-called native type ids, and such type id has been found. * * @since 2.4 */ protected Object _deserializeWithNativeTypeId(JsonParser jp, DeserializationContext ctxt, Object typeId) throws IOException { JsonDeserializer<Object> deser; if (typeId == null) { /* 04-May-2014, tatu: Should error be obligatory, or should there be another method * for "try to deserialize with native tpye id"? */ deser = _findDefaultImplDeserializer(ctxt); if (deser == null) { ctxt.reportMappingException("No (native) type id found when one was expected for polymorphic type handling"); return null; } } else { String typeIdStr = (typeId instanceof String) ? (String) typeId : String.valueOf(typeId); deser = _findDeserializer(ctxt, typeIdStr); } return deser.deserialize(jp, ctxt); }
/** * @since 2.4 */ @SuppressWarnings("unchecked") public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException { JsonDeserializer<Object> deser = findContextualValueDeserializer(type, prop); if (deser == null) { String propName = (prop == null) ? "NULL" : ("'"+prop.getName()+"'"); reportMappingException( "Could not find JsonDeserializer for type %s (via property %s)", type, propName); } return (T) deser.deserialize(p, this); }
protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingException { // First: do we have injectable value? Object injectableValueId = prop.getInjectableValueId(); if (injectableValueId != null) { return _context.findInjectableValue(prop.getInjectableValueId(), prop, null); } // Second: required? if (prop.isRequired()) { _context.reportMappingException("Missing required creator property '%s' (index %d)", prop.getName(), prop.getCreatorIndex()); } if (_context.isEnabled(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES)) { _context.reportMappingException("Missing creator property '%s' (index %d); DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES enabled", prop.getName(), prop.getCreatorIndex()); } // Third: default value JsonDeserializer<Object> deser = prop.getValueDeserializer(); return deser.getNullValue(_context); }