io.soffa.foundation.commons.JsonUtil Maven / Gradle / Ivy
package io.soffa.foundation.commons;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public final class JsonUtil {
private static final ObjectMapper MAPPER = ObjectFactory.create(false);
private static final ObjectMapper MAPPER_FULL_ACCESS = ObjectFactory.create(true);
private JsonUtil() {
}
public static boolean isJson(String input) {
try {
new JSONObject(input);
return true;
} catch (JSONException e) {
return false;
}
}
public static String fromXml(String xmlInput) {
if (xmlInput == null) return null;
return XML.toJSONObject(xmlInput).toString();
}
@SneakyThrows
public static T fromXml(String xmlInput, String root, Class kind) {
if (xmlInput == null) {
return ClassUtil.newInstance(kind);
}
JSONObject object = XML.toJSONObject(xmlInput);
return deserialize(object.getJSONObject(root).toString(), kind);
}
@SneakyThrows
public static T deserialize(String jsonString, Class type) {
return ObjectFactory.deserialize(MAPPER, jsonString, type);
}
@SneakyThrows
public static T deserializeIgnoreAccess(String jsonString, Class type) {
return ObjectFactory.deserialize(MAPPER_FULL_ACCESS, jsonString, type);
}
@SneakyThrows
public static T deserialize(byte[] data, Class type) {
return ObjectFactory.deserialize(MAPPER, new String(data, StandardCharsets.UTF_8), type);
}
public static T convert(Object input, Class type) {
return ObjectFactory.convert(MAPPER, input, type);
}
public static T convertIgnoreAccess(Object input, Class type) {
return ObjectFactory.convert(MAPPER_FULL_ACCESS, input, type);
}
public static String serialize(Object src) {
return ObjectFactory.serialize(MAPPER, src);
}
public static String serializeIgnoreAccess(Object src) {
return ObjectFactory.serialize(MAPPER_FULL_ACCESS, src);
}
public static void serializeToFile(Object content, File file) {
ObjectFactory.serializeToFile(MAPPER, content, file);
}
public static Map deserializeMap(String input) {
return ObjectFactory.deserializeMap(MAPPER, input);
}
public static Map deserializeMap(InputStream input) {
return ObjectFactory.deserializeMap(MAPPER, input);
}
public static Map deserializeMap(InputStream input, Class type) {
return ObjectFactory.deserializeMap(MAPPER, input, type);
}
public static Map deserializeMap(String input, Class type) {
return ObjectFactory.deserializeMap(MAPPER, input, type);
}
public static T deserializeParametricType(String input, Class> rawType, Class>... parameterClasses) {
return ObjectFactory.deserializeParametricType(MAPPER, input, rawType, parameterClasses);
}
public static List deserializeList(String input, Class type) {
return ObjectFactory.deserializeList(MAPPER, input, type);
}
public static List deserializeList(InputStream input, Class type) {
return ObjectFactory.deserializeList(MAPPER, input, type);
}
public static Map toMap(Object input) {
return toMap(input, Object.class);
}
public static Map toMapFullAccess(Object input) {
return ObjectFactory.toMap(MAPPER_FULL_ACCESS, input, Object.class);
}
public static Map toMap(Object input, Class valueClass) {
return ObjectFactory.toMap(MAPPER, input, valueClass);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy