简体   繁体   中英

Sending JSON Strings with “\n” Line Breaks

I have a Delphi XE2 Datasnap server and I need to receive a string with line breaks from a database and resend it as a JSON String to the client application. The problem is I'm receiving the string with #10#13 line breaks, and my client expects \\n linebreaks. That seems to be the JSON default way of sending line breaks and I was expecting the conversion to be automatic, but seemingly it isn't so.

I'm thinking of implementing a method for conversion of my own, but that would be in a worst-case scenario, and I want to avoid a possible "reinvent the wheel" scenario. So my question is, is there a better option?

#10 is a newline or line feed (LF) character, represented as \\n in c-derived languages, and #13 is a carriage return (CR) represented as \\r . The usual order in Windows and some internet protocols (eg SMTP and HTTP) is \\r\\n .

Unfortunately, different operating systems uses different line breaks (see also http://en.wikipedia.org/wiki/Newline )

Delphi (or the JSON framework you use) cannot guess your line break expectation. I am afraid you have to write extra code to make line breaks fit your client.

In reality, what is probably happening is that the strings are coming from a database or other source where they are stored with literal CR LF sequences. Delphi is just passing them on unchanged.

Before you do any work, make sure the client actually cares.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM