| 1 |
{ |
| 2 |
This file is part of the Free Component Library |
| 3 |
|
| 4 |
JSON Data structures |
| 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 |
unit fpjson; |
| 16 |
|
| 17 |
{$i fcl-json.inc} |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
{$IFNDEF PAS2JS} |
| 23 |
variants, |
| 24 |
{$ENDIF} |
| 25 |
{$IFDEF PAS2JS} |
| 26 |
JS, RTLConsts, Types, |
| 27 |
{$ENDIF} |
| 28 |
SysUtils, |
| 29 |
classes, |
| 30 |
contnrs, CastleUtils, Generics.Collections, FPHashCompatibility; |
| 31 |
|
| 32 |
type |
| 33 |
TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject); |
| 34 |
TJSONInstanceType = ( |
| 35 |
jitUnknown, |
| 36 |
jitNumberInteger, |
| 37 |
{$IFNDEF PAS2JS} |
| 38 |
jitNumberInt64, |
| 39 |
jitNumberQWord, |
| 40 |
{$ELSE} |
| 41 |
jitNumberNativeInt, |
| 42 |
{$ENDIF} |
| 43 |
jitNumberFloat, |
| 44 |
jitString, |
| 45 |
jitBoolean, |
| 46 |
jitNull, |
| 47 |
jitArray, |
| 48 |
jitObject); |
| 49 |
TJSONFloat = Double; |
| 50 |
TJSONStringType = {$IFNDEF PAS2JS}UTF8String{$else}string{$ENDIF}; |
| 51 |
TJSONUnicodeStringType = Unicodestring; |
| 52 |
{$IFNDEF PAS2JS} |
| 53 |
TJSONCharType = AnsiChar; |
| 54 |
PJSONCharType = ^TJSONCharType; |
| 55 |
TJSONVariant = variant; |
| 56 |
TFPJSStream = TMemoryStream; |
| 57 |
TJSONLargeInt = Int64; |
| 58 |
{$else} |
| 59 |
TJSONCharType = char; |
| 60 |
TJSONVariant = jsvalue; |
| 61 |
TFPJSStream = TJSArray; |
| 62 |
TJSONLargeInt = NativeInt; |
| 63 |
{$ENDIF} |
| 64 |
TFormatOption = (foSingleLineArray, // Array without CR/LF : all on one line |
| 65 |
foSingleLineObject, // Object without CR/LF : all on one line |
| 66 |
foDoNotQuoteMembers, // Do not quote object member names. |
| 67 |
foUseTabchar, // Use tab characters instead of spaces. |
| 68 |
foSkipWhiteSpace, // Do not use whitespace at all |
| 69 |
foSkipWhiteSpaceOnlyLeading // When foSkipWhiteSpace is active, skip whitespace for object members only before : |
| 70 |
); |
| 71 |
TFormatOptions = set of TFormatOption; |
| 72 |
|
| 73 |
Const |
| 74 |
DefaultIndentSize = 2; |
| 75 |
DefaultFormat = []; |
| 76 |
AsJSONFormat = [foSingleLineArray,foSingleLineObject]; // These options make FormatJSON behave as AsJSON |
| 77 |
AsCompressedJSON = [foSingleLineArray,foSingleLineObject,foskipWhiteSpace]; // These options make FormatJSON behave as AsJSON with TJSONData.CompressedJSON=True |
| 78 |
AsCompactJSON = [foSingleLineArray,foSingleLineObject,foskipWhiteSpace,foDoNotQuoteMembers]; // These options make FormatJSON behave as AsJSON with TJSONData.CompressedJSON=True and TJSONObject.UnquotedMemberNames=True |
| 79 |
ValueJSONTypes = [jtNumber, jtString, jtBoolean, jtNull]; |
| 80 |
ActualValueJSONTypes = ValueJSONTypes - [jtNull]; |
| 81 |
StructuredJSONTypes = [jtArray,jtObject]; |
| 82 |
{$IFDEF PAS2JS} |
| 83 |
jitNumberLargeInt = jitNumberNativeInt; |
| 84 |
{$ELSE} |
| 85 |
jitNumberLargeInt = jitNumberInt64; |
| 86 |
{$ENDIF} |
| 87 |
|
| 88 |
Type |
| 89 |
TJSONData = Class; |
| 90 |
|
| 91 |
{ TBaseJSONEnumerator } |
| 92 |
|
| 93 |
TJSONEnum = Record |
| 94 |
Key : TJSONStringType; |
| 95 |
KeyNum : Integer; |
| 96 |
Value : TJSONData; |
| 97 |
end; |
| 98 |
|
| 99 |
TBaseJSONEnumerator = class |
| 100 |
public |
| 101 |
function GetCurrent: TJSONEnum; virtual; abstract; |
| 102 |
function MoveNext : Boolean; virtual; abstract; |
| 103 |
property Current: TJSONEnum read GetCurrent; |
| 104 |
end; |
| 105 |
|
| 106 |
{ TJSONData } |
| 107 |
|
| 108 |
TJSONData = class(TObject) |
| 109 |
private |
| 110 |
Const |
| 111 |
ElementSeps : Array[Boolean] of TJSONStringType = (', ',','); |
| 112 |
Class Var FCompressedJSON : Boolean; |
| 113 |
Class Var FElementSep : TJSONStringType; |
| 114 |
class procedure DetermineElementSeparators; |
| 115 |
class function GetCompressedJSON: Boolean; {$IFNDEF PAS2JS}static;{$ENDIF} |
| 116 |
class procedure SetCompressedJSON(AValue: Boolean); {$IFNDEF PAS2JS}static;{$ENDIF} |
| 117 |
protected |
| 118 |
Class Procedure DoError(Const Msg : String); overload; |
| 119 |
Class Procedure DoError(Const Fmt : String; const Args : Array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}); overload; |
| 120 |
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; virtual; |
| 121 |
function GetAsBoolean: Boolean; virtual; abstract; |
| 122 |
function GetAsFloat: TJSONFloat; virtual; abstract; |
| 123 |
function GetAsInteger: Integer; virtual; abstract; |
| 124 |
function GetIsNull: Boolean; virtual; |
| 125 |
{$IFNDEF PAS2JS} |
| 126 |
function GetAsInt64: Int64; virtual; abstract; |
| 127 |
function GetAsQWord: QWord; virtual; abstract; |
| 128 |
function GetAsUnicodeString: TJSONUnicodeStringType; virtual; |
| 129 |
procedure SetAsInt64(const AValue: Int64); virtual; abstract; |
| 130 |
procedure SetAsQword(const AValue: QWord); virtual; abstract; |
| 131 |
procedure SetAsUnicodeString(const AValue: TJSONUnicodeStringType); virtual; |
| 132 |
{$ELSE} |
| 133 |
function GetAsNativeInt: NativeInt; virtual; abstract; |
| 134 |
procedure SetAsNativeInt(const AValue: NativeInt); virtual; abstract; |
| 135 |
{$ENDIF} |
| 136 |
procedure SetAsBoolean(const AValue: Boolean); virtual; abstract; |
| 137 |
procedure SetAsFloat(const AValue: TJSONFloat); virtual; abstract; |
| 138 |
procedure SetAsInteger(const AValue: Integer); virtual; abstract; |
| 139 |
function GetAsJSON: TJSONStringType; virtual; abstract; |
| 140 |
function GetAsString: TJSONStringType; virtual; abstract; |
| 141 |
procedure SetAsString(const AValue: TJSONStringType); virtual; abstract; |
| 142 |
function GetValue: TJSONVariant; virtual; abstract; |
| 143 |
procedure SetValue(const AValue: TJSONVariant); virtual; abstract; |
| 144 |
function GetItem(Index : Integer): TJSONData; virtual; |
| 145 |
procedure SetItem(Index : Integer; const AValue: TJSONData); virtual; |
| 146 |
Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; virtual; |
| 147 |
function GetCount: Integer; virtual; |
| 148 |
Public |
| 149 |
Class function JSONType: TJSONType; virtual; |
| 150 |
Class Property CompressedJSON : Boolean Read GetCompressedJSON Write SetCompressedJSON; |
| 151 |
public |
| 152 |
Constructor Create; virtual; |
| 153 |
Procedure Clear; virtual; Abstract; |
| 154 |
Procedure DumpJSON(S : TFPJSStream); |
| 155 |
// Get enumerator |
| 156 |
function GetEnumerator: TBaseJSONEnumerator; virtual; |
| 157 |
Function FindPath(Const APath : TJSONStringType) : TJSONdata; |
| 158 |
Function GetPath(Const APath : TJSONStringType) : TJSONdata; |
| 159 |
Function Clone : TJSONData; virtual; abstract; |
| 160 |
Function FormatJSON(Options : TFormatOptions = DefaultFormat; Indentsize : Integer = DefaultIndentSize) : TJSONStringType; |
| 161 |
property Count: Integer read GetCount; |
| 162 |
property Items[Index: Integer]: TJSONData read GetItem write SetItem; |
| 163 |
property Value: TJSONVariant read GetValue write SetValue; |
| 164 |
Property AsString : TJSONStringType Read GetAsString Write SetAsString; |
| 165 |
{$IFNDEF PAS2JS} |
| 166 |
Property AsUnicodeString : TJSONUnicodeStringType Read GetAsUnicodeString Write SetAsUnicodeString; |
| 167 |
Property AsInt64 : Int64 Read GetAsInt64 Write SetAsInt64; |
| 168 |
Property AsQWord : QWord Read GetAsQWord Write SetAsQword; |
| 169 |
Property AsLargeInt : TJSONLargeInt Read GetAsInt64 Write SetAsInt64; |
| 170 |
{$ELSE} |
| 171 |
Property AsNativeInt : NativeInt Read GetAsNativeInt Write SetAsNativeInt; |
| 172 |
Property AsLargeInt : TJSONLargeInt Read GetAsNativeInt Write SetAsNativeInt; |
| 173 |
{$ENDIF} |
| 174 |
Property AsFloat : TJSONFloat Read GetAsFloat Write SetAsFloat; |
| 175 |
Property AsInteger : Integer Read GetAsInteger Write SetAsInteger; |
| 176 |
Property AsBoolean : Boolean Read GetAsBoolean Write SetAsBoolean; |
| 177 |
Property IsNull : Boolean Read GetIsNull; |
| 178 |
Property AsJSON : TJSONStringType Read GetAsJSON; |
| 179 |
end; |
| 180 |
|
| 181 |
TJSONDataClass = Class of TJSONData; |
| 182 |
TJSONNumberType = ( |
| 183 |
ntFloat, |
| 184 |
ntInteger |
| 185 |
{$IFNDEF PAS2JS} |
| 186 |
,ntInt64 |
| 187 |
,ntQWord |
| 188 |
{$else} |
| 189 |
,ntNativeInt |
| 190 |
{$ENDIF} |
| 191 |
); |
| 192 |
|
| 193 |
TJSONNumber = class(TJSONData) |
| 194 |
protected |
| 195 |
public |
| 196 |
class function JSONType: TJSONType; override; |
| 197 |
class function NumberType : TJSONNumberType; virtual; abstract; |
| 198 |
end; |
| 199 |
|
| 200 |
{ TJSONFloatNumber } |
| 201 |
|
| 202 |
TJSONFloatNumber = class(TJSONNumber) |
| 203 |
Private |
| 204 |
FValue : TJSONFloat; |
| 205 |
protected |
| 206 |
function GetAsBoolean: Boolean; override; |
| 207 |
function GetAsFloat: TJSONFloat; override; |
| 208 |
function GetAsInteger: Integer; override; |
| 209 |
function GetAsJSON: TJSONStringType; override; |
| 210 |
function GetAsString: TJSONStringType; override; |
| 211 |
function GetValue: TJSONVariant; override; |
| 212 |
{$IFNDEF PAS2JS} |
| 213 |
function GetAsInt64: Int64; override; |
| 214 |
function GetAsQWord: QWord; override; |
| 215 |
procedure SetAsInt64(const AValue: Int64); override; |
| 216 |
procedure SetAsQword(const AValue: QWord); override; |
| 217 |
{$ELSE} |
| 218 |
function GetAsNativeInt: NativeInt; override; |
| 219 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 220 |
{$ENDIF} |
| 221 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 222 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 223 |
procedure SetAsInteger(const AValue: Integer); override; |
| 224 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 225 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 226 |
public |
| 227 |
Constructor Create(AValue : TJSONFloat); reintroduce; |
| 228 |
class function NumberType : TJSONNumberType; override; |
| 229 |
Procedure Clear; override; |
| 230 |
Function Clone : TJSONData; override; |
| 231 |
end; |
| 232 |
TJSONFloatNumberClass = Class of TJSONFloatNumber; |
| 233 |
|
| 234 |
{ TJSONIntegerNumber } |
| 235 |
|
| 236 |
TJSONIntegerNumber = class(TJSONNumber) |
| 237 |
Private |
| 238 |
FValue : Integer; |
| 239 |
protected |
| 240 |
function GetAsBoolean: Boolean; override; |
| 241 |
function GetAsFloat: TJSONFloat; override; |
| 242 |
function GetAsInteger: Integer; override; |
| 243 |
{$IFNDEF PAS2JS} |
| 244 |
function GetAsInt64: Int64; override; |
| 245 |
function GetAsQWord: QWord; override; |
| 246 |
procedure SetAsInt64(const AValue: Int64); override; |
| 247 |
procedure SetAsQword(const AValue: QWord); override; |
| 248 |
{$ELSE} |
| 249 |
function GetAsNativeInt: NativeInt; override; |
| 250 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 251 |
{$ENDIF} |
| 252 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 253 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 254 |
procedure SetAsInteger(const AValue: Integer); override; |
| 255 |
function GetAsJSON: TJSONStringType; override; |
| 256 |
function GetAsString: TJSONStringType; override; |
| 257 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 258 |
function GetValue: TJSONVariant; override; |
| 259 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 260 |
public |
| 261 |
Constructor Create(AValue : Integer); reintroduce; |
| 262 |
class function NumberType : TJSONNumberType; override; |
| 263 |
Procedure Clear; override; |
| 264 |
Function Clone : TJSONData; override; |
| 265 |
end; |
| 266 |
TJSONIntegerNumberClass = Class of TJSONIntegerNumber; |
| 267 |
|
| 268 |
{$IFNDEF PAS2JS} |
| 269 |
{ TJSONInt64Number } |
| 270 |
|
| 271 |
TJSONInt64Number = class(TJSONNumber) |
| 272 |
Private |
| 273 |
FValue : Int64; |
| 274 |
protected |
| 275 |
function GetAsBoolean: Boolean; override; |
| 276 |
function GetAsFloat: TJSONFloat; override; |
| 277 |
function GetAsInteger: Integer; override; |
| 278 |
function GetAsInt64: Int64; override; |
| 279 |
function GetAsQWord: QWord; override; |
| 280 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 281 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 282 |
procedure SetAsInteger(const AValue: Integer); override; |
| 283 |
procedure SetAsInt64(const AValue: Int64); override; |
| 284 |
procedure SetAsQword(const AValue: QWord); override; |
| 285 |
function GetAsJSON: TJSONStringType; override; |
| 286 |
function GetAsString: TJSONStringType; override; |
| 287 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 288 |
function GetValue: TJSONVariant; override; |
| 289 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 290 |
public |
| 291 |
Constructor Create(AValue : Int64); reintroduce; |
| 292 |
class function NumberType : TJSONNumberType; override; |
| 293 |
Procedure Clear; override; |
| 294 |
Function Clone : TJSONData; override; |
| 295 |
end; |
| 296 |
TJSONInt64NumberClass = Class of TJSONInt64Number; |
| 297 |
|
| 298 |
TJSONLargeIntNumber = TJSONInt64Number; |
| 299 |
TJSONLargeIntNumberClass = TJSONInt64NumberClass; |
| 300 |
{$ELSE} |
| 301 |
{ TJSONNativeIntNumber } |
| 302 |
|
| 303 |
TJSONNativeIntNumber = class(TJSONNumber) |
| 304 |
Private |
| 305 |
FValue : NativeInt; |
| 306 |
protected |
| 307 |
function GetAsBoolean: Boolean; override; |
| 308 |
function GetAsFloat: TJSONFloat; override; |
| 309 |
function GetAsInteger: Integer; override; |
| 310 |
function GetAsNativeInt: NativeInt; override; |
| 311 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 312 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 313 |
procedure SetAsInteger(const AValue: Integer); override; |
| 314 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 315 |
function GetAsJSON: TJSONStringType; override; |
| 316 |
function GetAsString: TJSONStringType; override; |
| 317 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 318 |
function GetValue: TJSONVariant; override; |
| 319 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 320 |
public |
| 321 |
Constructor Create(AValue : NativeInt); reintroduce; |
| 322 |
class function NumberType : TJSONNumberType; override; |
| 323 |
Procedure Clear; override; |
| 324 |
Function Clone : TJSONData; override; |
| 325 |
end; |
| 326 |
TJSONNativeIntNumberClass = Class of TJSONNativeIntNumber; |
| 327 |
|
| 328 |
TJSONLargeIntNumber = TJSONNativeIntNumber; |
| 329 |
TJSONLargeIntNumberClass = TJSONNativeIntNumberClass; |
| 330 |
{$ENDIF} |
| 331 |
|
| 332 |
{$IFNDEF PAS2JS} |
| 333 |
{ TJSONQWordNumber } |
| 334 |
|
| 335 |
TJSONQWordNumber = class(TJSONNumber) |
| 336 |
Private |
| 337 |
FValue : Qword; |
| 338 |
protected |
| 339 |
function GetAsBoolean: Boolean; override; |
| 340 |
function GetAsFloat: TJSONFloat; override; |
| 341 |
function GetAsInteger: Integer; override; |
| 342 |
function GetAsInt64: Int64; override; |
| 343 |
function GetAsQWord: QWord; override; |
| 344 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 345 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 346 |
procedure SetAsInteger(const AValue: Integer); override; |
| 347 |
procedure SetAsInt64(const AValue: Int64); override; |
| 348 |
procedure SetAsQword(const AValue: QWord); override; |
| 349 |
function GetAsJSON: TJSONStringType; override; |
| 350 |
function GetAsString: TJSONStringType; override; |
| 351 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 352 |
function GetValue: TJSONVariant; override; |
| 353 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 354 |
public |
| 355 |
Constructor Create(AValue : QWord); reintroduce; |
| 356 |
class function NumberType : TJSONNumberType; override; |
| 357 |
Procedure Clear; override; |
| 358 |
Function Clone : TJSONData; override; |
| 359 |
end; |
| 360 |
TJSONQWordNumberClass = Class of TJSONQWordNumber; |
| 361 |
{$ENDIF} |
| 362 |
|
| 363 |
{ TJSONString } |
| 364 |
|
| 365 |
TJSONString = class(TJSONData) |
| 366 |
Private |
| 367 |
FValue: TJSONStringType; |
| 368 |
protected |
| 369 |
function GetValue: TJSONVariant; override; |
| 370 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 371 |
function GetAsBoolean: Boolean; override; |
| 372 |
function GetAsFloat: TJSONFloat; override; |
| 373 |
function GetAsInteger: Integer; override; |
| 374 |
{$IFNDEF PAS2JS} |
| 375 |
function GetAsInt64: Int64; override; |
| 376 |
function GetAsQWord: QWord; override; |
| 377 |
procedure SetAsInt64(const AValue: Int64); override; |
| 378 |
procedure SetAsQword(const AValue: QWord); override; |
| 379 |
{$ELSE} |
| 380 |
function GetAsNativeInt: NativeInt; override; |
| 381 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 382 |
{$ENDIF} |
| 383 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 384 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 385 |
procedure SetAsInteger(const AValue: Integer); override; |
| 386 |
function GetAsJSON: TJSONStringType; override; |
| 387 |
function GetAsString: TJSONStringType; override; |
| 388 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 389 |
Public |
| 390 |
Class var StrictEscaping : Boolean; |
| 391 |
public |
| 392 |
Constructor Create(const AValue : TJSONStringType); reintroduce; overload; |
| 393 |
{$IFNDEF PAS2JS} |
| 394 |
Constructor Create(const AValue : TJSONUnicodeStringType); reintroduce; overload; |
| 395 |
{$ENDIF} |
| 396 |
class function JSONType: TJSONType; override; |
| 397 |
Procedure Clear; override; |
| 398 |
Function Clone : TJSONData; override; |
| 399 |
end; |
| 400 |
TJSONStringClass = Class of TJSONString; |
| 401 |
|
| 402 |
{ TJSONBoolean } |
| 403 |
|
| 404 |
TJSONBoolean = class(TJSONData) |
| 405 |
Private |
| 406 |
FValue: Boolean; |
| 407 |
protected |
| 408 |
function GetValue: TJSONVariant; override; |
| 409 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 410 |
function GetAsBoolean: Boolean; override; |
| 411 |
function GetAsFloat: TJSONFloat; override; |
| 412 |
function GetAsInteger: Integer; override; |
| 413 |
{$IFNDEF PAS2JS} |
| 414 |
function GetAsInt64: Int64; override; |
| 415 |
function GetAsQWord: QWord; override; |
| 416 |
procedure SetAsInt64(const AValue: Int64); override; |
| 417 |
procedure SetAsQword(const AValue: QWord); override; |
| 418 |
{$ELSE} |
| 419 |
function GetAsNativeInt: NativeInt; override; |
| 420 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 421 |
{$ENDIF} |
| 422 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 423 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 424 |
procedure SetAsInteger(const AValue: Integer); override; |
| 425 |
function GetAsJSON: TJSONStringType; override; |
| 426 |
function GetAsString: TJSONStringType; override; |
| 427 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 428 |
public |
| 429 |
Constructor Create(AValue : Boolean); reintroduce; |
| 430 |
class function JSONType: TJSONType; override; |
| 431 |
Procedure Clear; override; |
| 432 |
Function Clone : TJSONData; override; |
| 433 |
end; |
| 434 |
TJSONBooleanClass = Class of TJSONBoolean; |
| 435 |
|
| 436 |
{ TJSONnull } |
| 437 |
|
| 438 |
TJSONNull = class(TJSONData) |
| 439 |
protected |
| 440 |
Procedure Converterror(From : Boolean); |
| 441 |
function GetAsBoolean: Boolean; override; |
| 442 |
function GetAsFloat: TJSONFloat; override; |
| 443 |
function GetAsInteger: Integer; override; |
| 444 |
function GetIsNull: Boolean; override; |
| 445 |
function GetAsJSON: TJSONStringType; override; |
| 446 |
function GetAsString: TJSONStringType; override; |
| 447 |
function GetValue: TJSONVariant; override; |
| 448 |
{$IFNDEF PAS2JS} |
| 449 |
function GetAsInt64: Int64; override; |
| 450 |
function GetAsQWord: QWord; override; |
| 451 |
procedure SetAsInt64(const AValue: Int64); override; |
| 452 |
procedure SetAsQword(const AValue: QWord); override; |
| 453 |
{$ELSE} |
| 454 |
function GetAsNativeInt: NativeInt; override; |
| 455 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 456 |
{$ENDIF} |
| 457 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 458 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 459 |
procedure SetAsInteger(const AValue: Integer); override; |
| 460 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 461 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 462 |
public |
| 463 |
class function JSONType: TJSONType; override; |
| 464 |
Procedure Clear; override; |
| 465 |
Function Clone : TJSONData; override; |
| 466 |
end; |
| 467 |
TJSONNullClass = Class of TJSONNull; |
| 468 |
|
| 469 |
TJSONArrayIterator = procedure(Item: TJSONData; Data: TObject; var Continue: Boolean) of object; |
| 470 |
|
| 471 |
{ TJSONArray } |
| 472 |
TJSONObject = Class; |
| 473 |
|
| 474 |
TJSONArray = class(TJSONData) |
| 475 |
Private |
| 476 |
FList : TObjectList; |
| 477 |
function GetArrays(Index : Integer): TJSONArray; |
| 478 |
function GetBooleans(Index : Integer): Boolean; |
| 479 |
function GetFloats(Index : Integer): TJSONFloat; |
| 480 |
function GetIntegers(Index : Integer): Integer; |
| 481 |
function GetNulls(Index : Integer): Boolean; |
| 482 |
function GetObjects(Index : Integer): TJSONObject; |
| 483 |
function GetStrings(Index : Integer): TJSONStringType; |
| 484 |
function GetTypes(Index : Integer): TJSONType; |
| 485 |
{$IFNDEF PAS2JS} |
| 486 |
function GetInt64s(Index : Integer): Int64; |
| 487 |
function GetQWords(Index : Integer): QWord; |
| 488 |
function GetUnicodeStrings(Index : Integer): TJSONUnicodeStringType; |
| 489 |
procedure SetInt64s(Index : Integer; const AValue: Int64); |
| 490 |
procedure SetQWords(Index : Integer; AValue: QWord); |
| 491 |
procedure SetUnicodeStrings(Index : Integer; const AValue: TJSONUnicodeStringType); |
| 492 |
{$ELSE} |
| 493 |
function GetNativeInts(Index : Integer): NativeInt; |
| 494 |
procedure SetNativeInts(Index : Integer; AValue: NativeInt); |
| 495 |
{$ENDIF} |
| 496 |
procedure SetArrays(Index : Integer; const AValue: TJSONArray); |
| 497 |
procedure SetBooleans(Index : Integer; const AValue: Boolean); |
| 498 |
procedure SetFloats(Index : Integer; const AValue: TJSONFloat); |
| 499 |
procedure SetIntegers(Index : Integer; const AValue: Integer); |
| 500 |
procedure SetObjects(Index : Integer; const AValue: TJSONObject); |
| 501 |
procedure SetStrings(Index : Integer; const AValue: TJSONStringType); |
| 502 |
protected |
| 503 |
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; override; |
| 504 |
Procedure Converterror(From : Boolean); |
| 505 |
function GetAsBoolean: Boolean; override; |
| 506 |
function GetAsFloat: TJSONFloat; override; |
| 507 |
function GetAsInteger: Integer; override; |
| 508 |
{$IFNDEF PAS2JS} |
| 509 |
function GetAsInt64: Int64; override; |
| 510 |
function GetAsQWord: QWord; override; |
| 511 |
procedure SetAsInt64(const AValue: Int64); override; |
| 512 |
procedure SetAsQword(const AValue: QWord); override; |
| 513 |
{$ELSE} |
| 514 |
function GetAsNativeInt: NativeInt; override; |
| 515 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 516 |
{$ENDIF} |
| 517 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 518 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 519 |
procedure SetAsInteger(const AValue: Integer); override; |
| 520 |
function GetAsJSON: TJSONStringType; override; |
| 521 |
function GetAsString: TJSONStringType; override; |
| 522 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 523 |
function GetValue: TJSONVariant; override; |
| 524 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 525 |
function GetCount: Integer; override; |
| 526 |
function GetItem(Index : Integer): TJSONData; override; |
| 527 |
procedure SetItem(Index : Integer; const AValue: TJSONData); override; |
| 528 |
Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override; |
| 529 |
public |
| 530 |
Constructor Create; reintroduce; overload; |
| 531 |
Constructor Create(const Elements : Array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}); reintroduce; overload; |
| 532 |
Destructor Destroy; override; |
| 533 |
class function JSONType: TJSONType; override; |
| 534 |
Function Clone : TJSONData; override; |
| 535 |
// Examine |
| 536 |
procedure Iterate(Iterator : TJSONArrayIterator; Data: TObject); |
| 537 |
function IndexOf(obj: TJSONData): Integer; |
| 538 |
function GetEnumerator: TBaseJSONEnumerator; override; |
| 539 |
// Manipulate |
| 540 |
Procedure Clear; override; |
| 541 |
function Add(Item : TJSONData): Integer; overload; |
| 542 |
function Add(I : Integer): Integer; overload; |
| 543 |
{$IFNDEF PAS2JS} |
| 544 |
function Add(I : Int64): Int64; overload; |
| 545 |
function Add(I : QWord): QWord; overload; |
| 546 |
/// function Add(const S : UnicodeString): Integer; overload; |
| 547 |
{$ELSE} |
| 548 |
function Add(I : NativeInt): Integer; |
| 549 |
{$ENDIF} |
| 550 |
function Add(const S : String): Integer; overload; |
| 551 |
function Add: Integer; overload; |
| 552 |
function Add(F : TJSONFloat): Integer; overload; |
| 553 |
function Add(B : Boolean): Integer; overload; |
| 554 |
function Add(AnArray : TJSONArray): Integer; overload; |
| 555 |
function Add(AnObject: TJSONObject): Integer; overload; |
| 556 |
Procedure Delete(Index : Integer); |
| 557 |
procedure Exchange(Index1, Index2: Integer); |
| 558 |
function Extract(Item: TJSONData): TJSONData; overload; |
| 559 |
function Extract(Index : Integer): TJSONData; overload; |
| 560 |
procedure Insert(Index: Integer); overload; |
| 561 |
procedure Insert(Index: Integer; Item : TJSONData); overload; |
| 562 |
procedure Insert(Index: Integer; I : Integer); overload; |
| 563 |
{$IFNDEF PAS2JS} |
| 564 |
procedure Insert(Index: Integer; I : Int64); overload; |
| 565 |
procedure Insert(Index: Integer; I : QWord); overload; |
| 566 |
/// procedure Insert(Index: Integer; const S : UnicodeString); |
| 567 |
{$ELSE} |
| 568 |
procedure Insert(Index: Integer; I : NativeInt); overload; |
| 569 |
{$ENDIF} |
| 570 |
procedure Insert(Index: Integer; const S : String); overload; |
| 571 |
procedure Insert(Index: Integer; F : TJSONFloat); overload; |
| 572 |
procedure Insert(Index: Integer; B : Boolean); overload; |
| 573 |
procedure Insert(Index: Integer; AnArray : TJSONArray); overload; |
| 574 |
procedure Insert(Index: Integer; AnObject: TJSONObject); overload; |
| 575 |
procedure Move(CurIndex, NewIndex: Integer); |
| 576 |
Procedure Remove(Item : TJSONData); |
| 577 |
Procedure Sort(Compare: TListSortCompare); |
| 578 |
// Easy Access Properties. |
| 579 |
property Items;default; |
| 580 |
Property Types[Index : Integer] : TJSONType Read GetTypes; |
| 581 |
Property Nulls[Index : Integer] : Boolean Read GetNulls; |
| 582 |
Property Integers[Index : Integer] : Integer Read GetIntegers Write SetIntegers; |
| 583 |
{$IFNDEF PAS2JS} |
| 584 |
Property Int64s[Index : Integer] : Int64 Read GetInt64s Write SetInt64s; |
| 585 |
Property LargeInts[Index : Integer] : TJSONLargeInt Read GetInt64s Write SetInt64s; |
| 586 |
Property QWords[Index : Integer] : QWord Read GetQWords Write SetQWords; |
| 587 |
Property UnicodeStrings[Index : Integer] : TJSONUnicodeStringType Read GetUnicodeStrings Write SetUnicodeStrings; |
| 588 |
{$ELSE} |
| 589 |
Property NativeInts[Index : Integer] : NativeInt Read GetNativeInts Write SetNativeInts; |
| 590 |
Property LargeInts[Index : Integer] : TJSONLargeInt Read GetNativeInts Write SetNativeInts; |
| 591 |
{$ENDIF} |
| 592 |
Property Strings[Index : Integer] : TJSONStringType Read GetStrings Write SetStrings; |
| 593 |
Property Floats[Index : Integer] : TJSONFloat Read GetFloats Write SetFloats; |
| 594 |
Property Booleans[Index : Integer] : Boolean Read GetBooleans Write SetBooleans; |
| 595 |
Property Arrays[Index : Integer] : TJSONArray Read GetArrays Write SetArrays; |
| 596 |
Property Objects[Index : Integer] : TJSONObject Read GetObjects Write SetObjects; |
| 597 |
end; |
| 598 |
TJSONArrayClass = Class of TJSONArray; |
| 599 |
|
| 600 |
TJSONObjectIterator = procedure(Const AName : TJSONStringType; Item: TJSONData; Data: TObject; var Continue: Boolean) of object; |
| 601 |
|
| 602 |
{ TJSONObject } |
| 603 |
|
| 604 |
TJSONObject = class(TJSONData) |
| 605 |
private |
| 606 |
Const |
| 607 |
ElementStart : Array[Boolean] of TJSONStringType = ('"',''); |
| 608 |
SpacedQuoted : Array[Boolean] of TJSONStringType = ('" : ',' : '); |
| 609 |
UnSpacedQuoted : Array[Boolean] of TJSONStringType = ('":',':'); |
| 610 |
ObjStartSeps : Array[Boolean] of TJSONStringType = ('{ ','{'); |
| 611 |
ObjEndSeps : Array[Boolean] of TJSONStringType = (' }','}'); |
| 612 |
Class var FUnquotedMemberNames: Boolean; |
| 613 |
Class var FObjStartSep,FObjEndSep,FElementEnd,FElementStart : TJSONStringType; |
| 614 |
function DoAdd(const AName: TJSONStringType; AValue: TJSONData; FreeOnError: Boolean=True): Integer; |
| 615 |
Class procedure DetermineElementQuotes; |
| 616 |
Private |
| 617 |
{$IFDEF PAS2JS} |
| 618 |
FCount: integer; |
| 619 |
FHash: TJSObject; |
| 620 |
FNames: TStringDynArray; |
| 621 |
{$else} |
| 622 |
FHash: TFPHashObjectList; // Careful : Names limited to 255 chars. |
| 623 |
{$ENDIF} |
| 624 |
function GetArrays(const AName : String): TJSONArray; |
| 625 |
function GetBooleans(const AName : String): Boolean; |
| 626 |
function GetElements(const AName: string): TJSONData; |
| 627 |
function GetFloats(const AName : String): TJSONFloat; |
| 628 |
function GetIntegers(const AName : String): Integer; |
| 629 |
function GetIsNull(const AName : String): Boolean; reintroduce; |
| 630 |
function GetNameOf(Index : Integer): TJSONStringType; |
| 631 |
function GetObjects(const AName : String): TJSONObject; |
| 632 |
function GetStrings(const AName : String): TJSONStringType; |
| 633 |
function GetTypes(const AName : String): TJSONType; |
| 634 |
procedure SetArrays(const AName : String; const AValue: TJSONArray); |
| 635 |
procedure SetBooleans(const AName : String; const AValue: Boolean); |
| 636 |
procedure SetElements(const AName: string; const AValue: TJSONData); |
| 637 |
procedure SetFloats(const AName : String; const AValue: TJSONFloat); |
| 638 |
procedure SetIntegers(const AName : String; const AValue: Integer); |
| 639 |
{$IFNDEF PAS2JS} |
| 640 |
function GetInt64s(const AName : String): Int64; |
| 641 |
function GetUnicodeStrings(const AName : String): TJSONUnicodeStringType; |
| 642 |
function GetQWords(const AName : String): QWord; |
| 643 |
procedure SetInt64s(const AName : String; const AValue: Int64); |
| 644 |
procedure SetQWords(const AName : String; AValue: QWord); |
| 645 |
procedure SetUnicodeStrings(const AName : String; const AValue: TJSONUnicodeStringType); |
| 646 |
{$ELSE} |
| 647 |
function GetNativeInts(const AName : String): NativeInt; |
| 648 |
procedure SetNativeInts(const AName : String; const AValue: NativeInt); |
| 649 |
{$ENDIF} |
| 650 |
procedure SetIsNull(const AName : String; const AValue: Boolean); |
| 651 |
procedure SetObjects(const AName : String; const AValue: TJSONObject); |
| 652 |
procedure SetStrings(const AName : String; const AValue: TJSONStringType); |
| 653 |
class function GetUnquotedMemberNames: Boolean; {$IFNDEF PAS2JS}static;{$ENDIF} |
| 654 |
class procedure SetUnquotedMemberNames(AValue: Boolean); {$IFNDEF PAS2JS}static;{$ENDIF} |
| 655 |
protected |
| 656 |
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; override; |
| 657 |
Procedure Converterror(From : Boolean); |
| 658 |
function GetAsBoolean: Boolean; override; |
| 659 |
function GetAsFloat: TJSONFloat; override; |
| 660 |
function GetAsInteger: Integer; override; |
| 661 |
{$IFNDEF PAS2JS} |
| 662 |
function GetAsInt64: Int64; override; |
| 663 |
function GetAsQWord: QWord; override; |
| 664 |
procedure SetAsInt64(const AValue: Int64); override; |
| 665 |
procedure SetAsQword(const AValue: QWord); override; |
| 666 |
{$ELSE} |
| 667 |
function GetAsNativeInt: NativeInt; override; |
| 668 |
procedure SetAsNativeInt(const AValue: NativeInt); override; |
| 669 |
{$ENDIF} |
| 670 |
procedure SetAsBoolean(const AValue: Boolean); override; |
| 671 |
procedure SetAsFloat(const AValue: TJSONFloat); override; |
| 672 |
procedure SetAsInteger(const AValue: Integer); override; |
| 673 |
function GetAsJSON: TJSONStringType; override; |
| 674 |
function GetAsString: TJSONStringType; override; |
| 675 |
procedure SetAsString(const AValue: TJSONStringType); override; |
| 676 |
function GetValue: TJSONVariant; override; |
| 677 |
procedure SetValue(const AValue: TJSONVariant); override; |
| 678 |
function GetCount: Integer; override; |
| 679 |
function GetItem(Index : Integer): TJSONData; override; |
| 680 |
procedure SetItem(Index : Integer; const AValue: TJSONData); override; |
| 681 |
Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override; |
| 682 |
public |
| 683 |
constructor Create; reintroduce; overload; |
| 684 |
Constructor Create(const Elements : Array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}); reintroduce; overload; |
| 685 |
destructor Destroy; override; |
| 686 |
class function JSONType: TJSONType; override; |
| 687 |
Class Property UnquotedMemberNames : Boolean Read GetUnquotedMemberNames Write SetUnquotedMemberNames; |
| 688 |
Function Clone : TJSONData; override; |
| 689 |
function GetEnumerator: TBaseJSONEnumerator; override; |
| 690 |
// Examine |
| 691 |
procedure Iterate(Iterator : TJSONObjectIterator; Data: TObject); |
| 692 |
function IndexOf(Item: TJSONData): Integer; |
| 693 |
Function IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer; |
| 694 |
Function Find(Const AName : String) : TJSONData; overload; |
| 695 |
Function Find(Const AName : String; AType : TJSONType) : TJSONData; overload; |
| 696 |
function Find(const key: TJSONStringType; out AValue: TJSONData): boolean; overload; |
| 697 |
function Find(const key: TJSONStringType; out AValue: TJSONObject): boolean; overload; |
| 698 |
function Find(const key: TJSONStringType; out AValue: TJSONArray): boolean; overload; |
| 699 |
function Find(const key: TJSONStringType; out AValue: TJSONString): boolean; overload; |
| 700 |
function Find(const key: TJSONStringType; out AValue: TJSONBoolean): boolean; overload; |
| 701 |
function Find(const key: TJSONStringType; out AValue: TJSONNumber): boolean; overload; |
| 702 |
Function Get(Const AName : String) : TJSONVariant; overload; |
| 703 |
Function Get(Const AName : String; ADefault : TJSONFloat) : TJSONFloat; overload; |
| 704 |
Function Get(Const AName : String; ADefault : Integer) : Integer; overload; |
| 705 |
{$IFNDEF PAS2JS} |
| 706 |
Function Get(Const AName : String; ADefault : Int64) : Int64; overload; |
| 707 |
Function Get(Const AName : String; ADefault : QWord) : QWord; overload; |
| 708 |
Function Get(Const AName : String; ADefault : TJSONUnicodeStringType) : TJSONUnicodeStringType; overload; |
| 709 |
{$ENDIF} |
| 710 |
Function Get(Const AName : String; ADefault : Boolean) : Boolean; overload; |
| 711 |
Function Get(Const AName : String; ADefault : TJSONStringType) : TJSONStringType; overload; |
| 712 |
Function Get(Const AName : String; ADefault : TJSONArray) : TJSONArray; overload; |
| 713 |
Function Get(Const AName : String; ADefault : TJSONObject) : TJSONObject; overload; |
| 714 |
// Manipulate |
| 715 |
Procedure Clear; override; |
| 716 |
function Add(const AName: TJSONStringType; AValue: TJSONData): Integer; overload; |
| 717 |
function Add(const AName: TJSONStringType; AValue: Boolean): Integer; overload; |
| 718 |
function Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer; overload; |
| 719 |
function Add(const AName, AValue: TJSONStringType): Integer; overload; |
| 720 |
{$IFNDEF PAS2JS} |
| 721 |
function Add(const AName : String; AValue: TJSONUnicodeStringType): Integer; overload; |
| 722 |
function Add(const AName: TJSONStringType; Avalue: Int64): Integer; overload; |
| 723 |
function Add(const AName: TJSONStringType; Avalue: QWord): Integer; overload; |
| 724 |
{$ELSE} |
| 725 |
function Add(const AName: TJSONStringType; Avalue: NativeInt): Integer; overload; |
| 726 |
{$ENDIF} |
| 727 |
function Add(const AName: TJSONStringType; Avalue: Integer): Integer; overload; |
| 728 |
function Add(const AName: TJSONStringType): Integer; overload; |
| 729 |
function Add(const AName: TJSONStringType; AValue : TJSONArray): Integer; overload; |
| 730 |
procedure Delete(Index : Integer); overload; |
| 731 |
procedure Delete(Const AName : string); overload; |
| 732 |
procedure Remove(Item : TJSONData); |
| 733 |
Function Extract(Index : Integer) : TJSONData; overload; |
| 734 |
Function Extract(Const AName : string) : TJSONData; overload; |
| 735 |
// Easy access properties. |
| 736 |
property Names[Index : Integer] : TJSONStringType read GetNameOf; |
| 737 |
property Elements[const AName: String] : TJSONData read GetElements write SetElements; default; |
| 738 |
|
| 739 |
Property Types[const AName : String] : TJSONType Read GetTypes; |
| 740 |
Property Nulls[const AName : String] : Boolean Read GetIsNull Write SetIsNull; |
| 741 |
Property Floats[const AName : String] : TJSONFloat Read GetFloats Write SetFloats; |
| 742 |
Property Integers[const AName : String] : Integer Read GetIntegers Write SetIntegers; |
| 743 |
{$IFNDEF PAS2JS} |
| 744 |
Property Int64s[const AName : String] : Int64 Read GetInt64s Write SetInt64s; |
| 745 |
Property QWords[const AName : String] : QWord Read GetQWords Write SetQWords; |
| 746 |
Property LargeInts[const AName : String] : TJSONLargeInt Read GetInt64s Write SetInt64s; |
| 747 |
Property UnicodeStrings[const AName : String] : TJSONUnicodeStringType Read GetUnicodeStrings Write SetUnicodeStrings; |
| 748 |
{$ELSE} |
| 749 |
Property NativeInts[const AName : String] : NativeInt Read GetNativeInts Write SetNativeInts; |
| 750 |
Property LargeInts[const AName : String] : TJSONLargeInt Read GetNativeInts Write SetNativeInts; |
| 751 |
{$ENDIF} |
| 752 |
Property Strings[const AName : String] : TJSONStringType Read GetStrings Write SetStrings; |
| 753 |
Property Booleans[const AName : String] : Boolean Read GetBooleans Write SetBooleans; |
| 754 |
Property Arrays[const AName : String] : TJSONArray Read GetArrays Write SetArrays; |
| 755 |
Property Objects[const AName : String] : TJSONObject Read GetObjects Write SetObjects; |
| 756 |
end; |
| 757 |
TJSONObjectClass = Class of TJSONObject; |
| 758 |
|
| 759 |
EJSON = Class(Exception); |
| 760 |
|
| 761 |
{$IFNDEF PAS2JS} |
| 762 |
TJSONParserHandler = Procedure(AStream : TStream; Const AUseUTF8 : Boolean; Out Data : TJSONData); |
| 763 |
TJSONStringParserHandler = Procedure(Const aJSON : TJSONStringType; Const AUseUTF8 : Boolean; Out Data : TJSONData); |
| 764 |
{$ENDIF} |
| 765 |
|
| 766 |
Function SetJSONInstanceType(AType : TJSONInstanceType; AClass : TJSONDataClass) : TJSONDataClass; |
| 767 |
Function GetJSONInstanceType(AType : TJSONInstanceType) : TJSONDataClass; |
| 768 |
|
| 769 |
Function StringToJSONString(const S : TJSONStringType; Strict : Boolean = False) : TJSONStringType; |
| 770 |
Function JSONStringToString(const S : TJSONStringType) : TJSONStringType; |
| 771 |
Function JSONTypeName(JSONType : TJSONType) : String; |
| 772 |
|
| 773 |
// These functions create JSONData structures, taking into account the instance types |
| 774 |
Function CreateJSON : TJSONNull; overload; |
| 775 |
Function CreateJSON(Data : Boolean) : TJSONBoolean; overload; |
| 776 |
Function CreateJSON(Data : Integer) : TJSONIntegerNumber; overload; |
| 777 |
{$IFNDEF PAS2JS} |
| 778 |
Function CreateJSON(Data : Int64) : TJSONInt64Number; overload; |
| 779 |
Function CreateJSON(Data : QWord) : TJSONQWordNumber; overload; |
| 780 |
{$ELSE} |
| 781 |
Function CreateJSON(Data : NativeInt) : TJSONNativeIntNumber; overload; |
| 782 |
{$ENDIF} |
| 783 |
Function CreateJSON(Data : TJSONFloat) : TJSONFloatNumber; overload; |
| 784 |
Function CreateJSON(const Data : TJSONStringType) : TJSONString; overload; |
| 785 |
{$IFNDEF PAS2JS} |
| 786 |
Function CreateJSON(const Data : TJSONUnicodeStringType) : TJSONString; overload; |
| 787 |
{$ENDIF} |
| 788 |
Function CreateJSONArray(const Data : Array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}) : TJSONArray; |
| 789 |
Function CreateJSONObject(const Data : Array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}) : TJSONObject; |
| 790 |
|
| 791 |
// These functions rely on a callback. If the callback is not set, they will raise an error. |
| 792 |
// When the jsonparser unit is included in the project, the callback is automatically set. |
| 793 |
{$IFNDEF PAS2JS} |
| 794 |
Function GetJSON(Const JSON : TJSONStringType; Const UseUTF8 : Boolean = True) : TJSONData; overload; |
| 795 |
Function GetJSON(Const JSON : TStream; Const UseUTF8 : Boolean = True) : TJSONData; overload; |
| 796 |
Function SetJSONParserHandler(AHandler : TJSONParserHandler) : TJSONParserHandler; overload; |
| 797 |
Function SetJSONStringParserHandler(AHandler : TJSONStringParserHandler) : TJSONStringParserHandler; overload; |
| 798 |
Function GetJSONParserHandler : TJSONParserHandler; |
| 799 |
Function GetJSONStringParserHandler: TJSONStringParserHandler; overload; |
| 800 |
{$ENDIF} |
| 801 |
|
| 802 |
implementation |
| 803 |
|
| 804 |
Uses typinfo; |
| 805 |
|
| 806 |
Resourcestring |
| 807 |
SErrCannotConvertFromNull = 'Cannot convert data from Null value'; |
| 808 |
SErrCannotConvertToNull = 'Cannot convert data to Null value'; |
| 809 |
SErrCannotConvertFromArray = 'Cannot convert data from array value'; |
| 810 |
SErrCannotConvertToArray = 'Cannot convert data to array value'; |
| 811 |
SErrCannotConvertFromObject = 'Cannot convert data from object value'; |
| 812 |
SErrCannotConvertToObject = 'Cannot convert data to object value'; |
| 813 |
SErrInvalidFloat = 'Invalid float value : %s'; |
| 814 |
SErrCannotSetNotIsNull = 'IsNull cannot be set to False'; |
| 815 |
SErrCannotAddArrayTwice = 'Adding an array object to an array twice is not allowed'; |
| 816 |
SErrCannotAddObjectTwice = 'Adding an object to an array twice is not allowed'; |
| 817 |
SErrUnknownTypeInConstructor = 'Unknown type in JSON%s constructor: %d'; |
| 818 |
SErrNotJSONData = 'Cannot add object of type %s to TJSON%s'; |
| 819 |
SErrOddNumber = 'TJSONObject must be constructed with name,value pairs'; |
| 820 |
SErrNameMustBeString = 'TJSONObject constructor element name at pos %d is not a string'; |
| 821 |
SErrNonexistentElement = 'Unknown object member: "%s"'; |
| 822 |
SErrDuplicateValue = 'Duplicate object member: "%s"'; |
| 823 |
SErrPathElementNotFound = 'Path "%s" invalid: element "%s" not found.'; |
| 824 |
SErrWrongInstanceClass = 'Cannot set instance class: %s does not descend from %s.'; |
| 825 |
{$IFNDEF PAS2JS} |
| 826 |
SErrPointerNotNil = 'Cannot add non-nil pointer to JSON%s'; |
| 827 |
SErrNoParserHandler = 'No JSON parser handler installed. Recompile your project with the jsonparser unit included'; |
| 828 |
{$ENDIF} |
| 829 |
|
| 830 |
Var |
| 831 |
DefaultJSONInstanceTypes : |
| 832 |
Array [TJSONInstanceType] of TJSONDataClass = ( |
| 833 |
TJSONData, |
| 834 |
TJSONIntegerNumber, |
| 835 |
{$IFNDEF PAS2JS} |
| 836 |
TJSONInt64Number, |
| 837 |
TJSONQWordNumber, |
| 838 |
{$ELSE} |
| 839 |
TJSONNativeIntNumber, |
| 840 |
{$ENDIF} |
| 841 |
TJSONFloatNumber, |
| 842 |
TJSONString, |
| 843 |
TJSONBoolean, |
| 844 |
TJSONNull, |
| 845 |
TJSONArray, |
| 846 |
TJSONObject); |
| 847 |
Const |
| 848 |
MinJSONInstanceTypes : |
| 849 |
Array [TJSONInstanceType] of TJSONDataClass = ( |
| 850 |
TJSONData, |
| 851 |
TJSONIntegerNumber, |
| 852 |
{$IFNDEF PAS2JS} |
| 853 |
TJSONInt64Number, |
| 854 |
TJSONQWordNumber, |
| 855 |
{$else} |
| 856 |
TJSONNativeIntNumber, |
| 857 |
{$ENDIF} |
| 858 |
TJSONFloatNumber, |
| 859 |
TJSONString, |
| 860 |
TJSONBoolean, |
| 861 |
TJSONNull, |
| 862 |
TJSONArray, |
| 863 |
TJSONObject |
| 864 |
); |
| 865 |
|
| 866 |
function SetJSONInstanceType(AType: TJSONInstanceType; AClass: TJSONDataClass): TJSONDataClass; |
| 867 |
begin |
| 868 |
if AClass=Nil then |
| 869 |
TJSONData.DoError(SErrWrongInstanceClass,['Nil',MinJSONInstanceTypes[AType].ClassName]); |
| 870 |
if Not AClass.InheritsFrom(MinJSONINstanceTypes[AType]) then |
| 871 |
TJSONData.DoError(SErrWrongInstanceClass,[AClass.ClassName,MinJSONInstanceTypes[AType].ClassName]); |
| 872 |
Result:=DefaultJSONInstanceTypes[AType]; |
| 873 |
DefaultJSONINstanceTypes[AType]:=AClass; |
| 874 |
end; |
| 875 |
|
| 876 |
function GetJSONInstanceType(AType: TJSONInstanceType): TJSONDataClass; |
| 877 |
begin |
| 878 |
Result:=DefaultJSONInstanceTypes[AType] |
| 879 |
end; |
| 880 |
|
| 881 |
function StringToJSONString(const S: TJSONStringType; Strict : Boolean = False): TJSONStringType; |
| 882 |
|
| 883 |
Var |
| 884 |
I,J,L : Integer; |
| 885 |
C : AnsiChar; |
| 886 |
|
| 887 |
begin |
| 888 |
I:=1; |
| 889 |
J:=1; |
| 890 |
Result:=''; |
| 891 |
L:=Length(S); |
| 892 |
While I<=L do |
| 893 |
begin |
| 894 |
C:=S[I]; |
| 895 |
if (C in ['"','/','\',#0..#31]) then |
| 896 |
begin |
| 897 |
Result:=Result+Copy(S,J,I-J); |
| 898 |
Case C of |
| 899 |
'\' : Result:=Result+'\\'; |
| 900 |
'/' : if Strict then |
| 901 |
Result:=Result+'\/' |
| 902 |
else |
| 903 |
Result:=Result+'/'; |
| 904 |
'"' : Result:=Result+'\"'; |
| 905 |
#8 : Result:=Result+'\b'; |
| 906 |
#9 : Result:=Result+'\t'; |
| 907 |
#10 : Result:=Result+'\n'; |
| 908 |
#12 : Result:=Result+'\f'; |
| 909 |
#13 : Result:=Result+'\r'; |
| 910 |
else |
| 911 |
Result:=Result+'\u'+IntToHex(Ord(C),4); |
| 912 |
end; |
| 913 |
J:=I+1; |
| 914 |
end; |
| 915 |
Inc(I); |
| 916 |
end; |
| 917 |
Result:=Result+Copy(S,J,I-1); |
| 918 |
end; |
| 919 |
|
| 920 |
function JSONStringToString(const S: TJSONStringType): TJSONStringType; |
| 921 |
|
| 922 |
{$IFDEF PAS2JS} |
| 923 |
Var |
| 924 |
J : JSValue; |
| 925 |
OK : Boolean; |
| 926 |
begin |
| 927 |
OK:=False; |
| 928 |
try |
| 929 |
J:=TJSJSON.parse('"'+S+'"'); |
| 930 |
if isString(J) then |
| 931 |
begin |
| 932 |
Result:=String(J); |
| 933 |
OK:=True; |
| 934 |
end; |
| 935 |
except |
| 936 |
OK:=False; |
| 937 |
end; |
| 938 |
if not OK then |
| 939 |
Raise EConvertError.Create('Invalid JSON String:'+S); |
| 940 |
end; |
| 941 |
{$ELSE} |
| 942 |
|
| 943 |
function BufferHexToInt(P : PAnsiChar): integer; |
| 944 |
var |
| 945 |
N, i: integer; |
| 946 |
ch: Ansichar; |
| 947 |
begin |
| 948 |
Result:= 0; |
| 949 |
for i:= 1 to 4 do |
| 950 |
begin |
| 951 |
ch:= p^; |
| 952 |
case ch of |
| 953 |
'0'..'9': |
| 954 |
N:= Ord(ch)-Ord('0'); |
| 955 |
'a'..'f': |
| 956 |
N:= Ord(ch)-(Ord('a')-10); |
| 957 |
'A'..'F': |
| 958 |
N:= Ord(ch)-(Ord('A')-10); |
| 959 |
else |
| 960 |
exit(-1); |
| 961 |
end; |
| 962 |
Inc(P); |
| 963 |
Result:= Result*16+N; |
| 964 |
end; |
| 965 |
end; |
| 966 |
|
| 967 |
Var |
| 968 |
|
| 969 |
I,J,L,U1,U2 : Integer; |
| 970 |
App : String; |
| 971 |
|
| 972 |
Procedure MaybeAppendUnicode; |
| 973 |
|
| 974 |
Var |
| 975 |
U : String; |
| 976 |
|
| 977 |
begin |
| 978 |
if (U1<>0) then |
| 979 |
begin |
| 980 |
U:={$IFDEF FPC_HAS_CPSTRING}UTF8Encode(WideChar(U1)){$ELSE}widechar(U1){$ENDIF}; |
| 981 |
Result:=Result+U; |
| 982 |
U1:=0; |
| 983 |
end; |
| 984 |
end; |
| 985 |
|
| 986 |
begin |
| 987 |
I:=1; |
| 988 |
J:=1; |
| 989 |
L:=Length(S); |
| 990 |
Result:=''; |
| 991 |
U1:=0; |
| 992 |
While (I<=L) do |
| 993 |
begin |
| 994 |
if (S[I]='\') then |
| 995 |
begin |
| 996 |
Result:=Result+Copy(S,J,I-J); |
| 997 |
If I<L then |
| 998 |
begin |
| 999 |
Inc(I); |
| 1000 |
App:=''; |
| 1001 |
Case S[I] of |
| 1002 |
'\','"','/' |
| 1003 |
: App:=S[I]; |
| 1004 |
'b' : App:=#8; |
| 1005 |
't' : App:=#9; |
| 1006 |
'n' : App:=#10; |
| 1007 |
'f' : App:=#12; |
| 1008 |
'r' : App:=#13; |
| 1009 |
'u' : begin |
| 1010 |
U2:=BufferHexToInt(PAnsiChar(@S[I+1])); |
| 1011 |
if U2=-1 then |
| 1012 |
Raise EJSON.Create('Invalid unicode hex code: '+Copy(S,I+1,4)); |
| 1013 |
Inc(I,4); |
| 1014 |
if (U1<>0) then |
| 1015 |
begin |
| 1016 |
App:={$IFDEF FPC_HAS_CPSTRING}UTF8Encode({$ENDIF}WideChar(U1)+WideChar(U2){$IFDEF FPC_HAS_CPSTRING}){$ENDIF}; |
| 1017 |
U2:=0; |
| 1018 |
end |
| 1019 |
else |
| 1020 |
U1:=U2; |
| 1021 |
end; |
| 1022 |
end; |
| 1023 |
if App<>'' then |
| 1024 |
begin |
| 1025 |
MaybeAppendUnicode; |
| 1026 |
Result:=Result+App; |
| 1027 |
end; |
| 1028 |
end; |
| 1029 |
J:=I+1; |
| 1030 |
end |
| 1031 |
else |
| 1032 |
MaybeAppendUnicode; |
| 1033 |
Inc(I); |
| 1034 |
end; |
| 1035 |
MaybeAppendUnicode; |
| 1036 |
Result:=Result+Copy(S,J,I-J+1); |
| 1037 |
end; |
| 1038 |
{$ENDIF} |
| 1039 |
|
| 1040 |
function JSONTypeName(JSONType: TJSONType): String; |
| 1041 |
begin |
| 1042 |
Result:=GetEnumName(TypeInfo(TJSONType),Ord(JSONType)); |
| 1043 |
end; |
| 1044 |
|
| 1045 |
function CreateJSON: TJSONNull; |
| 1046 |
begin |
| 1047 |
Result:=TJSONNullClass(DefaultJSONInstanceTypes[jitNull]).Create |
| 1048 |
end; |
| 1049 |
|
| 1050 |
function CreateJSON(Data: Boolean): TJSONBoolean; |
| 1051 |
begin |
| 1052 |
Result:=TJSONBooleanClass(DefaultJSONInstanceTypes[jitBoolean]).Create(Data); |
| 1053 |
end; |
| 1054 |
|
| 1055 |
function CreateJSON(Data: Integer): TJSONIntegerNumber; |
| 1056 |
begin |
| 1057 |
Result:=TJSONIntegerNumberCLass(DefaultJSONInstanceTypes[jitNumberInteger]).Create(Data); |
| 1058 |
end; |
| 1059 |
|
| 1060 |
{$IFNDEF PAS2JS} |
| 1061 |
function CreateJSON(Data: Int64): TJSONInt64Number; |
| 1062 |
begin |
| 1063 |
Result:=TJSONInt64NumberCLass(DefaultJSONInstanceTypes[jitNumberInt64]).Create(Data); |
| 1064 |
end; |
| 1065 |
|
| 1066 |
function CreateJSON(Data: QWord): TJSONQWordNumber; |
| 1067 |
begin |
| 1068 |
Result:=TJSONQWordNumberClass(DefaultJSONInstanceTypes[jitNumberQWord]).Create(Data); |
| 1069 |
end; |
| 1070 |
{$ELSE} |
| 1071 |
function CreateJSON(Data: NativeInt): TJSONNativeIntNumber; |
| 1072 |
begin |
| 1073 |
Result:=TJSONNativeIntNumberCLass(DefaultJSONInstanceTypes[jitNumberNativeInt]).Create(Data); |
| 1074 |
end; |
| 1075 |
{$ENDIF} |
| 1076 |
|
| 1077 |
function CreateJSON(Data: TJSONFloat): TJSONFloatNumber; |
| 1078 |
begin |
| 1079 |
Result:=TJSONFloatNumberCLass(DefaultJSONInstanceTypes[jitNumberFloat]).Create(Data); |
| 1080 |
end; |
| 1081 |
|
| 1082 |
function CreateJSON(const Data: TJSONStringType): TJSONString; |
| 1083 |
begin |
| 1084 |
Result:=TJSONStringCLass(DefaultJSONInstanceTypes[jitString]).Create(Data); |
| 1085 |
end; |
| 1086 |
|
| 1087 |
{$IFNDEF PAS2JS} |
| 1088 |
function CreateJSON(const Data: TJSONUnicodeStringType): TJSONString; |
| 1089 |
begin |
| 1090 |
Result:=TJSONStringCLass(DefaultJSONInstanceTypes[jitString]).Create(Data); |
| 1091 |
end; |
| 1092 |
{$ENDIF} |
| 1093 |
|
| 1094 |
function CreateJSONArray(const Data: array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}): TJSONArray; |
| 1095 |
begin |
| 1096 |
Result:=TJSONArrayCLass(DefaultJSONInstanceTypes[jitArray]).Create(Data); |
| 1097 |
end; |
| 1098 |
|
| 1099 |
function CreateJSONObject(const Data: array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}): TJSONObject; |
| 1100 |
begin |
| 1101 |
Result:=TJSONObjectClass(DefaultJSONInstanceTypes[jitObject]).Create(Data); |
| 1102 |
end; |
| 1103 |
|
| 1104 |
{$IFNDEF PAS2JS} |
| 1105 |
Var |
| 1106 |
JPH : TJSONParserHandler; |
| 1107 |
JPSH : TJSONStringParserHandler; |
| 1108 |
|
| 1109 |
function GetJSON(const JSON: TJSONStringType; const UseUTF8: Boolean): TJSONData; |
| 1110 |
|
| 1111 |
Var |
| 1112 |
SS : TStringStream; |
| 1113 |
begin |
| 1114 |
if Assigned(JPSH) then |
| 1115 |
JPSH(JSON,UseUTF8,Result) |
| 1116 |
else |
| 1117 |
begin |
| 1118 |
///{$IF FPC_FULLVERSION>30300} |
| 1119 |
if UseUTF8 then |
| 1120 |
SS:=TStringStream.Create(JSON, TEncoding.UTF8) |
| 1121 |
else |
| 1122 |
///{$ENDIF} |
| 1123 |
SS:=TStringStream.Create(JSON); |
| 1124 |
try |
| 1125 |
Result:=GetJSON(SS,UseUTF8); |
| 1126 |
finally |
| 1127 |
SS.Free; |
| 1128 |
end; |
| 1129 |
end; |
| 1130 |
end; |
| 1131 |
{$ENDIF} |
| 1132 |
|
| 1133 |
{$IFNDEF PAS2JS} |
| 1134 |
function GetJSON(const JSON: TStream; const UseUTF8: Boolean): TJSONData; |
| 1135 |
|
| 1136 |
Var |
| 1137 |
S : TJSONStringType; |
| 1138 |
|
| 1139 |
begin |
| 1140 |
Result:=Nil; |
| 1141 |
If (Assigned(JPH)) then |
| 1142 |
JPH(JSON,UseUTF8,Result) |
| 1143 |
else if Assigned(JPSH) then |
| 1144 |
TJSONData.DoError(SErrNoParserHandler) |
| 1145 |
else |
| 1146 |
begin |
| 1147 |
S:=''; |
| 1148 |
Setlength(S{%H-},JSON.Size); |
| 1149 |
if Length(S)>0 then |
| 1150 |
JSON.ReadBuffer(S[1],Length(S)); |
| 1151 |
end; |
| 1152 |
end; |
| 1153 |
|
| 1154 |
|
| 1155 |
Function SetJSONStringParserHandler(AHandler : TJSONStringParserHandler) : TJSONStringParserHandler; |
| 1156 |
begin |
| 1157 |
Result:=JPSH; |
| 1158 |
@JPSH:=@AHandler; |
| 1159 |
end; |
| 1160 |
|
| 1161 |
function SetJSONParserHandler(AHandler: TJSONParserHandler): TJSONParserHandler; |
| 1162 |
begin |
| 1163 |
Result:=JPH; |
| 1164 |
@JPH:=@AHandler; |
| 1165 |
end; |
| 1166 |
|
| 1167 |
function GetJSONParserHandler: TJSONParserHandler; |
| 1168 |
begin |
| 1169 |
Result:=@JPH; |
| 1170 |
end; |
| 1171 |
|
| 1172 |
function GetJSONStringParserHandler: TJSONStringParserHandler; |
| 1173 |
begin |
| 1174 |
Result:=@JPSH; |
| 1175 |
end; |
| 1176 |
{$ENDIF} |
| 1177 |
|
| 1178 |
Type |
| 1179 |
{ TJSONEnumerator } |
| 1180 |
|
| 1181 |
TJSONEnumerator = class(TBaseJSONEnumerator) |
| 1182 |
Private |
| 1183 |
FData : TJSONData; |
| 1184 |
public |
| 1185 |
Constructor Create(AData : TJSONData); |
| 1186 |
function GetCurrent: TJSONEnum; override; |
| 1187 |
function MoveNext : Boolean; override; |
| 1188 |
end; |
| 1189 |
|
| 1190 |
{ TJSONArrayEnumerator } |
| 1191 |
|
| 1192 |
TJSONArrayEnumerator = class(TBaseJSONEnumerator) |
| 1193 |
Private |
| 1194 |
FData : TJSONArray; |
| 1195 |
FCurrent : Integer; |
| 1196 |
public |
| 1197 |
Constructor Create(AData : TJSONArray); |
| 1198 |
function GetCurrent: TJSONEnum; override; |
| 1199 |
function MoveNext : Boolean; override; |
| 1200 |
end; |
| 1201 |
|
| 1202 |
{ TJSONObjectEnumerator } |
| 1203 |
|
| 1204 |
TJSONObjectEnumerator = class(TBaseJSONEnumerator) |
| 1205 |
Private |
| 1206 |
FData : TJSONObject; |
| 1207 |
FCurrent : Integer; |
| 1208 |
public |
| 1209 |
Constructor Create(AData : TJSONObject); |
| 1210 |
function GetCurrent: TJSONEnum; override; |
| 1211 |
function MoveNext : Boolean; override; |
| 1212 |
end; |
| 1213 |
|
| 1214 |
{$IFNDEF PAS2JS} |
| 1215 |
{ TJSONQWordNumber } |
| 1216 |
|
| 1217 |
function TJSONQWordNumber.GetAsBoolean: Boolean; |
| 1218 |
begin |
| 1219 |
Result:=FValue<>0; |
| 1220 |
end; |
| 1221 |
|
| 1222 |
function TJSONQWordNumber.GetAsFloat: TJSONFloat; |
| 1223 |
begin |
| 1224 |
Result:= FValue; |
| 1225 |
end; |
| 1226 |
|
| 1227 |
function TJSONQWordNumber.GetAsInteger: Integer; |
| 1228 |
begin |
| 1229 |
Result := FValue; |
| 1230 |
end; |
| 1231 |
|
| 1232 |
function TJSONQWordNumber.GetAsInt64: Int64; |
| 1233 |
begin |
| 1234 |
Result := FValue; |
| 1235 |
end; |
| 1236 |
|
| 1237 |
function TJSONQWordNumber.GetAsQWord: QWord; |
| 1238 |
begin |
| 1239 |
Result := FValue; |
| 1240 |
end; |
| 1241 |
|
| 1242 |
procedure TJSONQWordNumber.SetAsBoolean(const AValue: Boolean); |
| 1243 |
begin |
| 1244 |
FValue:=Ord(AValue); |
| 1245 |
end; |
| 1246 |
|
| 1247 |
procedure TJSONQWordNumber.SetAsFloat(const AValue: TJSONFloat); |
| 1248 |
begin |
| 1249 |
FValue:=Round(AValue); |
| 1250 |
end; |
| 1251 |
|
| 1252 |
procedure TJSONQWordNumber.SetAsInteger(const AValue: Integer); |
| 1253 |
begin |
| 1254 |
FValue:=AValue; |
| 1255 |
end; |
| 1256 |
|
| 1257 |
procedure TJSONQWordNumber.SetAsInt64(const AValue: Int64); |
| 1258 |
begin |
| 1259 |
FValue := AValue; |
| 1260 |
end; |
| 1261 |
|
| 1262 |
procedure TJSONQWordNumber.SetAsQword(const AValue: QWord); |
| 1263 |
begin |
| 1264 |
FValue:=AValue; |
| 1265 |
end; |
| 1266 |
|
| 1267 |
function TJSONQWordNumber.GetAsJSON: TJSONStringType; |
| 1268 |
begin |
| 1269 |
Result:=AsString; |
| 1270 |
end; |
| 1271 |
|
| 1272 |
function TJSONQWordNumber.GetAsString: TJSONStringType; |
| 1273 |
begin |
| 1274 |
Result:=IntToStr(FValue); |
| 1275 |
end; |
| 1276 |
|
| 1277 |
procedure TJSONQWordNumber.SetAsString(const AValue: TJSONStringType); |
| 1278 |
begin |
| 1279 |
FValue:=StrToQWord(AValue); |
| 1280 |
end; |
| 1281 |
|
| 1282 |
function TJSONQWordNumber.GetValue: TJSONVariant; |
| 1283 |
begin |
| 1284 |
Result:=FValue; |
| 1285 |
end; |
| 1286 |
|
| 1287 |
procedure TJSONQWordNumber.SetValue(const AValue: TJSONVariant); |
| 1288 |
begin |
| 1289 |
FValue:=AValue; |
| 1290 |
end; |
| 1291 |
|
| 1292 |
constructor TJSONQWordNumber.Create(AValue: QWord); |
| 1293 |
begin |
| 1294 |
FValue := AValue; |
| 1295 |
end; |
| 1296 |
|
| 1297 |
class function TJSONQWordNumber.NumberType: TJSONNumberType; |
| 1298 |
begin |
| 1299 |
Result:=ntQWord; |
| 1300 |
end; |
| 1301 |
|
| 1302 |
procedure TJSONQWordNumber.Clear; |
| 1303 |
begin |
| 1304 |
FValue:=0; |
| 1305 |
end; |
| 1306 |
|
| 1307 |
function TJSONQWordNumber.Clone: TJSONData; |
| 1308 |
begin |
| 1309 |
Result:=TJSONQWordNumberClass(ClassType).Create(Self.FValue); |
| 1310 |
end; |
| 1311 |
{$ENDIF} |
| 1312 |
|
| 1313 |
{ TJSONObjectEnumerator } |
| 1314 |
|
| 1315 |
constructor TJSONObjectEnumerator.Create(AData: TJSONObject); |
| 1316 |
begin |
| 1317 |
FData:=AData; |
| 1318 |
FCurrent:=-1; |
| 1319 |
end; |
| 1320 |
|
| 1321 |
function TJSONObjectEnumerator.GetCurrent: TJSONEnum; |
| 1322 |
begin |
| 1323 |
Result.KeyNum:=FCurrent; |
| 1324 |
Result.Key:=FData.Names[FCurrent]; |
| 1325 |
Result.Value:=FData.Items[FCurrent]; |
| 1326 |
end; |
| 1327 |
|
| 1328 |
function TJSONObjectEnumerator.MoveNext: Boolean; |
| 1329 |
begin |
| 1330 |
Inc(FCurrent); |
| 1331 |
Result:=FCurrent<FData.Count; |
| 1332 |
end; |
| 1333 |
|
| 1334 |
{ TJSONArrayEnumerator } |
| 1335 |
|
| 1336 |
constructor TJSONArrayEnumerator.Create(AData: TJSONArray); |
| 1337 |
begin |
| 1338 |
FData:=AData; |
| 1339 |
FCurrent:=-1; |
| 1340 |
end; |
| 1341 |
|
| 1342 |
function TJSONArrayEnumerator.GetCurrent: TJSONEnum; |
| 1343 |
begin |
| 1344 |
Result.KeyNum:=FCurrent; |
| 1345 |
Result.Key:=IntToStr(FCurrent); |
| 1346 |
Result.Value:=FData.Items[FCurrent]; |
| 1347 |
end; |
| 1348 |
|
| 1349 |
function TJSONArrayEnumerator.MoveNext: Boolean; |
| 1350 |
begin |
| 1351 |
Inc(FCurrent); |
| 1352 |
Result:=FCurrent<FData.Count; |
| 1353 |
end; |
| 1354 |
|
| 1355 |
{ TJSONEnumerator } |
| 1356 |
|
| 1357 |
constructor TJSONEnumerator.Create(AData: TJSONData); |
| 1358 |
begin |
| 1359 |
FData:=AData; |
| 1360 |
end; |
| 1361 |
|
| 1362 |
function TJSONEnumerator.GetCurrent: TJSONEnum; |
| 1363 |
begin |
| 1364 |
Result.Key:=''; |
| 1365 |
Result.KeyNum:=0; |
| 1366 |
Result.Value:=FData; |
| 1367 |
FData:=Nil; |
| 1368 |
end; |
| 1369 |
|
| 1370 |
function TJSONEnumerator.MoveNext: Boolean; |
| 1371 |
begin |
| 1372 |
Result:=FData<>Nil; |
| 1373 |
end; |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
{ TJSONData } |
| 1378 |
|
| 1379 |
{$IFNDEF PAS2JS} |
| 1380 |
function TJSONData.GetAsUnicodeString: TJSONUnicodeStringType; |
| 1381 |
begin |
| 1382 |
Result:=TJSONUnicodeStringType(AsString); |
| 1383 |
end; |
| 1384 |
|
| 1385 |
procedure TJSONData.SetAsUnicodeString(const AValue: TJSONUnicodeStringType); |
| 1386 |
begin |
| 1387 |
AsString:=TJSONStringType(AValue); |
| 1388 |
end; |
| 1389 |
{$ENDIF} |
| 1390 |
|
| 1391 |
function TJSONData.GetItem(Index : Integer): TJSONData; |
| 1392 |
begin |
| 1393 |
Result:=nil; |
| 1394 |
if Index>0 then ; |
| 1395 |
end; |
| 1396 |
|
| 1397 |
function TJSONData.GetCount: Integer; |
| 1398 |
begin |
| 1399 |
Result:=0; |
| 1400 |
end; |
| 1401 |
|
| 1402 |
constructor TJSONData.Create; |
| 1403 |
begin |
| 1404 |
Clear; |
| 1405 |
end; |
| 1406 |
|
| 1407 |
procedure TJSONData.DumpJSON(S: TFPJSStream); |
| 1408 |
|
| 1409 |
Procedure W(T : String); |
| 1410 |
begin |
| 1411 |
if T='' then exit; |
| 1412 |
{$IFDEF PAS2JS} |
| 1413 |
S.push(T); |
| 1414 |
{$else} |
| 1415 |
S.WriteBuffer(T[1],Length(T)*SizeOf(Char)); |
| 1416 |
{$ENDIF} |
| 1417 |
end; |
| 1418 |
|
| 1419 |
Var |
| 1420 |
I: Integer; |
| 1421 |
O : TJSONObject; |
| 1422 |
|
| 1423 |
begin |
| 1424 |
Case JSONType of |
| 1425 |
jtObject : |
| 1426 |
begin |
| 1427 |
O:=TJSONObject(Self); |
| 1428 |
W('{'); |
| 1429 |
For I:=0 to O.Count-1 do |
| 1430 |
begin |
| 1431 |
if (I>0) then |
| 1432 |
W(','); |
| 1433 |
W('"'); |
| 1434 |
W(StringToJSONString(O.Names[i],False)); |
| 1435 |
W('":'); |
| 1436 |
O.Items[I].DumpJSON(S); |
| 1437 |
end; |
| 1438 |
W('}'); |
| 1439 |
end; |
| 1440 |
jtArray : |
| 1441 |
begin |
| 1442 |
W('['); |
| 1443 |
For I:=0 to Count-1 do |
| 1444 |
begin |
| 1445 |
if (I>0) then |
| 1446 |
W(','); |
| 1447 |
Items[I].DumpJSON(S); |
| 1448 |
end; |
| 1449 |
W(']'); |
| 1450 |
end |
| 1451 |
else |
| 1452 |
W(AsJSON) |
| 1453 |
end; |
| 1454 |
end; |
| 1455 |
|
| 1456 |
class function TJSONData.GetCompressedJSON: Boolean; ///{$IFNDEF PAS2JS}static;{$ENDIF} |
| 1457 |
begin |
| 1458 |
Result:=FCompressedJSON; |
| 1459 |
end; |
| 1460 |
|
| 1461 |
class procedure TJSONData.DetermineElementSeparators; |
| 1462 |
|
| 1463 |
|
| 1464 |
begin |
| 1465 |
FElementSep:=ElementSeps[FCompressedJSON]; |
| 1466 |
end; |
| 1467 |
|
| 1468 |
class procedure TJSONData.SetCompressedJSON(AValue: Boolean); ///{$IFNDEF PAS2JS}static;{$ENDIF} |
| 1469 |
|
| 1470 |
|
| 1471 |
begin |
| 1472 |
if AValue=FCompressedJSON then exit; |
| 1473 |
FCompressedJSON:=AValue; |
| 1474 |
DetermineElementSeparators; |
| 1475 |
TJSONObject.DetermineElementQuotes; |
| 1476 |
end; |
| 1477 |
|
| 1478 |
class procedure TJSONData.DoError(const Msg: String); |
| 1479 |
begin |
| 1480 |
Raise EJSON.Create(Msg); |
| 1481 |
end; |
| 1482 |
|
| 1483 |
class procedure TJSONData.DoError(const Fmt: String; |
| 1484 |
const Args: array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}); |
| 1485 |
begin |
| 1486 |
Raise EJSON.CreateFmt(Fmt,Args); |
| 1487 |
end; |
| 1488 |
|
| 1489 |
function TJSONData.DoFindPath(const APath: TJSONStringType; out |
| 1490 |
NotFound: TJSONStringType): TJSONdata; |
| 1491 |
begin |
| 1492 |
If APath<>'' then |
| 1493 |
begin |
| 1494 |
NotFound:=APath; |
| 1495 |
Result:=Nil; |
| 1496 |
end |
| 1497 |
else |
| 1498 |
Result:=Self; |
| 1499 |
end; |
| 1500 |
|
| 1501 |
function TJSONData.GetIsNull: Boolean; |
| 1502 |
begin |
| 1503 |
Result:=False; |
| 1504 |
end; |
| 1505 |
|
| 1506 |
class function TJSONData.JSONType: TJSONType; |
| 1507 |
begin |
| 1508 |
JSONType:=jtUnknown; |
| 1509 |
end; |
| 1510 |
|
| 1511 |
function TJSONData.GetEnumerator: TBaseJSONEnumerator; |
| 1512 |
begin |
| 1513 |
Result:=TJSONEnumerator.Create(Self); |
| 1514 |
end; |
| 1515 |
|
| 1516 |
function TJSONData.FindPath(const APath: TJSONStringType): TJSONdata; |
| 1517 |
var |
| 1518 |
M: TJSONStringType; |
| 1519 |
begin |
| 1520 |
Result := DoFindPath(APath, M); |
| 1521 |
end; |
| 1522 |
|
| 1523 |
function TJSONData.GetPath(const APath: TJSONStringType): TJSONdata; |
| 1524 |
|
| 1525 |
Var |
| 1526 |
M : TJSONStringType; |
| 1527 |
begin |
| 1528 |
Result:=DoFindPath(APath,M); |
| 1529 |
If Result=Nil then |
| 1530 |
DoError(SErrPathElementNotFound,[APath,M]); |
| 1531 |
end; |
| 1532 |
|
| 1533 |
procedure TJSONData.SetItem(Index : Integer; const AValue: |
| 1534 |
TJSONData); |
| 1535 |
begin |
| 1536 |
// Do Nothing |
| 1537 |
if Index>0 then ; |
| 1538 |
if AValue<>nil then ; |
| 1539 |
end; |
| 1540 |
|
| 1541 |
function TJSONData.FormatJSON(Options: TFormatOptions; Indentsize: Integer |
| 1542 |
): TJSONStringType; |
| 1543 |
|
| 1544 |
begin |
| 1545 |
Result:=DoFormatJSON(Options,0,IndentSize); |
| 1546 |
end; |
| 1547 |
|
| 1548 |
function TJSONData.DoFormatJSON(Options: TFormatOptions; CurrentIndent, |
| 1549 |
Indent: Integer): TJSONStringType; |
| 1550 |
|
| 1551 |
begin |
| 1552 |
Result:=AsJSON; |
| 1553 |
if Options=[] then ; |
| 1554 |
if CurrentIndent=0 then ; |
| 1555 |
if Indent>0 then ; |
| 1556 |
end; |
| 1557 |
|
| 1558 |
{ TJSONnumber } |
| 1559 |
|
| 1560 |
class function TJSONnumber.JSONType: TJSONType; |
| 1561 |
begin |
| 1562 |
Result:=jtNumber; |
| 1563 |
end; |
| 1564 |
|
| 1565 |
|
| 1566 |
{ TJSONstring } |
| 1567 |
|
| 1568 |
class function TJSONString.JSONType: TJSONType; |
| 1569 |
begin |
| 1570 |
Result:=jtString; |
| 1571 |
end; |
| 1572 |
|
| 1573 |
procedure TJSONString.Clear; |
| 1574 |
begin |
| 1575 |
FValue:=''; |
| 1576 |
end; |
| 1577 |
|
| 1578 |
function TJSONString.Clone: TJSONData; |
| 1579 |
|
| 1580 |
begin |
| 1581 |
Result:=TJSONStringClass(ClassType).Create(Self.FValue); |
| 1582 |
end; |
| 1583 |
|
| 1584 |
function TJSONString.GetValue: TJSONVariant; |
| 1585 |
begin |
| 1586 |
Result:=FValue; |
| 1587 |
end; |
| 1588 |
|
| 1589 |
procedure TJSONString.SetValue(const AValue: TJSONVariant); |
| 1590 |
begin |
| 1591 |
FValue:={$IFDEF PAS2JS}TJSONStringType(AValue){$else}AValue{$ENDIF}; |
| 1592 |
end; |
| 1593 |
|
| 1594 |
|
| 1595 |
function TJSONString.GetAsBoolean: Boolean; |
| 1596 |
begin |
| 1597 |
Result:=StrToBool(FValue); |
| 1598 |
end; |
| 1599 |
|
| 1600 |
function TJSONString.GetAsFloat: TJSONFloat; |
| 1601 |
|
| 1602 |
Var |
| 1603 |
C : Integer; |
| 1604 |
|
| 1605 |
begin |
| 1606 |
Val(FValue,Result,C); |
| 1607 |
If (C<>0) then |
| 1608 |
If Not TryStrToFloatDot(FValue,Result) then |
| 1609 |
Raise EConvertError.CreateFmt(SErrInvalidFloat,[FValue]); |
| 1610 |
end; |
| 1611 |
|
| 1612 |
function TJSONString.GetAsInteger: Integer; |
| 1613 |
begin |
| 1614 |
Result:=StrToInt(FValue); |
| 1615 |
end; |
| 1616 |
|
| 1617 |
{$IFNDEF PAS2JS} |
| 1618 |
function TJSONString.GetAsInt64: Int64; |
| 1619 |
begin |
| 1620 |
Result:=StrToInt64(FValue); |
| 1621 |
end; |
| 1622 |
|
| 1623 |
function TJSONString.GetAsQWord: QWord; |
| 1624 |
begin |
| 1625 |
Result:=StrToQWord(FValue); |
| 1626 |
end; |
| 1627 |
|
| 1628 |
procedure TJSONString.SetAsInt64(const AValue: Int64); |
| 1629 |
begin |
| 1630 |
FValue:=IntToStr(AValue); |
| 1631 |
end; |
| 1632 |
|
| 1633 |
procedure TJSONString.SetAsQword(const AValue: QWord); |
| 1634 |
begin |
| 1635 |
FValue:=IntToStr(AValue); |
| 1636 |
end; |
| 1637 |
|
| 1638 |
{$ELSE} |
| 1639 |
|
| 1640 |
function TJSONString.GetAsNativeInt: NativeInt; |
| 1641 |
begin |
| 1642 |
Result:=StrToInt64(FValue); |
| 1643 |
end; |
| 1644 |
|
| 1645 |
procedure TJSONString.SetAsNativeInt(const AValue: NativeInt); |
| 1646 |
|
| 1647 |
begin |
| 1648 |
FValue:=IntToStr(aValue); |
| 1649 |
end; |
| 1650 |
{$ENDIF} |
| 1651 |
|
| 1652 |
procedure TJSONString.SetAsBoolean(const AValue: Boolean); |
| 1653 |
begin |
| 1654 |
FValue:=BoolToStr(AValue); |
| 1655 |
end; |
| 1656 |
|
| 1657 |
procedure TJSONString.SetAsFloat(const AValue: TJSONFloat); |
| 1658 |
begin |
| 1659 |
FValue:=FloatToStrDot(AValue); |
| 1660 |
end; |
| 1661 |
|
| 1662 |
procedure TJSONString.SetAsInteger(const AValue: Integer); |
| 1663 |
begin |
| 1664 |
FValue:=IntToStr(AValue); |
| 1665 |
end; |
| 1666 |
|
| 1667 |
function TJSONString.GetAsJSON: TJSONStringType; |
| 1668 |
begin |
| 1669 |
Result:='"'+StringToJSONString(FValue,StrictEscaping)+'"'; |
| 1670 |
end; |
| 1671 |
|
| 1672 |
function TJSONString.GetAsString: TJSONStringType; |
| 1673 |
begin |
| 1674 |
Result:=FValue; |
| 1675 |
end; |
| 1676 |
|
| 1677 |
procedure TJSONString.SetAsString(const AValue: TJSONStringType); |
| 1678 |
begin |
| 1679 |
FValue:=AValue; |
| 1680 |
end; |
| 1681 |
|
| 1682 |
constructor TJSONString.Create(const AValue: TJSONStringType); |
| 1683 |
begin |
| 1684 |
FValue:=AValue; |
| 1685 |
end; |
| 1686 |
|
| 1687 |
{$IFNDEF PAS2JS} |
| 1688 |
constructor TJSONString.Create(const AValue: TJSONUnicodeStringType); |
| 1689 |
begin |
| 1690 |
FValue:= TJSONStringType(AValue); |
| 1691 |
end; |
| 1692 |
{$ENDIF} |
| 1693 |
|
| 1694 |
{ TJSONboolean } |
| 1695 |
|
| 1696 |
|
| 1697 |
function TJSONBoolean.GetValue: TJSONVariant; |
| 1698 |
begin |
| 1699 |
Result:=FValue; |
| 1700 |
end; |
| 1701 |
|
| 1702 |
class function TJSONBoolean.JSONType: TJSONType; |
| 1703 |
begin |
| 1704 |
Result:=jtBoolean; |
| 1705 |
end; |
| 1706 |
|
| 1707 |
procedure TJSONBoolean.Clear; |
| 1708 |
begin |
| 1709 |
FValue:=False; |
| 1710 |
end; |
| 1711 |
|
| 1712 |
function TJSONBoolean.Clone: TJSONData; |
| 1713 |
begin |
| 1714 |
Result:=TJSONBooleanClass(Self.ClassType).Create(Self.Fvalue); |
| 1715 |
end; |
| 1716 |
|
| 1717 |
|
| 1718 |
procedure TJSONBoolean.SetValue(const AValue: TJSONVariant); |
| 1719 |
begin |
| 1720 |
FValue:=boolean(AValue); |
| 1721 |
end; |
| 1722 |
|
| 1723 |
function TJSONBoolean.GetAsBoolean: Boolean; |
| 1724 |
begin |
| 1725 |
Result:=FValue; |
| 1726 |
end; |
| 1727 |
|
| 1728 |
function TJSONBoolean.GetAsFloat: TJSONFloat; |
| 1729 |
begin |
| 1730 |
Result:=Ord(FValue); |
| 1731 |
end; |
| 1732 |
|
| 1733 |
function TJSONBoolean.GetAsInteger: Integer; |
| 1734 |
begin |
| 1735 |
Result:=Ord(FValue); |
| 1736 |
end; |
| 1737 |
|
| 1738 |
{$IFNDEF PAS2JS} |
| 1739 |
function TJSONBoolean.GetAsInt64: Int64; |
| 1740 |
begin |
| 1741 |
Result:=Ord(FValue); |
| 1742 |
end; |
| 1743 |
|
| 1744 |
function TJSONBoolean.GetAsQWord: QWord; |
| 1745 |
begin |
| 1746 |
Result:=Ord(FValue); |
| 1747 |
end; |
| 1748 |
|
| 1749 |
procedure TJSONBoolean.SetAsInt64(const AValue: Int64); |
| 1750 |
begin |
| 1751 |
FValue:=(AValue<>0) |
| 1752 |
end; |
| 1753 |
|
| 1754 |
procedure TJSONBoolean.SetAsQword(const AValue: QWord); |
| 1755 |
begin |
| 1756 |
FValue:=(AValue<>0) |
| 1757 |
end; |
| 1758 |
|
| 1759 |
{$ELSE} |
| 1760 |
|
| 1761 |
function TJSONBoolean.GetAsNativeInt: NativeInt; |
| 1762 |
begin |
| 1763 |
Result:=Ord(FValue); |
| 1764 |
end; |
| 1765 |
|
| 1766 |
procedure TJSONBoolean.SetAsNativeInt(const AValue: NativeInt); |
| 1767 |
begin |
| 1768 |
FValue:=aValue<>0; |
| 1769 |
end; |
| 1770 |
{$ENDIF} |
| 1771 |
|
| 1772 |
procedure TJSONBoolean.SetAsBoolean(const AValue: Boolean); |
| 1773 |
begin |
| 1774 |
FValue:=AValue; |
| 1775 |
end; |
| 1776 |
|
| 1777 |
procedure TJSONBoolean.SetAsFloat(const AValue: TJSONFloat); |
| 1778 |
begin |
| 1779 |
FValue:=(AValue<>0) |
| 1780 |
end; |
| 1781 |
|
| 1782 |
procedure TJSONBoolean.SetAsInteger(const AValue: Integer); |
| 1783 |
begin |
| 1784 |
FValue:=(AValue<>0) |
| 1785 |
end; |
| 1786 |
|
| 1787 |
function TJSONBoolean.GetAsJSON: TJSONStringType; |
| 1788 |
begin |
| 1789 |
If FValue then |
| 1790 |
Result:='true' |
| 1791 |
else |
| 1792 |
Result:='false'; |
| 1793 |
end; |
| 1794 |
|
| 1795 |
function TJSONBoolean.GetAsString: TJSONStringType; |
| 1796 |
begin |
| 1797 |
Result:=BoolToStr(FValue, True); |
| 1798 |
end; |
| 1799 |
|
| 1800 |
procedure TJSONBoolean.SetAsString(const AValue: TJSONStringType); |
| 1801 |
begin |
| 1802 |
FValue:=StrToBool(AValue); |
| 1803 |
end; |
| 1804 |
|
| 1805 |
|
| 1806 |
constructor TJSONBoolean.Create(AValue: Boolean); |
| 1807 |
begin |
| 1808 |
FValue:=AValue; |
| 1809 |
end; |
| 1810 |
|
| 1811 |
{ TJSONnull } |
| 1812 |
|
| 1813 |
procedure TJSONNull.Converterror(From: Boolean); |
| 1814 |
begin |
| 1815 |
If From then |
| 1816 |
DoError(SErrCannotConvertFromNull) |
| 1817 |
else |
| 1818 |
DoError(SErrCannotConvertToNull); |
| 1819 |
end; |
| 1820 |
|
| 1821 |
{$warnings off} |
| 1822 |
function TJSONNull.GetAsBoolean: Boolean; |
| 1823 |
begin |
| 1824 |
ConvertError(True); |
| 1825 |
Result:=false; |
| 1826 |
end; |
| 1827 |
|
| 1828 |
function TJSONNull.GetAsFloat: TJSONFloat; |
| 1829 |
begin |
| 1830 |
ConvertError(True); |
| 1831 |
Result:=0.0; |
| 1832 |
end; |
| 1833 |
|
| 1834 |
function TJSONNull.GetAsInteger: Integer; |
| 1835 |
begin |
| 1836 |
ConvertError(True); |
| 1837 |
Result:=0; |
| 1838 |
end; |
| 1839 |
|
| 1840 |
|
| 1841 |
{$IFNDEF PAS2JS} |
| 1842 |
function TJSONNull.GetAsInt64: Int64; |
| 1843 |
begin |
| 1844 |
ConvertError(True); |
| 1845 |
end; |
| 1846 |
|
| 1847 |
function TJSONNull.GetAsQWord: QWord; |
| 1848 |
begin |
| 1849 |
ConvertError(True); |
| 1850 |
end; |
| 1851 |
|
| 1852 |
procedure TJSONNull.SetAsInt64(const AValue: Int64); |
| 1853 |
begin |
| 1854 |
ConvertError(False); |
| 1855 |
if AValue>0 then ; |
| 1856 |
end; |
| 1857 |
|
| 1858 |
procedure TJSONNull.SetAsQword(const AValue: QWord); |
| 1859 |
begin |
| 1860 |
ConvertError(False); |
| 1861 |
if AValue>0 then ; |
| 1862 |
end; |
| 1863 |
|
| 1864 |
{$ELSE} |
| 1865 |
function TJSONNull.GetAsNativeInt: NativeInt; |
| 1866 |
begin |
| 1867 |
ConvertError(True); |
| 1868 |
Result:=0; |
| 1869 |
end; |
| 1870 |
|
| 1871 |
procedure TJSONNull.SetAsNativeInt(const AValue: NativeInt); |
| 1872 |
begin |
| 1873 |
ConvertError(False); |
| 1874 |
if AValue<>0 then ; |
| 1875 |
end; |
| 1876 |
{$ENDIF} |
| 1877 |
|
| 1878 |
function TJSONNull.GetIsNull: Boolean; |
| 1879 |
begin |
| 1880 |
Result:=True; |
| 1881 |
end; |
| 1882 |
|
| 1883 |
procedure TJSONNull.SetAsBoolean(const AValue: Boolean); |
| 1884 |
begin |
| 1885 |
ConvertError(False); |
| 1886 |
if AValue then ; |
| 1887 |
end; |
| 1888 |
|
| 1889 |
procedure TJSONNull.SetAsFloat(const AValue: TJSONFloat); |
| 1890 |
begin |
| 1891 |
ConvertError(False); |
| 1892 |
if AValue>0 then ; |
| 1893 |
end; |
| 1894 |
|
| 1895 |
procedure TJSONNull.SetAsInteger(const AValue: Integer); |
| 1896 |
begin |
| 1897 |
ConvertError(False); |
| 1898 |
if AValue>0 then ; |
| 1899 |
end; |
| 1900 |
|
| 1901 |
function TJSONNull.GetAsJSON: TJSONStringType; |
| 1902 |
begin |
| 1903 |
Result:='null'; |
| 1904 |
end; |
| 1905 |
|
| 1906 |
function TJSONNull.GetAsString: TJSONStringType; |
| 1907 |
begin |
| 1908 |
ConvertError(True); |
| 1909 |
Result:=''; |
| 1910 |
end; |
| 1911 |
|
| 1912 |
procedure TJSONNull.SetAsString(const AValue: TJSONStringType); |
| 1913 |
begin |
| 1914 |
ConvertError(True); |
| 1915 |
if AValue='' then ; |
| 1916 |
end; |
| 1917 |
|
| 1918 |
|
| 1919 |
function TJSONNull.GetValue: TJSONVariant; |
| 1920 |
begin |
| 1921 |
Result:={$IFDEF PAS2JS}js.Null{$else}variants.Null{$ENDIF}; |
| 1922 |
end; |
| 1923 |
|
| 1924 |
procedure TJSONNull.SetValue(const AValue: TJSONVariant); |
| 1925 |
begin |
| 1926 |
ConvertError(False); |
| 1927 |
{$IFDEF PAS2JS} |
| 1928 |
if AValue=0 then ; |
| 1929 |
{$else} |
| 1930 |
if VarType(AValue)=0 then ; |
| 1931 |
{$ENDIF} |
| 1932 |
end; |
| 1933 |
|
| 1934 |
class function TJSONNull.JSONType: TJSONType; |
| 1935 |
begin |
| 1936 |
Result:=jtNull; |
| 1937 |
end; |
| 1938 |
|
| 1939 |
procedure TJSONNull.Clear; |
| 1940 |
begin |
| 1941 |
// Do nothing |
| 1942 |
end; |
| 1943 |
|
| 1944 |
function TJSONNull.Clone: TJSONData; |
| 1945 |
begin |
| 1946 |
Result:=TJSONNullClass(Self.ClassType).Create; |
| 1947 |
end; |
| 1948 |
|
| 1949 |
{$warnings on} |
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
{ TJSONFloatNumber } |
| 1954 |
|
| 1955 |
function TJSONFloatNumber.GetAsBoolean: Boolean; |
| 1956 |
begin |
| 1957 |
Result:=(FValue<>0); |
| 1958 |
end; |
| 1959 |
|
| 1960 |
function TJSONFloatNumber.GetAsFloat: TJSONFloat; |
| 1961 |
begin |
| 1962 |
Result:=FValue; |
| 1963 |
end; |
| 1964 |
|
| 1965 |
function TJSONFloatNumber.GetAsInteger: Integer; |
| 1966 |
begin |
| 1967 |
Result:=Round(FValue); |
| 1968 |
end; |
| 1969 |
|
| 1970 |
{$IFNDEF PAS2JS} |
| 1971 |
function TJSONFloatNumber.GetAsInt64: Int64; |
| 1972 |
begin |
| 1973 |
Result:=Round(FValue); |
| 1974 |
end; |
| 1975 |
|
| 1976 |
function TJSONFloatNumber.GetAsQWord: QWord; |
| 1977 |
begin |
| 1978 |
Result:=Round(FValue); |
| 1979 |
end; |
| 1980 |
|
| 1981 |
procedure TJSONFloatNumber.SetAsInt64(const AValue: Int64); |
| 1982 |
begin |
| 1983 |
FValue:=AValue; |
| 1984 |
end; |
| 1985 |
|
| 1986 |
procedure TJSONFloatNumber.SetAsQword(const AValue: QWord); |
| 1987 |
begin |
| 1988 |
FValue:=AValue; |
| 1989 |
end; |
| 1990 |
|
| 1991 |
{$ELSE} |
| 1992 |
|
| 1993 |
function TJSONFloatNumber.GetAsNativeInt: NativeInt; |
| 1994 |
|
| 1995 |
begin |
| 1996 |
Result:=Round(FValue); |
| 1997 |
end; |
| 1998 |
|
| 1999 |
procedure TJSONFloatNumber.SetAsNativeInt(const AValue: NativeInt); |
| 2000 |
|
| 2001 |
begin |
| 2002 |
FValue:=aValue; |
| 2003 |
end; |
| 2004 |
|
| 2005 |
{$ENDIF} |
| 2006 |
|
| 2007 |
procedure TJSONFloatNumber.SetAsBoolean(const AValue: Boolean); |
| 2008 |
begin |
| 2009 |
FValue:=Ord(AValue); |
| 2010 |
end; |
| 2011 |
|
| 2012 |
procedure TJSONFloatNumber.SetAsFloat(const AValue: TJSONFloat); |
| 2013 |
begin |
| 2014 |
FValue:=AValue; |
| 2015 |
end; |
| 2016 |
|
| 2017 |
procedure TJSONFloatNumber.SetAsInteger(const AValue: Integer); |
| 2018 |
begin |
| 2019 |
FValue:=AValue; |
| 2020 |
end; |
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
function TJSONFloatNumber.GetAsJSON: TJSONStringType; |
| 2025 |
begin |
| 2026 |
Result:=AsString; |
| 2027 |
end; |
| 2028 |
|
| 2029 |
function TJSONFloatNumber.GetAsString: TJSONStringType; |
| 2030 |
begin |
| 2031 |
Str(FValue,Result); |
| 2032 |
// Str produces a ' ' in front where the - can go. |
| 2033 |
if (Result<>'') and (Result[1]=' ') then |
| 2034 |
Delete(Result,1,1); |
| 2035 |
end; |
| 2036 |
|
| 2037 |
procedure TJSONFloatNumber.SetAsString(const AValue: TJSONStringType); |
| 2038 |
Var |
| 2039 |
C : Integer; |
| 2040 |
begin |
| 2041 |
Val(AValue,FValue,C); |
| 2042 |
If (C<>0) then |
| 2043 |
Raise EConvertError.CreateFmt(SErrInvalidFloat,[AValue]); |
| 2044 |
end; |
| 2045 |
|
| 2046 |
|
| 2047 |
function TJSONFloatNumber.GetValue: TJSONVariant; |
| 2048 |
begin |
| 2049 |
Result:=FValue; |
| 2050 |
end; |
| 2051 |
|
| 2052 |
procedure TJSONFloatNumber.SetValue(const AValue: TJSONVariant); |
| 2053 |
begin |
| 2054 |
FValue:={$IFDEF PAS2JS}TJSONFloat(AValue){$else}AValue{$ENDIF}; |
| 2055 |
end; |
| 2056 |
|
| 2057 |
constructor TJSONFloatNumber.Create(AValue: TJSONFloat); |
| 2058 |
begin |
| 2059 |
FValue:=AValue; |
| 2060 |
end; |
| 2061 |
|
| 2062 |
class function TJSONFloatNumber.NumberType: TJSONNumberType; |
| 2063 |
begin |
| 2064 |
Result:=ntFloat; |
| 2065 |
end; |
| 2066 |
|
| 2067 |
procedure TJSONFloatNumber.Clear; |
| 2068 |
begin |
| 2069 |
FValue:=0; |
| 2070 |
end; |
| 2071 |
|
| 2072 |
function TJSONFloatNumber.Clone: TJSONData; |
| 2073 |
|
| 2074 |
begin |
| 2075 |
Result:=TJSONFloatNumberClass(ClassType).Create(Self.FValue); |
| 2076 |
end; |
| 2077 |
|
| 2078 |
{ TJSONIntegerNumber } |
| 2079 |
|
| 2080 |
function TJSONIntegerNumber.GetAsBoolean: Boolean; |
| 2081 |
begin |
| 2082 |
Result:=FValue<>0; |
| 2083 |
end; |
| 2084 |
|
| 2085 |
function TJSONIntegerNumber.GetAsFloat: TJSONFloat; |
| 2086 |
begin |
| 2087 |
Result:=FValue; |
| 2088 |
end; |
| 2089 |
|
| 2090 |
function TJSONIntegerNumber.GetAsInteger: Integer; |
| 2091 |
begin |
| 2092 |
Result:=FValue; |
| 2093 |
end; |
| 2094 |
|
| 2095 |
{$IFNDEF PAS2JS} |
| 2096 |
function TJSONIntegerNumber.GetAsInt64: Int64; |
| 2097 |
begin |
| 2098 |
Result:=FValue; |
| 2099 |
end; |
| 2100 |
|
| 2101 |
function TJSONIntegerNumber.GetAsQWord: QWord; |
| 2102 |
begin |
| 2103 |
result:=FValue; |
| 2104 |
end; |
| 2105 |
|
| 2106 |
procedure TJSONIntegerNumber.SetAsInt64(const AValue: Int64); |
| 2107 |
begin |
| 2108 |
FValue:=AValue; |
| 2109 |
end; |
| 2110 |
|
| 2111 |
procedure TJSONIntegerNumber.SetAsQword(const AValue: QWord); |
| 2112 |
begin |
| 2113 |
FValue:=AValue; |
| 2114 |
end; |
| 2115 |
|
| 2116 |
{$ELSE} |
| 2117 |
function TJSONIntegerNumber.GetAsNativeInt: NativeInt; |
| 2118 |
begin |
| 2119 |
result:=FValue; |
| 2120 |
end; |
| 2121 |
|
| 2122 |
procedure TJSONIntegerNumber.SetAsNativeInt(const AValue: NativeInt); |
| 2123 |
|
| 2124 |
begin |
| 2125 |
FValue:=aValue; |
| 2126 |
end; |
| 2127 |
{$ENDIF} |
| 2128 |
|
| 2129 |
procedure TJSONIntegerNumber.SetAsBoolean(const AValue: Boolean); |
| 2130 |
begin |
| 2131 |
FValue:=Ord(AValue); |
| 2132 |
end; |
| 2133 |
|
| 2134 |
procedure TJSONIntegerNumber.SetAsFloat(const AValue: TJSONFloat); |
| 2135 |
begin |
| 2136 |
FValue:=Round(AValue); |
| 2137 |
end; |
| 2138 |
|
| 2139 |
procedure TJSONIntegerNumber.SetAsInteger(const AValue: Integer); |
| 2140 |
begin |
| 2141 |
FValue:=AValue; |
| 2142 |
end; |
| 2143 |
|
| 2144 |
|
| 2145 |
function TJSONIntegerNumber.GetAsJSON: TJSONStringType; |
| 2146 |
begin |
| 2147 |
Result:=AsString; |
| 2148 |
end; |
| 2149 |
|
| 2150 |
function TJSONIntegerNumber.GetAsString: TJSONStringType; |
| 2151 |
begin |
| 2152 |
Result:=IntToStr(FValue) |
| 2153 |
end; |
| 2154 |
|
| 2155 |
procedure TJSONIntegerNumber.SetAsString(const AValue: TJSONStringType); |
| 2156 |
begin |
| 2157 |
FValue:=StrToInt(AValue); |
| 2158 |
end; |
| 2159 |
|
| 2160 |
|
| 2161 |
function TJSONIntegerNumber.GetValue: TJSONVariant; |
| 2162 |
begin |
| 2163 |
Result:=FValue; |
| 2164 |
end; |
| 2165 |
|
| 2166 |
procedure TJSONIntegerNumber.SetValue(const AValue: TJSONVariant); |
| 2167 |
begin |
| 2168 |
FValue:={$IFDEF PAS2JS}Integer(AValue){$else}AValue{$ENDIF}; |
| 2169 |
end; |
| 2170 |
|
| 2171 |
constructor TJSONIntegerNumber.Create(AValue: Integer); |
| 2172 |
begin |
| 2173 |
FValue:=AValue; |
| 2174 |
end; |
| 2175 |
|
| 2176 |
class function TJSONIntegerNumber.NumberType: TJSONNumberType; |
| 2177 |
begin |
| 2178 |
Result:=ntInteger; |
| 2179 |
end; |
| 2180 |
|
| 2181 |
procedure TJSONIntegerNumber.Clear; |
| 2182 |
begin |
| 2183 |
FValue:=0; |
| 2184 |
end; |
| 2185 |
|
| 2186 |
function TJSONIntegerNumber.Clone: TJSONData; |
| 2187 |
|
| 2188 |
begin |
| 2189 |
Result:=TJSONIntegerNumberClass(ClassType).Create(Self.FValue); |
| 2190 |
end; |
| 2191 |
|
| 2192 |
{$IFNDEF PAS2JS} |
| 2193 |
{ TJSONInt64Number } |
| 2194 |
|
| 2195 |
function TJSONInt64Number.GetAsInt64: Int64; |
| 2196 |
begin |
| 2197 |
Result := FValue; |
| 2198 |
end; |
| 2199 |
|
| 2200 |
function TJSONInt64Number.GetAsQWord: QWord; |
| 2201 |
begin |
| 2202 |
Result := FValue; |
| 2203 |
end; |
| 2204 |
|
| 2205 |
procedure TJSONInt64Number.SetAsInt64(const AValue: Int64); |
| 2206 |
begin |
| 2207 |
FValue := AValue; |
| 2208 |
end; |
| 2209 |
|
| 2210 |
procedure TJSONInt64Number.SetAsQword(const AValue: QWord); |
| 2211 |
begin |
| 2212 |
FValue := AValue; |
| 2213 |
end; |
| 2214 |
|
| 2215 |
function TJSONInt64Number.GetAsBoolean: Boolean; |
| 2216 |
begin |
| 2217 |
Result:=FValue<>0; |
| 2218 |
end; |
| 2219 |
|
| 2220 |
function TJSONInt64Number.GetAsFloat: TJSONFloat; |
| 2221 |
begin |
| 2222 |
Result:= FValue; |
| 2223 |
end; |
| 2224 |
|
| 2225 |
function TJSONInt64Number.GetAsInteger: Integer; |
| 2226 |
begin |
| 2227 |
Result := FValue; |
| 2228 |
end; |
| 2229 |
|
| 2230 |
procedure TJSONInt64Number.SetAsBoolean(const AValue: Boolean); |
| 2231 |
begin |
| 2232 |
FValue:=Ord(AValue); |
| 2233 |
end; |
| 2234 |
|
| 2235 |
procedure TJSONInt64Number.SetAsFloat(const AValue: TJSONFloat); |
| 2236 |
begin |
| 2237 |
FValue:=Round(AValue); |
| 2238 |
end; |
| 2239 |
|
| 2240 |
procedure TJSONInt64Number.SetAsInteger(const AValue: Integer); |
| 2241 |
begin |
| 2242 |
FValue:=AValue; |
| 2243 |
end; |
| 2244 |
|
| 2245 |
function TJSONInt64Number.GetAsJSON: TJSONStringType; |
| 2246 |
begin |
| 2247 |
Result:=AsString; |
| 2248 |
end; |
| 2249 |
|
| 2250 |
function TJSONInt64Number.GetAsString: TJSONStringType; |
| 2251 |
begin |
| 2252 |
Result:=IntToStr(FValue) |
| 2253 |
end; |
| 2254 |
|
| 2255 |
procedure TJSONInt64Number.SetAsString(const AValue: TJSONStringType); |
| 2256 |
begin |
| 2257 |
FValue:=StrToInt64(AValue); |
| 2258 |
end; |
| 2259 |
|
| 2260 |
function TJSONInt64Number.GetValue: TJSONVariant; |
| 2261 |
begin |
| 2262 |
Result:=FValue; |
| 2263 |
end; |
| 2264 |
|
| 2265 |
procedure TJSONInt64Number.SetValue(const AValue: TJSONVariant); |
| 2266 |
begin |
| 2267 |
FValue:=AValue; |
| 2268 |
end; |
| 2269 |
|
| 2270 |
constructor TJSONInt64Number.Create(AValue: Int64); |
| 2271 |
begin |
| 2272 |
FValue := AValue; |
| 2273 |
end; |
| 2274 |
|
| 2275 |
class function TJSONInt64Number.NumberType: TJSONNumberType; |
| 2276 |
begin |
| 2277 |
Result:=ntInt64; |
| 2278 |
end; |
| 2279 |
|
| 2280 |
procedure TJSONInt64Number.Clear; |
| 2281 |
begin |
| 2282 |
FValue:=0; |
| 2283 |
end; |
| 2284 |
|
| 2285 |
function TJSONInt64Number.Clone: TJSONData; |
| 2286 |
|
| 2287 |
begin |
| 2288 |
Result:=TJSONInt64NumberClass(ClassType).Create(Self.FValue); |
| 2289 |
end; |
| 2290 |
{$else} |
| 2291 |
{ TJSONNativeIntNumber } |
| 2292 |
|
| 2293 |
function TJSONNativeIntNumber.GetAsNativeInt: NativeInt; |
| 2294 |
begin |
| 2295 |
Result := FValue; |
| 2296 |
end; |
| 2297 |
|
| 2298 |
|
| 2299 |
procedure TJSONNativeIntNumber.SetAsNativeInt(const AValue: NativeInt); |
| 2300 |
begin |
| 2301 |
FValue := AValue; |
| 2302 |
end; |
| 2303 |
|
| 2304 |
function TJSONNativeIntNumber.GetAsBoolean: Boolean; |
| 2305 |
begin |
| 2306 |
Result:=FValue<>0; |
| 2307 |
end; |
| 2308 |
|
| 2309 |
function TJSONNativeIntNumber.GetAsFloat: TJSONFloat; |
| 2310 |
begin |
| 2311 |
Result:= FValue; |
| 2312 |
end; |
| 2313 |
|
| 2314 |
function TJSONNativeIntNumber.GetAsInteger: Integer; |
| 2315 |
begin |
| 2316 |
Result := FValue; |
| 2317 |
end; |
| 2318 |
|
| 2319 |
procedure TJSONNativeIntNumber.SetAsBoolean(const AValue: Boolean); |
| 2320 |
begin |
| 2321 |
FValue:=Ord(AValue); |
| 2322 |
end; |
| 2323 |
|
| 2324 |
procedure TJSONNativeIntNumber.SetAsFloat(const AValue: TJSONFloat); |
| 2325 |
begin |
| 2326 |
FValue:=Round(AValue); |
| 2327 |
end; |
| 2328 |
|
| 2329 |
procedure TJSONNativeIntNumber.SetAsInteger(const AValue: Integer); |
| 2330 |
begin |
| 2331 |
FValue:=AValue; |
| 2332 |
end; |
| 2333 |
|
| 2334 |
function TJSONNativeIntNumber.GetAsJSON: TJSONStringType; |
| 2335 |
begin |
| 2336 |
Result:=AsString; |
| 2337 |
end; |
| 2338 |
|
| 2339 |
function TJSONNativeIntNumber.GetAsString: TJSONStringType; |
| 2340 |
begin |
| 2341 |
Result:=IntToStr(FValue) |
| 2342 |
end; |
| 2343 |
|
| 2344 |
procedure TJSONNativeIntNumber.SetAsString(const AValue: TJSONStringType); |
| 2345 |
begin |
| 2346 |
FValue:=StrToNativeInt(AValue); |
| 2347 |
end; |
| 2348 |
|
| 2349 |
function TJSONNativeIntNumber.GetValue: TJSONVariant; |
| 2350 |
begin |
| 2351 |
Result:=FValue; |
| 2352 |
end; |
| 2353 |
|
| 2354 |
procedure TJSONNativeIntNumber.SetValue(const AValue: TJSONVariant); |
| 2355 |
begin |
| 2356 |
FValue:=NativeInt(AValue); |
| 2357 |
end; |
| 2358 |
|
| 2359 |
constructor TJSONNativeIntNumber.Create(AValue: NativeInt); |
| 2360 |
begin |
| 2361 |
FValue := AValue; |
| 2362 |
end; |
| 2363 |
|
| 2364 |
class function TJSONNativeIntNumber.NumberType: TJSONNumberType; |
| 2365 |
begin |
| 2366 |
Result:=ntNativeInt; |
| 2367 |
end; |
| 2368 |
|
| 2369 |
procedure TJSONNativeIntNumber.Clear; |
| 2370 |
begin |
| 2371 |
FValue:=0; |
| 2372 |
end; |
| 2373 |
|
| 2374 |
function TJSONNativeIntNumber.Clone: TJSONData; |
| 2375 |
|
| 2376 |
begin |
| 2377 |
Result:=TJSONNativeIntNumberClass(ClassType).Create(Self.FValue); |
| 2378 |
end; |
| 2379 |
{$ENDIF} |
| 2380 |
|
| 2381 |
{ TJSONArray } |
| 2382 |
|
| 2383 |
function TJSONArray.GetBooleans(Index : Integer): Boolean; |
| 2384 |
begin |
| 2385 |
Result:=Items[Index].AsBoolean; |
| 2386 |
end; |
| 2387 |
|
| 2388 |
function TJSONArray.GetArrays(Index : Integer): TJSONArray; |
| 2389 |
begin |
| 2390 |
Result:=Items[Index] as TJSONArray; |
| 2391 |
end; |
| 2392 |
|
| 2393 |
function TJSONArray.GetFloats(Index : Integer): TJSONFloat; |
| 2394 |
begin |
| 2395 |
Result:=Items[Index].AsFloat; |
| 2396 |
end; |
| 2397 |
|
| 2398 |
function TJSONArray.GetIntegers(Index : Integer): Integer; |
| 2399 |
begin |
| 2400 |
Result:=Items[Index].AsInteger; |
| 2401 |
end; |
| 2402 |
|
| 2403 |
|
| 2404 |
{$IFNDEF PAS2JS} |
| 2405 |
function TJSONArray.GetInt64s(Index : Integer): Int64; |
| 2406 |
begin |
| 2407 |
Result:=Items[Index].AsInt64; |
| 2408 |
end; |
| 2409 |
{$ELSE} |
| 2410 |
function TJSONArray.GetNativeInts(Index : Integer): NativeInt; |
| 2411 |
begin |
| 2412 |
Result:=Items[Index].AsNativeInt; |
| 2413 |
end; |
| 2414 |
{$ENDIF} |
| 2415 |
|
| 2416 |
function TJSONArray.GetNulls(Index : Integer): Boolean; |
| 2417 |
begin |
| 2418 |
Result:=Items[Index].IsNull; |
| 2419 |
end; |
| 2420 |
|
| 2421 |
function TJSONArray.GetObjects(Index : Integer): TJSONObject; |
| 2422 |
begin |
| 2423 |
Result:=Items[Index] as TJSONObject; |
| 2424 |
end; |
| 2425 |
|
| 2426 |
{$IFNDEF PAS2JS} |
| 2427 |
function TJSONArray.GetQWords(Index : Integer): QWord; |
| 2428 |
begin |
| 2429 |
Result:=Items[Index].AsQWord; |
| 2430 |
end; |
| 2431 |
{$ENDIF} |
| 2432 |
|
| 2433 |
function TJSONArray.GetStrings(Index : Integer): TJSONStringType; |
| 2434 |
begin |
| 2435 |
Result:=Items[Index].AsString; |
| 2436 |
end; |
| 2437 |
|
| 2438 |
{$IFNDEF PAS2JS} |
| 2439 |
function TJSONArray.GetUnicodeStrings(Index : Integer): TJSONUnicodeStringType; |
| 2440 |
begin |
| 2441 |
Result:=Items[Index].AsUnicodeString; |
| 2442 |
end; |
| 2443 |
{$ENDIF} |
| 2444 |
|
| 2445 |
function TJSONArray.GetTypes(Index : Integer): TJSONType; |
| 2446 |
begin |
| 2447 |
Result:=Items[Index].JSONType; |
| 2448 |
end; |
| 2449 |
|
| 2450 |
procedure TJSONArray.SetArrays(Index : Integer; const AValue: TJSONArray); |
| 2451 |
begin |
| 2452 |
Items[Index]:=AValue; |
| 2453 |
end; |
| 2454 |
|
| 2455 |
procedure TJSONArray.SetBooleans(Index : Integer; const AValue: Boolean); |
| 2456 |
|
| 2457 |
begin |
| 2458 |
Items[Index]:=CreateJSON(AValue); |
| 2459 |
end; |
| 2460 |
|
| 2461 |
procedure TJSONArray.SetFloats(Index : Integer; const AValue: TJSONFloat); |
| 2462 |
begin |
| 2463 |
Items[Index]:=CreateJSON(AValue); |
| 2464 |
end; |
| 2465 |
|
| 2466 |
procedure TJSONArray.SetIntegers(Index : Integer; const AValue: Integer); |
| 2467 |
begin |
| 2468 |
Items[Index]:=CreateJSON(AValue); |
| 2469 |
end; |
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
{$IFNDEF PAS2JS} |
| 2474 |
procedure TJSONArray.SetInt64s(Index : Integer; const AValue: Int64); |
| 2475 |
begin |
| 2476 |
Items[Index]:=CreateJSON(AValue); |
| 2477 |
end; |
| 2478 |
{$ELSE} |
| 2479 |
procedure TJSONArray.SetNativeInts(Index : Integer; AValue: NativeInt); |
| 2480 |
begin |
| 2481 |
Items[Index]:=CreateJSON(AValue); |
| 2482 |
end; |
| 2483 |
{$ENDIF} |
| 2484 |
|
| 2485 |
procedure TJSONArray.SetObjects(Index : Integer; const AValue: TJSONObject); |
| 2486 |
begin |
| 2487 |
Items[Index]:=AValue; |
| 2488 |
end; |
| 2489 |
|
| 2490 |
{$IFNDEF PAS2JS} |
| 2491 |
procedure TJSONArray.SetQWords(Index : Integer; AValue: QWord); |
| 2492 |
begin |
| 2493 |
Items[Index]:=CreateJSON(AValue); |
| 2494 |
end; |
| 2495 |
{$ENDIF} |
| 2496 |
|
| 2497 |
procedure TJSONArray.SetStrings(Index : Integer; const AValue: TJSONStringType); |
| 2498 |
begin |
| 2499 |
Items[Index]:=CreateJSON(AValue); |
| 2500 |
end; |
| 2501 |
|
| 2502 |
{$IFNDEF PAS2JS} |
| 2503 |
procedure TJSONArray.SetUnicodeStrings(Index: Integer; |
| 2504 |
const AValue: TJSONUnicodeStringType); |
| 2505 |
begin |
| 2506 |
Items[Index]:=CreateJSON(AValue); |
| 2507 |
end; |
| 2508 |
{$ENDIF} |
| 2509 |
|
| 2510 |
function TJSONArray.DoFindPath(const APath: TJSONStringType; out |
| 2511 |
NotFound: TJSONStringType): TJSONdata; |
| 2512 |
|
| 2513 |
Var |
| 2514 |
P,I : integer; |
| 2515 |
E : String; |
| 2516 |
|
| 2517 |
begin |
| 2518 |
if (APath<>'') and (APath[1]='[') then |
| 2519 |
begin |
| 2520 |
P:=Pos(']',APath); |
| 2521 |
I:=-1; |
| 2522 |
If (P>2) then |
| 2523 |
I:=StrToIntDef(Copy(APath,2,P-2),-1); |
| 2524 |
If (I>=0) and (I<Count) then |
| 2525 |
begin |
| 2526 |
E:=APath; |
| 2527 |
System.Delete(E,1,P); |
| 2528 |
Result:=Items[i].DoFindPath(E,NotFound); |
| 2529 |
end |
| 2530 |
else |
| 2531 |
begin |
| 2532 |
Result:=Nil; |
| 2533 |
If (P>0) then |
| 2534 |
NotFound:=Copy(APath,1,P) |
| 2535 |
else |
| 2536 |
NotFound:=APath; |
| 2537 |
end; |
| 2538 |
end |
| 2539 |
else |
| 2540 |
Result:=inherited DoFindPath(APath, NotFound); |
| 2541 |
end; |
| 2542 |
|
| 2543 |
procedure TJSONArray.Converterror(From: Boolean); |
| 2544 |
begin |
| 2545 |
If From then |
| 2546 |
DoError(SErrCannotConvertFromArray) |
| 2547 |
else |
| 2548 |
DoError(SErrCannotConvertToArray); |
| 2549 |
end; |
| 2550 |
|
| 2551 |
{$warnings off} |
| 2552 |
function TJSONArray.GetAsBoolean: Boolean; |
| 2553 |
begin |
| 2554 |
ConvertError(True); |
| 2555 |
Result:=false; |
| 2556 |
end; |
| 2557 |
|
| 2558 |
function TJSONArray.GetAsFloat: TJSONFloat; |
| 2559 |
begin |
| 2560 |
ConvertError(True); |
| 2561 |
Result:=0.0; |
| 2562 |
end; |
| 2563 |
|
| 2564 |
function TJSONArray.GetAsInteger: Integer; |
| 2565 |
begin |
| 2566 |
ConvertError(True); |
| 2567 |
Result:=0; |
| 2568 |
end; |
| 2569 |
|
| 2570 |
{$IFNDEF PAS2JS} |
| 2571 |
|
| 2572 |
{$ELSE} |
| 2573 |
{$ENDIF} |
| 2574 |
|
| 2575 |
procedure TJSONArray.SetAsBoolean(const AValue: Boolean); |
| 2576 |
begin |
| 2577 |
ConvertError(False); |
| 2578 |
if AValue then ; |
| 2579 |
end; |
| 2580 |
|
| 2581 |
procedure TJSONArray.SetAsFloat(const AValue: TJSONFloat); |
| 2582 |
begin |
| 2583 |
ConvertError(False); |
| 2584 |
if AValue>0 then ; |
| 2585 |
end; |
| 2586 |
|
| 2587 |
procedure TJSONArray.SetAsInteger(const AValue: Integer); |
| 2588 |
begin |
| 2589 |
ConvertError(False); |
| 2590 |
if AValue>0 then ; |
| 2591 |
end; |
| 2592 |
|
| 2593 |
|
| 2594 |
{$warnings on} |
| 2595 |
|
| 2596 |
function TJSONArray.GetAsJSON: TJSONStringType; |
| 2597 |
|
| 2598 |
Var |
| 2599 |
I : Integer; |
| 2600 |
Sep : String; |
| 2601 |
D : TJSONData; |
| 2602 |
V : TJSONStringType; |
| 2603 |
|
| 2604 |
begin |
| 2605 |
Sep:=TJSONData.FElementSep; |
| 2606 |
Result:='['; |
| 2607 |
For I:=0 to Count-1 do |
| 2608 |
begin |
| 2609 |
D:=Items[i]; |
| 2610 |
if D<>Nil then |
| 2611 |
V:=D.AsJSON |
| 2612 |
else |
| 2613 |
V:='null'; |
| 2614 |
Result:=Result+V; |
| 2615 |
If (I<Count-1) then |
| 2616 |
Result:=Result+Sep; |
| 2617 |
end; |
| 2618 |
Result:=Result+']'; |
| 2619 |
end; |
| 2620 |
|
| 2621 |
Function IndentString(Options : TFormatOptions; Indent : Integer) : TJSONStringType; |
| 2622 |
|
| 2623 |
begin |
| 2624 |
If (foUseTabChar in Options) then |
| 2625 |
Result:=StringofChar(#9,Indent) |
| 2626 |
else |
| 2627 |
Result:=StringOfChar(' ',Indent); |
| 2628 |
end; |
| 2629 |
|
| 2630 |
function TJSONArray.DoFormatJSON(Options: TFormatOptions; CurrentIndent, |
| 2631 |
Indent: Integer): TJSONStringType; |
| 2632 |
|
| 2633 |
Var |
| 2634 |
I : Integer; |
| 2635 |
MultiLine : Boolean; |
| 2636 |
SkipWhiteSpace : Boolean; |
| 2637 |
Ind : String; |
| 2638 |
|
| 2639 |
begin |
| 2640 |
Result:='['; |
| 2641 |
MultiLine:=Not (foSingleLineArray in Options); |
| 2642 |
SkipWhiteSpace:=foSkipWhiteSpace in Options; |
| 2643 |
Ind:=IndentString(Options, CurrentIndent+Indent); |
| 2644 |
if MultiLine then |
| 2645 |
Result:=Result+sLineBreak; |
| 2646 |
For I:=0 to Count-1 do |
| 2647 |
begin |
| 2648 |
if MultiLine then |
| 2649 |
Result:=Result+Ind; |
| 2650 |
if Items[i]=Nil then |
| 2651 |
Result:=Result+'null' |
| 2652 |
else |
| 2653 |
Result:=Result+Items[i].DoFormatJSON(Options,CurrentIndent+Indent,Indent); |
| 2654 |
If (I<Count-1) then |
| 2655 |
if MultiLine then |
| 2656 |
Result:=Result+',' |
| 2657 |
else |
| 2658 |
Result:=Result+ElementSeps[SkipWhiteSpace]; |
| 2659 |
if MultiLine then |
| 2660 |
Result:=Result+sLineBreak |
| 2661 |
end; |
| 2662 |
if MultiLine then |
| 2663 |
Result:=Result+IndentString(Options, CurrentIndent); |
| 2664 |
Result:=Result+']'; |
| 2665 |
end; |
| 2666 |
|
| 2667 |
|
| 2668 |
{$warnings off} |
| 2669 |
function TJSONArray.GetAsString: TJSONStringType; |
| 2670 |
begin |
| 2671 |
ConvertError(True); |
| 2672 |
Result:=''; |
| 2673 |
end; |
| 2674 |
|
| 2675 |
procedure TJSONArray.SetAsString(const AValue: TJSONStringType); |
| 2676 |
begin |
| 2677 |
ConvertError(False); |
| 2678 |
if AValue='' then ; |
| 2679 |
end; |
| 2680 |
|
| 2681 |
function TJSONArray.GetValue: TJSONVariant; |
| 2682 |
begin |
| 2683 |
ConvertError(True); |
| 2684 |
Result:=0; |
| 2685 |
end; |
| 2686 |
|
| 2687 |
procedure TJSONArray.SetValue(const AValue: TJSONVariant); |
| 2688 |
begin |
| 2689 |
ConvertError(False); |
| 2690 |
{$IFDEF PAS2JS} |
| 2691 |
if AValue=0 then ; |
| 2692 |
{$else} |
| 2693 |
if VarType(AValue)=0 then ; |
| 2694 |
{$ENDIF} |
| 2695 |
end; |
| 2696 |
{$warnings on} |
| 2697 |
|
| 2698 |
function TJSONArray.GetCount: Integer; |
| 2699 |
begin |
| 2700 |
Result:=FList.Count; |
| 2701 |
end; |
| 2702 |
|
| 2703 |
function TJSONArray.GetItem(Index: Integer): TJSONData; |
| 2704 |
begin |
| 2705 |
Result:=FList[Index] as TJSONData; |
| 2706 |
end; |
| 2707 |
|
| 2708 |
procedure TJSONArray.SetItem(Index: Integer; const AValue: TJSONData); |
| 2709 |
begin |
| 2710 |
If (Index=FList.Count) then |
| 2711 |
FList.Add(AValue) |
| 2712 |
else |
| 2713 |
FList[Index]:=AValue; |
| 2714 |
end; |
| 2715 |
|
| 2716 |
constructor TJSONArray.Create; |
| 2717 |
begin |
| 2718 |
Flist:=TObjectList.Create(True); |
| 2719 |
end; |
| 2720 |
|
| 2721 |
{$IFDEF PAS2JS} |
| 2722 |
Function VarRecToJSON(Const Element : jsvalue; const SourceType : String) : TJSONData; |
| 2723 |
var |
| 2724 |
i: NativeInt; |
| 2725 |
VObject: TObject; |
| 2726 |
begin |
| 2727 |
Result:=nil; |
| 2728 |
if Element=nil then |
| 2729 |
Result:=CreateJSON // TJSONNull |
| 2730 |
else if isBoolean(Element) then |
| 2731 |
Result:=CreateJSON(boolean(Element)) |
| 2732 |
else if isString(Element) then |
| 2733 |
Result:=CreateJSON(String(Element)) |
| 2734 |
else if isNumber(Element) then |
| 2735 |
begin |
| 2736 |
if isInteger(Element) then |
| 2737 |
begin |
| 2738 |
i:=NativeInt(Element); |
| 2739 |
if (i>=low(integer)) and (i<=high(integer)) then |
| 2740 |
Result:=CreateJSON(Integer(Element)) |
| 2741 |
else |
| 2742 |
Result:=CreateJSON(NativeInt(Element)); |
| 2743 |
end |
| 2744 |
else |
| 2745 |
Result:=CreateJSON(TJSONFloat(Element)); |
| 2746 |
end |
| 2747 |
else if isObject(Element) and (Element is TObject) then |
| 2748 |
begin |
| 2749 |
VObject:=TObject(Element); |
| 2750 |
if VObject is TJSONData then |
| 2751 |
Result:=TJSONData(VObject) |
| 2752 |
else |
| 2753 |
TJSONData.DoError(SErrNotJSONData,[VObject.ClassName,SourceType]); |
| 2754 |
end |
| 2755 |
else |
| 2756 |
TJSONData.DoError(SErrUnknownTypeInConstructor,[SourceType,jsTypeOf(Element)]); |
| 2757 |
end; |
| 2758 |
{$else} |
| 2759 |
Function VarRecToJSON(Const Element : TVarRec; const SourceType : String) : TJSONData; |
| 2760 |
|
| 2761 |
begin |
| 2762 |
Result:=Nil; |
| 2763 |
With Element do |
| 2764 |
case VType of |
| 2765 |
vtInteger : Result:=CreateJSON(VInteger); |
| 2766 |
vtBoolean : Result:=CreateJSON(VBoolean); |
| 2767 |
vtChar : Result:=CreateJSON(VChar); |
| 2768 |
vtExtended : Result:=CreateJSON(VExtended^); |
| 2769 |
vtString : Result:=CreateJSON(vString^); |
| 2770 |
vtAnsiString : Result:=CreateJSON(UTF8Decode(StrPas(VPChar))); |
| 2771 |
vtUnicodeString: Result:=CreateJSON(UnicodeString(VUnicodeString)); |
| 2772 |
vtWideString: Result:=CreateJSON(WideString(VWideString)); |
| 2773 |
vtPChar : Result:=CreateJSON(StrPas(VPChar)); |
| 2774 |
vtWideChar : Result:=CreateJSON(VWideChar); |
| 2775 |
vtPointer : If (VPointer<>Nil) then |
| 2776 |
TJSONData.DoError(SErrPointerNotNil,[SourceType]) |
| 2777 |
else |
| 2778 |
Result:=CreateJSON(); |
| 2779 |
vtCurrency : Result:=CreateJSON(vCurrency^); |
| 2780 |
vtInt64 : Result:=CreateJSON(vInt64^); |
| 2781 |
vtObject : if (VObject is TJSONData) then |
| 2782 |
Result:=TJSONData(VObject) |
| 2783 |
else |
| 2784 |
TJSONData.DoError(SErrNotJSONData,[VObject.ClassName,SourceType]); |
| 2785 |
//vtVariant : |
| 2786 |
else |
| 2787 |
TJSONData.DoError(SErrUnknownTypeInConstructor,[SourceType,VType]) |
| 2788 |
end; |
| 2789 |
end; |
| 2790 |
{$ENDIF} |
| 2791 |
|
| 2792 |
constructor TJSONArray.Create(const Elements: array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}); |
| 2793 |
|
| 2794 |
Var |
| 2795 |
I : integer; |
| 2796 |
J : TJSONData; |
| 2797 |
|
| 2798 |
begin |
| 2799 |
Create; |
| 2800 |
For I:=Low(Elements) to High(Elements) do |
| 2801 |
begin |
| 2802 |
J:=VarRecToJSON(Elements[i],'Array'); |
| 2803 |
Add(J); |
| 2804 |
end; |
| 2805 |
end; |
| 2806 |
|
| 2807 |
destructor TJSONArray.Destroy; |
| 2808 |
begin |
| 2809 |
FreeAndNil(FList); |
| 2810 |
inherited Destroy; |
| 2811 |
end; |
| 2812 |
|
| 2813 |
class function TJSONArray.JSONType: TJSONType; |
| 2814 |
begin |
| 2815 |
Result:=jtArray; |
| 2816 |
end; |
| 2817 |
|
| 2818 |
function TJSONArray.Clone: TJSONData; |
| 2819 |
|
| 2820 |
Var |
| 2821 |
A : TJSONArray; |
| 2822 |
I : Integer; |
| 2823 |
|
| 2824 |
begin |
| 2825 |
A:=TJSONArrayClass(ClassType).Create; |
| 2826 |
try |
| 2827 |
For I:=0 to Count-1 do |
| 2828 |
A.Add(Self.Items[I].Clone); |
| 2829 |
Result:=A; |
| 2830 |
except |
| 2831 |
A.Free; |
| 2832 |
Raise; |
| 2833 |
end; |
| 2834 |
end; |
| 2835 |
|
| 2836 |
procedure TJSONArray.Iterate(Iterator: TJSONArrayIterator; Data: TObject); |
| 2837 |
|
| 2838 |
Var |
| 2839 |
I : Integer; |
| 2840 |
Cont : Boolean; |
| 2841 |
|
| 2842 |
begin |
| 2843 |
I:=0; |
| 2844 |
Cont:=True; |
| 2845 |
While (I<FList.Count) and cont do |
| 2846 |
begin |
| 2847 |
Iterator(Items[i],Data,Cont); |
| 2848 |
Inc(I); |
| 2849 |
end; |
| 2850 |
end; |
| 2851 |
|
| 2852 |
function TJSONArray.IndexOf(obj: TJSONData): Integer; |
| 2853 |
begin |
| 2854 |
Result:=FList.IndexOf(Obj); |
| 2855 |
end; |
| 2856 |
|
| 2857 |
function TJSONArray.GetEnumerator: TBaseJSONEnumerator; |
| 2858 |
begin |
| 2859 |
Result:=TJSONArrayEnumerator.Create(Self); |
| 2860 |
end; |
| 2861 |
|
| 2862 |
procedure TJSONArray.Clear; |
| 2863 |
begin |
| 2864 |
FList.Clear; |
| 2865 |
end; |
| 2866 |
|
| 2867 |
function TJSONArray.Add(Item: TJSONData): Integer; |
| 2868 |
begin |
| 2869 |
Result:=FList.Add(Item); |
| 2870 |
end; |
| 2871 |
|
| 2872 |
function TJSONArray.Add(I: Integer): Integer; |
| 2873 |
begin |
| 2874 |
Result:=Add(CreateJSON(I)); |
| 2875 |
end; |
| 2876 |
|
| 2877 |
|
| 2878 |
{$IFNDEF PAS2JS} |
| 2879 |
function TJSONArray.GetAsInt64: Int64; |
| 2880 |
begin |
| 2881 |
ConvertError(True); |
| 2882 |
Result:=0; |
| 2883 |
end; |
| 2884 |
|
| 2885 |
function TJSONArray.GetAsQWord: QWord; |
| 2886 |
begin |
| 2887 |
ConvertError(True); |
| 2888 |
Result:=0; |
| 2889 |
end; |
| 2890 |
|
| 2891 |
procedure TJSONArray.SetAsInt64(const AValue: Int64); |
| 2892 |
begin |
| 2893 |
ConvertError(False); |
| 2894 |
if AValue>0 then ; |
| 2895 |
end; |
| 2896 |
|
| 2897 |
procedure TJSONArray.SetAsQword(const AValue: QWord); |
| 2898 |
begin |
| 2899 |
ConvertError(False); |
| 2900 |
if AValue>0 then ; |
| 2901 |
end; |
| 2902 |
|
| 2903 |
function TJSONArray.Add(I: Int64): Int64; |
| 2904 |
begin |
| 2905 |
Result:=Add(CreateJSON(I)); |
| 2906 |
end; |
| 2907 |
|
| 2908 |
function TJSONArray.Add(I: QWord): QWord; |
| 2909 |
begin |
| 2910 |
Result:=Add(CreateJSON(I)); |
| 2911 |
end; |
| 2912 |
|
| 2913 |
///function TJSONArray.Add(const S: UnicodeString): Integer; |
| 2914 |
///begin |
| 2915 |
/// Result:=Add(CreateJSON(S)); |
| 2916 |
///end; |
| 2917 |
|
| 2918 |
procedure TJSONArray.Insert(Index: Integer; I: Int64); |
| 2919 |
begin |
| 2920 |
FList.Insert(Index, CreateJSON(I)); |
| 2921 |
end; |
| 2922 |
|
| 2923 |
procedure TJSONArray.Insert(Index: Integer; I: QWord); |
| 2924 |
begin |
| 2925 |
FList.Insert(Index, CreateJSON(I)); |
| 2926 |
end; |
| 2927 |
|
| 2928 |
///procedure TJSONArray.Insert(Index: Integer; const S: UnicodeString); |
| 2929 |
///begin |
| 2930 |
/// FList.Insert(Index, CreateJSON(S)); |
| 2931 |
///end; |
| 2932 |
|
| 2933 |
{$ELSE} |
| 2934 |
|
| 2935 |
function TJSONArray.GetAsNativeInt: NativeInt; |
| 2936 |
begin |
| 2937 |
ConvertError(True); |
| 2938 |
Result:=0; |
| 2939 |
end; |
| 2940 |
|
| 2941 |
procedure TJSONArray.SetAsNativeInt(const AValue: NativeInt); |
| 2942 |
begin |
| 2943 |
ConvertError(False); |
| 2944 |
if AValue<>0 then; |
| 2945 |
end; |
| 2946 |
|
| 2947 |
function TJSONArray.Add(I: NativeInt): Integer; |
| 2948 |
begin |
| 2949 |
Result:=Add(CreateJSON(I)); |
| 2950 |
end; |
| 2951 |
|
| 2952 |
procedure TJSONArray.Insert(Index: Integer; I: NativeInt); |
| 2953 |
begin |
| 2954 |
FList.Insert(Index, CreateJSON(I)); |
| 2955 |
end; |
| 2956 |
|
| 2957 |
{$ENDIF} |
| 2958 |
|
| 2959 |
function TJSONArray.Add(const S: String): Integer; |
| 2960 |
begin |
| 2961 |
Result:=Add(CreateJSON(S)); |
| 2962 |
end; |
| 2963 |
|
| 2964 |
function TJSONArray.Add: Integer; |
| 2965 |
begin |
| 2966 |
Result:=Add(CreateJSON); |
| 2967 |
end; |
| 2968 |
|
| 2969 |
function TJSONArray.Add(F: TJSONFloat): Integer; |
| 2970 |
begin |
| 2971 |
Result:=Add(CreateJSON(F)); |
| 2972 |
end; |
| 2973 |
|
| 2974 |
function TJSONArray.Add(B: Boolean): Integer; |
| 2975 |
begin |
| 2976 |
Result:=Add(CreateJSON(B)); |
| 2977 |
end; |
| 2978 |
|
| 2979 |
function TJSONArray.Add(AnArray: TJSONArray): Integer; |
| 2980 |
begin |
| 2981 |
If (IndexOf(AnArray)<>-1) then |
| 2982 |
DoError(SErrCannotAddArrayTwice); |
| 2983 |
Result:=Add(TJSONData(AnArray)); |
| 2984 |
end; |
| 2985 |
|
| 2986 |
function TJSONArray.Add(AnObject: TJSONObject): Integer; |
| 2987 |
begin |
| 2988 |
If (IndexOf(AnObject)<>-1) then |
| 2989 |
DoError(SErrCannotAddObjectTwice); |
| 2990 |
Result:=Add(TJSONData(AnObject)); |
| 2991 |
end; |
| 2992 |
|
| 2993 |
procedure TJSONArray.Delete(Index: Integer); |
| 2994 |
begin |
| 2995 |
FList.Delete(Index); |
| 2996 |
end; |
| 2997 |
|
| 2998 |
procedure TJSONArray.Exchange(Index1, Index2: Integer); |
| 2999 |
begin |
| 3000 |
FList.Exchange(Index1, Index2); |
| 3001 |
end; |
| 3002 |
|
| 3003 |
function TJSONArray.Extract(Item: TJSONData): TJSONData; |
| 3004 |
begin |
| 3005 |
Result := TJSONData(FList.Extract(Item)); |
| 3006 |
end; |
| 3007 |
|
| 3008 |
function TJSONArray.Extract(Index: Integer): TJSONData; |
| 3009 |
begin |
| 3010 |
Result := TJSONData(FList.Extract(FList.Items[Index])); |
| 3011 |
end; |
| 3012 |
|
| 3013 |
procedure TJSONArray.Insert(Index: Integer); |
| 3014 |
begin |
| 3015 |
Insert(Index,CreateJSON); |
| 3016 |
end; |
| 3017 |
|
| 3018 |
procedure TJSONArray.Insert(Index: Integer; Item: TJSONData); |
| 3019 |
begin |
| 3020 |
FList.Insert(Index, Item); |
| 3021 |
end; |
| 3022 |
|
| 3023 |
procedure TJSONArray.Insert(Index: Integer; I: Integer); |
| 3024 |
begin |
| 3025 |
FList.Insert(Index, CreateJSON(I)); |
| 3026 |
end; |
| 3027 |
|
| 3028 |
|
| 3029 |
procedure TJSONArray.Insert(Index: Integer; const S: String); |
| 3030 |
begin |
| 3031 |
FList.Insert(Index, CreateJSON(S)); |
| 3032 |
end; |
| 3033 |
|
| 3034 |
procedure TJSONArray.Insert(Index: Integer; F: TJSONFloat); |
| 3035 |
begin |
| 3036 |
FList.Insert(Index, CreateJSON(F)); |
| 3037 |
end; |
| 3038 |
|
| 3039 |
procedure TJSONArray.Insert(Index: Integer; B: Boolean); |
| 3040 |
begin |
| 3041 |
FList.Insert(Index, CreateJSON(B)); |
| 3042 |
end; |
| 3043 |
|
| 3044 |
procedure TJSONArray.Insert(Index: Integer; AnArray: TJSONArray); |
| 3045 |
begin |
| 3046 |
if (IndexOf(AnArray)<>-1) then |
| 3047 |
DoError(SErrCannotAddArrayTwice); |
| 3048 |
FList.Insert(Index, AnArray); |
| 3049 |
end; |
| 3050 |
|
| 3051 |
procedure TJSONArray.Insert(Index: Integer; AnObject: TJSONObject); |
| 3052 |
begin |
| 3053 |
if (IndexOf(AnObject)<>-1) then |
| 3054 |
DoError(SErrCannotAddObjectTwice); |
| 3055 |
FList.Insert(Index, AnObject); |
| 3056 |
end; |
| 3057 |
|
| 3058 |
procedure TJSONArray.Move(CurIndex, NewIndex: Integer); |
| 3059 |
begin |
| 3060 |
FList.Move(CurIndex, NewIndex); |
| 3061 |
end; |
| 3062 |
|
| 3063 |
procedure TJSONArray.Remove(Item: TJSONData); |
| 3064 |
begin |
| 3065 |
FList.Remove(Item); |
| 3066 |
end; |
| 3067 |
|
| 3068 |
procedure TJSONArray.Sort(Compare: TListSortCompare); |
| 3069 |
begin |
| 3070 |
FList.Sort(Compare); |
| 3071 |
end; |
| 3072 |
|
| 3073 |
{ TJSONObject } |
| 3074 |
|
| 3075 |
function TJSONObject.GetArrays(const AName: String): TJSONArray; |
| 3076 |
begin |
| 3077 |
Result:=GetElements(AName) as TJSONArray; |
| 3078 |
end; |
| 3079 |
|
| 3080 |
function TJSONObject.GetBooleans(const AName: String): Boolean; |
| 3081 |
begin |
| 3082 |
Result:=GetElements(AName).AsBoolean; |
| 3083 |
end; |
| 3084 |
|
| 3085 |
function TJSONObject.GetElements(const AName: string): TJSONData; |
| 3086 |
begin |
| 3087 |
{$IFDEF PAS2JS} |
| 3088 |
if FHash.hasOwnProperty('%'+AName) then |
| 3089 |
Result:=TJSONData(FHash['%'+AName]) |
| 3090 |
else |
| 3091 |
DoError(SErrNonexistentElement,[AName]); |
| 3092 |
{$else} |
| 3093 |
Result:=TJSONData(FHash.Find(AName)); |
| 3094 |
If (Result=Nil) then |
| 3095 |
DoError(SErrNonexistentElement,[AName]); |
| 3096 |
{$ENDIF} |
| 3097 |
end; |
| 3098 |
|
| 3099 |
function TJSONObject.GetFloats(const AName: String): TJSONFloat; |
| 3100 |
begin |
| 3101 |
Result:=GetElements(AName).AsFloat; |
| 3102 |
end; |
| 3103 |
|
| 3104 |
function TJSONObject.GetIntegers(const AName: String): Integer; |
| 3105 |
begin |
| 3106 |
Result:=GetElements(AName).AsInteger; |
| 3107 |
end; |
| 3108 |
|
| 3109 |
{$IFNDEF PAS2JS} |
| 3110 |
function TJSONObject.GetInt64s(const AName: String): Int64; |
| 3111 |
begin |
| 3112 |
Result:=GetElements(AName).AsInt64; |
| 3113 |
end; |
| 3114 |
|
| 3115 |
function TJSONObject.GetQWords(const AName : String): QWord; |
| 3116 |
begin |
| 3117 |
Result:=GetElements(AName).AsQWord; |
| 3118 |
end; |
| 3119 |
|
| 3120 |
function TJSONObject.GetUnicodeStrings(const AName: String |
| 3121 |
): TJSONUnicodeStringType; |
| 3122 |
begin |
| 3123 |
Result:=GetElements(AName).AsUnicodeString; |
| 3124 |
end; |
| 3125 |
|
| 3126 |
procedure TJSONObject.SetInt64s(const AName : String; const AValue: Int64); |
| 3127 |
begin |
| 3128 |
SetElements(AName,CreateJSON(AVAlue)); |
| 3129 |
end; |
| 3130 |
|
| 3131 |
procedure TJSONObject.SetQWords(const AName : String; AValue: QWord); |
| 3132 |
begin |
| 3133 |
SetElements(AName,CreateJSON(AVAlue)); |
| 3134 |
end; |
| 3135 |
|
| 3136 |
procedure TJSONObject.SetUnicodeStrings(const AName: String; |
| 3137 |
const AValue: TJSONUnicodeStringType); |
| 3138 |
begin |
| 3139 |
SetElements(AName,CreateJSON(AValue)); |
| 3140 |
end; |
| 3141 |
{$ELSE} |
| 3142 |
function TJSONObject.GetNativeInts(const AName: String): NativeInt; |
| 3143 |
begin |
| 3144 |
Result:=GetElements(AName).AsNativeInt; |
| 3145 |
end; |
| 3146 |
|
| 3147 |
procedure TJSONObject.SetNativeInts(const AName: String; const AValue: NativeInt); |
| 3148 |
begin |
| 3149 |
SetElements(AName,CreateJSON(AVAlue)); |
| 3150 |
end; |
| 3151 |
|
| 3152 |
{$ENDIF} |
| 3153 |
|
| 3154 |
function TJSONObject.GetIsNull(const AName: String): Boolean; |
| 3155 |
begin |
| 3156 |
Result:=GetElements(AName).IsNull; |
| 3157 |
end; |
| 3158 |
|
| 3159 |
function TJSONObject.GetNameOf(Index: Integer): TJSONStringType; |
| 3160 |
begin |
| 3161 |
{$IFDEF PAS2JS} |
| 3162 |
if FNames=nil then |
| 3163 |
FNames:=TJSObject.getOwnPropertyNames(FHash); |
| 3164 |
if (Index<0) or (Index>=FCount) then |
| 3165 |
DoError(SListIndexError,[Index]); |
| 3166 |
Result:=copy(FNames[Index],2); |
| 3167 |
{$else} |
| 3168 |
Result := FHash.NameOfIndex(Index); |
| 3169 |
{$ENDIF} |
| 3170 |
end; |
| 3171 |
|
| 3172 |
function TJSONObject.GetObjects(const AName : String): TJSONObject; |
| 3173 |
begin |
| 3174 |
Result:=GetElements(AName) as TJSONObject; |
| 3175 |
end; |
| 3176 |
|
| 3177 |
|
| 3178 |
function TJSONObject.GetStrings(const AName : String): TJSONStringType; |
| 3179 |
begin |
| 3180 |
Result:=GetElements(AName).AsString; |
| 3181 |
end; |
| 3182 |
|
| 3183 |
function TJSONObject.GetTypes(const AName : String): TJSONType; |
| 3184 |
begin |
| 3185 |
Result:=Getelements(Aname).JSONType; |
| 3186 |
end; |
| 3187 |
|
| 3188 |
class function TJSONObject.GetUnquotedMemberNames: Boolean; ///{$IFNDEF PAS2JS}static;{$ENDIF} |
| 3189 |
begin |
| 3190 |
Result:=FUnquotedMemberNames; |
| 3191 |
end; |
| 3192 |
|
| 3193 |
procedure TJSONObject.SetArrays(const AName : String; const AValue: TJSONArray); |
| 3194 |
|
| 3195 |
begin |
| 3196 |
SetElements(AName,AVAlue); |
| 3197 |
end; |
| 3198 |
|
| 3199 |
procedure TJSONObject.SetBooleans(const AName : String; const AValue: Boolean); |
| 3200 |
begin |
| 3201 |
SetElements(AName,CreateJSON(AVAlue)); |
| 3202 |
end; |
| 3203 |
|
| 3204 |
procedure TJSONObject.SetElements(const AName: string; const AValue: TJSONData); |
| 3205 |
{$IFDEF PAS2JS} |
| 3206 |
begin |
| 3207 |
if not FHash.hasOwnProperty('%'+AName) then |
| 3208 |
inc(FCount); |
| 3209 |
FHash['%'+AName]:=AValue; |
| 3210 |
FNames:=nil; |
| 3211 |
end; |
| 3212 |
{$else} |
| 3213 |
Var |
| 3214 |
Index : Integer; |
| 3215 |
|
| 3216 |
begin |
| 3217 |
Index:=FHash.FindIndexOf(AName); |
| 3218 |
If (Index=-1) then |
| 3219 |
FHash.Add(AName,AValue) |
| 3220 |
else |
| 3221 |
FHash.Items[Index]:=AValue; // Will free the previous value. |
| 3222 |
end; |
| 3223 |
{$ENDIF} |
| 3224 |
|
| 3225 |
procedure TJSONObject.SetFloats(const AName : String; const AValue: TJSONFloat); |
| 3226 |
begin |
| 3227 |
SetElements(AName,CreateJSON(AVAlue)); |
| 3228 |
end; |
| 3229 |
|
| 3230 |
procedure TJSONObject.SetIntegers(const AName : String; const AValue: Integer); |
| 3231 |
begin |
| 3232 |
SetElements(AName,CreateJSON(AVAlue)); |
| 3233 |
end; |
| 3234 |
|
| 3235 |
|
| 3236 |
procedure TJSONObject.SetIsNull(const AName : String; const AValue: Boolean); |
| 3237 |
begin |
| 3238 |
If Not AValue then |
| 3239 |
DoError(SErrCannotSetNotIsNull); |
| 3240 |
SetElements(AName,CreateJSON); |
| 3241 |
end; |
| 3242 |
|
| 3243 |
procedure TJSONObject.SetObjects(const AName : String; const AValue: TJSONObject); |
| 3244 |
begin |
| 3245 |
SetElements(AName,AValue); |
| 3246 |
end; |
| 3247 |
|
| 3248 |
|
| 3249 |
procedure TJSONObject.SetStrings(const AName : String; const AValue: TJSONStringType); |
| 3250 |
begin |
| 3251 |
SetElements(AName,CreateJSON(AValue)); |
| 3252 |
end; |
| 3253 |
|
| 3254 |
|
| 3255 |
class procedure TJSONObject.DetermineElementQuotes; |
| 3256 |
|
| 3257 |
begin |
| 3258 |
FObjStartSep:=ObjStartSeps[TJSONData.FCompressedJSON]; |
| 3259 |
FObjEndSep:=ObjEndSeps[TJSONData.FCompressedJSON]; |
| 3260 |
if TJSONData.FCompressedJSON then |
| 3261 |
FElementEnd:=UnSpacedQuoted[FUnquotedMemberNames] |
| 3262 |
else |
| 3263 |
FElementEnd:=SpacedQuoted[FUnquotedMemberNames]; |
| 3264 |
FElementStart:=ElementStart[FUnquotedMemberNames] |
| 3265 |
end; |
| 3266 |
|
| 3267 |
class procedure TJSONObject.SetUnquotedMemberNames(AValue: Boolean); ///{$IFNDEF PAS2JS}static;{$ENDIF} |
| 3268 |
|
| 3269 |
begin |
| 3270 |
if FUnquotedMemberNames=AValue then exit; |
| 3271 |
FUnquotedMemberNames:=AValue; |
| 3272 |
DetermineElementQuotes; |
| 3273 |
end; |
| 3274 |
|
| 3275 |
function TJSONObject.DoFindPath(const APath: TJSONStringType; out |
| 3276 |
NotFound: TJSONStringType): TJSONdata; |
| 3277 |
|
| 3278 |
Var |
| 3279 |
N: TJSONStringType; |
| 3280 |
L,P,P2 : Integer; |
| 3281 |
|
| 3282 |
begin |
| 3283 |
If (APath='') then |
| 3284 |
Exit(Self); |
| 3285 |
N:=APath; |
| 3286 |
L:=Length(N); |
| 3287 |
P:=1; |
| 3288 |
While (P<L) and (N[P]='.') do |
| 3289 |
inc(P); |
| 3290 |
P2:=P; |
| 3291 |
While (P2<=L) and (Not (N[P2] in ['.','['])) do |
| 3292 |
inc(P2); |
| 3293 |
N:=Copy(APath,P,P2-P); |
| 3294 |
If (N='') then |
| 3295 |
Result:=Self |
| 3296 |
else |
| 3297 |
begin |
| 3298 |
Result:=Find(N); |
| 3299 |
If Result=Nil then |
| 3300 |
NotFound:=N+Copy(APath,P2,L-P2) |
| 3301 |
else |
| 3302 |
begin |
| 3303 |
N:=Copy(APath,P2,L-P2+1); |
| 3304 |
Result:=Result.DoFindPath(N,NotFound); |
| 3305 |
end; |
| 3306 |
end; |
| 3307 |
end; |
| 3308 |
|
| 3309 |
procedure TJSONObject.Converterror(From: Boolean); |
| 3310 |
begin |
| 3311 |
If From then |
| 3312 |
DoError(SErrCannotConvertFromObject) |
| 3313 |
else |
| 3314 |
DoError(SErrCannotConvertToObject); |
| 3315 |
end; |
| 3316 |
|
| 3317 |
{$warnings off} |
| 3318 |
function TJSONObject.GetAsBoolean: Boolean; |
| 3319 |
begin |
| 3320 |
ConvertError(True); |
| 3321 |
Result:=false; |
| 3322 |
end; |
| 3323 |
|
| 3324 |
function TJSONObject.GetAsFloat: TJSONFloat; |
| 3325 |
begin |
| 3326 |
ConvertError(True); |
| 3327 |
Result:=0.0; |
| 3328 |
end; |
| 3329 |
|
| 3330 |
function TJSONObject.GetAsInteger: Integer; |
| 3331 |
begin |
| 3332 |
ConvertError(True); |
| 3333 |
Result:=0; |
| 3334 |
end; |
| 3335 |
|
| 3336 |
procedure TJSONObject.SetAsBoolean(const AValue: Boolean); |
| 3337 |
begin |
| 3338 |
ConvertError(False); |
| 3339 |
if AValue then ; |
| 3340 |
end; |
| 3341 |
|
| 3342 |
procedure TJSONObject.SetAsFloat(const AValue: TJSONFloat); |
| 3343 |
begin |
| 3344 |
ConvertError(False); |
| 3345 |
if AValue>0 then ; |
| 3346 |
end; |
| 3347 |
|
| 3348 |
procedure TJSONObject.SetAsInteger(const AValue: Integer); |
| 3349 |
begin |
| 3350 |
ConvertError(False); |
| 3351 |
if AValue>0 then ; |
| 3352 |
end; |
| 3353 |
|
| 3354 |
{$IFNDEF PAS2JS} |
| 3355 |
function TJSONObject.Add(const AName: String; AValue: TJSONUnicodeStringType |
| 3356 |
): Integer; |
| 3357 |
begin |
| 3358 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3359 |
end; |
| 3360 |
|
| 3361 |
function TJSONObject.Add(const AName: TJSONStringType; Avalue: Int64): Integer; |
| 3362 |
begin |
| 3363 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3364 |
end; |
| 3365 |
|
| 3366 |
function TJSONObject.Add(const AName: TJSONStringType; Avalue: QWord): Integer; |
| 3367 |
begin |
| 3368 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3369 |
end; |
| 3370 |
|
| 3371 |
function TJSONObject.GetAsInt64: Int64; |
| 3372 |
begin |
| 3373 |
ConvertError(True); |
| 3374 |
end; |
| 3375 |
|
| 3376 |
function TJSONObject.GetAsQWord: QWord; |
| 3377 |
begin |
| 3378 |
ConvertError(True); |
| 3379 |
end; |
| 3380 |
|
| 3381 |
procedure TJSONObject.SetAsInt64(const AValue: Int64); |
| 3382 |
begin |
| 3383 |
ConvertError(False); |
| 3384 |
if AValue>0 then ; |
| 3385 |
end; |
| 3386 |
|
| 3387 |
procedure TJSONObject.SetAsQword(const AValue: QWord); |
| 3388 |
begin |
| 3389 |
ConvertError(False); |
| 3390 |
if AValue>0 then ; |
| 3391 |
end; |
| 3392 |
|
| 3393 |
{$ELSE} |
| 3394 |
function TJSONObject.GetAsNativeInt: NativeInt; |
| 3395 |
begin |
| 3396 |
ConvertError(True); |
| 3397 |
Result:=0; |
| 3398 |
end; |
| 3399 |
|
| 3400 |
Procedure TJSONObject.SetAsNativeInt(const aValue : NativeInt); |
| 3401 |
begin |
| 3402 |
ConvertError(False); |
| 3403 |
if AValue<>0 then; |
| 3404 |
end; |
| 3405 |
|
| 3406 |
function TJSONObject.Add(const AName: TJSONStringType; Avalue: NativeInt): Integer; |
| 3407 |
begin |
| 3408 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3409 |
end; |
| 3410 |
|
| 3411 |
{$ENDIF} |
| 3412 |
|
| 3413 |
{$warnings on} |
| 3414 |
|
| 3415 |
function TJSONObject.GetAsJSON: TJSONStringType; |
| 3416 |
|
| 3417 |
Var |
| 3418 |
I : Integer; |
| 3419 |
Sep : String; |
| 3420 |
V : TJSONStringType; |
| 3421 |
D : TJSONData; |
| 3422 |
|
| 3423 |
begin |
| 3424 |
Sep:=TJSONData.FElementSep; |
| 3425 |
Result:=''; |
| 3426 |
For I:=0 to Count-1 do |
| 3427 |
begin |
| 3428 |
If (Result<>'') then |
| 3429 |
Result:=Result+Sep; |
| 3430 |
D:=Items[i]; |
| 3431 |
if Assigned(D) then |
| 3432 |
V:=Items[I].AsJSON |
| 3433 |
else |
| 3434 |
V:='null'; |
| 3435 |
Result:=Result+FElementStart+StringToJSONString(Names[i])+FElementEnd+V; |
| 3436 |
end; |
| 3437 |
If (Result<>'') then |
| 3438 |
Result:=FObjStartSep+Result+FObjEndSep |
| 3439 |
else |
| 3440 |
Result:='{}'; |
| 3441 |
end; |
| 3442 |
|
| 3443 |
{$warnings off} |
| 3444 |
function TJSONObject.GetAsString: TJSONStringType; |
| 3445 |
begin |
| 3446 |
ConvertError(True); |
| 3447 |
Result:=''; |
| 3448 |
end; |
| 3449 |
|
| 3450 |
procedure TJSONObject.SetAsString(const AValue: TJSONStringType); |
| 3451 |
begin |
| 3452 |
ConvertError(False); |
| 3453 |
if AValue='' then ; |
| 3454 |
end; |
| 3455 |
|
| 3456 |
function TJSONObject.GetValue: TJSONVariant; |
| 3457 |
begin |
| 3458 |
ConvertError(True); |
| 3459 |
Result:=0; |
| 3460 |
end; |
| 3461 |
|
| 3462 |
procedure TJSONObject.SetValue(const AValue: TJSONVariant); |
| 3463 |
begin |
| 3464 |
ConvertError(False); |
| 3465 |
{$IFDEF PAS2JS} |
| 3466 |
if AValue=0 then ; |
| 3467 |
{$else} |
| 3468 |
if VarType(AValue)=0 then ; |
| 3469 |
{$ENDIF} |
| 3470 |
end; |
| 3471 |
{$warnings on} |
| 3472 |
|
| 3473 |
function TJSONObject.GetCount: Integer; |
| 3474 |
begin |
| 3475 |
{$IFDEF PAS2JS} |
| 3476 |
Result:=FCount; |
| 3477 |
{$else} |
| 3478 |
Result:=FHash.Count; |
| 3479 |
{$ENDIF} |
| 3480 |
end; |
| 3481 |
|
| 3482 |
function TJSONObject.GetItem(Index: Integer): TJSONData; |
| 3483 |
begin |
| 3484 |
{$IFDEF PAS2JS} |
| 3485 |
Result:=GetElements(GetNameOf(Index)); |
| 3486 |
{$else} |
| 3487 |
Result:=TJSONData(FHash.Items[Index]); |
| 3488 |
{$ENDIF} |
| 3489 |
end; |
| 3490 |
|
| 3491 |
procedure TJSONObject.SetItem(Index: Integer; const AValue: TJSONData); |
| 3492 |
begin |
| 3493 |
{$IFDEF PAS2JS} |
| 3494 |
SetElements(GetNameOf(Index),AValue); |
| 3495 |
{$else} |
| 3496 |
FHash.Items[Index]:=AValue; |
| 3497 |
{$ENDIF} |
| 3498 |
end; |
| 3499 |
|
| 3500 |
constructor TJSONObject.Create; |
| 3501 |
begin |
| 3502 |
{$IFDEF PAS2JS} |
| 3503 |
FHash:=TJSObject.new; |
| 3504 |
{$else} |
| 3505 |
FHash:=TFPHashObjectList.Create(True); |
| 3506 |
{$ENDIF} |
| 3507 |
end; |
| 3508 |
|
| 3509 |
constructor TJSONObject.Create(const Elements: array of {$IFDEF PAS2JS}jsvalue{$else}Const{$ENDIF}); |
| 3510 |
|
| 3511 |
Var |
| 3512 |
I : integer; |
| 3513 |
AName : TJSONUnicodeStringType; |
| 3514 |
J : TJSONData; |
| 3515 |
|
| 3516 |
begin |
| 3517 |
Create; |
| 3518 |
If ((High(Elements)-Low(Elements)) mod 2)=0 then |
| 3519 |
DoError(SErrOddNumber); |
| 3520 |
I:=Low(Elements); |
| 3521 |
While I<=High(Elements) do |
| 3522 |
begin |
| 3523 |
{$IFDEF PAS2JS} |
| 3524 |
if isString(Elements[I]) then |
| 3525 |
AName:=String(Elements[I]) |
| 3526 |
else |
| 3527 |
DoError(SErrNameMustBeString,[I+1]); |
| 3528 |
{$else} |
| 3529 |
With Elements[i] do |
| 3530 |
Case VType of |
| 3531 |
vtChar : AName:=TJSONUnicodeStringType(VChar); |
| 3532 |
vtWideChar : AName:=TJSONUnicodeStringType(VWideChar); |
| 3533 |
vtString : AName:=TJSONUnicodeStringType(vString^); |
| 3534 |
vtAnsiString : AName:=UTF8Decode(StrPas(VPChar)); |
| 3535 |
vtPChar : AName:=TJSONUnicodeStringType(StrPas(VPChar)); |
| 3536 |
else |
| 3537 |
DoError(SErrNameMustBeString,[I+1]); |
| 3538 |
end; |
| 3539 |
{$ENDIF} |
| 3540 |
If (AName='') then |
| 3541 |
DoError(SErrNameMustBeString,[I+1]); |
| 3542 |
Inc(I); |
| 3543 |
J:=VarRecToJSON(Elements[i],'Object'); |
| 3544 |
{$IFDEF FPC_HAS_CPSTRING} |
| 3545 |
Add(UTF8Encode(AName),J); |
| 3546 |
{$ELSE} |
| 3547 |
Add(AName,J); |
| 3548 |
{$ENDIF} |
| 3549 |
Inc(I); |
| 3550 |
end; |
| 3551 |
end; |
| 3552 |
|
| 3553 |
destructor TJSONObject.Destroy; |
| 3554 |
begin |
| 3555 |
{$IFDEF PAS2JS} |
| 3556 |
FHash:=nil; |
| 3557 |
{$else} |
| 3558 |
FreeAndNil(FHash); |
| 3559 |
{$ENDIF} |
| 3560 |
inherited Destroy; |
| 3561 |
end; |
| 3562 |
|
| 3563 |
class function TJSONObject.JSONType: TJSONType; |
| 3564 |
begin |
| 3565 |
Result:=jtObject; |
| 3566 |
end; |
| 3567 |
|
| 3568 |
function TJSONObject.Clone: TJSONData; |
| 3569 |
|
| 3570 |
Var |
| 3571 |
O : TJSONObject; |
| 3572 |
I: Integer; |
| 3573 |
|
| 3574 |
begin |
| 3575 |
O:=TJSONObjectClass(ClassType).Create; |
| 3576 |
try |
| 3577 |
For I:=0 to Count-1 do |
| 3578 |
O.Add(Self.Names[I],Self.Items[I].Clone); |
| 3579 |
Result:=O; |
| 3580 |
except |
| 3581 |
FreeAndNil(O); |
| 3582 |
Raise; |
| 3583 |
end; |
| 3584 |
end; |
| 3585 |
|
| 3586 |
function TJSONObject.GetEnumerator: TBaseJSONEnumerator; |
| 3587 |
begin |
| 3588 |
Result:=TJSONObjectEnumerator.Create(Self); |
| 3589 |
end; |
| 3590 |
|
| 3591 |
|
| 3592 |
function TJSONObject.DoFormatJSON(Options: TFormatOptions; CurrentIndent, |
| 3593 |
Indent: Integer): TJSONStringType; |
| 3594 |
|
| 3595 |
Var |
| 3596 |
i : Integer; |
| 3597 |
S : TJSONStringType; |
| 3598 |
MultiLine,UseQuotes, SkipWhiteSpace,SkipWhiteSpaceOnlyLeading : Boolean; |
| 3599 |
NSep,Sep,Ind : String; |
| 3600 |
V : TJSONStringType; |
| 3601 |
D : TJSONData; |
| 3602 |
|
| 3603 |
begin |
| 3604 |
Result:=''; |
| 3605 |
UseQuotes:=Not (foDoNotQuoteMembers in options); |
| 3606 |
MultiLine:=Not (foSingleLineObject in Options); |
| 3607 |
SkipWhiteSpace:=foSkipWhiteSpace in Options; |
| 3608 |
SkipWhiteSpaceOnlyLeading:=foSkipWhiteSpaceOnlyLeading in Options; |
| 3609 |
CurrentIndent:=CurrentIndent+Indent; |
| 3610 |
Ind:=IndentString(Options, CurrentIndent); |
| 3611 |
If SkipWhiteSpace then |
| 3612 |
begin |
| 3613 |
if SkipWhiteSpaceOnlyLeading then |
| 3614 |
NSep:=': ' |
| 3615 |
else |
| 3616 |
NSep:=':' |
| 3617 |
end |
| 3618 |
else |
| 3619 |
NSep:=' : '; |
| 3620 |
If MultiLine then |
| 3621 |
Sep:=','+SLineBreak+Ind |
| 3622 |
else if SkipWhiteSpace then |
| 3623 |
Sep:=',' |
| 3624 |
else |
| 3625 |
Sep:=', '; |
| 3626 |
For I:=0 to Count-1 do |
| 3627 |
begin |
| 3628 |
If (I>0) then |
| 3629 |
Result:=Result+Sep |
| 3630 |
else If MultiLine then |
| 3631 |
Result:=Result+Ind; |
| 3632 |
S:=StringToJSONString(Names[i]); |
| 3633 |
If UseQuotes then |
| 3634 |
S:='"'+S+'"'; |
| 3635 |
D:=Items[i]; |
| 3636 |
if D=Nil then |
| 3637 |
V:='null' |
| 3638 |
else |
| 3639 |
v:=Items[I].DoFormatJSON(Options,CurrentIndent,Indent); |
| 3640 |
Result:=Result+S+NSep+V; |
| 3641 |
end; |
| 3642 |
If (Result<>'') then |
| 3643 |
begin |
| 3644 |
if MultiLine then |
| 3645 |
Result:='{'+sLineBreak+Result+sLineBreak+indentString(options,CurrentIndent-Indent)+'}' |
| 3646 |
else |
| 3647 |
Result:=ObjStartSeps[SkipWhiteSpace]+Result+ObjEndSeps[SkipWhiteSpace] |
| 3648 |
end |
| 3649 |
else |
| 3650 |
Result:='{}'; |
| 3651 |
end; |
| 3652 |
|
| 3653 |
procedure TJSONObject.Iterate(Iterator: TJSONObjectIterator; Data: TObject); |
| 3654 |
{$IFDEF PAS2JS} |
| 3655 |
var |
| 3656 |
i: Integer; |
| 3657 |
Cont: Boolean; |
| 3658 |
begin |
| 3659 |
if FNames=nil then |
| 3660 |
FNames:=TJSObject.getOwnPropertyNames(FHash); |
| 3661 |
Cont:=True; |
| 3662 |
for i:=0 to length(FNames) do |
| 3663 |
begin |
| 3664 |
Iterator(copy(FNames[I],2),TJSONData(FHash[FNames[i]]),Data,Cont); |
| 3665 |
if not Cont then break; |
| 3666 |
end; |
| 3667 |
end; |
| 3668 |
{$else} |
| 3669 |
Var |
| 3670 |
I : Integer; |
| 3671 |
Cont : Boolean; |
| 3672 |
|
| 3673 |
begin |
| 3674 |
I:=0; |
| 3675 |
Cont:=True; |
| 3676 |
While (I<FHash.Count) and Cont do |
| 3677 |
begin |
| 3678 |
Iterator(Names[I],Items[i],Data,Cont); |
| 3679 |
Inc(I); |
| 3680 |
end; |
| 3681 |
end; |
| 3682 |
{$ENDIF} |
| 3683 |
|
| 3684 |
function TJSONObject.IndexOf(Item: TJSONData): Integer; |
| 3685 |
begin |
| 3686 |
{$IFDEF PAS2JS} |
| 3687 |
if FNames=nil then |
| 3688 |
FNames:=TJSObject.getOwnPropertyNames(FHash); |
| 3689 |
for Result:=0 to length(FNames)-1 do |
| 3690 |
if TJSONData(FHash[FNames[Result]])=Item then exit; |
| 3691 |
Result:=-1; |
| 3692 |
{$else} |
| 3693 |
Result:=FHash.IndexOf(Item); |
| 3694 |
{$ENDIF} |
| 3695 |
end; |
| 3696 |
|
| 3697 |
function TJSONObject.IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer; |
| 3698 |
begin |
| 3699 |
{$IFDEF PAS2JS} |
| 3700 |
if FNames=nil then |
| 3701 |
FNames:=TJSObject.getOwnPropertyNames(FHash); |
| 3702 |
Result:=TJSArray(FNames).indexOf('%'+AName); // -1 if not found |
| 3703 |
{$else} |
| 3704 |
Result:=FHash.FindIndexOf(AName); |
| 3705 |
{$ENDIF} |
| 3706 |
if (Result<0) and CaseInsensitive then |
| 3707 |
begin |
| 3708 |
Result:=Count-1; |
| 3709 |
While (Result>=0) and (CompareText(Names[Result],AName)<>0) do |
| 3710 |
Dec(Result); |
| 3711 |
end; |
| 3712 |
end; |
| 3713 |
|
| 3714 |
procedure TJSONObject.Clear; |
| 3715 |
begin |
| 3716 |
{$IFDEF PAS2JS} |
| 3717 |
FCount:=0; |
| 3718 |
FHash:=TJSObject.new; |
| 3719 |
FNames:=nil; |
| 3720 |
{$else} |
| 3721 |
FHash.Clear; |
| 3722 |
{$ENDIF} |
| 3723 |
end; |
| 3724 |
|
| 3725 |
function TJSONObject.DoAdd(const AName: TJSONStringType; AValue: TJSONData; FreeOnError : Boolean = True): Integer; |
| 3726 |
begin |
| 3727 |
if {$IFDEF PAS2JS}FHash.hasOwnProperty('%'+AName){$else}(IndexOfName(aName)<>-1){$ENDIF} then |
| 3728 |
begin |
| 3729 |
if FreeOnError then |
| 3730 |
FreeAndNil(AValue); |
| 3731 |
DoError(SErrDuplicateValue,[aName]); |
| 3732 |
end; |
| 3733 |
{$IFDEF PAS2JS} |
| 3734 |
FHash['%'+AName]:=AValue; |
| 3735 |
FNames:=nil; |
| 3736 |
inc(FCount); |
| 3737 |
Result:=FCount; |
| 3738 |
{$else} |
| 3739 |
Result:=FHash.Add(AName,AValue); |
| 3740 |
{$ENDIF} |
| 3741 |
end; |
| 3742 |
|
| 3743 |
function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONData |
| 3744 |
): Integer; |
| 3745 |
begin |
| 3746 |
Result:=DoAdd(aName,AValue,False); |
| 3747 |
end; |
| 3748 |
|
| 3749 |
function TJSONObject.Add(const AName: TJSONStringType; AValue: Boolean |
| 3750 |
): Integer; |
| 3751 |
begin |
| 3752 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3753 |
end; |
| 3754 |
|
| 3755 |
function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer; |
| 3756 |
begin |
| 3757 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3758 |
end; |
| 3759 |
|
| 3760 |
function TJSONObject.Add(const AName, AValue: TJSONStringType): Integer; |
| 3761 |
begin |
| 3762 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3763 |
end; |
| 3764 |
|
| 3765 |
function TJSONObject.Add(const AName: TJSONStringType; Avalue: Integer): Integer; |
| 3766 |
begin |
| 3767 |
Result:=DoAdd(AName,CreateJSON(AValue)); |
| 3768 |
end; |
| 3769 |
|
| 3770 |
|
| 3771 |
function TJSONObject.Add(const AName: TJSONStringType): Integer; |
| 3772 |
begin |
| 3773 |
Result:=DoAdd(AName,CreateJSON); |
| 3774 |
end; |
| 3775 |
|
| 3776 |
function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONArray |
| 3777 |
): Integer; |
| 3778 |
begin |
| 3779 |
Result:=DoAdd(AName,TJSONData(AValue),False); |
| 3780 |
end; |
| 3781 |
|
| 3782 |
procedure TJSONObject.Delete(Index: Integer); |
| 3783 |
begin |
| 3784 |
{$IFDEF PAS2JS} |
| 3785 |
if (Index<0) or (Index>=FCount) then |
| 3786 |
DoError(SListIndexError,[Index]); |
| 3787 |
JSDelete(FHash,'%'+GetNameOf(Index)); |
| 3788 |
FNames:=nil; |
| 3789 |
dec(FCount); |
| 3790 |
{$else} |
| 3791 |
FHash.Delete(Index); |
| 3792 |
{$ENDIF} |
| 3793 |
end; |
| 3794 |
|
| 3795 |
procedure TJSONObject.Delete(const AName: string); |
| 3796 |
{$IFDEF PAS2JS} |
| 3797 |
begin |
| 3798 |
if not FHash.hasOwnProperty('%'+AName) then exit; |
| 3799 |
JSDelete(FHash,'%'+AName); |
| 3800 |
FNames:=nil; |
| 3801 |
dec(FCount); |
| 3802 |
end; |
| 3803 |
{$else} |
| 3804 |
Var |
| 3805 |
I : Integer; |
| 3806 |
|
| 3807 |
begin |
| 3808 |
I:=IndexOfName(AName); |
| 3809 |
if (I<>-1) then |
| 3810 |
Delete(I); |
| 3811 |
end; |
| 3812 |
{$ENDIF} |
| 3813 |
|
| 3814 |
procedure TJSONObject.Remove(Item: TJSONData); |
| 3815 |
{$IFDEF PAS2JS} |
| 3816 |
var AName: String; |
| 3817 |
begin |
| 3818 |
for AName in FHash do |
| 3819 |
if FHash.hasOwnProperty(AName) and (FHash[AName]=Item) then |
| 3820 |
begin |
| 3821 |
JSDelete(FHash,AName); |
| 3822 |
FNames:=nil; |
| 3823 |
dec(FCount); |
| 3824 |
exit; |
| 3825 |
end; |
| 3826 |
end; |
| 3827 |
{$else} |
| 3828 |
begin |
| 3829 |
FHash.Remove(Item); |
| 3830 |
end; |
| 3831 |
{$ENDIF} |
| 3832 |
|
| 3833 |
|
| 3834 |
function TJSONObject.Extract(Index: Integer): TJSONData; |
| 3835 |
{$IFDEF PAS2JS} |
| 3836 |
|
| 3837 |
Var |
| 3838 |
N : String; |
| 3839 |
begin |
| 3840 |
N:=GetNameOf(Index); |
| 3841 |
Result:=Extract(N); |
| 3842 |
end; |
| 3843 |
{$ELSE} |
| 3844 |
begin |
| 3845 |
Result:=Items[Index]; |
| 3846 |
FHash.Extract(Result); |
| 3847 |
end; |
| 3848 |
{$ENDIF} |
| 3849 |
|
| 3850 |
function TJSONObject.Extract(const AName: string): TJSONData; |
| 3851 |
|
| 3852 |
|
| 3853 |
{$IFDEF PAS2JS} |
| 3854 |
begin |
| 3855 |
if FHash.hasOwnProperty('%'+AName) then |
| 3856 |
begin |
| 3857 |
Result:=TJSONData(FHash['%'+AName]); |
| 3858 |
FHash['%'+AName]:=undefined; |
| 3859 |
Dec(FCount); |
| 3860 |
end |
| 3861 |
else |
| 3862 |
Result:=nil; |
| 3863 |
end; |
| 3864 |
{$ELSE} |
| 3865 |
Var |
| 3866 |
I : Integer; |
| 3867 |
|
| 3868 |
begin |
| 3869 |
I:=IndexOfName(AName); |
| 3870 |
if (I<>-1) then |
| 3871 |
Result:=Extract(I) |
| 3872 |
else |
| 3873 |
Result:=Nil |
| 3874 |
end; |
| 3875 |
{$ENDIF} |
| 3876 |
|
| 3877 |
function TJSONObject.Get(const AName: String): TJSONVariant; |
| 3878 |
{$IFDEF PAS2JS} |
| 3879 |
begin |
| 3880 |
if FHash.hasOwnProperty('%'+AName) then |
| 3881 |
Result:=TJSONData(FHash['%'+AName]).Value |
| 3882 |
else |
| 3883 |
Result:=nil; |
| 3884 |
end; |
| 3885 |
{$else} |
| 3886 |
Var |
| 3887 |
I : Integer; |
| 3888 |
|
| 3889 |
begin |
| 3890 |
I:=IndexOfName(AName); |
| 3891 |
If (I<>-1) then |
| 3892 |
Result:=Items[i].Value |
| 3893 |
else |
| 3894 |
Result:=Null; |
| 3895 |
end; |
| 3896 |
{$ENDIF} |
| 3897 |
|
| 3898 |
function TJSONObject.Get(const AName: String; ADefault: TJSONFloat |
| 3899 |
): TJSONFloat; |
| 3900 |
|
| 3901 |
Var |
| 3902 |
D : TJSONData; |
| 3903 |
|
| 3904 |
begin |
| 3905 |
D:=Find(AName,jtNumber); |
| 3906 |
If D<>Nil then |
| 3907 |
Result:=D.AsFloat |
| 3908 |
else |
| 3909 |
Result:=ADefault; |
| 3910 |
end; |
| 3911 |
|
| 3912 |
function TJSONObject.Get(const AName: String; ADefault: Integer |
| 3913 |
): Integer; |
| 3914 |
|
| 3915 |
Var |
| 3916 |
D : TJSONData; |
| 3917 |
|
| 3918 |
begin |
| 3919 |
D:=Find(AName,jtNumber); |
| 3920 |
If D<>Nil then |
| 3921 |
Result:=D.AsInteger |
| 3922 |
else |
| 3923 |
Result:=ADefault; |
| 3924 |
end; |
| 3925 |
|
| 3926 |
{$IFNDEF PAS2JS} |
| 3927 |
function TJSONObject.Get(const AName: String; ADefault: Int64): Int64; |
| 3928 |
Var |
| 3929 |
D : TJSONData; |
| 3930 |
|
| 3931 |
begin |
| 3932 |
D:=Find(AName,jtNumber); |
| 3933 |
If D<>Nil then |
| 3934 |
Result:=D.AsInt64 |
| 3935 |
else |
| 3936 |
Result:=ADefault; |
| 3937 |
end; |
| 3938 |
|
| 3939 |
function TJSONObject.Get(const AName: String; ADefault: QWord): QWord; |
| 3940 |
Var |
| 3941 |
D : TJSONData; |
| 3942 |
|
| 3943 |
begin |
| 3944 |
D:=Find(AName,jtNumber); |
| 3945 |
If D<>Nil then |
| 3946 |
Result:=D.AsQWord |
| 3947 |
else |
| 3948 |
Result:=ADefault; |
| 3949 |
end; |
| 3950 |
{$ENDIF} |
| 3951 |
|
| 3952 |
function TJSONObject.Get(const AName: String; ADefault: Boolean |
| 3953 |
): Boolean; |
| 3954 |
Var |
| 3955 |
D : TJSONData; |
| 3956 |
|
| 3957 |
begin |
| 3958 |
D:=Find(AName,jtBoolean); |
| 3959 |
If D<>Nil then |
| 3960 |
Result:=D.AsBoolean |
| 3961 |
else |
| 3962 |
Result:=ADefault; |
| 3963 |
end; |
| 3964 |
|
| 3965 |
function TJSONObject.Get(const AName: String; ADefault: TJSONStringType |
| 3966 |
): TJSONStringType; |
| 3967 |
Var |
| 3968 |
D : TJSONData; |
| 3969 |
|
| 3970 |
begin |
| 3971 |
D:=Find(AName,jtString); |
| 3972 |
If (D<>Nil) then |
| 3973 |
Result:=D.AsString |
| 3974 |
else |
| 3975 |
Result:=ADefault; |
| 3976 |
end; |
| 3977 |
|
| 3978 |
{$IFNDEF PAS2JS} |
| 3979 |
function TJSONObject.Get(const AName: String; ADefault: TJSONUnicodeStringType |
| 3980 |
): TJSONUnicodeStringType; |
| 3981 |
Var |
| 3982 |
D : TJSONData; |
| 3983 |
|
| 3984 |
begin |
| 3985 |
D:=Find(AName,jtString); |
| 3986 |
If (D<>Nil) then |
| 3987 |
Result:=D.AsUnicodeString |
| 3988 |
else |
| 3989 |
Result:=ADefault; |
| 3990 |
end; |
| 3991 |
{$ENDIF} |
| 3992 |
|
| 3993 |
function TJSONObject.Get(const AName: String; ADefault: TJSONArray |
| 3994 |
): TJSONArray; |
| 3995 |
Var |
| 3996 |
D : TJSONData; |
| 3997 |
|
| 3998 |
begin |
| 3999 |
D:=Find(AName,jtArray); |
| 4000 |
If (D<>Nil) then |
| 4001 |
Result:=TJSONArray(D) |
| 4002 |
else |
| 4003 |
Result:=ADefault; |
| 4004 |
end; |
| 4005 |
|
| 4006 |
function TJSONObject.Get(const AName: String; ADefault: TJSONObject |
| 4007 |
): TJSONObject; |
| 4008 |
Var |
| 4009 |
D : TJSONData; |
| 4010 |
|
| 4011 |
begin |
| 4012 |
D:=Find(AName,jtObject); |
| 4013 |
If (D<>Nil) then |
| 4014 |
Result:=TJSONObject(D) |
| 4015 |
else |
| 4016 |
Result:=ADefault; |
| 4017 |
end; |
| 4018 |
|
| 4019 |
function TJSONObject.Find(const AName: String): TJSONData; |
| 4020 |
{$IFDEF PAS2JS} |
| 4021 |
begin |
| 4022 |
if FHash.hasOwnProperty('%'+AName) then |
| 4023 |
Result:=TJSONData(FHash['%'+AName]) |
| 4024 |
else |
| 4025 |
Result:=nil; |
| 4026 |
end; |
| 4027 |
{$else} |
| 4028 |
Var |
| 4029 |
I : Integer; |
| 4030 |
|
| 4031 |
begin |
| 4032 |
I:=IndexOfName(AName); |
| 4033 |
If (I<>-1) then |
| 4034 |
Result:=Items[i] |
| 4035 |
else |
| 4036 |
Result:=Nil; |
| 4037 |
end; |
| 4038 |
{$ENDIF} |
| 4039 |
|
| 4040 |
function TJSONObject.Find(const AName: String; AType: TJSONType): TJSONData; |
| 4041 |
begin |
| 4042 |
Result:=Find(AName); |
| 4043 |
If Assigned(Result) and (Result.JSONType<>AType) then |
| 4044 |
Result:=Nil; |
| 4045 |
end; |
| 4046 |
|
| 4047 |
function TJSONObject.Find(const key: TJSONStringType; out AValue: TJSONData): boolean; |
| 4048 |
begin |
| 4049 |
AValue := Find(key); |
| 4050 |
Result := assigned(AValue); |
| 4051 |
end; |
| 4052 |
|
| 4053 |
function TJSONObject.Find(const key: TJSONStringType; out AValue: TJSONObject): boolean; |
| 4054 |
var |
| 4055 |
v: TJSONData; |
| 4056 |
begin |
| 4057 |
v := Find(key); |
| 4058 |
Result := assigned(v) and (v.JSONType = jtObject); |
| 4059 |
if Result then |
| 4060 |
AValue := TJSONObject(v); |
| 4061 |
end; |
| 4062 |
|
| 4063 |
function TJSONObject.Find(const key: TJSONStringType; out AValue: TJSONArray): boolean; |
| 4064 |
var |
| 4065 |
v: TJSONData; |
| 4066 |
begin |
| 4067 |
v := Find(key); |
| 4068 |
Result := assigned(v) and (v.JSONType = jtArray); |
| 4069 |
if Result then |
| 4070 |
AValue := TJSONArray(v); |
| 4071 |
end; |
| 4072 |
|
| 4073 |
function TJSONObject.Find(const key: TJSONStringType; out AValue: TJSONString): boolean; |
| 4074 |
var |
| 4075 |
v: TJSONData; |
| 4076 |
begin |
| 4077 |
v := Find(key); |
| 4078 |
Result := assigned(v) and (v.JSONType = jtString); |
| 4079 |
if Result then |
| 4080 |
AValue := TJSONString(v); |
| 4081 |
end; |
| 4082 |
|
| 4083 |
function TJSONObject.Find(const key: TJSONStringType; out AValue: TJSONBoolean): boolean; |
| 4084 |
var |
| 4085 |
v: TJSONData; |
| 4086 |
begin |
| 4087 |
v := Find(key); |
| 4088 |
Result := assigned(v) and (v.JSONType = jtBoolean); |
| 4089 |
if Result then |
| 4090 |
AValue := TJSONBoolean(v); |
| 4091 |
end; |
| 4092 |
|
| 4093 |
function TJSONObject.Find(const key: TJSONStringType; out AValue: TJSONNumber): boolean; |
| 4094 |
var |
| 4095 |
v: TJSONData; |
| 4096 |
begin |
| 4097 |
v := Find(key); |
| 4098 |
Result := assigned(v) and (v.JSONType = jtNumber); |
| 4099 |
if Result then |
| 4100 |
AValue := TJSONNumber(v); |
| 4101 |
end; |
| 4102 |
|
| 4103 |
initialization |
| 4104 |
// Need to force initialization; |
| 4105 |
TJSONData.DetermineElementSeparators; |
| 4106 |
TJSONObject.DetermineElementQuotes; |
| 4107 |
end. |
| 4108 |
|