View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.lib.jackson;
20  
21  import com.fasterxml.jackson.core.JsonGenerator;
22  import com.fasterxml.jackson.core.JsonProcessingException;
23  import com.fasterxml.jackson.databind.SerializationFeature;
24  import com.fasterxml.jackson.databind.json.JsonMapper;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.io.Writer;
29  import java.util.Map;
30  
31  /**
32   * Jackson ObjectMapper that unwraps singleton map values and enable default
33   * typing for handling abstract types serialization.
34   */
35  public class SyncopeJsonMapper extends JsonMapper {
36  
37      private static final long serialVersionUID = -317191546835195103L;
38  
39      public SyncopeJsonMapper() {
40          super();
41  
42          findAndRegisterModules();
43  
44          configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
45      }
46  
47      /**
48       * Unwraps the given value if it implements the Map interface and contains only a single entry, otherwise the
49       * value is returned unmodified.
50       *
51       * @param value the potential Map to unwrap
52       * @return the unwrapped map or the original value
53       */
54      protected Object unwrapMap(final Object value) {
55          if (value instanceof Map) {
56              Map<?, ?> map = (Map<?, ?>) value;
57              if (map.size() == 1) {
58                  return map.values().iterator().next();
59              }
60          }
61  
62          return value;
63      }
64  
65      @Override
66      public void writeValue(final JsonGenerator jgen, final Object value) throws IOException {
67          super.writeValue(jgen, unwrapMap(value));
68      }
69  
70      @Override
71      public void writeValue(final File resultFile, final Object value) throws IOException {
72          super.writeValue(resultFile, unwrapMap(value));
73      }
74  
75      @Override
76      public void writeValue(final OutputStream out, final Object value) throws IOException {
77          super.writeValue(out, unwrapMap(value));
78      }
79  
80      @Override
81      public void writeValue(final Writer writer, final Object value) throws IOException {
82          super.writeValue(writer, unwrapMap(value));
83      }
84  
85      @Override
86      public byte[] writeValueAsBytes(final Object value) throws JsonProcessingException {
87          return super.writeValueAsBytes(unwrapMap(value));
88      }
89  
90      @Override
91      public String writeValueAsString(final Object value) throws JsonProcessingException {
92          return super.writeValueAsString(unwrapMap(value));
93      }
94  }