com.silanis.esl.api.util.JacksonUtil Maven / Gradle / Ivy
The newest version!
package com.silanis.esl.api.util;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.silanis.esl.api.JsonDeserializationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Sam Habbab
*
*/
public class JacksonUtil {
private static final String CLASS = JacksonUtil.class.getName();
protected static Logger log = Logger.getLogger( CLASS);
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
public static T deserialize(String json, Class cl) {
try {
return mapper.readValue(json, cl);
} catch (IOException e) {
log.log(Level.SEVERE, "Failed to deserialize json string: " + json, e);
throw new JsonDeserializationException(cl, e);
}
}
public static List deserializeList(String json, Class cl) {
List obj;
try {
obj = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(ArrayList.class, cl));
} catch (JsonMappingException me) {
// maybe the string representation is an object
obj = new ArrayList();
obj.add(deserialize(json, cl));
} catch (IOException e) {
log.log(Level.SEVERE, "Failed to deserialize json list: " + json, e);
throw new JsonDeserializationException(cl, e);
}
return obj;
}
/**
* Generic deserializer.
*
* Example:
* Result result = JacksonUtil.deserialize(reusltString, new TypeReference>() {});
*/
public static T deserialize(String json, TypeReference typeReference) {
try {
return mapper.readValue(json, typeReference);
} catch (Exception e) {
log.log(Level.SEVERE, "Failed to deserialize generic object: " + json, e);
throw new JsonDeserializationException(typeReference, e);
}
}
// ******************************************************************************************************************************************************
public static String serializeDirty(Object json) {
return _serialize(json, true);
}
public static String serialize(Object json) {
return _serialize(json, false);
}
// ******************************************************************************************************************************************************
private static String _serialize(Object json, boolean filterDirtyFields) {
String result = null;
try {
ObjectMapper mapperFilter = new ObjectMapper();
DirtyFieldsFilter filter = new DirtyFieldsFilter(filterDirtyFields);
FilterProvider fp = new SimpleFilterProvider().addFilter("DirtyFieldsFilter", filter).setDefaultFilter(filter).setFailOnUnknownId(false);
result = mapperFilter.writer(fp).writeValueAsString(json);
} catch (Exception e) {
log.log(Level.SEVERE, "Failed to serialize json object: " + json, e);
throw new RuntimeException("Invalid arguments.", e);
}
return result;
}
public static String merge(String base, String delta) throws IOException {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
TypeReference