(可线上使用)Gson实现POJO与字符串序列化和反序列化 及其 与node.js的对比测试

1)pom.xml

  <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>

2)ProtoMgr.java

package gson;

import com.google.gson.Gson;

public class ProtoMgr {
    private static final Gson gson = new Gson();

    public static String toJson(Object obj) {
        return gson.toJson(obj);
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }
}

3)POJO

RankItem.java

package gson;

public class RankItem {
    public int index;

    public RankItem() {
        this.index = (int) Math.floor(Math.random() * 1000);
    }
}

Rank.java

package gson;

import java.util.ArrayList;
import java.util.HashMap;

public class Rank {
    public int r;

    public ArrayList<RankItem> rankItems = new ArrayList<>();

    public HashMap<Integer, RankItem> hashMap = new HashMap<>();

    public Rank(int r) {
        this.r = r;

        rankItems.add(new RankItem());
        rankItems.add(new RankItem());
        rankItems.add(new RankItem());
        rankItems.add(new RankItem());
        rankItems.add(new RankItem());

        hashMap.put(1, new RankItem());
        hashMap.put(2, new RankItem());
        hashMap.put(3, new RankItem());
        hashMap.put(4, new RankItem());
    }
}

PlayerInfo.java

package gson;

import java.util.ArrayList;

public class PlayerInfo {
    public ArrayList<Rank> arrayList = new ArrayList<>();

    public PlayerInfo() {
        arrayList.add(new Rank(1));
        arrayList.add(new Rank(2));
        arrayList.add(new Rank(3));
    }
}

4)测试: POJO-->JSON与JSON-->POJO

package gson;

public class Main {
    public static void main(String[] args) {
        PlayerInfo playerInfo = new PlayerInfo();

        // POJO-->JSON
        String jsonStr = ProtoMgr.toJson(playerInfo);
        System.out.println(jsonStr);

        // JSON-->POJO
        PlayerInfo info = ProtoMgr.fromJson(jsonStr, PlayerInfo.class);
        System.out.println(info);

    }
}


pojo-->json 内容如下

{
    "arrayList":[
        {
            "r":1,
            "rankItems":[
                {
                    "index":819
                },
                {
                    "index":978
                },
                {
                    "index":675
                },
                {
                    "index":860
                },
                {
                    "index":655
                }
            ],
            "hashMap":{
                "1":{
                    "index":203
                },
                "2":{
                    "index":820
                },
                "3":{
                    "index":300
                },
                "4":{
                    "index":404
                }
            }
        },
        {
            "r":2,
            "rankItems":[
                {
                    "index":934
                },
                {
                    "index":424
                },
                {
                    "index":683
                },
                {
                    "index":186
                },
                {
                    "index":20
                }
            ],
            "hashMap":{
                "1":{
                    "index":548
                },
                "2":{
                    "index":794
                },
                "3":{
                    "index":562
                },
                "4":{
                    "index":152
                }
            }
        },
        {
            "r":3,
            "rankItems":[
                {
                    "index":478
                },
                {
                    "index":655
                },
                {
                    "index":704
                },
                {
                    "index":564
                },
                {
                    "index":477
                }
            ],
            "hashMap":{
                "1":{
                    "index":243
                },
                "2":{
                    "index":843
                },
                "3":{
                    "index":711
                },
                "4":{
                    "index":461
                }
            }
        }
    ]
}

json-->pojo 内容如下:

======

js

class PlayerInfo {
    constructor() {
        this.arrayList = [];
        this.arrayList.push(new Rank(1));
        this.arrayList.push(new Rank(2));
        this.arrayList.push(new Rank(3));
    }
}

class Rank {

    constructor(r) {
        this.r = r;

        this.rankItems = [];
        this.hashMap = {};

        this.rankItems.push(new RankItem());
        this.rankItems.push(new RankItem());
        this.rankItems.push(new RankItem());
        this.rankItems.push(new RankItem());
        this.rankItems.push(new RankItem());

        this.hashMap[1] = new RankItem();
        this.hashMap[2] = new RankItem();
        this.hashMap[3] = new RankItem();
        this.hashMap[4] = new RankItem();
    }
}

class RankItem {
    constructor() {
        this.index = Math.floor(Math.random() * 1000);
    }
}

let playerInfo = new PlayerInfo();

let s = new Date().getTime();

let n = 100000;

for (let i = 0; i < n; i++) {
    let jsonStr = JSON.stringify(playerInfo);
    let info = JSON.parse(jsonStr)
}

let e = new Date().getTime();

console.log((e - s) / 1000)

发现js序列化效率反而是java的6倍,比java高

猜你喜欢

转载自blog.csdn.net/themagickeyjianan/article/details/106626185