shz.spring.JsonHelp Maven / Gradle / Ivy
The newest version!
package shz.spring;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.*;
import shz.core.NullHelp;
import shz.core.ToSet;
import shz.core.constant.ArrayConstant;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
@SuppressWarnings("unchecked")
public final class JsonHelp {
private JsonHelp() {
throw new IllegalStateException();
}
public static Filter simpleByIncludes(String... properties) {
return new SimplePropertyPreFilter(ToSet.asSet(properties), null);
}
public static Filter simpleByExcludes(String... properties) {
return new SimplePropertyPreFilter(null, ToSet.asSet(properties));
}
public static Filter simpleByProperties(Set includes, Set excludes) {
return new SimplePropertyPreFilter(includes, excludes);
}
static final class SimplePropertyPreFilter implements PropertyPreFilter {
final Set includes;
final Set excludes;
SimplePropertyPreFilter(Set includes, Set excludes) {
this.includes = includes;
this.excludes = excludes;
}
@Override
public boolean process(JSONWriter jsonWriter, Object source, String name) {
if (source == null) return true;
if (excludes != null && excludes.contains(name)) return false;
return includes == null || includes.contains(name);
}
}
public static Filter simpleNameFilter(Map nameMapper) {
return (NameFilter) (source, name, value) -> nameMapper.get(name);
}
public static Filter simpleValueFilter(BiConsumer valueUpdater) {
return (ValueFilter) (source, name, value) -> {
valueUpdater.accept(name, value);
return value;
};
}
public static String toJSONString(Object obj, Set includes, Set excludes,
Map nameMapper, BiConsumer valueUpdater,
com.alibaba.fastjson2.JSONWriter.Feature... features) {
List filters = new ArrayList<>();
if (NullHelp.nonEmpty(includes) && NullHelp.nonEmpty(excludes))
filters.add(simpleByProperties(includes, excludes));
if (NullHelp.nonEmpty(nameMapper)) filters.add(simpleNameFilter(nameMapper));
if (valueUpdater != null) filters.add(simpleValueFilter(valueUpdater));
if (filters.size() == 0) return JSON.toJSONString(obj, features);
if (filters.size() == 1) return JSON.toJSONString(obj, filters.get(0), features);
return JSON.toJSONString(obj, filters.toArray(new Filter[0]), features);
}
public static String toExcludeJSONString(Object obj, Set excludes, com.alibaba.fastjson2.JSONWriter.Feature... features) {
return toJSONString(obj, null, excludes, null, null, features);
}
public static String toIncludeJSONString(Object obj, Set includes, com.alibaba.fastjson2.JSONWriter.Feature... features) {
return toJSONString(obj, includes, null, null, null, features);
}
public static byte[] toJSONBytes(Object obj, Set includes, Set excludes, Map nameMapper, BiConsumer valueUpdater) {
List filters = new ArrayList<>();
if (NullHelp.nonEmpty(includes) && NullHelp.nonEmpty(excludes))
filters.add(simpleByProperties(includes, excludes));
if (NullHelp.nonEmpty(nameMapper)) filters.add(simpleNameFilter(nameMapper));
if (valueUpdater != null) filters.add(simpleValueFilter(valueUpdater));
if (filters.size() == 0) return JSON.toJSONBytes(obj);
if (filters.size() == 1) return JSON.toJSONBytes(obj, filters.get(0));
return JSON.toJSONBytes(obj, filters.toArray(new Filter[0]));
}
public static byte[] toExcludeJSONBytes(Object obj, Set excludes) {
return toJSONBytes(obj, null, excludes, null, null);
}
public static byte[] toIncludeJSONBytes(Object obj, Set includes) {
return toJSONBytes(obj, includes, null, null, null);
}
private static final Map> CLASS_CACHE = new ConcurrentHashMap<>(128);
public static byte[] toJSONBytes(T t, Function toJSONBytes) {
if (toJSONBytes == null || t == null) return ArrayConstant.EMPTY_BYTE_ARRAY;
byte[] data = toJSONBytes.apply(t);
Class> cls = t.getClass();
String name = cls.getName();
CLASS_CACHE.putIfAbsent(name, cls);
int nameLen = name.length();
int dataLen = data.length;
byte[] bytes = new byte[19 + nameLen + dataLen];
bytes[0] = 123;
bytes[1] = 34;
bytes[2] = 116;
bytes[3] = 121;
bytes[4] = 112;
bytes[5] = 101;
bytes[6] = 34;
bytes[7] = 58;
bytes[8] = 34;
System.arraycopy(name.getBytes(), 0, bytes, 9, nameLen);
int i = nameLen + 9;
bytes[i++] = 34;
bytes[i++] = 44;
bytes[i++] = 34;
bytes[i++] = 100;
bytes[i++] = 97;
bytes[i++] = 116;
bytes[i++] = 97;
bytes[i++] = 34;
bytes[i++] = 58;
System.arraycopy(data, 0, bytes, i, dataLen);
bytes[bytes.length - 1] = 125;
return bytes;
}
public static T parseObject(byte[] bytes, BiFunction, T> parseObject) {
int len;
if (parseObject == null || bytes == null || (len = bytes.length) < 23) return null;
StringBuilder sb = new StringBuilder();
int i = 9;
for (; i < len; ++i) {
byte b = bytes[i];
if (b == 34) break;
sb.append((char) b);
}
Class cls = (Class) CLASS_CACHE.get(sb.toString());
if (cls == null) return null;
byte[] data = new byte[len - i - 10];
System.arraycopy(bytes, i + 9, data, 0, data.length);
return parseObject.apply(data, cls);
}
public static byte[] toJSONBytes(Object obj) {
return toJSONBytes(obj, JSON::toJSONBytes);
}
public static T parseObject(byte[] bytes) {
return parseObject(bytes, JSON::parseObject);
}
}