#90 Gson implementation. #106
@@ -24,35 +24,53 @@ public class JsonController implements RestController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting")
 | 
					    @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting")
 | 
				
			||||||
    public void formatting(Request request, Response response) {
 | 
					    public void formatting(Request request, Response response) {
 | 
				
			||||||
 | 
					        long startProcess = System.currentTimeMillis();
 | 
				
			||||||
 | 
					        JsonObject responseJson = new JsonObject();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            JsonObject jsonObject = this.gson.fromJson(request.body(), JsonObject.class);
 | 
					            JsonObject requestJson = this.gson.fromJson(request.body(), JsonObject.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            response.status(200);
 | 
					            response.status(200);
 | 
				
			||||||
            response.body(this.prettyGson.toJson(jsonObject));
 | 
					
 | 
				
			||||||
 | 
					            responseJson.addProperty("data", this.prettyGson.toJson(requestJson));
 | 
				
			||||||
 | 
					            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            response.body(this.prettyGson.toJson(responseJson));
 | 
				
			||||||
        } catch (Exception e) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
            response.status(500);
 | 
					 | 
				
			||||||
            Throwable cause = e.getCause();
 | 
					            Throwable cause = e.getCause();
 | 
				
			||||||
            if (cause == null) {
 | 
					
 | 
				
			||||||
                response.body(e.getMessage());
 | 
					            response.status(500);
 | 
				
			||||||
            } else {
 | 
					
 | 
				
			||||||
                response.body(cause.getMessage());
 | 
					            responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage());
 | 
				
			||||||
            }
 | 
					            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            response.body(this.prettyGson.toJson(responseJson));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize")
 | 
					    @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize")
 | 
				
			||||||
    public void minimize(Request request, Response response) {
 | 
					    public void minimize(Request request, Response response) {
 | 
				
			||||||
 | 
					        long startProcess = System.currentTimeMillis();
 | 
				
			||||||
 | 
					        JsonObject responseJson = new JsonObject();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            JsonObject jsonObject = this.prettyGson.fromJson(request.body(), JsonObject.class);
 | 
					            JsonObject requestJson = this.prettyGson.fromJson(request.body(), JsonObject.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            response.status(200);
 | 
					            response.status(200);
 | 
				
			||||||
            response.body(this.gson.toJson(jsonObject));
 | 
					
 | 
				
			||||||
 | 
					            responseJson.addProperty("data", this.gson.toJson(requestJson));
 | 
				
			||||||
 | 
					            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            response.body(this.gson.toJson(responseJson));
 | 
				
			||||||
        } catch (Exception e) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
            response.status(500);
 | 
					 | 
				
			||||||
            Throwable cause = e.getCause();
 | 
					            Throwable cause = e.getCause();
 | 
				
			||||||
            if (cause == null) {
 | 
					
 | 
				
			||||||
                response.body(e.getMessage());
 | 
					            response.status(500);
 | 
				
			||||||
            } else {
 | 
					
 | 
				
			||||||
                response.body(cause.getMessage());
 | 
					            responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage());
 | 
				
			||||||
            }
 | 
					            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            response.body(this.prettyGson.toJson(responseJson));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,6 @@
 | 
				
			|||||||
function formatAndValidateJson(errorElement) {
 | 
					function formatAndValidateJson(errorElement) {
 | 
				
			||||||
  const input = document.querySelector('#jsonBlock');
 | 
					  const input = document.querySelector('#jsonBlock');
 | 
				
			||||||
  const processInfo = document.getElementById(errorElement);
 | 
					  const processInfo = document.getElementById(errorElement);
 | 
				
			||||||
  const start = new Date();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/formatting"
 | 
					  const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/formatting"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -10,22 +9,22 @@ function formatAndValidateJson(errorElement) {
 | 
				
			|||||||
    body: input.textContent
 | 
					    body: input.textContent
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  .then(async (response) => {
 | 
					  .then(async (response) => {
 | 
				
			||||||
 | 
					    const promise = response.json();
 | 
				
			||||||
    if (!response.ok) {
 | 
					    if (!response.ok) {
 | 
				
			||||||
      throw Error(await response.text());
 | 
					      throw Error(await promise);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return response.text();
 | 
					    return promise;
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  .then((data) => {
 | 
					  .then((data) => {
 | 
				
			||||||
    input.innerText = data;
 | 
					    input.innerText = data.data;
 | 
				
			||||||
    processInfo.innerText = "";
 | 
					    processInfo.innerText = "";
 | 
				
			||||||
    hljs.highlightElement(input);
 | 
					    hljs.highlightElement(input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const end = new Date();
 | 
					    processInfo.innerHTML = "<b style='color: green'>Computed in </b> <span style='color: green'>" + data.time + "ms</span>";
 | 
				
			||||||
    processInfo.innerHTML = "<b style='color: green'>Computed in </b> <span style='color: green'>" + (end.getMilliseconds() - start.getMilliseconds()) + "ms</span>";
 | 
					 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  .catch((error) => {
 | 
					  .catch((error) => {
 | 
				
			||||||
    processInfo.innerHTML = "<b style='color: red'>" + error + "</b>";
 | 
					    processInfo.innerHTML = "<b style='color: red'>" + error.data + "</b>";
 | 
				
			||||||
    console.error('Error:', error);
 | 
					    console.error('Error:', error);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -34,7 +33,6 @@ function minimizeJson(errorElement) {
 | 
				
			|||||||
  const input = document.querySelector('#jsonBlock');
 | 
					  const input = document.querySelector('#jsonBlock');
 | 
				
			||||||
  const processInfo = document.getElementById(errorElement);
 | 
					  const processInfo = document.getElementById(errorElement);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const start = new Date();
 | 
					 | 
				
			||||||
  const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/minimize"
 | 
					  const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/minimize"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  fetch(address, {
 | 
					  fetch(address, {
 | 
				
			||||||
@@ -42,22 +40,22 @@ function minimizeJson(errorElement) {
 | 
				
			|||||||
    body: input.textContent
 | 
					    body: input.textContent
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  .then(async (response) => {
 | 
					  .then(async (response) => {
 | 
				
			||||||
 | 
					    const promise = response.json();
 | 
				
			||||||
    if (!response.ok) {
 | 
					    if (!response.ok) {
 | 
				
			||||||
      throw Error(await response.text());
 | 
					      throw Error(await promise);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return response.text();
 | 
					    return promise;
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  .then((data) => {
 | 
					  .then((data) => {
 | 
				
			||||||
    input.innerText = data;
 | 
					    input.innerText = data.data;
 | 
				
			||||||
    processInfo.innerText = "";
 | 
					    processInfo.innerText = "";
 | 
				
			||||||
    hljs.highlightElement(input);
 | 
					    hljs.highlightElement(input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const end = new Date();
 | 
					    processInfo.innerHTML = "<b style='color: green'>Computed in </b> <span style='color: green'>" + data.time + "ms</span>";
 | 
				
			||||||
    processInfo.innerHTML = "<b style='color: green'>Computed in </b> <span style='color: green'>" + (end.getMilliseconds() - start.getMilliseconds()) + "ms</span>";
 | 
					 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  .catch((error) => {
 | 
					  .catch((error) => {
 | 
				
			||||||
    processInfo.innerHTML = "<b style='color: red'>" + error + "</b>";
 | 
					    processInfo.innerHTML = "<b style='color: red'>" + error.data + "</b>";
 | 
				
			||||||
    console.error('Error:', error);
 | 
					    console.error('Error:', error);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user