DagrA guide to binary formats, and to Dagrdraft

10 Chapter ten

Data without a fixed shape, next to CBOR

Every format with a schema, so far, knew the shape of its data in advance. Some data does not have one: a settings file, the attributes someone attached to a log line, a record from a program you have never seen. CBOR was made for exactly that. Dagr always has a schema, and can still hold it.

A format that describes itself

Chapter 3 introduced CBOR as JSON's shape in binary. Every item starts with a head byte that says what the item is and how long: a number, a text of five bytes, a list of three items, a map of two pairs. Names are spelled out. A reader needs nothing but the bytes.

Dagr's way to hold the same data is to write a schema that says "anything", with the two tools of the last two chapters: a union from chapter 8, and lists of it from chapter 9.

Value = UnionType("Value", types=[
    ("null",    t.bool),
    ("boolean", t.bool),
    ("integer", t.i64),
    ("number",  t.f64),
    ("text",    t.utf8),
    ("list",    t.ref("Value").array),
    ("map",     t.ref("Entry").array),
])

Entry = Node("Entry", fields=[
    "key"   >> t.utf8 >> required,
    "value" >> t.ref("Value") >> required,
], packed=True)

A value is one of seven things. Two of them contain values again, so the type describes itself all the way down. This is an ordinary Dagr schema. Nothing in the format is special about it.

Type any JSON below, or choose an example, and compare the two.

a reader wants only:

The two are built from the same parts: something that says what comes next, then the thing. They are not the same size. CBOR is the smaller one, and on small values like these it is not close.

Counting items, or counting bytes

One difference is not about size. A CBOR list says how many items it has. A Dagr list or map says how many bytes it is, and so does every pair.

Choose "a log, then a status" and ask for status, the last name. To get there a CBOR reader has to walk through the whole log in front of it, head by head, because it cannot know where a nested list ends without reading its insides. The Dagr reader reads the first pair's length and name, and steps over the rest of it in one move.

For small values that does not matter. For a large document from which you want one corner, it does: the CBOR reader's work grows with the log, the Dagr reader's does not.

What a schema is worth

Holding shapeless data this way works, and it is not where a format with a schema is at its best. To see what the schema buys, take data whose shape is known: the sensor reading from chapter 6. Here it is written four ways.

the valuesthe nameseverything else

Dagr's "anything" is the largest of the four, larger even than the JSON text. The first three all carry the five names, and the names are a large part of the message. The schema's whole contribution is that both sides already know them, so they are not sent. What is left is the values and a tag in front of each.

The names are sent again with every record. A thousand readings in CBOR spell samples a thousand times. Compression gets much of that back, at the price of compressing and decompressing.

Both in one message

Real data is often mostly known, with a corner that is not. A log record has a time, a level and a text, and then whatever attributes its author thought of. A record in an open protocol has the fields its specification lists, and then fields added by a program the specification has not heard of.

Because "anything" is an ordinary type, it can be one field among typed ones:

LogRecord = Node("LogRecord", fields=[
    "time"       >> t.u64 >> required,
    "level"      >> t.ref("Level") >> required,
    "text"       >> t.utf8 >> required,
    "attributes" >> t.ref("Entry").array,
], packed=True)

The known part costs what a schema makes it cost. Only the open corner pays for names. Two places where Dagr has been tried on existing protocols are built this way: OpenTelemetry's attributes, and records of the AT Protocol, where fields that a record's specification does not list are kept as such pairs so that nothing is lost.

The union is also yours to shape. If your open data has timestamps, binary blobs or links to other records, they can be variants of their own, stored as what they are and not as text to be parsed again.

What CBOR has that this does not

One thing to know about CBOR if you ever sign or hash it: the same value can be written in several ways. A number may use more bytes than it needs, a map's pairs may come in any order. Two correct encoders can produce different bytes, and so different hashes, for equal data. Protocols that hash CBOR therefore add rules that leave exactly one encoding. The next chapter comes back to this.

Side by side

CBORDagr, "anything"Dagr, with a schema
a reader needsnothingthe small schema abovethe data's schema
names in the bytesyesyesno
a pair costs, besides name and value2 bytes5 bytes or more1 byte
stepping over a nested valuewalk itone moveone move
can be mixed with typed fieldsit has no typed fieldsyesyes

If all your data is shapeless, CBOR is the better tool and this chapter is not an argument against it. Dagr's answer is for data that is mostly known: describe what you know, and keep a corner open for the rest.

What these panels are.

Try it

  1. Type 23, then 24. What happens to the CBOR bytes, and why at that number?
  2. Type {"a":1} and add pairs one at a time. By how much does each format grow for each pair?
  3. Type 0.5, then 0.1. Which format writes them differently from each other, and what does it know about 0.5?
  4. In "a log, then a status", ask for status. Then add entries to the log. Which reader's count grows?
  5. In the second panel, make the name of the sensor very long. Which of the four sizes move together? Now imagine renaming the field samples to s: which sizes would change?

What comes next

Chapter 11 is about data that must be trusted: a signed token of the kind that JSON Web Tokens made common. It needs bytes that are the same every time, and a place beside the data for a signature, which is what Dagr's headers are for.