/** * Method called to get hold of a serializer for a value of given type; or if no such serializer * can be found, a default handler (which may do a best-effort generic serialization or just * simply throw an exception when invoked). * * <p>Note: this method is only called for non-null values; not for keys or null values. For * these, check out other accessor methods. * * <p>Note that serializers produced should NOT handle polymorphic serialization aspects; separate * {@link TypeSerializer} is to be constructed by caller if and as necessary. * * @throws JsonMappingException if there are fatal problems with accessing suitable serializer; * including that of not finding any serializer */ @SuppressWarnings("unchecked") public JsonSerializer<Object> findValueSerializer(Class<?> valueType, BeanProperty property) throws JsonMappingException { // Fast lookup from local lookup thingy works? JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType); if (ser == null) { // If not, maybe shared map already has it? ser = _serializerCache.untypedValueSerializer(valueType); if (ser == null) { // ... possibly as fully typed? ser = _serializerCache.untypedValueSerializer(_config.constructType(valueType)); if (ser == null) { // If neither, must create ser = _createAndCacheUntypedSerializer(valueType); // Not found? Must use the unknown type serializer, which will report error later on if (ser == null) { ser = getUnknownTypeSerializer(valueType); // Should this be added to lookups? if (CACHE_UNKNOWN_MAPPINGS) { _serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this); } return ser; } } } } // at this point, resolution has occured, but not contextualization return (JsonSerializer<Object>) handleSecondaryContextualization(ser, property); }
protected JsonSerializer<Object> _createAndCacheUntypedSerializer(JavaType type) throws JsonMappingException { JsonSerializer<Object> ser; try { ser = _createUntypedSerializer(type); } catch (IllegalArgumentException iae) { /* We better only expose checked exceptions, since those * are what caller is expected to handle */ throw JsonMappingException.from(this, iae.getMessage(), iae); } if (ser != null) { _serializerCache.addAndResolveNonTypedSerializer(type, ser, this); } return ser; }
/** * Method variant used when we do NOT want contextualization to happen; it will need to be handled * at a later point, but caller wants to be able to do that as needed; sometimes to avoid infinite * loops * * @since 2.5 */ public JsonSerializer<Object> findValueSerializer(JavaType valueType) throws JsonMappingException { // (see comments from above method) JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType); if (ser == null) { ser = _serializerCache.untypedValueSerializer(valueType); if (ser == null) { ser = _createAndCacheUntypedSerializer(valueType); if (ser == null) { ser = getUnknownTypeSerializer(valueType.getRawClass()); if (CACHE_UNKNOWN_MAPPINGS) { _serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this); } } } } return ser; }
/** * Method that will try to construct a value serializer; and if one is successfully created, cache * it for reuse. */ protected JsonSerializer<Object> _createAndCacheUntypedSerializer(Class<?> rawType) throws JsonMappingException { JavaType fullType = _config.constructType(rawType); JsonSerializer<Object> ser; try { ser = _createUntypedSerializer(fullType); } catch (IllegalArgumentException iae) { /* We better only expose checked exceptions, since those * are what caller is expected to handle */ throw JsonMappingException.from(this, iae.getMessage(), iae); } if (ser != null) { // 21-Dec-2015, tatu: Best to cache for both raw and full-type key _serializerCache.addAndResolveNonTypedSerializer(rawType, fullType, ser, this); } return ser; }
/** * Similar to {@link #findValueSerializer(JavaType, BeanProperty)}, but used when finding * "primary" property value serializer (one directly handling value of the property). Difference * has to do with contextual resolution, and method(s) called: this method should only be called * when caller is certain that this is the primary property value serializer. * * @param property Property that is being handled; will never be null, and its type has to match * <code>valueType</code> parameter. * @since 2.3 */ @SuppressWarnings("unchecked") public JsonSerializer<Object> findPrimaryPropertySerializer( JavaType valueType, BeanProperty property) throws JsonMappingException { JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType); if (ser == null) { ser = _serializerCache.untypedValueSerializer(valueType); if (ser == null) { ser = _createAndCacheUntypedSerializer(valueType); if (ser == null) { ser = getUnknownTypeSerializer(valueType.getRawClass()); // Should this be added to lookups? if (CACHE_UNKNOWN_MAPPINGS) { _serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this); } return ser; } } } return (JsonSerializer<Object>) handlePrimaryContextualization(ser, property); }
/** * Similar to {@link #findValueSerializer(Class,BeanProperty)}, but takes full generics-aware type * instead of raw class. This is necessary for accurate handling of external type information, to * handle polymorphic types. * * @param property When creating secondary serializers, property for which serializer is needed: * annotations of the property (or bean that contains it) may be checked to create contextual * serializers. */ @SuppressWarnings("unchecked") public JsonSerializer<Object> findValueSerializer(JavaType valueType, BeanProperty property) throws JsonMappingException { // (see comments from above method) JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType); if (ser == null) { ser = _serializerCache.untypedValueSerializer(valueType); if (ser == null) { ser = _createAndCacheUntypedSerializer(valueType); if (ser == null) { ser = getUnknownTypeSerializer(valueType.getRawClass()); if (CACHE_UNKNOWN_MAPPINGS) { _serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this); } return ser; } } } return (JsonSerializer<Object>) handleSecondaryContextualization(ser, property); }
/** @since 2.3 */ @SuppressWarnings("unchecked") public JsonSerializer<Object> findPrimaryPropertySerializer( Class<?> valueType, BeanProperty property) throws JsonMappingException { JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType); if (ser == null) { ser = _serializerCache.untypedValueSerializer(valueType); if (ser == null) { ser = _serializerCache.untypedValueSerializer(_config.constructType(valueType)); if (ser == null) { ser = _createAndCacheUntypedSerializer(valueType); if (ser == null) { ser = getUnknownTypeSerializer(valueType); if (CACHE_UNKNOWN_MAPPINGS) { _serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this); } return ser; } } } } return (JsonSerializer<Object>) handlePrimaryContextualization(ser, property); }