All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.configuration2.JSONConfiguration Maven / Gradle / Ivy

Go to download

Tools to assist in the reading of configuration/preferences files in various formats

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.configuration2;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;

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

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

/**
 * 

* A specialized hierarchical configuration class that is able to parse JSON documents. *

* * @since 2.2 */ public class JSONConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport { /** * The object mapper used by the {@code JSONConfiguration}. */ private final ObjectMapper mapper = new ObjectMapper(); /** * The {@code MapType} used to convert types. */ private final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class); /** * Creates a new instance of {@code JSONConfiguration}. */ public JSONConfiguration() { } /** * Creates a new instance of {@code JSONConfiguration} as a copy of the specified configuration. * * @param c the configuration to be copied */ public JSONConfiguration(final HierarchicalConfiguration c) { super(c); } /** * Loads the configuration from the given input stream. * * @param in the input stream * @throws ConfigurationException if an error occurs */ @Override public void read(final InputStream in) throws ConfigurationException { try { load(mapper.readValue(in, this.type)); } catch (final Exception e) { rethrowException(e); } } @Override public void read(final Reader in) throws ConfigurationException { try { load(mapper.readValue(in, this.type)); } catch (final Exception e) { rethrowException(e); } } @Override public void write(final Writer out) throws ConfigurationException, IOException { this.mapper.writer().writeValue(out, constructMap(getNodeModel().getNodeHandler().getRootNode())); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy