Forum > Windows

[solved]fcl-json and newlines

(1/2) > >>

dbannon:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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: ---Hello
--------- Line Feed
Again
--- End code ---

On both Linux and Windows. And, of course, Windows does not like it.

Just what am I missing here ?

Davo

six1:
In case of Windows, try to replace #10 to #13 may be a solution

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...  begin     jData := GetJSON('{"Fld1" : "Hello\nAgain"}');     S := jObject.Get('Fld1');    {$IFDEF Windows}      s:=stringreplace(s,#10,#13,[rfreplaceall]);    {$endif}... 

AlexTP:
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:
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:

--- 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 .....

--- End quote ---
Not really. here: https://www.json.org/json-en.html

Special escape for linefeed, carriage return etc.

Navigation

[0] Message Index

[#] Next page

Go to full version