package com.ljunggren.storm.utils;
import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONUtils {
public static boolean isValidJSON(String json) {
if (json == null) {
return false;
}
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.readTree(json);
return true;
} catch (IOException e) {
return false;
}
}
public static String objectToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
String json = "";
json = mapper.writeValueAsString(object);
return json;
}
@SuppressWarnings("rawtypes")
public static <T> T jsonToListObject(String json, Class clazz) throws JsonMappingException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
return mapper.readValue(json, type);
}
}