import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.springframework.boot.json.JsonParseException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
public class JsonUtils {
public static String parseJsonObject(Object obj) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
DefaultSerializerProvider sp = new DefaultSerializerProvider.Impl();
sp.setNullValueSerializer(new NullSerializer());
mapper.setSerializerProvider(sp);
return mapper.writeValueAsString(obj);
}
public static List<Map<String, Object>> parseStringAsList(String str) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> dtlList = null;
dtlList = mapper.readValue(str, new TypeReference<List<Map<String, Object>>>(){});
return dtlList;
}
public static JsonNode parseString(String str) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(str, JsonNode.class);
}
public static JsonNode parseObject(Object obj) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.valueToTree(obj);
}
public static <T> T parseString(String str, Class<T> classType) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(str, classType);
}
public static <T> T parseString(JsonNode jsonNode, Class<T> classType) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(parseJsonObject(jsonNode), classType);
}
public static class NullSerializer extends JsonSerializer<Object> {
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString("");
}
}
}