org.voovan.tools.json.JSONEncode Maven / Gradle / Ivy
package org.voovan.tools.json;
import org.voovan.Global;
import org.voovan.tools.TDateTime;
import org.voovan.tools.TEnv;
import org.voovan.tools.TObject;
import org.voovan.tools.TString;
import org.voovan.tools.collection.IntKeyMap;
import org.voovan.tools.hashwheeltimer.HashWheelTask;
import org.voovan.tools.reflect.TReflect;
import org.voovan.tools.security.THash;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* JSON打包类
*
* @author helyho
*
* Voovan Framework.
* WebSite: https://github.com/helyho/Voovan
* Licence: Apache v2 License
*/
public class JSONEncode {
public final static boolean JSON_CACHE_ENABLE = TEnv.getSystemProperty("JsonCacheEnable", true);
public final static IntKeyMap JSON_ENCODE_CACHE = new IntKeyMap(1024);
static {
if(JSON_CACHE_ENABLE) {
Global.getHashWheelTimer().addTask(new HashWheelTask() {
@Override
public void run() {
JSON_ENCODE_CACHE.clear();
}
}, 1);
}
}
/**
* 分析自定义对象为JSON字符串
*
* @param object 自定义对象
* @return JSON字符串
* @throws ReflectiveOperationException
*/
private static String complexObject(Object object, boolean allField) throws ReflectiveOperationException {
return mapObject(TReflect.getMapfromObject(object, allField), allField);
}
/**
* 分析Map对象为JSON字符串
*
* @param mapObject map对象b
* @return JSON字符串
* @throws ReflectiveOperationException
*/
private static String mapObject(Map, ?> mapObject, boolean allField) throws ReflectiveOperationException {
StringBuilder contentStringBuilder = new StringBuilder(Global.STR_LC_BRACES);
for (Object mapkey : mapObject.keySet()) {
String key = fromObject(mapkey, allField);
String Value = fromObject(mapObject.get(mapkey), allField);
String wrapQuote = key.startsWith(Global.STR_QUOTE) && key.endsWith(Global.STR_QUOTE) ? Global.EMPTY_STRING : Global.STR_QUOTE;
contentStringBuilder.append(wrapQuote);
contentStringBuilder.append(key);
contentStringBuilder.append(wrapQuote);
contentStringBuilder.append(Global.STR_COLON);
contentStringBuilder.append(Value);
contentStringBuilder.append(Global.STR_COMMA);
}
if (contentStringBuilder.length()>1){
contentStringBuilder.setLength(contentStringBuilder.length() - 1);
}
contentStringBuilder.append(Global.STR_RC_BRACES);
return contentStringBuilder.toString();
}
/**
* 分析Array对象为JSON字符串
*
* @param arrayObject Array对象
* @throws ReflectiveOperationException
* @return JSON字符串
*/
private static String arrayObject(Object[] arrayObject, boolean allField) throws ReflectiveOperationException {
StringBuilder contentStringBuilder = new StringBuilder(Global.STR_LS_BRACES);
for (Object object : arrayObject) {
String Value = fromObject(object, allField);
contentStringBuilder.append(Value);
contentStringBuilder.append(Global.STR_COMMA);
}
if (contentStringBuilder.length()>1){
contentStringBuilder.setLength(contentStringBuilder.length() - 1);
}
contentStringBuilder.append(Global.STR_RS_BRACES);
return contentStringBuilder.toString();
}
/**
* 分析Collection对象为JSON字符串
*
* @param collectionObject Collection 对象
* @throws ReflectiveOperationException
* @return JSON字符串
*/
private static String CollectionObject(Collection collectionObject, boolean allField) throws ReflectiveOperationException {
StringBuilder contentStringBuilder = new StringBuilder(Global.STR_LS_BRACES);
for (Object object : collectionObject) {
String Value = fromObject(object, allField);
contentStringBuilder.append(Value);
contentStringBuilder.append(Global.STR_COMMA);
}
if (contentStringBuilder.length()>1){
contentStringBuilder.setLength(contentStringBuilder.length() - 1);
}
contentStringBuilder.append(Global.STR_RS_BRACES);
return contentStringBuilder.toString();
}
/**
* 将对象转换成JSON字符串
*
* @param object 要转换的对象
* @return 类型:String 对象对应的JSON字符串
* @throws ReflectiveOperationException 反射异常
*/
@SuppressWarnings("unchecked")
public static String fromObject(Object object) throws ReflectiveOperationException {
return fromObject(object, false);
}
/**
* 将对象转换成JSON字符串
*
* @param object 要转换的对象
* @param allField 是否处理所有属性
* @return 类型:String 对象对应的JSON字符串
* @throws ReflectiveOperationException 反射异常
*/
@SuppressWarnings("unchecked")
public static String fromObject(Object object, boolean allField) throws ReflectiveOperationException {
String value = null;
int jsonHash = Objects.hash(object, allField);
if(JSON_CACHE_ENABLE) {
value = JSON_ENCODE_CACHE.get(jsonHash);
if (value != null) {
return value;
}
}
if (object == null) {
return "null";
}
Class clazz = object.getClass();
if (object instanceof Class) {
return "\"" + ((Class)object).getCanonicalName() + "\"";
} else if (object instanceof BigDecimal) {
value = ((BigDecimal) object).stripTrailingZeros().toPlainString();
} else if (object instanceof Date) {
value = "\"" + TDateTime.format(((Date)object), TDateTime.STANDER_DATETIME_TEMPLATE)+ "\"";;
} else if (object instanceof Map) {
Map