JSON.parse(String, TypeReference) 추가

master
mjkhan21 1 year ago
parent 84ad9e5366
commit 33f41fda83

@ -4,6 +4,7 @@ import java.io.InputStream;
import java.util.Set; import java.util.Set;
import com.fasterxml.jackson.core.JsonParser.Feature; 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.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
@ -64,6 +65,14 @@ public class JSON extends AbstractComponent {
} }
} }
public <T> T parse(String json, TypeReference<T> typeRef) {
try {
return getObjectMapper().readValue(json, typeRef);
} catch (Exception e) {
throw runtimeException(e);
}
}
/**input . /**input .
* @param <T> * @param <T>
* @param input JSON input * @param input JSON input
@ -78,6 +87,14 @@ public class JSON extends AbstractComponent {
} }
} }
public <T> T parse(InputStream input, TypeReference<T> typeRef) {
try {
return getObjectMapper().readValue(input, typeRef);
} catch (Exception e) {
throw runtimeException(e);
}
}
/** JSON . /** JSON .
* @param obj * @param obj
* @return JSON * @return JSON

@ -29,6 +29,7 @@ import java.util.function.Consumer;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import cokr.xit.foundation.Assert; import cokr.xit.foundation.Assert;
import cokr.xit.foundation.Log;
import cokr.xit.foundation.data.JSON; import cokr.xit.foundation.data.JSON;
/** http . /** http .
@ -468,13 +469,15 @@ public class WebClient {
private String inJSON() { private String inJSON() {
Object body = bodyData(); Object body = bodyData();
String str = "";
if (!Assert.isEmpty(body)) 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)) Log.get(getClass()).debug("json request:\n{}", str);
return json().stringify(keyValues);
return ""; return str;
} }
private String encode(String str) { private String encode(String str) {

Loading…
Cancel
Save