Skip to content

Commit

Permalink
refactor: size == 1 일 때에는 model 에서 value 를 빼서 json 으로 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchannn committed Sep 26, 2023
1 parent 4f9b7ab commit f2a9a89
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mvc/src/main/java/nextstep/mvc/view/JsonView.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +18,14 @@ public class JsonView implements View {
public void render(final Map<String, ?> 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<String, ?> model) throws JsonProcessingException {
if (model.size() == 1) {
Object value = model.values().iterator().next();
return objectMapper.writeValueAsString(value);
}
return objectMapper.writeValueAsString(model);
}
}

0 comments on commit f2a9a89

Please sign in to comment.