-
Notifications
You must be signed in to change notification settings - Fork 7
/
uLkJSON.pas
2732 lines (2435 loc) · 72.8 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.02
27/01/2022 * changed lkJSON source code for Firebird UDR-engine
+ TlkJSONbase was added link on parent object
* Enlargement a size for UTF8 a params
* Formating code (refact)
v1.07.01
23/11/2020 * changed lkJSON source code for Firebird UDR-engine
+ enabled Conv params for disable UTF8 conversion
+ added NULL_SQL directive for PSQL integration
* 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 objName 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} // old-style
{.$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}
private
FParentObj: TlkJSONbase;
protected
procedure AfterConstruction; override;
function GetField(const objName: Variant): TlkJSONbase; virtual;
function GetCount: Integer; virtual;
function GetParent(): TlkJSONbase; virtual;
procedure SetParent(const pObj: TlkJSONbase); virtual;
function GetChild(const Idx: Integer): TlkJSONbase; virtual;
procedure SetChild(const Idx: Integer; const Obj: TlkJSONbase); virtual;
function GetValue: Variant; virtual;
procedure SetValue(const Value: Variant); virtual;
public
property Field[objName: Variant]: TlkJSONbase read GetField; default;
property Count: Integer read GetCount;
property Parent: TlkJSONbase read GetParent;
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;
TlkJSONboolean = class(TlkJSONbase)
protected
FValue: Boolean;
function GetValue: Variant; override;
procedure SetValue(const valBool: Variant); override;
public
procedure AfterConstruction; override;
class function Generate(const valBool: Boolean = True): TlkJSONboolean;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONnumber = class(TlkJSONbase)
protected
FValue: Extended;
function GetValue: Variant; override;
procedure SetValue(const valNumber: Variant); override;
public
procedure AfterConstruction; override;
class function Generate(const valNumber: 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 valString: Variant); override;
public
procedure AfterConstruction; override;
class function Generate(const valString: WideString = ''): TlkJSONstring;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONnull = class(TlkJSONbase)
protected
function GetValue: Variant; override;
public
procedure AfterConstruction; override;
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
FList: TList;
function GetField(const objName: Variant): TlkJSONbase; override;
function GetCount: Integer; override;
function GetChild(const Idx: Integer): TlkJSONbase; override;
procedure SetChild(const Idx: Integer; const Obj: TlkJSONbase); override;
function _Add(const Obj: TlkJSONbase): Integer; virtual;
procedure _Delete(const Idx: Integer); virtual;
function _IndexOf(const Obj: TlkJSONbase): Integer; virtual;
function ForEachElement(const Idx: Integer; var objName: String): TlkJSONbase; virtual;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function getBoolean(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF})
: Boolean; virtual;
function getDouble(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF})
: Double; virtual;
function getInt(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF})
: Integer; virtual;
function getString(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF})
: String; virtual;
function getWideString(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF})
: WideString; virtual;
procedure ForEach(fnCallBack: TlkJSONFuncEnum; pUserData: Pointer);
end;
TlkJSONlist = class(TlkJSONcustomlist)
public
function IndexOf(const Obj: TlkJSONbase): Integer;
function Add(const Obj: TlkJSONbase): Integer; overload;
function Add(const valBool: Boolean): Integer; overload;
function Add(const valDouble: Double): Integer; overload;
function Add(const valInteger: Integer): Integer; overload;
function Add(const valString: String): Integer; overload;
function Add(const valWString: WideString): Integer; overload;
procedure Delete(const Idx: Integer);
class function Generate: TlkJSONlist;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
TlkJSONobjectmethod = class(TlkJSONbase)
private
FValue: TlkJSONbase;
FName: WideString;
protected
procedure SetName(const objName: WideString);
procedure SetParent(const pObj: TlkJSONbase = Nil); override;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property ObjValue: TlkJSONbase read FValue;
property Name: WideString read FName write SetName;
class function Generate(const objName: WideString; const Obj: TlkJSONbase)
: TlkJSONobjectmethod;
end;
{$IFDEF USE_HASH} // old-style
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
constructor Create;
destructor Destroy; override;
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);
end;
{$ELSE}
{
Implementation based on "Arne Andersson, Balanced Search Trees Made Simpler"
}
PlkBalNode = ^TlkBalNode;
TlkBalNode = packed record
Left,
Right: PlkBalNode;
Level: Byte;
Key: Integer;
objName: WideString;
end;
TlkBalTree = class
protected
fDeleted, fLast, fBottom, fRoot: PlkBalNode;
procedure Skew(var T: PlkBalNode);
procedure Split(var T: PlkBalNode);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function Counters: String;
function Insert(const objName: WideString; X: Integer): Boolean;
function Delete(const objName: WideString): Boolean;
function IndexOf(const objName: WideString): Integer;
end;
{$ENDIF USE_HASH}
TlkJSONobject = class(TlkJSONcustomlist)
protected
{$IFDEF USE_HASH} // old-style
ht: TlkHashTable;
{$ELSE}
ht: TlkBalTree;
{$ENDIF USE_HASH}
FUseHash: Boolean;
function GetField(const objName: Variant):TlkJSONbase; override;
function GetFieldByIndex(const Idx: Integer): TlkJSONbase;
procedure SetFieldByIndex(const Idx: Integer; const Obj: TlkJSONbase);
function GetNameOf(const Idx: Integer): WideString;
{$IFDEF USE_HASH} // old-style
function GetHashTable: TlkHashTable;
{$ELSE}
function GetHashTable: TlkBalTree;
{$ENDIF USE_HASH}
function ForEachElement(const Idx: Integer; var objName: String): TlkJSONbase; override;
public
property UseHash: Boolean read FUseHash;
{$IFDEF USE_HASH} // old-style
property HashTable: TlkHashTable read GetHashTable;
{$ELSE}
property HashTable: TlkBalTree read GetHashTable;
{$ENDIF USE_HASH}
constructor Create(bUseHash: Boolean = True);
destructor Destroy; override;
function OldGetField(const objName: WideString): TlkJSONbase;
procedure OldSetField(const objName: WideString; const Obj: TlkJSONbase);
property Field[objName: WideString]: TlkJSONbase read OldGetField write OldSetField; default;
property FieldByIndex[Idx: Integer]: TlkJSONbase read GetFieldByIndex write SetFieldByIndex;
property NameOf[Idx: Integer]: WideString read GetNameOf;
function IndexOfName(const objName: WideString): Integer;
function IndexOfObject(const Obj: TlkJSONbase): Integer;
function Add(const objName: WideString; const Obj: TlkJSONbase): Integer; overload;
function Add(const objName: WideString; const valBool: Boolean): Integer; overload;
function Add(const objName: WideString; const valDouble: Double): Integer; overload;
function Add(const objName: WideString; const valInteger: Integer): Integer; overload;
function Add(const objName: WideString; const valString: String): Integer; overload;
function Add(const objName: WideString; const valWString: WideString): Integer; overload;
function getBoolean
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Boolean; overload; override;
function getDouble
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Double; overload; override;
function getInt
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Integer; overload; override;
function getString
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): String; overload; override;
function getWideString
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): WideString; overload; override;
function {$IFDEF TCB_EXT}getBooleanFromName{$ELSE}getBoolean{$ENDIF}
(const objName: WideString{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Boolean; overload;
function {$IFDEF TCB_EXT}getDoubleFromName{$ELSE}getDouble{$ENDIF}
(const objName: WideString{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Double; overload;
function {$IFDEF TCB_EXT}getIntFromName{$ELSE}getInt{$ENDIF}
(const objName: WideString{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Integer; overload;
function {$IFDEF TCB_EXT}getStringFromName{$ELSE}getString{$ENDIF}
(const objName: WideString{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): String; overload;
function {$IFDEF TCB_EXT}getWideStringFromName{$ELSE}getWideString{$ENDIF}
(const objName: WideString{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): WideString; overload;
procedure Delete(const Idx: Integer);
class function Generate(AUseHash: Boolean = True): TlkJSONobject;
class function SelfType: TlkJSONtypes; override;
class function SelfTypeName: String; override;
end;
PlkJSONobject = ^TlkJSONobject;
TlkJSON = class
public
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; 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; Conv: Boolean = False; Nested: Boolean = False)
: String;
implementation
uses StrUtils;
type
ElkIntException = class(Exception)
protected
FExceptionIdx: Integer;
public
constructor Create(const ExceptionIdx: Integer; const Msg: 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; 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
vStr := vStr + ','#13#10;
vStr := vStr + Indent(vLevel) +
GenerateReadableText(TlkJSONobject(vObj).Child[i], vLevel, conv, True);
end;
if vStr <> '' then
vStr := '{'#13#10 + vStr + #13#10 + Indent(vLevel - 1) + '}'
else
vStr := '{}';
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
vStr := vStr + ','#13#10;
vStr := vStr + Indent(vLevel) +
GenerateReadableText(TlkJSONList(vObj).Child[i], vLevel, Conv, True);
end;
if vStr <> '' then
vStr := '['#13#10 + vStr + #13#10 + Indent(vLevel - 1) + ']'
else
vStr := '[]';
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, Conv, True);
vLevel := vLevel - 1;
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
if TlkJSONobjectMethod(vObj).Name <> '' then
begin
end;
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 }
procedure TlkJSONbase.AfterConstruction;
begin
inherited;
FParentObj := Nil;
end;
function TlkJSONbase.GetField(const objName: Variant):TlkJSONbase;
begin
Result := Self;
end;
function TlkJSONbase.GetCount: Integer;
begin
Result := 0;
end;
function TlkJSONbase.GetParent(): TlkJSONbase;
begin
Result := FParentObj;
end;
procedure TlkJSONbase.SetParent(const pObj: TlkJSONbase);
begin
FParentObj := pObj;
end;
function TlkJSONbase.GetChild(const Idx: Integer): TlkJSONbase;
begin
Result := Nil;
end;
procedure TlkJSONbase.SetChild(const Idx: Integer; const Obj: TlkJSONbase);
begin
end;
function TlkJSONbase.GetValue: Variant;
begin
Result := Variants.Null;
end;
procedure TlkJSONbase.SetValue(const Value: Variant);
begin
end;
class function TlkJSONbase.SelfType: TlkJSONtypes;
begin
Result := jsBase;
end;
class function TlkJSONbase.SelfTypeName: String;
begin
Result := 'jsBase';
end;
{ TlkJSONboolean }
procedure TlkJSONboolean.AfterConstruction;
begin
inherited;
FValue := False;
end;
function TlkJSONboolean.GetValue: Variant;
begin
Result := FValue;
end;
procedure TlkJSONboolean.SetValue(const valBool: Variant);
begin
FValue := Boolean(valBool);
end;
class function TlkJSONboolean.SelfType: TlkJSONtypes;
begin
Result := jsBoolean;
end;
class function TlkJSONboolean.Generate(const valBool: Boolean):
TlkJSONboolean;
begin
Result := TlkJSONboolean.Create;
Result.FValue := valBool;
end;
class function TlkJSONboolean.SelfTypeName: String;
begin
Result := 'jsBoolean';
end;
{ TlkJSONnumber }
procedure TlkJSONnumber.AfterConstruction;
begin
inherited;
FValue := 0;
end;
function TlkJSONnumber.GetValue: Variant;
begin
Result := FValue;
end;
procedure TlkJSONnumber.SetValue(const valNumber: Variant);
begin
FValue := VarAsType(valNumber, varDouble);
end;
class function TlkJSONnumber.Generate(const valNumber: Extended):
TlkJSONnumber;
begin
Result := TlkJSONnumber.Create;
Result.FValue := valNumber;
end;
class function TlkJSONnumber.SelfType: TlkJSONtypes;
begin
Result := jsNumber;
end;
class function TlkJSONnumber.SelfTypeName: String;
begin
Result := 'jsNumber';
end;
{ TlkJSONstring }
procedure TlkJSONstring.AfterConstruction;
begin
inherited;
FValue := '';
end;
function TlkJSONstring.GetValue: Variant;
begin
Result := FValue;
end;
procedure TlkJSONstring.SetValue(const valString: Variant);
begin
FValue := VarToWideStr(valString);
end;
class function TlkJSONstring.SelfType: TlkJSONtypes;
begin
Result := jsString;
end;
class function TlkJSONstring.Generate(const valString: WideString):
TlkJSONstring;
begin
Result := TlkJSONstring.Create;
Result.FValue := valString;
end;
class function TlkJSONstring.SelfTypeName: String;
begin
Result := 'jsString';
end;
{ TlkJSONnull }
procedure TlkJSONnull.AfterConstruction;
begin
inherited;
end;
function TlkJSONnull.GetValue: Variant;
begin
Result := Variants.Null;
end;
class function TlkJSONnull.Generate: TlkJSONnull;
begin
Result := TlkJSONnull.Create;
end;
class function TlkJSONnull.SelfType: TlkJSONtypes;
begin
Result := jsNull;
end;
class function TlkJSONnull.SelfTypeName: String;
begin
Result := 'jsNull';
end;
{ TlkJSONcustomlist }
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;
function TlkJSONcustomlist.GetField(const objName: Variant): TlkJSONbase;
var
Idx: Integer;
begin
if VarIsNumeric(objName) then
begin
Idx := Integer(objName);
Result := GetChild(Idx);
end
else
Result := inherited GetField(objName);
end;
function TlkJSONcustomlist.GetCount: Integer;
begin
Result := FList.Count;
end;
function TlkJSONcustomlist.GetChild(const Idx: Integer): TlkJSONbase;
begin
if (Idx < 0) or (Idx >= Count) then
Result := Nil else
Result := TlkJSONbase(FList.Items[Idx]);
end;
procedure TlkJSONcustomlist.SetChild(const Idx: Integer; const Obj: 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] := Obj;
Obj.SetParent(Self);
end;
end;
function TlkJSONcustomlist._Add(const Obj: TlkJSONbase): Integer;
begin
if not Assigned(Obj) then
begin
Result := -1;
Exit;
end;
Result := FList.Add(Obj);
Obj.SetParent(Self);
end;
procedure TlkJSONcustomlist._Delete(const Idx: Integer);
var
predIdx: Integer;
begin
if not ((Idx < 0) or (Idx >= Count)) then
begin
if FList.Items[Idx] <> Nil then
TlkJSONbase(FList.Items[Idx]).Free;
predIdx := Pred(FList.Count);
if Idx < predIdx then
begin
FList.Items[Idx] := FList.Items[predIdx]; // last element move to hole
FList.Delete(predIdx);
end
else
FList.Delete(Idx);
end;
end;
function TlkJSONcustomlist._IndexOf(const Obj: TlkJSONbase): Integer;
begin
Result := FList.IndexOf(Obj);
end;
function TlkJSONcustomlist.ForEachElement(const Idx: Integer; var objName: String)
: TlkJSONbase;
begin
objName := IntToStr(Idx);
Result := GetChild(Idx);
end;
function TlkJSONcustomlist.getDouble
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Double;
var
jn: {$IFDEF NULL_SQL}TlkJSONbase{$ELSE}TlkJSONnumber{$ENDIF};
begin
{$IFDEF NULL_SQL}
jn := Child[Idx] as TlkJSONbase;
if not Assigned(jn) or (jn.SelfType = jsNull) then
begin
Result := 0;
Null := True;
end
else
begin
Result := TlkJSONnumber(jn).Value;
Null := False;
end;
{$ELSE}
jn := Child[Idx] as TlkJSONnumber;
if not Assigned(jn) then Result := 0
else Result := jn.Value;
{$ENDIF}
end;
function TlkJSONcustomlist.getInt
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): Integer;
var
jn: {$IFDEF NULL_SQL}TlkJSONbase{$ELSE}TlkJSONnumber{$ENDIF};
begin
{$IFDEF NULL_SQL}
jn := Child[Idx] as TlkJSONbase;
if not Assigned(jn) or (jn.SelfType = jsNull) then
begin
Result := 0;
Null := True;
end
else
begin
Result := Round(Int(TlkJSONnumber(jn).Value));
Null := False;
end;
{$ELSE}
jn := Child[Idx] as TlkJSONnumber;
if not Assigned(jn) then Result := 0
else Result := Round(Int(jn.Value));
{$ENDIF}
end;
function TlkJSONcustomlist.getString
(const Idx: Integer{$IFDEF NULL_SQL}; var Null: Boolean{$ENDIF}): String;
var
js: {$IFDEF NULL_SQL}TlkJSONbase{$ELSE}TlkJSONstring{$ENDIF};
begin
{$IFDEF NULL_SQL}
js := Child[Idx] as TlkJSONbase;
if not Assigned(js) or (js.SelfType = jsNull) then
begin
Result := '';
Null := True;
end
else
begin
Result := VarToStr(TlkJSONstring(js).Value);
Null := False;
end;