generator: Escape the control characters correctly

Escaping these as octals is out of the JSON spec completely,
so roll our own string encoder.

https://bugzilla.gnome.org/show_bug.cgi?id=730425