• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.ser;
2 
3 import com.fasterxml.jackson.databind.*;
4 
5 /**
6  * Add-on interface that {@link JsonSerializer}s can implement to get a callback
7  * that can be used to create contextual instances of serializer to use for
8  * handling properties of supported type. This can be useful
9  * for serializers that can be configured by annotations, or should otherwise
10  * have differing behavior depending on what kind of property is being serialized.
11  *<p>
12  * Note that in cases where serializer needs both contextualization and
13  * resolution -- that is, implements both this interface and {@link ResolvableSerializer}
14  * -- resolution via {@link ResolvableSerializer} occurs first, and contextual
15  * resolution (via this interface) later on.
16  */
17 public interface ContextualSerializer
18 {
19     /**
20      * Method called to see if a different (or differently configured) serializer
21      * is needed to serialize values of specified property.
22      * Note that instance that this method is called on is typically shared one and
23      * as a result method should <b>NOT</b> modify this instance but rather construct
24      * and return a new instance. This instance should only be returned as-is, in case
25      * it is already suitable for use.
26      *
27      * @param prov Serializer provider to use for accessing config, other serializers
28      * @param property Method or field that represents the property
29      *   (and is used to access value to serialize).
30      *   Should be available; but there may be cases where caller cannot provide it and
31      *   null is passed instead (in which case impls usually pass 'this' serializer as is)
32      *
33      * @return Serializer to use for serializing values of specified property;
34      *   may be this instance or a new instance.
35      *
36      * @throws JsonMappingException
37      */
createContextual(SerializerProvider prov, BeanProperty property)38     public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
39         throws JsonMappingException;
40 }
41