What text gives you for nothing
Consider what happens to a JSON file in an ordinary week, with no special software.
- Somebody opens it to see what is in it.
- Somebody searches it for a name.
- Somebody changes a value by hand, to try something.
- Version control shows what changed between two versions, and merges the work of two people.
- Somebody writes a small one by hand as a test case.
None of this is the format's doing. It is the work of editors, search tools and version control, all written for text over decades. A binary format starts without any of it, and each item has to be supplied.
Dagr's tools all come as one command, dagr, and all work from the same thing: your schema. Everything in this chapter was run on one small project, a team of people as a graph and a stream of events as a sink.
Opening a file: what does each byte mean?
dagr explain takes a file and the schema and accounts for every byte: what it is, which node and field it belongs to, what value it holds, and where it points. Below is its real output for a team of three, 63 bytes. Point at any byte.
You have used this tool already. The coloured bytes in chapters 4 and 5 came from it.
- Nothing is left unexplained. The tool's own test is that its entries cover the buffer completely, padding included. A byte it could not account for would be a defect, in the file or in the tool.
- It is the specification, applied. When two implementations disagree about a byte, this is where the argument is settled: here is the byte, and here is the rule that says what it is.
- It comes in three forms. Lines of text for other programs to read, a page with the bytes and the graph side by side, and bookmarks for ImHex, a hex editor, for those who prefer to stay in one (
dagr imhex).
Browsing, searching and editing
Explain is for when you care about bytes. Usually you care about the data. For that, Dagr writes an explorer: a single web page, generated for your schema, that reads files of that schema in the browser. Nothing is uploaded and no server is involved.
dagr graph-explorershows a graph as boxes and arrows, or nested like a document. Shared nodes appear once, with every arrow that leads to them, so the difference between a tree and a graph (chapter 4) is visible. You can search the text fields, filter by the value of any field, change values, and save the result as a new file.dagr sink-explorershows a sink as a list of records with their kinds, sizes and positions. It filters by the value of any field, shows times as dates, reads only the records on screen, so that a very long log opens as quickly as a short one, and can write out what it shows as JSON Lines for the tools that want text.
Both are here to try, as the tool wrote them. Open one and drop the file on it.
- The graph explorer for this schema, and the team to drop on it (63 bytes), or a random team.
- The sink explorer, and forty events for it.
This is the answer to "open the file and there is your data". It takes one more step than a text editor, the page has to be generated once for each schema, and after that it shows more than a text editor would: types, links between things, and the same node reached by two paths.
Comparing and merging
Version control is where binary files are usually given up on. Git shows "binary files differ" and offers to keep one version or the other.
dagr git-tools builds a small program for your schema and tells Git to use it for files of that kind. It does three things. The first is to print a file as text, one line for each value, with its path from the root:
A line such as $.members[0] -> &$.lead says that the first member is not a copy of the lead. It is the same node. A text format has no way to say that, and a tool that compared two files as trees would report changes that are not there, or miss ones that are.
The second is a comparison of two versions, and the third a merge of two people's changes. Both are below. Somebody started from the team above. Choose what was done to it.
- The comparison is of values, not of bytes. Adding a member moves every byte after it. The tool reports one member added.
- It knows about sharing. The new member's manager is shown as a link to an existing person, not as a new person with the same name.
- Changes in different places merge without help. One person added a member and corrected an age. Another renamed the team and changed a role. The merged file has all four changes, and it is a valid Dagr file, written by the same writer as any other.
- A real conflict is stated exactly. Two people gave the same person two different ages. The tool names the field and the three values, and can open a page in which you choose, conflict by conflict.
With text, a merge works on lines, and it can produce a file that is no longer valid JSON, or valid and wrong. A merge that works on values and knows the schema cannot produce a broken file. On this point the binary format is ahead of text.
Writing one by hand
Nobody types a binary file. There are three substitutes.
- Edit in the explorer and save, for a small change to an existing file.
- The Python library, for a test case: a value is a few lines of ordinary Python, and one call writes the bytes. The files for this chapter were made that way.
dagr fuzz, for when you want many files and do not care what is in them. It writes a random, valid file of about the size you ask for, the same one every time for the same seed. Its main use is to give a new reader something to be tested on.
Starting, and keeping promises
The rest of the command is about the schema itself.
dagr initwrites a starting project.- Importers turn an existing schema into a Dagr one: from Protocol Buffers, FlatBuffers, JSON Schema and ASN.1. They are a way to try Dagr on data you already have, and they say where a concept does not carry over.
dagr uiis an editor for schemas in the browser, for those who would sooner not write the Python.dagr buildgenerates the code and writes the receipt, anddagr checkcompares the schema with the last receipt and refuses a breaking change. Chapter 16 was about these two.dagr verifyanswers a smaller question: is the generated code still what the generator wrote? Somebody will, one day, fix something quickly in a generated file. The next build would silently undo it.
Where text is still ahead
- You need the schema. Every tool here starts from it. A JSON file found on a disk ten years from now explains itself. A Dagr file without its schema is a puzzle. Keep the schema next to the data, and put a note of which schema it is in a header (chapter 11).
- You need the tools. A text editor is on every machine.
dagrhas to be installed, and three of its tools, explain, fuzz and the Git program, build a small Rust program for your schema the first time and so need a Rust compiler. - General tools do not know it. Search across a directory, a log viewer, a web browser's inspector, a colleague's favourite script: none of them reads Dagr. Where a file must be handled by tools you do not control, that settles it.
- The explorers are young. They have been used on real projects, and by few people.
Why these tools exist
A list like this used to belong only to formats with a company behind them. An explorer, a diff, a merge with a page for conflicts, a fuzzer and four importers are each a project, and none is the format itself. For a format made by one person they would have stayed on a list of things to do some day.
They were built the way the previous chapter described, each with a plan written first and a test that settles whether it works: the explanation must cover every byte, a merge must equal the file built directly with both changes, an exported edit must read back as what was shown. With an agent to do the writing, each took days.
That changes the argument of chapter 1. Text was convenient because its tools already existed, and a binary format could not afford to build its own. That cost has fallen a long way. What remains to text is that its tools are general and already installed, which is worth a great deal, and is no longer everything.
Where this leaves you
That is the whole of Dagr: one schema language, three containers, and a set of tools around them.
- A graph, for a value that is built and then read, in four layouts you choose between for each type (chapters 4 to 7).
- A sink, for records that keep arriving (chapters 12 and 13).
- A shared buffer, for memory that is changed where it lies (chapter 15).
None of them is the best at everything, and the chapters tried to say where each loses. For a single message, there are formats that are smaller, formats that read faster, and formats with far more users. What is unusual about Dagr is the three together: one schema for a document, the log of changes to it and the memory two programs share while working on it. All three are checked by the same receipt, and the first two are opened, searched and compared with the tools of this chapter. That combination, more than any one measurement, is the reason to choose it.
The next step is to write a schema of your own: the repository has the tool, the specification and examples in every language Dagr generates.
What these panels are.
- Everything shown is the output of the real tools, run on the schema above by a script in the guide's repository. The page displays it and computes nothing.
- The explorer pages are the files the tools wrote, unchanged. They load one typeface from Google's font service. Apart from that they make no request, and a file you drop on them stays in your browser.
- The colours in the first panel are assigned by this guide from the tool's descriptions, as in chapter 4.
Try it
- In the first panel, find the three bytes that say where the three members are. Two of the people have a manager. Where do those pointers lead?
- Open the graph explorer and drop the team on it. Press "Expand all". How many boxes are there for the four mentions of Ada?
- In the merge panel, look at "my change". Why is the new member's manager shown with an arrow and not with a name?
- The merged team is larger than either changed version. By how much, and what accounts for it?
- Open the sink explorer with the forty events and add a filter: page views that took more than 400 milliseconds. What would the same question cost with a text editor and a file of JSON Lines?
