public static void main(String[] args) {
String aStr = "{\"value\":1L,\"set\":[1L,2B,3S,4]}";
Object bObj = JSON.parseObject(aStr);
String cStr = JSON.toJSONString(bObj);
Crab dCrab = JSON.parseObject(cStr, Crab.class);
System.out.println(dCrab.getSet().contains(1L));
}
public static class Crab {
private Object value;
private Set
以上代码简述:
A发出JSON字符串aStr
B不关心(也是不知道)类型,直接粗暴的转换成bObj
C也不管是啥,再转成cStr
D最后到了D这里,发现D也就只有外壳是具体知道类型的,里面放的value和set都是不关心内容类型
最后,使用的来了,使用的人想知道这个set里面有没有Long类型的1L
答案是否定的,因为传输过程中的类型早已丢失变成了Integer
在这个过程中,BCD实际上都是传输者,他们不知道传输东西的类型,更不知道要干什么,他们BC只管传输,D虽然是最后的接收,但是类型都是泛泛的,直到最后的使用set.contains()才确定了具体的业务
此文将以Long,Byte,Short的传输类型丢失为原型,介绍如何解决问题
(1).因为B环节用的是最粗暴的Object去接收,后续又用具体类型Crab去接受,因此使用SerializerFeature.WriteClassName明确对象类型的方法也就不行了。
(2).考虑到传输过程中可以使用L,S,B等后缀保证类型,但是要想toJSONString()的时候只有这几个后缀被加上,其他多余类型剔除,就可以了。
(3).在仿写的过程中,注意到SerializerFeature里的枚举是使用位运算进行确认序列化方式的,SerializerFeature使用的是int的mask,而现在已经有30个枚举,因此也就只有两个可用的位位置。
备注:
(1).此文仅以Long举例,其他Number使用相同方法提供类扩展就可以。
(2).List类在传输过程中会变成JSONArray,因此JSONArray也需要更改
(3).本文基于fastjson版本->1.2.60
package com.kowalski;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.JavaBeanSerializer;
import com.alibaba.fastjson.serializer.LongCodec;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerialContext;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.util.TypeUtils;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
/**
* @author kowalski
*/
public class TestJson {
static{
FastJsonConfig fastJsonConfig = new FastJsonConfig();
/**Long的Codec和JSONArray的Serializer 使用自己的*/
SerializeConfig serializeConfig = fastJsonConfig.getSerializeConfig();
serializeConfig.put(Long.class, MyLongCodec.instance);
serializeConfig.put(JSONArray.class, MyListSerializer.getInstance());
}
public static void main(String... args) {
String aStr = "{\"value\":1L,\"set\":[1L,2B,3S,4]}";
Object bObj = JSON.parseObject(aStr);
String cStr = JSON.toJSONString(bObj, 1 << 30 | JSON.DEFAULT_GENERATE_FEATURE);
Crab dCrab = JSON.parseObject(cStr, Crab.class);
System.out.println(dCrab.getSet().contains(1L));
}
public static class Crab {
private Object value;
private Set set;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
}
public static final class MyLongCodec extends LongCodec {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull(SerializerFeature.WriteNullNumberAsZero);
} else {
long value = (Long) object;
out.writeLong(value);
/**1<<30是用来避开已经拥有的SerializerFeature(已有的已经到1<<29)*/
boolean isWiteL =
(out.isEnabled(1 << 30) || out.isEnabled(SerializerFeature.WriteClassName)) && value <= Integer.MAX_VALUE
&& value >= Integer.MIN_VALUE && fieldType != Long.class && fieldType != long.class;
if (isWiteL) {
out.write('L');
}
}
}
}
public static final class MyListSerializer implements ObjectSerializer {
private static final MyListSerializer INSTANCE = new MyListSerializer();
public static MyListSerializer getInstance() {
return INSTANCE;
}
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
throws IOException {
boolean isWiteL = serializer.out.isEnabled(1 << 30) || ((features & 1 << 30) != 0) || (
serializer.out.isEnabled(SerializerFeature.WriteClassName)
|| (features & SerializerFeature.WriteClassName.mask) != 0);
SerializeWriter out = serializer.out;
Type elementType = null;
if (isWiteL) {
elementType = TypeUtils.getCollectionItemType(fieldType);
}
if (object == null) {
out.writeNull(SerializerFeature.WriteNullListAsEmpty);
return;
}
List> list = (List>) object;
if (list.isEmpty()) {
out.append("[]");
return;
}
SerialContext context = serializer.getContext();
serializer.setContext(context, object, fieldName, 0);
try {
ObjectSerializer itemSerializer;
if (out.isEnabled(SerializerFeature.PrettyFormat)) {
out.append('[');
serializer.incrementIndent();
int i = 0;
for (Object item : list) {
if (i != 0) {
out.append(',');
}
serializer.println();
if (item != null) {
if (serializer.containsReference(item)) {
serializer.writeReference(item);
} else {
itemSerializer = serializer.getObjectWriter(item.getClass());
serializer.setContext(new SerialContext(context, object, fieldName, 0, 0));
itemSerializer.write(serializer, item, i, elementType, features);
}
} else {
serializer.out.writeNull();
}
i++;
}
serializer.decrementIdent();
serializer.println();
out.append(']');
return;
}
out.append('[');
for (int i = 0, size = list.size(); i < size; ++i) {
Object item = list.get(i);
if (i != 0) {
out.append(',');
}
if (item == null) {
out.append("null");
} else {
Class> clazz = item.getClass();
if (clazz == Integer.class) {
out.writeInt((Integer) item);
} else if (clazz == Long.class) {
long val = (Long) item;
out.writeLong(val);
if (isWiteL) {
out.write('L');
}
} else {
if ((SerializerFeature.DisableCircularReferenceDetect.mask & features) == 0) {
serializer.setContext(new SerialContext(context, object, fieldName, 0, 0));
if (serializer.containsReference(item)) {
serializer.writeReference(item);
} else {
itemSerializer = serializer.getObjectWriter(item.getClass());
if ((SerializerFeature.WriteClassName.mask & features) != 0
&& itemSerializer instanceof JavaBeanSerializer) {
JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) itemSerializer;
javaBeanSerializer.writeNoneASM(serializer, item, i, elementType, features);
} else {
itemSerializer.write(serializer, item, i, elementType, features);
}
}
} else {
itemSerializer = serializer.getObjectWriter(item.getClass());
itemSerializer.write(serializer, item, i, elementType, features);
}
}
}
}
out.append(']');
} finally {
serializer.setContext(context);
}
}
}
}
完结~ 撒花~
有更好方案的小伙伴还望不吝赐教~
E:[email protected]