
com.webapp.utils.json.JSONUtils Maven / Gradle / Ivy
The newest version!
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.JSONObject;
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();
}
public JSONObject toJSONObject() {
return JSON.parseObject(toString());
}
/*
* @return format json string
*/
public String toPrettyString() {
jsonSerializer.config(SerializerFeature.PrettyFormat, true);
return toString();
}
/*
* For example
*
* Obj class {int id1=1, id2=2;}
* JSONUtils.of(new Obj()).toBeanToArray() - [1,2]
* @return value array
*/
public String toBeanToArray() {
jsonSerializer.config(SerializerFeature.BeanToArray, true);
return toString();
}
/*
* For example
*
* Obj class {int id=1;}
* JSONUtils.of(new Obj()).before("b", "v").toString() - {b:v, id:1}
* @return this
*/
public JSONUtils before(final String key, final Object value) {
jsonSerializer.getBeforeFilters().add(new BeforeFilter() {
public void writeBefore(Object object) {
this.writeKeyValue(key, value);
}
});
return this;
}
/*
* For example
*
* Obj class {int id=1;}
* JSONUtils.of(new Obj()).after("a", "v").toString() - {id:1, a:f}
* @return this
*/
public JSONUtils after(final String key, final Object value) {
jsonSerializer.getAfterFilters().add(new AfterFilter() {
public void writeAfter(Object object) {
this.writeKeyValue(key, value);
}
});
return this;
}
/*
* For example
*
* Obj class {int id=1; String name="2";}
* JSONUtils.of(new Obj()).include("id").toString() - {id:1}
* @return this
*/
public JSONUtils include(String... includes) {
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(includes);
jsonSerializer.getPropertyPreFilters().add(filter);
return this;
}
/*
* For example
*
* Obj class {int id=1; String name="2";}
* JSONUtils.of(new Obj()).exclude("name").toString() - {id:1}
* @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
*
* Obj class {int id=1;}
* JSONUtils.of(new Obj()).modifyKey("id", "ok").toString() - {ok:1}
* @return this
*/
public JSONUtils modifyKey(final String key, final String replacement) {
jsonSerializer.getNameFilters().add(new NameFilter() {
public String process(Object object, String name, Object value) {
return key.equals(name) ? replacement : name;
}
});
return this;
}
/*
* For example
*
* Obj class {int id=1;}
* JSONUtils.of(new Obj()).modifyVal("id", "2").toString() - {id:2}
* @return this
*/
public JSONUtils modifyVal(final String key, final Object replacement) {
jsonSerializer.getValueFilters().add(new ValueFilter() {
String orgKey = key;
List nameFilters = jsonSerializer.getNameFilters();
public Object process(Object object, String name, Object value) {
for(NameFilter filter : nameFilters){
orgKey = filter.process(object, orgKey, value);
}
return orgKey.equals(name) ? replacement : value;
}
});
return this;
}
/*
* For example
*
* Obj class {int id=1;}
* JSONUtils.of(new Obj()).modifyVal("id", "\d", "2").toString() - {id:2}
* @return this
*/
public JSONUtils modifyVal(final String key, final String regexVal, final String replacement) {
jsonSerializer.getValueFilters().add(new ValueFilter() {
String orgKey = key;
List nameFilters = jsonSerializer.getNameFilters();
public Object process(Object object, String name, Object value) {
for(NameFilter filter : nameFilters){
orgKey = filter.process(object, orgKey, value);
}
if(orgKey.equals(name) && String.valueOf(value).matches(regexVal)){
value = replacement;
}
return value;
}
});
return this;
}
/*
* For example
*
* Obj class {int id=1;}
* JSONUtils.of(new Obj()).filterVal("id", "\d").toString() - {id:2}
* @return this
* @tag label
* name = "ffff"
* comment = "fefefe"
*/
public JSONUtils filterVal(final String key, final String regexVal) {
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object object, String name, Object value) {
return key.equals(name) ? String.valueOf(value).matches(regexVal) :true;
}
};
jsonSerializer.getPropertyFilters().add(filter);
return this;
}
/*
* For example
* JSONUtils.of(object).dateFormat("yyyy-MM-dd HH:mm:ss").toString() - {date:2000-10-10 10:10:10}
* @return this
*/
public JSONUtils dateFormat(String format) {
jsonSerializer.getMapping().put(Date.class, new SimpleDateFormatSerializer(format));
// jsonSerializer.setDateFormat(format);
return this;
}
public JSONUtils doubleFormat(final String pattern, final Class clz){
jsonSerializer.getValueFilters().add(new ValueFilter() {
DecimalFormat format = new DecimalFormat(pattern);
List nameFilters = jsonSerializer.getNameFilters();
public Object process(Object object, String name, Object value) {
Field[] fields = clz.getDeclaredFields();
for(Field field : fields){
if(field.getType().getSimpleName().equalsIgnoreCase(double.class.getSimpleName())){
String key = field.getName();
for(NameFilter filter : nameFilters){
key = filter.process(null, key, null);
}
if(key.equals(name)) return format.format(value);
}
}
return value;
}
});
return this;
}
/*
* For example
*
* Obj class {String nation="中国";}
* JSONUtils.of(new Obj()).toCompatible().toString() - {nation:\u4E2D\u56FD}
*
* @return this
*/
public JSONUtils toCompatible() {
jsonSerializer.config(SerializerFeature.BrowserCompatible, true);
return this;
}
public JSONUtils toSort() {
jsonSerializer.config(SerializerFeature.SortField, true);
return this;
}
/*
* For example
*
* Obj class {int i;}
* JSONUtils.of(new Obj()).toNullNumAsZero().toString() - {i:0}
*
* @return this
*/
public JSONUtils toNullNumAsZero() {
jsonSerializer.config(SerializerFeature.WriteNullNumberAsZero, true);
return this;
}
/*
* For example
*
* Obj class {boolean i;}
* JSONUtils.of(new Obj()).toNullBoolAsFalse().toString() - {i:false}
*
* @return this
*/
public JSONUtils toNullBoolAsFalse() {
jsonSerializer.config(SerializerFeature.WriteNullBooleanAsFalse, true);
return this;
}
/*
* For example
*
* Obj class {String i;}
* JSONUtils.of(new Obj()).toNullStrAsEmpty().toString() - {}
*
* @return this
*/
public JSONUtils toNullStrAsEmpty() {
jsonSerializer.config(SerializerFeature.WriteNullStringAsEmpty, false);
return this;
}
public JSONUtils toNonStrKeyAsStr() {
jsonSerializer.config(SerializerFeature.WriteNonStringKeyAsString, true);
return this;
}
/*
* For example
*
* Obj class {Map i;}
* JSONUtils.of(new Obj()).toMapNullValue().toString() - {}
*
* @return this
*/
public JSONUtils toMapNullValue() {
jsonSerializer.config(SerializerFeature.WriteMapNullValue, true);
return this;
}
/*
* For example
*
* Obj class {List i;}
* JSONUtils.of(new Obj()).toNullListAsEmpty().toString() - {}
*
* @return this
*/
public JSONUtils toNullListAsEmpty() {
jsonSerializer.config(SerializerFeature.WriteNullListAsEmpty, true);
return this;
}
public JSONUtils addNameFilter(NameFilter filter) {
jsonSerializer.getNameFilters().add(filter);
return this;
}
public JSONUtils addValueFilters(ValueFilter filter) {
jsonSerializer.getValueFilters().add(filter);
return this;
}
public JSONUtils addPropertyFilter(PropertyFilter filter) {
jsonSerializer.getPropertyFilters().add(filter);
return this;
}
public JSONUtils addPropertyPreFilter(PropertyPreFilter filter) {
jsonSerializer.getPropertyPreFilters().add(filter);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy