For the serialization of a Java object, I would like to test the difference between using JSON and using general serialization tools in terms of temporal and spatial performance.
json chooses to use fastjson.
The serialization tool uses Protostuff and Kyro. Why not use protobuf? Because I feel that for a java class with hundreds of existing properties, creating a new matching proto file is a bit anti-human. Protostuff is an improved version of Protobuf, which allows you to serialize a Java Object directly, using it in a bit like Kyro, without as many intermediate processes as Protobuf. Others, such as Hession, Java with serialization, etc., are said to have much worse performance than Kryo and Protobuf, so I don't know what to expect.
After a simple test, I found that the gap was quite obvious, so I felt that there was no need to do a specific evaluation. Cut a paragraph of the log and send it out, everyone feel it.
cost time is System.nanoTime(); All three are the default configurations without any configuration. The space footprint after serialization is slightly lower than that of protostuff, and both are much higher than JSON. This is easy to understand, after all, json strings are readable, don't force too much. The time required for serialization and deserialization is better than Kyro over FastJSON, and the difference is quite obvious.
So in conclusion, if there are no extremely demanding requirements for space, Protostuff may be the best choice. Protostuff has an additional advantage over Kyro, that is, if the java class adds fields after serialization and before deserialization (which is unavoidable in real business), Kyro will be useless. However, protostuff can be used as long as it is added at the end of the class and uses the sun series JDK. Therefore, if serialization is used in scenarios such as cache, and the serialized object needs to be stored for a long time, you can only choose protostuff.
Of course, if there is a need for readability or something, you can only use json.
|