Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace org.json with json-simple #7400

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions spring-boot-tools/spring-boot-configuration-metadata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<!-- Compile (should stick to the bare minimum) -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.simple.parser.ParseException;

/**
* Load a {@link ConfigurationMetadataRepository} from the content of arbitrary
Expand Down Expand Up @@ -98,8 +98,7 @@ public ConfigurationMetadataRepository build() {
return result;
}

private SimpleConfigurationMetadataRepository add(InputStream in, Charset charset)
throws IOException {
private SimpleConfigurationMetadataRepository add(InputStream in, Charset charset) {
try {
RawConfigurationMetadata metadata = this.reader.read(in, charset);
return create(metadata);
Expand All @@ -108,7 +107,7 @@ private SimpleConfigurationMetadataRepository add(InputStream in, Charset charse
throw new IllegalArgumentException(
"Failed to read configuration " + "metadata", ex);
}
catch (JSONException ex) {
catch (ParseException ex) {
throw new IllegalArgumentException(
"Invalid configuration " + "metadata document", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
* Read standard json metadata format as {@link ConfigurationMetadataRepository}.
Expand All @@ -40,7 +45,7 @@ class JsonReader {
private final DescriptionExtractor descriptionExtractor = new DescriptionExtractor();

public RawConfigurationMetadata read(InputStream in, Charset charset)
throws IOException {
throws IOException, ParseException {
JSONObject json = readJson(in, charset);
List<ConfigurationMetadataSource> groups = parseAllSources(json);
List<ConfigurationMetadataItem> items = parseAllItems(json);
Expand All @@ -49,102 +54,83 @@ public RawConfigurationMetadata read(InputStream in, Charset charset)
}

private List<ConfigurationMetadataSource> parseAllSources(JSONObject root) {
List<ConfigurationMetadataSource> result = new ArrayList<ConfigurationMetadataSource>();
if (!root.has("groups")) {
return result;
}
JSONArray sources = root.getJSONArray("groups");
for (int i = 0; i < sources.length(); i++) {
JSONObject source = sources.getJSONObject(i);
result.add(parseSource(source));
}
return result;
return parse(root, this::parseSource, "groups");
}

private List<ConfigurationMetadataItem> parseAllItems(JSONObject root) {
List<ConfigurationMetadataItem> result = new ArrayList<ConfigurationMetadataItem>();
if (!root.has("properties")) {
return result;
}
JSONArray items = root.getJSONArray("properties");
for (int i = 0; i < items.length(); i++) {
JSONObject item = items.getJSONObject(i);
result.add(parseItem(item));
}
return result;
return parse(root, this::parseItem, "properties");
}

private List<ConfigurationMetadataHint> parseAllHints(JSONObject root) {
List<ConfigurationMetadataHint> result = new ArrayList<ConfigurationMetadataHint>();
if (!root.has("hints")) {
return result;
}
JSONArray items = root.getJSONArray("hints");
for (int i = 0; i < items.length(); i++) {
JSONObject item = items.getJSONObject(i);
result.add(parseHint(item));
return parse(root, this::parseHint, "hints");
}

private <T> List<T> parse(JSONObject root, Function<JSONObject, T> parser, String key) {
if (!root.containsKey(key)) {
return Collections.emptyList();
}
return result;

List<JSONObject> items = getMustExist(root, (key));
return items.stream().map(parser).collect(Collectors.toList());
}

private ConfigurationMetadataSource parseSource(JSONObject json) {
ConfigurationMetadataSource source = new ConfigurationMetadataSource();
source.setGroupId(json.getString("name"));
source.setType(json.optString("type", null));
String description = json.optString("description", null);
source.setGroupId(getMustExist(json, "name"));
source.setType(getOrNull(json, "type"));
String description = getOrNull(json, "description");
source.setDescription(description);
source.setShortDescription(
this.descriptionExtractor.getShortDescription(description));
source.setSourceType(json.optString("sourceType", null));
source.setSourceMethod(json.optString("sourceMethod", null));
source.setSourceType(getOrNull(json, "sourceType"));
source.setSourceMethod(getOrNull(json, "sourceMethod"));
return source;
}

private ConfigurationMetadataItem parseItem(JSONObject json) {
ConfigurationMetadataItem item = new ConfigurationMetadataItem();
item.setId(json.getString("name"));
item.setType(json.optString("type", null));
String description = json.optString("description", null);
item.setId(getMustExist(json, "name"));
item.setType(getOrNull(json, "type"));
String description = getOrNull(json, "description");
item.setDescription(description);
item.setShortDescription(
this.descriptionExtractor.getShortDescription(description));
item.setDefaultValue(readItemValue(json.opt("defaultValue")));
item.setDefaultValue(readItemValue(getOrNull(json, "defaultValue")));
item.setDeprecation(parseDeprecation(json));
item.setSourceType(json.optString("sourceType", null));
item.setSourceMethod(json.optString("sourceMethod", null));
item.setSourceType(getOrNull(json, "sourceType"));
item.setSourceMethod(getOrNull(json, "sourceMethod"));
return item;
}

private ConfigurationMetadataHint parseHint(JSONObject json) {
ConfigurationMetadataHint hint = new ConfigurationMetadataHint();
hint.setId(json.getString("name"));
if (json.has("values")) {
JSONArray values = json.getJSONArray("values");
for (int i = 0; i < values.length(); i++) {
JSONObject value = values.getJSONObject(i);
hint.setId(getMustExist(json, ("name")));
if (json.containsKey("values")) {
JSONArray values = getMustExist(json, "values");
for (int i = 0; i < values.size(); i++) {
JSONObject value = (JSONObject) values.get(i);
ValueHint valueHint = new ValueHint();
valueHint.setValue(readItemValue(value.get("value")));
String description = value.optString("description", null);
valueHint.setValue(readItemValue(getMustExist(value, "value")));
String description = getOrNull(value, "description");
valueHint.setDescription(description);
valueHint.setShortDescription(
this.descriptionExtractor.getShortDescription(description));
hint.getValueHints().add(valueHint);
}
}
if (json.has("providers")) {
JSONArray providers = json.getJSONArray("providers");
for (int i = 0; i < providers.length(); i++) {
JSONObject provider = providers.getJSONObject(i);
if (json.containsKey("providers")) {
JSONArray providers = getMustExist(json, ("providers"));
for (int i = 0; i < providers.size(); i++) {
JSONObject provider = (JSONObject) providers.get(i);
ValueProvider valueProvider = new ValueProvider();
valueProvider.setName(provider.getString("name"));
if (provider.has("parameters")) {
JSONObject parameters = provider.getJSONObject("parameters");
Iterator<?> keys = parameters.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
valueProvider.setName(getMustExist(provider, "name"));
if (provider.containsKey("parameters")) {
JSONObject parameters = getMustExist(provider, "parameters");
Set<String> keys = parameters.keySet();
keys.forEach(key -> {
valueProvider.getParameters().put(key,
readItemValue(parameters.get(key)));
}
readItemValue(getMustExist(parameters, key)));
});
}
hint.getValueProviders().add(valueProvider);
}
Expand All @@ -153,43 +139,66 @@ private ConfigurationMetadataHint parseHint(JSONObject json) {
}

private Deprecation parseDeprecation(JSONObject object) {
if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
if (object.containsKey("deprecation")) {
JSONObject deprecationJsonObject = getMustExist(object, "deprecation");
Deprecation deprecation = new Deprecation();
deprecation.setReason(deprecationJsonObject.optString("reason", null));
deprecation.setReason(getOrNull(deprecationJsonObject, "reason"));
deprecation
.setReplacement(deprecationJsonObject.optString("replacement", null));
.setReplacement(getOrNull(deprecationJsonObject, "replacement"));
return deprecation;
}
return (object.optBoolean("deprecated") ? new Deprecation() : null);
final Boolean deprecated = getOrNull(object, "deprecated");
return (deprecated != null && deprecated) ? new Deprecation() : null;
}

private Object readItemValue(Object value) {
if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
Object[] content = new Object[array.length()];
for (int i = 0; i < array.length(); i++) {
Object[] content = new Object[array.size()];
for (int i = 0; i < array.size(); i++) {
content[i] = array.get(i);
}
return content;
}
return value;
}

private JSONObject readJson(InputStream in, Charset charset) throws IOException {
private JSONObject readJson(InputStream in, Charset charset) throws IOException, ParseException {
try {
StringBuilder out = new StringBuilder();
StringWriter out = new StringWriter();
InputStreamReader reader = new InputStreamReader(in, charset);
char[] buffer = new char[BUFFER_SIZE];
int bytesRead = -1;
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
out.append(buffer, 0, bytesRead);
out.append(new String(buffer), 0, bytesRead);
}
return new JSONObject(out.toString());
JSONParser parser = new JSONParser();
return (JSONObject) parser.parse(out.toString());
}
finally {
in.close();
}
}

@SuppressWarnings("unchecked")
private <T> T get(JSONObject source, String key, boolean mustExist) {
final T value = (T) source.get(key);
if (value != null) {
return value;
}

if (mustExist) {
throw new IllegalStateException("Key " + key + "not found.");
}

return null;
}

private <T> T getOrNull(JSONObject source, String key) {
return get(source, key, false);
}

private <T> T getMustExist(JSONObject source, String key) {
return get(source, key, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package org.springframework.boot.configurationmetadata;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.json.JSONException;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -37,20 +35,20 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
private final JsonReader reader = new JsonReader();

@Test
public void emptyMetadata() throws IOException {
public void emptyMetadata() throws Exception {
RawConfigurationMetadata rawMetadata = readFor("empty");
assertThat(rawMetadata.getSources()).isEmpty();
assertThat(rawMetadata.getItems()).isEmpty();
}

@Test
public void invalidMetadata() throws IOException {
this.thrown.expect(JSONException.class);
public void invalidMetadata() throws Exception {
this.thrown.expect(IllegalStateException.class);
readFor("invalid");
}

@Test
public void emptyGroupName() throws IOException {
public void emptyGroupName() throws Exception {
RawConfigurationMetadata rawMetadata = readFor("empty-groups");
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
assertThat(items).hasSize(2);
Expand All @@ -62,7 +60,7 @@ public void emptyGroupName() throws IOException {
}

@Test
public void simpleMetadata() throws IOException {
public void simpleMetadata() throws Exception {
RawConfigurationMetadata rawMetadata = readFor("foo");
List<ConfigurationMetadataSource> sources = rawMetadata.getSources();
assertThat(sources).hasSize(2);
Expand Down Expand Up @@ -92,7 +90,7 @@ public void simpleMetadata() throws IOException {
assertThat(hint.getId()).isEqualTo("spring.foo.counter");
assertThat(hint.getValueHints()).hasSize(1);
ValueHint valueHint = hint.getValueHints().get(0);
assertThat(valueHint.getValue()).isEqualTo(42);
assertThat(valueHint.getValue()).isEqualTo(42L);
assertThat(valueHint.getDescription()).isEqualTo(
"Because that's the answer to any question, choose it. \nReally.");
assertThat(valueHint.getShortDescription())
Expand All @@ -106,7 +104,7 @@ public void simpleMetadata() throws IOException {
}

@Test
public void metadataHints() throws IOException {
public void metadataHints() throws Exception {
RawConfigurationMetadata rawMetadata = readFor("bar");
List<ConfigurationMetadataHint> hints = rawMetadata.getHints();
assertThat(hints).hasSize(1);
Expand All @@ -133,7 +131,7 @@ public void metadataHints() throws IOException {
}

@Test
public void rootMetadata() throws IOException {
public void rootMetadata() throws Exception {
RawConfigurationMetadata rawMetadata = readFor("root");
List<ConfigurationMetadataSource> sources = rawMetadata.getSources();
assertThat(sources).isEmpty();
Expand All @@ -144,7 +142,7 @@ public void rootMetadata() throws IOException {
}

@Test
public void deprecatedMetadata() throws IOException {
public void deprecatedMetadata() throws Exception {
RawConfigurationMetadata rawMetadata = readFor("deprecated");
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
assertThat(items).hasSize(3);
Expand All @@ -171,8 +169,7 @@ public void deprecatedMetadata() throws IOException {
assertThat(item3.getDeprecation()).isEqualTo(null);
}

RawConfigurationMetadata readFor(String path) throws IOException {
private RawConfigurationMetadata readFor(String path) throws Exception {
return this.reader.read(getInputStreamFor(path), DEFAULT_CHARSET);
}

}
Loading