网络知识|序列化和非序列化以及选择

发布时间 2024-01-04 04:28:22作者: 跪求个offer

1.JSON

  • one of simplest and most popular formats
  • We can represent an object in plain text with fields of type: String, Number, Boolean, Array, Object

Example:

Adv:

  • Simple and human readable
    • Even after serialization we can understand it and debug it
    • It's plain text so we can create the object and send it without any special software
  • Programming language independent
  • Easy integration with JavaScript/Front-End
    • JavaScript Object Notation is very similar to JavaScript Object representation

Deserialization:

2. Java Object Seralization

  • The class needs to match the original class definition
  • The class has to have an accessible no-args constructor
  • If either of the conditions are violated, we will get an InvalidClassException during the deseralization process

Advantages:

  • Guarantees about correst state reconstruction without any type ambiguity
  • Very clear source of truth for the object we want to send over the network
  • Native support in all the JVM languages
    • No need for external libraries
    • Simple development process

Disadvantages:

  • Not human readable, harder to test
    • We need to import the class and write a Java program to send a message to the server
  • Tight coupling to JVM languages
    • Won't work if we want to exchange messages with applications written in Golong/G++ for example
    • That is a big problem for building a distributed system that utilizes different technologies and runs on different platforms.

3. Google's Protocol Buffers

Advantages

  • No type ambiguity
  • Clear and well defined schema
  • Language independent thanks to the 2 step process
    • Proto file definition
    • Language specific stub generation using proto compiler
  • Efficient serialization & deserialization
  • Security

Disadvantages

  • Not Human readable
  • Hard to debug
  • More complex development process
    • Proto file definition
    • Stub generation