Notes

Part 2 of 11

Formats for encoding data

Data has two forms:

  • In memory: objects, structs, lists, hash tables, and trees, linked by pointers and laid out for fast CPU access.
  • As bytes: a self-contained sequence you can write to a file or send over the network. Pointers make no sense here, because an address in one process means nothing to another.

Moving between the two has names:

  • Encoding (also serialization or marshaling): in-memory data to bytes.
  • Decoding (also parsing, deserialization, or unmarshaling): bytes back to in-memory data.

"Serialization" also has an unrelated meaning in transactions (a later chapter), so this course says "encoding".

Sometimes no conversion is needed at all. Some databases can run queries directly on compressed data without unpacking it first. Zero-copy formats use a layout that works as-is both in memory and on disk or the network.

Language-specific formats

Many languages have a built-in way to save objects, like Python's pickle or Java's built-in serialization. They are handy, because you can save and restore objects with very little code. But they have serious problems:

  • Tied to one language. Other languages struggle to read the bytes, so you are stuck with that language, and sharing data with other companies' systems gets hard.
  • Security risk. Decoding must be able to create any class named in the data. If an attacker can feed you bytes, they may get your program to run their code.
  • Versioning is an afterthought. Forward and backward compatibility are weak or missing.
  • Efficiency is an afterthought. Encoding can be slow and the output large.

Rule of thumb: use a built-in language format only for data that lives a very short time.

JSON, XML, and CSV

JSON and XML are the obvious standards that work across languages. CSV is common too, but it only holds flat tables, with no nesting. All three are text, so people can read them, at least somewhat. Their problems:

  • XML is wordy and more complex than it needs to be.
  • Numbers are vague. XML and CSV cannot tell a number from a string of digits unless a separate schema says so. JSON knows strings from numbers, but not whole numbers from decimals, and it says nothing about precision.
  • Big whole numbers break. Languages like JavaScript store numbers as 64-bit floating point, which holds whole numbers exactly only up to 2^53. A 64-bit ID above that quietly turns into a slightly different number. Some APIs work around this by sending each big ID twice, once as a number and once as a string.
  • No binary data. JSON and XML only hold text. The usual trick is Base64, which turns bytes into text, but a schema must say "this field is Base64", and the data grows by about a third.
  • Schemas are optional and complex. XML Schema and JSON Schema are powerful but hard to learn. Without them, each app must hard-code how to read numbers and binary fields.
  • CSV has no schema at all. The app decides what each column means, and handles new columns by hand. Quoting rules for commas and line breaks are vague; a formal spec exists, but not every tool follows it.

Despite all this, these formats are good enough for many jobs. This is especially true when two organizations swap data, where agreeing on any format at all matters more than neatness or speed.