import com.alibaba.fastjson.JSON;
import lombok.Data;
@Data
class a {
private int z;
private long x;
private double c;
}
@Data
class b {
private Integer z;
private Long x;
private Double c;
}
public class JavalongLong {
public static void main(String[] args) {
String xStr = "{x:1}";
String yStr = "{y:1}";
String zStr = "{z:1}";
System.out.println("========= 测试包装类反序列化默认值 =========");
System.out.println("========= test long =========");
System.out.println(JSON.parseObject(xStr, a.class));
System.out.println(JSON.parseObject(yStr, a.class));
System.out.println(JSON.parseObject(zStr, a.class));
System.out.println("========= test long =========");
System.out.println(JSON.parseObject(xStr, b.class));
System.out.println(JSON.parseObject(yStr, b.class));
System.out.println(JSON.parseObject(zStr, b.class));
/*
* ========= 测试包装类反序列化默认值 =========
========= test long =========
a(z=0, x=1, c=0.0)
a(z=0, x=0, c=0.0)
a(z=1, x=0, c=0.0)
========= test long =========
b(z=null, x=1, c=null)
b(z=null, x=null, c=null)
b(z=1, x=null, c=null)
* */
}
}
可以看出,使用fastJson
反序列化时,包装类的默认值是null
;而非包装类是0
。
网友评论