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 com.fasterxml.jackson.databind.ObjectMapper;
021import com.fasterxml.jackson.databind.type.MapType;
022import org.apache.commons.configuration2.ex.ConfigurationException;
023import org.apache.commons.configuration2.io.InputStreamSupport;
024import org.apache.commons.configuration2.tree.ImmutableNode;
025
026import java.io.IOException;
027import java.io.InputStream;
028import java.io.Reader;
029import java.io.Writer;
030import java.util.Map;
031
032/**
033 * <p>
034 * A specialized hierarchical configuration class that is able to parse JSON documents.
035 * </p>
036 *
037 * @since 2.2
038 */
039public class JSONConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
040
041    /**
042     * The object mapper used by the {@code JSONConfiguration}.
043     */
044    private final ObjectMapper mapper = new ObjectMapper();
045
046    /**
047     * The {@code MapType} used to convert types.
048     */
049    private final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
050
051    /**
052     * Creates a new instance of {@code JSONConfiguration}.
053     */
054    public JSONConfiguration() {
055    }
056
057    /**
058     * Creates a new instance of {@code JSONConfiguration} as a copy of the specified configuration.
059     *
060     * @param c the configuration to be copied
061     */
062    public JSONConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
063        super(c);
064    }
065
066    @Override
067    public void read(final Reader in) throws ConfigurationException {
068        try {
069            load(mapper.readValue(in, this.type));
070        } catch (final Exception e) {
071            rethrowException(e);
072        }
073    }
074
075    @Override
076    public void write(final Writer out) throws ConfigurationException, IOException {
077        this.mapper.writer().writeValue(out, constructMap(this.getNodeModel().getNodeHandler().getRootNode()));
078    }
079
080    /**
081     * Loads the configuration from the given input stream.
082     *
083     * @param in the input stream
084     * @throws ConfigurationException if an error occurs
085     */
086    @Override
087    public void read(final InputStream in) throws ConfigurationException {
088        try {
089            load(mapper.readValue(in, this.type));
090        } catch (final Exception e) {
091            rethrowException(e);
092        }
093    }
094
095}