diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-26 19:21:54 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-27 17:11:11 +0300 |
commit | 815899837ca07904fe9dc9223b4a4e8ac486cb59 (patch) | |
tree | b92834ad5dd92bdd9eb8504e8bd649e81e559d41 /sonar-ws-client/src/main/java/org/sonar/wsclient | |
parent | 40e0e78a54535e1a5009a626fb43dccad9230328 (diff) | |
download | sonarqube-815899837ca07904fe9dc9223b4a4e8ac486cb59.tar.gz sonarqube-815899837ca07904fe9dc9223b4a4e8ac486cb59.zip |
SONAR-2046: Reuse unmarshallers from sonar-ws-client for sonar-gwt-api
* WSUtils has two implementations - one for GWT and another for Java,
so can be used by unmarshallers in both cases. But this not very good,
because code is not type-safe. In fact it would be better to use
emulation of types from org.json.simple for GWT, but this solution is
more straightforward.
Diffstat (limited to 'sonar-ws-client/src/main/java/org/sonar/wsclient')
16 files changed, 341 insertions, 194 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java index 464fe78cb31..d1f21e42646 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java @@ -19,12 +19,17 @@ */ package org.sonar.wsclient; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; import org.sonar.wsclient.services.WSUtils; +import org.sonar.wsclient.unmarshallers.JsonUtils; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.Set; public final class JdkUtils extends WSUtils { @@ -42,4 +47,58 @@ public final class JdkUtils extends WSUtils { } } + @Override + public Object getField(Object json, String field) { + return ((JSONObject) json).get(field); + } + + @Override + public String getString(Object json, String field) { + return JsonUtils.getString((JSONObject) json, field); + } + + @Override + public Boolean getBoolean(Object json, String field) { + return JsonUtils.getBoolean((JSONObject) json, field); + } + + @Override + public Integer getInteger(Object json, String field) { + return JsonUtils.getInteger((JSONObject) json, field); + } + + @Override + public Double getDouble(Object json, String field) { + return JsonUtils.getDouble((JSONObject) json, field); + } + + @Override + public Long getLong(Object json, String field) { + return JsonUtils.getLong((JSONObject) json, field); + } + + @Override + public Date getDateTime(Object json, String field) { + return JsonUtils.getDateTime((JSONObject) json, field); + } + + @Override + public int getArraySize(Object array) { + return ((ArrayList) array).size(); + } + + @Override + public Object getArrayElement(Object array, int i) { + return ((ArrayList) array).get(i); + } + + @Override + public Object parse(String jsonStr) { + return JSONValue.parse(jsonStr); + } + + @Override + public Set<String> getFields(Object json) { + return ((JSONObject) json).keySet(); + } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java index ab4cd8d1028..8119bacee8c 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java @@ -20,7 +20,13 @@ package org.sonar.wsclient.services; import java.util.Date; +import java.util.Set; +/** + * Compatibility layer between GWT and plain Java. + * Well, this is bad, because code is not type-safe, so all unmarshallers also, + * but this allows to remove duplications between sonar-gwt-api and sonar-ws-client. + */ public abstract class WSUtils { private static WSUtils INSTANCE = null; @@ -36,4 +42,67 @@ public abstract class WSUtils { public abstract String format(Date date, String format); public abstract String encodeUrl(String url); + + /** + * @return value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract Object getField(Object json, String field); + + /** + * @return String value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract String getString(Object json, String field); + + /** + * @return Boolean value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract Boolean getBoolean(Object json, String field); + + /** + * @return Integer value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract Integer getInteger(Object json, String field); + + /** + * @return Double value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract Double getDouble(Object json, String field); + + /** + * @return Long value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract Long getLong(Object json, String field); + + /** + * @return Date value of specified field from specified JSON object, + * or <code>null</code> if field does not exist + */ + public abstract Date getDateTime(Object json, String field); + + /** + * @return size of specified JSON array + */ + public abstract int getArraySize(Object array); + + /** + * @return element from specified JSON array + */ + public abstract Object getArrayElement(Object array, int i); + + /** + * @return JSON object + */ + public abstract Object parse(String jsonStr); + + /** + * @return field names in specified JSON object + */ + public abstract Set<String> getFields(Object json); + } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java index d906f4ad9d8..13ddb93409d 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java @@ -19,10 +19,8 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.sonar.wsclient.services.Model; +import org.sonar.wsclient.services.WSUtils; import java.util.ArrayList; import java.util.List; @@ -30,10 +28,11 @@ import java.util.List; public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmarshaller<MODEL> { public final MODEL toModel(String json) { - MODEL result = null; - JSONArray array = (JSONArray) JSONValue.parse(json); - if (array.size() >= 1) { - JSONObject elt = (JSONObject) array.get(0); + WSUtils utils = WSUtils.getINSTANCE(); + MODEL result = null; + Object array = utils.parse(json); + if (utils.getArraySize(array) >= 1) { + Object elt = utils.getArrayElement(array, 0); if (elt != null) { result = parse(elt); } @@ -43,10 +42,11 @@ public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmar } public final List<MODEL> toModels(String json) { + WSUtils utils = WSUtils.getINSTANCE(); List<MODEL> result = new ArrayList<MODEL>(); - JSONArray array = (JSONArray) JSONValue.parse(json); - for (Object anArray : array) { - JSONObject elt = (JSONObject) anArray; + Object array = utils.parse(json); + for (int i = 0; i < utils.getArraySize(array); i++) { + Object elt = utils.getArrayElement(array, i); if (elt != null) { result.add(parse(elt)); } @@ -54,5 +54,5 @@ public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmar return result; } - protected abstract MODEL parse(JSONObject elt); + protected abstract MODEL parse(Object elt); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java index f326ba02176..f91b8af085d 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java @@ -19,34 +19,34 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.DependencyTree; +import org.sonar.wsclient.services.WSUtils; import java.util.ArrayList; import java.util.List; public class DependencyTreeUnmarshaller extends AbstractUnmarshaller<DependencyTree> { @Override - protected DependencyTree parse(JSONObject json) { + protected DependencyTree parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); DependencyTree tree = new DependencyTree() - .setDepId(JsonUtils.getString(json, "did")) - .setResourceId(JsonUtils.getString(json, "rid")) - .setResourceKey(JsonUtils.getString(json, "k")) - .setResourceName(JsonUtils.getString(json, "n")) - .setResourceScope(JsonUtils.getString(json, "s")) - .setResourceQualifier(JsonUtils.getString(json, "q")) - .setResourceVersion(JsonUtils.getString(json, "v")) - .setUsage(JsonUtils.getString(json, "u")) - .setWeight(JsonUtils.getInteger(json, "w")); + .setDepId(utils.getString(json, "did")) + .setResourceId(utils.getString(json, "rid")) + .setResourceKey(utils.getString(json, "k")) + .setResourceName(utils.getString(json, "n")) + .setResourceScope(utils.getString(json, "s")) + .setResourceQualifier(utils.getString(json, "q")) + .setResourceVersion(utils.getString(json, "v")) + .setUsage(utils.getString(json, "u")) + .setWeight(utils.getInteger(json, "w")); List<DependencyTree> to = new ArrayList<DependencyTree>(); tree.setTo(to); - JSONArray toJson = (JSONArray) json.get("to"); + Object toJson = utils.getField(json, "to"); if (toJson != null) { - for (Object aToJson : toJson) { - to.add(parse((JSONObject) aToJson)); + for (int i = 0; i < utils.getArraySize(toJson); i++) { + to.add(parse(utils.getArrayElement(toJson, i))); } } return tree; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java index 803753aede7..bf8b19db704 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java @@ -19,24 +19,25 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Dependency; +import org.sonar.wsclient.services.WSUtils; public class DependencyUnmarshaller extends AbstractUnmarshaller<Dependency> { @Override - protected Dependency parse(JSONObject json) { + protected Dependency parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); return new Dependency() - .setId(JsonUtils.getString(json, "id")) - .setFromId(JsonUtils.getLong(json, "fi")) - .setToId(JsonUtils.getLong(json, "ti")) - .setFromKey(JsonUtils.getString(json, "fk")) - .setToKey(JsonUtils.getString(json, "tk")) - .setUsage(JsonUtils.getString(json, "u")) - .setWeight(JsonUtils.getInteger(json, "w")) - .setFromName(JsonUtils.getString(json, "fn")) - .setFromQualifier(JsonUtils.getString(json, "fq")) - .setToName(JsonUtils.getString(json, "tn")) - .setToQualifier(JsonUtils.getString(json, "tq")); + .setId(utils.getString(json, "id")) + .setFromId(utils.getLong(json, "fi")) + .setToId(utils.getLong(json, "ti")) + .setFromKey(utils.getString(json, "fk")) + .setToKey(utils.getString(json, "tk")) + .setUsage(utils.getString(json, "u")) + .setWeight(utils.getInteger(json, "w")) + .setFromName(utils.getString(json, "fn")) + .setFromQualifier(utils.getString(json, "fq")) + .setToName(utils.getString(json, "tn")) + .setToQualifier(utils.getString(json, "tq")); } -}
\ No newline at end of file +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java index 2f7878e55ef..adff1c0f2db 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java @@ -19,20 +19,21 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Event; +import org.sonar.wsclient.services.WSUtils; public class EventUnmarshaller extends AbstractUnmarshaller<Event> { @Override - protected Event parse(JSONObject json) { + protected Event parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); return new Event() - .setId(JsonUtils.getString(json, "id")) - .setResourceKey(JsonUtils.getString(json, "rk")) - .setName(JsonUtils.getString(json, "n")) - .setCategory(JsonUtils.getString(json, "c")) - .setDate(JsonUtils.getDateTime(json, "dt")) - .setDescription(JsonUtils.getString(json, "ds")) - .setData(JsonUtils.getString(json, "data")); + .setId(utils.getString(json, "id")) + .setResourceKey(utils.getString(json, "rk")) + .setName(utils.getString(json, "n")) + .setCategory(utils.getString(json, "c")) + .setDate(utils.getDateTime(json, "dt")) + .setDescription(utils.getString(json, "ds")) + .setData(utils.getString(json, "data")); } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java index ab93ef28082..1d40609208e 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java @@ -19,19 +19,20 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Favourite; +import org.sonar.wsclient.services.WSUtils; public class FavouriteUnmarshaller extends AbstractUnmarshaller<Favourite> { @Override - protected Favourite parse(JSONObject json) { + protected Favourite parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); return new Favourite() - .setId(JsonUtils.getInteger(json, "id")) - .setKey(JsonUtils.getString(json, "key")) - .setName(JsonUtils.getString(json, "name")) - .setScope(JsonUtils.getString(json, "scope")) - .setQualifier(JsonUtils.getString(json, "qualifier")) - .setLanguage(JsonUtils.getString(json, "lang")); + .setId(utils.getInteger(json, "id")) + .setKey(utils.getString(json, "key")) + .setName(utils.getString(json, "name")) + .setScope(utils.getString(json, "scope")) + .setQualifier(utils.getString(json, "qualifier")) + .setLanguage(utils.getString(json, "lang")); } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java index e63bcd8c08b..123e94583c1 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java @@ -19,21 +19,22 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Metric; +import org.sonar.wsclient.services.WSUtils; public class MetricUnmarshaller extends AbstractUnmarshaller<Metric> { @Override - protected Metric parse(JSONObject json) { + protected Metric parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); return new Metric() - .setKey(JsonUtils.getString(json, "key")) - .setName(JsonUtils.getString(json, "name")) - .setDomain(JsonUtils.getString(json, "domain")) - .setDescription(JsonUtils.getString(json, "description")) - .setDirection(JsonUtils.getInteger(json, "direction")) - .setType(JsonUtils.getString(json, "val_type")) - .setUserManaged(JsonUtils.getBoolean(json, "user_managed")) - .setHidden(JsonUtils.getBoolean(json, "hidden")); + .setKey(utils.getString(json, "key")) + .setName(utils.getString(json, "name")) + .setDomain(utils.getString(json, "domain")) + .setDescription(utils.getString(json, "description")) + .setDirection(utils.getInteger(json, "direction")) + .setType(utils.getString(json, "val_type")) + .setUserManaged(utils.getBoolean(json, "user_managed")) + .setHidden(utils.getBoolean(json, "hidden")); } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java index 29d785f485e..dbcfafa6faa 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java @@ -1,7 +1,7 @@ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Plugin; +import org.sonar.wsclient.services.WSUtils; /** * @since 2.4 @@ -9,11 +9,12 @@ import org.sonar.wsclient.services.Plugin; public class PluginUnmarshaller extends AbstractUnmarshaller<Plugin> { @Override - protected Plugin parse(JSONObject json) { + protected Plugin parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); return new Plugin() - .setKey(JsonUtils.getString(json, "key")) - .setName(JsonUtils.getString(json, "name")) - .setVersion(JsonUtils.getString(json, "version")); + .setKey(utils.getString(json, "key")) + .setName(utils.getString(json, "name")) + .setVersion(utils.getString(json, "version")); } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java index 9df96d1433d..5efea1ca841 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java @@ -19,15 +19,16 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Property; +import org.sonar.wsclient.services.WSUtils; public class PropertyUnmarshaller extends AbstractUnmarshaller<Property> { @Override - protected Property parse(JSONObject json) { + protected Property parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); return new Property() - .setKey(JsonUtils.getString(json, "key")) - .setValue(JsonUtils.getString(json, "value")); + .setKey(utils.getString(json, "key")) + .setValue(utils.getString(json, "value")); } -}
\ No newline at end of file +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java index fba424d4bbe..bdb9db20e17 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java @@ -19,10 +19,9 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Measure; import org.sonar.wsclient.services.Resource; +import org.sonar.wsclient.services.WSUtils; import java.util.ArrayList; import java.util.List; @@ -30,53 +29,56 @@ import java.util.List; public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> { @Override - protected Resource parse(JSONObject json) { + protected Resource parse(Object json) { Resource resource = new Resource(); parseResourceFields(json, resource); parseMeasures(json, resource); return resource; } - private void parseResourceFields(JSONObject json, Resource resource) { - resource.setId(JsonUtils.getInteger(json, "id")) - .setKey(JsonUtils.getString(json, "key")) - .setName(JsonUtils.getString(json, "name")) - .setLongName(JsonUtils.getString(json, "lname")) - .setScope(JsonUtils.getString(json, "scope")) - .setQualifier(JsonUtils.getString(json, "qualifier")) - .setLanguage(JsonUtils.getString(json, "lang")) - .setDescription(JsonUtils.getString(json, "description")) - .setDate(JsonUtils.getDateTime(json, "date")) - .setVersion(JsonUtils.getString(json, "version")) - .setPeriod1Mode(JsonUtils.getString(json, "p1")) - .setPeriod1Param(JsonUtils.getString(json, "p1p")) - .setPeriod1Date(JsonUtils.getDateTime(json, "p1d")) - .setPeriod2Mode(JsonUtils.getString(json, "p2")) - .setPeriod2Param(JsonUtils.getString(json, "p2p")) - .setPeriod2Date(JsonUtils.getDateTime(json, "p2d")) - .setPeriod3Mode(JsonUtils.getString(json, "p3")) - .setPeriod3Param(JsonUtils.getString(json, "p3p")) - .setPeriod3Date(JsonUtils.getDateTime(json, "p3d")) - .setPeriod4Mode(JsonUtils.getString(json, "p4")) - .setPeriod4Param(JsonUtils.getString(json, "p4p")) - .setPeriod4Date(JsonUtils.getDateTime(json, "p4d")) - .setPeriod5Mode(JsonUtils.getString(json, "p5")) - .setPeriod5Param(JsonUtils.getString(json, "p5p")) - .setPeriod5Date(JsonUtils.getDateTime(json, "p5d")); + private void parseResourceFields(Object json, Resource resource) { + WSUtils utils = WSUtils.getINSTANCE(); + resource.setId(utils.getInteger(json, "id")) + .setKey(utils.getString(json, "key")) + .setName(utils.getString(json, "name")) + .setLongName(utils.getString(json, "lname")) + .setScope(utils.getString(json, "scope")) + .setQualifier(utils.getString(json, "qualifier")) + .setLanguage(utils.getString(json, "lang")) + .setDescription(utils.getString(json, "description")) + .setDate(utils.getDateTime(json, "date")) + .setVersion(utils.getString(json, "version")) + .setPeriod1Mode(utils.getString(json, "p1")) + .setPeriod1Param(utils.getString(json, "p1p")) + .setPeriod1Date(utils.getDateTime(json, "p1d")) + .setPeriod2Mode(utils.getString(json, "p2")) + .setPeriod2Param(utils.getString(json, "p2p")) + .setPeriod2Date(utils.getDateTime(json, "p2d")) + .setPeriod3Mode(utils.getString(json, "p3")) + .setPeriod3Param(utils.getString(json, "p3p")) + .setPeriod3Date(utils.getDateTime(json, "p3d")) + .setPeriod4Mode(utils.getString(json, "p4")) + .setPeriod4Param(utils.getString(json, "p4p")) + .setPeriod4Date(utils.getDateTime(json, "p4d")) + .setPeriod5Mode(utils.getString(json, "p5")) + .setPeriod5Param(utils.getString(json, "p5p")) + .setPeriod5Date(utils.getDateTime(json, "p5d")); } - private void parseMeasures(JSONObject json, Resource resource) { - JSONArray measuresJson = JsonUtils.getArray(json, "msr"); + private void parseMeasures(Object json, Resource resource) { + WSUtils utils = WSUtils.getINSTANCE(); + Object measuresJson = utils.getField(json, "msr"); if (measuresJson != null) { resource.setMeasures(parseMeasures(measuresJson)); } } - private List<Measure> parseMeasures(JSONArray measuresJson) { + private List<Measure> parseMeasures(Object measuresJson) { + WSUtils utils = WSUtils.getINSTANCE(); List<Measure> projectMeasures = new ArrayList<Measure>(); - int len = measuresJson.size(); + int len = utils.getArraySize(measuresJson); for (int i = 0; i < len; i++) { - JSONObject measureJson = (JSONObject) measuresJson.get(i); + Object measureJson = utils.getArrayElement(measuresJson, i); if (measureJson != null) { Measure measure = parseMeasure(measureJson); projectMeasures.add(measure); @@ -85,27 +87,29 @@ public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> { return projectMeasures; } - private Measure parseMeasure(JSONObject json) { + private Measure parseMeasure(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); + Measure measure = new Measure(); measure - .setMetricKey(JsonUtils.getString(json, "key")) - .setMetricName(JsonUtils.getString(json, "name")) - .setValue(JsonUtils.getDouble(json, "val")) - .setFormattedValue(JsonUtils.getString(json, "frmt_val")) - .setTrend(JsonUtils.getInteger(json, "trend")) - .setVar(JsonUtils.getInteger(json, "var")) - .setData(JsonUtils.getString(json, "data")) - .setRuleKey(JsonUtils.getString(json, "rule_key")) - .setRuleName(JsonUtils.getString(json, "rule_name")) - .setRuleCategory(JsonUtils.getString(json, "rule_category")) - .setRuleSeverity(JsonUtils.getString(json, "rule_priority")) - .setCharacteristicKey(JsonUtils.getString(json, "ctic_key")) - .setCharacteristicName(JsonUtils.getString(json, "ctic_name")) - .setVariation1(JsonUtils.getDouble(json, "var1")) - .setVariation2(JsonUtils.getDouble(json, "var2")) - .setVariation3(JsonUtils.getDouble(json, "var3")) - .setVariation4(JsonUtils.getDouble(json, "var4")) - .setVariation5(JsonUtils.getDouble(json, "var5")); + .setMetricKey(utils.getString(json, "key")) + .setMetricName(utils.getString(json, "name")) + .setValue(utils.getDouble(json, "val")) + .setFormattedValue(utils.getString(json, "frmt_val")) + .setTrend(utils.getInteger(json, "trend")) + .setVar(utils.getInteger(json, "var")) + .setData(utils.getString(json, "data")) + .setRuleKey(utils.getString(json, "rule_key")) + .setRuleName(utils.getString(json, "rule_name")) + .setRuleCategory(utils.getString(json, "rule_category")) + .setRuleSeverity(utils.getString(json, "rule_priority")) + .setCharacteristicKey(utils.getString(json, "ctic_key")) + .setCharacteristicName(utils.getString(json, "ctic_name")) + .setVariation1(utils.getDouble(json, "var1")) + .setVariation2(utils.getDouble(json, "var2")) + .setVariation3(utils.getDouble(json, "var3")) + .setVariation4(utils.getDouble(json, "var4")) + .setVariation5(utils.getDouble(json, "var5")); return measure; } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java index 041d3afd573..9cb3b12f56e 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java @@ -19,10 +19,9 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Rule; import org.sonar.wsclient.services.RuleParam; +import org.sonar.wsclient.services.WSUtils; import java.util.ArrayList; import java.util.List; @@ -33,34 +32,40 @@ import java.util.List; public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> { @Override - protected Rule parse(JSONObject json) { + protected Rule parse(Object json) { Rule rule = new Rule(); parseRuleFields(json, rule); parseParams(json, rule); return rule; } - private void parseRuleFields(JSONObject json, Rule rule) { - rule.setTitle(JsonUtils.getString(json, "title")) - .setKey(JsonUtils.getString(json, "key")) - .setRepository(JsonUtils.getString(json, "plugin")) - .setDescription(JsonUtils.getString(json, "description")) - .setSeverity(JsonUtils.getString(json, "priority")) - .setActive("ACTIVE".equals(JsonUtils.getString(json, "status"))); + private void parseRuleFields(Object json, Rule rule) { + WSUtils utils = WSUtils.getINSTANCE(); + + rule.setTitle(utils.getString(json, "title")) + .setKey(utils.getString(json, "key")) + .setRepository(utils.getString(json, "plugin")) + .setDescription(utils.getString(json, "description")) + .setSeverity(utils.getString(json, "priority")) + .setActive("ACTIVE".equals(utils.getString(json, "status"))); } - private void parseParams(JSONObject json, Rule rule) { - JSONArray paramsJson = (JSONArray) json.get("params"); + private void parseParams(Object json, Rule rule) { + WSUtils utils = WSUtils.getINSTANCE(); + + Object paramsJson = utils.getField(json, "params"); if (paramsJson != null) { rule.setParams(parseParams(paramsJson)); } } - private List<RuleParam> parseParams(JSONArray paramsJson) { + private List<RuleParam> parseParams(Object paramsJson) { + WSUtils utils = WSUtils.getINSTANCE(); + List<RuleParam> ruleParams = new ArrayList<RuleParam>(); - int len = paramsJson.size(); + int len = utils.getArraySize(paramsJson); for (int i = 0; i < len; i++) { - JSONObject paramJson = (JSONObject) paramsJson.get(i); + Object paramJson = utils.getArrayElement(paramsJson, i); if (paramJson != null) { RuleParam param = parseParam(paramJson); ruleParams.add(param); @@ -69,11 +74,13 @@ public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> { return ruleParams; } - private RuleParam parseParam(JSONObject json) { + private RuleParam parseParam(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); + RuleParam param = new RuleParam(); - param.setName(JsonUtils.getString(json, "name")) - .setDescription(JsonUtils.getString(json, "description")) - .setValue(JsonUtils.getString(json, "value")); + param.setName(utils.getString(json, "name")) + .setDescription(utils.getString(json, "description")) + .setValue(utils.getString(json, "value")); return param; } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java index a17a36189d4..829b174dc47 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java @@ -19,9 +19,8 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.sonar.wsclient.services.Server; +import org.sonar.wsclient.services.WSUtils; import java.util.List; @@ -30,12 +29,13 @@ import java.util.List; */ public class ServerUnmarshaller implements Unmarshaller<Server> { public Server toModel(String json) { - JSONObject map = (JSONObject) JSONValue.parse(json); + WSUtils utils = WSUtils.getINSTANCE(); + Object map = utils.parse(json); Server server = new Server() - .setId(JsonUtils.getString(map, "id")) - .setVersion(JsonUtils.getString(map, "version")) - .setStatusMessage(JsonUtils.getString(map, "status_msg")); - String status = JsonUtils.getString(map, "status"); + .setId(utils.getString(map, "id")) + .setVersion(utils.getString(map, "version")) + .setStatusMessage(utils.getString(map, "status_msg")); + String status = utils.getString(map, "status"); if (status != null) { server.setStatus(Server.Status.valueOf(status)); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java index cbda6b4c5d0..16eb4aff98d 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java @@ -19,20 +19,18 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Source; - -import java.util.Map; +import org.sonar.wsclient.services.WSUtils; public class SourceUnmarshaller extends AbstractUnmarshaller<Source> { @Override - protected Source parse(JSONObject json) { + protected Source parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); Source source = new Source(); - for (Object o : json.entrySet()) { - Map.Entry<String, String> entry = (Map.Entry<String, String>) o; - source.addLine(Integer.parseInt(entry.getKey()), entry.getValue()); + for (String field : utils.getFields(json)) { + source.addLine(Integer.parseInt(field), utils.getString(json, field)); } return source; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java index b59a364bc31..08a0ac26d87 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java @@ -19,43 +19,45 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.TimeMachine; import org.sonar.wsclient.services.TimeMachineCell; import org.sonar.wsclient.services.TimeMachineColumn; +import org.sonar.wsclient.services.WSUtils; public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> { - protected TimeMachine parse(JSONObject json) { - JSONArray cols = JsonUtils.getArray(json, "cols"); - JSONArray cells = JsonUtils.getArray(json, "cells"); + protected TimeMachine parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); + Object cols = utils.getField(json, "cols"); + Object cells = utils.getField(json, "cells"); return new TimeMachine(toColumns(cols), toCells(cells)); } - private TimeMachineColumn[] toColumns(JSONArray cols) { - int size = cols.size(); + private TimeMachineColumn[] toColumns(Object cols) { + WSUtils utils = WSUtils.getINSTANCE(); + int size = utils.getArraySize(cols); TimeMachineColumn[] result = new TimeMachineColumn[size]; for (int index = 0; index < size; index++) { - JSONObject colJson = (JSONObject) cols.get(index); - result[index] = new TimeMachineColumn(index, JsonUtils.getString(colJson, "metric"), null, null); + Object colJson = utils.getArrayElement(cols, index); + result[index] = new TimeMachineColumn(index, utils.getString(colJson, "metric"), null, null); } return result; } - private TimeMachineCell[] toCells(JSONArray cells) { - int size = cells.size(); + private TimeMachineCell[] toCells(Object cells) { + WSUtils utils = WSUtils.getINSTANCE(); + int size = utils.getArraySize(cells); TimeMachineCell[] result = new TimeMachineCell[size]; for (int i = 0; i < size; i++) { - JSONObject cellJson = (JSONObject) cells.get(i); - JSONArray valuesJson = JsonUtils.getArray(cellJson, "v"); + Object cellJson = utils.getArrayElement(cells, i); + Object valuesJson = utils.getField(cellJson, "v"); - Object[] resultValues = new Object[valuesJson.size()]; - for (int indexValue = 0; indexValue < valuesJson.size(); indexValue++) { - Object value = valuesJson.get(indexValue); + Object[] resultValues = new Object[utils.getArraySize(valuesJson)]; + for (int indexValue = 0; indexValue < utils.getArraySize(valuesJson); indexValue++) { + Object value = utils.getArrayElement(valuesJson, indexValue); resultValues[indexValue] = value; } - result[i] = new TimeMachineCell(JsonUtils.getDateTime(cellJson, "d"), resultValues); + result[i] = new TimeMachineCell(utils.getDateTime(cellJson, "d"), resultValues); } return result; } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java index 258e5938570..18c297b22cb 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java @@ -19,31 +19,33 @@ */ package org.sonar.wsclient.unmarshallers; -import org.json.simple.JSONObject; import org.sonar.wsclient.services.Violation; +import org.sonar.wsclient.services.WSUtils; public class ViolationUnmarshaller extends AbstractUnmarshaller<Violation> { @Override - protected Violation parse(JSONObject json) { + protected Violation parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); + Violation violation = new Violation(); - violation.setMessage(JsonUtils.getString(json, "message")); - violation.setLine(JsonUtils.getInteger(json, "line")); - violation.setSeverity(JsonUtils.getString(json, "priority")); - violation.setCreatedAt(JsonUtils.getDateTime(json, "createdAt")); + violation.setMessage(utils.getString(json, "message")); + violation.setLine(utils.getInteger(json, "line")); + violation.setSeverity(utils.getString(json, "priority")); + violation.setCreatedAt(utils.getDateTime(json, "createdAt")); - JSONObject rule = (JSONObject) json.get("rule"); + Object rule = utils.getField(json, "rule"); if (rule != null) { - violation.setRuleKey(JsonUtils.getString(rule, "key")); - violation.setRuleName(JsonUtils.getString(rule, "name")); + violation.setRuleKey(utils.getString(rule, "key")); + violation.setRuleName(utils.getString(rule, "name")); } - JSONObject resource = (JSONObject) json.get("resource"); + Object resource = utils.getField(json, "resource"); if (resource != null) { - violation.setResourceKey(JsonUtils.getString(resource, "key")); - violation.setResourceName(JsonUtils.getString(resource, "name")); - violation.setResourceQualifier(JsonUtils.getString(resource, "qualifier")); - violation.setResourceScope(JsonUtils.getString(resource, "scope")); + violation.setResourceKey(utils.getString(resource, "key")); + violation.setResourceName(utils.getString(resource, "name")); + violation.setResourceQualifier(utils.getString(resource, "qualifier")); + violation.setResourceScope(utils.getString(resource, "scope")); } return violation; } |