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.
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.
- What one pair costs. Choose "a profile" and point at the bytes around a name. In CBOR a pair is a name and a value, each behind one head byte. In Dagr a pair is a small record: its length, a tag for the name, a tag for the value, and the byte that says what the value is. That is five bytes around each pair where CBOR spends two.
- Small things in the head. CBOR puts a small number,
true,falseandnullinside the head byte itself. Dagr spends a byte to say what the value is and another for the value. - Numbers. Choose "a list of numbers". Here they are close. Dagr writes large whole numbers as LEB128 and folds negative ones, CBOR picks one, two, four or eight bytes. Dagr also shortens a float that is really a whole number or fits fewer bytes. CBOR may do the same, and the encoder these bytes are checked against always writes eight.
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.
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
- No schema anywhere. Any CBOR tool can open any CBOR file and show what is in it. The Dagr bytes above mean nothing without the small schema at the top of this chapter. Two programs that want to exchange shapeless Dagr data have to agree on that schema first, and no standard one exists.
- A standard, and libraries everywhere. CBOR is an internet standard with implementations in nearly every language, and is built into other standards for signed tokens and for device security.
- It is the smaller of the two for this kind of data. Real records are mostly text, so the gap is narrower than in the panel above, but it stays: where Dagr was measured on real records with no schema, it came out somewhat larger than their CBOR. The measurement is in the AT Protocol study in Dagr's repository.
- More kinds of key and value. A CBOR map's keys can be numbers or anything else. Tags mark a value as a date, a big number or a type of your own.
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
| CBOR | Dagr, "anything" | Dagr, with a schema | |
|---|---|---|---|
| a reader needs | nothing | the small schema above | the data's schema |
| names in the bytes | yes | yes | no |
| a pair costs, besides name and value | 2 bytes | 5 bytes or more | 1 byte |
| stepping over a nested value | walk it | one move | one move |
| can be mixed with typed fields | it has no typed fields | yes | yes |
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.
- The bytes are computed in your browser. The CBOR bytes are checked against a widely used CBOR library, and the Dagr bytes against Dagr's reference writer, over several hundred values: identical.
- A whole number is a whole number. JSON does not tell
1from1.0, so the panel writes both as a whole number. Whole numbers too large for JavaScript to hold exactly are written as floats. - The JSON size in the second panel is the text without spaces.
Try it
- Type
23, then24. What happens to the CBOR bytes, and why at that number? - Type
{"a":1}and add pairs one at a time. By how much does each format grow for each pair? - Type
0.5, then0.1. Which format writes them differently from each other, and what does it know about0.5? - In "a log, then a status", ask for
status. Then add entries to the log. Which reader's count grows? - In the second panel, make the name of the sensor very long. Which of the four sizes move together? Now imagine renaming the field
samplestos: 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.
