博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FastJSON学习
阅读量:4497 次
发布时间:2019-06-08

本文共 4507 字,大约阅读时间需要 15 分钟。

这几天在用FastJSON,发现需要测试一些关键点,包括:

1、是否支持内部类:测试结果是支持,但是需要设置为静态类(static)

2、是否支持继承的自动序列化及反序列化:测试结果是支持

3、缺字段或者多出字段时,反序列化(JSON.parseObject)是否会崩溃:测试结果是不会,对应的成员会保持默认值

 

下面贴出程序:

 

package com.test.fastJSON;import java.io.Serializable;import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;public class Main {    //父类    static class Parent implements Serializable {        private static final long serialVersionUID = 1L;                private String title;        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }    }    //子类1    static class Child1 extends Parent implements Serializable  {        private static final long serialVersionUID = 1L;                private List
extra; private int index = -1; public List
getExtra() { return extra; } public void setExtra(List
extra) { this.extra = extra; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } } //子类2 static class Child2 extends Parent implements Serializable { private static final long serialVersionUID = 1L; private ExtraStruct extra; public ExtraStruct getExtra() { return extra; } public void setExtra(ExtraStruct extra) { this.extra = extra; } } //子类2嵌套类 static class ExtraStruct implements Serializable { private static final long serialVersionUID = 1L; private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } /** * @param args */ public static void main(String[] args) { //测试序列化 Child1 c1 = new Child1(); c1.setTitle("c1"); c1.setIndex(123); c1.setExtra(new ArrayList
()); c1.getExtra().add("111"); c1.getExtra().add("222"); Child2 c2 = new Child2(); c2.setTitle("c2"); c2.setExtra(new ExtraStruct()); c2.getExtra().setKey("k"); c2.getExtra().setValue("v"); System.out.println(JSON.toJSONString(c1)); System.out.println(JSON.toJSONString(c2)); System.out.println("======================"); //测试反序列化 String s1 = "{\"extra\":[\"111\",\"222\"],\"title\":\"c1\",\"index\":123}"; String s2 = "{\"extra\":{\"key\":\"k\",\"value\":\"v\"},\"title\":\"c2\"}"; Child1 cc1 = JSON.parseObject(s1, Child1.class); Child2 cc2 = JSON.parseObject(s2, Child2.class); System.out.println(JSON.toJSONString(cc1)); System.out.println(JSON.toJSONString(cc2)); System.out.println("======================"); //测试缺字段情况 s1 = "{\"extra\":[\"111\",\"222\"]}"; s2 = "{\"extra\":{\"key\":\"k\"},\"title\":\"c2\"}"; cc1 = JSON.parseObject(s1, Child1.class); cc2 = JSON.parseObject(s2, Child2.class); System.out.println(JSON.toJSONString(cc1)); System.out.println(JSON.toJSONString(cc2)); System.out.println("======================"); //测试多出字段情况 s1 = "{\"extra\":[\"111\",\"222\"],\"title\":\"c1\",\"tt\":\"111\"}"; s2 = "{\"extra\":{\"key\":\"k\",\"value\":\"v\",\"tt\":\"111\"},\"title\":\"c2\"}"; cc1 = JSON.parseObject(s1, Child1.class); cc2 = JSON.parseObject(s2, Child2.class); System.out.println(JSON.toJSONString(cc1)); System.out.println(JSON.toJSONString(cc2)); System.out.println("======================"); } }

 

 

 

下面是运行结果:

{"extra":["111","222"],"index":123,"title":"c1"}{"extra":{"key":"k","value":"v"},"title":"c2"}======================{"extra":["111","222"],"index":123,"title":"c1"}{"extra":{"key":"k","value":"v"},"title":"c2"}======================{"extra":["111","222"],"index":-1}{"extra":{"key":"k"},"title":"c2"}======================{"extra":["111","222"],"index":-1,"title":"c1"}{"extra":{"key":"k","value":"v"},"title":"c2"}======================

 

转载于:https://www.cnblogs.com/alexcai/p/4368545.html

你可能感兴趣的文章
string成员函数
查看>>
onSaveInstanceState()方法问题
查看>>
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>
大数据时代侦查机制有哪些改变
查看>>
雷林鹏分享:jQuery EasyUI 菜单与按钮 - 创建链接按钮
查看>>
Apache Traffic Server服务搭建
查看>>
poj1990两个树状数组
查看>>
学习python-day1
查看>>
Zend_Db_Table->insert ()和zend_db_adapter::insert方法返回值不同
查看>>
递归问题
查看>>
Hyperledger下子项目
查看>>
Linq-查询上一条下一条
查看>>
常见前端开发的题目,可能对你有用
查看>>
BeautifulSoap库入门
查看>>
乐观锁与悲观锁
查看>>
Codeforces Round #328 (Div. 2)D. Super M 虚树直径
查看>>
Java判断是否为移动端
查看>>
chromedriver下载链接以及对应版本
查看>>
[SimplePlayer] 6. 音频同步
查看>>
把一个SVN项目的目录结构 导入到另外一个空白的SVN项目里
查看>>