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

com.liuchink.hcutils.json.HcFastJsonUtils Maven / Gradle / Ivy

There is a newer version: 1.0.13.RELEASE
Show newest version
package com.liuchink.hcutils.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.liuchink.hcutils.date.HcDateFormatUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * JSON工具类
 *
 * @author 刘钢
 * @since v1.0.1
 */
@Slf4j
public final class HcFastJsonUtils {

	private HcFastJsonUtils() {
	}

	/**
	 * json转字符串
	 * 
	 * @param obj
	 *            obj
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj) {
		return toJsonString(obj,
				HcDateFormatUtils.DATEFORMAT_YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param features
	 *            features
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj,
			SerializerFeature... features) {
		return toJsonString(obj,
				HcDateFormatUtils.DATEFORMAT_YYYY_MM_DD_HH_MM_SS,
				new SimplePropertyPreFilter(), features);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param simplePropertyPreFilter
	 *            simplePropertyPreFilter
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj,
			SimplePropertyPreFilter simplePropertyPreFilter) {
		return toJsonString(obj,
				HcDateFormatUtils.DATEFORMAT_YYYY_MM_DD_HH_MM_SS,
				simplePropertyPreFilter);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param simplePropertyPreFilter
	 *            simplePropertyPreFilter
	 * @param features
	 *            features
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj,
			SimplePropertyPreFilter simplePropertyPreFilter,
			SerializerFeature... features) {
		return toJsonString(obj,
				HcDateFormatUtils.DATEFORMAT_YYYY_MM_DD_HH_MM_SS,
				simplePropertyPreFilter, features);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param dateFormat
	 *            dateFormat
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj, String dateFormat) {
		return toJsonString(obj, dateFormat, new SimplePropertyPreFilter());
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param features
	 *            features
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj, String dateFormat,
			SerializerFeature... features) {
		JSON.DEFFAULT_DATE_FORMAT = dateFormat;
		return toJsonString(obj, dateFormat, null, features);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param dateFormat
	 *            dateFormat
	 * @param filter
	 *            filter
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj, String dateFormat,
			SimplePropertyPreFilter filter) {
		return toJsonString(obj, dateFormat, filter,
				SerializerFeature.DisableCircularReferenceDetect,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.SortField);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param dateFormat
	 *            dateFormat
	 * @param filter
	 *            filter
	 * @param features
	 *            features
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonString(Object obj, String dateFormat,
			SimplePropertyPreFilter filter, SerializerFeature... features) {
		JSON.DEFFAULT_DATE_FORMAT = dateFormat;
		return JSON.toJSONString(obj, filter, features);
	}

	/**
	 * json转字符串
	 *
	 * @param obj
	 *            obj
	 * @param dateFormat
	 *            dateFormat
	 * @return 字符串
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonStringNoNull(Object obj, String dateFormat) {
		return toJsonString(obj, dateFormat,
				SerializerFeature.DisableCircularReferenceDetect,
				SerializerFeature.WriteDateUseDateFormat);
	}

	/**
	 * json转字符串
	 * 
	 * @param obj
	 *            obj
	 * @return java.lang.String
	 * @author 刘钢
	 * @since v1.0.1
	 */
	public static String toJsonStringNoNull(Object obj) {
		return toJsonString(obj,
				SerializerFeature.DisableCircularReferenceDetect,
				SerializerFeature.WriteDateUseDateFormat);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy