    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 *     https://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    /**
    068     * Loads the configuration from the given input stream.
    069     *
    070     * @param in the input stream
    071     * @throws ConfigurationException if an error occurs
    072     */
    073    @Override
    074    public void read(final InputStream in) throws ConfigurationException {
    075        try {
    076            load(mapper.readValue(in, this.type));
    077        } catch (final Exception e) {
    078            rethrowException(e);
    079        }
    080    }
    081
    082    @Override
    083    public void read(final Reader in) throws ConfigurationException {
    084        try {
    085            load(mapper.readValue(in, this.type));
    086        } catch (final Exception e) {
    087            rethrowException(e);
    088        }
    089    }
    090
    091    @Override
    092    public void write(final Writer out) throws ConfigurationException, IOException {
    093        this.mapper.writer().writeValue(out, constructMap(getNodeModel().getNodeHandler().getRootNode()));
    094    }
    095
    096}