JsonUtil
public class JsonUtil {
private static final ObjectMapper jsonMapper = new ObjectMapper();
public static <T> T toObj(String str, Class<T> clz) {
try {
return jsonMapper.readValue(str, clz);
} catch (JsonProcessingException e) {
throw new UnsupportedOperationException(e);
}
}
public static <T> T toObj(String str, TypeReference<T> clz) {
try {
return jsonMapper.readValue(str, clz);
} catch (JsonProcessingException e) {
throw new UnsupportedOperationException(e);
}
}
public static <T> List<T> toList(String str, Class<T> clz) {
try {
return jsonMapper.readValue(str, new TypeReference<List<T>>() {
});
} catch (JsonProcessingException e) {
throw new UnsupportedOperationException(e);
}
}
public static JsonNode toJsonNode(String str) {
try {
return jsonMapper.readTree(str);
} catch (JsonProcessingException e) {
throw new UnsupportedOperationException(e);
}
}
public static <T> T nodeToValue(JsonNode node, Class<T> clz) {
try {
return jsonMapper.treeToValue(node, clz);
} catch (JsonProcessingException e) {
throw new UnsupportedOperationException(e);
}
}
public static String toStr(Object t) {
try {
return jsonMapper.writeValueAsString(t);
} catch (Exception e) {
throw new UnsupportedOperationException(e);
}
}
}