Frontend Labs
⋯

36. cJSON: recursive-descent parsing and tagged unions

What it is: a small (~2500-line), extremely widely embedded JSON library — if Section 16.3's mini JSON parser felt approachable, cJSON is the natural next read: same recursive-descent structure, production- hardened.

Concept mapping, directly against your own Section 16.3 parser:

  • cJSON the struct is Section 8.3's tagged union, just flattened into one struct with a type int field and multiple payload fields instead of a union block — a deliberate simplicity trade-off (slightly more memory per node) for slightly simpler code than a real union.
  • cJSON_Parse → parse_value → parse_object/parse_array/ parse_string/parse_number is exactly your Section 16.3 function breakdown, function-for-function, because that decomposition is simply what JSON's grammar requires — reading cJSON's actual cJSON_Parse* functions after writing your own is one of the fastest ways to feel the jump from "I can write basic C" to "I can read real C."
  • cJSON_Delete walking the tree and freeing children before parents is Section 16.3's json_free, and it's the same recursive-teardown shape as Section 18.1's list_free and Section 18.3's tree traversal — once you've written one recursive free function, you can read every other one.

Generated from real-life-c-guide.md · 8 Oct 2026 16:36