-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjsonSet.pas
92 lines (75 loc) · 1.71 KB
/
jsonSet.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
{
jsonSet.pas
Copyright 2021 Stijn Sanders
Made available under terms described in file "LICENSE"
https://github.com/stijnsanders/jsonDoc
v1.0.0
}
unit jsonSet;
interface
uses jsonDoc;
type
TJSONDocSet=class(TJSONDocArray)
private
FKeyIndex:IJSONDocument;
public
procedure SetDoc(const Key:WideString;Value:IJSONDocument);
function GetDoc(const Key:WideString):IJSONDocument;
procedure LoadDoc(const Key:WideString;Value:IJSONDocument);
property Doc[const Key:WideString]:IJSONDocument read GetDoc write SetDoc; default;
constructor Create;
destructor Destroy; override;
function AsKeyedDoc:IJSONDocument;
end;
implementation
uses Variants;
{ TJSONDocSet }
constructor TJSONDocSet.Create;
begin
inherited Create;
FKeyIndex:=JSON;//TODO: better with sorted TStringList?
end;
destructor TJSONDocSet.Destroy;
begin
FKeyIndex:=nil;
inherited;
end;
function TJSONDocSet.GetDoc(const Key: WideString): IJSONDocument;
var
v:Variant;
begin
v:=FKeyIndex[Key];
if VarIsNull(v) then Result:=nil else
begin
Result:=JSON;
Result.Parse(GetJSON(v));
end;
end;
procedure TJSONDocSet.LoadDoc(const Key: WideString; Value: IJSONDocument);
var
v:Variant;
begin
//IMPORTANT: caller does Value.Clear!!!
v:=FKeyIndex[Key];
if not(VarIsNull(v)) then Value.Parse(GetJSON(v));
end;
procedure TJSONDocSet.SetDoc(const Key: WideString; Value: IJSONDocument);
var
v:Variant;
begin
v:=FKeyIndex[Key];
if VarIsNull(v) then
FKeyIndex[Key]:=Add(Value)
else
Set_Item(v,Value);
end;
function TJSONDocSet.AsKeyedDoc: IJSONDocument;
var
je:IJSONEnumerator;
begin
Result:=JSON;
je:=JSONEnum(FKeyIndex);
while je.Next do
Result[je.Key]:=Get_Item(je.Value);
end;
end.