Skip to main content

Data Transfer Showdown: JSON vs Protobuf vs Object Serialisation

In recent months, I’ve had to dive deep into serialization and its impact on system performance for professional reasons. While it may seem like a minor technical detail, choosing the right method for data exchange can have a significant effect on application efficiency, especially in distributed systems.

JSON is a standard for sending and receiving data over the network. Sending data over the network requires sending every character of JSON over the network and although it is very readable. At least, more than XML or any other format, it is not the one that sends the fewest bytes over the network. 

In this article, I’ll compare three common approaches: JSON, Protocol Buffers (Protobuf), and Object Serialization (when both the client and server use the same language, such as Kotlin or Dart). I’ll also share a practical table summarizing estimated results based on size and speed.


JSON

JSON (JavaScript Object Notation) is a human-readable text-based format for structuring data. It is widely used due to its simplicity and compatibility with almost any programming language.

Example in JSON:

Example of a JSON object representing a user with fields for ID, name, active status, and roles.

Advantages:

  • Readable by humans, making debugging easier.
  • Extremely compatible across different programming languages.
  • Ideal for heterogeneous systems.

Disadvantages:

  • Size: Includes redundant text labels, making it heavier.
  • Speed: Parsing text is slower compared to binary formats.
  • Computational Cost: Requires conversion at both the client and server.

Protocol Buffers (Protobuf)

Protobuf, developed by Google, is a binary serialization format that encodes data in a compact and efficient structure. It requires a predefined schema (.proto) for data modeling.

Example in Protobuf:

Protocol Buffers schema defining a User message with fields for ID, name, active status, and roles.

Advantages:

  • Compact Size: Binary messages are much smaller than JSON.
  • High Speed: Serialization and deserialization are optimized for performance.
  • Efficiency: Ideal for systems exchanging large amounts of data.

Disadvantages:

  • Requires a predefined schema, which adds maintenance overhead.
  • Not human-readable, making debugging harder.
  • Both client and server must support Protobuf.

Object Serialization

When the client and server share the same language, Object Serialization allows direct conversion of objects into a binary format using language-specific libraries.

Example in Kotlin for KMP:

Kotlin code defining a serializable User data class with fields for ID, name, active status, and roles.

Example in Dart (Serverpod):

YAML definition of a User model for Serverpod, including fields for name, isActive status, and roles.

Advantages:

  • Efficiency: Comparable to Protobuf in size and speed.
  • Ease of Use: No separate schema is needed; the code serves as the model.
  • Speed: Avoids intermediate parsing steps like JSON.

Disadvantages:

  • Limited Compatibility: Only works when the client and server share the same language.
  • Tight Coupling: May depend on language or framework versions.
  • If we want this binary serialization efficiency, we must check and ensure that the technology we use does not create a JSON-based encoding and decoding wrapper.

Comparative Table with Estimated Results


Feature JSON Protobuf Object Serialization
Payload Size ~120 bytes ~40 bytes ~50 bytes
Serialization Speed ~10 ms / 10,000 ops ~3 ms / 10,000 ops ~4 ms / 10,000 ops
Deserialization Speed ~12 ms / 10,000 ops ~3.5 ms / 10,000 ops ~4.5 ms / 10,000 ops
Human Readability High (Text) Low (Binary) Low (Binary)
Interoperability High Medium (requires schema) Low (same language)



Personal Reflection

This comparison reveals how serialization methods can influence both network performance and resource usage. JSON remains a practical choice for heterogeneous systems due to its simplicity, but its verbosity makes it unsuitable for high-performance scenarios. Protobuf, with its compact size and speed, is a clear winner for systems requiring efficient data transfer. Object serialization offers similar efficiency but is limited by its language dependence.

One of the most surprising findings is how much JSON can lag behind in terms of size and speed—Protobuf often generates payloads 2–3 times smaller, with significantly faster processing. However, this isn’t widely discussed online, and I hope this article provides some clarity for others exploring serialization. If you have questions or experiences to share, I’d love to hear from you in the comments.

Comments

© 2020 Mobile Dev Hub