Initial Commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package net.locusworks.common.objectmapper;
|
||||
|
||||
/**
|
||||
* Error handler for the object mapper class
|
||||
* @author Isaac Parenteau
|
||||
* @version 1.0.0
|
||||
* @date 02/15/2018
|
||||
*
|
||||
*/
|
||||
public interface ObjectMapperError {
|
||||
void getError(Throwable e);
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package net.locusworks.common.objectmapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
/**
|
||||
* Object mapper to map to convert objects to json string or
|
||||
* json string back to object
|
||||
* @author Isaac Parenteau
|
||||
* @version 1.0.0
|
||||
* @date 02/15/2018
|
||||
*/
|
||||
public class ObjectMapperHelper {
|
||||
|
||||
private static ObjectMapper mapper;
|
||||
static {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an object out to json
|
||||
* @param object Object to convert to json
|
||||
* @return return a string representation of the object converted to json
|
||||
*/
|
||||
public static ObjectMapperResults<String> writeValue(Object object) {
|
||||
String results = "";
|
||||
try {
|
||||
results = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
|
||||
} catch (Exception ex) {
|
||||
try {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
results = gson.toJson(object);
|
||||
} catch (Exception e) {
|
||||
new ObjectMapperResults<>(e);
|
||||
}
|
||||
}
|
||||
return new ObjectMapperResults<>(results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert binary data to an object
|
||||
* @param src Binary data to convert
|
||||
* @param clazz Class to convert the data to
|
||||
* @param <T> The expected class of the value
|
||||
* @return the object populated with the data in the json string
|
||||
*/
|
||||
public static <T> ObjectMapperResults<T> readValue(byte[] src, Class<T> clazz) {
|
||||
try {
|
||||
return new ObjectMapperResults<T>(mapper.readValue(src, clazz));
|
||||
} catch (Exception ex) {
|
||||
return new ObjectMapperResults<T>(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a json string to an object
|
||||
* @param src Json String
|
||||
* @param clazz Class to convert the json string to
|
||||
* @param <T> The expected class of the value
|
||||
* @return the object populated with the data in the json string
|
||||
*/
|
||||
public static <T> ObjectMapperResults<T> readValue(String src, Class<T> clazz) {
|
||||
try {
|
||||
return new ObjectMapperResults<T>(mapper.readValue(src, clazz));
|
||||
} catch (Exception ex) {
|
||||
return new ObjectMapperResults<T>(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an java object to a class
|
||||
* @param src Object to convert
|
||||
* @param clazz Class to convert the object to
|
||||
* @param <T> The expected class of the value
|
||||
* @return the object populated with the data in the json string
|
||||
*/
|
||||
public static <T> ObjectMapperResults<T> readValue(Object src, Class<T> clazz) {
|
||||
try {
|
||||
return readValue(mapper.writeValueAsString(src), clazz);
|
||||
} catch (Exception ex) {
|
||||
return new ObjectMapperResults<T>(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object to a list
|
||||
* @param object Object to convert
|
||||
* @param objectClass Class to convert the object to
|
||||
* @param <T> The expected class of the object
|
||||
* @return the object list populated with the data in the json string
|
||||
*/
|
||||
public static <T, L extends Collection<?>> ObjectMapperListResults<List<T>> readListValue(Object object, Class<T> objectClass) {
|
||||
return readListValue(object, objectClass, ArrayList.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object to a list
|
||||
* @param object Object to convert
|
||||
* @param objectClass Class to convert the object to
|
||||
* @param listClass List type to make
|
||||
* @param <T> The expected class of the object
|
||||
* @param <L> The expect class of the list
|
||||
* @return the object list populated with the data in the json string
|
||||
*/
|
||||
public static <T, L extends Collection<?>> ObjectMapperListResults<List<T>> readListValue(Object object, Class<T> objectClass, Class<L> listClass) {
|
||||
try {
|
||||
return readListValue(mapper.writeValueAsString(object), objectClass, listClass);
|
||||
} catch (Exception ex) {
|
||||
return new ObjectMapperListResults<>(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object to a list
|
||||
* @param src Source to convert
|
||||
* @param objectClass Class to convert the object to
|
||||
* @param listClass List type to make
|
||||
* @param <T> The expected class of the object
|
||||
* @param <L> The expect class of the list
|
||||
* @return the object list populated with the data in the json string
|
||||
*/
|
||||
public static <T, L extends Collection<?>> ObjectMapperListResults<List<T>> readListValue(String src, Class<T> objectClass, Class<L> listClass) {
|
||||
try {
|
||||
List<T> item = mapper.readValue(src, mapper.getTypeFactory().constructCollectionType(listClass, objectClass));
|
||||
return new ObjectMapperListResults<>(item);
|
||||
} catch (Exception ex) {
|
||||
return new ObjectMapperListResults<>(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package net.locusworks.common.objectmapper;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Holds the results from the object mapper list conversion
|
||||
* @author Isaac Parenteau
|
||||
* @version 1.0.0
|
||||
* @date 02/15/2018
|
||||
* @param <T> class type of the object mapper
|
||||
*/
|
||||
public class ObjectMapperListResults<T extends Collection<?>> extends ObjectMapperResults<T> {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param results results from the conversion
|
||||
*/
|
||||
public ObjectMapperListResults(T results) {
|
||||
this(results, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param exception exception that was thrown during conversion
|
||||
*/
|
||||
public ObjectMapperListResults(Throwable exception) {
|
||||
this(null, exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param results results from the conversion
|
||||
* @param exception exception that was thrown during conversion
|
||||
*/
|
||||
public ObjectMapperListResults(T results, Throwable exception) {
|
||||
super(results, exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the error handler to the results to retrieve the error that caused
|
||||
* the exception
|
||||
* @param error the error handler to use
|
||||
* @return this
|
||||
*/
|
||||
public ObjectMapperListResults<T> withErrorHandler(ObjectMapperError error) {
|
||||
if (this.hasError() && error != null) {
|
||||
error.getError(this.getException());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package net.locusworks.common.objectmapper;
|
||||
|
||||
/**
|
||||
* Holds the results from the object mapper list conversion
|
||||
* @author Isaac Parenteau
|
||||
* @version 1.0.0
|
||||
* @date 02/15/2018
|
||||
* @param <T> class type of the object being converted from json to object
|
||||
*/
|
||||
public class ObjectMapperResults<T> {
|
||||
|
||||
private Throwable exception;
|
||||
private T results;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param results results from the conversion
|
||||
*/
|
||||
public ObjectMapperResults(T results) {
|
||||
this(results, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param exception exception that was thrown during conversion
|
||||
*/
|
||||
public ObjectMapperResults(Throwable exception) {
|
||||
this(null, exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param results results from the conversion
|
||||
* @param exception exception that was thrown during conversion
|
||||
*/
|
||||
public ObjectMapperResults(T results, Throwable exception) {
|
||||
this.results = results;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the exception that happened during conversion
|
||||
* @return exception
|
||||
*/
|
||||
public Throwable getException() {
|
||||
return exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the exception
|
||||
* @param exception
|
||||
*/
|
||||
public void setException(Throwable exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result
|
||||
* @return the converted results
|
||||
*/
|
||||
public T getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the results
|
||||
* @param results results to set
|
||||
*/
|
||||
public void setResults(T results) {
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the conversion caused an error
|
||||
* @return true if there is an error, false otherwise
|
||||
*/
|
||||
public boolean hasError() {
|
||||
return this.exception != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the error handler to the results to retrieve the error that caused
|
||||
* the exception
|
||||
* @param error the error handler to use
|
||||
* @return this
|
||||
*/
|
||||
public ObjectMapperResults<T> withErrorHandler(ObjectMapperError error) {
|
||||
if (this.hasError() && error != null) {
|
||||
error.getError(this.getException());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user