json工具类
/** * @Description JsonUtil.java * @author 张军 * @date 2016-7-6 下午1:29:18 * @version V1.0 */ package zj.json; import java.io.Serializable; import java.sql.Timestamp; import java.util.Date; import zj.date.bean.DateTime; import zj.json.bean.DateFormatSerializer; import zj.json.bean.ToJSONString; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.NameFilter; import com.alibaba.fastjson.serializer.PropertyFilter; import com.alibaba.fastjson.serializer.PropertyPreFilter; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializeWriter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.ValueFilter; /** * json工具类 * * @author 张军 * @date 2016-7-6 下午1:29:18 * @version V1.0 * @author SHNKCS 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>} */ public class JsonUtil implements Serializable { private static final long serialVersionUID = 1L; public static SerializeConfig MAPPING = new SerializeConfig(); static { register(); } /** * 转换为json字符串 * * @param value * 转换对象值 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return */ public static String toJSONString(Object value) { return toJSONString(value, null); } /** * 转换为json字符串 * * @param object * 转换对象值 * @param toJson * 参数对象 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return */ public static final String toJSONString(Object object, ToJSONString toJson) { if (toJson == null) { return JSON.toJSONString(object, MAPPING); } SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = null; serializer = new JSONSerializer(out, MAPPING); if (toJson.prettyFormat) { toJson.features.add(SerializerFeature.PrettyFormat); } for (SerializerFeature feature : toJson.features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); if (toJson.filter != null) { if (toJson.filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) toJson.filter); } if (toJson.filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) toJson.filter); } if (toJson.filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) toJson.filter); } if (toJson.filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) toJson.filter); } } serializer.write(object); return out.toString(); } finally { out.close(); } } /** * 注册日期格式化 * * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return 批量集合值 */ private static void register() { MAPPING.put(Date.class, new DateFormatSerializer()); MAPPING.put(DateTime.class, new DateFormatSerializer()); MAPPING.put(Timestamp.class, new DateFormatSerializer()); } }
json工具类参数对象
/** * @Description JsonUtil.java * @author 张军 * @date 2016-7-6 下午1:29:18 * @version V1.0 */ package zj.json.bean; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import com.alibaba.fastjson.serializer.SerializeFilter; import com.alibaba.fastjson.serializer.SerializerFeature; /** * json工具类参数对象 * * @author 张军 * @date 2016-7-6 下午1:29:18 * @version V1.0 * @author SHNKCS 张军 {@link <a target=_blank href="http://www.shanghaijiadun.com">上海加盾信息科技有限公司</a> <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>} */ public class ToJSONString implements Serializable { private static final long serialVersionUID = 1L; /** 过虑属性 **/ public SerializeFilter filter; /** 数组 **/ public Set<SerializerFeature> features = new HashSet<SerializerFeature>(); /** 是否格式化json串 **/ public boolean prettyFormat; /** * 构造方法默认参数 */ public ToJSONString() { // 在springMVC中必须移除pageModel.remove("org.springframework.validation.BindingResult.[user当前类]"); // FastJson提供了SerializerFeature.DisableCircularReferenceDetect // 这个序列化选项,用来关闭引用检测。关闭引用检测后,重复引用对象时就不会被$ref代替,但是在循环引用时也会导致StackOverflowError异常。 this.features.add(SerializerFeature.DisableCircularReferenceDetect); } }
json属性过虑器
package zj.json.bean; import java.util.HashSet; import java.util.Set; import zj.check.util.CheckUtil; import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.PropertyPreFilter; /** * json属性过虑器 * * @author 张军 * @date 2016-7-6 下午1:29:18 * @version V1.0 * @author SHNKCS 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>} */ public class SimplePropertyPreFilter implements PropertyPreFilter { private final Class<?> clazz; // 包含的属性 private final Set<String> includes = new HashSet<String>(); // 排除的属性 private final Set<String> excludes = new HashSet<String>(); public SimplePropertyPreFilter(String... properties) { this(null, properties); } public SimplePropertyPreFilter(Class<?> clazz, String... properties) { super(); this.clazz = clazz; for (String item : properties) { if (CheckUtil.isNotNull(item)) { this.includes.add(item); } } } public Class<?> getClazz() { return clazz; } public Set<String> getIncludes() { return includes; } public Set<String> getExcludes() { return excludes; } /** * 是否应用json字段 * * @param serializer * @see com.alibaba.fastjson.serializer.JSONSerializer * @param source * 被序列化的对象 * @param name * 被序列化的对象中的属性 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return true:包含属性 false:不包含属性 */ public boolean apply(JSONSerializer serializer, Object source, String name) { if (source == null) { return true; } if (clazz != null && !clazz.isInstance(source)) { return true; } if (this.excludes.contains(name)) { return false; } if (includes.size() == 0 || includes.contains(name)) { return true; } return false; } }
json日期格式化类
/** * @Description DateFormatSerializer.java * @author 张军 * @date 2016-7-6 下午4:37:19 * @version V1.0 */ package zj.json.bean; import java.io.IOException; import java.lang.reflect.Type; import java.sql.Timestamp; import zj.check.util.CheckUtil; import zj.date.bean.DateTime; import zj.date.util.DateUtil; import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.ObjectSerializer; /** * json日期格式化类 * * @author 张军 * @date 2016-7-6 下午1:29:18 * @version V1.0 * @author SHNKCS 张军 {@link <a target=_blank href="http://www.shanghaijiadun.com">上海加盾信息科技有限公司</a> <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>} */ public class DateFormatSerializer implements ObjectSerializer { private String pattern; public DateFormatSerializer() { } public DateFormatSerializer(String pattern) { this.pattern = pattern; } /** * 重写write方法 */ @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { if (object == null) { serializer.getWriter().writeNull(); return; } String text = ""; if (CheckUtil.isNull(pattern)) { // 自动判断 if (object instanceof DateTime || object instanceof Timestamp) { text = DateUtil.dateParse(object, "yyyy-MM-dd HH:mm:ss"); } else if (object instanceof java.util.Date) { //java.sql.Date继承自java.util.Date text = DateUtil.dateParse(object, "yyyy-MM-dd"); } else { // 默认情况下 text = DateUtil.dateParse(object, "yyyy-MM-dd HH:mm:ss"); } } else { text = DateUtil.dateParse(object, pattern); } serializer.write(text); } }
本文为张军原创文章,转载无需和我联系,但请注明来自张军的军军小站,个人博客http://www.zhangjunbk.com