Skip to content

Commit 4c8338a

Browse files
author
eznedan
committed
UPDATE: json import
1 parent 0986392 commit 4c8338a

File tree

5 files changed

+35
-45
lines changed

5 files changed

+35
-45
lines changed

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@
6161
<scope>provided</scope>
6262
</dependency>
6363
<dependency>
64-
<groupId>org.json</groupId>
65-
<artifactId>json</artifactId>
66-
<version>20180130</version>
64+
<groupId>com.google.code.gson</groupId>
65+
<artifactId>gson</artifactId>
66+
<version>2.8.4</version>
6767
</dependency>
68+
6869
</dependencies>
6970
<build>
7071
<plugins>

src/main/java/com/ericsson/ei/frontend/BackEndInformationControllerImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import com.ericsson.ei.frontend.model.BackEndInformation;
2020
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
21-
import org.json.JSONArray;
22-
import org.json.JSONObject;
21+
import com.google.gson.JsonObject;
22+
import com.google.gson.JsonParser;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.http.HttpStatus;
2525
import org.springframework.http.ResponseEntity;
@@ -45,7 +45,7 @@ public ResponseEntity<String> getInstances(Model model) {
4545
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
4646
try {
4747
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
48-
utils.setInstances(new JSONArray(body));
48+
utils.setInstances(new JsonParser().parse(body).getAsJsonArray());
4949
utils.writeIntoFile();
5050
utils.parseBackEndInstancesFile();
5151
for (BackEndInformation backEndInformation : utils.getInformation()) {
@@ -62,7 +62,7 @@ public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequ
6262
public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request) {
6363
try {
6464
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
65-
utils.setInstances(new JSONArray(body));
65+
utils.setInstances(new JsonParser().parse(body).getAsJsonArray());
6666
utils.writeIntoFile();
6767
return new ResponseEntity<>(HttpStatus.OK);
6868
} catch (Exception e) {
@@ -73,10 +73,10 @@ public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequ
7373
public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) {
7474
try {
7575
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
76-
JSONObject instance = new JSONObject(body);
76+
JsonObject instance = new JsonParser().parse(body).getAsJsonObject();
7777
if (!utils.checkIfInstanceAlreadyExist(instance)) {
78-
instance.put("checked", false);
79-
utils.getInstances().put(instance);
78+
instance.addProperty("checked", false);
79+
utils.getInstances().add(instance);
8080
utils.writeIntoFile();
8181
return new ResponseEntity<>(HttpStatus.OK);
8282
} else {

src/main/java/com/ericsson/ei/frontend/WebController.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,11 @@
1717
package com.ericsson.ei.frontend;
1818

1919
import com.ericsson.ei.frontend.model.BackEndInformation;
20-
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
21-
import org.json.JSONArray;
22-
import org.json.JSONObject;
23-
import org.slf4j.Logger;
24-
import org.slf4j.LoggerFactory;
2520
import org.springframework.beans.factory.annotation.Autowired;
2621
import org.springframework.beans.factory.annotation.Value;
27-
import org.springframework.http.HttpStatus;
28-
import org.springframework.http.ResponseEntity;
2922
import org.springframework.stereotype.Controller;
3023
import org.springframework.ui.Model;
3124
import org.springframework.web.bind.annotation.RequestMapping;
32-
import org.springframework.web.bind.annotation.RequestMethod;
33-
34-
import javax.servlet.http.HttpServletRequest;
35-
import java.util.stream.Collectors;
3625

3726
@Controller
3827
public class WebController {

src/main/java/com/ericsson/ei/frontend/utils/BackEndInstancesUtils.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import com.ericsson.ei.frontend.model.BackEndInformation;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.google.gson.JsonArray;
22+
import com.google.gson.JsonElement;
23+
import com.google.gson.JsonObject;
24+
import com.google.gson.JsonParser;
2125
import lombok.Getter;
2226
import lombok.Setter;
23-
import org.json.JSONArray;
24-
import org.json.JSONObject;
2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
2729
import org.springframework.beans.factory.annotation.Autowired;
@@ -63,28 +65,28 @@ public class BackEndInstancesUtils {
6365
private BackEndInformation backEndInformation;
6466

6567
private List<BackEndInformation> information = new ArrayList<>();
66-
private JSONArray instances = new JSONArray();
68+
private JsonArray instances = new JsonArray();
6769

6870
@PostConstruct
6971
public void init() {
7072
parseBackEndInstancesFile();
7173
if (!checkIfInstanceAlreadyExist(getCurrentInstance())) {
72-
instances.put(getCurrentInstance());
74+
instances.add(getCurrentInstance());
7375
}
7476
if (eiInstancesPath.equals("")) {
7577
setEiInstancesPath(PATH);
7678
}
7779
writeIntoFile();
7880
}
7981

80-
private JSONObject getCurrentInstance() {
81-
JSONObject instance = new JSONObject();
82-
instance.put("name", "core");
83-
instance.put("host", host);
84-
instance.put("port", port);
85-
instance.put("path", path);
86-
instance.put("https", https);
87-
instance.put("active", true);
82+
private JsonObject getCurrentInstance() {
83+
JsonObject instance = new JsonObject();
84+
instance.addProperty("name", "core");
85+
instance.addProperty("host", host);
86+
instance.addProperty("port", port);
87+
instance.addProperty("path", path);
88+
instance.addProperty("https", https);
89+
instance.addProperty("active", true);
8890
return instance;
8991
}
9092

@@ -96,11 +98,11 @@ public void setBackEndProperties(BackEndInformation properties) {
9698
backEndInformation.setHttps(properties.isHttps());
9799
}
98100

99-
public boolean checkIfInstanceAlreadyExist(JSONObject instance) {
100-
for (int i = 0; i < instances.length(); i++) {
101-
if (instances.getJSONObject(i).get("host").equals(instance.get("host")) &&
102-
instances.getJSONObject(i).get("port").equals(instance.get("port")) &&
103-
instances.getJSONObject(i).get("path").equals(instance.get("path"))) {
101+
public boolean checkIfInstanceAlreadyExist(JsonObject instance) {
102+
for (JsonElement element : instances) {
103+
if (element.getAsJsonObject().get("host").equals(instance.get("host")) &&
104+
element.getAsJsonObject().get("port").equals(instance.get("port")) &&
105+
element.getAsJsonObject().get("path").equals(instance.get("path"))) {
104106
return true;
105107
}
106108
}
@@ -123,12 +125,11 @@ public void parseBackEndInstancesFile() {
123125
if (eiInstancesPath != null) {
124126
try {
125127
information.clear();
126-
instances = new JSONArray();
127-
JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath))));
128-
for (Object o : inputBackEndInstances) {
129-
JSONObject instance = (JSONObject) o;
130-
information.add(new ObjectMapper().readValue(instance.toString(), BackEndInformation.class));
131-
instances.put(instance);
128+
instances = new JsonArray();
129+
JsonArray inputBackEndInstances = new JsonParser().parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
130+
for (JsonElement element : inputBackEndInstances) {
131+
information.add(new ObjectMapper().readValue(element.toString(), BackEndInformation.class));
132+
instances.add(element);
132133
}
133134
} catch (IOException e) {
134135
LOG.error("Failure when try to parse json file" + e.getMessage());

src/main/resources/static/js/switch-instances.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function multipleInstancesModel(data) {
3636
self.submit = function(instances) {
3737
var count = 0;
3838
var json = JSON.parse(ko.toJSON(instances));
39-
console.log(json);
4039
for(var i = 0; i < json.length; i++){
4140
var obj = json[i];
4241
if(obj.active == true){

0 commit comments

Comments
 (0)