JSONConfiguration.java

  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. package org.apache.commons.configuration2;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.Reader;
  21. import java.io.Writer;
  22. import java.util.Map;

  23. import org.apache.commons.configuration2.ex.ConfigurationException;
  24. import org.apache.commons.configuration2.io.InputStreamSupport;
  25. import org.apache.commons.configuration2.tree.ImmutableNode;

  26. import com.fasterxml.jackson.databind.ObjectMapper;
  27. import com.fasterxml.jackson.databind.type.MapType;

  28. /**
  29.  * <p>
  30.  * A specialized hierarchical configuration class that is able to parse JSON documents.
  31.  * </p>
  32.  *
  33.  * @since 2.2
  34.  */
  35. public class JSONConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {

  36.     /**
  37.      * The object mapper used by the {@code JSONConfiguration}.
  38.      */
  39.     private final ObjectMapper mapper = new ObjectMapper();

  40.     /**
  41.      * The {@code MapType} used to convert types.
  42.      */
  43.     private final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);

  44.     /**
  45.      * Creates a new instance of {@code JSONConfiguration}.
  46.      */
  47.     public JSONConfiguration() {
  48.     }

  49.     /**
  50.      * Creates a new instance of {@code JSONConfiguration} as a copy of the specified configuration.
  51.      *
  52.      * @param c the configuration to be copied
  53.      */
  54.     public JSONConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
  55.         super(c);
  56.     }

  57.     /**
  58.      * Loads the configuration from the given input stream.
  59.      *
  60.      * @param in the input stream
  61.      * @throws ConfigurationException if an error occurs
  62.      */
  63.     @Override
  64.     public void read(final InputStream in) throws ConfigurationException {
  65.         try {
  66.             load(mapper.readValue(in, this.type));
  67.         } catch (final Exception e) {
  68.             rethrowException(e);
  69.         }
  70.     }

  71.     @Override
  72.     public void read(final Reader in) throws ConfigurationException {
  73.         try {
  74.             load(mapper.readValue(in, this.type));
  75.         } catch (final Exception e) {
  76.             rethrowException(e);
  77.         }
  78.     }

  79.     @Override
  80.     public void write(final Writer out) throws ConfigurationException, IOException {
  81.         this.mapper.writer().writeValue(out, constructMap(getNodeModel().getNodeHandler().getRootNode()));
  82.     }

  83. }