From f2a9a896dc547a6cc5bbbbfe60c169f8cae440d0 Mon Sep 17 00:00:00 2001 From: gitchan Date: Tue, 26 Sep 2023 15:25:50 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20size=20=3D=3D=201=20=EC=9D=BC=20?= =?UTF-8?q?=EB=95=8C=EC=97=90=EB=8A=94=20model=20=EC=97=90=EC=84=9C=20valu?= =?UTF-8?q?e=20=EB=A5=BC=20=EB=B9=BC=EC=84=9C=20json=20=EC=9C=BC=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mvc/src/main/java/nextstep/mvc/view/JsonView.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mvc/src/main/java/nextstep/mvc/view/JsonView.java b/mvc/src/main/java/nextstep/mvc/view/JsonView.java index c4ebbcdf07..8077bd36b2 100644 --- a/mvc/src/main/java/nextstep/mvc/view/JsonView.java +++ b/mvc/src/main/java/nextstep/mvc/view/JsonView.java @@ -1,5 +1,6 @@ package nextstep.mvc.view; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -17,6 +18,14 @@ public class JsonView implements View { public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType(APPLICATION_JSON_UTF8_VALUE); final PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(model)); + writer.write(generateJson(model)); + } + + private String generateJson(Map model) throws JsonProcessingException { + if (model.size() == 1) { + Object value = model.values().iterator().next(); + return objectMapper.writeValueAsString(value); + } + return objectMapper.writeValueAsString(model); } }