5

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?

Swift
  • 13,118
  • 5
  • 56
  • 80
gtd
  • 16,956
  • 6
  • 49
  • 65
  • Why not scan the lines for `"` pairs and add the new line at the end of those? It would require writing a pretty in-depth regex to calculate for characters and line lengths, but would solve your problem. – Swift Jun 27 '12 at 19:14
  • I thought about this, but I didn't want to write a JSON parser just for this purpose. Regexp wouldn't be enough it would have to be a full on parser. I also don't really want to include another JSON library just for this one little use case, though if I already has a SAX style parser included I think it would give the best control for doing exactly what I want. – gtd Jun 28 '12 at 10:00
  • What's consuming the JSON? If you control that code, perhaps Base64 encoding the JSON first would solve the problem. You could then split the Base64-encoded JSON however you want, send it via SMTP, and decode it at the other end. – Duncan Bayne Oct 18 '12 at 02:43
  • Can you be sure that the strings containing meaningful whitespace never exceed 1000 chars? – Patrick Oscity Oct 27 '12 at 10:59
  • This was the solution that helped me in this case: https://stackoverflow.com/questions/15992281/newlines-resolved-as-0a-in-sendgrid-x-smtpapi-header – stereoscott Jun 24 '19 at 21:09

1 Answers1

1
options = {
  indent:'',
  space:"\n",
  space_before:"\n",
  object_nl:"\n",
  array_nl:"\n",
}

This puts a newline at every place where doing so won't affect the semantics of the JSON, and disables any indentation.

It's not terse and not human friendly, but a newline is just 1 extra character, so having a lot of them won't affect performance in any real way. It also gives you the shortest possible lines without affecting the content of your strings. You should probably check those to make sure they're all under the length limit.

Christophe Biocca
  • 3,388
  • 1
  • 23
  • 24