• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.ser;
2 
3 import java.util.Map;
4 
5 import com.fasterxml.jackson.core.*;
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
8 import com.fasterxml.jackson.databind.ser.std.MapSerializer;
9 
10 /**
11  * Class similar to {@link BeanPropertyWriter}, but that will be used
12  * for serializing {@link com.fasterxml.jackson.annotation.JsonAnyGetter} annotated
13  * (Map) properties
14  */
15 public class AnyGetterWriter
16 {
17     protected final BeanProperty _property;
18 
19     /**
20      * Method (or field) that represents the "any getter"
21      */
22     protected final AnnotatedMember _accessor;
23 
24     protected JsonSerializer<Object> _serializer;
25 
26     protected MapSerializer _mapSerializer;
27 
28     @SuppressWarnings("unchecked")
AnyGetterWriter(BeanProperty property, AnnotatedMember accessor, JsonSerializer<?> serializer)29     public AnyGetterWriter(BeanProperty property,
30             AnnotatedMember accessor, JsonSerializer<?> serializer)
31     {
32         _accessor = accessor;
33         _property = property;
34         _serializer = (JsonSerializer<Object>) serializer;
35         if (serializer instanceof MapSerializer) {
36             _mapSerializer = (MapSerializer) serializer;
37         }
38     }
39 
40     /**
41      * @since 2.8.3
42      */
fixAccess(SerializationConfig config)43     public void fixAccess(SerializationConfig config) {
44         _accessor.fixAccess(
45                 config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
46     }
47 
getAndSerialize(Object bean, JsonGenerator gen, SerializerProvider provider)48     public void getAndSerialize(Object bean, JsonGenerator gen, SerializerProvider provider)
49         throws Exception
50     {
51         Object value = _accessor.getValue(bean);
52         if (value == null) {
53             return;
54         }
55         if (!(value instanceof Map<?,?>)) {
56             provider.reportBadDefinition(_property.getType(), String.format(
57                     "Value returned by 'any-getter' %s() not java.util.Map but %s",
58                     _accessor.getName(), value.getClass().getName()));
59         }
60         // 23-Feb-2015, tatu: Nasty, but has to do (for now)
61         if (_mapSerializer != null) {
62             _mapSerializer.serializeWithoutTypeInfo((Map<?,?>) value, gen, provider);
63             return;
64         }
65         _serializer.serialize(value, gen, provider);
66     }
67 
68     /**
69      * @since 2.3
70      */
getAndFilter(Object bean, JsonGenerator gen, SerializerProvider provider, PropertyFilter filter)71     public void getAndFilter(Object bean, JsonGenerator gen, SerializerProvider provider,
72             PropertyFilter filter)
73         throws Exception
74     {
75         Object value = _accessor.getValue(bean);
76         if (value == null) {
77             return;
78         }
79         if (!(value instanceof Map<?,?>)) {
80             provider.reportBadDefinition(_property.getType(),
81                     String.format("Value returned by 'any-getter' (%s()) not java.util.Map but %s",
82                     _accessor.getName(), value.getClass().getName()));
83         }
84         // 19-Oct-2014, tatu: Should we try to support @JsonInclude options here?
85         if (_mapSerializer != null) {
86             _mapSerializer.serializeFilteredAnyProperties(provider, gen, bean,(Map<?,?>) value,
87                     filter, null);
88             return;
89         }
90         // ... not sure how custom handler would do it
91         _serializer.serialize(value, gen, provider);
92     }
93 
94     // Note: NOT part of ResolvableSerializer...
95     @SuppressWarnings("unchecked")
resolve(SerializerProvider provider)96     public void resolve(SerializerProvider provider) throws JsonMappingException
97     {
98         // 05-Sep-2013, tatu: I _think_ this can be considered a primary property...
99         if (_serializer instanceof ContextualSerializer) {
100             JsonSerializer<?> ser = provider.handlePrimaryContextualization(_serializer, _property);
101             _serializer = (JsonSerializer<Object>) ser;
102             if (ser instanceof MapSerializer) {
103                 _mapSerializer = (MapSerializer) ser;
104             }
105         }
106     }
107 }
108