View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration2;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.Reader;
23  import java.io.Writer;
24  import java.util.Map;
25  
26  import org.apache.commons.configuration2.ex.ConfigurationException;
27  import org.apache.commons.configuration2.io.InputStreamSupport;
28  import org.apache.commons.configuration2.tree.ImmutableNode;
29  
30  import com.fasterxml.jackson.databind.ObjectMapper;
31  import com.fasterxml.jackson.databind.type.MapType;
32  
33  /**
34   * <p>
35   * A specialized hierarchical configuration class that is able to parse JSON documents.
36   * </p>
37   *
38   * @since 2.2
39   */
40  public class JSONConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
41  
42      /**
43       * The object mapper used by the {@code JSONConfiguration}.
44       */
45      private final ObjectMapper mapper = new ObjectMapper();
46  
47      /**
48       * The {@code MapType} used to convert types.
49       */
50      private final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
51  
52      /**
53       * Creates a new instance of {@code JSONConfiguration}.
54       */
55      public JSONConfiguration() {
56      }
57  
58      /**
59       * Creates a new instance of {@code JSONConfiguration} as a copy of the specified configuration.
60       *
61       * @param c the configuration to be copied
62       */
63      public JSONConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
64          super(c);
65      }
66  
67      @Override
68      public void read(final Reader in) throws ConfigurationException {
69          try {
70              load(mapper.readValue(in, this.type));
71          } catch (final Exception e) {
72              rethrowException(e);
73          }
74      }
75  
76      @Override
77      public void write(final Writer out) throws ConfigurationException, IOException {
78          this.mapper.writer().writeValue(out, constructMap(this.getNodeModel().getNodeHandler().getRootNode()));
79      }
80  
81      /**
82       * Loads the configuration from the given input stream.
83       *
84       * @param in the input stream
85       * @throws ConfigurationException if an error occurs
86       */
87      @Override
88      public void read(final InputStream in) throws ConfigurationException {
89          try {
90              load(mapper.readValue(in, this.type));
91          } catch (final Exception e) {
92              rethrowException(e);
93          }
94      }
95  
96  }