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查看耗时统计信息。
The newest version!
package io.leopard.json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
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.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.leopard.lang.inum.Onum;
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);
{
SimpleModule module = new SimpleModule();
module.addSerializer(new OnumJsonSerializer());
// module.addDeserializer(Onum.class, new OnumJsonDeserializer());
mapper.registerModule(module);
mapperIgnoreUnknownField.registerModule(module);
}
mapper.setAnnotationIntrospector(new DisablingJsonSerializerIntrospector());
mapperIgnoreUnknownField.setAnnotationIntrospector(new DisablingJsonSerializerIntrospector());
writer = mapper.writer().withDefaultPrettyPrinter();
mapperIgnoreUnknownField.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* Onum Json序列化.
*
* @author 谭海潮
*
*/
public static class OnumJsonSerializer extends StdSerializer> {
private static final long serialVersionUID = 1L;
public OnumJsonSerializer() {
super(Onum.class, false);
}
@Override
public void serialize(Onum, ?> onum, JsonGenerator generator, SerializerProvider provider) throws IOException {
generator.writeObject(onum.getKey());
}
}
/**
* 将对象转成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