Browse Source

Merge branch 'develop'

Unknown 6 years ago
parent
commit
491fabd0f9
3 changed files with 41 additions and 13 deletions
  1. 26 6
      Quick.Json.Serializer.pas
  2. 4 0
      QuickLib.inc
  3. 11 7
      samples/delphi/QuickJsonSerializer/main.pas

+ 26 - 6
Quick.Json.Serializer.pas

@@ -5,9 +5,9 @@
   Unit        : Quick.JSON.Serializer
   Unit        : Quick.JSON.Serializer
   Description : Json Serializer
   Description : Json Serializer
   Author      : Kike Pérez
   Author      : Kike Pérez
-  Version     : 1.4
+  Version     : 1.5
   Created     : 21/05/2018
   Created     : 21/05/2018
-  Modified    : 16/10/2018
+  Modified    : 06/12/2018
 
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
 
@@ -50,6 +50,9 @@ uses
     {$IFDEF DELPHIXE7_UP}
     {$IFDEF DELPHIXE7_UP}
     System.Json,
     System.Json,
     {$ENDIF}
     {$ENDIF}
+    {$IFDEF DELPHIRX103_UP}
+    System.Generics.Collections,
+    {$ENDIF}
   {$ENDIF}
   {$ENDIF}
   DateUtils,
   DateUtils,
   Quick.Commons,
   Quick.Commons,
@@ -465,11 +468,19 @@ begin
 
 
   if not rValue.IsEmpty then
   if not rValue.IsEmpty then
   begin
   begin
+    {$IFDEF DELPHIRX103_UP}
+    if (TObjectList<TObject>(aObject) <> nil) and (rvalue.IsArray) then
+    begin
+      for i := 0 to rvalue.GetArrayLength - 1 do
+      begin
+        TObjectList<TObject>(aObject).Add(rvalue.GetArrayElement(i).AsObject);
+      end;
+    end;
+    {$ELSE}
     for rfield in rType.GetFields do
     for rfield in rType.GetFields do
     begin
     begin
       if rfield.Name = 'FOwnsObjects' then rfield.SetValue(aObject,True);
       if rfield.Name = 'FOwnsObjects' then rfield.SetValue(aObject,True);
       //if rfield.Name = 'FCount' then rfield.SetValue(aObject,i);
       //if rfield.Name = 'FCount' then rfield.SetValue(aObject,i);
-
       if rfield.Name = 'FItems' then
       if rfield.Name = 'FItems' then
       begin
       begin
         //if TList(aObject) <> nil then TList(aObject).Clear;
         //if TList(aObject) <> nil then TList(aObject).Clear;
@@ -480,6 +491,7 @@ begin
     end;
     end;
     rProp := rType.GetProperty('Count');
     rProp := rType.GetProperty('Count');
     rProp.SetValue(aObject,i);
     rProp.SetValue(aObject,i);
+    {$ENDIF}
   end;
   end;
 end;
 end;
 {$ENDIF}
 {$ENDIF}
@@ -564,7 +576,12 @@ begin
         begin
         begin
           {$IFNDEF FPC}
           {$IFNDEF FPC}
           //avoid return unicode escaped chars if string
           //avoid return unicode escaped chars if string
-          if aProperty.PropertyType.TypeKind in [tkString, tkLString, tkWString, tkUString] then rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aProperty.GetValue(aObject).TypeInfo,member.JsonString.ToString)
+          if aProperty.PropertyType.TypeKind in [tkString, tkLString, tkWString, tkUString] then
+            {$IFDEF DELPHIRX103_UP}
+            rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aProperty.GetValue(aObject).TypeInfo,TJsonValue(member).value)
+            {$ELSE}
+            rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aProperty.GetValue(aObject).TypeInfo,member.JsonString.ToString)
+            {$ENDIF}
             else rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aProperty.GetValue(aObject).TypeInfo,member.ToJSON);
             else rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aProperty.GetValue(aObject).TypeInfo,member.ToJSON);
           {$ELSE}
           {$ELSE}
           rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aName,member.ToJSON);
           rValue := DeserializeType(aObject,aProperty.PropertyType.TypeKind,aName,member.ToJSON);
@@ -990,8 +1007,11 @@ begin
               try
               try
                 //jValue := TJsonValue(jPair.JsonValue.Clone);
                 //jValue := TJsonValue(jPair.JsonValue.Clone);
                 jValue := jPair.JsonValue;
                 jValue := jPair.JsonValue;
-                jArray.AddElement(jValue);
-                if jPair.JsonValue <> nil then jPair.JsonValue.Owned := False;
+                if jValue <> nil then
+                begin
+                  jArray.AddElement(jValue);
+                  jPair.JsonValue.Owned := False;
+                end;
               finally
               finally
                 jPair.Free;
                 jPair.Free;
                 if jValue <> nil then jValue.Owned := True;
                 if jValue <> nil then jValue.Owned := True;

+ 4 - 0
QuickLib.inc

@@ -124,6 +124,10 @@
         {$define DELPHILINUX}
         {$define DELPHILINUX}
       {$ifend}
       {$ifend}
     {$ifend}
     {$ifend}
+    {$if CompilerVersion >= 33.0} //Delphi RX10.3 Rio
+      {$define DELPHIRX103_UP}
+	    {$define DELPHIRIO_UP}
+    {$ifend}
   {$else}
   {$else}
     //Delphi 5 or older
     //Delphi 5 or older
     {$define DELPHI6OROLDER}
     {$define DELPHI6OROLDER}

+ 11 - 7
samples/delphi/QuickJsonSerializer/main.pas

@@ -109,6 +109,7 @@ type
     fSurname : string;
     fSurname : string;
     fAge : Integer;
     fAge : Integer;
     fAddress : string;
     fAddress : string;
+    fPath : string;
     fOptions : TOptions;
     fOptions : TOptions;
     fLastConnections : TConnectionArray;
     fLastConnections : TConnectionArray;
     fMarried : Boolean;
     fMarried : Boolean;
@@ -134,6 +135,7 @@ type
     property Genre : TGenre read fGenre write fGenre;
     property Genre : TGenre read fGenre write fGenre;
     property Department : TDepartment read fDepartment write fDepartment;
     property Department : TDepartment read fDepartment write fDepartment;
     property Address : string read fAddress write fAddress;
     property Address : string read fAddress write fAddress;
+    property Path : string read fPath write fPath;
     property Balance : Double read fBalance write fBalance;
     property Balance : Double read fBalance write fBalance;
     [TCustomNameProperty('IsMarried')]
     [TCustomNameProperty('IsMarried')]
     property Married : Boolean read fMarried write fMarried;
     property Married : Boolean read fMarried write fMarried;
@@ -183,6 +185,7 @@ begin
   if User2 <> nil then User2.Free;
   if User2 <> nil then User2.Free;
   User2 := TUser.Create;
   User2 := TUser.Create;
   User2.FromJson(Memo1.Text);
   User2.FromJson(Memo1.Text);
+  //User2 := TUser.CreateFromJson(Memo1.Text);
   //User2.CreateFromJson(Memo1.Text);
   //User2.CreateFromJson(Memo1.Text);
   Memo1.Lines.Add('User2 as json:');
   Memo1.Lines.Add('User2 as json:');
   Memo1.Lines.Add(User2.ToJson(True));
   Memo1.Lines.Add(User2.ToJson(True));
@@ -218,6 +221,7 @@ begin
   user.Age := 30;
   user.Age := 30;
   user.Married := True;
   user.Married := True;
   user.Address := 'Sunset st. 2 \b';
   user.Address := 'Sunset st. 2 \b';
+  User.Path := 'C:\documents\files';
   user.Options.Option1 := 1;
   user.Options.Option1 := 1;
   user.Options.Option2 := 'good';
   user.Options.Option2 := 'good';
   user.Options.AllowGroups := gtExternal;
   user.Options.AllowGroups := gtExternal;
@@ -231,13 +235,13 @@ begin
   department.Id := 10;
   department.Id := 10;
   department.Name := 'IT';
   department.Name := 'IT';
   user.Department := department;
   user.Department := department;
-  //user.Status := TUserStatus.usOnVacation;
-  //lastcon.IP := '127.0.0.1';
-  //lastcon.ConnectionDate := Now();
-  //User.LastConnections := [lastcon];
-  //lastcon.IP := '192.0.0.1';
-  //lastcon.ConnectionDate := Now();
-  //User.LastConnections := User.LastConnections + [lastcon];
+  user.Status := TUserStatus.usOnVacation;
+  lastcon.IP := '127.0.0.1';
+  lastcon.ConnectionDate := Now();
+  User.LastConnections := [lastcon];
+  lastcon.IP := '192.0.0.1';
+  lastcon.ConnectionDate := Now();
+  User.LastConnections := User.LastConnections + [lastcon];
   group := TGroup.Create;
   group := TGroup.Create;
   group.Id := 1;
   group.Id := 1;
   group.GType := gtInternal;
   group.GType := gtInternal;