fpjson.pp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. {
  2. This file is part of the Free Component Library
  3. JSON Data structures
  4. Copyright (c) 2007 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. unit fpjson;
  14. interface
  15. uses
  16. variants,
  17. SysUtils,
  18. classes,
  19. contnrs;
  20. type
  21. TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject);
  22. TJSONFloat = Double;
  23. TJSONStringType = AnsiString;
  24. TJSONCharType = AnsiChar;
  25. PJSONCharType = ^TJSONCharType;
  26. TFormatOption = (foSingleLineArray, // Array without CR/LF : all on one line
  27. foSingleLineObject, // Object without CR/LF : all on one line
  28. foDoNotQuoteMembers, // Do not quote object member names.
  29. foUseTabchar); // Use tab characters instead of spaces.
  30. TFormatOptions = set of TFormatOption;
  31. Const
  32. DefaultIndentSize = 2;
  33. DefaultFormat = [];
  34. AsJSONFormat = [foSingleLineArray,foSingleLineObject]; // These options make FormatJSON behave as AsJSON
  35. Type
  36. { TJSONData }
  37. TJSONData = class(TObject)
  38. protected
  39. function GetAsBoolean: Boolean; virtual; abstract;
  40. function GetAsFloat: TJSONFloat; virtual; abstract;
  41. function GetAsInteger: Integer; virtual; abstract;
  42. function GetAsInt64: Int64; virtual; abstract;
  43. function GetIsNull: Boolean; virtual;
  44. procedure SetAsBoolean(const AValue: Boolean); virtual; abstract;
  45. procedure SetAsFloat(const AValue: TJSONFloat); virtual; abstract;
  46. procedure SetAsInteger(const AValue: Integer); virtual; abstract;
  47. procedure SetAsInt64(const AValue: Int64); virtual; abstract;
  48. function GetAsJSON: TJSONStringType; virtual; abstract;
  49. function GetAsString: TJSONStringType; virtual; abstract;
  50. procedure SetAsString(const AValue: TJSONStringType); virtual; abstract;
  51. function GetValue: variant; virtual; abstract;
  52. procedure SetValue(const AValue: variant); virtual; abstract;
  53. function GetItem(Index : Integer): TJSONData; virtual;
  54. procedure SetItem(Index : Integer; const AValue: TJSONData); virtual;
  55. Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; virtual;
  56. function GetCount: Integer; virtual;
  57. public
  58. Constructor Create; virtual;
  59. Class function JSONType: TJSONType; virtual;
  60. Procedure Clear; virtual; Abstract;
  61. Function Clone : TJSONData; virtual; abstract;
  62. Function FormatJSON(Options : TFormatOptions = DefaultFormat; Indentsize : Integer = DefaultIndentSize) : TJSONStringType;
  63. property Count: Integer read GetCount;
  64. property Items[Index: Integer]: TJSONData read GetItem write SetItem;
  65. property Value: variant read GetValue write SetValue;
  66. Property AsString : TJSONStringType Read GetAsString Write SetAsString;
  67. Property AsFloat : TJSONFloat Read GetAsFloat Write SetAsFloat;
  68. Property AsInteger : Integer Read GetAsInteger Write SetAsInteger;
  69. Property AsInt64 : Int64 Read GetAsInt64 Write SetAsInt64;
  70. Property AsBoolean : Boolean Read GetAsBoolean Write SetAsBoolean;
  71. Property IsNull : Boolean Read GetIsNull;
  72. Property AsJSON : TJSONStringType Read GetAsJSON;
  73. end;
  74. TJSONDataClass = Class of TJSONData;
  75. TJSONNumberType = (ntFloat,ntInteger,ntInt64);
  76. TJSONNumber = class(TJSONData)
  77. protected
  78. public
  79. class function JSONType: TJSONType; override;
  80. class function NumberType : TJSONNumberType; virtual; abstract;
  81. end;
  82. { TJSONFloatNumber }
  83. TJSONFloatNumber = class(TJSONNumber)
  84. Private
  85. FValue : TJSONFloat;
  86. protected
  87. function GetAsBoolean: Boolean; override;
  88. function GetAsFloat: TJSONFloat; override;
  89. function GetAsInteger: Integer; override;
  90. function GetAsInt64: Int64; override;
  91. procedure SetAsBoolean(const AValue: Boolean); override;
  92. procedure SetAsFloat(const AValue: TJSONFloat); override;
  93. procedure SetAsInteger(const AValue: Integer); override;
  94. procedure SetAsInt64(const AValue: Int64); override;
  95. function GetAsJSON: TJSONStringType; override;
  96. function GetAsString: TJSONStringType; override;
  97. procedure SetAsString(const AValue: TJSONStringType); override;
  98. function GetValue: variant; override;
  99. procedure SetValue(const AValue: variant); override;
  100. public
  101. Constructor Create(AValue : TJSONFloat); reintroduce;
  102. class function NumberType : TJSONNumberType; override;
  103. Procedure Clear; override;
  104. Function Clone : TJSONData; override;
  105. end;
  106. { TJSONIntegerNumber }
  107. TJSONIntegerNumber = class(TJSONNumber)
  108. Private
  109. FValue : Integer;
  110. protected
  111. function GetAsBoolean: Boolean; override;
  112. function GetAsFloat: TJSONFloat; override;
  113. function GetAsInteger: Integer; override;
  114. function GetAsInt64: Int64; override;
  115. procedure SetAsBoolean(const AValue: Boolean); override;
  116. procedure SetAsFloat(const AValue: TJSONFloat); override;
  117. procedure SetAsInteger(const AValue: Integer); override;
  118. procedure SetAsInt64(const AValue: Int64); override;
  119. function GetAsJSON: TJSONStringType; override;
  120. function GetAsString: TJSONStringType; override;
  121. procedure SetAsString(const AValue: TJSONStringType); override;
  122. function GetValue: variant; override;
  123. procedure SetValue(const AValue: variant); override;
  124. public
  125. Constructor Create(AValue : Integer); reintroduce;
  126. class function NumberType : TJSONNumberType; override;
  127. Procedure Clear; override;
  128. Function Clone : TJSONData; override;
  129. end;
  130. { TJSONInt64Number }
  131. TJSONInt64Number = class(TJSONNumber)
  132. Private
  133. FValue : Int64;
  134. protected
  135. function GetAsBoolean: Boolean; override;
  136. function GetAsFloat: TJSONFloat; override;
  137. function GetAsInteger: Integer; override;
  138. function GetAsInt64: Int64; override;
  139. procedure SetAsBoolean(const AValue: Boolean); override;
  140. procedure SetAsFloat(const AValue: TJSONFloat); override;
  141. procedure SetAsInteger(const AValue: Integer); override;
  142. procedure SetAsInt64(const AValue: Int64); override;
  143. function GetAsJSON: TJSONStringType; override;
  144. function GetAsString: TJSONStringType; override;
  145. procedure SetAsString(const AValue: TJSONStringType); override;
  146. function GetValue: variant; override;
  147. procedure SetValue(const AValue: variant); override;
  148. public
  149. Constructor Create(AValue : Int64); reintroduce;
  150. class function NumberType : TJSONNumberType; override;
  151. Procedure Clear; override;
  152. Function Clone : TJSONData; override;
  153. end;
  154. { TJSONString }
  155. TJSONString = class(TJSONData)
  156. Private
  157. FValue: TJSONStringType;
  158. protected
  159. function GetValue: Variant; override;
  160. procedure SetValue(const AValue: Variant); override;
  161. function GetAsBoolean: Boolean; override;
  162. function GetAsFloat: TJSONFloat; override;
  163. function GetAsInteger: Integer; override;
  164. function GetAsInt64: Int64; override;
  165. procedure SetAsBoolean(const AValue: Boolean); override;
  166. procedure SetAsFloat(const AValue: TJSONFloat); override;
  167. procedure SetAsInteger(const AValue: Integer); override;
  168. procedure SetAsInt64(const AValue: Int64); override;
  169. function GetAsJSON: TJSONStringType; override;
  170. function GetAsString: TJSONStringType; override;
  171. procedure SetAsString(const AValue: TJSONStringType); override;
  172. public
  173. Constructor Create(const AValue : TJSONStringType); reintroduce;
  174. class function JSONType: TJSONType; override;
  175. Procedure Clear; override;
  176. Function Clone : TJSONData; override;
  177. end;
  178. { TJSONboolean }
  179. TJSONBoolean = class(TJSONData)
  180. Private
  181. FValue: Boolean;
  182. protected
  183. function GetValue: Variant; override;
  184. procedure SetValue(const AValue: Variant); override;
  185. function GetAsBoolean: Boolean; override;
  186. function GetAsFloat: TJSONFloat; override;
  187. function GetAsInteger: Integer; override;
  188. function GetAsInt64: Int64; override;
  189. procedure SetAsBoolean(const AValue: Boolean); override;
  190. procedure SetAsFloat(const AValue: TJSONFloat); override;
  191. procedure SetAsInteger(const AValue: Integer); override;
  192. procedure SetAsInt64(const AValue: Int64); override;
  193. function GetAsJSON: TJSONStringType; override;
  194. function GetAsString: TJSONStringType; override;
  195. procedure SetAsString(const AValue: TJSONStringType); override;
  196. public
  197. Constructor Create(AValue : Boolean); reintroduce;
  198. class function JSONType: TJSONType; override;
  199. Procedure Clear; override;
  200. Function Clone : TJSONData; override;
  201. end;
  202. { TJSONnull }
  203. TJSONNull = class(TJSONData)
  204. protected
  205. Procedure Converterror(From : Boolean);
  206. function GetAsBoolean: Boolean; override;
  207. function GetAsFloat: TJSONFloat; override;
  208. function GetAsInteger: Integer; override;
  209. function GetAsInt64: Int64; override;
  210. function GetIsNull: Boolean; override;
  211. procedure SetAsBoolean(const AValue: Boolean); override;
  212. procedure SetAsFloat(const AValue: TJSONFloat); override;
  213. procedure SetAsInteger(const AValue: Integer); override;
  214. procedure SetAsInt64(const AValue: Int64); override;
  215. function GetAsJSON: TJSONStringType; override;
  216. function GetAsString: TJSONStringType; override;
  217. procedure SetAsString(const AValue: TJSONStringType); override;
  218. function GetValue: variant; override;
  219. procedure SetValue(const AValue: variant); override;
  220. public
  221. class function JSONType: TJSONType; override;
  222. Procedure Clear; override;
  223. Function Clone : TJSONData; override;
  224. end;
  225. TJSONArrayIterator = procedure(Item: TJSONData; Data: TObject; var Continue: Boolean) of object;
  226. { TJSONArray }
  227. TJSONObject = Class;
  228. TJSONArray = class(TJSONData)
  229. Private
  230. FList : TFPObjectList;
  231. function GetArrays(Index : Integer): TJSONArray;
  232. function GetBooleans(Index : Integer): Boolean;
  233. function GetFloats(Index : Integer): TJSONFloat;
  234. function GetIntegers(Index : Integer): Integer;
  235. function GetInt64s(Index : Integer): Int64;
  236. function GetNulls(Index : Integer): Boolean;
  237. function GetObjects(Index : Integer): TJSONObject;
  238. function GetStrings(Index : Integer): TJSONStringType;
  239. function GetTypes(Index : Integer): TJSONType;
  240. procedure SetArrays(Index : Integer; const AValue: TJSONArray);
  241. procedure SetBooleans(Index : Integer; const AValue: Boolean);
  242. procedure SetFloats(Index : Integer; const AValue: TJSONFloat);
  243. procedure SetIntegers(Index : Integer; const AValue: Integer);
  244. procedure SetInt64s(Index : Integer; const AValue: Int64);
  245. procedure SetObjects(Index : Integer; const AValue: TJSONObject);
  246. procedure SetStrings(Index : Integer; const AValue: TJSONStringType);
  247. protected
  248. Procedure Converterror(From : Boolean);
  249. function GetAsBoolean: Boolean; override;
  250. function GetAsFloat: TJSONFloat; override;
  251. function GetAsInteger: Integer; override;
  252. function GetAsInt64: Int64; override;
  253. procedure SetAsBoolean(const AValue: Boolean); override;
  254. procedure SetAsFloat(const AValue: TJSONFloat); override;
  255. procedure SetAsInteger(const AValue: Integer); override;
  256. procedure SetAsInt64(const AValue: Int64); override;
  257. function GetAsJSON: TJSONStringType; override;
  258. function GetAsString: TJSONStringType; override;
  259. procedure SetAsString(const AValue: TJSONStringType); override;
  260. function GetValue: variant; override;
  261. procedure SetValue(const AValue: variant); override;
  262. function GetCount: Integer; override;
  263. function GetItem(Index : Integer): TJSONData; override;
  264. procedure SetItem(Index : Integer; const AValue: TJSONData); override;
  265. Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override;
  266. public
  267. Constructor Create; overload; reintroduce;
  268. Constructor Create(const Elements : Array of Const); overload;
  269. Destructor Destroy; override;
  270. class function JSONType: TJSONType; override;
  271. Function Clone : TJSONData; override;
  272. // Examine
  273. procedure Iterate(Iterator : TJSONArrayIterator; Data: TObject);
  274. function IndexOf(obj: TJSONData): Integer;
  275. // Manipulate
  276. Procedure Clear; override;
  277. function Add(Item : TJSONData): Integer;
  278. function Add(I : Integer): Integer;
  279. function Add(I : Int64): Int64;
  280. function Add(const S : String): Integer;
  281. function Add: Integer;
  282. function Add(F : TJSONFloat): Integer;
  283. function Add(B : Boolean): Integer;
  284. function Add(AnArray : TJSONArray): Integer;
  285. function Add(AnObject: TJSONObject): Integer;
  286. Procedure Delete(Index : Integer);
  287. procedure Exchange(Index1, Index2: Integer);
  288. procedure Insert(Index: Integer);
  289. procedure Insert(Index: Integer; Item : TJSONData);
  290. procedure Insert(Index: Integer; I : Integer);
  291. procedure Insert(Index: Integer; I : Int64);
  292. procedure Insert(Index: Integer; const S : String);
  293. procedure Insert(Index: Integer; F : TJSONFloat);
  294. procedure Insert(Index: Integer; B : Boolean);
  295. procedure Insert(Index: Integer; AnArray : TJSONArray);
  296. procedure Insert(Index: Integer; AnObject: TJSONObject);
  297. procedure Move(CurIndex, NewIndex: Integer);
  298. Procedure Remove(Item : TJSONData);
  299. // Easy Access Properties.
  300. property Items;default;
  301. Property Types[Index : Integer] : TJSONType Read GetTypes;
  302. Property Nulls[Index : Integer] : Boolean Read GetNulls;
  303. Property Integers[Index : Integer] : Integer Read GetIntegers Write SetIntegers;
  304. Property Int64s[Index : Integer] : Int64 Read GetInt64s Write SetInt64s;
  305. Property Strings[Index : Integer] : TJSONStringType Read GetStrings Write SetStrings;
  306. Property Floats[Index : Integer] : TJSONFloat Read GetFloats Write SetFloats;
  307. Property Booleans[Index : Integer] : Boolean Read GetBooleans Write SetBooleans;
  308. Property Arrays[Index : Integer] : TJSONArray Read GetArrays Write SetArrays;
  309. Property Objects[Index : Integer] : TJSONObject Read GetObjects Write SetObjects;
  310. end;
  311. TJSONObjectIterator = procedure(Const AName : TJSONStringType; Item: TJSONData; Data: TObject; var Continue: Boolean) of object;
  312. { TJSONObject }
  313. TJSONObject = class(TJSONData)
  314. private
  315. FHash : TFPHashObjectList; // Careful : Names limited to 255 chars.
  316. function GetArrays(const AName : String): TJSONArray;
  317. function GetBooleans(const AName : String): Boolean;
  318. function GetElements(const AName: string): TJSONData;
  319. function GetFloats(const AName : String): TJSONFloat;
  320. function GetIntegers(const AName : String): Integer;
  321. function GetInt64s(const AName : String): Int64;
  322. function GetIsNull(const AName : String): Boolean; reintroduce;
  323. function GetNameOf(Index : Integer): TJSONStringType;
  324. function GetObjects(const AName : String): TJSONObject;
  325. function GetStrings(const AName : String): TJSONStringType;
  326. function GetTypes(const AName : String): TJSONType;
  327. procedure SetArrays(const AName : String; const AValue: TJSONArray);
  328. procedure SetBooleans(const AName : String; const AValue: Boolean);
  329. procedure SetElements(const AName: string; const AValue: TJSONData);
  330. procedure SetFloats(const AName : String; const AValue: TJSONFloat);
  331. procedure SetIntegers(const AName : String; const AValue: Integer);
  332. procedure SetInt64s(const AName : String; const AValue: Int64);
  333. procedure SetIsNull(const AName : String; const AValue: Boolean);
  334. procedure SetObjects(const AName : String; const AValue: TJSONObject);
  335. procedure SetStrings(const AName : String; const AValue: TJSONStringType);
  336. protected
  337. Procedure Converterror(From : Boolean);
  338. function GetAsBoolean: Boolean; override;
  339. function GetAsFloat: TJSONFloat; override;
  340. function GetAsInteger: Integer; override;
  341. function GetAsInt64: Int64; override;
  342. procedure SetAsBoolean(const AValue: Boolean); override;
  343. procedure SetAsFloat(const AValue: TJSONFloat); override;
  344. procedure SetAsInteger(const AValue: Integer); override;
  345. procedure SetAsInt64(const AValue: Int64); override;
  346. function GetAsJSON: TJSONStringType; override;
  347. function GetAsString: TJSONStringType; override;
  348. procedure SetAsString(const AValue: TJSONStringType); override;
  349. function GetValue: variant; override;
  350. procedure SetValue(const AValue: variant); override;
  351. function GetCount: Integer; override;
  352. function GetItem(Index : Integer): TJSONData; override;
  353. procedure SetItem(Index : Integer; const AValue: TJSONData); override;
  354. Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override;
  355. public
  356. constructor Create; reintroduce;
  357. Constructor Create(const Elements : Array of Const); overload;
  358. destructor Destroy; override;
  359. class function JSONType: TJSONType; override;
  360. Function Clone : TJSONData; override;
  361. // Examine
  362. procedure Iterate(Iterator : TJSONObjectIterator; Data: TObject);
  363. function IndexOf(Item: TJSONData): Integer;
  364. Function IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer;
  365. // Manipulate
  366. Procedure Clear; override;
  367. function Add(const AName: TJSONStringType; AValue: TJSONData): Integer; overload;
  368. function Add(const AName: TJSONStringType; AValue: Boolean): Integer; overload;
  369. function Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer; overload;
  370. function Add(const AName, AValue: TJSONStringType): Integer; overload;
  371. function Add(const AName: TJSONStringType; Avalue: Integer): Integer; overload;
  372. function Add(const AName: TJSONStringType; Avalue: Int64): Integer; overload;
  373. function Add(const AName: TJSONStringType): Integer; overload;
  374. function Add(const AName: TJSONStringType; AValue : TJSONArray): Integer; overload;
  375. procedure Delete(Index : Integer);
  376. procedure Remove(Item : TJSONData);
  377. Function Extract(Index : Integer) : TJSONData;
  378. // Easy access properties.
  379. property Names[Index : Integer] : TJSONStringType read GetNameOf;
  380. property Elements[AName: string] : TJSONData read GetElements write SetElements; default;
  381. Property Types[AName : String] : TJSONType Read GetTypes;
  382. Property Nulls[AName : String] : Boolean Read GetIsNull Write SetIsNull;
  383. Property Floats[AName : String] : TJSONFloat Read GetFloats Write SetFloats;
  384. Property Integers[AName : String] : Integer Read GetIntegers Write SetIntegers;
  385. Property Int64s[AName : String] : Int64 Read GetInt64s Write SetInt64s;
  386. Property Strings[AName : String] : TJSONStringType Read GetStrings Write SetStrings;
  387. Property Booleans[AName : String] : Boolean Read GetBooleans Write SetBooleans;
  388. Property Arrays[AName : String] : TJSONArray Read GetArrays Write SetArrays;
  389. Property Objects[AName : String] : TJSONObject Read GetObjects Write SetObjects;
  390. end;
  391. EJSON = Class(Exception);
  392. Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
  393. Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;
  394. Function JSONTypeName(JSONType : TJSONType) : String;
  395. implementation
  396. Uses typinfo;
  397. Resourcestring
  398. SErrCannotConvertFromNull = 'Cannot convert data from Null value';
  399. SErrCannotConvertToNull = 'Cannot convert data to Null value';
  400. SErrCannotConvertFromArray = 'Cannot convert data from array value';
  401. SErrCannotConvertToArray = 'Cannot convert data to array value';
  402. SErrCannotConvertFromObject = 'Cannot convert data from object value';
  403. SErrCannotConvertToObject = 'Cannot convert data to object value';
  404. SErrInvalidFloat = 'Invalid float value : %s';
  405. SErrInvalidInteger = 'Invalid float value : %s';
  406. SErrCannotSetNotIsNull = 'IsNull cannot be set to False';
  407. SErrCannotAddArrayTwice = 'Adding an array object to an array twice is not allowed';
  408. SErrCannotAddObjectTwice = 'Adding an object to an array twice is not allowed';
  409. SErrUnknownTypeInConstructor = 'Unknown type in JSON%s constructor: %d';
  410. SErrNotJSONData = 'Cannot add object of type %s to TJSON%s';
  411. SErrPointerNotNil = 'Cannot add non-nil pointer to JSON%s';
  412. SErrOddNumber = 'TJSONObject must be constructed with name,value pairs';
  413. SErrNameMustBeString = 'TJSONObject constructor element name at pos %d is not a string';
  414. SErrNonexistentElement = 'Unknown object member: "%s"';
  415. Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
  416. Var
  417. I,J,L : Integer;
  418. P : PJSONCharType;
  419. begin
  420. I:=1;
  421. J:=1;
  422. Result:='';
  423. L:=Length(S);
  424. P:=PJSONCharType(S);
  425. While I<=L do
  426. begin
  427. if (AnsiChar(P^) in ['"','/','\',#8,#9,#10,#12,#13]) then
  428. begin
  429. Result:=Result+Copy(S,J,I-J);
  430. Case P^ of
  431. '\' : Result:=Result+'\\';
  432. '/' : Result:=Result+'\/';
  433. '"' : Result:=Result+'\"';
  434. #8 : Result:=Result+'\b';
  435. #9 : Result:=Result+'\t';
  436. #10 : Result:=Result+'\n';
  437. #12 : Result:=Result+'\f';
  438. #13 : Result:=Result+'\r';
  439. end;
  440. J:=I+1;
  441. end;
  442. Inc(I);
  443. Inc(P);
  444. end;
  445. Result:=Result+Copy(S,J,I-1);
  446. end;
  447. Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;
  448. Var
  449. I,J,L : Integer;
  450. P : PJSONCharType;
  451. w : String;
  452. begin
  453. I:=1;
  454. J:=1;
  455. L:=Length(S);
  456. Result:='';
  457. P:=PJSONCharType(S);
  458. While (I<=L) do
  459. begin
  460. if (P^='\') then
  461. begin
  462. Result:=Result+Copy(S,J,I-J);
  463. Inc(P);
  464. If (P^<>#0) then
  465. begin
  466. Inc(I);
  467. Case AnsiChar(P^) of
  468. '\','"','/'
  469. : Result:=Result+P^;
  470. 'b' : Result:=Result+#8;
  471. 't' : Result:=Result+#9;
  472. 'n' : Result:=Result+#10;
  473. 'f' : Result:=Result+#12;
  474. 'r' : Result:=Result+#13;
  475. 'u' : begin
  476. W:=Copy(S,I+1,4);
  477. Inc(I,4);
  478. Inc(P,4);
  479. Result:=Result+WideChar(StrToInt('$'+W));
  480. end;
  481. end;
  482. end;
  483. J:=I+1;
  484. end;
  485. Inc(I);
  486. Inc(P);
  487. end;
  488. Result:=Result+Copy(S,J,I-J+1);
  489. end;
  490. function JSONTypeName(JSONType: TJSONType): String;
  491. begin
  492. Result:=GetEnumName(TypeInfo(TJSONType),Ord(JSONType));
  493. end;
  494. { TJSONData }
  495. function TJSONData.GetItem(Index : Integer): TJSONData;
  496. begin
  497. Result:=nil;
  498. end;
  499. function TJSONData.GetCount: Integer;
  500. begin
  501. Result:=0;
  502. end;
  503. constructor TJSONData.Create;
  504. begin
  505. Clear;
  506. end;
  507. function TJSONData.GetIsNull: Boolean;
  508. begin
  509. Result:=False;
  510. end;
  511. class function TJSONData.JSONType: TJSONType;
  512. begin
  513. JSONType:=jtUnknown;
  514. end;
  515. procedure TJSONData.SetItem(Index : Integer; const AValue:
  516. TJSONData);
  517. begin
  518. // Do Nothing
  519. end;
  520. Function TJSONData.FormatJSON(Options : TFormatOptions = DefaultFormat; IndentSize : Integer = DefaultIndentSize) : TJSONStringType;
  521. begin
  522. Result:=DoFormatJSON(Options,0,IndentSize);
  523. end;
  524. Function TJSONData.DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType;
  525. begin
  526. Result:=AsJSON;
  527. end;
  528. { TJSONnumber }
  529. class function TJSONnumber.JSONType: TJSONType;
  530. begin
  531. Result:=jtNumber;
  532. end;
  533. { TJSONstring }
  534. class function TJSONstring.JSONType: TJSONType;
  535. begin
  536. Result:=jtString;
  537. end;
  538. procedure TJSONString.Clear;
  539. begin
  540. FValue:='';
  541. end;
  542. function TJSONString.Clone: TJSONData;
  543. begin
  544. Result:=TJSONString.Create(Self.FValue);
  545. end;
  546. function TJSONstring.GetValue: Variant;
  547. begin
  548. Result:=FValue;
  549. end;
  550. procedure TJSONstring.SetValue(const AValue: Variant);
  551. begin
  552. FValue:=AValue;
  553. end;
  554. function TJSONstring.GetAsBoolean: Boolean;
  555. begin
  556. Result:=StrToBool(FValue);
  557. end;
  558. function TJSONstring.GetAsFloat: TJSONFloat;
  559. Var
  560. C : Integer;
  561. begin
  562. Val(FValue,Result,C);
  563. If (C<>0) then
  564. If Not TryStrToFloat(FValue,Result) then
  565. Raise EConvertError.CreateFmt(SErrInvalidFloat,[FValue]);
  566. end;
  567. function TJSONstring.GetAsInteger: Integer;
  568. begin
  569. Result:=StrToInt(FValue);
  570. end;
  571. function TJSONstring.GetAsInt64: Int64;
  572. begin
  573. Result:=StrToInt64(FValue);
  574. end;
  575. procedure TJSONstring.SetAsBoolean(const AValue: Boolean);
  576. begin
  577. FValue:=BoolToStr(AValue);
  578. end;
  579. procedure TJSONstring.SetAsFloat(const AValue: TJSONFloat);
  580. begin
  581. FValue:=FloatToStr(AValue);
  582. end;
  583. procedure TJSONstring.SetAsInteger(const AValue: Integer);
  584. begin
  585. FValue:=IntToStr(AValue);
  586. end;
  587. procedure TJSONstring.SetAsInt64(const AValue: Int64);
  588. begin
  589. FValue:=IntToStr(AValue);
  590. end;
  591. function TJSONstring.GetAsJSON: TJSONStringType;
  592. begin
  593. Result:='"'+StringToJSONString(FValue)+'"';
  594. end;
  595. function TJSONstring.GetAsString: TJSONStringType;
  596. begin
  597. Result:=FValue;
  598. end;
  599. procedure TJSONstring.SetAsString(const AValue: TJSONStringType);
  600. begin
  601. FValue:=AValue;
  602. end;
  603. constructor TJSONstring.Create(const AValue: TJSONStringType);
  604. begin
  605. FValue:=AValue;
  606. end;
  607. { TJSONboolean }
  608. function TJSONboolean.GetValue: Variant;
  609. begin
  610. Result:=FValue;
  611. end;
  612. class function TJSONboolean.JSONType: TJSONType;
  613. begin
  614. Result:=jtBoolean;
  615. end;
  616. procedure TJSONBoolean.Clear;
  617. begin
  618. FValue:=False;
  619. end;
  620. function TJSONBoolean.Clone: TJSONData;
  621. begin
  622. Result:=TJSONBoolean.Create(Self.Fvalue);
  623. end;
  624. procedure TJSONboolean.SetValue(const AValue: Variant);
  625. begin
  626. FValue:=boolean(AValue);
  627. end;
  628. function TJSONboolean.GetAsBoolean: Boolean;
  629. begin
  630. Result:=FValue;
  631. end;
  632. function TJSONboolean.GetAsFloat: TJSONFloat;
  633. begin
  634. Result:=Ord(FValue);
  635. end;
  636. function TJSONboolean.GetAsInteger: Integer;
  637. begin
  638. Result:=Ord(FValue);
  639. end;
  640. function TJSONboolean.GetAsInt64: Int64;
  641. begin
  642. Result:=Ord(FValue);
  643. end;
  644. procedure TJSONboolean.SetAsBoolean(const AValue: Boolean);
  645. begin
  646. FValue:=AValue;
  647. end;
  648. procedure TJSONboolean.SetAsFloat(const AValue: TJSONFloat);
  649. begin
  650. FValue:=(AValue<>0)
  651. end;
  652. procedure TJSONboolean.SetAsInteger(const AValue: Integer);
  653. begin
  654. FValue:=(AValue<>0)
  655. end;
  656. procedure TJSONboolean.SetAsInt64(const AValue: Int64);
  657. begin
  658. FValue:=(AValue<>0)
  659. end;
  660. function TJSONboolean.GetAsJSON: TJSONStringType;
  661. begin
  662. If FValue then
  663. Result:='true'
  664. else
  665. Result:='false';
  666. end;
  667. function TJSONboolean.GetAsString: TJSONStringType;
  668. begin
  669. Result:=BoolToStr(FValue, True);
  670. end;
  671. procedure TJSONboolean.SetAsString(const AValue: TJSONStringType);
  672. begin
  673. FValue:=StrToBool(AValue);
  674. end;
  675. constructor TJSONboolean.Create(AValue: Boolean);
  676. begin
  677. FValue:=AValue;
  678. end;
  679. { TJSONnull }
  680. procedure TJSONnull.Converterror(From : Boolean);
  681. begin
  682. If From then
  683. Raise EJSON.Create(SErrCannotConvertFromNull)
  684. else
  685. Raise EJSON.Create(SErrCannotConvertToNull);
  686. end;
  687. {$warnings off}
  688. function TJSONnull.GetAsBoolean: Boolean;
  689. begin
  690. ConvertError(True);
  691. end;
  692. function TJSONnull.GetAsFloat: TJSONFloat;
  693. begin
  694. ConvertError(True);
  695. end;
  696. function TJSONnull.GetAsInteger: Integer;
  697. begin
  698. ConvertError(True);
  699. end;
  700. function TJSONnull.GetAsInt64: Int64;
  701. begin
  702. ConvertError(True);
  703. end;
  704. function TJSONnull.GetIsNull: Boolean;
  705. begin
  706. Result:=True;
  707. end;
  708. procedure TJSONnull.SetAsBoolean(const AValue: Boolean);
  709. begin
  710. ConvertError(False);
  711. end;
  712. procedure TJSONnull.SetAsFloat(const AValue: TJSONFloat);
  713. begin
  714. ConvertError(False);
  715. end;
  716. procedure TJSONnull.SetAsInteger(const AValue: Integer);
  717. begin
  718. ConvertError(False);
  719. end;
  720. procedure TJSONnull.SetAsInt64(const AValue: Int64);
  721. begin
  722. ConvertError(False);
  723. end;
  724. function TJSONnull.GetAsJSON: TJSONStringType;
  725. begin
  726. Result:='null';
  727. end;
  728. function TJSONnull.GetAsString: TJSONStringType;
  729. begin
  730. ConvertError(True);
  731. end;
  732. procedure TJSONnull.SetAsString(const AValue: TJSONStringType);
  733. begin
  734. ConvertError(True);
  735. end;
  736. function TJSONnull.GetValue: Variant;
  737. begin
  738. Result:=variants.Null;
  739. end;
  740. procedure TJSONnull.SetValue(const AValue: variant);
  741. begin
  742. ConvertError(False);
  743. end;
  744. class function TJSONnull.JSONType: TJSONType;
  745. begin
  746. Result:=jtNull;
  747. end;
  748. procedure TJSONNull.Clear;
  749. begin
  750. // Do nothing
  751. end;
  752. function TJSONNull.Clone: TJSONData;
  753. begin
  754. Result:=TJSONNull.Create;
  755. end;
  756. {$warnings on}
  757. { TJSONFloatNumber }
  758. function TJSONFloatNumber.GetAsBoolean: Boolean;
  759. begin
  760. Result:=(FValue<>0);
  761. end;
  762. function TJSONFloatNumber.GetAsFloat: TJSONFloat;
  763. begin
  764. Result:=FValue;
  765. end;
  766. function TJSONFloatNumber.GetAsInteger: Integer;
  767. begin
  768. Result:=Round(FValue);
  769. end;
  770. function TJSONFloatNumber.GetAsInt64: Int64;
  771. begin
  772. Result:=Round(FValue);
  773. end;
  774. procedure TJSONFloatNumber.SetAsBoolean(const AValue: Boolean);
  775. begin
  776. FValue:=Ord(AValue);
  777. end;
  778. procedure TJSONFloatNumber.SetAsFloat(const AValue: TJSONFloat);
  779. begin
  780. FValue:=AValue;
  781. end;
  782. procedure TJSONFloatNumber.SetAsInteger(const AValue: Integer);
  783. begin
  784. FValue:=AValue;
  785. end;
  786. procedure TJSONFloatNumber.SetAsInt64(const AValue: Int64);
  787. begin
  788. FValue:=AValue;
  789. end;
  790. function TJSONFloatNumber.GetAsJSON: TJSONStringType;
  791. begin
  792. Result:=AsString;
  793. end;
  794. function TJSONFloatNumber.GetAsString: TJSONStringType;
  795. begin
  796. Str(FValue,Result);
  797. end;
  798. procedure TJSONFloatNumber.SetAsString(const AValue: TJSONStringType);
  799. Var
  800. C : Integer;
  801. begin
  802. Val(AValue,FValue,C);
  803. If (C<>0) then
  804. Raise EConvertError.CreateFmt(SErrInvalidFloat,[AValue]);
  805. end;
  806. function TJSONFloatNumber.GetValue: variant;
  807. begin
  808. Result:=FValue;
  809. end;
  810. procedure TJSONFloatNumber.SetValue(const AValue: variant);
  811. begin
  812. FValue:=AValue;
  813. end;
  814. constructor TJSONFloatNumber.Create(AValue: TJSONFloat);
  815. begin
  816. FValue:=AValue;
  817. end;
  818. class function TJSONFloatNumber.NumberType: TJSONNumberType;
  819. begin
  820. Result:=ntFloat;
  821. end;
  822. procedure TJSONFloatNumber.Clear;
  823. begin
  824. FValue:=0;
  825. end;
  826. function TJSONFloatNumber.Clone: TJSONData;
  827. begin
  828. Result:=TJSONFloatNumber.Create(Self.FValue);
  829. end;
  830. { TJSONIntegerNumber }
  831. function TJSONIntegerNumber.GetAsBoolean: Boolean;
  832. begin
  833. Result:=FValue<>0;
  834. end;
  835. function TJSONIntegerNumber.GetAsFloat: TJSONFloat;
  836. begin
  837. Result:=Ord(FValue);
  838. end;
  839. function TJSONIntegerNumber.GetAsInteger: Integer;
  840. begin
  841. Result:=FValue;
  842. end;
  843. function TJSONIntegerNumber.GetAsInt64: Int64;
  844. begin
  845. Result:=FValue;
  846. end;
  847. procedure TJSONIntegerNumber.SetAsBoolean(const AValue: Boolean);
  848. begin
  849. FValue:=Ord(AValue);
  850. end;
  851. procedure TJSONIntegerNumber.SetAsFloat(const AValue: TJSONFloat);
  852. begin
  853. FValue:=Round(AValue);
  854. end;
  855. procedure TJSONIntegerNumber.SetAsInteger(const AValue: Integer);
  856. begin
  857. FValue:=AValue;
  858. end;
  859. procedure TJSONIntegerNumber.SetAsInt64(const AValue: Int64);
  860. begin
  861. FValue:=AValue;
  862. end;
  863. function TJSONIntegerNumber.GetAsJSON: TJSONStringType;
  864. begin
  865. Result:=AsString;
  866. end;
  867. function TJSONIntegerNumber.GetAsString: TJSONStringType;
  868. begin
  869. Result:=IntToStr(FValue)
  870. end;
  871. procedure TJSONIntegerNumber.SetAsString(const AValue: TJSONStringType);
  872. begin
  873. FValue:=StrToInt(AValue);
  874. end;
  875. function TJSONIntegerNumber.GetValue: variant;
  876. begin
  877. Result:=FValue;
  878. end;
  879. procedure TJSONIntegerNumber.SetValue(const AValue: variant);
  880. begin
  881. FValue:=AValue;
  882. end;
  883. constructor TJSONIntegerNumber.Create(AValue: Integer);
  884. begin
  885. FValue:=AValue;
  886. end;
  887. class function TJSONIntegerNumber.NumberType: TJSONNumberType;
  888. begin
  889. Result:=ntInteger;
  890. end;
  891. procedure TJSONIntegerNumber.Clear;
  892. begin
  893. FValue:=0;
  894. end;
  895. function TJSONIntegerNumber.Clone: TJSONData;
  896. begin
  897. Result:=TJSONIntegerNumber.Create(Self.FValue);
  898. end;
  899. { TJSONInt64Number }
  900. function TJSONInt64Number.GetAsInt64: Int64;
  901. begin
  902. Result := FValue;
  903. end;
  904. procedure TJSONInt64Number.SetAsInt64(const AValue: Int64);
  905. begin
  906. FValue := AValue;
  907. end;
  908. function TJSONInt64Number.GetAsBoolean: Boolean;
  909. begin
  910. Result:=FValue<>0;
  911. end;
  912. function TJSONInt64Number.GetAsFloat: TJSONFloat;
  913. begin
  914. Result:= FValue;
  915. end;
  916. function TJSONInt64Number.GetAsInteger: Integer;
  917. begin
  918. Result := FValue;
  919. end;
  920. procedure TJSONInt64Number.SetAsBoolean(const AValue: Boolean);
  921. begin
  922. FValue:=Ord(AValue);
  923. end;
  924. procedure TJSONInt64Number.SetAsFloat(const AValue: TJSONFloat);
  925. begin
  926. FValue:=Round(AValue);
  927. end;
  928. procedure TJSONInt64Number.SetAsInteger(const AValue: Integer);
  929. begin
  930. FValue:=AValue;
  931. end;
  932. function TJSONInt64Number.GetAsJSON: TJSONStringType;
  933. begin
  934. Result:=AsString;
  935. end;
  936. function TJSONInt64Number.GetAsString: TJSONStringType;
  937. begin
  938. Result:=IntToStr(FValue)
  939. end;
  940. procedure TJSONInt64Number.SetAsString(const AValue: TJSONStringType);
  941. begin
  942. FValue:=StrToInt64(AValue);
  943. end;
  944. function TJSONInt64Number.GetValue: variant;
  945. begin
  946. Result:=FValue;
  947. end;
  948. procedure TJSONInt64Number.SetValue(const AValue: variant);
  949. begin
  950. FValue:=AValue;
  951. end;
  952. constructor TJSONInt64Number.Create(AValue: Int64);
  953. begin
  954. FValue := AValue;
  955. end;
  956. class function TJSONInt64Number.NumberType: TJSONNumberType;
  957. begin
  958. Result:=ntInt64;
  959. end;
  960. procedure TJSONInt64Number.Clear;
  961. begin
  962. FValue:=0;
  963. end;
  964. function TJSONInt64Number.Clone: TJSONData;
  965. begin
  966. Result:=TJSONInt64Number.Create(Self.FValue);
  967. end;
  968. { TJSONArray }
  969. function TJSONArray.GetBooleans(Index : Integer): Boolean;
  970. begin
  971. Result:=Items[Index].AsBoolean;
  972. end;
  973. function TJSONArray.GetArrays(Index : Integer): TJSONArray;
  974. begin
  975. Result:=Items[Index] as TJSONArray;
  976. end;
  977. function TJSONArray.GetFloats(Index : Integer): TJSONFloat;
  978. begin
  979. Result:=Items[Index].AsFloat;
  980. end;
  981. function TJSONArray.GetIntegers(Index : Integer): Integer;
  982. begin
  983. Result:=Items[Index].AsInteger;
  984. end;
  985. function TJSONArray.GetInt64s(Index : Integer): Int64;
  986. begin
  987. Result:=Items[Index].AsInt64;
  988. end;
  989. function TJSONArray.GetNulls(Index : Integer): Boolean;
  990. begin
  991. Result:=Items[Index].IsNull;
  992. end;
  993. function TJSONArray.GetObjects(Index : Integer): TJSONObject;
  994. begin
  995. Result:=Items[Index] as TJSONObject;
  996. end;
  997. function TJSONArray.GetStrings(Index : Integer): TJSONStringType;
  998. begin
  999. Result:=Items[Index].AsString;
  1000. end;
  1001. function TJSONArray.GetTypes(Index : Integer): TJSONType;
  1002. begin
  1003. Result:=Items[Index].JSONType;
  1004. end;
  1005. procedure TJSONArray.SetArrays(Index : Integer; const AValue: TJSONArray);
  1006. begin
  1007. Items[Index]:=AValue;
  1008. end;
  1009. procedure TJSONArray.SetBooleans(Index : Integer; const AValue: Boolean);
  1010. begin
  1011. Items[Index]:=TJSonBoolean.Create(AValue);
  1012. end;
  1013. procedure TJSONArray.SetFloats(Index : Integer; const AValue: TJSONFloat);
  1014. begin
  1015. Items[Index]:=TJSONFloatNumber.Create(AValue);
  1016. end;
  1017. procedure TJSONArray.SetIntegers(Index : Integer; const AValue: Integer);
  1018. begin
  1019. Items[Index]:=TJSONIntegerNumber.Create(AValue);
  1020. end;
  1021. procedure TJSONArray.SetInt64s(Index : Integer; const AValue: Int64);
  1022. begin
  1023. Items[Index]:=TJSONInt64Number.Create(AValue);
  1024. end;
  1025. procedure TJSONArray.SetObjects(Index : Integer; const AValue: TJSONObject);
  1026. begin
  1027. Items[Index]:=AValue;
  1028. end;
  1029. procedure TJSONArray.SetStrings(Index : Integer; const AValue: TJSONStringType);
  1030. begin
  1031. Items[Index]:=TJSONString.Create(AValue);
  1032. end;
  1033. procedure TJSONArray.Converterror(From: Boolean);
  1034. begin
  1035. If From then
  1036. Raise EJSON.Create(SErrCannotConvertFromArray)
  1037. else
  1038. Raise EJSON.Create(SErrCannotConvertToArray);
  1039. end;
  1040. {$warnings off}
  1041. function TJSONArray.GetAsBoolean: Boolean;
  1042. begin
  1043. ConvertError(True);
  1044. end;
  1045. function TJSONArray.GetAsFloat: TJSONFloat;
  1046. begin
  1047. ConvertError(True);
  1048. end;
  1049. function TJSONArray.GetAsInteger: Integer;
  1050. begin
  1051. ConvertError(True);
  1052. end;
  1053. function TJSONArray.GetAsInt64: Int64;
  1054. begin
  1055. ConvertError(True);
  1056. end;
  1057. procedure TJSONArray.SetAsBoolean(const AValue: Boolean);
  1058. begin
  1059. ConvertError(False);
  1060. end;
  1061. procedure TJSONArray.SetAsFloat(const AValue: TJSONFloat);
  1062. begin
  1063. ConvertError(False);
  1064. end;
  1065. procedure TJSONArray.SetAsInteger(const AValue: Integer);
  1066. begin
  1067. ConvertError(False);
  1068. end;
  1069. procedure TJSONArray.SetAsInt64(const AValue: Int64);
  1070. begin
  1071. ConvertError(False);
  1072. end;
  1073. {$warnings on}
  1074. function TJSONArray.GetAsJSON: TJSONStringType;
  1075. Var
  1076. I : Integer;
  1077. begin
  1078. Result:='[';
  1079. For I:=0 to Count-1 do
  1080. begin
  1081. Result:=Result+Items[i].AsJSON;
  1082. If (I<Count-1) then
  1083. Result:=Result+', '
  1084. end;
  1085. Result:=Result+']';
  1086. end;
  1087. {$warnings off}
  1088. Function IndentString(Options : TFormatOptions; Indent : Integer) : TJSONStringType;
  1089. begin
  1090. If (foUseTabChar in Options) then
  1091. Result:=StringofChar(#9,Indent)
  1092. else
  1093. Result:=StringOfChar(' ',Indent);
  1094. end;
  1095. Function TJSONArray.DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType;
  1096. Var
  1097. I : Integer;
  1098. begin
  1099. Result:='[';
  1100. if not (foSingleLineArray in Options) then
  1101. Result:=Result+sLineBreak;
  1102. For I:=0 to Count-1 do
  1103. begin
  1104. if not (foSingleLineArray in Options) then
  1105. Result:=Result+IndentString(Options, CurrentIndent+Indent);
  1106. Result:=Result+Items[i].DoFormatJSON(Options,CurrentIndent+Indent,Indent);
  1107. If (I<Count-1) then
  1108. if (foSingleLineArray in Options) then
  1109. Result:=Result+', '
  1110. else
  1111. Result:=Result+',';
  1112. if not (foSingleLineArray in Options) then
  1113. Result:=Result+sLineBreak
  1114. end;
  1115. if not (foSingleLineArray in Options) then
  1116. Result:=Result+IndentString(Options, CurrentIndent);
  1117. Result:=Result+']';
  1118. end;
  1119. function TJSONArray.GetAsString: TJSONStringType;
  1120. begin
  1121. ConvertError(True);
  1122. end;
  1123. procedure TJSONArray.SetAsString(const AValue: TJSONStringType);
  1124. begin
  1125. ConvertError(False);
  1126. end;
  1127. function TJSONArray.GetValue: variant;
  1128. begin
  1129. ConvertError(True);
  1130. end;
  1131. procedure TJSONArray.SetValue(const AValue: variant);
  1132. begin
  1133. ConvertError(False);
  1134. end;
  1135. {$warnings on}
  1136. function TJSONArray.GetCount: Integer;
  1137. begin
  1138. Result:=Flist.Count;
  1139. end;
  1140. function TJSONArray.GetItem(Index: Integer): TJSONData;
  1141. begin
  1142. Result:=FList[Index] as TJSONData;
  1143. end;
  1144. procedure TJSONArray.SetItem(Index: Integer; const AValue: TJSONData);
  1145. begin
  1146. If (Index=FList.Count) then
  1147. FList.Add(AValue)
  1148. else
  1149. FList[Index]:=AValue;
  1150. end;
  1151. constructor TJSONArray.Create;
  1152. begin
  1153. Flist:=TFPObjectList.Create(True);
  1154. end;
  1155. Function VarRecToJSON(Const Element : TVarRec; const SourceType : String) : TJSONData;
  1156. begin
  1157. Result:=Nil;
  1158. With Element do
  1159. case VType of
  1160. vtInteger : Result:=TJSONIntegerNumber.Create(VInteger);
  1161. vtBoolean : Result:=TJSONBoolean.Create(VBoolean);
  1162. vtChar : Result:=TJSONString.Create(VChar);
  1163. vtExtended : Result:=TJSONFloatNumber.Create(VExtended^);
  1164. vtString : Result:=TJSONString.Create(vString^);
  1165. vtAnsiString : Result:=TJSONString.Create(AnsiString(vAnsiString));
  1166. vtPChar : Result:=TJSONString.Create(StrPas(VPChar));
  1167. vtPointer : If (VPointer<>Nil) then
  1168. Raise EJSON.CreateFmt(SErrPointerNotNil,[SourceType])
  1169. else
  1170. Result:=TJSONNull.Create;
  1171. vtCurrency : Result:=TJSONFloatNumber.Create(vCurrency^);
  1172. vtInt64 : Result:=TJSONInt64Number.Create(vInt64^);
  1173. vtObject : if (VObject is TJSONData) then
  1174. Result:=TJSONData(VObject)
  1175. else
  1176. Raise EJSON.CreateFmt(SErrNotJSONData,[VObject.ClassName,SourceType]);
  1177. //vtVariant :
  1178. else
  1179. Raise EJSON.CreateFmt(SErrUnknownTypeInConstructor,[SourceType,VType])
  1180. end;
  1181. end;
  1182. constructor TJSONArray.Create(Const Elements: array of const);
  1183. Var
  1184. I : integer;
  1185. J : TJSONData;
  1186. begin
  1187. Create;
  1188. For I:=Low(Elements) to High(Elements) do
  1189. begin
  1190. J:=VarRecToJSON(Elements[i],'Array');
  1191. Add(J);
  1192. end;
  1193. end;
  1194. Destructor TJSONArray.Destroy;
  1195. begin
  1196. FreeAndNil(FList);
  1197. inherited Destroy;
  1198. end;
  1199. class function TJSONArray.JSONType: TJSONType;
  1200. begin
  1201. Result:=jtArray;
  1202. end;
  1203. function TJSONArray.Clone: TJSONData;
  1204. Var
  1205. A : TJSONArray;
  1206. I : Integer;
  1207. begin
  1208. A:=TJSONArray.Create;
  1209. try
  1210. For I:=0 to Count-1 do
  1211. A.Add(Self.Items[I].Clone);
  1212. Result:=A;
  1213. except
  1214. A.Free;
  1215. Raise;
  1216. end;
  1217. end;
  1218. procedure TJSONArray.Iterate(Iterator: TJSONArrayIterator; Data: TObject);
  1219. Var
  1220. I : Integer;
  1221. Cont : Boolean;
  1222. begin
  1223. I:=0;
  1224. Cont:=True;
  1225. While (I<FList.Count) and cont do
  1226. begin
  1227. Iterator(Items[i],Data,Cont);
  1228. Inc(I);
  1229. end;
  1230. end;
  1231. function TJSONArray.IndexOf(obj: TJSONData): Integer;
  1232. begin
  1233. Result:=FList.IndexOf(Obj);
  1234. end;
  1235. procedure TJSONArray.Clear;
  1236. begin
  1237. FList.Clear;
  1238. end;
  1239. function TJSONArray.Add(Item: TJSONData): Integer;
  1240. begin
  1241. Result:=FList.Add(Item);
  1242. end;
  1243. function TJSONArray.Add(I: Integer): Integer;
  1244. begin
  1245. Result:=Add(TJSONIntegerNumber.Create(I));
  1246. end;
  1247. function TJSONArray.Add(I: Int64): Int64;
  1248. begin
  1249. Result:=Add(TJSONInt64Number.Create(I));
  1250. end;
  1251. function TJSONArray.Add(const S: String): Integer;
  1252. begin
  1253. Result:=Add(TJSONString.Create(S));
  1254. end;
  1255. function TJSONArray.Add: Integer;
  1256. begin
  1257. Result:=Add(TJSONNull.Create);
  1258. end;
  1259. function TJSONArray.Add(F: TJSONFloat): Integer;
  1260. begin
  1261. Result:=Add(TJSONFloatNumber.Create(F));
  1262. end;
  1263. function TJSONArray.Add(B: Boolean): Integer;
  1264. begin
  1265. Result:=Add(TJSONBoolean.Create(B));
  1266. end;
  1267. function TJSONArray.Add(AnArray: TJSONArray): Integer;
  1268. begin
  1269. If (IndexOf(AnArray)<>-1) then
  1270. Raise EJSON.Create(SErrCannotAddArrayTwice);
  1271. Result:=Add(TJSONData(AnArray));
  1272. end;
  1273. function TJSONArray.Add(AnObject: TJSONObject): Integer;
  1274. begin
  1275. If (IndexOf(AnObject)<>-1) then
  1276. Raise EJSON.Create(SErrCannotAddObjectTwice);
  1277. Result:=Add(TJSONData(AnObject));
  1278. end;
  1279. procedure TJSONArray.Delete(Index: Integer);
  1280. begin
  1281. FList.Delete(Index);
  1282. end;
  1283. procedure TJSONArray.Exchange(Index1, Index2: Integer);
  1284. begin
  1285. FList.Exchange(Index1, Index2);
  1286. end;
  1287. procedure TJSONArray.Insert(Index: Integer);
  1288. begin
  1289. Insert(Index,TJSONNull.Create);
  1290. end;
  1291. procedure TJSONArray.Insert(Index: Integer; Item: TJSONData);
  1292. begin
  1293. FList.Insert(Index, Item);
  1294. end;
  1295. procedure TJSONArray.Insert(Index: Integer; I: Integer);
  1296. begin
  1297. FList.Insert(Index, TJSONIntegerNumber.Create(I));
  1298. end;
  1299. procedure TJSONArray.Insert(Index: Integer; I: Int64);
  1300. begin
  1301. FList.Insert(Index, TJSONInt64Number.Create(I));
  1302. end;
  1303. procedure TJSONArray.Insert(Index: Integer; const S: String);
  1304. begin
  1305. FList.Insert(Index, TJSONString.Create(S));
  1306. end;
  1307. procedure TJSONArray.Insert(Index: Integer; F: TJSONFloat);
  1308. begin
  1309. FList.Insert(Index, TJSONFloatNumber.Create(F));
  1310. end;
  1311. procedure TJSONArray.Insert(Index: Integer; B: Boolean);
  1312. begin
  1313. FList.Insert(Index, TJSONBoolean.Create(B));
  1314. end;
  1315. procedure TJSONArray.Insert(Index: Integer; AnArray: TJSONArray);
  1316. begin
  1317. if (IndexOf(AnArray)<>-1) then
  1318. raise EJSON.Create(SErrCannotAddArrayTwice);
  1319. FList.Insert(Index, AnArray);
  1320. end;
  1321. procedure TJSONArray.Insert(Index: Integer; AnObject: TJSONObject);
  1322. begin
  1323. if (IndexOf(AnObject)<>-1) then
  1324. raise EJSON.Create(SErrCannotAddObjectTwice);
  1325. FList.Insert(Index, AnObject);
  1326. end;
  1327. procedure TJSONArray.Move(CurIndex, NewIndex: Integer);
  1328. begin
  1329. FList.Move(CurIndex, NewIndex);
  1330. end;
  1331. procedure TJSONArray.Remove(Item: TJSONData);
  1332. begin
  1333. FList.Remove(Item);
  1334. end;
  1335. { TJSONObject }
  1336. function TJSONObject.GetArrays(Const AName : String): TJSONArray;
  1337. begin
  1338. Result:=GetElements(AName) as TJSONArray;
  1339. end;
  1340. function TJSONObject.GetBooleans(Const AName : String): Boolean;
  1341. begin
  1342. Result:=GetElements(AName).AsBoolean;
  1343. end;
  1344. function TJSONObject.GetElements(Const AName: string): TJSONData;
  1345. begin
  1346. Result:=TJSONData(FHash.Find(AName));
  1347. If (Result=Nil) then
  1348. Raise EJSON.CreateFmt(SErrNonexistentElement,[AName]);
  1349. end;
  1350. function TJSONObject.GetFloats(Const AName : String): TJSONFloat;
  1351. begin
  1352. Result:=GetElements(AName).AsFloat;
  1353. end;
  1354. function TJSONObject.GetIntegers(Const AName : String): Integer;
  1355. begin
  1356. Result:=GetElements(AName).AsInteger;
  1357. end;
  1358. function TJSONObject.GetInt64s(Const AName : String): Int64;
  1359. begin
  1360. Result:=GetElements(AName).AsInt64;
  1361. end;
  1362. function TJSONObject.GetIsNull(Const AName : String): Boolean;
  1363. begin
  1364. Result:=GetElements(AName).IsNull;
  1365. end;
  1366. function TJSONObject.GetNameOf(Index: Integer): TJSONStringType;
  1367. begin
  1368. Result:=FHash.NameOfIndex(Index);
  1369. end;
  1370. function TJSONObject.GetObjects(const AName : String): TJSONObject;
  1371. begin
  1372. Result:=GetElements(AName) as TJSONObject;
  1373. end;
  1374. function TJSONObject.GetStrings(const AName : String): TJSONStringType;
  1375. begin
  1376. Result:=GetElements(AName).AsString;
  1377. end;
  1378. function TJSONObject.GetTypes(const AName : String): TJSONType;
  1379. begin
  1380. Result:=Getelements(Aname).JSONType;
  1381. end;
  1382. procedure TJSONObject.SetArrays(const AName : String; const AValue: TJSONArray);
  1383. begin
  1384. SetElements(AName,AVAlue);
  1385. end;
  1386. procedure TJSONObject.SetBooleans(const AName : String; const AValue: Boolean);
  1387. begin
  1388. SetElements(AName,TJSONBoolean.Create(AVAlue));
  1389. end;
  1390. procedure TJSONObject.SetElements(const AName: string; const AValue: TJSONData);
  1391. Var
  1392. Index : Integer;
  1393. begin
  1394. Index:=FHash.FindIndexOf(AName);
  1395. If (Index=-1) then
  1396. FHash.Add(AName,AValue)
  1397. else
  1398. FHash.Items[Index]:=AValue; // Will free the previous value.
  1399. end;
  1400. procedure TJSONObject.SetFloats(const AName : String; const AValue: TJSONFloat);
  1401. begin
  1402. SetElements(AName,TJSONFloatNumber.Create(AVAlue));
  1403. end;
  1404. procedure TJSONObject.SetIntegers(const AName : String; const AValue: Integer);
  1405. begin
  1406. SetElements(AName,TJSONIntegerNumber.Create(AVAlue));
  1407. end;
  1408. procedure TJSONObject.SetInt64s(const AName : String; const AValue: Int64);
  1409. begin
  1410. SetElements(AName,TJSONInt64Number.Create(AVAlue));
  1411. end;
  1412. procedure TJSONObject.SetIsNull(const AName : String; const AValue: Boolean);
  1413. begin
  1414. If Not AValue then
  1415. Raise EJSON.Create(SErrCannotSetNotIsNull);
  1416. SetElements(AName,TJSONNull.Create);
  1417. end;
  1418. procedure TJSONObject.SetObjects(const AName : String; const AValue: TJSONObject);
  1419. begin
  1420. SetElements(AName,AValue);
  1421. end;
  1422. procedure TJSONObject.SetStrings(const AName : String; const AValue: TJSONStringType);
  1423. begin
  1424. SetElements(AName,TJSONString.Create(AVAlue));
  1425. end;
  1426. procedure TJSONObject.Converterror(From: Boolean);
  1427. begin
  1428. If From then
  1429. Raise EJSON.Create(SErrCannotConvertFromObject)
  1430. else
  1431. Raise EJSON.Create(SErrCannotConvertToObject);
  1432. end;
  1433. {$warnings off}
  1434. function TJSONObject.GetAsBoolean: Boolean;
  1435. begin
  1436. ConvertError(True);
  1437. end;
  1438. function TJSONObject.GetAsFloat: TJSONFloat;
  1439. begin
  1440. ConvertError(True);
  1441. end;
  1442. function TJSONObject.GetAsInteger: Integer;
  1443. begin
  1444. ConvertError(True);
  1445. end;
  1446. function TJSONObject.GetAsInt64: Int64;
  1447. begin
  1448. ConvertError(True);
  1449. end;
  1450. procedure TJSONObject.SetAsBoolean(const AValue: Boolean);
  1451. begin
  1452. ConvertError(False);
  1453. end;
  1454. procedure TJSONObject.SetAsFloat(const AValue: TJSONFloat);
  1455. begin
  1456. ConvertError(False);
  1457. end;
  1458. procedure TJSONObject.SetAsInteger(const AValue: Integer);
  1459. begin
  1460. ConvertError(False);
  1461. end;
  1462. procedure TJSONObject.SetAsInt64(const AValue: Int64);
  1463. begin
  1464. ConvertError(False);
  1465. end;
  1466. {$warnings on}
  1467. function TJSONObject.GetAsJSON: TJSONStringType;
  1468. Var
  1469. I : Integer;
  1470. begin
  1471. Result:='';
  1472. For I:=0 to Count-1 do
  1473. begin
  1474. If (Result<>'') then
  1475. Result:=Result+', ';
  1476. Result:=Result+'"'+StringToJSONString(Names[i])+'" : '+Items[I].AsJSON;
  1477. end;
  1478. If (Result<>'') then
  1479. Result:='{ '+Result+' }'
  1480. else
  1481. Result:='{}';
  1482. end;
  1483. {$warnings off}
  1484. function TJSONObject.GetAsString: TJSONStringType;
  1485. begin
  1486. ConvertError(True);
  1487. end;
  1488. procedure TJSONObject.SetAsString(const AValue: TJSONStringType);
  1489. begin
  1490. ConvertError(False);
  1491. end;
  1492. function TJSONObject.GetValue: variant;
  1493. begin
  1494. ConvertError(True);
  1495. end;
  1496. procedure TJSONObject.SetValue(const AValue: variant);
  1497. begin
  1498. ConvertError(False);
  1499. end;
  1500. {$warnings on}
  1501. function TJSONObject.GetCount: Integer;
  1502. begin
  1503. Result:=FHash.Count;
  1504. end;
  1505. function TJSONObject.GetItem(Index: Integer): TJSONData;
  1506. begin
  1507. Result:=TJSONData(FHash.Items[Index]);
  1508. end;
  1509. procedure TJSONObject.SetItem(Index: Integer; const AValue: TJSONData);
  1510. begin
  1511. FHash.Items[Index]:=AValue;
  1512. end;
  1513. constructor TJSONObject.Create;
  1514. begin
  1515. FHash:=TFPHashObjectList.Create(True);
  1516. end;
  1517. constructor TJSONObject.Create(const Elements: array of const);
  1518. Var
  1519. I : integer;
  1520. AName : String;
  1521. J : TJSONData;
  1522. begin
  1523. Create;
  1524. If ((High(Elements)-Low(Elements)) mod 2)=0 then
  1525. Raise EJSON.Create(SErrOddNumber);
  1526. I:=Low(Elements);
  1527. While I<=High(Elements) do
  1528. begin
  1529. With Elements[i] do
  1530. Case VType of
  1531. vtChar : AName:=VChar;
  1532. vtString : AName:=vString^;
  1533. vtAnsiString : AName:=(AnsiString(vAnsiString));
  1534. vtPChar : AName:=StrPas(VPChar);
  1535. else
  1536. Raise EJSON.CreateFmt(SErrNameMustBeString,[I+1]);
  1537. end;
  1538. If (ANAme='') then
  1539. Raise EJSON.CreateFmt(SErrNameMustBeString,[I+1]);
  1540. Inc(I);
  1541. J:=VarRecToJSON(Elements[i],'Object');
  1542. Add(AName,J);
  1543. Inc(I);
  1544. end;
  1545. end;
  1546. destructor TJSONObject.Destroy;
  1547. begin
  1548. FreeAndNil(FHash);
  1549. inherited Destroy;
  1550. end;
  1551. class function TJSONObject.JSONType: TJSONType;
  1552. begin
  1553. Result:=jtObject;
  1554. end;
  1555. function TJSONObject.Clone: TJSONData;
  1556. Var
  1557. O : TJSONObject;
  1558. I: Integer;
  1559. begin
  1560. O:=TJSONObject.Create;
  1561. try
  1562. For I:=0 to Count-1 do
  1563. O.Add(Self.Names[I],Self.Items[I].Clone);
  1564. Result:=O;
  1565. except
  1566. FreeAndNil(O);
  1567. Raise;
  1568. end;
  1569. end;
  1570. Function TJSONObject.DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType;
  1571. Var
  1572. i : Integer;
  1573. S : TJSONStringType;
  1574. begin
  1575. CurrentIndent:=CurrentIndent+Indent;
  1576. For I:=0 to Count-1 do
  1577. begin
  1578. If (Result<>'') then
  1579. begin
  1580. If (foSingleLineObject in Options) then
  1581. Result:=Result+', '
  1582. else
  1583. Result:=Result+','+SLineBreak;
  1584. end;
  1585. If not (foSingleLineObject in Options) then
  1586. Result:=Result+IndentString(Options,CurrentIndent);
  1587. S:=StringToJSONString(Names[i]);
  1588. If not (foDoNotQuoteMembers in options) then
  1589. S:='"'+S+'"';
  1590. Result:=Result+S+' : '+Items[I].DoFormatJSON(Options,CurrentIndent,Indent);
  1591. end;
  1592. If (Result<>'') then
  1593. begin
  1594. if (foSingleLineObject in Options) then
  1595. Result:='{ '+Result+' }'
  1596. else
  1597. Result:='{'+sLineBreak+Result+sLineBreak+indentString(options,CurrentIndent-Indent)+'}'
  1598. end
  1599. else
  1600. Result:='{}';
  1601. end;
  1602. procedure TJSONObject.Iterate(Iterator: TJSONObjectIterator; Data: TObject);
  1603. Var
  1604. I : Integer;
  1605. Cont : Boolean;
  1606. begin
  1607. I:=0;
  1608. Cont:=True;
  1609. While (I<FHash.Count) and cont do
  1610. begin
  1611. Iterator(Names[I],Items[i],Data,Cont);
  1612. Inc(I);
  1613. end;
  1614. end;
  1615. function TJSONObject.IndexOf(Item: TJSONData): Integer;
  1616. begin
  1617. Result:=FHash.IndexOf(Item);
  1618. end;
  1619. function TJSONObject.IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer;
  1620. begin
  1621. Result:=FHash.FindIndexOf(AName);
  1622. if (Result=-1) and CaseInsensitive then
  1623. begin
  1624. Result:=Count-1;
  1625. While (Result>=0) and (CompareText(Names[Result],AName)<>0) do
  1626. Dec(Result);
  1627. end;
  1628. end;
  1629. procedure TJSONObject.Clear;
  1630. begin
  1631. FHash.Clear;
  1632. end;
  1633. function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONData
  1634. ): Integer;
  1635. begin
  1636. Result:=FHash.Add(AName,AValue);
  1637. end;
  1638. function TJSONObject.Add(const AName: TJSONStringType; AValue: Boolean
  1639. ): Integer;
  1640. begin
  1641. Result:=Add(AName,TJSONBoolean.Create(AValue));
  1642. end;
  1643. function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer;
  1644. begin
  1645. Result:=Add(AName,TJSONFloatNumber.Create(AValue));
  1646. end;
  1647. function TJSONObject.Add(const AName, AValue: TJSONStringType): Integer;
  1648. begin
  1649. Result:=Add(AName,TJSONString.Create(AValue));
  1650. end;
  1651. function TJSONObject.Add(const AName: TJSONStringType; Avalue: Integer): Integer;
  1652. begin
  1653. Result:=Add(AName,TJSONIntegerNumber.Create(AValue));
  1654. end;
  1655. function TJSONObject.Add(const AName: TJSONStringType; Avalue: Int64): Integer;
  1656. begin
  1657. Result:=Add(AName,TJSONInt64Number.Create(AValue));
  1658. end;
  1659. function TJSONObject.Add(const AName: TJSONStringType): Integer;
  1660. begin
  1661. Result:=Add(AName,TJSONNull.Create);
  1662. end;
  1663. function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONArray
  1664. ): Integer;
  1665. begin
  1666. Result:=Add(AName,TJSONData(AValue));
  1667. end;
  1668. procedure TJSONObject.Delete(Index: Integer);
  1669. begin
  1670. FHash.Delete(Index);
  1671. end;
  1672. procedure TJSONObject.Remove(Item: TJSONData);
  1673. begin
  1674. FHash.Remove(Item);
  1675. end;
  1676. function TJSONObject.Extract(Index: Integer): TJSONData;
  1677. begin
  1678. Result:=Items[Index];
  1679. FHash.Extract(Result);
  1680. end;
  1681. end.