-
Notifications
You must be signed in to change notification settings - Fork 27
/
Pkg.Json.JsonValueHelper.pas
82 lines (67 loc) · 1.64 KB
/
Pkg.Json.JsonValueHelper.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
unit Pkg.Json.JsonValueHelper;
interface
uses System.Json;
type
TJsonType = (jtUnknown, jtObject, jtArray, jtString, jtTrue, jtFalse, jtNumber, jtDateTime, jtBytes, jtInteger, jtInteger64);
TJsonValueHelper = class
public
class function GetJsonType(aJsonValue: TJsonValue): TJsonType;
end;
implementation
uses System.SysUtils, System.DateUtils;
{ TJsonValueHelper }
class function TJsonValueHelper.GetJsonType(aJsonValue: TJsonValue): TJsonType;
var
Value: string;
i: Integer;
j: Int64;
b: Boolean;
d: Double;
E: Extended;
DT: TdateTime;
begin
if aJsonValue = nil then
exit(jtObject);
if aJsonValue is TJSONNull then
exit(jtUnknown);
if aJsonValue is TJSONObject then
exit(jtObject);
if aJsonValue is TJSONArray then
exit(jtArray);
if (aJsonValue is TJSONNumber) then
begin
Value := aJsonValue.AsType<string>;
if not TryJsonToFloat(Value, d) then
Result := jtString
else if TryStrToInt(Value, i) then
Result := jtInteger
else if TryStrToInt64(Value, j) then
Result := jtInteger64
else
Result := jtNumber
end
else if aJsonValue is TJSONTrue then
Result := jtTrue
else if aJsonValue is TJSONFalse then
Result := jtFalse
else if aJsonValue is TJsonString then
begin
Value := aJsonValue.AsType<string>;
if TryISO8601ToDate(Value, DT) then
exit(jtDateTime);
if TryStrToFloat(Value, E) then
Result := jtString
else if TryStrToBool(Value, b) then
begin
if b then
Result := jtTrue
else
Result := jtFalse
end
else
Result := jtString
end
else
Result := jtUnknown;
end;
end.