Migrate to android-json
Migrate from `org.json:json` to the clean room Apache 2.0 licensed version that was developed for Android. Fixes gh-5929
This commit is contained in:
@@ -23,8 +23,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
/**
|
||||
* Load a {@link ConfigurationMetadataRepository} from the content of arbitrary
|
||||
* resource(s).
|
||||
@@ -104,13 +102,8 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
|
||||
RawConfigurationMetadata metadata = this.reader.read(in, charset);
|
||||
return create(metadata);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Failed to read configuration " + "metadata", ex);
|
||||
}
|
||||
catch (JSONException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid configuration " + "metadata document", ex);
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Failed to read configuration metadata", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,14 +41,26 @@ class JsonReader {
|
||||
|
||||
public RawConfigurationMetadata read(InputStream in, Charset charset)
|
||||
throws IOException {
|
||||
JSONObject json = readJson(in, charset);
|
||||
List<ConfigurationMetadataSource> groups = parseAllSources(json);
|
||||
List<ConfigurationMetadataItem> items = parseAllItems(json);
|
||||
List<ConfigurationMetadataHint> hints = parseAllHints(json);
|
||||
return new RawConfigurationMetadata(groups, items, hints);
|
||||
try {
|
||||
JSONObject json = readJson(in, charset);
|
||||
List<ConfigurationMetadataSource> groups = parseAllSources(json);
|
||||
List<ConfigurationMetadataItem> items = parseAllItems(json);
|
||||
List<ConfigurationMetadataHint> hints = parseAllHints(json);
|
||||
return new RawConfigurationMetadata(groups, items, hints);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (ex instanceof IOException) {
|
||||
throw (IOException) ex;
|
||||
}
|
||||
if (ex instanceof RuntimeException) {
|
||||
throw (RuntimeException) ex;
|
||||
}
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ConfigurationMetadataSource> parseAllSources(JSONObject root) {
|
||||
private List<ConfigurationMetadataSource> parseAllSources(JSONObject root)
|
||||
throws Exception {
|
||||
List<ConfigurationMetadataSource> result = new ArrayList<ConfigurationMetadataSource>();
|
||||
if (!root.has("groups")) {
|
||||
return result;
|
||||
@@ -61,7 +73,8 @@ class JsonReader {
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ConfigurationMetadataItem> parseAllItems(JSONObject root) {
|
||||
private List<ConfigurationMetadataItem> parseAllItems(JSONObject root)
|
||||
throws Exception {
|
||||
List<ConfigurationMetadataItem> result = new ArrayList<ConfigurationMetadataItem>();
|
||||
if (!root.has("properties")) {
|
||||
return result;
|
||||
@@ -74,7 +87,8 @@ class JsonReader {
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ConfigurationMetadataHint> parseAllHints(JSONObject root) {
|
||||
private List<ConfigurationMetadataHint> parseAllHints(JSONObject root)
|
||||
throws Exception {
|
||||
List<ConfigurationMetadataHint> result = new ArrayList<ConfigurationMetadataHint>();
|
||||
if (!root.has("hints")) {
|
||||
return result;
|
||||
@@ -87,7 +101,7 @@ class JsonReader {
|
||||
return result;
|
||||
}
|
||||
|
||||
private ConfigurationMetadataSource parseSource(JSONObject json) {
|
||||
private ConfigurationMetadataSource parseSource(JSONObject json) throws Exception {
|
||||
ConfigurationMetadataSource source = new ConfigurationMetadataSource();
|
||||
source.setGroupId(json.getString("name"));
|
||||
source.setType(json.optString("type", null));
|
||||
@@ -100,7 +114,7 @@ class JsonReader {
|
||||
return source;
|
||||
}
|
||||
|
||||
private ConfigurationMetadataItem parseItem(JSONObject json) {
|
||||
private ConfigurationMetadataItem parseItem(JSONObject json) throws Exception {
|
||||
ConfigurationMetadataItem item = new ConfigurationMetadataItem();
|
||||
item.setId(json.getString("name"));
|
||||
item.setType(json.optString("type", null));
|
||||
@@ -115,7 +129,7 @@ class JsonReader {
|
||||
return item;
|
||||
}
|
||||
|
||||
private ConfigurationMetadataHint parseHint(JSONObject json) {
|
||||
private ConfigurationMetadataHint parseHint(JSONObject json) throws Exception {
|
||||
ConfigurationMetadataHint hint = new ConfigurationMetadataHint();
|
||||
hint.setId(json.getString("name"));
|
||||
if (json.has("values")) {
|
||||
@@ -152,7 +166,7 @@ class JsonReader {
|
||||
return hint;
|
||||
}
|
||||
|
||||
private Deprecation parseDeprecation(JSONObject object) {
|
||||
private Deprecation parseDeprecation(JSONObject object) throws Exception {
|
||||
if (object.has("deprecation")) {
|
||||
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
|
||||
Deprecation deprecation = new Deprecation();
|
||||
@@ -164,7 +178,7 @@ class JsonReader {
|
||||
return (object.optBoolean("deprecated") ? new Deprecation() : null);
|
||||
}
|
||||
|
||||
private Object readItemValue(Object value) {
|
||||
private Object readItemValue(Object value) throws Exception {
|
||||
if (value instanceof JSONArray) {
|
||||
JSONArray array = (JSONArray) value;
|
||||
Object[] content = new Object[array.length()];
|
||||
@@ -176,7 +190,7 @@ class JsonReader {
|
||||
return value;
|
||||
}
|
||||
|
||||
private JSONObject readJson(InputStream in, Charset charset) throws IOException {
|
||||
private JSONObject readJson(InputStream in, Charset charset) throws Exception {
|
||||
try {
|
||||
StringBuilder out = new StringBuilder();
|
||||
InputStreamReader reader = new InputStreamReader(in, charset);
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -45,7 +46,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
|
||||
|
||||
@Test
|
||||
public void invalidMetadata() throws IOException {
|
||||
this.thrown.expect(JSONException.class);
|
||||
this.thrown.expectCause(CoreMatchers.<Throwable>instanceOf(JSONException.class));
|
||||
readFor("invalid");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user