So I'm generating a potentially lengthy JSON string for use in Sendgrid's SMTP API. Because it is going as an SMTP header, it should have a maximum line length (recommended 72, but absolutely no longer than 1000). One naive solution is described in the documentation at the end of:
http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/
They suggest doing this:
$js =~ s/(.{1,72})(\s)/$1\n /g;
But I don't like that because it could split inside a string where whitespace is meaningful. Furthermore, performance when spaces are few and far between seems like it could be pretty terrible.
Now I'm using Ruby and I can do something like:
JSON.generate(@hash, options)
Where options provide different formatting options documented at http://flori.github.com/json/doc/classes/JSON.html#method-i-generate. But none of those give me what I want, which is terse JSON with a newline every once in a while.
Any ideas?