2
0

main.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. unit main;
  2. {$mode delphi}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Quick.Json.Serializer,
  6. Generics.Collections;
  7. type
  8. TID = Int64;
  9. TGenre = (gnMale, gnFemale);
  10. TGroupType = (gtInternal, gtExternal);
  11. TDayOfWeek = (wdSunday, wdMonday, wdThuesday, wdWednesday, wdThursday, wdFriday, wdSaturday);
  12. TUserStatus = (usAtOffice, usAtHome, usOnVacation);
  13. TDays = set of TDayOfWeek;
  14. const
  15. DEF_WORKDAYS : TDays = [wdMonday, wdThuesday, wdWednesday, wdThursday, wdFriday];
  16. DEF_WEEKEND : TDays = [wdSaturday, wdSunday];
  17. type
  18. TDepartment = record
  19. Id : TID;
  20. Name : string;
  21. end;
  22. TContactIdArray = array of TID;
  23. TGroup = class
  24. private
  25. fId : TID;
  26. fGType : TGroupType;
  27. published
  28. property Id : TID read fId write fId;
  29. property GType : TGroupType read fGType write fGType;
  30. end;
  31. TOptions = class
  32. private
  33. fOption1 : Integer;
  34. fOption2 : string;
  35. fAllowGroups : TGroupType;
  36. published
  37. property Option1 : Integer read fOption1 write fOption1;
  38. property Option2 : string read fOption2 write fOption2;
  39. property AllowGroups : TGroupType read fAllowGroups write fAllowGroups;
  40. end;
  41. TConnectionInfo = record
  42. IP : string;
  43. ConnectionDate : TDateTime;
  44. end;
  45. TConnectionArray = array of TConnectionInfo;
  46. TGroupList = TObjectList<TGroup>;
  47. TWorkingTime = class
  48. private
  49. fName : string;
  50. fWorkDays : TDays;
  51. fFreeDays : TDays;
  52. published
  53. property Name : string read fName write fName;
  54. property WorkDays : TDays read fWorkDays write fWorkDays;
  55. property FreeDays : TDays read fFreeDays write fFreeDays;
  56. end;
  57. TLevelPrivilege = array of TID;
  58. TUser = class
  59. private
  60. fId : TID;
  61. fName : string;
  62. fSurname : string;
  63. fAge : Integer;
  64. fAddress : string;
  65. fOptions : TOptions;
  66. fLastConnections : TConnectionArray;
  67. fMarried : Boolean;
  68. fWorkingTime : TWorkingTime;
  69. //[TCommentProperty('gnFemale or gnMale')]
  70. fGenre : TGenre;
  71. fBalance : Double;
  72. fHireDate : TDateTime;
  73. fLevelPrivilege : TLevelPrivilege;
  74. fObservations : string;
  75. fStatus : TUserStatus;
  76. fGroups : TGroupList;
  77. public
  78. constructor Create;
  79. destructor Destroy; override;
  80. published
  81. //[TCommentProperty('Is user Id')]
  82. property Id : TID read fId write fId;
  83. property Name : string read fName write fName;
  84. property Surname : string read fSurname write fSurname;
  85. property Age : Integer read fAge write fAge;
  86. property Address : string read fAddress write fAddress;
  87. property Balance : Double read fBalance write fBalance;
  88. //[TCustomNameProperty('IsMarried')]
  89. property Married : Boolean read fMarried write fMarried;
  90. property WorkingTime : TWorkingTime read fWorkingTime write fWorkingTime;
  91. property HireDate : TDateTime read fHireDate write fHireDate;
  92. //[TCommentProperty('Possible values = usAtOffice, usAtHome or usOnVacation')]
  93. property Status : TUserStatus read fStatus write fStatus;
  94. //property LastConnections : TConnectionArray read fLastConnections write fLastConnections;
  95. property Observations : string read fObservations write fObservations;
  96. property LevelPrivilege : TLevelPrivilege read fLevelPrivilege write fLevelPrivilege;
  97. property Options : TOptions read fOptions write fOptions;
  98. property Groups : TGroupList read fGroups write fGroups;
  99. end;
  100. TUserList = TObjectList<TUser>;
  101. { TForm1 }
  102. TForm1 = class(TForm)
  103. btnFromJson: TButton;
  104. btnToJson: TButton;
  105. Memo1: TMemo;
  106. procedure btnFromJsonClick(Sender: TObject);
  107. procedure btnToJsonClick(Sender: TObject);
  108. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  109. procedure FormCreate(Sender: TObject);
  110. private
  111. public
  112. end;
  113. var
  114. serializer : TJsonSerializer;
  115. User : TUser;
  116. UserList : TUserList;
  117. var
  118. Form1: TForm1;
  119. implementation
  120. {$R *.lfm}
  121. { TForm1 }
  122. procedure TForm1.btnToJsonClick(Sender: TObject);
  123. begin
  124. Memo1.Text := serializer.ObjectToJson(User,True);
  125. btnFromJson.Enabled := True;
  126. end;
  127. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  128. begin
  129. User.Free;
  130. serializer.Free;
  131. end;
  132. procedure TForm1.btnFromJsonClick(Sender: TObject);
  133. var
  134. newuser : TUser;
  135. begin
  136. newuser := TUser.Create;
  137. try
  138. newuser := serializer.JsonToObject(newuser,Memo1.Text) as TUser;
  139. Memo1.Lines.Add('NewUser:');
  140. Memo1.Lines.Add(serializer.ObjectToJson(newuser));
  141. finally
  142. newuser.Free;
  143. end;
  144. end;
  145. procedure TForm1.FormCreate(Sender: TObject);
  146. var
  147. lastcon : TConnectionInfo;
  148. group : TGroup;
  149. begin
  150. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  151. user := TUser.Create;
  152. user.Id := 77;
  153. user.Name := 'Joe';
  154. user.Surname := 'Smith';
  155. user.Age := 30;
  156. user.Married := True;
  157. user.Address := 'Sunset st. 2';
  158. user.Options.Option1 := 1;
  159. user.Options.Option2 := 'good';
  160. user.Options.AllowGroups := gtExternal;
  161. user.Balance := 99.9;
  162. user.HireDate := Now();
  163. user.LevelPrivilege := [1,2,3,4];
  164. user.WorkingTime.Name:= 'WeekConfig';
  165. user.WorkingTime.WorkDays := DEF_WORKDAYS;
  166. user.WorkingTime.FreeDays := DEF_WEEKEND;
  167. user.Observations := 'Good aptitude';
  168. user.Status := TUserStatus.usOnVacation;
  169. //lastcon.IP := '127.0.0.1';
  170. //lastcon.ConnectionDate := Now();
  171. //User.LastConnections := [lastcon];
  172. //lastcon.IP := '192.0.0.1';
  173. //lastcon.ConnectionDate := Now();
  174. //User.LastConnections := User.LastConnections + [lastcon];
  175. group := TGroup.Create;
  176. group.Id := 1;
  177. group.GType := gtInternal;
  178. user.Groups.Add(group);
  179. group := TGroup.Create;
  180. group.Id := 2;
  181. group.GType := gtExternal;
  182. user.Groups.Add(group);
  183. end;
  184. { TUser }
  185. constructor TUser.Create;
  186. begin
  187. fOptions := TOptions.Create;
  188. fWorkingTime := TWorkingTime.Create;
  189. fGroups := TGroupList.Create(True);
  190. end;
  191. destructor TUser.Destroy;
  192. begin
  193. fOptions.Free;
  194. fWorkingTime.Free;
  195. fGroups.Free;
  196. inherited;
  197. end;
  198. end.