forked from mnf71/udr-lkJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uLkJSON.pas
2704 lines (2454 loc) · 70.6 KB
/
uLkJSON.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
LkJSON v1.07+
23 november 2020
* Copyright (c) 2006,2007,2008,2009 Leonid Koninin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Leonid Koninin ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Leonid Koninin BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
changes:
v1.07 23/11/2020 * changed lkJSON source code for Firebird UDR-engine
+ add conv params for disable UTF8 conversion
* other improve & fix
v1.07 06/11/2009 * fixed a bug in js_string - thanks to Andrew G. Khodotov
* fixed error with Double-slashes - thanks to anonymous user
* fixed a BOM bug in parser, thanks to jasper_dale
v1.06 13/03/2009 * fixed a bug in string parsing routine
* looked routine from the Adrian M. Jones, and get some
ideas from it; thanks a lot, Adrian!
* checked error reported by phpop and fix it in the string
routine; also, thanks for advice.
v1.05 26/01/2009 + added port to D2009 by Daniele Teti, thanx a lot! really,
i haven'T^ the 2009 version, so i can'T^ play with it. I was
add USE_D2009 directive below, disabled by default
* fixed two small bugs in parsing object: errors with empty
object and list; thanx to RSDN's delphi forum members
* fixed "[2229135] Value deletion is broken" tracker
issue, thanx to anonymous sender provided code for
tree version
* fixed js_string according to "[1917047] (much) faster
js_string Parse" tracker issue by Joao Inacio; a lot of
thanx, great speedup!
v1.04 05/04/2008 + a declaration of Field property moved from TlkJSONobject
to TlkJSONbase; thanx for idea to Andrey Lukyanov; this
improve objects use, look the bottom of SAMPLE2.DPR
* fixed field name in TlkJSONobject to WideString
v1.03 14/03/2008 + added a code for generating readable JSON text, sended to
me by Kusnassriyanto Saiful Bahri, thanx to him!
* from this version, library distributed with BSD
license, more pleasure for commercial programmers :)
* was rewritten internal storing of objects, repacing
hash tables with balanced trees (AA tree, by classic
author's Variant). On mine machine, with enabled fastmm,
tree Variant is about 30% slower in from-zero creation,
but about 50% faster in parsing; also deletion of
objects will be much faster than a hash-one.
Hashes (old-style) can be switched on by enabling
USE_HASH directive below
v1.02 14/09/2007 * fix mistypes in diffrent places; thanx for reports
to Aleksandr Fedorov and Tobias Wrede
v1.01 18/05/2007 * fix small bug in new text generation routine, check
library for leaks by fastmm4; thanx for idea and comments
for Glynn Owen
v1.00 12/05/2007 * some fixes in new code (mistypes, mistypes...)
* also many fixes by ideas of Henri Gourvest - big thanx
for him again; he send me code for thread-safe initializing
of hash table, some FPC-compatible issues (not tested by
mySelf) and better code for localization in latest
delphi versions; very, very big thanx!
* rewritten procedure of json text generating, with wich
work of it speeds up 4-5 times (on test) its good for
a large objects
* started a large work for making source code Self-doc
(not autodoc!)
v0.99 10/05/2007 + add functions to list and object:
function getInt(Idx: Integer): Integer;
function getString(Idx: Integer): String;
function getWideString(Idx: Integer):WideString;
function getDouble(Idx: Integer): Double;
function getBoolean(Idx: Integer): Boolean;
+ add overloaded functions to object:
function getDouble(nm: String): Double; overload;
function getInt(nm: String): Integer; overload;
function getString(nm: String): String; overload;
function getWideString(nm: String): WideString; overload;
function getBoolean(nm: String): Boolean; overload;
* changed storing mech of TlkJSONcustomlist descendants from
dynamic array to TList; this gives us great speedup with
lesser changes; thanx for idea to Henri Gourvest
* also reworked hashtable to work with TList, so it also
increase speed of work
v0.98 09/05/2007 * fix small bug in work with WideStrings(UTF8), thanx to
IVO GELOV to description and sources
v0.97 10/04/2007 + add capabilities to work with KOL delphi projects; for
this will define KOL variable in begin of text; of course,
in this case object TlkJSONstreamed is not compiled.
v0.96 03/30/2007 + add TlkJSONFuncEnum and method ForEach in all
TlkJSONcustomlist descendants
+ add property UseHash(r/o) to TlkJSONobject, and parameter
UseHash:Boolean to object constructors; set it to false
allow to disable using of hash-table, what can increase
speed of work in case of objects with low number of
methods(fields); [by default it is True]
+ added conditional compile directive DOTNET for use in .Net
based delphi versions; remove dot in declaration below
(thanx for idea and sample code to Tim Radford)
+ added property HashOf to TlkHashTable to allow use of
users hash functions; on enter is widestring, on exit is
Cardinal (32 bit unsigned). Original HashOf renamed to
DefaultHashOf
* hash table object of TlkJSONobject wrapped by property called
HashTable
* fixed some minor bugs
v0.95 03/29/2007 + add object TlkJSONstreamed what descendant of TlkJSON and
able to load/save JSON objects from/to streams/files.
* fixed small bug in generating of unicode strings representation
v0.94 03/27/2007 + add properties NameOf and FieldByIndex to TlkJSONobject
* fix small error in parsing unicode chars
* small changes in hashing code (try to speed up)
v0.93 03/05/2007 + add overloaded functions to list and object
+ add enum type TlkJSONtypes
+ add functions: SelfType:TlkJSONtypes and
SelfTypeName: String to every TlkJSONbase child
* fix mistype 'IndefOfName' to 'IndexOfName'
* fix mistype 'IndefOfObject' to 'IndexOfObject'
v0.92 03/02/2007 + add some fix to TlkJSON.ParseText to fix bug with parsing
objects - object methods not always added properly
to hash array (thanx to Chris Matheson)
...
}
unit uLkJSON;
{$IFDEF FPC}
{$MODE OBJFPC}
{.$DEFINE HAVE_FORMATSETTING}
{$H+}
{$ELSE}
{$IF RTLVersion > 14.00}
{$DEFINE HAVE_FORMATSETTING}
{$IF RTLVersion > 19.00}
{$DEFINE USE_D2009}
{$IFEND}
{$H+}
{$IFEND}
{.$DEFINE DOTNET}
{$ENDIF}
interface
{$DEFINE NEW_STYLE_GENERATE}
{.$DEFINE USE_HASH}
{.$DEFINE TCB_EXT}
{.$DEFINE STREAM}
uses Classes, SysUtils, Variants;
type
TlkJSONtypes =
(jsBase, jsNumber, jsString, jsBoolean, jsNull, jsList, jsObject);
{$IFDEF DOTNET}
TlkJSONdotnetclass = class
public
constructor Create;
destructor Destroy; override;
procedure AfterConstruction; virtual;
procedure BeforeDestruction; virtual;
end;
{$ENDIF DOTNET}
TlkJSONbase = class{$IFDEF DOTNET}(TlkJSONdotnetclass){$ENDIF}
protected
function GetValue: Variant; virtual;
procedure SetValue(const AValue: Variant); virtual;
function GetChild(Idx: Integer): TlkJSONbase; virtual;
procedure SetChild(Idx: Integer; const AValue: TlkJSONbase);
virtual;
function GetCount: Integer; virtual;
function GetField(aName: Variant):TlkJSONbase; virtual;
public
{ исправлено
property Field[aName: Variant]: TlkJSONbase read GetField;
}
property Field[aName: Variant]: TlkJSONbase read GetField; default;
property Count: Integer read GetCount;
property Child[Idx: Integer]: TlkJSONbase read GetChild write SetChild;
property Value: Variant read GetValue write SetValue;
class function SelfType: TlkJSONtypes; virtual;
class function SelfTypeName: String; virtual;
end;
TlkJSONnumber = class(TlkJSONbase)
protected
FValue: Extended;
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
public
procedure AfterConstruction; override;
class function Generate(AValue: Extended = 0): TlkJSONnumber;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONstring = class(TlkJSONbase)
protected
FValue: WideString;
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
public
procedure AfterConstruction; override;
class function Generate(const wsValue: WideString = ''):
TlkJSONstring;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONboolean = class(TlkJSONbase)
protected
FValue: Boolean;
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
public
procedure AfterConstruction; override;
class function Generate(AValue: Boolean = True): TlkJSONboolean;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONnull = class(TlkJSONbase)
protected
function GetValue: Variant; override;
{ -
function Generate: TlkJSONnull;
}
public
{ + }
class function Generate: TlkJSONnull;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONFuncEnum = procedure
(ElName: String; Elem: TlkJSONbase; Data: Pointer; var Continue: Boolean)
of object;
TlkJSONcustomlist = class(TlkJSONbase)
protected
{
FValue: array of TlkJSONbase;
}
fList: TList;
function GetCount: Integer; override;
function GetChild(Idx: Integer): TlkJSONbase; override;
procedure SetChild(Idx: Integer; const AValue: TlkJSONbase);
override;
function ForEachElement(Idx: Integer; var nm: String):
TlkJSONbase; virtual;
function GetField(aName: Variant):TlkJSONbase; override;
function _Add(Obj: TlkJSONbase): Integer; virtual;
procedure _Delete(iIndex: Integer); virtual;
function _IndexOf(Obj: TlkJSONbase): Integer; virtual;
public
procedure ForEach(fnCallBack: TlkJSONFuncEnum; pUserData:
pointer);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function getInt(Idx: Integer): Integer; virtual;
function getString(Idx: Integer): String; virtual;
function getWideString(Idx: Integer): WideString; virtual;
function getDouble(Idx: Integer): Double; virtual;
function getBoolean(Idx: Integer): Boolean; virtual;
end;
TlkJSONlist = class(TlkJSONcustomlist)
protected
public
function Add(Obj: TlkJSONbase): Integer; overload;
function Add(bool: Boolean): Integer; overload;
function Add(nmb: Double): Integer; overload;
function Add(s: String): Integer; overload;
function Add(const ws: WideString): Integer; overload;
function Add(inmb: Integer): Integer; overload;
procedure Delete(Idx: Integer);
function IndexOf(Obj: TlkJSONbase): Integer;
class function Generate: TlkJSONlist;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONobjectmethod = class(TlkJSONbase)
protected
FValue: TlkJSONbase;
FName: WideString;
procedure SetName(const AValue: WideString);
public
property ObjValue: TlkJSONbase read FValue;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property Name: WideString read FName write SetName;
class function Generate(const aName: WideString; aobj: TlkJSONbase):
TlkJSONobjectmethod;
end;
{$IFDEF USE_HASH}
PlkHashItem = ^TlkHashItem;
TlkHashItem = packed record
hash: Cardinal;
index: Integer;
end;
TlkHashFunction = function(const ws: WideString): Cardinal of
object;
TlkHashTable = class
private
FParent: TObject; // TCB: parent for check chaining op.
FHashFunction: TlkHashFunction;
procedure SetHashFunction(const AValue: TlkHashFunction);
protected
a_x: array[0..255] of TList;
procedure hswap(j, k, l: Integer);
function InTable(const ws: WideString; var i, j, k: Cardinal):
Boolean;
public
function counters: String;
function DefaultHashOf(const ws: WideString): Cardinal;
function SimpleHashOf(const ws: WideString): Cardinal;
property HashOf: TlkHashFunction read FHashFunction write
SetHashFunction;
function IndexOf(const ws: WideString): Integer;
procedure AddPair(const ws: WideString; Idx: Integer);
procedure Delete(const ws: WideString);
constructor Create;
destructor Destroy; override;
end;
{$ELSE}
{
Implementation based on "Arne Andersson, Balanced Search Trees Made Simpler"
}
PlkBalNode = ^TlkBalNode;
TlkBalNode = packed record
Left,
Right: PlkBalNode;
Level: Byte;
Key: Integer;
nm: WideString;
end;
TlkBalTree = class
protected
fDeleted, fLast, fBottom, fRoot: PlkBalNode;
procedure Skew(var T: PlkBalNode);
procedure Split(var T: PlkBalNode);
public
procedure Clear;
function Counters: String;
function Insert(const ws: WideString; X: Integer): Boolean;
function Delete(const ws: WideString): Boolean;
function IndexOf(const ws: WideString): Integer;
constructor Create;
destructor Destroy; override;
end;
{$ENDIF USE_HASH}
TlkJSONobject = class(TlkJSONcustomlist)
protected
{$IFDEF USE_HASH}
ht: TlkHashTable;
{$ELSE}
ht: TlkBalTree;
{$ENDIF USE_HASH}
FUseHash: Boolean;
function GetFieldByIndex(Idx: Integer): TlkJSONbase;
function GetNameOf(Idx: Integer): WideString;
procedure SetFieldByIndex(Idx: Integer; const AValue: TlkJSONbase);
{$IFDEF USE_HASH}
function GetHashTable: TlkHashTable;
{$ELSE}
function GetHashTable: TlkBalTree;
{$ENDIF USE_HASH}
function ForEachElement(Idx: Integer; var nm: String): TlkJSONbase;
override;
function GetField(aName: Variant):TlkJSONbase; override;
public
property UseHash: Boolean read FUseHash;
{$IFDEF USE_HASH}
property HashTable: TlkHashTable read GetHashTable;
{$ELSE}
property HashTable: TlkBalTree read GetHashTable;
{$ENDIF USE_HASH}
constructor Create(bUseHash: Boolean = True);
destructor Destroy; override;
function Add(const aName: WideString; aobj: TlkJSONbase): Integer;
overload;
function OldGetField(nm: WideString): TlkJSONbase;
procedure OldSetField(nm: WideString; const AValue: TlkJSONbase);
function Add(const aName: WideString; bool: Boolean): Integer; overload;
function Add(const aName: WideString; nmb: Double): Integer; overload;
function Add(const aName: WideString; inmb: Integer): Integer; overload;
function Add(const aName: WideString; s: String): Integer; overload;
function Add(const aName: WideString; const ws: WideString): Integer;
overload;
procedure Delete(Idx: Integer);
function IndexOfName(const aName: WideString): Integer;
function IndexOfObject(aobj: TlkJSONbase): Integer;
property Field[nm: WideString]: TlkJSONbase read OldGetField
write OldSetField; default;
class function Generate(AUseHash: Boolean = True): TlkJSONobject;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
property FieldByIndex[Idx: Integer]:
TlkJSONbase read GetFieldByIndex write SetFieldByIndex;
property NameOf[Idx: Integer]: WideString read GetNameOf;
function getBoolean(Idx: Integer): Boolean; overload; override;
function getDouble(Idx: Integer): Double; overload; override;
function getInt(Idx: Integer): Integer; overload; override;
function getString(Idx: Integer): String; overload; override;
function getWideString(Idx: Integer): WideString; overload; override;
function {$ifdef TCB_EXT}getBooleanFromName{$else}getBoolean{$endif}
(nm: String): Boolean; overload;
function {$ifdef TCB_EXT}getDoubleFromName{$else}getDouble{$endif}
(nm: String): Double; overload;
function {$ifdef TCB_EXT}getIntFromName{$else}getInt{$endif}
(nm: String): Integer; overload;
function {$ifdef TCB_EXT}getStringFromName{$else}getString{$endif}
(nm: String): String; overload;
function {$ifdef TCB_EXT}getWideStringFromName{$else}getWideString{$endif}
(nm: String): WideString; overload;
end;
PlkJSONobject = ^TlkJSONobject;
TlkJSON = class
public
{ исправлено
class function ParseText(const Txt: String): TlkJSONbase;
class function GenerateText(Obj: TlkJSONbase): String;
}
class function ParseText(const Txt: String; Conv: Boolean = False): TlkJSONbase;
class function GenerateText(Obj: TlkJSONbase; Conv: Boolean = False): String;
end;
PlkJSON = ^TlkJSON;
{$IFDEF STREAM}
TlkJSONstreamed = class(TlkJSON)
{ исправлено
class function LoadFromStream(Src: TStream): TlkJSONbase;
class procedure SaveToStream(Obj: TlkJSONbase; dst: TStream);
class function LoadFromFile(SrcName: String): TlkJSONbase;
class procedure SaveToFile(Obj: TlkJSONbase; DstName: String);
}
class function LoadFromStream(Src: TStream; Conv: Boolean = False): TlkJSONbase;
class procedure SaveToStream(Obj: TlkJSONbase; Dst: TStream; conv: Boolean = False);
class function LoadFromFile(SrcName: String; Conv: Boolean = False): TlkJSONbase;
class procedure SaveToFile(Obj: TlkJSONbase; DstName: String; conv: Boolean = False);
end;
{$ENDIF STREAM}
{ исправлено
function GenerateReadableText(vObj: TlkJSONbase; var vLevel:
Integer): String;
}
function GenerateReadableText
(vObj: TlkJSONbase; var vLevel: Integer; conv: Boolean = False;
nested: Boolean = False
): String;
implementation
uses { исправлено Math, } StrUtils;
type
ElkIntException = class(Exception)
protected
Idx: Integer;
public
constructor Create(AIdx: Integer; AMsg: String);
end;
// author of next two functions is Kusnassriyanto Saiful Bahri
function Indent(vTab: Integer): String;
begin
Result := DupeString(' ', vTab);
end;
{ исправлено
function GenerateReadableText(vObj: TlkJSONbase; var vLevel:
Integer): String;
}
function GenerateReadableText
(vObj: TlkJSONbase; var vLevel: Integer; conv: Boolean = False;
nested: Boolean = False
): String;
var
i: Integer;
vStr: String;
xs: TlkJSONstring;
begin
vLevel := vLevel + 1;
if vObj is TlkJSONObject then
begin
vStr := '';
for i := 0 to TlkJSONobject(vObj).Count - 1 do
begin
if vStr <> '' then
begin
vStr := vStr + ','#13#10;
end;
vStr := vStr + Indent(vLevel) +
{ исправлено
GenerateReadableText(TlkJSONobject(vObj).Child[i], vLevel);
}
GenerateReadableText(TlkJSONobject(vObj).Child[i], vLevel, conv, True);
end;
if vStr <> '' then
begin
vStr := '{'#13#10 + vStr + #13#10 + Indent(vLevel - 1) + '}';
end
else
begin
vStr := '{}';
end;
if not nested then
vStr := Indent(vLevel - 1) + vStr;
Result := vStr;
end
else if vObj is TlkJSONList then
begin
vStr := '';
for i := 0 to TlkJSONList(vObj).Count - 1 do
begin
if vStr <> '' then
begin
vStr := vStr + ','#13#10;
end;
vStr := vStr + Indent(vLevel) +
{ исправлено
GenerateReadableText(TlkJSONList(vObj).Child[i], vLevel);
}
GenerateReadableText(TlkJSONList(vObj).Child[i], vLevel, conv, True);
end;
if vStr <> '' then
begin
vStr := '['#13#10 + vStr + #13#10 + Indent(vLevel - 1) + ']';
end
else
begin
vStr := '[]';
end;
if not nested then
vStr := Indent(vLevel - 1) + vStr;
Result := vStr;
end
else if vObj is TlkJSONobjectmethod then
begin
vStr := '';
xs := TlkJSONstring.Create;
try
xs.Value := TlkJSONobjectMethod(vObj).Name;
{ исправлено
vStr := GenerateReadableText(xs, vLevel);
}
vStr := GenerateReadableText(xs, vLevel, conv, True);
vLevel := vLevel - 1;
{ исправлено
vStr := vStr + ':' + GenerateReadableText(TlkJSONbase(TlkJSONobjectmethod(vObj).ObjValue), vLevel);
}
vStr := vStr + ':' + GenerateReadableText(TlkJSONbase(TlkJSONobjectmethod(vObj).ObjValue), vLevel, Conv, True);
vLevel := vLevel + 1;
Result := vStr;
finally
xs.Free;
end;
end
else
begin
if vObj is TlkJSONobjectmethod then
begin
if TlkJSONobjectMethod(vObj).Name <> '' then
begin
end;
end;
{ исправлено
Result := TlkJSON.GenerateText(vObj);
}
Result := TlkJSON.GenerateText(vObj, conv);
end;
vLevel := vLevel - 1;
end;
// author of this routine is IVO GELOV
function Code2UTF(iNumber: Integer): UTF8String;
begin
Result := '';
if iNumber < 128 then Result := Chr(iNumber)
else if iNumber < 2048 then
Result := Chr((iNumber shr 6) + 192) + Chr((iNumber and 63) + 128)
else if iNumber < 65536 then
Result := Chr((iNumber shr 12) + 224) + Chr(((iNumber shr 6) and
63) + 128) + Chr((iNumber and 63) + 128)
else if iNumber < 2097152 then
Result := Chr((iNumber shr 18) + 240) + Chr(((iNumber shr 12) and
63) + 128) + Chr(((iNumber shr 6) and 63) + 128) +
Chr((iNumber and 63) + 128);
end;
{ TlkJSONbase }
function TlkJSONbase.GetChild(Idx: Integer): TlkJSONbase;
begin
Result := Nil;
end;
function TlkJSONbase.GetCount: Integer;
begin
Result := 0;
end;
function TlkJSONbase.GetField(aName: Variant):TlkJSONbase;
begin
Result := Self;
end;
function TlkJSONbase.GetValue: Variant;
begin
Result := Variants.Null;
end;
class function TlkJSONbase.SelfType: TlkJSONtypes;
begin
Result := jsBase;
end;
class function TlkJSONbase.SelfTypeName: String;
begin
Result := 'jsBase';
end;
procedure TlkJSONbase.SetChild(Idx: Integer; const AValue:
TlkJSONbase);
begin
end;
procedure TlkJSONbase.SetValue(const AValue: Variant);
begin
end;
{ TlkJSONnumber }
procedure TlkJSONnumber.AfterConstruction;
begin
inherited;
FValue := 0;
end;
class function TlkJSONnumber.Generate(AValue: Extended):
TlkJSONnumber;
begin
Result := TlkJSONnumber.Create;
Result.FValue := AValue;
end;
function TlkJSONnumber.GetValue: Variant;
begin
Result := FValue;
end;
class function TlkJSONnumber.SelfType: TlkJSONtypes;
begin
Result := jsNumber;
end;
class function TlkJSONnumber.SelfTypeName: String;
begin
Result := 'jsNumber';
end;
procedure TlkJSONnumber.SetValue(const AValue: Variant);
begin
FValue := VarAsType(AValue, varDouble);
end;
{ TlkJSONstring }
procedure TlkJSONstring.AfterConstruction;
begin
inherited;
FValue := '';
end;
class function TlkJSONstring.Generate(const wsValue: WideString):
TlkJSONstring;
begin
Result := TlkJSONstring.Create;
Result.FValue := wsValue;
end;
function TlkJSONstring.GetValue: Variant;
begin
Result := FValue;
end;
class function TlkJSONstring.SelfType: TlkJSONtypes;
begin
Result := jsString;
end;
class function TlkJSONstring.SelfTypeName: String;
begin
Result := 'jsString';
end;
procedure TlkJSONstring.SetValue(const AValue: Variant);
begin
FValue := VarToWideStr(AValue);
end;
{ TlkJSONboolean }
procedure TlkJSONboolean.AfterConstruction;
begin
FValue := False;
end;
class function TlkJSONboolean.Generate(AValue: Boolean):
TlkJSONboolean;
begin
Result := TlkJSONboolean.Create;
Result.Value := AValue;
end;
function TlkJSONboolean.GetValue: Variant;
begin
Result := FValue;
end;
class function TlkJSONboolean.SelfType: TlkJSONtypes;
begin
Result := jsBoolean;
end;
class function TlkJSONboolean.SelfTypeName: String;
begin
Result := 'jsBoolean';
end;
procedure TlkJSONboolean.SetValue(const AValue: Variant);
begin
FValue := boolean(AValue);
end;
{ TlkJSONnull }
class function TlkJSONnull.Generate: TlkJSONnull;
begin
Result := TlkJSONnull.Create;
end;
function TlkJSONnull.GetValue: Variant;
begin
Result := Variants.Null;
end;
class function TlkJSONnull.SelfType: TlkJSONtypes;
begin
Result := jsNull;
end;
class function TlkJSONnull.SelfTypeName: String;
begin
Result := 'jsNull';
end;
{ TlkJSONcustomlist }
function TlkJSONcustomlist._Add(Obj: TlkJSONbase): Integer;
begin
if not Assigned(Obj) then
begin
Result := -1;
Exit;
end;
Result := fList.Add(Obj);
end;
procedure TlkJSONcustomlist.AfterConstruction;
begin
inherited;
fList := TList.Create;
end;
procedure TlkJSONcustomlist.BeforeDestruction;
var
i: Integer;
begin
for i := (Count - 1) downto 0 do _Delete(i);
fList.Free;
inherited;
end;
// renamed
procedure TlkJSONcustomlist._Delete(iIndex: Integer);
var
Idx: Integer;
begin
if not ((iIndex < 0) or (iIndex >= Count)) then
begin
if fList.Items[iIndex] <> nil then
TlkJSONbase(fList.Items[iIndex]).Free;
Idx := Pred(fList.Count);
if iIndex<idx then
begin
fList.Items[iIndex] := fList.Items[Idx];
fList.Delete(Idx);
end
else
begin
fList.Delete(iIndex);
end;
end;
end;
function TlkJSONcustomlist.GetChild(Idx: Integer): TlkJSONbase;
begin
if (Idx < 0) or (Idx >= Count) then
begin
Result := Nil;
end
else
begin
{ исправлено
Result := fList.Items[Idx];
}
Result := TlkJSONbase(fList.Items[Idx]);
end;
end;
function TlkJSONcustomlist.GetCount: Integer;
begin
Result := fList.Count;
end;
function TlkJSONcustomlist._IndexOf(Obj: TlkJSONbase): Integer;
begin
Result := fList.IndexOf(Obj);
end;
procedure TlkJSONcustomlist.SetChild(Idx: Integer; const AValue:
TlkJSONbase);
begin
if not ((Idx < 0) or (Idx >= Count)) then
begin
if fList.Items[Idx] <> Nil then
TlkJSONbase(fList.Items[Idx]).Free;
fList.Items[Idx] := AValue;
end;
end;
procedure TlkJSONcustomlist.ForEach
(fnCallBack: TlkJSONFuncEnum; pUserData: Pointer);
var
iCount: Integer;
IsContinue: Boolean;
anJSON: TlkJSONbase;
wsObject: String = '';
begin
if not Assigned(fnCallBack) then Exit;
IsContinue := True;
for iCount := 0 to GetCount - 1 do
begin
anJSON := ForEachElement(iCount, wsObject);
if Assigned(anJSON) then
fnCallBack(wsObject, anJSON, pUserData, IsContinue);
if not IsContinue then
break;
end;
end;
///---- renamed to here
function TlkJSONcustomlist.GetField(aName: Variant):TlkJSONbase;
var
index: Integer;
begin
if VarIsNumeric(aName) then
begin
index := Integer(aName);
Result := GetChild(index);
end
else
begin
Result := inherited GetField(aName);
end;
end;
function TlkJSONcustomlist.ForEachElement(Idx: Integer; var nm:
string): TlkJSONbase;
begin
nm := inttostr(Idx);
Result := GetChild(Idx);
end;
function TlkJSONcustomlist.getDouble(Idx: Integer): Double;
var
jn: TlkJSONnumber;
begin
jn := Child[Idx] as TlkJSONnumber;
if not Assigned(jn) then Result := 0
else Result := jn.Value;
end;
function TlkJSONcustomlist.getInt(Idx: Integer): Integer;
var
jn: TlkJSONnumber;
begin
jn := Child[Idx] as TlkJSONnumber;
if not Assigned(jn) then Result := 0
else Result := round(int(jn.Value));
end;
function TlkJSONcustomlist.getString(Idx: Integer): String;
var
js: TlkJSONstring;
begin
js := Child[Idx] as TlkJSONstring;
if not Assigned(js) then Result := ''
else Result := VarToStr(js.Value);
end;
function TlkJSONcustomlist.getWideString(Idx: Integer): WideString;
var
js: TlkJSONstring;
begin
js := Child[Idx] as TlkJSONstring;
if not Assigned(js) then Result := ''
else Result := VarToWideStr(js.Value);
end;
function TlkJSONcustomlist.getBoolean(Idx: Integer): Boolean;
var
jb: TlkJSONboolean;
begin
jb := Child[Idx] as TlkJSONboolean;
if not Assigned(jb) then Result := False
else Result := jb.Value;
end;
{ TlkJSONobjectmethod }
procedure TlkJSONobjectmethod.AfterConstruction;
begin
inherited;
FValue := Nil;
FName := '';
end;
procedure TlkJSONobjectmethod.BeforeDestruction;
begin