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 035 * documents. 036 * </p> 037 * 038 * @since 2.2 039 */ 040public class JSONConfiguration extends AbstractYAMLBasedConfiguration 041 implements FileBasedConfiguration, InputStreamSupport 042{ 043 044 /** 045 * The object mapper used by the {@code JSONConfiguration}. 046 */ 047 private final ObjectMapper mapper = new ObjectMapper(); 048 049 /** 050 * The {@code MapType} used to convert types. 051 */ 052 private final MapType type = mapper.getTypeFactory() 053 .constructMapType(Map.class, String.class, Object.class); 054 055 /** 056 * Creates a new instance of {@code JSONConfiguration}. 057 */ 058 public JSONConfiguration() 059 { 060 super(); 061 } 062 063 /** 064 * Creates a new instance of {@code JSONConfiguration} as a copy of the 065 * specified configuration. 066 * 067 * @param c the configuration to be copied 068 */ 069 public JSONConfiguration(final HierarchicalConfiguration<ImmutableNode> c) 070 { 071 super(c); 072 } 073 074 @Override 075 public void read(final Reader in) throws ConfigurationException 076 { 077 try 078 { 079 final Map<String, Object> map = mapper.readValue(in, this.type); 080 load(map); 081 } 082 catch (final Exception e) 083 { 084 rethrowException(e); 085 } 086 } 087 088 @Override 089 public void write(final Writer out) throws ConfigurationException, IOException 090 { 091 this.mapper.writer().writeValue(out, constructMap( 092 this.getNodeModel().getNodeHandler().getRootNode())); 093 } 094 095 /** 096 * Loads the configuration from the given input stream. 097 * 098 * @param in the input stream 099 * @throws ConfigurationException if an error occurs 100 */ 101 @Override 102 public void read(final InputStream in) throws ConfigurationException 103 { 104 try 105 { 106 final Map<String, Object> map = mapper.readValue(in, this.type); 107 load(map); 108 } 109 catch (final Exception e) 110 { 111 rethrowException(e); 112 } 113 } 114 115}