001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.configuration2;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.Reader;
023import java.io.Writer;
024import java.util.Map;
025
026import org.apache.commons.configuration2.ex.ConfigurationException;
027import org.apache.commons.configuration2.io.InputStreamSupport;
028import org.apache.commons.configuration2.tree.ImmutableNode;
029
030import com.fasterxml.jackson.databind.ObjectMapper;
031import com.fasterxml.jackson.databind.type.MapType;
032
033/**
034 * <p>
035 * A specialized hierarchical configuration class that is able to parse JSON documents.
036 * </p>
037 *
038 * @since 2.2
039 */
040public class JSONConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
041
042    /**
043     * The object mapper used by the {@code JSONConfiguration}.
044     */
045    private final ObjectMapper mapper = new ObjectMapper();
046
047    /**
048     * The {@code MapType} used to convert types.
049     */
050    private final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
051
052    /**
053     * Creates a new instance of {@code JSONConfiguration}.
054     */
055    public JSONConfiguration() {
056    }
057
058    /**
059     * Creates a new instance of {@code JSONConfiguration} as a copy of the specified configuration.
060     *
061     * @param c the configuration to be copied
062     */
063    public JSONConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
064        super(c);
065    }
066
067    @Override
068    public void read(final Reader in) throws ConfigurationException {
069        try {
070            load(mapper.readValue(in, this.type));
071        } catch (final Exception e) {
072            rethrowException(e);
073        }
074    }
075
076    @Override
077    public void write(final Writer out) throws ConfigurationException, IOException {
078        this.mapper.writer().writeValue(out, constructMap(this.getNodeModel().getNodeHandler().getRootNode()));
079    }
080
081    /**
082     * Loads the configuration from the given input stream.
083     *
084     * @param in the input stream
085     * @throws ConfigurationException if an error occurs
086     */
087    @Override
088    public void read(final InputStream in) throws ConfigurationException {
089        try {
090            load(mapper.readValue(in, this.type));
091        } catch (final Exception e) {
092            rethrowException(e);
093        }
094    }
095
096}