1 package com.fasterxml.jackson.databind.exc; 2 3 import com.fasterxml.jackson.core.JsonLocation; 4 import com.fasterxml.jackson.core.JsonParser; 5 import com.fasterxml.jackson.databind.JavaType; 6 import com.fasterxml.jackson.databind.JsonMappingException; 7 import com.fasterxml.jackson.databind.util.ClassUtil; 8 9 /** 10 * General exception type used as the base class for all {@link JsonMappingException}s 11 * that are due to input not mapping to target definition; these are typically 12 * considered "client errors" since target type definition itself is not the root cause 13 * but mismatching input. This is in contrast to {@link InvalidDefinitionException} which 14 * signals a problem with target type definition and not input. 15 *<p> 16 * This type is used as-is for some input problems, but in most cases there should be 17 * more explicit subtypes to use. 18 *<p> 19 * NOTE: name chosen to differ from `java.util.InputMismatchException` since while that 20 * would have been better name, use of same overlapping name causes nasty issues 21 * with IDE auto-completion, so slightly less optimal chosen. 22 * 23 * @since 2.9 24 */ 25 @SuppressWarnings("serial") 26 public class MismatchedInputException 27 extends JsonMappingException 28 { 29 /** 30 * Type of value that was to be deserialized 31 */ 32 protected Class<?> _targetType; 33 MismatchedInputException(JsonParser p, String msg)34 protected MismatchedInputException(JsonParser p, String msg) { 35 this(p, msg, (JavaType) null); 36 } 37 MismatchedInputException(JsonParser p, String msg, JsonLocation loc)38 protected MismatchedInputException(JsonParser p, String msg, JsonLocation loc) { 39 super(p, msg, loc); 40 } 41 MismatchedInputException(JsonParser p, String msg, Class<?> targetType)42 protected MismatchedInputException(JsonParser p, String msg, Class<?> targetType) { 43 super(p, msg); 44 _targetType = targetType; 45 } 46 MismatchedInputException(JsonParser p, String msg, JavaType targetType)47 protected MismatchedInputException(JsonParser p, String msg, JavaType targetType) { 48 super(p, msg); 49 _targetType = ClassUtil.rawClass(targetType); 50 } 51 52 // Only to prevent super-class static method from getting called 53 @Deprecated // as of 2.9 from(JsonParser p, String msg)54 public static MismatchedInputException from(JsonParser p, String msg) { 55 return from(p, (Class<?>) null, msg); 56 } 57 from(JsonParser p, JavaType targetType, String msg)58 public static MismatchedInputException from(JsonParser p, JavaType targetType, String msg) { 59 return new MismatchedInputException(p, msg, targetType); 60 } 61 from(JsonParser p, Class<?> targetType, String msg)62 public static MismatchedInputException from(JsonParser p, Class<?> targetType, String msg) { 63 return new MismatchedInputException(p, msg, targetType); 64 } 65 setTargetType(JavaType t)66 public MismatchedInputException setTargetType(JavaType t) { 67 _targetType = t.getRawClass(); 68 return this; 69 } 70 71 /** 72 * Accessor for getting intended target type, with which input did not match, 73 * if known; `null` if not known for some reason. 74 */ getTargetType()75 public Class<?> getTargetType() { 76 return _targetType; 77 } 78 } 79