• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.codegen.internal;
17 
18 import com.fasterxml.jackson.core.JsonParser;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.databind.ObjectWriter;
21 import com.fasterxml.jackson.databind.SerializationFeature;
22 import com.fasterxml.jackson.jr.ob.JSON;
23 import com.fasterxml.jackson.jr.stree.JacksonJrsTreeCodec;
24 import com.fasterxml.jackson.jr.stree.JrsValue;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.Writer;
28 
29 public final class Jackson {
30     private static final JSON MAPPER;
31     private static final JSON FAIL_ON_UNKNOWN_PROPERTIES_MAPPER;
32     private static volatile ObjectWriter OBJECT_MAPPER;
33 
34     static {
35         JSON.Builder mapperBuilder = JSON.builder()
36                                          .disable(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY)
37                                          .enable(JSON.Feature.PRETTY_PRINT_OUTPUT)
38                                          .enable(JSON.Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS)
39                                          .treeCodec(new JacksonJrsTreeCodec());
40 
41         mapperBuilder.streamFactory().enable(JsonParser.Feature.ALLOW_COMMENTS);
42 
43         MAPPER = mapperBuilder.build();
44 
45         mapperBuilder.enable(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY);
46 
47         FAIL_ON_UNKNOWN_PROPERTIES_MAPPER = mapperBuilder.build();
48     }
49 
Jackson()50     private Jackson() {
51     }
52 
readJrsValue(String input)53     public static JrsValue readJrsValue(String input) throws IOException {
54         return MAPPER.beanFrom(JrsValue.class, input);
55     }
56 
load(Class<T> clazz, File file)57     public static <T> T load(Class<T> clazz, File file) throws IOException {
58         return MAPPER.beanFrom(clazz, file);
59     }
60 
load(Class<T> clazz, String content)61     public static <T> T load(Class<T> clazz, String content) throws IOException {
62         return MAPPER.beanFrom(clazz, content);
63     }
64 
load(Class<T> clazz, File file, boolean failOnUnknownProperties)65     public static <T> T load(Class<T> clazz, File file, boolean failOnUnknownProperties) throws IOException {
66         if (failOnUnknownProperties) {
67             return FAIL_ON_UNKNOWN_PROPERTIES_MAPPER.beanFrom(clazz, file);
68         } else {
69             return load(clazz, file);
70         }
71     }
72 
writeWithObjectMapper(Object value, Writer w)73     public static void writeWithObjectMapper(Object value, Writer w) throws IOException {
74         if (OBJECT_MAPPER == null) {
75             synchronized (Jackson.class) {
76                 if (OBJECT_MAPPER == null) {
77                     OBJECT_MAPPER = new ObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
78                                                       .writerWithDefaultPrettyPrinter();
79                 }
80             }
81         }
82 
83         OBJECT_MAPPER.writeValue(w, value);
84     }
85 
86 }
87