Rust · Chapter 18 of 26

Serde and JSON

By the end of this chapter you can turn your own structs and enums into JSON and back with serde and serde_json, shape the JSON with attributes (rename_all, rename, default, skip_serializing_if), handle Option fields and enums the way another program expects them, read the error a bad document gives and pass it on, reach into JSON you have no struct for with serde_json::Value, and say why money travels as whole paise in an integer. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real programs
  • 4 tasks
  • Rust
  • In the all labs plan

The first 2 chapters of Rust are free. This one comes with the all labs plan.

Why you need this

At the end of every shift each of the 120 collection boxes sends the plant what it collected, and the plant answers: stored, refused, or try again later. Both sides speak JSON, the text format nearly every program on earth can read. On Tuesday that was 10,164 pourings, 1.6 MB of it, and Neha's payout file is JSON too, one line a farmer.

With what you know so far you would build that text with format! and read it back with split(','). The first note with a quote mark in it breaks the text:

let note = "Lakhan said \"restart\"";
println!("{{\"note\":\"{note}\"}}");
{"note":"Lakhan said "restart""}

That is no longer JSON: the plant would stop reading at the second quote. And reading JSON with split falls apart on the first comma inside a farmer's name, on a nested list of pourings, on keys in another order. JSON has rules (escapes, nesting, numbers, null) that one format! per struct gets wrong a little differently each time. serde writes that code for you, correctly, from the struct itself.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    summary.rs: a shift summary as JSON

    Make ShiftSummary serialisable with serde's Serialize, with attributes so that its keys are in camelCase and note is left out when it is None. Keep the field names as they are. Then write to_json(summary: &ShiftSummary) -> String, its JSON on one line (serde_json::to_string), and to_pretty(summary: &ShiftSummary) -> String, serde_json's pretty form.

    For S014's morning (pourings 66, rejected 4, total_ml 655230, total_paise 2165708, no note), to_json returns exactly {"society":"S014","shift":"morning","pourings":66,"rejected":4,"totalMl":655230,"totalPaise":2165708}, and to_pretty returns the same keys and values one a line, indented by two spaces, between a line with { and a line with }, with no line break at the end.

    The Check also tries S120's evening (37, 1, 258560, 905888) with the note "analyser restarted at 06:47": then to_json ends with ,"note":"analyser restarted at 06:47"}.

  2. Task 2

    upload.rs: decode a box's upload, and refuse a bad one

    A box sends {"box":"B2014","society":"S014","shift":"morning","pourings":[{"farmer":"F10663","kind":"cow","ml":15430,"fat":48,"snf":81}]}. Make Pouring and Upload deserialisable with serde's Deserialize: the key "box" goes into box_id, and late is false when the upload has no "late" key ("late":true makes it true).

    Write decode(text: &str) -> Result<Upload, String>. It returns the Upload, or an Err that is "bad upload: " followed by serde_json's own message: "bad upload: expected value at line 1 column 1" for text that is not JSON, "bad upload: missing field society at line 1 column 47" for {"box":"B2014","shift":"morning","pourings":[]}, an invalid type message for "ml":"6.25", and an unknown variant message for "kind":"goat". An upload that decodes but has no pourings is Err("bad upload: no pourings from B2014"), with its own box. The Check decodes a good upload of two pourings and one with "late":true, and compares every field.

  3. Task 3

    reply.rs: the plant's reply as a tagged enum

    The plant's replies are one JSON object with a "type" key: {"type":"stored","society":"S014","pourings":66,"paise":2165708}, {"type":"refused","society":"S120","reason":"shift already closed"} and {"type":"retry","after_ms":500}. Make Reply serialisable and deserialisable with serde's Serialize and Deserialize, with attributes that put the variant's name, in lower case, in a "type" key.

    Write reply_json(reply: &Reply) -> String, its one line of JSON, exactly as above for those three replies, and parse_reply(text: &str) -> Result<Reply, String>, which reads one. The Check reads a refused reply for S061 ("unknown box"), a retry after 2500 ms and a stored reply for S002 (35 pourings, 1225791 paise). A reply it cannot read is an Err with serde_json's message: "unknown variant lost, expected one of stored, refused, retry at line 1 column 14" for {"type":"lost"}, and "missing field type at line 1 column 14" for {"after_ms":5}.

  4. Task 4 · interview question

    Interview: payout.rs, the paisa that went missing

    Lakhan's payout file has one JSON line a farmer with rupees as a decimal, and total_paise adds them up short: 0.29 rupees times 100 is 28.999999999999996 in an f64, and as u64 drops the rest. Fix the format so money is whole paise in an integer.

    payout_json(farmer: &str, paise: u64) -> String returns exactly {"farmer":"F10122","paise":21084} for F10122 and 21084. total_paise(lines: &str) -> Result<u64, String> reads lines like that, one a line, and returns the exact sum of their paise; the Check tries seven lines of 29, 113, 21084, 57, 105000, 99 and 444431 paise (the sum is 570813). An empty text is Ok(0). A line it cannot read is an Err with serde_json's message, and a decimal is one of them: {"farmer":"F10122","paise":210.84} gives Err("invalid type: floating point 210.84, expected u64 at line 1 column 33").

    Farhan will ask: why does money travel as whole paise in an integer in JSON, and never as rupees in a decimal?

What you will learn

  • Serialize and Deserialize: derive them, and serde writes the converting code.
  • serde_json: to_string, to_string_pretty and from_str.
  • Attributes: rename_all, rename, default, skip_serializing_if.
  • Option fields and enums in JSON: missing keys, null, and #[serde(tag = "...")].
  • The error a bad document gives: what was wrong, with its line and column.
  • serde_json::Value: JSON with no struct of your own.
  • Money in JSON: whole paise, never rupees as a decimal.

More in Real programs

Build "Serde and JSON" yourself

DoodhSetu runs the milk collection boxes of 120 village societies around Anand. The old box program crashed on an empty line, and you rebuild it in Rust. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Rust are free. This one comes with the all labs plan.