Skip to content

Instantly share code, notes, and snippets.

@eungju
Created October 5, 2011 02:48
Show Gist options
  • Select an option

  • Save eungju/1263507 to your computer and use it in GitHub Desktop.

Select an option

Save eungju/1263507 to your computer and use it in GitHub Desktop.
JsonPropertiesPersister for Spring
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="propertiesPersister">
<bean class="JsonPropertiesPersister"></bean>
</property>
<property name="locations">
<list>
<value>classpath:public.json</value>
<value>classpath:private.json</value>
</list>
</property>
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
</bean>
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.util.PropertiesPersister;
public class JsonPropertiesPersister implements PropertiesPersister {
@Override
public void load(Properties props, InputStream is) throws IOException {
load(props, new InputStreamReader(is));
}
@Override
public void load(Properties props, Reader reader) throws IOException {
ObjectMapper m = new ObjectMapper();
m.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
m.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
m.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
JsonNode node = m.readTree(reader);
load(props, node, "");
}
void load(Properties props, JsonNode node, String key) {
if (node.isObject()) {
Iterator<Map.Entry<String, JsonNode>> fields = node.getFields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> field = fields.next();
load(props, field.getValue(), join(key, field.getKey()));
}
} else if (node.isArray()) {
Iterator<JsonNode> values = node.getElements();
int i = 0;
while (values.hasNext()) {
JsonNode value = values.next();
load(props, value, join(key, String.valueOf(i)));
i++;
}
} else {
props.setProperty(key, node.getValueAsText());
}
}
String join(String prefix, String key) {
return prefix.isEmpty() ? key : prefix + "." + key;
}
@Override
public void store(Properties props, OutputStream os, String header) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void store(Properties props, Writer writer, String header) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void loadFromXml(Properties props, InputStream is) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
throw new UnsupportedOperationException();
}
}
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class JsonPropertiesPersisterTest {
private JsonPropertiesPersister dut;
private Properties props;
@Before public void beforeEach() {
dut = new JsonPropertiesPersister();
props = new Properties();
}
@Test public void empty() throws IOException {
dut.load(props, new StringReader("{}"));
assertThat(props.isEmpty(), is(true));
}
@Test public void stringValue() throws IOException {
dut.load(props, new StringReader("{key:\"value\"}"));
assertThat(props.getProperty("key"), is("value"));
}
@Test public void numberValue() throws IOException {
dut.load(props, new StringReader("{key:123}"));
assertThat(props.getProperty("key"), is("123"));
}
@Test public void booleanValue() throws IOException {
dut.load(props, new StringReader("{key:true}"));
assertThat(props.getProperty("key"), is("true"));
}
@Test public void array() throws IOException {
dut.load(props, new StringReader("{key:['a','b']}"));
assertThat(props.getProperty("key.0"), is("a"));
assertThat(props.getProperty("key.1"), is("b"));
}
@Test public void object() throws IOException {
dut.load(props, new StringReader("{key:{subKey:'value'}}"));
assertThat(props.getProperty("key.subKey"), is("value"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment