Skip to content
Snippets Groups Projects
Commit ddad684f authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

avoid multiple instanciations of simple Gson object

parent c70efd94
No related branches found
No related tags found
......@@ -42,7 +42,9 @@ public class ExperimentApi {
private static final String EXAREME_ALGO_JSON_FILE="data/exareme_algorithms.json";
private static final Gson gson = new GsonBuilder()
private static final Gson gson = new Gson();
private static final Gson gsonOnlyExposed = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.excludeFieldsWithoutExposeAnnotation()
......@@ -74,9 +76,9 @@ public class ExperimentApi {
public ResponseEntity<String> runExperiment(@RequestBody ExperimentQuery incomingQueryObj) {
LOGGER.info("Run an experiment");
String incomingQueryString = new Gson().toJson(incomingQueryObj);
String incomingQueryString = gson.toJson(incomingQueryObj);
JsonObject incomingQuery = gson.fromJson(incomingQueryString, JsonObject.class);
JsonObject incomingQuery = gsonOnlyExposed.fromJson(incomingQueryString, JsonObject.class);
Experiment experiment = new Experiment();
experiment.setUuid(UUID.randomUUID());
......@@ -102,7 +104,7 @@ public class ExperimentApi {
}
} catch (MalformedURLException mue) { LOGGER.trace(mue.getMessage()); } // ignore
return new ResponseEntity<>(gson.toJson(experiment.jsonify()), HttpStatus.OK);
return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK);
}
@ApiOperation(value = "get an experiment", response = Experiment.class)
......@@ -126,7 +128,7 @@ public class ExperimentApi {
return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(gson.toJson(experiment.jsonify()), HttpStatus.OK);
return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK);
}
@ApiOperation(value = "Mark an experiment as viewed", response = Experiment.class)
......@@ -153,7 +155,7 @@ public class ExperimentApi {
LOGGER.info("Experiment updated (marked as viewed)");
return new ResponseEntity<>(gson.toJson(experiment.jsonify()), HttpStatus.OK);
return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK);
}
@ApiOperation(value = "Mark an experiment as shared", response = Experiment.class)
......@@ -218,7 +220,7 @@ public class ExperimentApi {
catalog.get("algorithms").getAsJsonArray().add(exaremeAlgo);
return new ResponseEntity<>(new Gson().toJson(catalog), HttpStatus.valueOf(code));
return new ResponseEntity<>(gson.toJson(catalog), HttpStatus.valueOf(code));
}
private ResponseEntity<String> doListExperiments(
......@@ -251,7 +253,7 @@ public class ExperimentApi {
}
}
return new ResponseEntity<>(gson.toJson(expList), HttpStatus.OK);
return new ResponseEntity<>(gsonOnlyExposed.toJson(expList), HttpStatus.OK);
}
private ResponseEntity<String> doMarkExperimentAsShared(String uuid, boolean shared) {
......@@ -276,7 +278,7 @@ public class ExperimentApi {
LOGGER.info("Experiment updated (marked as shared)");
return new ResponseEntity<>(gson.toJson(experiment.jsonify()), HttpStatus.OK);
return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK);
}
private void sendExperiment(Experiment experiment) throws MalformedURLException {
......
......@@ -31,6 +31,8 @@ public class GroupsApi {
private static final Logger LOGGER = Logger.getLogger(GroupsApi.class);
private static final Gson gson = new Gson();
private static String groups;
@Autowired
......@@ -45,7 +47,7 @@ public class GroupsApi {
loadGroups();
return ResponseEntity.ok(new Gson().fromJson(groups, Object.class));
return ResponseEntity.ok(gson.fromJson(groups, Object.class));
}
private void loadGroups() {
......@@ -56,10 +58,10 @@ public class GroupsApi {
data.next();
String json = ((PGobject) data.getObject("hierarchy")).getValue();
JsonObject root = new Gson().fromJson(json, JsonObject.class);
JsonObject root = gson.fromJson(json, JsonObject.class);
removeVariablesRecursive(root);
groups = new Gson().toJson(root);
groups = gson.toJson(root);
}
}
......
......@@ -36,6 +36,8 @@ public class RequestsApi {
private static final Logger LOGGER = Logger.getLogger(RequestsApi.class);
private static final Gson gson = new Gson();
@Autowired
@Qualifier("dataUtil")
private DataUtil dataUtil;
......@@ -59,7 +61,6 @@ public class RequestsApi {
List<String> covariables = new LinkedList<>();
List<String> filters = new LinkedList<>();
Gson gson = new Gson();
JsonObject q = gson.fromJson(gson.toJson(query, Query.class), JsonObject.class);
JsonArray queryVars = q.getAsJsonArray("variables") != null ? q.getAsJsonArray("variables") : new JsonArray();
......@@ -101,7 +102,7 @@ public class RequestsApi {
dataset.add("filter", gson.toJsonTree(filters));
dataset.add("data", dataUtil.getDataFromVariables(allVars));
return ResponseEntity.ok(new Gson().fromJson(dataset, Object.class));
return ResponseEntity.ok(gson.fromJson(dataset, Object.class));
}
......
......@@ -31,6 +31,8 @@ public class VariablesApi {
private static final Logger LOGGER = Logger.getLogger(VariablesApi.class);
private static final Gson gson = new Gson();
private static LinkedList<String> variables;
@Autowired
......@@ -56,7 +58,7 @@ public class VariablesApi {
for (String var : variables)
{
variablesObjects.add(new Gson().fromJson(var, Object.class));
variablesObjects.add(gson.fromJson(var, Object.class));
}
return ResponseEntity.ok(variablesObjects);
......@@ -73,10 +75,10 @@ public class VariablesApi {
for (String var : variables)
{
JsonObject varObj = new Gson().fromJson(var, JsonElement.class).getAsJsonObject();
JsonObject varObj = gson.fromJson(var, JsonElement.class).getAsJsonObject();
if (varObj.get("code").getAsString().equals(code))
{
return ResponseEntity.ok(new Gson().fromJson(varObj, Object.class));
return ResponseEntity.ok(gson.fromJson(varObj, Object.class));
}
}
......@@ -98,13 +100,13 @@ public class VariablesApi {
for (String var : variables)
{
JsonObject varObj = new Gson().fromJson(var, JsonElement.class).getAsJsonObject();
JsonObject varObj = gson.fromJson(var, JsonElement.class).getAsJsonObject();
if (varObj.get("code").getAsString().equals(code))
{
JsonArray values = varObj.get("enumerations").getAsJsonArray();
LinkedList<Object> valuesObjects = new LinkedList<>();
for (JsonElement value : values){
valuesObjects.add(new Gson().fromJson(value, Object.class));
valuesObjects.add(gson.fromJson(value, Object.class));
}
return ResponseEntity.ok(valuesObjects);
}
......@@ -126,7 +128,7 @@ public class VariablesApi {
data.next();
String json = ((PGobject) data.getObject("hierarchy")).getValue();
Object hierarchy = new Gson().fromJson(json, Object.class);
Object hierarchy = gson.fromJson(json, Object.class);
return ResponseEntity.ok(hierarchy);
}
......@@ -140,7 +142,7 @@ public class VariablesApi {
data.next();
String json = ((PGobject) data.getObject("hierarchy")).getValue();
JsonObject root = new Gson().fromJson(json, JsonObject.class);
JsonObject root = gson.fromJson(json, JsonObject.class);
variables = new LinkedList<>();
extractVariablesRecursive(root);
......@@ -160,7 +162,7 @@ public class VariablesApi {
grp.addProperty("label", element.getAsJsonPrimitive("label").getAsString());
var.getAsJsonObject().add("group", grp);
var.getAsJsonObject().addProperty("isVariable", true);
variables.add(new Gson().toJson(var));
variables.add(gson.toJson(var));
}
}
}
......
......@@ -20,7 +20,9 @@ public class Experiment {
private static final Logger LOGGER = Logger.getLogger(Experiment.class);
private static final Gson gson = new GsonBuilder()
private static final Gson gson = new Gson();
private static final Gson gsonOnlyExposed = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.excludeFieldsWithoutExposeAnnotation()
......@@ -85,13 +87,13 @@ public class Experiment {
public String computeQuery() {
JsonObject outgoingQuery = new JsonObject();
outgoingQuery.add("algorithms", gson.fromJson(algorithms, JsonArray.class));
outgoingQuery.add("validations", gson.fromJson(validations, JsonArray.class));
outgoingQuery.add("algorithms", gsonOnlyExposed.fromJson(algorithms, JsonArray.class));
outgoingQuery.add("validations", gsonOnlyExposed.fromJson(validations, JsonArray.class));
outgoingQuery.add("covariables", gson.toJsonTree(model.getQuery().getCovariables()));
outgoingQuery.add("variables", gson.toJsonTree(model.getQuery().getVariables()));
outgoingQuery.add("filters", gson.toJsonTree(model.getQuery().getFilters()));
outgoingQuery.add("grouping", gson.toJsonTree(model.getQuery().getGrouping()));
outgoingQuery.add("covariables", gsonOnlyExposed.toJsonTree(model.getQuery().getCovariables()));
outgoingQuery.add("variables", gsonOnlyExposed.toJsonTree(model.getQuery().getVariables()));
outgoingQuery.add("filters", gsonOnlyExposed.toJsonTree(model.getQuery().getFilters()));
outgoingQuery.add("grouping", gsonOnlyExposed.toJsonTree(model.getQuery().getGrouping()));
return outgoingQuery.toString();
}
......@@ -134,11 +136,11 @@ public class Experiment {
formatEl.setValue("True");
queryElements.add(formatEl);
return new Gson().toJson(queryElements);
return gson.toJson(queryElements);
}
public JsonObject jsonify() {
JsonObject exp = new Gson().toJsonTree(this).getAsJsonObject();
JsonObject exp = gson.toJsonTree(this).getAsJsonObject();
JsonParser parser = new JsonParser();
if (this.algorithms != null)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment