| 1 |
{ |
| 2 |
This file is part of the Free Component Library |
| 3 |
|
| 4 |
JSON source parser |
| 5 |
Copyright (c) 2007 by Michael Van Canneyt [email protected] |
| 6 |
|
| 7 |
See the file COPYING.FPC, included in this distribution, |
| 8 |
for details about the copyright. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
**********************************************************************} |
| 15 |
///{$mode objfpc} |
| 16 |
{$h+} |
| 17 |
unit jsonparser; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, fpJSON, jsonscanner, jsonreader, CastleUtils; |
| 23 |
|
| 24 |
Type |
| 25 |
|
| 26 |
{ TJSONParser } |
| 27 |
|
| 28 |
TJSONParser = Class(TBaseJSONReader) |
| 29 |
private |
| 30 |
FStack : Array of TJSONData; |
| 31 |
FStackPos : integer; |
| 32 |
FStruct : TJSONData; |
| 33 |
FValue : TJSONData; |
| 34 |
FKey: TJSONStringType; |
| 35 |
procedure Pop(aType: TJSONType); |
| 36 |
Procedure Push(AValue : TJSONData); |
| 37 |
Function NewValue(AValue : TJSONData) : TJSONData; |
| 38 |
Protected |
| 39 |
Procedure KeyValue(Const AKey : TJSONStringType); override; |
| 40 |
Procedure StringValue(Const AValue : TJSONStringType);override; |
| 41 |
Procedure NullValue; override; |
| 42 |
Procedure FloatValue(Const AValue : Double); override; |
| 43 |
Procedure BooleanValue(Const AValue : Boolean); override; |
| 44 |
Procedure NumberValue(Const AValue : TJSONStringType); override; |
| 45 |
Procedure IntegerValue(Const AValue : integer); override; |
| 46 |
Procedure Int64Value(Const AValue : int64); override; |
| 47 |
Procedure QWordValue(Const AValue : QWord); override; |
| 48 |
Procedure StartArray; override; |
| 49 |
Procedure StartObject; override; |
| 50 |
Procedure EndArray; override; |
| 51 |
Procedure EndObject; override; |
| 52 |
Public |
| 53 |
function Parse: TJSONData; |
| 54 |
end; |
| 55 |
|
| 56 |
EJSONParser = jsonReader.EJSONParser; |
| 57 |
|
| 58 |
implementation |
| 59 |
|
| 60 |
Resourcestring |
| 61 |
SErrStructure = 'Structural error'; |
| 62 |
|
| 63 |
{ TJSONParser } |
| 64 |
|
| 65 |
procedure DefJSONParserHandler(AStream: TStream; const AUseUTF8: Boolean; out |
| 66 |
Data: TJSONData); |
| 67 |
|
| 68 |
Var |
| 69 |
P : TJSONParser; |
| 70 |
AOptions: TJSONOptions; |
| 71 |
|
| 72 |
begin |
| 73 |
Data:=Nil; |
| 74 |
AOptions:=[]; |
| 75 |
if AUseUTF8 then |
| 76 |
Include(AOptions,joUTF8); |
| 77 |
P:=TJSONParser.Create(AStream,AOptions); |
| 78 |
try |
| 79 |
Data:=P.Parse; |
| 80 |
finally |
| 81 |
P.Free; |
| 82 |
end; |
| 83 |
end; |
| 84 |
|
| 85 |
procedure DefJSONStringParserHandler(Const S : TJSONStringType; const AUseUTF8: Boolean; out |
| 86 |
Data: TJSONData); |
| 87 |
|
| 88 |
Var |
| 89 |
P : TJSONParser; |
| 90 |
AOptions: TJSONOptions; |
| 91 |
|
| 92 |
begin |
| 93 |
Data:=Nil; |
| 94 |
AOptions:=[]; |
| 95 |
if AUseUTF8 then |
| 96 |
Include(AOptions,joUTF8); |
| 97 |
P:=TJSONParser.Create(S,AOptions); |
| 98 |
try |
| 99 |
Data:=P.Parse; |
| 100 |
finally |
| 101 |
P.Free; |
| 102 |
end; |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TJSONParser.Pop(aType: TJSONType); |
| 106 |
|
| 107 |
begin |
| 108 |
if (FStackPos=0) then |
| 109 |
DoError(SErrStructure); |
| 110 |
If (FStruct.JSONType<>aType) then |
| 111 |
DoError(SErrStructure); |
| 112 |
Dec(FStackPos); |
| 113 |
FStruct:=FStack[FStackPos]; |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TJSONParser.Push(AValue: TJSONData); |
| 117 |
|
| 118 |
begin |
| 119 |
if (FStackPos=Length(FStack)) then |
| 120 |
SetLength(FStack,FStackPos+10); |
| 121 |
FStack[FStackPos]:=FStruct; |
| 122 |
Inc(FStackPos); |
| 123 |
FStruct:=AValue; |
| 124 |
end; |
| 125 |
|
| 126 |
function TJSONParser.NewValue(AValue: TJSONData): TJSONData; |
| 127 |
begin |
| 128 |
Result:=AValue; |
| 129 |
// Add to existing structural type |
| 130 |
if (FStruct is TJSONObject) then |
| 131 |
begin |
| 132 |
if (Not (joIgnoreDuplicates in options)) then |
| 133 |
try |
| 134 |
TJSONObject(FStruct).Add(FKey,AValue); |
| 135 |
except |
| 136 |
AValue.Free; |
| 137 |
Raise; |
| 138 |
end |
| 139 |
else if (TJSONObject(FStruct).IndexOfName(FKey)=-1) then |
| 140 |
TJSONObject(FStruct).Add(FKey,AValue) |
| 141 |
else |
| 142 |
AValue.Free; |
| 143 |
FKey:=''; |
| 144 |
end |
| 145 |
else if (FStruct is TJSONArray) then |
| 146 |
TJSONArray(FStruct).Add(AValue); |
| 147 |
// The first actual value is our result |
| 148 |
if (FValue=Nil) then |
| 149 |
FValue:=AValue; |
| 150 |
end; |
| 151 |
|
| 152 |
procedure TJSONParser.KeyValue(const AKey: TJSONStringType); |
| 153 |
begin |
| 154 |
if (FStruct is TJSONObject) and (FKey='') then |
| 155 |
FKey:=Akey |
| 156 |
else |
| 157 |
DoError('Duplicatekey or no object'); |
| 158 |
end; |
| 159 |
|
| 160 |
procedure TJSONParser.StringValue(const AValue: TJSONStringType); |
| 161 |
begin |
| 162 |
NewValue(CreateJSON(AValue)); |
| 163 |
end; |
| 164 |
|
| 165 |
procedure TJSONParser.NullValue; |
| 166 |
begin |
| 167 |
NewValue(CreateJSON); |
| 168 |
end; |
| 169 |
|
| 170 |
procedure TJSONParser.FloatValue(const AValue: Double); |
| 171 |
begin |
| 172 |
NewValue(CreateJSON(AValue)); |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TJSONParser.BooleanValue(const AValue: Boolean); |
| 176 |
begin |
| 177 |
NewValue(CreateJSON(AValue)); |
| 178 |
end; |
| 179 |
|
| 180 |
procedure TJSONParser.NumberValue(const AValue: TJSONStringType); |
| 181 |
begin |
| 182 |
// Do nothing |
| 183 |
if AValue='' then ; |
| 184 |
end; |
| 185 |
|
| 186 |
procedure TJSONParser.IntegerValue(const AValue: integer); |
| 187 |
begin |
| 188 |
NewValue(CreateJSON(AValue)); |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TJSONParser.Int64Value(const AValue: int64); |
| 192 |
begin |
| 193 |
NewValue(CreateJSON(AValue)); |
| 194 |
end; |
| 195 |
|
| 196 |
procedure TJSONParser.QWordValue(const AValue: QWord); |
| 197 |
begin |
| 198 |
NewValue(CreateJSON(AValue)); |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TJSONParser.StartArray; |
| 202 |
begin |
| 203 |
Push(NewValue(CreateJSONArray([]))) |
| 204 |
end; |
| 205 |
|
| 206 |
|
| 207 |
procedure TJSONParser.StartObject; |
| 208 |
begin |
| 209 |
Push(NewValue(CreateJSONObject([]))); |
| 210 |
end; |
| 211 |
|
| 212 |
procedure TJSONParser.EndArray; |
| 213 |
begin |
| 214 |
Pop(jtArray); |
| 215 |
end; |
| 216 |
|
| 217 |
procedure TJSONParser.EndObject; |
| 218 |
begin |
| 219 |
Pop(jtObject); |
| 220 |
end; |
| 221 |
|
| 222 |
|
| 223 |
function TJSONParser.Parse: TJSONData; |
| 224 |
|
| 225 |
begin |
| 226 |
SetLength(FStack,0); |
| 227 |
FStackPos:=0; |
| 228 |
FValue:=Nil; |
| 229 |
FStruct:=Nil; |
| 230 |
try |
| 231 |
DoExecute; |
| 232 |
Result:=FValue; |
| 233 |
except |
| 234 |
On E : exception do |
| 235 |
begin |
| 236 |
FreeAndNil(FValue); |
| 237 |
FStackPos:=0; |
| 238 |
SetLength(FStack,0); |
| 239 |
Raise; |
| 240 |
end; |
| 241 |
end; |
| 242 |
end; |
| 243 |
|
| 244 |
{ |
| 245 |
Consume next token and convert to JSON data structure. |
| 246 |
If AtCurrent is true, the current token is used. If false, |
| 247 |
a token is gotten from the scanner. |
| 248 |
If AllowEOF is false, encountering a tkEOF will result in an exception. |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
procedure InitJSONHandler; |
| 253 |
begin |
| 254 |
if @(GetJSONParserHandler) = nil then |
| 255 |
SetJSONParserHandler(DefJSONParserHandler); |
| 256 |
if @(GetJSONStringParserHandler) = nil then |
| 257 |
SetJSONStringParserHandler(DefJSONStringParserHandler); |
| 258 |
end; |
| 259 |
|
| 260 |
procedure DoneJSONHandler; |
| 261 |
begin |
| 262 |
if @(GetJSONParserHandler) = @DefJSONParserHandler then |
| 263 |
SetJSONParserHandler(nil); |
| 264 |
if @(GetJSONStringParserHandler) = @DefJSONStringParserHandler then |
| 265 |
SetJSONStringParserHandler(nil); |
| 266 |
end; |
| 267 |
|
| 268 |
initialization |
| 269 |
InitJSONHandler; |
| 270 |
finalization |
| 271 |
DoneJSONHandler; |
| 272 |
end. |
| 273 |
|