序列化和反序列化作用(存储数据)

版权声明:该博客SunTen丿所有,侵权必究 https://blog.csdn.net/weixin_38150130/article/details/82190809

序列化和反序列化作用(存储数据)

纯手工原创,讲得不够详细,凑合着看吧,不嫌弃的话可以点个赞回个评论哦,有什么不足的地方请指出,我会虚心学习的,谢谢
- 主要内容
- 序列化
- 反序列化


主要内容

最近在做一个手机棋牌游戏的项目,我负责的是java后台这一块,中间就遇到一个这样的问题---

游戏回放的json数据要存储到mysql
而一开始我是这样做的,直接把C++ POST过来的json数据用List< string > 来接收,然后转成 string字符串写进mysql里游戏回放字段(varchar类型),
游戏回放数据json格式:(主要部分)

[{
        "UserData_t": [{
            "Chu_Pai_Count": 0,
            "Gang_Pai_Count": 0,
            "HandPai": [4, 5, 6, 7, 8, 9, 11, 13, 14, 15, 16, 17, 18, 11],
            "Hand_Pai_Count": 14,
            "LaiZi_Count": 0,
            "Men_Pai_Count": 28,
            "Peng_Pai_Count": 0,
            "m_BeiShu": 1,
            "m_Final_Score": 0,
            "m_Part_Score": 0,
            "m_Player_ID": 101000,
            "m_Player_Location": 0
        }, {
            "Chu_Pai_Count": 0,
            "Gang_Pai_Count": 0,
            "HandPai": [1, 2, 4, 2, 2, 2, 6, 23, 24, 25, 26, 27, 28],
            "Hand_Pai_Count": 13,
            "LaiZi_Count": 0,
            "Men_Pai_Count": 26,
            "Peng_Pai_Count": 0,
            "m_BeiShu": 1,
            "m_Final_Score": 0,
            "m_Part_Score": 0,
            "m_Player_ID": 102000,
            "m_Player_Location": 1
        }, {
            "Chu_Pai_Count": 0,
            "Gang_Pai_Count": 0,
            "HandPai": [29, 16, 2, 4, 4, 5, 6, 7, 8, 9, 11, 12, 13],
            "Hand_Pai_Count": 13,
            "LaiZi_Count": 0,
            "Men_Pai_Count": 28,
            "Peng_Pai_Count": 0,
            "m_BeiShu": 1,
            "m_Final_Score": 0,
            "m_Part_Score": 0,
            "m_Player_ID": 103000,
            "m_Player_Location": 2
        }, {
            "Chu_Pai_Count": 0,
            "Gang_Pai_Count": 0,
            "HandPai": [14, 6, 16, 4, 4, 19, 21, 22, 23, 24, 25, 26, 27],
            "Hand_Pai_Count": 13,
            "LaiZi_Count": 0,
            "Men_Pai_Count": 26,
            "Peng_Pai_Count": 0,
            "m_BeiShu": 1,
            "m_Final_Score": 0,
            "m_Part_Score": 0,
            "m_Player_ID": 104000,
            "m_Player_Location": 3
        }],
        "m_ChaoTian": 19,
        "m_Dice_1": 6,
        "m_Dice_2": 2,
        "m_LaiZi": 11,
        "m_MenPai_EndIndex": 1,
        "m_MenPai_EndLoc": 1,
        "m_MenPai_LaiziIndex": 3,
        "m_MenPai_LaiziLoc": 1,
        "m_MenPai_StartIndex": 4,
        "m_MenPai_StartLoc": 3,
        "m_MenPai_ZhuangIndex": 2,
        "m_MenPai_ZhuangLoc": 1,
        "m_Zhuang_Location": 0
    }, {
        "Action": 1,
        "ChuPai": [19, 4],
        "NewHandPai": [5, 6, 7, 8, 9, 11, 11, 13, 14, 15, 16, 17, 18],
        "m_ChuPai": 4,
        "m_Player_Location": 0,
        "m_User_ID": 101000
    }, {
        "Action": 2,
        "NewHandPai": [2, 5, 6, 7, 8, 9, 11, 12, 13, 16, 29],
        "PengPai": [4],
        "m_Desk_ID": 120434,
        "m_PengPai": 4,
        "m_Player_Location": 2,
        "m_User_ID": 103000
    }, {
        "Action": 12,
        "ChuPai": [19],
        "NewHandPai": [5, 6, 7, 8, 9, 11, 11, 13, 14, 15, 16, 17, 18],
        "m_Desk_ID": 120434,
        "m_PengPai": 4,
        "m_Player_Location": 0,
        "m_User_ID": 101000
    }, {
        "Action": 1,
        "ChuPai": [2],
        "NewHandPai": [5, 6, 7, 8, 9, 11, 12, 13, 16, 29],
        "PengPai": [4],
        "m_ChuPai": 2,
        "m_Player_Location": 2,
        "m_User_ID": 103000
    }, {
        "Action": 0,
        "MenPaiCount": [28, 21, 0, 4],
        "NewHandPai": [4, 4, 6, 14, 16, 19, 21, 21, 22, 23, 24, 25, 26, 27],
        "m_GangPai_Count": 0,
        "m_Player_Loacation": 3,
        "m_User_ID": 104000,
        "m_ZhuaPai": 21
    }, {
        "Action": 11
    }]

后来用api调试工具postman获取到的数据:(主要部分)

[{
        UserData_t=[{
            Chu_Pai_Count=0,
            Gang_Pai_Count=0,
            HandPai=[4,5,6,7,8,9,11,13,14,15,16,17,18,11],
            Hand_Pai_Count=14,
            LaiZi_Count=0,
            Men_Pai_Count=28,
            Peng_Pai_Count=0,
            m_BeiShu=1,
            m_Final_Score=0,
            m_Part_Score=0,
            m_Player_ID=101000,
            m_Player_Location=0
        },{
            Chu_Pai_Count=0,
            Gang_Pai_Count=0,
            HandPai=[1,2,4,2,2,2,6,23,24,25,26,27,28],
            Hand_Pai_Count=13,
            LaiZi_Count=0,
            Men_Pai_Count=26,
            Peng_Pai_Count=0,
            m_BeiShu=1,
            m_Final_Score=0,
            m_Part_Score=0,
            m_Player_ID=102000,
            m_Player_Location=1
        },{
            Chu_Pai_Count=0,
            Gang_Pai_Count=0,
            HandPai=[29,16,2,4,4,5,6,7,8,9,11,12,13],
            Hand_Pai_Count=13,
            LaiZi_Count=0,
            Men_Pai_Count=28,
            Peng_Pai_Count=0,
            m_BeiShu=1,
            m_Final_Score=0,
            m_Part_Score=0,
            m_Player_ID=103000,
            m_Player_Location=2
        },{
            Chu_Pai_Count=0,
            Gang_Pai_Count=0,
            HandPai=[14,6,16,4,4,19,21,22,23,24,25,26,27],
            Hand_Pai_Count=13,
            LaiZi_Count=0,
            Men_Pai_Count=26,
            Peng_Pai_Count=0,
            m_BeiShu=1,
            m_Final_Score=0,
            m_Part_Score=0,
            m_Player_ID=104000,
            m_Player_Location=3
        }],
        m_ChaoTian=19,
        m_Dice_1=6,
        m_Dice_2=2,
        m_LaiZi=11,
        m_MenPai_EndIndex=1,
        m_MenPai_EndLoc=1,
        m_MenPai_LaiziIndex=3,
        m_MenPai_LaiziLoc=1,
        m_MenPai_StartIndex=4,
        m_MenPai_StartLoc=3,
        m_MenPai_ZhuangIndex=2,
        m_MenPai_ZhuangLoc=1,
        m_Zhuang_Location=0
    },{
        Action=1,
        ChuPai=[19,4],
        NewHandPai=[5,6,7,8,9,11,11,13,14,15,16,17,18],
        m_ChuPai=4,
        m_Player_Location=0,
        m_User_ID=101000
    },{
        Action=2,
        NewHandPai=[2,5,6,7,8,9,11,12,13,16,29],
        PengPai=[4],
        m_Desk_ID=120434,
        m_PengPai=4,
        m_Player_Location=2,
        m_User_ID=103000
    },{
        Action=12,
        ChuPai=[19],
        NewHandPai=[5,6,7,8,9,11,11,13,14,15,16,17,18],
        m_Desk_ID=120434,
        m_PengPai=4,
        m_Player_Location=0,
        m_User_ID=101000
    },{
        Action=1,
        ChuPai=[2],
        NewHandPai=[5,6,7,8,9,11,12,13,16,29],
        PengPai=[4],
        m_ChuPai=2,
        m_Player_Location=2,
        m_User_ID=103000
    },{
        Action=0,
        MenPaiCount=[28,21,0,4],
        NewHandPai=[4,4,6,14,16,19,21,21,22,23,24,25,26,27],
        m_GangPai_Count=0,
        m_Player_Loacation=3,
        m_User_ID=104000,
        m_ZhuaPai=21
    },{
        Action=11
    }]

我们可以看到前后的区别,前者的 “ 消失了 和 :变成了 后者的=,显而易见,数据发生的变化,后者的数据无法被客户端正常解析或解析难度大(C++ 服务端 通过post java后端 把数据写进 mysql数据库,app客户端 通过post请求获取 数据 ,然后解析数据 做成游戏回放 ) ,嗯,这样存数据是行不通的。

后来我找到的解决方法,就是先把json数据序列化后再存进mysql数据库,此时数据库字段类型应改为blob类型(底层byte[]存储),
当客户端获取时,后台从mysql数据库获取到json数据,再把json数据反序列化后再返回给客户端。

序列化

            List<Object> gamePlayback = (List<Object>) map.get("json");
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(out);
            outputStream.writeObject(gamePlayback);
            byte[] bytes = out.toByteArray();
            outputStream.close();

最后把bytes存数据库

反序列化

            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream in = new ObjectInputStream(byteArrayInputStream);
            List<Object> gamePlayback = (List<Object>) in.readObject();
            in.close();  

最后把gamePlayback 返回给app客户端

数据如下:(配图)

这里写图片描述

end——————————

猜你喜欢

转载自blog.csdn.net/weixin_38150130/article/details/82190809