• 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"}
    ======================
  • 相关阅读:
    ADO.NET 2.调用存储过程
    Resharper上手指南
    获取HTML源码(只取文字,判断编码,过滤标签)
    .net(c#) winform文本框只能输入数字,不能其他非法字符(转)
    ADO.NET – 3.书籍管理系统详解
    GemBox.ExcelLite.dll导出到Excel
    C#4.0图解教程 第7章 类和继承
    C#读取网站HTML内容
    C#回顾 – 1.IO文件操作
    Javascript s10 (Jquery相关手册整合及功能实现)
  • 原文地址:https://www.cnblogs.com/alexcai/p/4368545.html
Copyright © 2020-2023  润新知