1. 指定某个类型序列化
问题描述:
JavaScript使用IEEE 754标准
1位用来表示符号位 11位用来表示指数 52位表示尾数
javaScript对于整形只能识别52位。遇到Long大整数可能会丢失数据
因为后台是Java写的难免有属性的类型是long。本来项目的做法是手动写每个类的toString();方法要拼接很多双引号。比较费劲也容易出错。
后来我使用了FastJson定制序列化遇到Long就写字符串来解决返回给客户端的JSON。
主要代码如下:
定制Long类型的序列化
public final static SerializeConfig resultJsonConfig = new SerializeConfig();
static{
resultJsonConfig.put(Long.class,new ObjectSerializer() {
@Override
public void write(JSONSerializer serializer,Object object,Object fieldName,Type fieldType,int features)
throws IOException {
SerializeWriter out = serializer.getWriter();
out.writeString(Objects.toString(object,null));
}
});
}
使用:
protected <T> String getResult(T value) {
JSONObject json = new JSONObject();
json.put("state",state.getState());
json.put("desc",state.getDesc());
json.put("value",value);
return JSON.toJSONString(json,resultJsonConfig,SerializerFeature.DisableCircularReferenceDetect);
}
2.如果要让这个策略全局生效
SerializeConfig.getGlobalInstance().put(Long.class,int features)
throws IOException {
SerializeWriter out = serializer.getWriter();
if (fieldType == long.class || fieldType == Long.class) {
out.writeString(Objects.toString(object,null));
}
// if (numberValue == null) {
// if (out.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
// out.write('0');
// } else {
// out.writeNull();
// }
// return;
// }
}
});
2. 对某些属性不序列化
2.1 定义一个注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ResultNotSerialize {
}
2.2 将注解加在属性上
public class MemberVO {
private String memberId;
private String memberName;
@ResultNotSerialize
private String password;
2.3 定义一个过滤器
public static final PropertyFilter controllerResultJsonFilter = new PropertyFilter() {
@Override
public boolean apply(Object object,String key,Object value) {
try {
Field field = object.getClass().getDeclaredField(key);
if(null !=field && null != field.getAnnotation(ResultNotSerialize.class)){
return false;
}
return true;
} catch (Exception e) {
return true;
}
}
};
2.4 如何使用
SON.toJSONString(obj,controllerResultJsonFilter);
3. Java长ID,js接收丢失数据问题最终的解决办法
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface SerializeAsSting {
}
public class MemberVO {
@SerializeAsSting
private Long id = 0L;//long type id
public static final PropertyFilter resultJsonPropertyFilter = new PropertyFilter() {
@Override
public boolean apply(Object object,Object value) {
try {
Field field = object.getClass().getDeclaredField(key);
if (null != field && null != field.getAnnotation(NotSerialize.class)) {
return false;
}
return true;
} catch (Exception e) {
return true;
}
}
};
public static final ValueFilter resultValueFilter = new ValueFilter() {
@Override
public Object process(Object object,String name,Object value) {
// LogCore.BASE.info("object={},name={},value={}",object,name,value);
if(value == null){
return value;
}
try {
Field field = object.getClass().getDeclaredField(name);
if (null != field && null != field.getAnnotation(SerializeAsSting.class)) {
return value.toString();
}
return value;
} catch (Exception e) {
return value;
}
}
};
public final static SerializeFilter[] resultJsonFilters = new SerializeFilter[]{resultJsonPropertyFilter,resultValueFilter};
protected <T> String getResult(ErrorState state,T value) {
JSONObject json = new JSONObject();
json.put("state",state.getState());
json.put("desc",state.getDesc());
json.put("value",value);
return JSON.toJSONString(json,resultJsonFilters,SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect);
}
其他的序列化见这篇文章fastjson使用@JSONField、@JSONType、SerializeFilter定制序列化