Browse Source

* Fix bug ID #34451, introduce foSkipWhiteSpaceOnlyLeading

git-svn-id: trunk@40055 -
michael 6 years ago
parent
commit
35d4a6449a
2 changed files with 16 additions and 7 deletions
  1. 11 3
      packages/fcl-json/src/fpjson.pp
  2. 5 4
      packages/fcl-json/tests/testjsondata.pp

+ 11 - 3
packages/fcl-json/src/fpjson.pp

@@ -60,7 +60,9 @@ type
                    foSingleLineObject,  // Object without CR/LF : all on one line
                    foSingleLineObject,  // Object without CR/LF : all on one line
                    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 :
+                   );
   TFormatOptions = set of TFormatOption;
   TFormatOptions = set of TFormatOption;
 
 
 Const
 Const
@@ -3200,7 +3202,7 @@ function TJSONObject.DoFormatJSON(Options: TFormatOptions; CurrentIndent,
 Var
 Var
   i : Integer;
   i : Integer;
   S : TJSONStringType;
   S : TJSONStringType;
-  MultiLine,UseQuotes, SkipWhiteSpace : Boolean;
+  MultiLine,UseQuotes, SkipWhiteSpace,SkipWhiteSpaceOnlyLeading : Boolean;
   NSep,Sep,Ind : String;
   NSep,Sep,Ind : String;
   V : TJSONStringType;
   V : TJSONStringType;
   D : TJSONData;
   D : TJSONData;
@@ -3210,10 +3212,16 @@ begin
   UseQuotes:=Not (foDoNotQuoteMembers in options);
   UseQuotes:=Not (foDoNotQuoteMembers in options);
   MultiLine:=Not (foSingleLineObject in Options);
   MultiLine:=Not (foSingleLineObject in Options);
   SkipWhiteSpace:=foSkipWhiteSpace in Options;
   SkipWhiteSpace:=foSkipWhiteSpace in Options;
+  SkipWhiteSpaceOnlyLeading:=foSkipWhiteSpaceOnlyLeading in Options;
   CurrentIndent:=CurrentIndent+Indent;
   CurrentIndent:=CurrentIndent+Indent;
   Ind:=IndentString(Options, CurrentIndent);
   Ind:=IndentString(Options, CurrentIndent);
   If SkipWhiteSpace then
   If SkipWhiteSpace then
-    NSep:=':'
+    begin
+    if SkipWhiteSpaceOnlyLeading then
+      NSep:=': '
+    else
+      NSep:=':'
+    end
   else
   else
     NSep:=' : ';
     NSep:=' : ';
   If MultiLine then
   If MultiLine then

+ 5 - 4
packages/fcl-json/tests/testjsondata.pp

@@ -3412,12 +3412,13 @@ begin
     TestJSON(O,'{ "x" : 1, "y" : 2 }');
     TestJSON(O,'{ "x" : 1, "y" : 2 }');
     AssertEquals('Format equals JSON',O.AsJSON,O.FormatJSON([foSingleLineObject]));
     AssertEquals('Format equals JSON',O.AsJSON,O.FormatJSON([foSingleLineObject]));
     AssertEquals('Format using SkipWhiteSpace','{"x":1,"y":2}',O.FormatJSON([foSingleLineObject,foSkipWhiteSpace]));
     AssertEquals('Format using SkipWhiteSpace','{"x":1,"y":2}',O.FormatJSON([foSingleLineObject,foSkipWhiteSpace]));
+    AssertEquals('Format using SkipWhiteSpace,foSkipWhiteSpaceOnlyLeading','{"x": 1,"y": 2}',O.FormatJSON([foSingleLineObject,foSkipWhiteSpace,foSkipWhiteSpaceOnlyLeading]));
     AssertEquals('Format using SkipWhiteSpace,unquotednames','{x:1,y:2}',O.FormatJSON([foSingleLineObject,foSkipWhiteSpace,foDoNotQuoteMembers]));
     AssertEquals('Format using SkipWhiteSpace,unquotednames','{x:1,y:2}',O.FormatJSON([foSingleLineObject,foSkipWhiteSpace,foDoNotQuoteMembers]));
-    AssertEquals('Format 1','{'+sLineBreak+'  "x" : 1,'+sLineBreak+'  "y" : 2'+sLineBreak+'}',O.FormatJSON([]));
-    AssertEquals('Format 1','{'+sLineBreak+'  x : 1,'+sLineBreak+'  y : 2'+sLineBreak+'}',O.FormatJSON([foDoNotQuoteMembers]));
-    AssertEquals('Format 1','{'+sLineBreak+#9'x : 1,'+sLineBreak+#9'y : 2'+sLineBreak+'}',O.FormatJSON([foUseTabChar,foDoNotQuoteMembers],1));
+    AssertEquals('Format []','{'+sLineBreak+'  "x" : 1,'+sLineBreak+'  "y" : 2'+sLineBreak+'}',O.FormatJSON([]));
+    AssertEquals('Format [foDoNotQuoteMembers]','{'+sLineBreak+'  x : 1,'+sLineBreak+'  y : 2'+sLineBreak+'}',O.FormatJSON([foDoNotQuoteMembers]));
+    AssertEquals('Format [foUseTabChar,foDoNotQuoteMembers]','{'+sLineBreak+#9'x : 1,'+sLineBreak+#9'y : 2'+sLineBreak+'}',O.FormatJSON([foUseTabChar,foDoNotQuoteMembers],1));
     O.Add('s',TJSONObject.Create(['w',10,'h',20]));
     O.Add('s',TJSONObject.Create(['w',10,'h',20]));
-    AssertEquals('Format 1','{'+sLineBreak+#9'x : 1,'+sLineBreak+#9'y : 2,'+sLineBreak+#9's : {'+sLineBreak+#9#9'w : 10,'+sLineBreak+#9#9'h : 20'+sLineBreak+#9'}'+sLineBreak+'}',O.FormatJSON([foUseTabChar,foDoNotQuoteMembers],1));
+    AssertEquals('Format [foUseTabChar,foDoNotQuoteMembers] 2','{'+sLineBreak+#9'x : 1,'+sLineBreak+#9'y : 2,'+sLineBreak+#9's : {'+sLineBreak+#9#9'w : 10,'+sLineBreak+#9#9'h : 20'+sLineBreak+#9'}'+sLineBreak+'}',O.FormatJSON([foUseTabChar,foDoNotQuoteMembers],1));
   finally
   finally
     O.Free;
     O.Free;
   end;
   end;