From 33f41fda832c4e1a3afd3a686a7bef9c1ef430f8 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Mon, 18 Sep 2023 13:23:35 +0900 Subject: [PATCH] =?UTF-8?q?JSON.parse(String,=20TypeReference)=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cokr/xit/foundation/data/JSON.java | 17 +++++++++++++++++ .../java/cokr/xit/foundation/web/WebClient.java | 11 +++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/cokr/xit/foundation/data/JSON.java b/src/main/java/cokr/xit/foundation/data/JSON.java index 5263943..963a957 100644 --- a/src/main/java/cokr/xit/foundation/data/JSON.java +++ b/src/main/java/cokr/xit/foundation/data/JSON.java @@ -4,6 +4,7 @@ import java.io.InputStream; import java.util.Set; import com.fasterxml.jackson.core.JsonParser.Feature; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; @@ -64,6 +65,14 @@ public class JSON extends AbstractComponent { } } + public T parse(String json, TypeReference typeRef) { + try { + return getObjectMapper().readValue(json, typeRef); + } catch (Exception e) { + throw runtimeException(e); + } + } + /**input의 내용을 파싱하여 주어진 클래스의 객체로 변환한다. * @param 클래스 유형 * @param input JSON input @@ -78,6 +87,14 @@ public class JSON extends AbstractComponent { } } + public T parse(InputStream input, TypeReference typeRef) { + try { + return getObjectMapper().readValue(input, typeRef); + } catch (Exception e) { + throw runtimeException(e); + } + } + /**주어진 객체를 JSON 문자열로 변환한다. * @param obj 객체 * @return 객체를 변환한 JSON 문자열 diff --git a/src/main/java/cokr/xit/foundation/web/WebClient.java b/src/main/java/cokr/xit/foundation/web/WebClient.java index d921cca..ac66992 100644 --- a/src/main/java/cokr/xit/foundation/web/WebClient.java +++ b/src/main/java/cokr/xit/foundation/web/WebClient.java @@ -29,6 +29,7 @@ import java.util.function.Consumer; import javax.net.ssl.SSLContext; import cokr.xit.foundation.Assert; +import cokr.xit.foundation.Log; import cokr.xit.foundation.data.JSON; /**서버와의 http 통신을 지원하는 유틸리티. @@ -468,13 +469,15 @@ public class WebClient { private String inJSON() { Object body = bodyData(); + String str = ""; if (!Assert.isEmpty(body)) - return json().stringify(body); + str = json().stringify(body); + else if (!Assert.isEmpty(keyValues)) + str = json().stringify(keyValues); - if (!Assert.isEmpty(keyValues)) - return json().stringify(keyValues); + Log.get(getClass()).debug("json request:\n{}", str); - return ""; + return str; } private String encode(String str) {