io.leopard.json.JsonJacksonImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leopard-json Show documentation
Show all versions of leopard-json Show documentation
规范、简化Json操作,可选Jackson、FastJson实现。并可在TopNB查看耗时统计信息。
package io.leopard.json;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class JsonJacksonImpl implements IJson {
private static ObjectMapper mapper = new ObjectMapper(); // can reuse, share
private static ObjectMapper mapperIgnoreUnknownField = new ObjectMapper(); // 忽略不存在的字段.
private static ObjectWriter writer;
static {
// DeserializationConfig.
// writer.wi
// mapper.setAnnotationIntrospector(ai)
// 自定义 Jackson 注解与禁用某一特定的注解
// http: // gloveangels.com/customize-jackson-annotation-and-disable-specific-annotation/
// mapper
// JsonIgnore dd;
// mapperIgnoreUnknownField.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
mapper.setAnnotationIntrospector(new DisablingJsonSerializerIntrospector());
writer = mapper.writer().withDefaultPrettyPrinter();
mapperIgnoreUnknownField.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 将对象转成json.
*
* @param obj 对象
* @return
*/
@Override
public String toFormatJson(Object obj) {
if (obj == null) {
return null;
}
try {
return writer.writeValueAsString(obj);
}
catch (Exception e) {
throw new JsonException(e.getMessage(), e);
}
}
/**
* 将对象转成json.
*
* @param obj 对象
* @return
*/
@Override
public String toJson(Object obj) {
try {
String json;
if (obj == null) {
json = null;
}
else {
json = mapper.writeValueAsString(obj);
}
return json;
}
catch (Exception e) {
throw new JsonException(e.getMessage(), e);
}
}
// @SuppressWarnings("deprecation")
@Override
public List toListObject(String json, Class clazz) {
if (json == null || json.length() == 0) {
return null;
}
JavaType javaType = mapper.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, clazz);
try {
return mapper.readValue(json, javaType);
}
catch (Exception e) {
throw new JsonException(e.getMessage(), e);
}
}
@Override
public List toListObject(String json, Class clazz, boolean ignoreUnknownField) {
if (!ignoreUnknownField) {
return this.toListObject(json, clazz);
}
if (json == null || json.length() == 0) {
return null;
}
JavaType javaType = mapperIgnoreUnknownField.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, clazz);
try {
return mapperIgnoreUnknownField.readValue(json, javaType);
}
catch (Exception e) {
throw new JsonException(e.getMessage(), e);
}
}
// public static List toObject(List jsonList, Class valueType) {
// return toObject(jsonList, valueType, false);
// }
// public static List toObject(List jsonList, Class valueType, boolean ignoreUnknownField) {
// if (jsonList == null || jsonList.isEmpty()) {
// return null;
// }
// List list = new ArrayList();
// for (String json : jsonList) {
// list.add(JsonJacksonImpl.toObject(json, valueType, ignoreUnknownField));
// }
// return list;
// }
// @SuppressWarnings("unchecked")
// public static Map toMap(String content) {
// return JsonJacksonImpl.toObject(content, Map.class);
// }
//
// @SuppressWarnings("unchecked")
// public static Set