-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.pp
176 lines (150 loc) · 5.31 KB
/
JSON.pp
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
unit JSON;
{$mode ObjFPC}{$H+}
interface
type
TJsDataType = ( jsNull, jsString, jsInteger, jsNumber, jsBoolean, jsObject, jsArray );
PJsonEntry = ^TJsonEntry;
PJsonEntries = ^TJsonEntries;
PJsonValues = ^TJsonValues;
TJsonValue = record
case DataType: TJsDataType of
jsString: ( StringValue: PChar );
jsInteger: ( IntegerValue: Integer );
jsNumber: ( NumberValue: Double );
jsBoolean: ( BooleanValue: Boolean );
jsObject: ( ObjectValue: PJsonEntries );
jsArray: ( Items: PJsonValues );
jsNull: ();
end;
TJsonObject = TJsonValue;
TJsonArray = TJsonValue;
TJsonValues = Array of TJsonValue;
TJsonEntry = record
Key: PChar;
Value: TJsonValue;
end;
TJsonEntries = Array of TJsonEntry;
function JsonObject(const Entries: TJsonEntries): TJsonObject;
function JsonArray(const Items: TJsonValues): TJsonArray;
function JsonValue(): TJsonValue; overload;
function JsonValue(const StringValue: PChar): TJsonValue; overload;
function JsonValue(const StringValue: String): TJsonValue; overload;
function JsonValue(const IntegerValue: Integer): TJsonValue; overload;
function JsonValue(const NumberValue: Double): TJsonValue; overload;
function JsonValue(const BooleanValue: Boolean): TJsonValue; overload;
function Entry(const Key: PChar): TJsonEntry; overload;
function Entry(const Key: PChar; const StringValue: PChar): TJsonEntry; overload;
function Entry(const Key: PChar; const StringValue: String): TJsonEntry; overload;
function Entry(const Key: PChar; const IntegerValue: Integer): TJsonEntry; overload;
function Entry(const Key: PChar; const NumberValue: Double): TJsonEntry; overload;
function Entry(const Key: PChar; const BooleanValue: Boolean): TJsonEntry; overload;
function Entry(const Key: PChar; const Value: TJsonValue): TJsonEntry; overload;
procedure FreeValue(Value: TJsonValue);
implementation
uses
Strings;
{*--------------------------------------------------------------------------*}
function JsonObject(const Entries: TJsonEntries): TJsonObject;
begin
JsonObject.DataType := jsObject;
JsonObject.ObjectValue := PJsonEntries(Entries);
end;
function JsonArray(const Items: TJsonValues): TJsonArray;
begin
JsonArray.DataType := jsArray;
JsonArray.Items := PJsonValues(Items);
end;
{*--------------------------------------------------------------------------*}
function JsonValue(): TJsonValue; overload;
begin
JsonValue.DataType := jsNull;
end;
function JsonValue(const StringValue: PChar): TJsonValue; overload;
begin
JsonValue.DataType := jsString;
JsonValue.StringValue := StrNew(StringValue);
end;
function JsonValue(const StringValue: String): TJsonValue; overload;
begin
JsonValue.DataType := jsString;
JsonValue.StringValue := StrNew(PChar(StringValue));
end;
function JsonValue(const IntegerValue: Integer): TJsonValue; overload;
begin
JsonValue.DataType := jsInteger;
JsonValue.IntegerValue := IntegerValue;
end;
function JsonValue(const NumberValue: Double): TJsonValue; overload;
begin
JsonValue.DataType := jsNumber;
JsonValue.NumberValue := NumberValue;
end;
function JsonValue(const BooleanValue: Boolean): TJsonValue; overload;
begin
JsonValue.DataType := jsBoolean;
JsonValue.BooleanValue := BooleanValue;
end;
{*--------------------------------------------------------------------------*}
function Entry(const Key: PChar): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := JsonValue();
end;
function Entry(const Key: PChar; const StringValue: PChar): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := JsonValue(StringValue);
end;
function Entry(const Key: PChar; const StringValue: String): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := JsonValue(StringValue);
end;
function Entry(const Key: PChar; const IntegerValue: Integer): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := JsonValue(IntegerValue);
end;
function Entry(const Key: PChar; const NumberValue: Double): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := JsonValue(NumberValue);
end;
function Entry(const Key: PChar; const BooleanValue: Boolean): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := JsonValue(BooleanValue);
end;
function Entry(const Key: PChar; const Value: TJsonValue): TJsonEntry; overload;
begin
Entry.Key := StrNew(Key);
Entry.Value := Value;
end;
{*--------------------------------------------------------------------------*}
procedure FreeValue(Value: TJsonValue);
var
I: Integer;
begin
with Value do begin
case DataType of
jsObject:
begin
for I := Low(TJsonEntries(ObjectValue)) to High(TJsonEntries(ObjectValue)) do
begin
FreeValue(TJsonEntries(ObjectValue)[I].Value);
StrDispose(TJsonEntries(ObjectValue)[I].Key);
end;
end;
jsArray:
begin
for I := Low(TJsonValues(Items)) to High(TJsonValues(Items)) do
FreeValue(TJsonValues(Items)[I]);
end;
jsString:
begin
StrDispose(StringValue);
end;
end;
end;
end;
end.