View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package com.addthis.codec.jackson;
15  
16  import java.util.Collection;
17  
18  import com.addthis.codec.plugins.PluginMap;
19  import com.addthis.codec.plugins.PluginRegistry;
20  
21  import com.fasterxml.jackson.annotation.JsonTypeInfo;
22  import com.fasterxml.jackson.databind.DeserializationConfig;
23  import com.fasterxml.jackson.databind.JavaType;
24  import com.fasterxml.jackson.databind.cfg.MapperConfig;
25  import com.fasterxml.jackson.databind.jsontype.NamedType;
26  import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
27  import com.fasterxml.jackson.databind.type.TypeFactory;
28  
29  public class CodecTypeResolverBuilder extends StdTypeResolverBuilder {
30      private final PluginMap pluginMap;
31      private final TypeFactory typeFactory;
32      private final PluginRegistry pluginRegistry;
33  
34      public CodecTypeResolverBuilder(PluginMap pluginMap, TypeFactory typeFactory, PluginRegistry pluginRegistry) {
35          this.pluginMap = pluginMap;
36          this.typeFactory = typeFactory;
37          this.pluginRegistry = pluginRegistry;
38          defaultImpl(pluginMap.defaultSugar());
39          typeProperty(pluginMap.classField());
40          inclusion(JsonTypeInfo.As.PROPERTY);
41      }
42  
43      @Override public CodecTypeDeserializer buildTypeDeserializer(DeserializationConfig config,
44                                                                   JavaType baseType,
45                                                                   Collection<NamedType> subtypes) {
46          CodecTypeIdResolver codecTypeIdResolver = idResolver(config, baseType, subtypes, false, true);
47          Class<?> defaultImplForType = _defaultImpl;
48          if ((_defaultImpl == null) || !baseType.getRawClass().isAssignableFrom(_defaultImpl)) {
49              defaultImplForType = baseType.getRawClass();
50          }
51          return new CodecTypeDeserializer(pluginMap, _includeAs, baseType, codecTypeIdResolver,
52                                           _typeProperty, _typeIdVisible, defaultImplForType);
53      }
54  
55      @Override
56      protected CodecTypeIdResolver idResolver(MapperConfig<?> config,
57                                               JavaType baseType,
58                                               Collection<NamedType> subtypes,
59                                               boolean forSer,
60                                               boolean forDeser) {
61          return new CodecTypeIdResolver(pluginMap, baseType, typeFactory, subtypes, pluginRegistry);
62      }
63  }