Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.webapp.utils.json;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.AfterFilter;
import com.alibaba.fastjson.serializer.BeforeFilter;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.PropertyPreFilter;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.webapp.utils.string.Utils;
/**
* JSON序列号工具完整版
*/
public final class JSONUtils {
private Object jsonObj;
private JSONSerializer jsonSerializer;
private static final ThreadLocal local = new ThreadLocal();
private JSONUtils(Object object) {
this.jsonObj = object;
jsonSerializer = new JSONSerializer(new SerializeConfig());
}
/* setData **/
private JSONUtils setData(Object jsonObj, JSONSerializer jsonSerializer) {
this.jsonObj = jsonObj;
this.jsonSerializer = jsonSerializer;
return this;
}
/* local data **/
private static JSONUtils localData(Object object) {
if (local.get() == null) local.set(new JSONUtils(object));
return local.get().setData(object, new JSONSerializer(new SerializeConfig()));
}
public static JSONUtils of(Object object) {
return localData(object);
}
/*
* For example
*
JSONUtils.toString(object) - String
* @param object
* @return JSON.toJSONString(object)
*/
public static String toString(Object object) {
return JSON.toJSONString(object);
}
/*
* For example
*
* Obj class {int user_id=1;}
* JSONUtils.of(new Obj()).toCamelKey() - {userId:1}
* @return Change after the json string
*/
public String toCamelKey() {
jsonSerializer.getNameFilters().add(new NameFilter() {
public String process(Object object, String name, Object value) {
return Utils.toCamel(name);
}
});
return toString();
}
/*
* For example
*
* Obj class {int userId=1;}
* JSONUtils.of(new Obj()).toPascalKey() - {UserId:1}
* @return Change after the json string
*/
public String toPascalKey() {
jsonSerializer.getNameFilters().add(new PascalNameFilter());
return toString();
}
/*
* For example
*
* Obj class {int userId=1;}
* JSONUtils.of(new Obj()).toSnakeKey() - {user_id:1}
* @return Change after the json string
*/
public String toSnakeKey() {
jsonSerializer.getNameFilters().add(new NameFilter() {
public String process(Object object, String name, Object value) {
return Utils.toSnake(name);
}
});
return toString();
}
/*
* @return json string
*/
public String toString() {
jsonSerializer.write(jsonObj);
return jsonSerializer.toString();
}
/*
* @return format json string
*/
public String toPrettyString() {
jsonSerializer.config(SerializerFeature.PrettyFormat, true);
return toString();
}
/*
* For example
*
* @return value array
*/
public String toBeanToArray() {
jsonSerializer.config(SerializerFeature.BeanToArray, true);
return toString();
}
/*
* For example
*
* @return this
*/
public JSONUtils include(String... includes) {
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(includes);
jsonSerializer.getPropertyPreFilters().add(filter);
return this;
}
/*
* For example
*
* @return this
*/
public JSONUtils exclude(String... excludes) {
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().addAll(Arrays.asList(excludes));
jsonSerializer.getPropertyPreFilters().add(filter);
return this;
}
/*
* For example
*
* @return this
*/
public JSONUtils toNullNumAsZero() {
jsonSerializer.config(SerializerFeature.WriteNullNumberAsZero, true);
return this;
}
/*
* For example
*
* @return this
*/
public JSONUtils toNullBoolAsFalse() {
jsonSerializer.config(SerializerFeature.WriteNullBooleanAsFalse, true);
return this;
}
/*
* For example
*
* @return this
*/
public JSONUtils toMapNullValue() {
jsonSerializer.config(SerializerFeature.WriteMapNullValue, true);
return this;
}
/*
* For example
*