[solved]fcl-json and newlines lazarus / 67-Windows ---------------------------------------------------------------------- dbannon · 2022-05-14 07:06 Using the FP json parser to read some json with a text field with newline characters surprised me on Windows.  I understand that correct JSON has an embedded "\n" in text where a newline is needed. And it works fine under Linux but does exactly the same thing under Windows, that is, converts "\n" to a LineFeed, not the Carriage Return, Line Feed that Windows needs. Thus - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses fpjson, jsonparser;.....procedure TFormDump.Button1Click(Sender: TObject);var  jData : TJSONData;  jObject : TJSONObject;  s  : string;  AWord : String = '';  i : integer;begin   jData := GetJSON('{"Fld1" : "Hello\nAgain"}');   S := jObject.Get('Fld1');   Dump;   jData.Free;   Memo1.Clear;   for i := 1 to length(S) do       // report on content of S       if (S[i] > ' ') and (S[i] <= '~') then           AWord := AWord + S[i]       else begin           Memo1.Append(AWord);           // one word at a time.           AWord := '';           case ord(S[i]) of              10  : Memo1.Append('--------- Line Feed');              13  : Memo1.Append('--------- Carriage Ret');           else               Memo1.Append('--------- Ord ' + inttostr(ord(InString[i])));         end;       end;  if AWord <> '' then Memo1.Append(AWord);end; Produces - Code: [Select]Hello --------- Line Feed Again On both Linux and Windows. And, of course, Windows does not like it. Just what am I missing here ? Davo ---------------------------------------------------------------------- six1 · 2022-05-14 07:25 In case of Windows, try to replace #10 to #13 may be a solution Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...  begin     jData := GetJSON('{"Fld1" : "Hello\nAgain"}');     S := jObject.Get('Fld1');    {$IFDEF Windows}      s:=stringreplace(s,#10,#13,[rfreplaceall]);    {$endif}... ---------------------------------------------------------------------- AlexTP · 2022-05-14 07:29 fpJSON converts '\n' to LF chars, because '\n' means LF. If you need CR LF newlines, you need to use text with '\r\n', or you can also replace LF -> CR LF by StringReplace. ---------------------------------------------------------------------- dbannon · 2022-05-14 11:34 Thanks both six1 and Alex but thats not really the point, while its easy to work around, I don't think it should be like that. "\n" is, obviously, a C newline, migrated to Javascript and then to JSON, by time it get to there, its not a Linefeed, its a newline, should be translated to a pascal LineEnding, and thats either #10 on Unix, #13#10 on Windows. (was #13 on Mac long, long ago). Or so I think ..... Davo ---------------------------------------------------------------------- Thausand · 2022-05-14 12:25 Quote from: dbannon on May 14, 2022, 01:34:11 pm"\n" is, obviously, a C newline, migrated to Javascript and then to JSON, by time it get to there, its not a Linefeed, its a newline, should be translated to a pascal LineEnding, and thats either #10 on Unix, #13#10 on Windows. (was #13 on Mac long, long ago). Or so I think ..... Not really. here: https://www.json.org/json-en.html Special escape for linefeed, carriage return etc. ---------------------------------------------------------------------- Thaddy · 2022-05-14 12:42 And that link is the ONLY reputable JSON doc, btw... Everything else is bolt-on rubbish. I wrote a grammar for the real standard that can output Pascal. Many implementations use that as core. It is on Embarcadero. ---------------------------------------------------------------------- dbannon · 2022-05-15 00:12 > Not really. here: https://www.json.org/json-en.html And nicely written too. Better than the resources I have been using.  But still not totally clean about newline IMHO. While it mentions LineFeed and CarriageReturn it also says - The Unicode code point U+000A is used as the newline. Embedding "\u000A" instead of "\n" produces the same result, just a LineFeed on Windows Thaddy, your JSON parser, made for Windows (or Delphi), did it convert "newline" to #10 or #13#10 on Windows ? Davo EDIT: OK, I give up, JSON Tools does the same thing, so right or wrong, thats how it it is ! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef WINDOWS}St := St.replace('#10', '#13#10', [rfReplaceAll]);{$endif} ---------------------------------------------------------------------- Thausand · 2022-05-15 01:30 Quote from: dbannon on May 15, 2022, 02:12:18 amWhile it mentions LineFeed and CarriageReturn it also says - The Unicode code point U+000A is used as the newline. Maybe you can help ? Because /me can not find it  "Newline" not exist for json. ECMA document (https://www.ecma-international.org/wp- content/uploads/ECMA-404_2nd_edition_december_2017.pdf) writes: Quote9 String A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). All code points may be placed within the quotation marks except for the code points that must be escaped: quotation mark (U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F. There are two-character escape sequence representations of some characters. \" represents the quotation mark character (U+0022). \\ represents the reverse solidus character (U+005C). \/ represents the solidus character (U+002F). \b represents the backspace character (U+0008). \f represents the form feed character (U+000C). \n represents the line feed character (U+000A). \r represents the carriage return character (U+000D). \t represents the character tabulation character (U+0009). So, for example, a string containing only a single reverse solidus character may be represented as "\\". json \n (Line feed) <> c \n (newline). Newline is afaik c-ishm and platform depending, see https://en.wikipedia.org/wiki/Newline#In_programming_languages QuoteEmbedding "\u000A" instead of "\n" produces the same result, just a LineFeed on Windows Yes, see pdf ecma: it be linefeed. I not know how to explain better (my English is bit better but not good). I think you wish for \n be JSON newline (I understand why) but that create same problem for JSON that is there for newline (example: TStringlist platform compatibility when read/write text lines). TStringlist solve problem but TStringlist is Pascal class and developer can make rules. JSON is official standard and developer have to follow (or fork/make new standard  ) ---------------------------------------------------------------------- dbannon · 2022-05-15 07:57 Quote from: Thausand on May 15, 2022, 03:30:41 amMaybe you can help ? Because /me can not find it  "Newline" not exist for json. From the link you posted, down right hand side, click on any of the entries under "escapes" and it takes you to https://www.crockford.com/mckeeman.html - not far down from the top.  But I suspect its not expressly about JS Quote...see https://en.wikipedia.org/wiki/Newline#In_programming_languages Java, PHP, and Python provide the '\r\n' sequence (for ASCII CR+LF). In contrast to C, these are guaranteed to represent the values U+000D and U+000A, respectively. I am convinced !  I always considered a Java \n pretty much the same as a C one and therefore JS also the same, but I was wrong ! QuoteI not know how to explain better (my English is bit better but not good). You have made yourself quite understandable, far, far better than I would be in your native language ! Thanks ---------------------------------------------------------------------- Thausand · 2022-05-15 11:45 Quote from: dbannon on May 15, 2022, 09:57:23 amFrom the link you posted, down right hand side, click on any of the entries under "escapes" and it takes you to https://www.crockford.com/mckeeman.html - not far down from the top.  But I suspect its not expressly about JS Thank you, light is now on  not expressly = not explicit ? If yes, then in first line of link it write: QuoteMcKeeman Form This is an excerpt from Chapter 22 of How JavaScript Works. Later/down it write about JSON. QuoteThis is the JSON grammar in McKeeman Form. Hope it helped.