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.json;
15  
16  import java.io.IOException;
17  
18  import com.addthis.codec.Codec;
19  import com.addthis.codec.config.Configs;
20  import com.addthis.codec.jackson.CodecJackson;
21  import com.addthis.codec.jackson.Jackson;
22  import com.addthis.maljson.JSONObject;
23  
24  import com.fasterxml.jackson.core.JsonProcessingException;
25  import com.fasterxml.jackson.databind.JsonNode;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Jackson based implementation of the old Codec interface. New code is probably
32   * better off using {@link Configs} or {@link CodecJackson}.
33   */
34  public final class CodecJSON implements Codec {
35      private CodecJSON() {}
36  
37      private static final Logger log = LoggerFactory.getLogger(CodecJSON.class);
38  
39      public static final CodecJSON INSTANCE = new CodecJSON();
40  
41      @Override
42      public byte[] encode(Object obj) throws Exception {
43          return Jackson.defaultCodec().getObjectMapper().writeValueAsBytes(obj);
44      }
45  
46      @Override
47      public <T> T decode(T shell, byte[] data) throws IOException {
48          return Jackson.defaultCodec().getObjectMapper().readerForUpdating(shell).readValue(data);
49      }
50  
51      @Override
52      public <T> T decode(Class<T> type, byte[] data) throws Exception {
53          return Jackson.defaultCodec().getObjectMapper().reader(type).readValue(data);
54      }
55  
56      @Override
57      public boolean storesNull(byte[] data) {
58          throw new UnsupportedOperationException();
59      }
60  
61      public static JsonNode encodeJsonNode(Object object) throws Exception {
62          return Jackson.defaultCodec().getObjectMapper().valueToTree(object);
63      }
64  
65      /** @deprecated Use {@link #encodeJsonNode(Object)} or {@link CodecJackson} */
66      @Deprecated
67      public static JSONObject encodeJSON(Object object) throws Exception {
68          return new JSONObject(Jackson.defaultCodec().getObjectMapper().writeValueAsString(object));
69      }
70  
71      public static String encodeString(Object object) throws JsonProcessingException {
72          return Jackson.defaultCodec().getObjectMapper().writeValueAsString(object);
73      }
74  
75      public static String tryEncodeString(Object object, String defaultValue) {
76          try {
77              return Jackson.defaultCodec().getObjectMapper().writeValueAsString(object);
78          } catch (JsonProcessingException ex) {
79              return defaultValue;
80          }
81      }
82  
83      /** @deprecated Use {@link #decodeString(Class, String)} instead. */
84      @Deprecated
85      public static <T> T decodeString(T object, String json) throws IOException {
86          return Jackson.defaultCodec().getObjectMapper().readerForUpdating(object).readValue(json);
87      }
88  
89      public static <T> T decodeString(Class<T> type, String json) throws IOException {
90          return Jackson.defaultCodec().getObjectMapper().reader(type).readValue(json);
91      }
92  
93      public static <T> T decodeJSON(T object, JsonNode json) throws IOException {
94          return Jackson.defaultCodec().getObjectMapper().readerForUpdating(object).readValue(json);
95      }
96  }