com.barchart.ondemand.util.JsonUtil Maven / Gradle / Ivy
The newest version!
package com.barchart.ondemand.util;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
public class JsonUtil {
private static final ObjectMapper json = new ObjectMapper();
private static Logger log = LoggerFactory.getLogger(JsonUtil.class);
static {
json.setSerializationInclusion(Include.NON_NULL);
}
public static T fromJson(final Class klaz, final String jsonText) {
try {
return json.readValue(jsonText, klaz);
} catch (JsonParseException e) {
log.error("failed to parse JSON, e = {}", e);
} catch (JsonMappingException e) {
log.error("failed to parse JSON, e = {}", e);
} catch (IOException e) {
log.error("failed to parse JSON, e = {}", e);
}
log.error("string value = {}", jsonText);
return null;
}
public static boolean update(final Object value, final String json) {
try {
final ObjectReader reader = JsonUtil.json.readerForUpdating(value);
reader.readValue(json);
return true;
} catch (final Exception e) {
log.error("", e);
return false;
}
}
public static String intoJson(final Object instance) {
try {
return json.writeValueAsString(instance);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy