All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.leopard.json.JsonJacksonImpl Maven / Gradle / Ivy

Go to download

规范、简化Json操作,可选Jackson、FastJson实现。并可在TopNB查看耗时统计信息。

There is a newer version: 0.9.12
Show newest version
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 toSet(String content) {
	// return JsonJacksonImpl.toObject(content, Set.class);
	// }
	//
	// @SuppressWarnings("unchecked")
	// public static  Map toMap(String json, Class clazz) {
	// return JsonJacksonImpl.toObject(json, Map.class);
	// }
	//
	// @SuppressWarnings("unchecked")
	// public static  Set toSet(String json, Class clazz) {
	// return JsonJacksonImpl.toObject(json, Set.class);
	// }

	// @SuppressWarnings("unchecked")
	// public static Map toNotNullMap(String json) {
	// Map map = JsonJacksonImpl.toObject(json, Map.class);
	// if (map == null) {
	// map = new LinkedHashMap();
	// }
	// return map;
	// }

	// @SuppressWarnings("unchecked")
	// public static  Map toNotNullMap(String json, Class clazz) {
	// Map map = JsonJacksonImpl.toObject(json, Map.class);
	// if (map == null) {
	// map = new LinkedHashMap();
	// }
	// return map;
	// }

	// @SuppressWarnings("unchecked")
	// public static  Set toNotNullSet(String json, Class clazz) {
	// Set set = JsonJacksonImpl.toObject(json, Set.class);
	// if (set == null) {
	// set = new LinkedHashSet();
	// }
	// return set;
	// }

	/**
	 * 将Json转换成对象.
	 * 
	 * @param json
	 * @param valueType
	 * @return
	 */
	@Override
	public  T toObject(String json, Class clazz) {
		return toObject(json, clazz, false);
	}

	/**
	 * 将Json转换成对象.
	 * 
	 * @param json
	 * @param clazz
	 * @param ignoreUnknownField 是否忽略不存在的字段?
	 * @return
	 */
	@Override
	public  T toObject(String json, Class clazz, boolean ignoreUnknownField) {
		if (json == null || json.length() == 0) {
			return null;
		}

		try {
			if (ignoreUnknownField) {
				return mapperIgnoreUnknownField.readValue(json, clazz);
			}
			else {
				return mapper.readValue(json, clazz);
			}
		}
		catch (Exception e) {
			throw new JsonException(e.getMessage(), e);
		}
	}

	@Override
	public  List toListObject(List jsonList, Class clazz) {
		if (jsonList == null || jsonList.isEmpty()) {
			return null;
		}
		List list = new ArrayList();
		for (String json : jsonList) {
			list.add(this.toObject(json, clazz));
		}
		return list;
	}

	@Override
	public  List toListObject(List jsonList, Class clazz, boolean ignoreUnknownField) {
		if (jsonList == null || jsonList.isEmpty()) {
			return null;
		}
		List list = new ArrayList();
		for (String json : jsonList) {
			list.add(this.toObject(json, clazz, ignoreUnknownField));
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map toMap(String json) {
		return toObject(json, Map.class);
	}
}