liveBook · Manning

Serialization

As explained in Rust Servers, Services, and Apps

Serialization is the conversion of an in-memory data structure into a structured representation suitable for transmission or storage.

Serialization converts an in-memory data structure into a structured representation for transmission or storage. In a Rust web server, an HttpResponse becomes a well-formed HTTP message that a browser or other client can understand. section 2.3.2

An HttpResponse stores the protocol version, status code, status text, optional headers, and optional body. Implementing From<HttpResponse> for String assembles these fields with HTTP line breaks, headers, Content-Length, and body content. section 2.3.2

A practical serialization path is:

  • Construct an HttpResponse with defaults or an explicit status, headers, and body. section 2.3.2
  • Convert the response into a String using its version, status, headers, content length, and body. section 2.3.2
  • Write the serialized string to a stream with send_response(). section 2.3.2
  • Use serde and serde_json when handlers load JSON data into Rust structures or return JSON through an API route. section 2.3.4

Serialized 404 response

A response with HTTP/1.1, status 404, Content-Type text/html, and the body “Item was shipped on 21st Dec 2020” becomes an HTTP string containing “HTTP/1.1 404 Not Found”, Content-Length: 33, blank-line separation, and the body. A unit test compares this exact result with the expected message. section 2.3.2

Figure 2.7 Structure of an HTTP response

Use the HTTP response structure to trace how serialized status, headers, and body fields map onto the wire-format message. section 2.3.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains response fields, Rust conversion code, stream writing, JSON APIs, and exact-output tests without covering serialization formats beyond HTTP and JSON.
Examples
Uses serialized HTTP 200 and 404 responses, an order-status JSON API, and exact-output unit tests.
What it assumes
Assumes Rust web-server code, HTTP request flow, structs, traits, and streams while demonstrating serialization through concrete handlers and tests.
What you can do after
Build it from scratch by converting Rust HTTP response structs into wire messages and handling JSON data in web handlers.
How it connects
Connects HTTP responses, Rust structs, HTTP requests, JSON, web handlers, routing, and deserialization.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.