dotcloud/docker — pull request #4276 by tianon (2014-02-21 06:50:45+00:00)
Title: Add newlines to the JSON stream functions

This makes the JSON streams a _lot_ easier to parse in less well-baked JSON parsers, and no less so in better ones.

Line-based JSON streams are very, very common, where simply chunk-based is not very common at all.

If you want a really pretty way to test this, start the compiled daemon listening on a TCP socket and run something like this (and watch it stream all pretty-like, right before your very eyes):
```console
$ python -c 'import tarfile, StringIO, sys; df = "FROM debian\nRUN apt-get update\n"; ti = tarfile.TarInfo("Dockerfile"); ti.size = len(df); tar = tarfile.open(fileobj=sys.stdout, mode="w|"); tar.addfile(ti, StringIO.StringIO(df)); tar.close()' | curl --data-binary @- --header 'Content-Type: application/x-tar' --dump-header - --no-buffer 'http://localhost:4243/build?rm=1&nocache=1'
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 21 Feb 2014 06:25:45 GMT
Transfer-Encoding: chunked

{"stream":"Step 0 : FROM debian\n"}
{"stream":" ---\u003e b5fe16f2ccba\n"}
{"stream":"Step 1 : RUN apt-get update\n"}
{"stream":" ---\u003e Running in c50d141e8d5d\n"}
{"stream":"Get:1 http://security.debian.org wheezy/updates Release.gpg [836 B]\n"}
{"stream":"Get:2 http://security.debian.org wheezy/updates Release [102 kB]\n"}
{"stream":"Get:3 http://http.debian.net wheezy Release.gpg [1672 B]\n"}
{"stream":"Get:4 http://http.debian.net wheezy-updates Release.gpg [836 B]\n"}
{"stream":"Get:5 http://security.debian.org wheezy/updates/main amd64 Packages [161 kB]\n"}
{"stream":"Get:6 http://http.debian.net wheezy Release [168 kB]\n"}
{"stream":"Get:7 http://http.debian.net wheezy-updates Release [124 kB]\n"}
{"stream":"Get:8 http://http.debian.net wheezy/main amd64 Packages [5845 kB]\n"}
{"stream":"Get:9 http://http.debian.net wheezy-updates/main amd64 Packages/DiffIndex [643 B]\n"}
{"stream":"Get:10 http://http.debian.net wheezy-updates/main amd64 2014-02-15-1443.31.pdiff [547 B]\n"}
{"stream":"Get:11 http://http.debian.net wheezy-updates/main amd64 2014-02-15-1443.31.pdiff [547 B]\n"}
{"stream":"Fetched 6405 kB in 6s (959 kB/s)\nReading package lists..."}
{"stream":"\n"}
{"stream":" ---\u003e e069e103898e\n"}
{"stream":"Successfully built e069e103898e\n"}
```
(see also https://gist.github.com/tianon/9129512 for more masochism along similar lines)

This came about from a discussion on IRC: https://botbot.me/freenode/docker/msg/11203433/

I also looked into swapping the Content-Type returned for these streams to something more like "application/x-json-stream", but that looked like it would be a bit more non-trivial (since the StreamFormatter is used in several places).