From ddad684f224844e87a22061c5ac32b1162a1df40 Mon Sep 17 00:00:00 2001 From: Mirco Nasuti <mirco.nasuti@chuv.ch> Date: Thu, 10 Nov 2016 09:41:29 +0100 Subject: [PATCH] avoid multiple instanciations of simple Gson object --- .../eu/hbp/mip/controllers/ExperimentApi.java | 20 ++++++++++--------- .../eu/hbp/mip/controllers/GroupsApi.java | 8 +++++--- .../eu/hbp/mip/controllers/RequestsApi.java | 5 +++-- .../eu/hbp/mip/controllers/VariablesApi.java | 18 +++++++++-------- .../java/eu/hbp/mip/model/Experiment.java | 20 ++++++++++--------- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java b/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java index 2def1e82b..ef0155c7d 100644 --- a/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java +++ b/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java @@ -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 { diff --git a/src/main/java/eu/hbp/mip/controllers/GroupsApi.java b/src/main/java/eu/hbp/mip/controllers/GroupsApi.java index e2e6721b4..80b3d6fdb 100644 --- a/src/main/java/eu/hbp/mip/controllers/GroupsApi.java +++ b/src/main/java/eu/hbp/mip/controllers/GroupsApi.java @@ -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); } } diff --git a/src/main/java/eu/hbp/mip/controllers/RequestsApi.java b/src/main/java/eu/hbp/mip/controllers/RequestsApi.java index 91175cd93..bbb864112 100644 --- a/src/main/java/eu/hbp/mip/controllers/RequestsApi.java +++ b/src/main/java/eu/hbp/mip/controllers/RequestsApi.java @@ -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)); } diff --git a/src/main/java/eu/hbp/mip/controllers/VariablesApi.java b/src/main/java/eu/hbp/mip/controllers/VariablesApi.java index ca3f455d6..065dcaf10 100644 --- a/src/main/java/eu/hbp/mip/controllers/VariablesApi.java +++ b/src/main/java/eu/hbp/mip/controllers/VariablesApi.java @@ -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)); } } } diff --git a/src/main/java/eu/hbp/mip/model/Experiment.java b/src/main/java/eu/hbp/mip/model/Experiment.java index 134fc2d18..f80937328 100644 --- a/src/main/java/eu/hbp/mip/model/Experiment.java +++ b/src/main/java/eu/hbp/mip/model/Experiment.java @@ -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) -- GitLab