2
0

JsonSerializerTest.dpr 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. program JsonSerializerTest;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils,
  6. System.Generics.Collections,
  7. Quick.Commons,
  8. Quick.Console,
  9. Quick.Json.Serializer;
  10. type
  11. THost = class
  12. private
  13. fID: TGUID;
  14. fName : string;
  15. fIP : string;
  16. fPort : Integer;
  17. published
  18. property ID: TGUID read fID write fID;
  19. property Name : string read fName write fName;
  20. property IP : string read fIP write fIP;
  21. property Port : Integer read fPort write fPort;
  22. end;
  23. THostList = TObjectList<THost>;
  24. TGroup = type Integer;
  25. TGroupHelper = class
  26. class function One : Integer;
  27. class function Two : Integer;
  28. class function Three : Integer;
  29. end;
  30. TConfig = class
  31. private
  32. fHosts : THostList;
  33. fDebugMode : Boolean;
  34. fLevel : Integer;
  35. fCompleted : Double;
  36. fGroup : TGroup;
  37. published
  38. constructor Create;
  39. destructor Destroy; override;
  40. property Hosts : THostList read fHosts write fHosts;
  41. property DebugMode : Boolean read fDebugMode write fDebugMode;
  42. property Level : Integer read fLevel write fLevel;
  43. property Completed : Double read fCompleted write fCompleted;
  44. property Group : TGroup read fGroup write fGroup;
  45. end;
  46. const
  47. jsonstring = '{"Hosts":['+
  48. '{"Name":"Host 1 año perfeción","IP":"127.0.0.1","Port":80, "ID":"{00FB3A62-F77D-4E71-9613-79E2E49D4562}"},'+
  49. '{"Name":"Host 2","IP":"192.168.1.1","Port":443,"ID":"{EBEBBC33-71F2-404A-8C0E-903CFA268616}"}'+
  50. '],'+
  51. '"DebugMode":true,"Level":1,"Completed":-0.658}';
  52. jsonstring2 = '{"Hosts":'+
  53. '{"List":['+
  54. '{"Name":"Host 1","IP":"127.0.0.2","Port":80, "ID":"{D52917AE-0A21-4B5B-945A-0F17FD158332}"},'+
  55. '{"Name":"Host 2","IP":"192.168.1.2","Port":443, "ID":"{80E6467A-282C-437E-B66A-D704004A2C3F}"}'+
  56. ']},'+
  57. '"DebugMode":true,"Level":2,"Completed":1.5,"Group":2'+
  58. '}';
  59. var
  60. config : TConfig;
  61. host : THost;
  62. serializer : TJsonSerializer;
  63. json : string;
  64. guid: TGUID;
  65. { TConfig }
  66. constructor TConfig.Create;
  67. begin
  68. fHosts := THostList.Create(True);
  69. end;
  70. destructor TConfig.Destroy;
  71. begin
  72. fHosts.Free;
  73. inherited;
  74. end;
  75. { TGroupHelper }
  76. class function TGroupHelper.One: Integer;
  77. begin
  78. Result := 1;
  79. end;
  80. class function TGroupHelper.Three: Integer;
  81. begin
  82. Result := 2;
  83. end;
  84. class function TGroupHelper.Two: Integer;
  85. begin
  86. Result := 3;
  87. end;
  88. begin
  89. try
  90. ReportMemoryLeaksOnShutdown := True;
  91. serializer := TJsonSerializer.Create(slPublishedProperty);
  92. try
  93. //created from object
  94. cout('Create from object',ccYellow);
  95. config := TConfig.Create;
  96. config.Group := TGroupHelper.One;
  97. try
  98. host := THost.Create;
  99. host.Name := 'Host 1';
  100. host.IP := '127.0.0.1';
  101. host.Port := 80;
  102. CreateGUID(guid);
  103. host.ID:=guid;
  104. config.DebugMode := True;
  105. config.Level := 1;
  106. config.Hosts.Add(host);
  107. host := THost.Create;
  108. host.Name := 'Host 2';
  109. host.IP := '192.168.1.1';
  110. host.Port := 443;
  111. CreateGUID(guid);
  112. host.ID:=guid;
  113. config.Hosts.Add(host);
  114. json := serializer.ObjectToJson(config,True);
  115. cout(json,ccWhite);
  116. coutFmt('Capacity: %d / Count: %d',[config.Hosts.Capacity,config.Hosts.Count],etInfo);
  117. finally
  118. config.Free;
  119. end;
  120. //from json string without list property
  121. cout('Create from jsonstring without "List" property',ccYellow);
  122. config := TConfig.Create;
  123. try
  124. serializer.JsonToObject(config,jsonstring);
  125. json := serializer.ObjectToJson(config,True);
  126. cout(json,ccWhite);
  127. coutFmt('Capacity: %d / Count: %d',[config.Hosts.Capacity,config.Hosts.Count],etInfo);
  128. finally
  129. config.Free;
  130. end;
  131. //from json string with list property
  132. cout('Create from jsonstring with "List" property',ccYellow);
  133. config := TConfig.Create;
  134. try
  135. serializer.JsonToObject(config,jsonstring2);
  136. json := serializer.ObjectToJson(config,True);
  137. cout(json,ccWhite);
  138. coutFmt('Capacity: %d / Count: %d',[config.Hosts.Capacity,config.Hosts.Count],etInfo);
  139. finally
  140. config.Free;
  141. end;
  142. finally
  143. serializer.Free;
  144. end;
  145. ConsoleWaitForEnterKey;
  146. except
  147. on E: Exception do
  148. Writeln(E.ClassName, ': ', E.Message);
  149. end;
  150. end.