2

I am using Delphi 2009 to build up a string variable containing a simple JON string from values I get from a database. This results in a string of the form below (although the real string could be much longer)

{"alice@example.com": {"first":"Alice", "id": 2},"bob@example.com": {"first":"Bob", "id":1},"cath@example.com": {"first":"Cath", "id":3},"derek@example.com": {"first":"Derek", "id": 4}}

This string gets sent as a header called Recipient-Variables in an email to a company.

The instructions I have for sending the emails to the company say

Note The value of the “Recipient-Variables” header should be valid JSON string, otherwise we won’t be able to parse it. If your “Recipient-Variables” header exceeds 998 characters, you should use folding to spread the variables over multiple lines.

I have looked at these SO posts to try to understand what is meant by folding but cannot really understand the replies as they often seem to be referencing a particular editor.

notepad++ user defined regions with folding

Folding JSON at specific points

Can you customize code folding?

Please can somebody use my example to show me what syntax I should use or what characters I need to insert in my string to comply with the instruction and fold my JSON string, say in between the records for bob and cath?

(BTW I understand what is meant by folding when viewing JSON or other code in an editor but I don't understand how a simple JSON string needs to be formatted in order for the folding to happen at a specific place)

user2834566
  • 775
  • 9
  • 22

1 Answers1

2

I finally found the answer myself so posting here to help others, just in case. The answer is given in this document on rfc2822 standards, published in 2001 by the Network Working Group (P. Resnick, Editor)

https://www.rfc-editor.org/rfc/rfc2822#page-11

The document ...

specifies a syntax for text messages that are sent between computer users, within the framework of "electronic mail" messages.

...and in particular describes how emails are constructed and in particular how to deal with long headers.

Section 2.2.3 talks about long header fields, > 998 characters, and says such headers need to be folded by inserting the CRLF characters followed immediately by some white space, eg a space character.

If the receiving server is following the same standards it will strip out the CRLF character before parsing the header, which will itself will include stripping space characters.

Though structured field bodies are defined in such a way that
folding can take place between many of the lexical tokens (and even
within some of the lexical tokens), folding SHOULD be limited to
placing the CRLF at higher-level syntactic breaks. For instance, if
a field body is defined as comma-separated values, it is recommended
that folding occur after the comma separating the structured items in preference to other places where the field could be folded, even if
it is allowed elsewhere.

Later, in section 3.2.3 it explains how comments may be combined with folding white space.

So it seems that if generating the string through code, it is necessary to fold long header lines by detecting a higher level syntactic boundary, such as a comma, that is less than 988 characters from the start of the header (or the last fold point) and insert the three hex characters x0D0A20. This could be done after the header has been constructed or on the fly as it is generated.

As a follow up, I now notice that the Overbytes ICS component I am using (TSslSmtpCli) has a boolean property FoldHeaders so this might do all the work for me.

Community
  • 1
  • 1
user2834566
  • 775
  • 9
  • 22
  • Best split on `},` then, though it might be that syntactic points are not needed and the JSON parsing is done on the entire joined JSON string. – Joop Eggen Apr 25 '19 at 07:07
  • Hey appreciate your work here. I'm dealing with the same issue, but for some reason my variables still aren't going through. Trying this but to no avail - const jsonString = JSON.stringify(templateVars).split('},').join('},\r\n'); – snn Mar 05 '21 at 01:38
  • Sorry, just seen this comment. I also found that inserting CR LF or \r\n didn't work. The only thing that did work was inserting the three hex characters x0D0A20. ie CR LF Space (don't forget the space character, as my answer explained) – user2834566 Jan 11 '22 at 19:20