简体   繁体   中英

Serialization VS Gson

I'm currently sending data through a Java socket, and I havent decided in which way to pass this data. I've been using Gson before which I think is great so I'm currently thinking of passing json strings through the socket for conversion. But since both the client and server is written in Java, native Java Serialization is also an option.

Looking at performance and efficiency, which is faster, Serialization or Gson?

The data i'm sending will be of the following nature (with modifications)

public class Packet
{
    private int errorType;
    private String errorMessage;

    // Getters and setters..
}

public class Statistics extends Packet
{
    private int score;
    private int winRatio;

    // Getters and setters
}

I will be passing different subclasses of packet

I don't know about others, but I used Protocol Buffer with java. It is really cool. Extremely fast and small data size for serialized object. Google use it to communicate with most of their applications. Since both of your client and server is written in java, I think, it is a good choice for you

Read this article to know how to work with Protocol Buffer in java

Here is a nice benchmarking comparison for different object serialization tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM