Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are the main alternatives to Java serialization for sending data between services, and what are the trade-offs between JSON and a binary format such as Protobuf?

The main alternatives are text formats such as JSON, usually with Jackson in Java, and schema-based binary formats such as Protobuf and Avro. JSON is human readable, supported everywhere and does not require a schema, but it is larger, slower to parse and loses type details such as exact decimals and 64-bit integers unless we handle them. Protobuf is compact and fast, with an explicit schema that gives clear rules for compatible changes, such as never reusing field numbers, at the cost of tooling and readability; both avoid Java serialization's risk of running arbitrary class code on read.

Why this question is asked

Every service that talks to another service has to choose how data travels, and Java serialization is no longer a reasonable answer: it ties both sides to Java and to the same class versions, and it is a security risk. The real choice today is usually between JSON and a binary format with a schema. Interviewers ask this to see whether we can explain the trade-offs with concrete reasons rather than preferences. In this answer, we will recap why Java serialization is avoided, look at the same message in JSON and Protobuf, compare size and speed, discuss schemas and evolution, cover the type pitfalls of JSON, and end with how to choose.

1Why Not Java Serialization

Java serialization writes Java class names and field layouts into the data. Only a JVM with compatible versions of the same classes can read it, so it fails between languages and often between releases of the same service. And because the data names the classes to build, reading untrusted bytes can run code through gadget chains. The alternatives below share one property that removes that risk: the reader decides which types to build, and the data only provides values.

2The Same Message in JSON and Protobuf

Let us send a customer with an id, a name and an active flag. In JSON, written with Jackson from a record, it looks like this.

Java
record Customer(int id, String name, boolean active) {}
String json = new ObjectMapper().writeValueAsString(new Customer(12345, "Ana", true));
// {"id":12345,"name":"Ana","active":true} 39 bytes
// customer.proto
syntax = "proto3";
message Customer {
int32 id = 1;
string name = 2;
bool active = 3;
}
byte[] proto = Customer.newBuilder().setId(12345).setName("Ana").setActive(true).build().toByteArray();
// 10 bytes: tag+varint for id (3), tag+length+"Ana" (5), tag+value for active (2)
JSON vs Protobuf
AspectJSON (Jackson)Protobuf
Readable by peopleyes, plain textno, binary; needs tools
Schemaoptional (JSON Schema)required (.proto file)
Size of our example39 bytes10 bytes
Parsing costhigher: text, field nameslower: compact binary
Language supporteverywhere, no codegengenerated code for many languages
Evolution rulesconventions we must agreebuilt in: field numbers, unknown fields kept

Notice where the difference comes from. JSON repeats the field names in every message and writes numbers as text. Protobuf writes a small number, the field number from the schema, in place of each name, and encodes integers in a compact variable-length form, so 12345 takes two bytes instead of five characters. The reader needs the schema to know that field 1 is id, which is exactly the trade: the names live in the schema, not in each message.

3Size and Speed in Practice

For small messages over a fast network, the difference rarely matters: a few dozen bytes either way. It starts to matter at scale, when a service handles tens of thousands of messages per second or stores billions of records. Then JSON's repeated field names and text numbers cost bandwidth, storage and CPU for parsing, and binary formats are typically several times smaller and faster to parse. Compression narrows the size gap for JSON, because repeated field names compress well, but it adds CPU of its own. The honest advice is to measure with our real messages before switching for performance alone.

4Schemas and Compatible Changes

A schema is a written definition of the message: field names, types and, for Protobuf, field numbers. Its biggest benefit is not speed but clear rules for change. In Protobuf, adding a new field with a new number is safe: old readers skip the unknown field, and new readers see a default value when reading old messages. Removing a field is safe if we never reuse its number, which we enforce by marking it reserved. Changing a field's number or reusing an old number for a different meaning is the one unforgivable change, because old data would be read as the wrong field.

Java
message Customer {
reserved 4; // was "string email = 4"; removed in v3, number never reused
reserved "email";
int32 id = 1;
string name = 2;
bool active = 3;
string country = 5; // added in v2: old readers ignore it, new readers get "" for old data
}

JSON has no such rules built in. Teams agree on conventions, such as readers ignoring unknown fields, which in Jackson means disabling FAIL_ON_UNKNOWN_PROPERTIES, or annotating classes with @JsonIgnoreProperties(ignoreUnknown = true), and never changing a field's meaning. JSON Schema can describe and check the structure, but someone has to adopt it.

5Where JSON Loses Type Information

JSON has one number type, and many parsers, including every JavaScript client, read numbers as 64-bit floating point. That causes two classic bugs. Decimal amounts such as 0.1 cannot be represented exactly as binary floating point, so money should travel as a string or as an integer count of cents, and Java readers should map it to BigDecimal. Long integers above 2^53, such as 9007199254740993, lose their last digits in a JavaScript client, so large ids should also be strings.

Java
// In a browser or Node.js client
JSON.parse('{"id": 9007199254740993}').id // 9007199254740992: the last digit changed
JSON.parse('{"amount": 0.1}').amount * 3 // 0.30000000000000004
// Safer JSON
// {"id":"9007199254740993","amount":"0.10","currency":"EUR"}

JSON also has no native date, binary or enum types, so dates become strings by convention, usually ISO 8601 such as 2026-11-04T10:15:30Z, and binary data becomes Base64. Protobuf has distinct integer types and well-known types for timestamps, but no decimal type either; money is usually sent as units and nanos, or as a string. Avro offers a decimal logical type.

6How to Choose

JSON is the default for public APIs, browser clients, configuration, and services where readability and easy debugging matter more than bytes. Protobuf, usually with gRPC, suits internal service-to-service calls with high volume or strict latency, and teams that want generated, typed clients in several languages. Avro is common for event streams and data lakes, where its schema registry integration and compact rows fit well. Whatever we choose, both JSON and binary formats avoid Java serialization's main danger, because the reader builds only the types it expects.

7Follow-up questions

Is Protobuf always faster than JSON?

Usually it is smaller and faster to parse, often by several times, but modern JSON libraries are very fast, and network or database time often dominates. For most request-response APIs the format is not the bottleneck; we should measure with real payloads before switching.

What does Avro do differently from Protobuf?

Avro does not put field numbers in the data at all; the reader uses the writer's schema, either stored in the file header or fetched from a schema registry, to resolve fields by name. That makes rows very compact and works well with schema registries, while Protobuf messages can be decoded field by field without the writer's exact schema.

Can JSON have a schema?

Yes, JSON Schema describes the structure and can validate messages, and it can be stored in a schema registry with compatibility checks. It brings some of the discipline of binary formats while keeping JSON readable.

8Conclusion

In this answer, we compared the main alternatives to Java serialization. JSON with Jackson is readable, supported everywhere and schema-optional, but larger and slower, and it loses exact decimals and large integers unless we send them as strings. Protobuf and Avro are compact and fast with explicit schemas, whose rules, such as never reusing Protobuf field numbers, make change safe. Both avoid running arbitrary class code on read, which is the main reason to leave Java serialization behind.

What to say in the interviewAbout 90 seconds aloud

Open with

Instead of Java serialization, which ties both sides to Java and the same class versions and is a security risk, I would use either JSON or a schema-based binary format like Protobuf or Avro.

Then, if they want more

JSON, usually with Jackson in Java, is human readable, every language and tool supports it, and a schema is optional, which makes it great for public APIs, browser clients and debugging.

The costs are size and speed, because every message repeats field names and writes numbers as text, and type precision: JSON numbers are often read as doubles, so exact decimals and longs above 2 to the 53 get corrupted unless we send them as strings, and dates and binary need conventions. Protobuf needs a .proto schema and generated code.

Each field is identified by a number instead of a name and integers are variable-length, so a small message can be a quarter of the JSON size and much faster to parse. The schema also gives clear evolution rules: add fields with new numbers, mark removed numbers as reserved, never reuse or renumber. Old readers skip unknown fields.

The downside is that it is not readable without tools. Both options avoid Java serialization's main danger, because the reader decides which types to build. My default is JSON for external APIs and Protobuf for high-volume internal calls.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are the main alternatives to Java serialization for sending data between services...": https://scrivio-interview-library.netlify.app/java/serialization/8622e635d2/