FastJSON学习

这几天在用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<String> extra;
        
        private int index = -1;

        public List<String> getExtra() {
            return extra;
        }

        public void setExtra(List<String> 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<String>());
        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"}
======================

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。