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      /**
68       * Loads the configuration from the given input stream.
69       *
70       * @param in the input stream
71       * @throws ConfigurationException if an error occurs
72       */
73      @Override
74      public void read(final InputStream in) throws ConfigurationException {
75          try {
76              load(mapper.readValue(in, this.type));
77          } catch (final Exception e) {
78              rethrowException(e);
79          }
80      }
81  
82      @Override
83      public void read(final Reader in) throws ConfigurationException {
84          try {
85              load(mapper.readValue(in, this.type));
86          } catch (final Exception e) {
87              rethrowException(e);
88          }
89      }
90  
91      @Override
92      public void write(final Writer out) throws ConfigurationException, IOException {
93          this.mapper.writer().writeValue(out, constructMap(getNodeModel().getNodeHandler().getRootNode()));
94      }
95  
96  }