-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrsJSONPortable.pas
More file actions
997 lines (915 loc) · 26.4 KB
/
UrsJSONPortable.pas
File metadata and controls
997 lines (915 loc) · 26.4 KB
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
unit UrsJSONPortable;
{$IF CompilerVersion > 26} // XE5
{$MESSAGE WARN 'Delphi version greatest at XE5 don''t needed this unit'}
{$IFEND}
interface
{$IF CompilerVersion <= 26} // XE5
uses
System.TypInfo, System.Rtti, System.SysUtils,
Data.DBXJSON;
type
TJsonValueBaseHelper = class helper for TJsonValue
private
function ObjValue: string;
end;
TJsonValueHelper = class helper(TJsonValueBaseHelper) for TJsonValue
private
procedure ErrorValueNotFound(const APath: string);
function Cast<T>: T;
function AsTValue(ATypeInfo: PTypeInfo; out AValue: TValue): Boolean;
function FindValue(const APath: string): TJSONValue;
public
function TryGetValue<T>(out AValue: T): Boolean; overload;
function TryGetValue<T>(const APath: string; out AValue: T): Boolean; overload;
function GetValue<T>(const APath: string = ''): T; overload;
function GetValue<T>(const APath: string; const ADefaultValue: T): T; overload;
function Value: string;
end;
TJsonArrayHelper = class helper for TJsonArray
strict private
function GetCount: Integer;
function GetItem(const AIndex: Integer): TJSONValue;
public
property Count: Integer read GetCount;
property Items[const AIndex: Integer]: TJSONValue read GetItem;
end;
EJSONPathException = class(Exception);
{$IFEND ~CompilerVersion <= 26} // XE5
implementation
{$IF CompilerVersion <= 26} // XE5
uses
System.SysConst,
System.DateUtils, System.TimeSpan,
System.Character;
const
SInvalidDateString = 'Invalid date string: %s';
SInvalidTimeString = 'Invalid time string: %s';
SInvalidOffsetString = 'Invalid time Offset string: %s';
SValueNotFound = 'Value ''%s'' not found';
SJSONPathUnexpectedRootChar = 'Unexpected char for root element: .';
SJSONPathEndedOpenBracket = 'Path ended with an open bracket';
SJSONPathEndedOpenString = 'Path ended with an open string';
SJSONPathInvalidArrayIndex = 'Invalid index for array: %s';
SJSONPathUnexpectedIndexedChar ='Unexpected character while parsing indexer: %s';
SJSONPathDotsEmptyName = 'Empty name not allowed in dot notation, use ['''']';
type
DTErrorCode = (InvDate, InvTime, InvOffset);
EDateTimeException = class(Exception);
TJSONPathParser = class
public type
TToken = (Undefined, Name, ArrayIndex, Eof, Error);
private
FPath: string;
FPos: Integer;
FTokenArrayIndex: Integer;
FToken: TToken;
FTokenName: string;
function GetIsEof: Boolean; inline;
procedure RaiseError(const AMsg: string); overload;
procedure RaiseErrorFmt(const AMsg: string; const AParams: array of const); overload;
procedure SetToken(const AToken: TToken); overload;
procedure SetToken(const AToken: TToken; const AValue); overload;
procedure ParseName;
procedure ParseQuotedName(AQuote: Char);
procedure ParseArrayIndex;
procedure ParseIndexer;
function EnsureLength(ALength: Integer): Boolean; inline;
procedure EatWhiteSpaces;
public
constructor Create(const APath: string);
function NextToken: TToken;
property IsEof: Boolean read GetIsEof;
property Token: TToken read FToken;
property TokenName: string read FTokenName;
property TokenArrayIndex: Integer read FTokenArrayIndex;
end;
TCharHelper = record helper for Char
public
function IsDigit: Boolean;
end;
var
JSONFormatSettings: TFormatSettings;
{$IF CompilerVersion <= 24} // XE3
function _ValUInt64(const s: string; var code: Integer): UInt64;
var
i: Integer;
dig: Integer;
sign: Boolean;
empty: Boolean;
begin
i := 1;
{$IFNDEF CPUX64} // avoid E1036: Variable 'dig' might not have been initialized
dig := 0;
{$ENDIF}
Result := 0;
if s = '' then
begin
code := 1;
exit;
end;
while s[i] = Char(' ') do
Inc(i);
sign := False;
if s[i] = Char('-') then
begin
sign := True;
Inc(i);
end
else if s[i] = Char('+') then
Inc(i);
empty := True;
if (s[i] = Char('$')) or (Upcase(s[i]) = Char('X'))
or ((s[i] = Char('0')) and (I < Length(S)) and (Upcase(s[i+1]) = Char('X'))) then
begin
if s[i] = Char('0') then
Inc(i);
Inc(i);
while True do
begin
case Char(s[i]) of
Char('0').. Char('9'): dig := Ord(s[i]) - Ord('0');
Char('A').. Char('F'): dig := Ord(s[i]) - (Ord('A') - 10);
Char('a').. Char('f'): dig := Ord(s[i]) - (Ord('a') - 10);
else
break;
end;
if Result > (High(UInt64) shr 4) then
Break;
if sign and (dig <> 0) then
Break;
Result := Result shl 4 + dig;
Inc(i);
empty := False;
end;
end
else
begin
while True do
begin
case Char(s[i]) of
Char('0').. Char('9'): dig := Ord(s[i]) - Ord('0');
else
break;
end;
if Result > (High(UInt64) div 10) then
Break;
if sign and (dig <> 0) then
Break;
Result := Result*10 + dig;
Inc(i);
empty := False;
end;
end;
if (s[i] <> Char(#0)) or empty then
code := i
else
code := 0;
end;
{$IFEND ~CompilerVersion <= 24} // XE3
{$IF CompilerVersion <= 24} // XE3
function TryStrToUInt64(const AStr: string; out AVal: UInt64): Boolean;
var
LErr: Integer;
begin
AVal := _ValUInt64(AStr, LErr);
Result := LErr = 0;
end;
{$IFEND ~CompilerVersion <= 24} // XE3
{$IF CompilerVersion <= 24} // XE3
function StrToUInt64(const AStr: string): UInt64;
begin
if not TryStrToUInt64(AStr, Result) then
raise EConvertError.CreateResFmt(@SInvalidInteger, [AStr]);
end;
{$IFEND ~CompilerVersion <= 24} // XE3
function InitJsonFormatSettings: TFormatSettings;
begin
Result := GetUSFormat;
Result.CurrencyString := #$00A4;
Result.CurrencyFormat := 0;
Result.CurrencyDecimals := 2;
Result.DateSeparator := '/';
Result.TimeSeparator := ':';
Result.ListSeparator := ',';
Result.ShortDateFormat := 'MM/dd/yyyy';
Result.LongDateFormat := 'dddd, dd MMMMM yyyy HH:mm:ss';
Result.TimeAMString := 'AM';
Result.TimePMString := 'PM';
Result.ShortTimeFormat := 'HH:mm';
Result.LongTimeFormat := 'HH:mm:ss';
// for I := Low(DefShortMonthNames) to High(DefShortMonthNames) do
// begin
// Result.ShortMonthNames[I] := LoadResString(DefShortMonthNames[I]);
// Result.LongMonthNames[I] := LoadResString(DefLongMonthNames[I]);
// end;
// for I := Low(DefShortDayNames) to High(DefShortDayNames) do
// begin
// Result.ShortDayNames[I] := LoadResString(DefShortDayNames[I]);
// Result.LongDayNames[I] := LoadResString(DefLongDayNames[I]);
// end;
Result.ThousandSeparator := ',';
Result.DecimalSeparator := '.';
Result.TwoDigitYearCenturyWindow := 50;
Result.NegCurrFormat := 0;
end;
procedure DTFmtError(AErrorCode: DTErrorCode; const AValue: string);
const
Errors: array[DTErrorCode] of string = (SInvalidDateString, SInvalidTimeString, SInvalidOffsetString);
begin
raise EDateTimeException.CreateFmt(Errors[AErrorCode], [AValue]);
end;
function GetNextDTComp(var P: PChar; const PEnd: PChar; ErrorCode: DTErrorCode;
const AValue: string; NumDigits: Integer): string; overload;
var
LDigits: Integer;
begin
LDigits := 0;
Result := '';
while ((P <= PEnd) and P^.IsDigit and (LDigits < NumDigits)) do
begin
Result := Result + P^;
Inc(P);
Inc(LDigits);
end;
if Result = '' then
DTFmtError(ErrorCode, AValue);
end;
function GetNextDTComp(var P: PChar; const PEnd: PChar; const DefValue: string; Prefix: Char;
IsOptional: Boolean; IsDate: Boolean; ErrorCode: DTErrorCode; const AValue: string; NumDigits: Integer): string; overload;
const
SEmptySeparator: Char = ' ';
var
LDigits: Integer;
begin
if (P >= PEnd) then
begin
Result := DefValue;
Exit;
end;
Result := '';
if (Prefix <> SEmptySeparator) and (IsDate or not (Byte(P^) in [Ord('+'), Ord('-')])) then
begin
if P^ <> Prefix then
DTFmtError(ErrorCode, AValue);
Inc(P);
end;
LDigits := 0;
while ((P <= PEnd) and P^.IsDigit and (LDigits < NumDigits)) do
begin
Result := Result + P^;
Inc(P);
Inc(LDigits);
end;
if Result = '' then
begin
if IsOptional then
Result := DefValue
else
DTFmtError(ErrorCode, AValue);
end;
end;
procedure DecodeISO8601Date(const DateString: string; var AYear, AMonth, ADay: Word);
procedure ConvertDate(const AValue: string);
const
SDateSeparator: Char = '-';
SEmptySeparator: Char = ' ';
var
P, PE: PChar;
begin
P := PChar(AValue);
PE := P + (AValue.Length - 1);
if AValue.IndexOf(SDateSeparator) < 0 then
begin
AYear := StrToInt(GetNextDTComp(P, PE, InvDate, AValue, 4));
AMonth := StrToInt(GetNextDTComp(P, PE, '00', SEmptySeparator, True, True, InvDate, AValue, 2));
ADay := StrToInt(GetNextDTComp(P, PE, '00', SEmptySeparator, True, True, InvDate, AValue, 2));
end
else
begin
AYear := StrToInt(GetNextDTComp(P, PE, InvDate, AValue, 4));
AMonth := StrToInt(GetNextDTComp(P, PE, '00', SDateSeparator, True, True, InvDate, AValue, 2));
ADay := StrToInt(GetNextDTComp(P, PE, '00', SDateSeparator, True, True, InvDate, AValue, 2));
end;
end;
var
TempValue: string;
LNegativeDate: Boolean;
begin
AYear := 0;
AMonth := 0;
ADay := 1;
LNegativeDate := (DateString <> '') and (DateString[Low(string)] = '-');
if LNegativeDate then
TempValue := DateString.Substring(1)
else
TempValue := DateString;
if Length(TempValue) < 4 then
raise EDateTimeException.CreateFmt(SInvalidDateString, [TempValue]);
ConvertDate(TempValue);
end;
procedure DecodeISO8601Time(const TimeString: string; var AHour, AMinute, ASecond, AMillisecond: Word;
var AHourOffset, AMinuteOffset: Integer);
const
SEmptySeparator: Char = ' ';
STimeSeparator: Char = ':';
SMilSecSeparator: Char = '.';
var
LFractionalSecondString: string;
P, PE: PChar;
LOffsetSign: Char;
LFraction: Double;
begin
AHour := 0;
AMinute := 0;
ASecond := 0;
AMillisecond := 0;
AHourOffset := 0;
AMinuteOffset := 0;
if TimeString <> '' then
begin
P := PChar(TimeString);
PE := P + (TimeString.Length - 1);
if TimeString.IndexOf(STimeSeparator) < 0 then
begin
AHour := StrToInt(GetNextDTComp(P, PE, InvTime, TimeString, 2));
AMinute := StrToInt(GetNextDTComp(P, PE, '00', SEmptySeparator, False, False, InvTime, TimeString, 2));
ASecond := StrToInt(GetNextDTComp(P, PE, '00', SEmptySeparator, True, False, InvTime, TimeString, 2));
LFractionalSecondString := GetNextDTComp(P, PE, '0', SMilSecSeparator, True, False, InvTime, TimeString, 18);
if LFractionalSecondString <> '0' then
begin
LFractionalSecondString := FormatSettings.DecimalSeparator + LFractionalSecondString;
LFraction := StrToFloat(LFractionalSecondString);
AMillisecond := Round(1000 * LFraction);
end;
end
else
begin
AHour := StrToInt(GetNextDTComp(P, PE, InvTime, TimeString, 2));
AMinute := StrToInt(GetNextDTComp(P, PE, '00', STimeSeparator, False, False, InvTime, TimeString, 2));
ASecond := StrToInt(GetNextDTComp(P, PE, '00', STimeSeparator, True, False, InvTime, TimeString, 2));
LFractionalSecondString := GetNextDTComp(P, PE, '0', SMilSecSeparator, True, False, InvTime, TimeString, 18);
if LFractionalSecondString <> '0' then
begin
LFractionalSecondString := FormatSettings.DecimalSeparator + LFractionalSecondString;
LFraction := StrToFloat(LFractionalSecondString);
AMillisecond := Round(1000 * LFraction);
end;
end;
if CharInSet(P^, ['-', '+']) then
begin
LOffsetSign := P^;
Inc(P);
if not P^.IsDigit then
DTFmtError(InvTime, TimeString);
AHourOffset := StrToInt(LOffsetSign + GetNextDTComp(P, PE, InvOffset, TimeString, 2));
AMinuteOffset:= StrToInt(LOffsetSign + GetNextDTComp(P, PE, '00', STimeSeparator, True, True, InvOffset, TimeString, 2));
end;
end;
end;
function AdjustDateTime(const ADate: TDateTime; AHourOffset, AMinuteOffset:Integer; IsUTC: Boolean = True): TDateTime;
var
AdjustDT: TDateTime;
BiasLocal: Int64;
BiasTime: Integer;
BiasHour: Integer;
BiasMins: Integer;
BiasDT: TDateTime;
TZ: TTimeZone;
begin
Result := ADate;
if IsUTC then
begin
{ If we have an offset, adjust time to go back to UTC }
if (AHourOffset <> 0) or (AMinuteOffset <> 0) then
begin
AdjustDT := EncodeTime(Abs(AHourOffset), Abs(AMinuteOffset), 0, 0);
if ((AHourOffset * MinsPerHour) + AMinuteOffset) > 0 then
Result := Result - AdjustDT
else
Result := Result + AdjustDT;
end;
end
else
begin
{ Now adjust TDateTime based on any offsets we have and the local bias }
{ There are two possibilities:
a. The time we have has the same offset as the local bias - nothing to do!!
b. The time we have and the local bias are different - requiring adjustments }
TZ := TTimeZone.Local;
BiasLocal := Trunc(TZ.GetUTCOffset(Result).Negate.TotalMinutes);
BiasTime := (AHourOffset * MinsPerHour) + AMinuteOffset;
if (BiasLocal + BiasTime) = 0 then
Exit;
{ Here we adjust the Local Bias to make it relative to the Time's own offset
instead of being relative to GMT }
BiasLocal := BiasLocal + BiasTime;
BiasHour := Abs(BiasLocal) div MinsPerHour;
BiasMins := Abs(BiasLocal) mod MinsPerHour;
BiasDT := EncodeTime(BiasHour, BiasMins, 0, 0);
if (BiasLocal > 0) then
Result := Result - BiasDT
else
Result := Result + BiasDT;
end;
end;
function ISO8601ToDate(const AISODate: string; AReturnUTC: Boolean = True): TDateTime;
const
STimePrefix: Char = 'T';
var
TimeString, DateString: string;
TimePosition: Integer;
Year, Month, Day, Hour, Minute, Second, Millisecond: Word;
HourOffset, MinuteOffset: Integer;
AddDay, AddMinute: Boolean;
begin
HourOffset := 0;
MinuteOffset := 0;
TimePosition := AISODate.IndexOf(STimePrefix);
if TimePosition >= 0 then
begin
DateString := AISODate.Substring(0, TimePosition);
TimeString := AISODate.Substring(TimePosition + 1);
end
else
begin
Hour := 0;
Minute := 0;
Second := 0;
Millisecond := 0;
HourOffset := 0;
MinuteOffset := 0;
DateString := AISODate;
TimeString := '';
end;
DecodeISO8601Date(DateString, Year, Month, Day);
DecodeISO8601Time(TimeString, Hour, Minute, Second, Millisecond, HourOffset, MinuteOffset);
AddDay := Hour = 24;
if AddDay then
Hour := 0;
AddMinute := Second = 60;
if AddMinute then
Second := 0;
Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
if AddDay then
Result := IncDay(Result);
if AddMinute then
Result := IncMinute(Result);
Result := AdjustDateTime(Result, HourOffset, MinuteOffset, AReturnUTC);
end;
function StrToTValue(const Str: string; const TypeInfo: PTypeInfo; out AValue: TValue): Boolean;
function CheckRange(const Min, Max: Int64; const Value: Int64; const Str: string): Int64;
begin
Result := Value;
if (Value < Min) or (Value > Max) then
raise EConvertError.CreateFmt(System.SysConst.SInvalidInteger, [Str]);
end;
var
TypeData: TTypeData;
TypeName: string;
begin
Result := True;
case TypeInfo.Kind of
tkInteger:
case GetTypeData(TypeInfo)^.OrdType of
otSByte: AValue := CheckRange(Low(Int8), High(Int8), StrToInt(Str), Str);
otSWord: AValue := CheckRange(Low(Int16), High(Int16), StrToInt(Str), Str);
otSLong: AValue := StrToInt(Str);
otUByte: AValue := CheckRange(Low(UInt8), High(UInt8), StrToInt(Str), Str);
otUWord: AValue := CheckRange(Low(UInt16), High(UInt16), StrToInt(Str), Str);
otULong: AValue := CheckRange(Low(UInt32), High(UInt32), StrToInt64(Str), Str);
end;
tkInt64:
begin
TypeData := GetTypeData(TypeInfo)^;
if TypeData.MinInt64Value > TypeData.MaxInt64Value then
AValue := StrToUInt64(Str)
else
AValue := StrToInt64(Str);
end;
tkEnumeration:
begin
TypeName := TypeInfo.NameFld.ToString;
if SameText(TypeName, 'boolean') or SameText(TypeName, 'bool') then
AValue := StrToBool(Str)
else
Result := False;
end;
tkFloat:
case GetTypeData(TypeInfo)^.FloatType of
ftSingle: AValue := StrToFloat(Str, JSONFormatSettings);
ftDouble:
begin
if TypeInfo = System.TypeInfo(TDate) then
AValue := ISO8601ToDate(Str)
else if TypeInfo = System.TypeInfo(TTime) then
AValue := ISO8601ToDate(Str)
else if TypeInfo = System.TypeInfo(TDateTime) then
AValue := ISO8601ToDate(Str)
else
AValue := StrToFloat(Str, JSONFormatSettings);
end;
ftExtended: AValue := StrToFloat(Str, JSONFormatSettings);
ftComp: AValue := StrToFloat(Str, JSONFormatSettings);
ftCurr: AValue := StrToCurr(Str, JSONFormatSettings);
end;
{$IFNDEF NEXTGEN}
tkChar,
{$ENDIF !NEXTGEN}
tkWChar:
begin
if Str.Length = 1 then
AValue := Str[Low(string)]
else
Result := False;
end;
tkString, tkLString, tkUString, tkWString:
AValue := Str;
else
Result := False;
end;
end;
function FindJSONValue(const AJSON: TJSONValue; const APath: string): TJsonValue; overload;
var
LCurrentValue: TJSONValue;
LParser: TJSONPathParser;
LPair: TJSONPair;
LError: Boolean;
begin
LParser := TJSONPathParser.Create(APath);
try
LCurrentValue := AJSON;
LError := False;
while (not LParser.IsEof) and (not LError) do
begin
case LParser.NextToken of
TJSONPathParser.TToken.Name:
begin
if LCurrentValue is TJSONObject then
begin
LPair := TJSONObject(LCurrentValue).Get(LParser.TokenName);
if LPair <> nil then
LCurrentValue := LPair.JsonValue
else
LCurrentValue := nil;
if LCurrentValue = nil then
LError := True;
end
else
LError := True;
end;
TJSONPathParser.TToken.ArrayIndex:
begin
if LCurrentValue is TJSONArray then
if LParser.TokenArrayIndex < TJSONArray(LCurrentValue).Count then
LCurrentValue := TJSONArray(LCurrentValue).Items[LParser.TokenArrayIndex]
else
LError := True
else
LError := True
end;
TJSONPathParser.TToken.Error:
LError := True;
else
Assert(LParser.Token = TJSONPathParser.TToken.Eof); // case statement is not complete
end;
end;
if LParser.IsEof and not LError then
Result := LCurrentValue
else
Result := nil;
finally
LParser.Free;
end;
end;
{ TJSONPathParser }
constructor TJSONPathParser.Create(const APath: string);
begin
FPath := APath;
end;
procedure TJSONPathParser.EatWhiteSpaces;
begin
while not IsEof and IsWhiteSpace(FPath.Chars[FPos]) do
Inc(FPos);
end;
function TJSONPathParser.EnsureLength(ALength: Integer): Boolean;
begin
Result := (FPos + ALength) < Length(FPath);
end;
function TJSONPathParser.GetIsEof: Boolean;
begin
Result := FPos >= Length(FPath);
end;
function TJSONPathParser.NextToken: TToken;
var
IsFirstToken: Boolean;
begin
IsFirstToken := FPos = 0;
EatWhiteSpaces;
if IsEof then
SetToken(TToken.Eof)
else
begin
case FPath.Chars[FPos] of
'.':
// Root element cannot start with a dot
if IsFirstToken then
RaiseError(SJSONPathUnexpectedRootChar)
else
ParseName;
'[':
ParseIndexer;
else
// In dot notation all names are prefixed by '.', except the root element
if IsFirstToken then
ParseName
else
RaiseErrorFmt(SJSONPathUnexpectedIndexedChar, [FPath.Chars[FPos]]);
end;
Inc(FPos);
end;
Result := FToken;
end;
procedure TJSONPathParser.ParseArrayIndex;
var
LEndPos: Integer;
LString: string;
I: Integer;
begin
LEndPos := FPath.IndexOf(']', FPos);
if LEndPos < 0 then
RaiseError(SJSONPathEndedOpenBracket)
else
begin
LString := Trim(FPath.Substring(FPos, LEndPos - FPos));
FPos := LEndPos - 1;
if TryStrToInt(LString, I) then
SetToken(TToken.ArrayIndex, I)
else
RaiseErrorFmt(SJSONPathInvalidArrayIndex, [LString])
end;
end;
procedure TJSONPathParser.ParseQuotedName(AQuote: Char);
var
LString: string;
begin
LString := '';
Inc(FPos);
while not IsEof do
begin
if (FPath.Chars[FPos] = '\') and EnsureLength(1) and (FPath.Chars[FPos + 1] = AQuote) then // \"
begin
Inc(FPos);
LString := LString + AQuote
end
else if FPath.Chars[FPos] = AQuote then
begin
SetToken(TToken.Name, LString);
Exit;
end
else
LString := LString + FPath.Chars[FPos];
Inc(FPos);
end;
RaiseError(SJSONPathEndedOpenString);
end;
procedure TJSONPathParser.RaiseError(const AMsg: string);
begin
RaiseErrorFmt(AMsg, []);
end;
procedure TJSONPathParser.RaiseErrorFmt(const AMsg: string; const AParams: array of const);
begin
SetToken(TToken.Error);
raise EJSONPathException.Create(Format(AMsg, AParams));
end;
procedure TJSONPathParser.ParseIndexer;
begin
Inc(FPos); // [
EatWhiteSpaces;
if IsEof then
RaiseError('Path ended with an open bracket');
case FPath.Chars[FPos] of
'"',
'''':
ParseQuotedName(FPath.Chars[FPos]);
else
ParseArrayIndex;
end;
Inc(FPos);
EatWhiteSpaces;
if FPath.Chars[FPos] <> ']' then
RaiseErrorFmt(SJSONPathUnexpectedIndexedChar,[FPath.Chars[FPos]]);
end;
procedure TJSONPathParser.ParseName;
var
LEndPos: Integer;
LString: string;
begin
if FPath.Chars[FPos] = '.' then
begin
Inc(FPos);
if IsEof then
begin
SetToken(TToken.Error);
Exit;
end;
end;
LEndPos := FPath.IndexOfAny(['.', '['], FPos);
if LEndPos < 0 then
begin
LString := FPath.Substring(FPos);
FPos := Length(FPath) - 1;
end
else
begin
LString := Trim(FPath.Substring(FPos, LEndPos - FPos));
FPos := LEndPos - 1;
end;
if LString = '' then
RaiseError(SJSONPathDotsEmptyName)
else
SetToken(TToken.Name, LString);
end;
procedure TJSONPathParser.SetToken(const AToken: TToken);
var
P: Pointer;
begin
SetToken(AToken, P);
end;
procedure TJSONPathParser.SetToken(const AToken: TToken; const AValue);
begin
FToken := AToken;
case FToken of
TToken.Name:
FTokenName := string(AValue);
TToken.ArrayIndex:
FTokenArrayIndex := Integer(AValue);
end;
end;
{ TCharHelper }
function TCharHelper.IsDigit: Boolean;
begin
Result := System.Character.IsDigit(Self)
end;
{ TJsonValueBaseHelper }
function TJsonValueBaseHelper.ObjValue: string;
begin
Result := Value;
end;
{ TJsonValueHelper }
procedure TJsonValueHelper.ErrorValueNotFound(const APath: string);
begin
raise TJSONException.Create(Format(SValueNotFound, [APath]));
end;
function TJsonValueHelper.Cast<T>: T;
var
LTypeInfo: PTypeInfo;
LValue: TValue;
begin
LTypeInfo := System.TypeInfo(T);
if not AsTValue(LTypeInfo, LValue) then
raise TJSONException.CreateFmt('Conversion from %0:s to %1:s is not supported', [Self.ClassName, LTypeInfo.Name]);
Result := LValue.AsType<T>;
end;
function TJsonValueHelper.AsTValue(ATypeInfo: PTypeInfo; out AValue: TValue): Boolean;
var
LNeedBase: Boolean;
LBool: Boolean;
begin
Result := False;
LNeedBase := False;
if (Self is TJSONTrue) or (Self is TJSONFalse) then begin
LBool := Self is TJSONTrue;
case ATypeInfo.Kind of
tkEnumeration: begin
AValue := TValue.FromOrdinal(ATypeInfo, Ord(LBool));
Result :=
AValue.IsType<Boolean> or
AValue.IsType<ByteBool> or
AValue.IsType<WordBool> or
AValue.IsType<LongBool>;
end;
tkInteger, tkInt64, tkFloat: begin
AValue := Ord(LBool);
Result := True;
end;
tkString, tkLString, tkWString, tkUString: begin
AValue := Value;
Result := True;
end
else
LNeedBase := True;
end;
end else if Self is TJSONNull then begin
case ATypeInfo.Kind of
tkString, tkLString, tkUString, TkWString: begin
AValue := '';
Result := True;
end
else
LNeedBase := True;
end;
end else if Self is TJSONString then begin
case ATypeInfo.Kind of
tkInteger, tkInt64, tkFloat,
tkString, tkLString, tkWString, tkUString,
{$IFNDEF NEXTGEN}
tkChar,
{$ENDIF !NEXTGEN}
tkWChar,
tkEnumeration:
Result := StrToTValue(Value, ATypeInfo, AValue)
else
LNeedBase := True;
end;
end else
LNeedBase := True;
if LNeedBase then begin
case ATypeInfo^.Kind of
tkClass: begin
AValue := Self;
Result := True;
end
end;
end;
end;
function TJsonValueHelper.FindValue(const APath: string): TJSONValue;
begin
if (Self is TJSONArray) or (Self is TJSONObject) then
Result := FindJSONValue(Self, APath)
else begin
if APath = '' then
Result := Self
else
Result := nil;
end;
end;
function TJsonValueHelper.TryGetValue<T>(out AValue: T): Boolean;
begin
Result := TryGetValue<T>('', AValue);
end;
function TJsonValueHelper.TryGetValue<T>(const APath: string; out AValue: T): Boolean;
var
LJSONValue: TJSONValue;
begin
LJSONValue := FindValue(APath);
Result := LJSONValue <> nil;
if Result then
begin
try
AValue := LJSONValue.Cast<T>;
except
Result := False;
end;
end;
end;
function TJsonValueHelper.GetValue<T>(const APath: string = ''): T;
var
LValue: T;
LJSONValue: TJSONValue;
begin
LJSONValue := FindValue(APath);
if LJSONValue = nil then
ErrorValueNotFound(APath);
Result := LJSONValue.Cast<T>;
end;
function TJsonValueHelper.GetValue<T>(const APath: string; const ADefaultValue: T): T;
var
LValue: T;
LJSONValue: TJSONValue;
LTypeInfo: PTypeInfo;
LReturnDefault: Boolean;
begin
LJSONValue := FindValue(APath);
LReturnDefault := LJSONValue = nil;
// Treat JSONNull as nil
if LJSONValue is TJSONNull then
begin
LTypeInfo := System.TypeInfo(T);
if LTypeInfo.TypeData.ClassType <> nil then
LJSONValue := nil;
end;
if LJSONValue <> nil then
Result := LJSONValue.Cast<T>
else
Result := ADefaultValue
end;
function TJsonValueHelper.Value: string;
begin
if (Self is TJSONTrue) or (Self is TJSONFalse) then
Result := ToString
else
Result := ObjValue;
end;
{ TJsonArrayHelper }
function TJsonArrayHelper.GetCount: Integer;
begin
Result := Size;
end;
function TJsonArrayHelper.GetItem(const AIndex: Integer): TJSONValue;
begin
Result := Get(AIndex);
end;
initialization
JSONFormatSettings := InitJsonFormatSettings;
{$IFEND ~CompilerVersion <= 26} // XE5
end.