Browse Source

* Merging revisions r45839,r45840,r45841,r45842 from trunk:
------------------------------------------------------------------------
r45839 | michael | 2020-07-24 13:03:00 +0200 (Fri, 24 Jul 2020) | 1 line

* Prevent warning
------------------------------------------------------------------------
r45840 | michael | 2020-07-24 13:03:25 +0200 (Fri, 24 Jul 2020) | 1 line

* Add joIgnoreDuplicates
------------------------------------------------------------------------
r45841 | michael | 2020-07-24 13:04:15 +0200 (Fri, 24 Jul 2020) | 1 line

* Add joIgnoreDuplicates
------------------------------------------------------------------------
r45842 | michael | 2020-07-24 13:04:35 +0200 (Fri, 24 Jul 2020) | 1 line

* Remove some compiler warnings
------------------------------------------------------------------------

git-svn-id: branches/fixes_3_2@46632 -

michael 5 years ago
parent
commit
d2f17d5a30

+ 1 - 7
packages/fcl-json/src/fpjsonrtti.pp

@@ -126,7 +126,6 @@ Type
     FOnGetObject: TJSONGetObjectEvent;
     FOnPropError: TJSONpropertyErrorEvent;
     FOnRestoreProp: TJSONRestorePropertyEvent;
-    FCaseInsensitive : Boolean;
     FOptions: TJSONDestreamOptions;
     procedure DeStreamClassProperty(AObject: TObject; PropInfo: PPropInfo; PropData: TJSONData);
     function GetCaseInsensitive: Boolean;
@@ -219,7 +218,7 @@ Type
 function TJSONDeStreamer.ObjectFromString(const JSON: TJSONStringType): TJSONData;
 
 begin
-  With TJSONParser.Create(JSON) do
+  With TJSONParser.Create(JSON,[]) do
     try
       Result:=Parse;
     finally
@@ -422,7 +421,6 @@ Var
   PI : PPropInfo;
   TI : PTypeInfo;
   I,J,S : Integer;
-  D : Double;
   A : TJSONArray;
   JS : TJSONStringType;
 begin
@@ -590,7 +588,6 @@ procedure TJSONDeStreamer.JSONToCollection(const JSON: TJSONData;
 Var
   I : integer;
   A : TJSONArray;
-  O : TJSONObject;
 
 begin
   If (JSON.JSONType=jtArray) then
@@ -1059,9 +1056,6 @@ end;
 
 function TJSONStreamer.StreamClassProperty(const AObject: TObject): TJSONData;
 
-Var
-  C : TCollection;
-  I : integer;
 
 begin
   Result:=Nil;

+ 2 - 1
packages/fcl-json/src/jsonparser.pp

@@ -129,7 +129,8 @@ begin
   // Add to existing structural type
   if (FStruct is TJSONObject) then
     begin
-    TJSONObject(FStruct).Add(FKey,AValue);
+    if (Not (joIgnoreDuplicates in options)) or  (TJSONObject(FStruct).IndexOfName(FKey)=-1) then
+      TJSONObject(FStruct).Add(FKey,AValue);
     FKey:='';
     end
   else if (FStruct is TJSONArray) then

+ 2 - 0
packages/fcl-json/src/jsonreader.pp

@@ -246,6 +246,8 @@ begin
         DoError(SErrUnexpectedToken);
     tkIdentifier :
         DoError(SErrUnexpectedToken);
+  else
+    // Do nothing
   end;
 end;
 

+ 1 - 1
packages/fcl-json/src/jsonscanner.pp

@@ -51,7 +51,7 @@ type
 
   EScannerError = class(EParserError);
 
-  TJSONOption = (joUTF8,joStrict,joComments,joIgnoreTrailingComma);
+  TJSONOption = (joUTF8,joStrict,joComments,joIgnoreTrailingComma,joIgnoreDuplicates);
   TJSONOptions = set of TJSONOption;
 
 Const

+ 49 - 0
packages/fcl-json/tests/testjsonparser.pp

@@ -33,6 +33,7 @@ type
   private
     FOptions : TJSONOptions;
     procedure CallNoHandlerStream;
+    procedure DoDuplicate;
     procedure DoTestError(S: String; Options : TJSONOptions = DefaultOpts);
     procedure DoTestFloat(F: TJSONFloat); overload;
     procedure DoTestFloat(F: TJSONFloat; S: String); overload;
@@ -75,6 +76,8 @@ type
     Procedure TestCommentLine;
     Procedure TestFirstLineComment;
     Procedure TestMultiLineComment;
+    Procedure TestIgnoreDuplicates;
+    Procedure TestNoIgnoreDuplicates;
   end;
 
 implementation
@@ -716,6 +719,52 @@ begin
     end;
 end;
 
+procedure TTestParser.TestIgnoreDuplicates;
+
+Const
+  MyJSON =
+        '{ "a":100, "b": 20, "a":300} ';
+
+var
+  J : TJSONData;
+
+begin
+  With TJSONParser.Create(MyJSON,[joIgnoreDuplicates]) do
+    Try
+      J:=Parse;
+      AssertEquals('Correct class',TJSONObject,J.ClassType);
+      AssertEquals('Correct value',100,TJSONObject(J).Get('a',0));
+      J.Free;
+    Finally
+      Free;
+    end;
+end;
+
+procedure TTestParser.DoDuplicate;
+
+Const
+  MyJSON =
+        '{ "a":100, "b": 20, "a":300} ';
+
+var
+  J : TJSONData;
+
+begin
+  With TJSONParser.Create(MyJSON,[]) do
+    Try
+      J:=Parse;
+      J.Free;
+    Finally
+      Free;
+    end;
+end;
+
+procedure TTestParser.TestNoIgnoreDuplicates;
+
+begin
+  AssertException('No duplicates allowed',EJSON,@DoDuplicate);
+end;
+
 procedure TTestParser.DoTestError(S : String; Options : TJSONOptions = DefaultOpts);
 
 Var