Serialization & Deserialization β€” Lecture Notes


1. The Core Problem

A JavaScript client and a Rust server need to exchange data over HTTP. The issue: they have completely different type systems.

  • JavaScript is dynamic and interpreted β€” no strict types.
  • Rust is statically typed and compiled β€” strict, low-level types.

How does data sent from one end get understood at the other end, across different languages, machines, and network hops?


2. The Solution: A Common Standard

The answer is to agree on a shared serialization standard β€” a set of rules both sides follow when sending and receiving data.

JavaScript Object  β†’  [serialize]  β†’  Common Format  β†’  [transmit]  β†’  Rust Struct
Rust Struct        β†’  [serialize]  β†’  Common Format  β†’  [transmit]  β†’  JavaScript Object
  • Serialization: Converting from a language-native data type β†’ common format (for transmission or storage).
  • Deserialization: Converting from common format β†’ language-native data type (after receiving).

Key property: The common format must be language-agnostic β€” any language/platform can read and write it.


3. Where This Fits in the OSI Model

As a backend engineer, you work at Layer 7 (Application Layer). Below that, the network stack handles conversion into IP packets, data frames, and physical bits β€” that is not your concern.

Client (App Layer)  β†’  serialize to JSON  β†’  ... network layers ... β†’  bits over wire
Server (App Layer)  ←  deserialize from JSON  ←  ... network layers ... ←  bits over wire

The data arrives back as the same format (JSON) at the application layer on both ends. Everything in between is the network’s job.


4. Serialization Standards

Text-Based (Human-Readable)

FormatNotes
JSONMost popular for HTTP/REST communication; human-readable
XMLOlder; still used in enterprise and SOAP APIs
YAMLCommon in config files (Docker, Kubernetes); not typical for HTTP transmission

Binary Format (Machine-Optimized)

FormatNotes
ProtobufMost popular; used with gRPC; compact and fast
Avro, Thrift, etc.Other binary options, less common

Text-based vs Binary tradeoff:

  • Text (JSON): human-readable, easy to debug, slightly larger payload.
  • Binary (Protobuf): not human-readable, much smaller and faster β€” preferred for high-performance internal services.

This course focus: JSON β€” used in ~80% of HTTP REST API communication.


5. JSON Deep Dive

JSON = JavaScript Object Notation

Despite the name, JSON is not JavaScript β€” it is a language-agnostic standard used everywhere: config files, log files, HTTP APIs, databases, and more.

Syntax Rules

{
  "id": 1,
  "title": "Some Book",
  "author": "John Doe",
  "available": true,
  "tags": ["fiction", "classic"],
  "address": {
    "country": "India",
    "pincode": 110001
  }
}
RuleDetail
Wrapped in { }Top-level must be an object (or array)
Keys in double quotesKeys are always strings: "key" not key
Value typesstring, number, boolean, null, array, nested object
Nesting allowedObjects can contain other objects or arrays recursively
No trailing commasStrict syntax β€” trailing commas are invalid

What JSON is NOT

  • Not a JavaScript object literal (JS objects can have unquoted keys, functions, etc.)
  • Not a binary format β€” it’s plain text
  • Not language-specific β€” parseable in Python, Rust, Go, Java, etc.

6. JSON in the Client-Server Flow

Client (JS)                         Server (Rust/Node/Python/etc.)
──────────                          ──────────────────────────────
JS Object                           Native struct / dict / class
    ↓  JSON.stringify() / serialize      ↓  json.parse() / deserialize
JSON string  ──── HTTP request ────►  JSON string
                                        ↓  parse into native type
                                    Business logic + DB ops
                                        ↓  serialize back to JSON
JSON string  ◄─── HTTP response ────  JSON string
    ↓  JSON.parse() / deserialize
JS Object β†’ render UI

Demo observation from lecture:

  • POST /api/books β€” request body is JSON with "id", "title", "author" (keys double-quoted, values typed).
  • Server responds with a JSON array of book objects in the same format.
  • Client parses the response and renders the list β€” the full serialize β†’ transmit β†’ deserialize cycle in action.

7. Summary

TermDefinition
SerializationConverting native data β†’ common format for transmission/storage
DeserializationConverting common format β†’ native data after receiving
JSONThe dominant text-based serialization standard for REST APIs
ProtobufDominant binary standard for gRPC / high-performance services

Serialization and deserialization exist to make data language-agnostic β€” so a JavaScript client and a Rust server (or any two systems) can communicate meaningfully without knowing anything about each other’s internal type systems.


Quick Revision Checklist

  • Serialization = native type β†’ common format; Deserialization = common format β†’ native type
  • Purpose: language/domain-agnostic data exchange
  • Backend engineer’s concern: application layer (JSON in/out); OSI layers below are network’s job
  • Text-based standards: JSON, XML, YAML β€” JSON dominates HTTP REST
  • Binary standards: Protobuf (gRPC), Avro, Thrift β€” faster, smaller, not human-readable
  • JSON rules: {} wrapper, keys must be double-quoted strings, values can be string/number/bool/null/array/object
  • JSON is language-agnostic despite being named after JavaScript