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 com.addthis.codec.plugins.PluginRegistry;
17  
18  import com.fasterxml.jackson.core.Version;
19  import com.fasterxml.jackson.databind.Module;
20  import com.typesafe.config.Config;
21  
22  public class CodecModule extends Module {
23  
24      private final PluginRegistry pluginRegistry;
25      private final Config globalConfig;
26  
27      public CodecModule(PluginRegistry pluginRegistry, Config globalConfig) {
28          this.pluginRegistry = pluginRegistry;
29          this.globalConfig = globalConfig;
30      }
31  
32      public CodecModule(PluginRegistry pluginRegistry) {
33          this.pluginRegistry = pluginRegistry;
34          this.globalConfig = pluginRegistry.config();
35      }
36  
37      @Override public void setupModule(SetupContext context) {
38          context.appendAnnotationIntrospector(new CodecIntrospector(pluginRegistry));
39          if (globalConfig.getBoolean("addthis.codec.jackson.ignore.underscore")) {
40              context.addDeserializationProblemHandler(new UnderscorePropertyIgnorer());
41          }
42          if (globalConfig.getBoolean("addthis.codec.jackson.ignore.write-only")) {
43              context.addDeserializationProblemHandler(new WriteonlyPropertyIgnorer());
44          }
45          if (globalConfig.getBoolean("addthis.codec.jackson.modify-fields")) {
46              context.addBeanDeserializerModifier(new CodecBeanDeserializerModifier(globalConfig));
47          }
48          context.addDeserializers(new CodecDeserializers());
49      }
50  
51      @Override public String getModuleName() {
52          return "addthis-codec";
53      }
54  
55      @Override public Version version() {
56          return Version.unknownVersion();
57      }
58  }