
com.crabshue.commons.json.serialization.JsonSerializationUtils Maven / Gradle / Ivy
package com.crabshue.commons.json.serialization;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.lang3.Validate;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.exceptions.context.CommonErrorContext;
import com.crabshue.commons.file.FileSystemUtils;
import com.crabshue.commons.file.exceptions.FileErrorContext;
import com.crabshue.commons.file.exceptions.FileErrorType;
import com.crabshue.commons.json.serialization.exceptions.JsonErrorType;
import com.crabshue.commons.json.serialization.serializers.JSR310DateTimeSerializer;
import com.crabshue.commons.json.serialization.serializers.JSR310LocalDateDeserializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
/**
* Utility class for JSON serialization/deserialization.
*
*/
public class JsonSerializationUtils {
protected JsonSerializationUtils() {
}
/**
* Convert an object to its JSON representation.
*
* @param object the object.
* @return the JSON representation.
*/
public static String convertObjectToJsonString(final Object object) {
final ObjectMapper mapper = buildObjectMapper();
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new SystemException(JsonErrorType.ERROR_CONVERTING_TO_JSON, e)
.addContextValue(CommonErrorContext.CAUSE, object);
}
}
/**
* Convert an object to its JSON representation and serialize it in the given file.
*
* @param object the object.
* @param file the file.
* @return the JSON representation serialized file.
*/
public static File convertObjectToJsonFile(final Object object, final File file) {
Validate.notNull(object);
Validate.notNull(file);
FileSystemUtils.retrieveOrCreateFile(file);
final ObjectMapper mapper = buildObjectMapper();
try {
mapper.writeValue(file, object);
} catch (IOException e) {
throw new SystemException(FileErrorType.ERROR_WRITING_FILE, e)
.addContextValue(CommonErrorContext.CAUSE, object)
.addContextValue(FileErrorContext.FILE, file);
}
return file;
}
/**
* Deserialize an object from its JSON file representation.
*
* @param file the file.
* @param clazz the target class to deserialize to.
* @param the return type.
* @return the deserialized object.
*/
public static T readJsonFileToObject(final File file, final Class clazz) {
Validate.notNull(file);
final ObjectMapper mapper = buildObjectMapper();
try {
return mapper.readValue(file, clazz);
} catch (IOException e) {
throw new SystemException(JsonErrorType.ERROR_PARSING_JSON, e)
.addContextValue(CommonErrorContext.CAUSE, file);
}
}
/**
* Deserialize a collection object from its JSON file representation.
*
* @param file the file.
* @param arrayClazz the array version of the target class to deserialize to.
* @param the return type.
* @return the deserialized object.
*/
public static Collection readJsonFileToCollection(final File file, final Class arrayClazz) {
Validate.notNull(file);
final ObjectMapper mapper = buildObjectMapper();
try {
return Arrays.asList(mapper.readValue(file, arrayClazz));
} catch (IOException e) {
throw new SystemException(JsonErrorType.ERROR_PARSING_JSON, e)
.addContextValue(CommonErrorContext.CAUSE, file);
}
}
private static ObjectMapper buildObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
mapper.registerModule(module);
return mapper;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy