1 package com.fasterxml.jackson.databind.deser.impl; 2 3 import java.io.IOException; 4 5 import com.fasterxml.jackson.annotation.ObjectIdGenerator; 6 import com.fasterxml.jackson.annotation.ObjectIdResolver; 7 8 import com.fasterxml.jackson.core.JsonParser; 9 10 import com.fasterxml.jackson.databind.*; 11 import com.fasterxml.jackson.databind.deser.SettableBeanProperty; 12 13 /** 14 * Object that knows how to deserialize Object Ids. 15 */ 16 public class ObjectIdReader 17 implements java.io.Serializable 18 { 19 private static final long serialVersionUID = 1L; 20 21 protected final JavaType _idType; 22 23 public final PropertyName propertyName; 24 25 /** 26 * Blueprint generator instance: actual instance will be 27 * fetched from {@link SerializerProvider} using this as 28 * the key. 29 */ 30 public final ObjectIdGenerator<?> generator; 31 32 public final ObjectIdResolver resolver; 33 34 /** 35 * Deserializer used for deserializing id values. 36 */ 37 protected final JsonDeserializer<Object> _deserializer; 38 39 public final SettableBeanProperty idProperty; 40 41 /* 42 /********************************************************** 43 /* Life-cycle 44 /********************************************************** 45 */ 46 47 @SuppressWarnings("unchecked") ObjectIdReader(JavaType t, PropertyName propName, ObjectIdGenerator<?> gen, JsonDeserializer<?> deser, SettableBeanProperty idProp, ObjectIdResolver resolver)48 protected ObjectIdReader(JavaType t, PropertyName propName, ObjectIdGenerator<?> gen, 49 JsonDeserializer<?> deser, SettableBeanProperty idProp, ObjectIdResolver resolver) 50 { 51 _idType = t; 52 propertyName = propName; 53 generator = gen; 54 this.resolver = resolver; 55 _deserializer = (JsonDeserializer<Object>) deser; 56 idProperty = idProp; 57 } 58 59 /** 60 * Factory method called by {@link com.fasterxml.jackson.databind.ser.std.BeanSerializerBase} 61 * with the initial information based on standard settings for the type 62 * for which serializer is being built. 63 */ construct(JavaType idType, PropertyName propName, ObjectIdGenerator<?> generator, JsonDeserializer<?> deser, SettableBeanProperty idProp, ObjectIdResolver resolver)64 public static ObjectIdReader construct(JavaType idType, PropertyName propName, 65 ObjectIdGenerator<?> generator, JsonDeserializer<?> deser, 66 SettableBeanProperty idProp, ObjectIdResolver resolver) 67 { 68 return new ObjectIdReader(idType, propName, generator, deser, idProp, resolver); 69 } 70 71 /* 72 /********************************************************** 73 /* API 74 /********************************************************** 75 */ 76 getDeserializer()77 public JsonDeserializer<Object> getDeserializer() { 78 return _deserializer; 79 } 80 getIdType()81 public JavaType getIdType() { 82 return _idType; 83 } 84 85 /** 86 * Convenience method, equivalent to calling: 87 *<code> 88 * readerInstance.generator.maySerializeAsObject(); 89 *</code> 90 * and used to determine whether Object Ids handled by the underlying 91 * generator may be in form of (JSON) Objects. 92 * Used for optimizing handling in cases where method returns false. 93 * 94 * @since 2.5 95 */ maySerializeAsObject()96 public boolean maySerializeAsObject() { 97 return generator.maySerializeAsObject(); 98 } 99 100 /** 101 * Convenience method, equivalent to calling: 102 *<code> 103 * readerInstance.generator.isValidReferencePropertyName(name, parser); 104 *</code> 105 * and used to determine whether Object Ids handled by the underlying 106 * generator may be in form of (JSON) Objects. 107 * Used for optimizing handling in cases where method returns false. 108 * 109 * @since 2.5 110 */ isValidReferencePropertyName(String name, JsonParser parser)111 public boolean isValidReferencePropertyName(String name, JsonParser parser) { 112 return generator.isValidReferencePropertyName(name, parser); 113 } 114 115 /** 116 * Method called to read value that is expected to be an Object Reference 117 * (that is, value of an Object Id used to refer to another object). 118 * 119 * @since 2.3 120 */ readObjectReference(JsonParser jp, DeserializationContext ctxt)121 public Object readObjectReference(JsonParser jp, DeserializationContext ctxt) throws IOException { 122 return _deserializer.deserialize(jp, ctxt); 123 } 124 } 125