Skip to content

Instantly share code, notes, and snippets.

@calvernaz
Last active February 2, 2017 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvernaz/d41a8304584aeaa92a405eae5058e007 to your computer and use it in GitHub Desktop.
Save calvernaz/d41a8304584aeaa92a405eae5058e007 to your computer and use it in GitHub Desktop.
package com.jackson.utils;
import java.io.IOException;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.joda.JodaModule;
/**
* {@link JsonMapper} implemented with Jackson.
*/
public class JacksonJsonMapper implements JsonMapper {
private static final String ERROR_WRITING_JSON_STRING = "Error writing JSON string";
private static final String ERROR_READING_JSON_STRING = "Error reading JSON string";
private final ObjectMapper mapper;
public JacksonJsonMapper(ObjectMapper mapper) {
this.mapper = mapper;
mapper.registerModule(new GuavaModule());
mapper.registerModule(new JodaModule());
this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Override
public <T> T fromJson(String json, Class<T> type) {
if (StringUtils.isEmpty(json)) {
// TODO fix for Jackson not respecting
// ACCEPT_EMPTY_STRING_AS_NULL_OBJECT should be investigated further
return null;
}
try {
return mapper.readValue(json, type);
} catch (IOException ex) {
throw new JsonMapperException(ERROR_READING_JSON_STRING, ex);
}
}
@Override
public <T> List<T> listFromJson(String json, Class<T> elementType) {
if (StringUtils.isEmpty(json)) {
// TODO fix for Jackson not respecting
// ACCEPT_EMPTY_STRING_AS_NULL_OBJECT should be investigated further
return Collections.emptyList();
}
JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, elementType);
try {
return mapper.readValue(json, javaType);
} catch (IOException ex) {
throw new JsonMapperException(ERROR_READING_JSON_STRING, ex);
}
}
@Override
public <K, V> Map<K, V> mapFromJson(String json, Class<K> keyType, Class<V> valueType) {
if (StringUtils.isEmpty(json)) {
// TODO fix for Jackson not respecting
// ACCEPT_EMPTY_STRING_AS_NULL_OBJECT should be investigated further
return null;
}
JavaType javaType = mapper.getTypeFactory().constructMapType(HashMap.class, keyType, valueType);
try {
return mapper.readValue(json, javaType);
} catch (IOException ex) {
throw new JsonMapperException(ERROR_READING_JSON_STRING, ex);
}
}
@Override
public <T> Set<T> setFromJson(String json, Class<T> elementType) {
if (StringUtils.isEmpty(json)) {
// TODO fix for Jackson not respecting
// ACCEPT_EMPTY_STRING_AS_NULL_OBJECT should be investigated further
return Collections.emptySet();
}
JavaType javaType = mapper.getTypeFactory().constructCollectionType(Set.class, elementType);
try {
return mapper.readValue(json, javaType);
} catch (IOException ex) {
throw new JsonMapperException(ERROR_READING_JSON_STRING, ex);
}
}
@Override
public String toJson(Object obj, Object... other) {
try {
if (other == null || other.length == 0) {
return mapper.writeValueAsString(obj);
}
return mapper.writeValueAsString(asList(obj, other));
} catch (IOException ex) {
throw new JsonMapperException(ERROR_WRITING_JSON_STRING, ex);
}
}
@Override
public String toJson(Class<?> rootType, Object... objs) {
try {
ObjectWriter writerWithType = mapper.writerFor(rootType);
if (objs.length == 1) {
return writerWithType.writeValueAsString(objs[0]);
}
return writerWithType.writeValueAsString(asList(objs));
} catch (IOException ex) {
throw new JsonMapperException(ERROR_WRITING_JSON_STRING, ex);
}
}
@SuppressWarnings("rawtypes")
@Override
public String toJson(List obj, Class<?> elementType, Object... other) {
try {
if (other == null || other.length == 0) {
JavaType rootType = mapper.getTypeFactory().constructCollectionType(List.class, elementType);
return mapper.writerFor(rootType).writeValueAsString(obj);
}
return mapper.writeValueAsString(asList(obj, other));
} catch (IOException ex) {
throw new JsonMapperException(ERROR_WRITING_JSON_STRING, ex);
}
}
private List<Object> asList(Object obj, Object... other) {
List<Object> merge = new ArrayList<>(other.length + 1);
merge.add(obj);
Collections.addAll(merge, other);
return merge;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment