Browse Source

* Add foForceLF to allow LF-separated files on windows. Fixes issue #39893

Michaël Van Canneyt 1 year ago
parent
commit
fb7c1d8805

+ 16 - 5
packages/fcl-json/src/fpjson.pp

@@ -81,7 +81,8 @@ type
                    foDoNotQuoteMembers, // Do not quote object member names.
                    foDoNotQuoteMembers, // Do not quote object member names.
                    foUseTabchar,        // Use tab characters instead of spaces.
                    foUseTabchar,        // Use tab characters instead of spaces.
                    foSkipWhiteSpace,    // Do not use whitespace at all
                    foSkipWhiteSpace,    // Do not use whitespace at all
-                   foSkipWhiteSpaceOnlyLeading   //  When foSkipWhiteSpace is active, skip whitespace for object members only before :
+                   foSkipWhiteSpaceOnlyLeading,   //  When foSkipWhiteSpace is active, skip whitespace for object members only before :
+                   foForceLF            // On Windows, use this to force use of LF instead of CR/LF
                    );
                    );
   TFormatOptions = set of TFormatOption;
   TFormatOptions = set of TFormatOption;
 
 
@@ -2742,14 +2743,19 @@ Var
   MultiLine : Boolean;
   MultiLine : Boolean;
   SkipWhiteSpace : Boolean;
   SkipWhiteSpace : Boolean;
   Ind : String;
   Ind : String;
+  LB : String;
   
   
 begin
 begin
   Result:='[';
   Result:='[';
   MultiLine:=Not (foSingleLineArray in Options);
   MultiLine:=Not (foSingleLineArray in Options);
+  if foForceLF in Options then
+    LB:=#10
+  else
+    LB:=sLineBreak;
   SkipWhiteSpace:=foSkipWhiteSpace in Options;
   SkipWhiteSpace:=foSkipWhiteSpace in Options;
   Ind:=IndentString(Options, CurrentIndent+Indent);
   Ind:=IndentString(Options, CurrentIndent+Indent);
   if MultiLine then
   if MultiLine then
-    Result:=Result+sLineBreak;
+    Result:=Result+LB;
   For I:=0 to Count-1 do
   For I:=0 to Count-1 do
     begin
     begin
     if MultiLine then
     if MultiLine then
@@ -2764,7 +2770,7 @@ begin
       else
       else
         Result:=Result+ElementSeps[SkipWhiteSpace];
         Result:=Result+ElementSeps[SkipWhiteSpace];
     if MultiLine then
     if MultiLine then
-      Result:=Result+sLineBreak
+      Result:=Result+LB
     end;
     end;
   if MultiLine then
   if MultiLine then
     Result:=Result+IndentString(Options, CurrentIndent);
     Result:=Result+IndentString(Options, CurrentIndent);
@@ -3705,11 +3711,16 @@ Var
   NSep,Sep,Ind : String;
   NSep,Sep,Ind : String;
   V : TJSONStringType;
   V : TJSONStringType;
   D : TJSONData;
   D : TJSONData;
+  LB : String;
 
 
 begin
 begin
   Result:='';
   Result:='';
   UseQuotes:=Not (foDoNotQuoteMembers in options);
   UseQuotes:=Not (foDoNotQuoteMembers in options);
   MultiLine:=Not (foSingleLineObject in Options);
   MultiLine:=Not (foSingleLineObject in Options);
+  if foForceLF in Options then
+    LB:=#10
+  else
+    LB:=sLineBreak;
   SkipWhiteSpace:=foSkipWhiteSpace in Options;
   SkipWhiteSpace:=foSkipWhiteSpace in Options;
   SkipWhiteSpaceOnlyLeading:=foSkipWhiteSpaceOnlyLeading in Options;
   SkipWhiteSpaceOnlyLeading:=foSkipWhiteSpaceOnlyLeading in Options;
   CurrentIndent:=CurrentIndent+Indent;
   CurrentIndent:=CurrentIndent+Indent;
@@ -3724,7 +3735,7 @@ begin
   else
   else
     NSep:=' : ';
     NSep:=' : ';
   If MultiLine then
   If MultiLine then
-    Sep:=','+SLineBreak+Ind
+    Sep:=','+LB+Ind
   else if SkipWhiteSpace then
   else if SkipWhiteSpace then
     Sep:=','
     Sep:=','
   else
   else
@@ -3748,7 +3759,7 @@ begin
   If (Result<>'') then
   If (Result<>'') then
     begin
     begin
     if MultiLine then
     if MultiLine then
-      Result:='{'+sLineBreak+Result+sLineBreak+indentString(options,CurrentIndent-Indent)+'}'
+      Result:='{'+LB+Result+LB+indentString(options,CurrentIndent-Indent)+'}'
     else
     else
       Result:=ObjStartSeps[SkipWhiteSpace]+Result+ObjEndSeps[SkipWhiteSpace]
       Result:=ObjStartSeps[SkipWhiteSpace]+Result+ObjEndSeps[SkipWhiteSpace]
     end
     end

+ 0 - 3
packages/fcl-json/tests/testjson.lpi

@@ -72,9 +72,6 @@
         <TrashVariables Value="True"/>
         <TrashVariables Value="True"/>
       </Debugging>
       </Debugging>
     </Linking>
     </Linking>
-    <Other>
-      <CustomOptions Value="-tunicodertl"/>
-    </Other>
   </CompilerOptions>
   </CompilerOptions>
   <Debugging>
   <Debugging>
     <Exceptions Count="2">
     <Exceptions Count="2">

+ 16 - 0
packages/fcl-json/tests/testjsondata.pas

@@ -267,6 +267,7 @@ type
     Procedure TestNonExistingAccessError;
     Procedure TestNonExistingAccessError;
     Procedure TestFormat;
     Procedure TestFormat;
     Procedure TestFormatNil;
     Procedure TestFormatNil;
+    Procedure TestFormatForceLF;
     Procedure TestFind;
     Procedure TestFind;
     Procedure TestIfFind;
     Procedure TestIfFind;
     Procedure TestDuplicate;
     Procedure TestDuplicate;
@@ -3470,6 +3471,21 @@ begin
   AssertEquals('FormatJSON, single line',J.AsJSON,J.FormatJSON([foSingleLineObject],1));
   AssertEquals('FormatJSON, single line',J.AsJSON,J.FormatJSON([foSingleLineObject],1));
 end;
 end;
 
 
+procedure TTestObject.TestFormatForceLF;
+Var
+  O : TJSONObject;
+begin
+  if sLineBreak=#10 then
+    Ignore('Not relevant when linebreak is LF');
+  O:=TJSONObject.Create(['x',1,'y',2]);
+  try
+    TestJSON(O,'{ "x" : 1, "y" : 2 }');
+    AssertEquals('FormatJSON, forced LF','{'+#10+'  "x" : 1,'+#10+'  "y" : 2'+#10+'}',O.FormatJSON([foForceLF]));
+  finally
+    O.Free;
+  end;
+end;
+
 procedure TTestObject.TestFind;
 procedure TTestObject.TestFind;
 
 
 Const
 Const