| 1 |
unit TestFpJsonRTTI; |
| 2 |
|
| 3 |
// tests from demortti.pp |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
Classes, SysUtils, fpjson, fpjsonrtti, variants; |
| 9 |
|
| 10 |
procedure TestRTTI; |
| 11 |
|
| 12 |
Var |
| 13 |
JS : TJSONStreamer; |
| 14 |
|
| 15 |
|
| 16 |
implementation |
| 17 |
|
| 18 |
Type |
| 19 |
|
| 20 |
{ TTestItem } |
| 21 |
|
| 22 |
TTestItem = Class(TCollectionItem) |
| 23 |
private |
| 24 |
FStrProp: String; |
| 25 |
Published |
| 26 |
Property StrProp : String Read FStrProp Write FStrProp; |
| 27 |
end; |
| 28 |
|
| 29 |
{ TCollComponent } |
| 30 |
|
| 31 |
TCollComponent = Class(TComponent) |
| 32 |
private |
| 33 |
FCollProp: TCollection; |
| 34 |
Published |
| 35 |
Property CollProp : TCollection Read FCollProp Write FCollProp; |
| 36 |
end; |
| 37 |
|
| 38 |
{ TCompComponent } |
| 39 |
|
| 40 |
TCompComponent = Class(TComponent) |
| 41 |
private |
| 42 |
FCompProp: TComponent; |
| 43 |
Published |
| 44 |
Property CompProp : TComponent Read FCompProp Write FCompProp; |
| 45 |
end; |
| 46 |
|
| 47 |
TDice = (one,two,three,four,five,six); |
| 48 |
TThrow = Set of TDice; |
| 49 |
|
| 50 |
{ TEnumComponent } |
| 51 |
|
| 52 |
TEnumComponent = Class(TComponent) |
| 53 |
private |
| 54 |
FDice: TDice; |
| 55 |
Published |
| 56 |
Property Dice : TDice Read FDice Write FDice; |
| 57 |
end; |
| 58 |
|
| 59 |
{ TSetComponent } |
| 60 |
|
| 61 |
TSetComponent = Class(TComponent) |
| 62 |
private |
| 63 |
FThrow: TThrow; |
| 64 |
Published |
| 65 |
Property Throw : TThrow Read FThrow Write FThrow; |
| 66 |
end; |
| 67 |
|
| 68 |
{ TChildComponent } |
| 69 |
|
| 70 |
TChildComponent = Class(TComponent) |
| 71 |
Protected |
| 72 |
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override; |
| 73 |
end; |
| 74 |
|
| 75 |
{ TChildComponent } |
| 76 |
|
| 77 |
procedure TChildComponent.GetChildren(Proc: TGetChildProc; Root: TComponent); |
| 78 |
Var |
| 79 |
I : Integer; |
| 80 |
begin |
| 81 |
Writeln('Children',ComponentCount); |
| 82 |
For I:=0 to ComponentCount-1 do |
| 83 |
Proc(Components[i]); |
| 84 |
end; |
| 85 |
|
| 86 |
Procedure DumpObject(const Header : String; var O : TJSONData); |
| 87 |
|
| 88 |
begin |
| 89 |
Writeln(Header,' : '); |
| 90 |
Writeln(O.FormatJSON()); |
| 91 |
writeln; |
| 92 |
FreeAndNil(O); |
| 93 |
JS.Options:=[]; |
| 94 |
end; |
| 95 |
|
| 96 |
Procedure DemoObject; |
| 97 |
|
| 98 |
Var |
| 99 |
C : TComponent; |
| 100 |
O : TJSONData; |
| 101 |
|
| 102 |
begin |
| 103 |
C:=TComponent.Create(Nil); |
| 104 |
try |
| 105 |
C.Name:='DemoComponent'; |
| 106 |
C.Tag:=23; |
| 107 |
O:=JS.ObjectToJSON(C); |
| 108 |
DumpObject('Complete component',O); |
| 109 |
finally |
| 110 |
FreeAndNil(C); |
| 111 |
end; |
| 112 |
end; |
| 113 |
|
| 114 |
Procedure DemoComponentObject; |
| 115 |
|
| 116 |
Var |
| 117 |
C : TCompComponent; |
| 118 |
O : TJSONData; |
| 119 |
|
| 120 |
begin |
| 121 |
C:=TCompComponent.Create(Nil); |
| 122 |
try |
| 123 |
C.Name:='DemoComponent'; |
| 124 |
C.Tag:=23; |
| 125 |
C.CompProp:=TCompComponent.Create(C); |
| 126 |
C.CompProp.Name:='SubComponent'; |
| 127 |
C.CompProp.Tag:=45; |
| 128 |
O:=JS.ObjectToJSON(C); |
| 129 |
DumpObject('Component-valued property',O); |
| 130 |
TCompComponent(C.CompProp).FComponentStyle:=[csSubComponent]; |
| 131 |
O:=JS.ObjectToJSON(C); |
| 132 |
DumpObject('Component-valued property, csSubComponent in Componentstyle',O); |
| 133 |
TCompComponent(C.CompProp).FComponentStyle:=[]; |
| 134 |
JS.options:=[jsoComponentsInline]; |
| 135 |
O:=JS.ObjectToJSON(C); |
| 136 |
DumpObject('Component-valued property, options:=[jsoComponentsInline] ',O); |
| 137 |
finally |
| 138 |
FreeAndNil(C); |
| 139 |
end; |
| 140 |
end; |
| 141 |
|
| 142 |
Procedure DemoChildObject; |
| 143 |
|
| 144 |
Var |
| 145 |
C : TChildComponent; |
| 146 |
O : TJSONData; |
| 147 |
|
| 148 |
begin |
| 149 |
C:=TChildComponent.Create(Nil); |
| 150 |
try |
| 151 |
C.Name:='ParentComponent'; |
| 152 |
C.Tag:=23; |
| 153 |
With TComponent.Create(C) do |
| 154 |
begin |
| 155 |
Name:='Child1'; |
| 156 |
Tag:=1; |
| 157 |
end; |
| 158 |
With TComponent.Create(C) do |
| 159 |
begin |
| 160 |
Name:='Child2'; |
| 161 |
Tag:=2; |
| 162 |
end; |
| 163 |
O:=JS.ObjectToJSON(C); |
| 164 |
DumpObject('Component with children, default options',O); |
| 165 |
JS.Options:=[jsoStreamChildren]; |
| 166 |
O:=JS.ObjectToJSON(C); |
| 167 |
DumpObject('Component with children, Options:=[jsoStreamChildren]',O); |
| 168 |
finally |
| 169 |
FreeAndNil(C); |
| 170 |
end; |
| 171 |
end; |
| 172 |
|
| 173 |
Procedure DemoEnumObject; |
| 174 |
|
| 175 |
Var |
| 176 |
C : TEnumComponent; |
| 177 |
O : TJSONData; |
| 178 |
|
| 179 |
begin |
| 180 |
C:=TEnumComponent.Create(Nil); |
| 181 |
try |
| 182 |
C.Dice:=Three; |
| 183 |
O:=JS.ObjectToJSON(C); |
| 184 |
DumpObject('Enumerated-typed property, default settings',O); |
| 185 |
JS.Options:=[jsoEnumeratedAsInteger]; |
| 186 |
O:=JS.ObjectToJSON(C); |
| 187 |
DumpObject('Enumerated-typed property, Options:=[jsoEnumeratedAsInteger];',O); |
| 188 |
finally |
| 189 |
FreeAndNil(C); |
| 190 |
end; |
| 191 |
end; |
| 192 |
|
| 193 |
Procedure DemoSetObject; |
| 194 |
|
| 195 |
Var |
| 196 |
C : TSetComponent; |
| 197 |
O : TJSONData; |
| 198 |
|
| 199 |
begin |
| 200 |
C:=TSetComponent.Create(Nil); |
| 201 |
try |
| 202 |
C.Throw:=[two,five]; |
| 203 |
O:=JS.ObjectToJSON(C); |
| 204 |
DumpObject('set-typed property, default settings',O); |
| 205 |
JS.Options:=[jsoSetAsString]; |
| 206 |
O:=JS.ObjectToJSON(C); |
| 207 |
DumpObject('Set-typed property, Options:=[jsoSetAsString];',O); |
| 208 |
JS.Options:=[jsoSetAsString,jsoSetBrackets]; |
| 209 |
O:=JS.ObjectToJSON(C); |
| 210 |
DumpObject('Set-typed property, Options:=[jsoSetAsString,jsoSetBrackets];',O); |
| 211 |
JS.Options:=[jsoSetEnumeratedAsInteger]; |
| 212 |
O:=JS.ObjectToJSON(C); |
| 213 |
DumpObject('Set-typed property, Options:=[jsoSetEnumeratedAsInteger];',O); |
| 214 |
finally |
| 215 |
FreeAndNil(C); |
| 216 |
end; |
| 217 |
end; |
| 218 |
|
| 219 |
Procedure DemoObjectAsString; |
| 220 |
|
| 221 |
Var |
| 222 |
C : TComponent; |
| 223 |
|
| 224 |
begin |
| 225 |
C:=TComponent.Create(Nil); |
| 226 |
try |
| 227 |
C.Name:='DemoComponent'; |
| 228 |
C.Tag:=23; |
| 229 |
Writeln('Complete component, directly as string :'); |
| 230 |
Writeln(JS.ObjectToJSONString(C)); |
| 231 |
JS.Options:=[jsoUseFormatString]; |
| 232 |
Writeln('Complete component, directly as string (Options:=[jsoUseFormatString]):'); |
| 233 |
Writeln(JS.ObjectToJSONString(C)); |
| 234 |
JS.Options:=[]; |
| 235 |
Writeln(''); |
| 236 |
finally |
| 237 |
FreeAndNil(C); |
| 238 |
end; |
| 239 |
end; |
| 240 |
|
| 241 |
Procedure DemoStrings; |
| 242 |
|
| 243 |
Var |
| 244 |
S : TStrings; |
| 245 |
O : TJSONData; |
| 246 |
C : TComponent; |
| 247 |
|
| 248 |
begin |
| 249 |
S:=TStringList.Create; |
| 250 |
try |
| 251 |
S.Add('One'); |
| 252 |
S.Add('two'); |
| 253 |
S.Add('Three'); |
| 254 |
O:=JS.StreamTStrings(S); |
| 255 |
DumpObject('Default TStrings',O); |
| 256 |
O:=JS.StreamTStringsArray(S); |
| 257 |
DumpObject('TStrings as array',O); |
| 258 |
C:=TComponent.Create(Nil); |
| 259 |
try |
| 260 |
C.Name:='SubComponent'; |
| 261 |
C.Tag:=12; |
| 262 |
S.Objects[0]:=C; |
| 263 |
O:=JS.StreamTStringsObject(S); |
| 264 |
DumpObject('TStrings as object',O); |
| 265 |
Writeln('TStrings Directly as JSON string, array'); |
| 266 |
Writeln(JS.StringsToJSON(S,False)); |
| 267 |
Writeln; |
| 268 |
Writeln('TStrings Directly as JSON string, object'); |
| 269 |
Writeln(JS.StringsToJSON(S,True)); |
| 270 |
Writeln; |
| 271 |
O:=JS.ObjectToJSON(S); |
| 272 |
DumpObject('Passing TStrings to ObjctToJSON',O); |
| 273 |
JS.Options:=[jsoTstringsAsArray]; |
| 274 |
O:=JS.ObjectToJSON(S); |
| 275 |
DumpObject('Passing TStrings to ObjctToJSON (Options:=[jsoTstringsAsArray])',O); |
| 276 |
JS.Options:=[jsoTstringsAsObject]; |
| 277 |
O:=JS.ObjectToJSON(S); |
| 278 |
DumpObject('Passing TStrings to ObjctToJSON (Options:=[jsoTstringsAsObject])',O); |
| 279 |
finally |
| 280 |
FreeAndNil(C); |
| 281 |
end; |
| 282 |
finally |
| 283 |
FreeAndNil(S); |
| 284 |
end; |
| 285 |
end; |
| 286 |
|
| 287 |
Procedure DemoCollection; |
| 288 |
|
| 289 |
Var |
| 290 |
C : TCollection; |
| 291 |
CC : TCollComponent; |
| 292 |
O : TJSONData; |
| 293 |
|
| 294 |
begin |
| 295 |
C:=TCollection.Create(TTestItem); |
| 296 |
try |
| 297 |
(C.Add as TTestItem).StrProp:='One'; |
| 298 |
(C.Add as TTestItem).StrProp:='Two'; |
| 299 |
(C.Add as TTestItem).StrProp:='Three'; |
| 300 |
CC:=TCollComponent.Create(Nil); |
| 301 |
try |
| 302 |
CC.CollProp:=C; |
| 303 |
O:=JS.ObjectToJSON(CC); |
| 304 |
DumpObject('Collection property',O); |
| 305 |
O:=JS.StreamCollection(C); |
| 306 |
DumpObject('StreamCollection result',O); |
| 307 |
O:=JS.ObjectToJSON(C); |
| 308 |
DumpObject('Passing collection to ObjectToJSON (returns an object)',O); |
| 309 |
Writeln('Direct Collection to JSON String :'); |
| 310 |
Writeln(JS.CollectionToJSON(C)); |
| 311 |
Writeln; |
| 312 |
finally |
| 313 |
FreeAndNil(CC); |
| 314 |
end; |
| 315 |
finally |
| 316 |
FreeAndNil(C); |
| 317 |
end; |
| 318 |
end; |
| 319 |
|
| 320 |
|
| 321 |
Procedure DemoVariant; |
| 322 |
|
| 323 |
Var |
| 324 |
V : Variant; |
| 325 |
O : TJSONData; |
| 326 |
I : integer; |
| 327 |
|
| 328 |
begin |
| 329 |
V:=3; |
| 330 |
O:=JS.StreamVariant(V); |
| 331 |
DumpObject('Simple integer variant streaming',O); |
| 332 |
V:=EncodeDate(2010,12,24); |
| 333 |
O:=JS.StreamVariant(V); |
| 334 |
DumpObject('Date variant streaming',O); |
| 335 |
JS.Options:=[jsoDateTimeAsString]; |
| 336 |
O:=JS.StreamVariant(V); |
| 337 |
DumpObject('Date variant streaming (Options:=[jsoDateTimeAsString];)',O); |
| 338 |
V:=VarArrayCreate([1,10],varInteger); |
| 339 |
For I:=1 to 10 do |
| 340 |
V[i]:=11-I; |
| 341 |
O:=JS.StreamVariant(V); |
| 342 |
DumpObject('Variant arrays also work',O); |
| 343 |
Writeln('Variant to JSON string :'); |
| 344 |
Writeln(JS.VariantToJSON(V)); |
| 345 |
Writeln('Variant to JSON string, with formatting :'); |
| 346 |
JS.Options:=[jsoUseFormatString]; |
| 347 |
Writeln(JS.VariantToJSON(V)); |
| 348 |
JS.Options:=[]; |
| 349 |
end; |
| 350 |
|
| 351 |
procedure TestRTTI; |
| 352 |
begin |
| 353 |
JS:=TJSONStreamer.Create(Nil); |
| 354 |
try |
| 355 |
DemoObject; |
| 356 |
DemoObjectAsString; |
| 357 |
DemoComponentObject; |
| 358 |
DemoEnumObject; |
| 359 |
DemoSetObject; |
| 360 |
DemoStrings; |
| 361 |
DemoCollection; |
| 362 |
DemoVariant; |
| 363 |
DemoChildObject; |
| 364 |
Finally |
| 365 |
FreeAndNil(JS); |
| 366 |
end; |
| 367 |
end; |
| 368 |
|
| 369 |
end. |