1 package com.fasterxml.jackson.databind.exc; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.core.JsonLocation; 6 import com.fasterxml.jackson.core.JsonParser; 7 import com.fasterxml.jackson.databind.JsonMappingException; 8 9 /** 10 * Specialized {@link JsonMappingException} sub-class specifically used 11 * to indicate problems due to encountering a JSON property that could 12 * not be mapped to an Object property (via getter, constructor argument 13 * or field). 14 */ 15 public class UnrecognizedPropertyException 16 extends PropertyBindingException 17 { 18 private static final long serialVersionUID = 1L; 19 UnrecognizedPropertyException(JsonParser p, String msg, JsonLocation loc, Class<?> referringClass, String propName, Collection<Object> propertyIds)20 public UnrecognizedPropertyException(JsonParser p, String msg, JsonLocation loc, 21 Class<?> referringClass, String propName, 22 Collection<Object> propertyIds) 23 { 24 super(p, msg, loc, referringClass, propName, propertyIds); 25 } 26 27 /** 28 * @deprecated Since 2.7 29 */ 30 @Deprecated // since 2.7 UnrecognizedPropertyException(String msg, JsonLocation loc, Class<?> referringClass, String propName, Collection<Object> propertyIds)31 public UnrecognizedPropertyException(String msg, JsonLocation loc, 32 Class<?> referringClass, String propName, 33 Collection<Object> propertyIds) 34 { 35 super(msg, loc, referringClass, propName, propertyIds); 36 } 37 38 /** 39 * Factory method used for constructing instances of this exception type. 40 * 41 * @param p Underlying parser used for reading input being used for data-binding 42 * @param fromObjectOrClass Reference to either instance of problematic type ( 43 * if available), or if not, type itself 44 * @param propertyName Name of unrecognized property 45 * @param propertyIds (optional, null if not available) Set of properties that 46 * type would recognize, if completely known: null if set cannot be determined. 47 */ from(JsonParser p, Object fromObjectOrClass, String propertyName, Collection<Object> propertyIds)48 public static UnrecognizedPropertyException from(JsonParser p, 49 Object fromObjectOrClass, String propertyName, 50 Collection<Object> propertyIds) 51 { 52 Class<?> ref; 53 if (fromObjectOrClass instanceof Class<?>) { 54 ref = (Class<?>) fromObjectOrClass; 55 } else { 56 ref = fromObjectOrClass.getClass(); 57 } 58 String msg = String.format("Unrecognized field \"%s\" (class %s), not marked as ignorable", 59 propertyName, ref.getName()); 60 UnrecognizedPropertyException e = new UnrecognizedPropertyException(p, msg, 61 p.getCurrentLocation(), ref, propertyName, propertyIds); 62 // but let's also ensure path includes this last (missing) segment 63 e.prependPath(fromObjectOrClass, propertyName); 64 return e; 65 } 66 } 67