| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030 |
- {
- This file is part of the Free Component Library
- JSON Data structures
- Copyright (c) 2007 by Michael Van Canneyt [email protected]
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- {$mode objfpc}
- {$h+}
- unit fpjson;
- interface
- uses
- variants,
- SysUtils,
- classes,
- contnrs;
- type
- TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject);
- TJSONInstanceType = (jitUnknown, jitNumberInteger,jitNumberInt64,jitNumberQWord,jitNumberFloat,
- jitString, jitBoolean, jitNull, jitArray, jitObject);
- TJSONFloat = Double;
- TJSONStringType = AnsiString;
- TJSONCharType = AnsiChar;
- PJSONCharType = ^TJSONCharType;
- TFormatOption = (foSingleLineArray, // Array without CR/LF : all on one line
- foSingleLineObject, // Object without CR/LF : all on one line
- foDoNotQuoteMembers, // Do not quote object member names.
- foUseTabchar, // Use tab characters instead of spaces.
- foSkipWhiteSpace); // Do not use whitespace at all
- TFormatOptions = set of TFormatOption;
- Const
- DefaultIndentSize = 2;
- DefaultFormat = [];
- AsJSONFormat = [foSingleLineArray,foSingleLineObject]; // These options make FormatJSON behave as AsJSON
- AsCompressedJSON = [foSingleLineArray,foSingleLineObject,foskipWhiteSpace]; // These options make FormatJSON behave as AsJSON with TJSONData.CompressedJSON=True
- AsCompactJSON = [foSingleLineArray,foSingleLineObject,foskipWhiteSpace,foDoNotQuoteMembers]; // These options make FormatJSON behave as AsJSON with TJSONData.CompressedJSON=True and TJSONObject.UnquotedMemberNames=True
- Type
- TJSONData = Class;
- { TMJBaseObjectEnumerator }
- TJSONEnum = Record
- Key : TJSONStringType;
- KeyNum : Integer;
- Value : TJSONData;
- end;
- TBaseJSONEnumerator = class
- public
- function GetCurrent: TJSONEnum; virtual; abstract;
- function MoveNext : Boolean; virtual; abstract;
- property Current: TJSONEnum read GetCurrent;
- end;
- { TMJObjectEnumerator }
- { TJSONData }
-
- TJSONData = class(TObject)
- private
- Const
- ElementSeps : Array[Boolean] of TJSONStringType = (', ',',');
- Class Var FCompressedJSON : Boolean;
- Class Var FElementSep : TJSONStringType;
- class procedure DetermineElementSeparators;
- class function GetCompressedJSON: Boolean; static;
- class procedure SetCompressedJSON(AValue: Boolean); static;
- protected
- Class Procedure DoError(Const Msg : String);
- Class Procedure DoError(Const Fmt : String; const Args : Array of const);
- Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; virtual;
- function GetAsBoolean: Boolean; virtual; abstract;
- function GetAsFloat: TJSONFloat; virtual; abstract;
- function GetAsInteger: Integer; virtual; abstract;
- function GetAsInt64: Int64; virtual; abstract;
- function GetAsQWord: QWord; virtual; abstract;
- function GetIsNull: Boolean; virtual;
- procedure SetAsBoolean(const AValue: Boolean); virtual; abstract;
- procedure SetAsFloat(const AValue: TJSONFloat); virtual; abstract;
- procedure SetAsInteger(const AValue: Integer); virtual; abstract;
- procedure SetAsInt64(const AValue: Int64); virtual; abstract;
- procedure SetAsQword(const AValue: QWord); virtual; abstract;
- function GetAsJSON: TJSONStringType; virtual; abstract;
- function GetAsString: TJSONStringType; virtual; abstract;
- procedure SetAsString(const AValue: TJSONStringType); virtual; abstract;
- function GetValue: variant; virtual; abstract;
- procedure SetValue(const AValue: variant); virtual; abstract;
- function GetItem(Index : Integer): TJSONData; virtual;
- procedure SetItem(Index : Integer; const AValue: TJSONData); virtual;
- Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; virtual;
- function GetCount: Integer; virtual;
- Public
- Class function JSONType: TJSONType; virtual;
- Class Property CompressedJSON : Boolean Read GetCompressedJSON Write SetCompressedJSON;
- public
- Constructor Create; virtual;
- Procedure Clear; virtual; Abstract;
- Procedure DumpJSON(S : TStream);
- // Get enumerator
- function GetEnumerator: TBaseJSONEnumerator; virtual;
- Function FindPath(Const APath : TJSONStringType) : TJSONdata;
- Function GetPath(Const APath : TJSONStringType) : TJSONdata;
- Function Clone : TJSONData; virtual; abstract;
- Function FormatJSON(Options : TFormatOptions = DefaultFormat; Indentsize : Integer = DefaultIndentSize) : TJSONStringType;
- property Count: Integer read GetCount;
- property Items[Index: Integer]: TJSONData read GetItem write SetItem;
- property Value: variant read GetValue write SetValue;
- Property AsString : TJSONStringType Read GetAsString Write SetAsString;
- Property AsFloat : TJSONFloat Read GetAsFloat Write SetAsFloat;
- Property AsInteger : Integer Read GetAsInteger Write SetAsInteger;
- Property AsInt64 : Int64 Read GetAsInt64 Write SetAsInt64;
- Property AsQWord : QWord Read GetAsQWord Write SetAsQword;
- Property AsBoolean : Boolean Read GetAsBoolean Write SetAsBoolean;
- Property IsNull : Boolean Read GetIsNull;
- Property AsJSON : TJSONStringType Read GetAsJSON;
- end;
- TJSONDataClass = Class of TJSONData;
- TJSONNumberType = (ntFloat,ntInteger,ntInt64,ntQWord);
- TJSONNumber = class(TJSONData)
- protected
- public
- class function JSONType: TJSONType; override;
- class function NumberType : TJSONNumberType; virtual; abstract;
- end;
- { TJSONFloatNumber }
- TJSONFloatNumber = class(TJSONNumber)
- Private
- FValue : TJSONFloat;
- protected
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- public
- Constructor Create(AValue : TJSONFloat); reintroduce;
- class function NumberType : TJSONNumberType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONFloatNumberClass = Class of TJSONFloatNumber;
- { TJSONIntegerNumber }
- TJSONIntegerNumber = class(TJSONNumber)
- Private
- FValue : Integer;
- protected
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- public
- Constructor Create(AValue : Integer); reintroduce;
- class function NumberType : TJSONNumberType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONIntegerNumberClass = Class of TJSONIntegerNumber;
- { TJSONInt64Number }
- TJSONInt64Number = class(TJSONNumber)
- Private
- FValue : Int64;
- protected
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- public
- Constructor Create(AValue : Int64); reintroduce;
- class function NumberType : TJSONNumberType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONInt64NumberClass = Class of TJSONInt64Number;
- { TJSONQWordNumber }
- TJSONQWordNumber = class(TJSONNumber)
- Private
- FValue : Qword;
- protected
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- public
- Constructor Create(AValue : QWord); reintroduce;
- class function NumberType : TJSONNumberType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONQWordNumberClass = Class of TJSONQWordNumber;
- { TJSONString }
- TJSONString = class(TJSONData)
- Private
- FValue: TJSONStringType;
- protected
- function GetValue: Variant; override;
- procedure SetValue(const AValue: Variant); override;
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- public
- Constructor Create(const AValue : TJSONStringType); reintroduce;
- class function JSONType: TJSONType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONStringClass = Class of TJSONString;
- { TJSONboolean }
- TJSONBoolean = class(TJSONData)
- Private
- FValue: Boolean;
- protected
- function GetValue: Variant; override;
- procedure SetValue(const AValue: Variant); override;
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- public
- Constructor Create(AValue : Boolean); reintroduce;
- class function JSONType: TJSONType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONBooleanClass = Class of TJSONBoolean;
- { TJSONnull }
- TJSONNull = class(TJSONData)
- protected
- Procedure Converterror(From : Boolean);
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- function GetIsNull: Boolean; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- public
- class function JSONType: TJSONType; override;
- Procedure Clear; override;
- Function Clone : TJSONData; override;
- end;
- TJSONNullClass = Class of TJSONNull;
- TJSONArrayIterator = procedure(Item: TJSONData; Data: TObject; var Continue: Boolean) of object;
- { TJSONArray }
- TJSONObject = Class;
- TJSONArray = class(TJSONData)
- Private
- FList : TFPObjectList;
- function GetArrays(Index : Integer): TJSONArray;
- function GetBooleans(Index : Integer): Boolean;
- function GetFloats(Index : Integer): TJSONFloat;
- function GetIntegers(Index : Integer): Integer;
- function GetInt64s(Index : Integer): Int64;
- function GetNulls(Index : Integer): Boolean;
- function GetObjects(Index : Integer): TJSONObject;
- function GetQWords(Index : Integer): QWord;
- function GetStrings(Index : Integer): TJSONStringType;
- function GetTypes(Index : Integer): TJSONType;
- procedure SetArrays(Index : Integer; const AValue: TJSONArray);
- procedure SetBooleans(Index : Integer; const AValue: Boolean);
- procedure SetFloats(Index : Integer; const AValue: TJSONFloat);
- procedure SetIntegers(Index : Integer; const AValue: Integer);
- procedure SetInt64s(Index : Integer; const AValue: Int64);
- procedure SetObjects(Index : Integer; const AValue: TJSONObject);
- procedure SetQWords(Index : Integer; AValue: QWord);
- procedure SetStrings(Index : Integer; const AValue: TJSONStringType);
- protected
- Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; override;
- Procedure Converterror(From : Boolean);
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- function GetCount: Integer; override;
- function GetItem(Index : Integer): TJSONData; override;
- procedure SetItem(Index : Integer; const AValue: TJSONData); override;
- Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override;
- public
- Constructor Create; overload; reintroduce;
- Constructor Create(const Elements : Array of Const); overload;
- Destructor Destroy; override;
- class function JSONType: TJSONType; override;
- Function Clone : TJSONData; override;
- // Examine
- procedure Iterate(Iterator : TJSONArrayIterator; Data: TObject);
- function IndexOf(obj: TJSONData): Integer;
- function GetEnumerator: TBaseJSONEnumerator; override;
- // Manipulate
- Procedure Clear; override;
- function Add(Item : TJSONData): Integer;
- function Add(I : Integer): Integer;
- function Add(I : Int64): Int64;
- function Add(I : QWord): QWord;
- function Add(const S : String): Integer;
- function Add: Integer;
- function Add(F : TJSONFloat): Integer;
- function Add(B : Boolean): Integer;
- function Add(AnArray : TJSONArray): Integer;
- function Add(AnObject: TJSONObject): Integer;
- Procedure Delete(Index : Integer);
- procedure Exchange(Index1, Index2: Integer);
- function Extract(Item: TJSONData): TJSONData;
- function Extract(Index : Integer): TJSONData;
- procedure Insert(Index: Integer);
- procedure Insert(Index: Integer; Item : TJSONData);
- procedure Insert(Index: Integer; I : Integer);
- procedure Insert(Index: Integer; I : Int64);
- procedure Insert(Index: Integer; I : QWord);
- procedure Insert(Index: Integer; const S : String);
- procedure Insert(Index: Integer; F : TJSONFloat);
- procedure Insert(Index: Integer; B : Boolean);
- procedure Insert(Index: Integer; AnArray : TJSONArray);
- procedure Insert(Index: Integer; AnObject: TJSONObject);
- procedure Move(CurIndex, NewIndex: Integer);
- Procedure Remove(Item : TJSONData);
- // Easy Access Properties.
- property Items;default;
- Property Types[Index : Integer] : TJSONType Read GetTypes;
- Property Nulls[Index : Integer] : Boolean Read GetNulls;
- Property Integers[Index : Integer] : Integer Read GetIntegers Write SetIntegers;
- Property Int64s[Index : Integer] : Int64 Read GetInt64s Write SetInt64s;
- Property QWords[Index : Integer] : QWord Read GetQWords Write SetQWords;
- Property Strings[Index : Integer] : TJSONStringType Read GetStrings Write SetStrings;
- Property Floats[Index : Integer] : TJSONFloat Read GetFloats Write SetFloats;
- Property Booleans[Index : Integer] : Boolean Read GetBooleans Write SetBooleans;
- Property Arrays[Index : Integer] : TJSONArray Read GetArrays Write SetArrays;
- Property Objects[Index : Integer] : TJSONObject Read GetObjects Write SetObjects;
- end;
- TJSONArrayClass = Class of TJSONArray;
- TJSONObjectIterator = procedure(Const AName : TJSONStringType; Item: TJSONData; Data: TObject; var Continue: Boolean) of object;
- { TJSONObject }
- TJSONObject = class(TJSONData)
- private
- Const
- ElementStart : Array[Boolean] of TJSONStringType = ('"','');
- SpacedQuoted : Array[Boolean] of TJSONStringType = ('" : ',' : ');
- UnSpacedQuoted : Array[Boolean] of TJSONStringType = ('":',':');
- ObjStartSeps : Array[Boolean] of TJSONStringType = ('{ ','{');
- ObjEndSeps : Array[Boolean] of TJSONStringType = (' }','}');
- Class var FUnquotedMemberNames: Boolean;
- Class var FObjStartSep,FObjEndSep,FElementEnd,FElementStart : TJSONStringType;
- Class procedure DetermineElementQuotes;
- Private
- FHash : TFPHashObjectList; // Careful : Names limited to 255 chars.
- function GetArrays(const AName : String): TJSONArray;
- function GetBooleans(const AName : String): Boolean;
- function GetElements(const AName: string): TJSONData;
- function GetFloats(const AName : String): TJSONFloat;
- function GetIntegers(const AName : String): Integer;
- function GetInt64s(const AName : String): Int64;
- function GetIsNull(const AName : String): Boolean; reintroduce;
- function GetNameOf(Index : Integer): TJSONStringType;
- function GetObjects(const AName : String): TJSONObject;
- function GetQWords(AName : String): QWord;
- function GetStrings(const AName : String): TJSONStringType;
- function GetTypes(const AName : String): TJSONType;
- procedure SetArrays(const AName : String; const AValue: TJSONArray);
- procedure SetBooleans(const AName : String; const AValue: Boolean);
- procedure SetElements(const AName: string; const AValue: TJSONData);
- procedure SetFloats(const AName : String; const AValue: TJSONFloat);
- procedure SetIntegers(const AName : String; const AValue: Integer);
- procedure SetInt64s(const AName : String; const AValue: Int64);
- procedure SetIsNull(const AName : String; const AValue: Boolean);
- procedure SetObjects(const AName : String; const AValue: TJSONObject);
- procedure SetQWords(AName : String; AValue: QWord);
- procedure SetStrings(const AName : String; const AValue: TJSONStringType);
- class function GetUnquotedMemberNames: Boolean; static;
- class procedure SetUnquotedMemberNames(AValue: Boolean); static;
- protected
- Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; override;
- Procedure Converterror(From : Boolean);
- function GetAsBoolean: Boolean; override;
- function GetAsFloat: TJSONFloat; override;
- function GetAsInteger: Integer; override;
- function GetAsInt64: Int64; override;
- function GetAsQWord: QWord; override;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); override;
- procedure SetAsQword(const AValue: QWord); override;
- function GetAsJSON: TJSONStringType; override;
- function GetAsString: TJSONStringType; override;
- procedure SetAsString(const AValue: TJSONStringType); override;
- function GetValue: variant; override;
- procedure SetValue(const AValue: variant); override;
- function GetCount: Integer; override;
- function GetItem(Index : Integer): TJSONData; override;
- procedure SetItem(Index : Integer; const AValue: TJSONData); override;
- Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override;
- public
- constructor Create; reintroduce;
- Constructor Create(const Elements : Array of Const); overload;
- destructor Destroy; override;
- class function JSONType: TJSONType; override;
- Class Property UnquotedMemberNames : Boolean Read GetUnquotedMemberNames Write SetUnquotedMemberNames;
- Function Clone : TJSONData; override;
- function GetEnumerator: TBaseJSONEnumerator; override;
- // Examine
- procedure Iterate(Iterator : TJSONObjectIterator; Data: TObject);
- function IndexOf(Item: TJSONData): Integer;
- Function IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer;
- Function Find(Const AName : String) : TJSONData; overload;
- Function Find(Const AName : String; AType : TJSONType) : TJSONData; overload;
- Function Get(Const AName : String) : Variant;
- Function Get(Const AName : String; ADefault : TJSONFloat) : TJSONFloat;
- Function Get(Const AName : String; ADefault : Integer) : Integer;
- Function Get(Const AName : String; ADefault : Int64) : Int64;
- Function Get(Const AName : String; ADefault : QWord) : QWord;
- Function Get(Const AName : String; ADefault : Boolean) : Boolean;
- Function Get(Const AName : String; ADefault : TJSONStringType) : TJSONStringTYpe;
- Function Get(Const AName : String; ADefault : TJSONArray) : TJSONArray;
- Function Get(Const AName : String; ADefault : TJSONObject) : TJSONObject;
- // Manipulate
- Procedure Clear; override;
- function Add(const AName: TJSONStringType; AValue: TJSONData): Integer; overload;
- function Add(const AName: TJSONStringType; AValue: Boolean): Integer; overload;
- function Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer; overload;
- function Add(const AName, AValue: TJSONStringType): Integer; overload;
- function Add(const AName: TJSONStringType; Avalue: Integer): Integer; overload;
- function Add(const AName: TJSONStringType; Avalue: Int64): Integer; overload;
- function Add(const AName: TJSONStringType; Avalue: QWord): Integer; overload;
- function Add(const AName: TJSONStringType): Integer; overload;
- function Add(const AName: TJSONStringType; AValue : TJSONArray): Integer; overload;
- procedure Delete(Index : Integer);
- procedure Delete(Const AName : string);
- procedure Remove(Item : TJSONData);
- Function Extract(Index : Integer) : TJSONData;
- Function Extract(Const AName : string) : TJSONData;
- // Easy access properties.
- property Names[Index : Integer] : TJSONStringType read GetNameOf;
- property Elements[AName: string] : TJSONData read GetElements write SetElements; default;
- Property Types[AName : String] : TJSONType Read GetTypes;
- Property Nulls[AName : String] : Boolean Read GetIsNull Write SetIsNull;
- Property Floats[AName : String] : TJSONFloat Read GetFloats Write SetFloats;
- Property Integers[AName : String] : Integer Read GetIntegers Write SetIntegers;
- Property Int64s[AName : String] : Int64 Read GetInt64s Write SetInt64s;
- Property QWords[AName : String] : QWord Read GetQWords Write SetQWords;
- Property Strings[AName : String] : TJSONStringType Read GetStrings Write SetStrings;
- Property Booleans[AName : String] : Boolean Read GetBooleans Write SetBooleans;
- Property Arrays[AName : String] : TJSONArray Read GetArrays Write SetArrays;
- Property Objects[AName : String] : TJSONObject Read GetObjects Write SetObjects;
- end;
- TJSONObjectClass = Class of TJSONObject;
- EJSON = Class(Exception);
- TJSONParserHandler = Procedure(AStream : TStream; Const AUseUTF8 : Boolean; Out Data : TJSONData);
- Function SetJSONInstanceType(AType : TJSONInstanceType; AClass : TJSONDataClass) : TJSONDataClass;
- Function GetJSONInstanceType(AType : TJSONInstanceType) : TJSONDataClass;
- Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
- Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;
- Function JSONTypeName(JSONType : TJSONType) : String;
- // These functions create JSONData structures, taking into account the instance types
- Function CreateJSON : TJSONNull;
- Function CreateJSON(Data : Boolean) : TJSONBoolean;
- Function CreateJSON(Data : Integer) : TJSONIntegerNumber;
- Function CreateJSON(Data : Int64) : TJSONInt64Number;
- Function CreateJSON(Data : QWord) : TJSONQWordNumber;
- Function CreateJSON(Data : TJSONFloat) : TJSONFloatNumber;
- Function CreateJSON(Data : TJSONStringType) : TJSONString;
- Function CreateJSONArray(Data : Array of const) : TJSONArray;
- Function CreateJSONObject(Data : Array of const) : TJSONObject;
- // These functions rely on a callback. If the callback is not set, they will raise an error.
- // When the jsonparser unit is included in the project, the callback is automatically set.
- Function GetJSON(Const JSON : TJSONStringType; Const UseUTF8 : Boolean = True) : TJSONData;
- Function GetJSON(Const JSON : TStream; Const UseUTF8 : Boolean = True) : TJSONData;
- Function SetJSONParserHandler(AHandler : TJSONParserHandler) : TJSONParserHandler;
- Function GetJSONParserHandler : TJSONParserHandler;
- implementation
- Uses typinfo;
- Resourcestring
- SErrCannotConvertFromNull = 'Cannot convert data from Null value';
- SErrCannotConvertToNull = 'Cannot convert data to Null value';
- SErrCannotConvertFromArray = 'Cannot convert data from array value';
- SErrCannotConvertToArray = 'Cannot convert data to array value';
- SErrCannotConvertFromObject = 'Cannot convert data from object value';
- SErrCannotConvertToObject = 'Cannot convert data to object value';
- SErrInvalidFloat = 'Invalid float value : %s';
- SErrInvalidInteger = 'Invalid float value : %s';
- SErrCannotSetNotIsNull = 'IsNull cannot be set to False';
- SErrCannotAddArrayTwice = 'Adding an array object to an array twice is not allowed';
- SErrCannotAddObjectTwice = 'Adding an object to an array twice is not allowed';
- SErrUnknownTypeInConstructor = 'Unknown type in JSON%s constructor: %d';
- SErrNotJSONData = 'Cannot add object of type %s to TJSON%s';
- SErrPointerNotNil = 'Cannot add non-nil pointer to JSON%s';
- SErrOddNumber = 'TJSONObject must be constructed with name,value pairs';
- SErrNameMustBeString = 'TJSONObject constructor element name at pos %d is not a string';
- SErrNonexistentElement = 'Unknown object member: "%s"';
- SErrPathElementNotFound = 'Path "%s" invalid: element "%s" not found.';
- SErrWrongInstanceClass = 'Cannot set instance class: %s does not descend from %s.';
- SErrNoParserHandler = 'No JSON parser handler installed. Recompile your project with the jsonparser unit included';
- Var
- DefaultJSONInstanceTypes :
- Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber,
- TJSONInt64Number, TJSONQWordNumber, TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
- TJSONObject);
- Const
- MinJSONInstanceTypes :
- Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber,
- TJSONInt64Number, TJSONQWordNumber, TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
- TJSONObject);
- function SetJSONInstanceType(AType: TJSONInstanceType; AClass: TJSONDataClass): TJSONDataClass;
- begin
- if AClass=Nil then
- TJSONData.DoError(SErrWrongInstanceClass,['Nil',MinJSONInstanceTypes[AType].ClassName]);
- if Not AClass.InheritsFrom(MinJSONINstanceTypes[AType]) then
- TJSONData.DoError(SErrWrongInstanceClass,[AClass.ClassName,MinJSONInstanceTypes[AType].ClassName]);
- Result:=DefaultJSONInstanceTypes[AType];
- DefaultJSONINstanceTypes[AType]:=AClass;
- end;
- function GetJSONInstanceType(AType: TJSONInstanceType): TJSONDataClass;
- begin
- Result:=DefaultJSONInstanceTypes[AType]
- end;
- Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
- Var
- I,J,L : Integer;
- P : PJSONCharType;
- begin
- I:=1;
- J:=1;
- Result:='';
- L:=Length(S);
- P:=PJSONCharType(S);
- While I<=L do
- begin
- if (AnsiChar(P^) in ['"','/','\',#8,#9,#10,#12,#13]) then
- begin
- Result:=Result+Copy(S,J,I-J);
- Case P^ of
- '\' : Result:=Result+'\\';
- '/' : Result:=Result+'\/';
- '"' : Result:=Result+'\"';
- #8 : Result:=Result+'\b';
- #9 : Result:=Result+'\t';
- #10 : Result:=Result+'\n';
- #12 : Result:=Result+'\f';
- #13 : Result:=Result+'\r';
- end;
- J:=I+1;
- end;
- Inc(I);
- Inc(P);
- end;
- Result:=Result+Copy(S,J,I-1);
- end;
- Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;
- Var
- I,J,L : Integer;
- P : PJSONCharType;
- w : String;
- begin
- I:=1;
- J:=1;
- L:=Length(S);
- Result:='';
- P:=PJSONCharType(S);
- While (I<=L) do
- begin
- if (P^='\') then
- begin
- Result:=Result+Copy(S,J,I-J);
- Inc(P);
- If (P^<>#0) then
- begin
- Inc(I);
- Case AnsiChar(P^) of
- '\','"','/'
- : Result:=Result+P^;
- 'b' : Result:=Result+#8;
- 't' : Result:=Result+#9;
- 'n' : Result:=Result+#10;
- 'f' : Result:=Result+#12;
- 'r' : Result:=Result+#13;
- 'u' : begin
- W:=Copy(S,I+1,4);
- Inc(I,4);
- Inc(P,4);
- Result:=Result+WideChar(StrToInt('$'+W));
- end;
- end;
- end;
- J:=I+1;
- end;
- Inc(I);
- Inc(P);
- end;
- Result:=Result+Copy(S,J,I-J+1);
- end;
- function JSONTypeName(JSONType: TJSONType): String;
- begin
- Result:=GetEnumName(TypeInfo(TJSONType),Ord(JSONType));
- end;
- function CreateJSON: TJSONNull;
- begin
- Result:=TJSONNullClass(DefaultJSONInstanceTypes[jitNull]).Create
- end;
- function CreateJSON(Data: Boolean): TJSONBoolean;
- begin
- Result:=TJSONBooleanClass(DefaultJSONInstanceTypes[jitBoolean]).Create(Data);
- end;
- function CreateJSON(Data: Integer): TJSONIntegerNumber;
- begin
- Result:=TJSONIntegerNumberCLass(DefaultJSONInstanceTypes[jitNumberInteger]).Create(Data);
- end;
- function CreateJSON(Data: Int64): TJSONInt64Number;
- begin
- Result:=TJSONInt64NumberCLass(DefaultJSONInstanceTypes[jitNumberInt64]).Create(Data);
- end;
- function CreateJSON(Data: QWord): TJSONQWordNumber;
- begin
- Result:=TJSONQWordNumberClass(DefaultJSONInstanceTypes[jitNumberQWord]).Create(Data);
- end;
- function CreateJSON(Data: TJSONFloat): TJSONFloatNumber;
- begin
- Result:=TJSONFloatNumberCLass(DefaultJSONInstanceTypes[jitNumberFloat]).Create(Data);
- end;
- function CreateJSON(Data: TJSONStringType): TJSONString;
- begin
- Result:=TJSONStringCLass(DefaultJSONInstanceTypes[jitString]).Create(Data);
- end;
- function CreateJSONArray(Data: array of const): TJSONArray;
- begin
- Result:=TJSONArrayCLass(DefaultJSONInstanceTypes[jitArray]).Create(Data);
- end;
- function CreateJSONObject(Data: array of const): TJSONObject;
- begin
- Result:=TJSONObjectCLass(DefaultJSONInstanceTypes[jitObject]).Create(Data);
- end;
- Var
- JPH : TJSONParserHandler;
- function GetJSON(const JSON: TJSONStringType; Const UseUTF8: Boolean): TJSONData;
- Var
- SS : TStringStream;
- begin
- SS:=TStringStream.Create(JSON);
- try
- Result:=GetJSON(SS,UseUTF8);
- finally
- SS.Free;
- end;
- end;
- function GetJSON(Const JSON: TStream; Const UseUTF8: Boolean): TJSONData;
- begin
- Result:=Nil;
- If (JPH=Nil) then
- TJSONData.DoError(SErrNoParserHandler);
- JPH(JSON,UseUTF8,Result);
- end;
- function SetJSONParserHandler(AHandler: TJSONParserHandler): TJSONParserHandler;
- begin
- Result:=JPH;
- JPH:=AHandler;
- end;
- function GetJSONParserHandler: TJSONParserHandler;
- begin
- Result:=JPH;
- end;
- Type
- { TJSONEnumerator }
- TJSONEnumerator = class(TBaseJSONEnumerator)
- Private
- FData : TJSONData;
- public
- Constructor Create(AData : TJSONData);
- function GetCurrent: TJSONEnum; override;
- function MoveNext : Boolean; override;
- end;
- { TJSONArrayEnumerator }
- TJSONArrayEnumerator = class(TBaseJSONEnumerator)
- Private
- FData : TJSONArray;
- FCurrent : Integer;
- public
- Constructor Create(AData : TJSONArray);
- function GetCurrent: TJSONEnum; override;
- function MoveNext : Boolean; override;
- end;
- { TJSONObjectEnumerator }
- TJSONObjectEnumerator = class(TBaseJSONEnumerator)
- Private
- FData : TJSONObject;
- FCurrent : Integer;
- public
- Constructor Create(AData : TJSONObject);
- function GetCurrent: TJSONEnum; override;
- function MoveNext : Boolean; override;
- end;
- { TJSONQWordNumber }
- function TJSONQWordNumber.GetAsBoolean: Boolean;
- begin
- Result:=FValue<>0;
- end;
- function TJSONQWordNumber.GetAsFloat: TJSONFloat;
- begin
- Result:= FValue;
- end;
- function TJSONQWordNumber.GetAsInteger: Integer;
- begin
- Result := FValue;
- end;
- function TJSONQWordNumber.GetAsInt64: Int64;
- begin
- Result := FValue;
- end;
- function TJSONQWordNumber.GetAsQWord: QWord;
- begin
- Result := FValue;
- end;
- procedure TJSONQWordNumber.SetAsBoolean(const AValue: Boolean);
- begin
- FValue:=Ord(AValue);
- end;
- procedure TJSONQWordNumber.SetAsFloat(const AValue: TJSONFloat);
- begin
- FValue:=Round(AValue);
- end;
- procedure TJSONQWordNumber.SetAsInteger(const AValue: Integer);
- begin
- FValue:=AValue;
- end;
- procedure TJSONQWordNumber.SetAsInt64(const AValue: Int64);
- begin
- FValue := AValue;
- end;
- procedure TJSONQWordNumber.SetAsQword(const AValue: QWord);
- begin
- FValue:=AValue;
- end;
- function TJSONQWordNumber.GetAsJSON: TJSONStringType;
- begin
- Result:=AsString;
- end;
- function TJSONQWordNumber.GetAsString: TJSONStringType;
- begin
- Result:=IntToStr(FValue);
- end;
- procedure TJSONQWordNumber.SetAsString(const AValue: TJSONStringType);
- begin
- FValue:=StrToQWord(AValue);
- end;
- function TJSONQWordNumber.GetValue: variant;
- begin
- Result:=FValue;
- end;
- procedure TJSONQWordNumber.SetValue(const AValue: variant);
- begin
- FValue:=AValue;
- end;
- constructor TJSONQWordNumber.Create(AValue: QWord);
- begin
- FValue := AValue;
- end;
- class function TJSONQWordNumber.NumberType: TJSONNumberType;
- begin
- Result:=ntQWord;
- end;
- procedure TJSONQWordNumber.Clear;
- begin
- FValue:=0;
- end;
- function TJSONQWordNumber.Clone: TJSONData;
- begin
- Result:=TJSONQWordNumberClass(ClassType).Create(Self.FValue);
- end;
- constructor TJSONObjectEnumerator.Create(AData: TJSONObject);
- begin
- FData:=AData;
- FCurrent:=-1;
- end;
- function TJSONObjectEnumerator.GetCurrent: TJSONEnum;
- begin
- Result.KeyNum:=FCurrent;
- Result.Key:=FData.Names[FCurrent];
- Result.Value:=FData.Items[FCurrent];
- end;
- function TJSONObjectEnumerator.MoveNext: Boolean;
- begin
- Inc(FCurrent);
- Result:=FCurrent<FData.Count;
- end;
- { TJSONArrayEnumerator }
- constructor TJSONArrayEnumerator.Create(AData: TJSONArray);
- begin
- FData:=AData;
- FCurrent:=-1;
- end;
- function TJSONArrayEnumerator.GetCurrent: TJSONEnum;
- begin
- Result.KeyNum:=FCurrent;
- Result.Key:=IntToStr(FCurrent);
- Result.Value:=FData.Items[FCurrent];
- end;
- function TJSONArrayEnumerator.MoveNext: Boolean;
- begin
- Inc(FCurrent);
- Result:=FCurrent<FData.Count;
- end;
- { TJSONEnumerator }
- constructor TJSONEnumerator.Create(AData: TJSONData);
- begin
- FData:=AData;
- end;
- function TJSONEnumerator.GetCurrent: TJSONEnum;
- begin
- Result.Key:='';
- Result.KeyNum:=0;
- Result.Value:=FData;
- FData:=Nil;
- end;
- function TJSONEnumerator.MoveNext: Boolean;
- begin
- Result:=FData<>Nil;
- end;
- { TJSONData }
- function TJSONData.GetItem(Index : Integer): TJSONData;
- begin
- Result:=nil;
- end;
- function TJSONData.GetCount: Integer;
- begin
- Result:=0;
- end;
- constructor TJSONData.Create;
- begin
- Clear;
- end;
- procedure TJSONData.DumpJSON(S: TStream);
- Procedure W(T : String);
- begin
- if (T<>'') then
- S.WriteBuffer(T[1],Length(T)*SizeOf(Char));
- end;
- Var
- I,C : Integer;
- O : TJSONObject;
- begin
- Case JSONType of
- jtObject :
- begin
- O:=TJSONObject(Self);
- W('{');
- For I:=0 to O.Count-1 do
- begin
- if (I>0) then
- W(',');
- W('"');
- W(StringToJSONString(O.Names[i]));
- W('":');
- O.Items[I].DumpJSON(S);
- end;
- W('}');
- end;
- jtArray :
- begin
- W('[');
- For I:=0 to Count-1 do
- begin
- if (I>0) then
- W(',');
- Items[I].DumpJSON(S);
- end;
- W(']');
- end
- else
- W(AsJSON)
- end;
- end;
- class function TJSONData.GetCompressedJSON: Boolean; static;
- begin
- Result:=FCompressedJSON;
- end;
- class procedure TJSONData.DetermineElementSeparators;
- begin
- FElementSep:=ElementSeps[FCompressedJSON];
- end;
- class procedure TJSONData.SetCompressedJSON(AValue: Boolean); static;
- begin
- if AValue=FCompressedJSON then exit;
- FCompressedJSON:=AValue;
- DetermineElementSeparators;
- TJSONObject.DetermineElementQuotes;
- end;
- class procedure TJSONData.DoError(const Msg: String);
- begin
- Raise EJSON.Create(Msg);
- end;
- class procedure TJSONData.DoError(const Fmt: String;
- const Args: array of const);
- begin
- Raise EJSON.CreateFmt(Fmt,Args);
- end;
- function TJSONData.DoFindPath(const APath: TJSONStringType; out
- NotFound: TJSONStringType): TJSONdata;
- begin
- If APath<>'' then
- begin
- NotFound:=APath;
- Result:=Nil;
- end
- else
- Result:=Self;
- end;
- function TJSONData.GetIsNull: Boolean;
- begin
- Result:=False;
- end;
- class function TJSONData.JSONType: TJSONType;
- begin
- JSONType:=jtUnknown;
- end;
- function TJSONData.GetEnumerator: TBaseJSONEnumerator;
- begin
- Result:=TJSONEnumerator.Create(Self);
- end;
- function TJSONData.FindPath(const APath: TJSONStringType): TJSONdata;
- Var
- M : String;
- begin
- Result:=DoFindPath(APath,M);
- end;
- function TJSONData.GetPath(const APath: TJSONStringType): TJSONdata;
- Var
- M : String;
- begin
- Result:=DoFindPath(APath,M);
- If Result=Nil then
- DoError(SErrPathElementNotFound,[APath,M]);
- end;
- procedure TJSONData.SetItem(Index : Integer; const AValue:
- TJSONData);
- begin
- // Do Nothing
- end;
- function TJSONData.FormatJSON(Options: TFormatOptions; Indentsize: Integer
- ): TJSONStringType;
- begin
- Result:=DoFormatJSON(Options,0,IndentSize);
- end;
- function TJSONData.DoFormatJSON(Options: TFormatOptions; CurrentIndent,
- Indent: Integer): TJSONStringType;
- begin
- Result:=AsJSON;
- end;
- { TJSONnumber }
- class function TJSONnumber.JSONType: TJSONType;
- begin
- Result:=jtNumber;
- end;
- { TJSONstring }
- class function TJSONString.JSONType: TJSONType;
- begin
- Result:=jtString;
- end;
- procedure TJSONString.Clear;
- begin
- FValue:='';
- end;
- function TJSONString.Clone: TJSONData;
- begin
- Result:=TJSONStringClass(ClassType).Create(Self.FValue);
- end;
- function TJSONString.GetValue: Variant;
- begin
- Result:=FValue;
- end;
- procedure TJSONString.SetValue(const AValue: Variant);
- begin
- FValue:=AValue;
- end;
- function TJSONString.GetAsBoolean: Boolean;
- begin
- Result:=StrToBool(FValue);
- end;
- function TJSONString.GetAsFloat: TJSONFloat;
- Var
- C : Integer;
- begin
- Val(FValue,Result,C);
- If (C<>0) then
- If Not TryStrToFloat(FValue,Result) then
- Raise EConvertError.CreateFmt(SErrInvalidFloat,[FValue]);
- end;
- function TJSONString.GetAsInteger: Integer;
- begin
- Result:=StrToInt(FValue);
- end;
- function TJSONString.GetAsInt64: Int64;
- begin
- Result:=StrToInt64(FValue);
- end;
- function TJSONString.GetAsQWord: QWord;
- begin
- Result:=StrToQWord(FValue);
- end;
- procedure TJSONString.SetAsBoolean(const AValue: Boolean);
- begin
- FValue:=BoolToStr(AValue);
- end;
- procedure TJSONString.SetAsFloat(const AValue: TJSONFloat);
- begin
- FValue:=FloatToStr(AValue);
- end;
- procedure TJSONString.SetAsInteger(const AValue: Integer);
- begin
- FValue:=IntToStr(AValue);
- end;
- procedure TJSONString.SetAsInt64(const AValue: Int64);
- begin
- FValue:=IntToStr(AValue);
- end;
- procedure TJSONString.SetAsQword(const AValue: QWord);
- begin
- FValue:=IntToStr(AValue);
- end;
- function TJSONString.GetAsJSON: TJSONStringType;
- begin
- Result:='"'+StringToJSONString(FValue)+'"';
- end;
- function TJSONString.GetAsString: TJSONStringType;
- begin
- Result:=FValue;
- end;
- procedure TJSONString.SetAsString(const AValue: TJSONStringType);
- begin
- FValue:=AValue;
- end;
- constructor TJSONString.Create(const AValue: TJSONStringType);
- begin
- FValue:=AValue;
- end;
- { TJSONboolean }
- function TJSONBoolean.GetValue: Variant;
- begin
- Result:=FValue;
- end;
- class function TJSONBoolean.JSONType: TJSONType;
- begin
- Result:=jtBoolean;
- end;
- procedure TJSONBoolean.Clear;
- begin
- FValue:=False;
- end;
- function TJSONBoolean.Clone: TJSONData;
- begin
- Result:=TJSONBooleanClass(Self.ClassType).Create(Self.Fvalue);
- end;
- procedure TJSONBoolean.SetValue(const AValue: Variant);
- begin
- FValue:=boolean(AValue);
- end;
- function TJSONBoolean.GetAsBoolean: Boolean;
- begin
- Result:=FValue;
- end;
- function TJSONBoolean.GetAsFloat: TJSONFloat;
- begin
- Result:=Ord(FValue);
- end;
- function TJSONBoolean.GetAsInteger: Integer;
- begin
- Result:=Ord(FValue);
- end;
- function TJSONBoolean.GetAsInt64: Int64;
- begin
- Result:=Ord(FValue);
- end;
- function TJSONBoolean.GetAsQWord: QWord;
- begin
- Result:=Ord(FValue);
- end;
- procedure TJSONBoolean.SetAsBoolean(const AValue: Boolean);
- begin
- FValue:=AValue;
- end;
- procedure TJSONBoolean.SetAsFloat(const AValue: TJSONFloat);
- begin
- FValue:=(AValue<>0)
- end;
- procedure TJSONBoolean.SetAsInteger(const AValue: Integer);
- begin
- FValue:=(AValue<>0)
- end;
- procedure TJSONBoolean.SetAsInt64(const AValue: Int64);
- begin
- FValue:=(AValue<>0)
- end;
- procedure TJSONBoolean.SetAsQword(const AValue: QWord);
- begin
- FValue:=(AValue<>0)
- end;
- function TJSONBoolean.GetAsJSON: TJSONStringType;
- begin
- If FValue then
- Result:='true'
- else
- Result:='false';
- end;
- function TJSONBoolean.GetAsString: TJSONStringType;
- begin
- Result:=BoolToStr(FValue, True);
- end;
- procedure TJSONBoolean.SetAsString(const AValue: TJSONStringType);
- begin
- FValue:=StrToBool(AValue);
- end;
- constructor TJSONBoolean.Create(AValue: Boolean);
- begin
- FValue:=AValue;
- end;
- { TJSONnull }
- procedure TJSONNull.Converterror(From: Boolean);
- begin
- If From then
- DoError(SErrCannotConvertFromNull)
- else
- DoError(SErrCannotConvertToNull);
- end;
- {$warnings off}
- function TJSONNull.GetAsBoolean: Boolean;
- begin
- ConvertError(True);
- end;
- function TJSONNull.GetAsFloat: TJSONFloat;
- begin
- ConvertError(True);
- end;
- function TJSONNull.GetAsInteger: Integer;
- begin
- ConvertError(True);
- end;
- function TJSONNull.GetAsInt64: Int64;
- begin
- ConvertError(True);
- end;
- function TJSONNull.GetAsQWord: QWord;
- begin
- ConvertError(True);
- end;
- function TJSONNull.GetIsNull: Boolean;
- begin
- Result:=True;
- end;
- procedure TJSONNull.SetAsBoolean(const AValue: Boolean);
- begin
- ConvertError(False);
- end;
- procedure TJSONNull.SetAsFloat(const AValue: TJSONFloat);
- begin
- ConvertError(False);
- end;
- procedure TJSONNull.SetAsInteger(const AValue: Integer);
- begin
- ConvertError(False);
- end;
- procedure TJSONNull.SetAsInt64(const AValue: Int64);
- begin
- ConvertError(False);
- end;
- procedure TJSONNull.SetAsQword(const AValue: QWord);
- begin
- ConvertError(False);
- end;
- function TJSONNull.GetAsJSON: TJSONStringType;
- begin
- Result:='null';
- end;
- function TJSONNull.GetAsString: TJSONStringType;
- begin
- ConvertError(True);
- end;
- procedure TJSONNull.SetAsString(const AValue: TJSONStringType);
- begin
- ConvertError(True);
- end;
- function TJSONNull.GetValue: variant;
- begin
- Result:=variants.Null;
- end;
- procedure TJSONNull.SetValue(const AValue: variant);
- begin
- ConvertError(False);
- end;
- class function TJSONNull.JSONType: TJSONType;
- begin
- Result:=jtNull;
- end;
- procedure TJSONNull.Clear;
- begin
- // Do nothing
- end;
- function TJSONNull.Clone: TJSONData;
- begin
- Result:=TJSONNullClass(Self.ClassType).Create;
- end;
- {$warnings on}
- { TJSONFloatNumber }
- function TJSONFloatNumber.GetAsBoolean: Boolean;
- begin
- Result:=(FValue<>0);
- end;
- function TJSONFloatNumber.GetAsFloat: TJSONFloat;
- begin
- Result:=FValue;
- end;
- function TJSONFloatNumber.GetAsInteger: Integer;
- begin
- Result:=Round(FValue);
- end;
- function TJSONFloatNumber.GetAsInt64: Int64;
- begin
- Result:=Round(FValue);
- end;
- function TJSONFloatNumber.GetAsQWord: QWord;
- begin
- Result:=Round(FValue);
- end;
- procedure TJSONFloatNumber.SetAsBoolean(const AValue: Boolean);
- begin
- FValue:=Ord(AValue);
- end;
- procedure TJSONFloatNumber.SetAsFloat(const AValue: TJSONFloat);
- begin
- FValue:=AValue;
- end;
- procedure TJSONFloatNumber.SetAsInteger(const AValue: Integer);
- begin
- FValue:=AValue;
- end;
- procedure TJSONFloatNumber.SetAsInt64(const AValue: Int64);
- begin
- FValue:=AValue;
- end;
- procedure TJSONFloatNumber.SetAsQword(const AValue: QWord);
- begin
- FValue:=AValue;
- end;
- function TJSONFloatNumber.GetAsJSON: TJSONStringType;
- begin
- Result:=AsString;
- end;
- function TJSONFloatNumber.GetAsString: TJSONStringType;
- begin
- Str(FValue,Result);
- // Str produces a ' ' in front where the - can go.
- if (Result<>'') and (Result[1]=' ') then
- Delete(Result,1,1);
- end;
- procedure TJSONFloatNumber.SetAsString(const AValue: TJSONStringType);
- Var
- C : Integer;
- begin
- Val(AValue,FValue,C);
- If (C<>0) then
- Raise EConvertError.CreateFmt(SErrInvalidFloat,[AValue]);
- end;
- function TJSONFloatNumber.GetValue: variant;
- begin
- Result:=FValue;
- end;
- procedure TJSONFloatNumber.SetValue(const AValue: variant);
- begin
- FValue:=AValue;
- end;
- constructor TJSONFloatNumber.Create(AValue: TJSONFloat);
- begin
- FValue:=AValue;
- end;
- class function TJSONFloatNumber.NumberType: TJSONNumberType;
- begin
- Result:=ntFloat;
- end;
- procedure TJSONFloatNumber.Clear;
- begin
- FValue:=0;
- end;
- function TJSONFloatNumber.Clone: TJSONData;
- begin
- Result:=TJSONFloatNumberClass(ClassType).Create(Self.FValue);
- end;
- { TJSONIntegerNumber }
- function TJSONIntegerNumber.GetAsBoolean: Boolean;
- begin
- Result:=FValue<>0;
- end;
- function TJSONIntegerNumber.GetAsFloat: TJSONFloat;
- begin
- Result:=Ord(FValue);
- end;
- function TJSONIntegerNumber.GetAsInteger: Integer;
- begin
- Result:=FValue;
- end;
- function TJSONIntegerNumber.GetAsInt64: Int64;
- begin
- Result:=FValue;
- end;
- function TJSONIntegerNumber.GetAsQWord: QWord;
- begin
- result:=FValue;
- end;
- procedure TJSONIntegerNumber.SetAsBoolean(const AValue: Boolean);
- begin
- FValue:=Ord(AValue);
- end;
- procedure TJSONIntegerNumber.SetAsFloat(const AValue: TJSONFloat);
- begin
- FValue:=Round(AValue);
- end;
- procedure TJSONIntegerNumber.SetAsInteger(const AValue: Integer);
- begin
- FValue:=AValue;
- end;
- procedure TJSONIntegerNumber.SetAsInt64(const AValue: Int64);
- begin
- FValue:=AValue;
- end;
- procedure TJSONIntegerNumber.SetAsQword(const AValue: QWord);
- begin
- FValue:=AValue;
- end;
- function TJSONIntegerNumber.GetAsJSON: TJSONStringType;
- begin
- Result:=AsString;
- end;
- function TJSONIntegerNumber.GetAsString: TJSONStringType;
- begin
- Result:=IntToStr(FValue)
- end;
- procedure TJSONIntegerNumber.SetAsString(const AValue: TJSONStringType);
- begin
- FValue:=StrToInt(AValue);
- end;
- function TJSONIntegerNumber.GetValue: variant;
- begin
- Result:=FValue;
- end;
- procedure TJSONIntegerNumber.SetValue(const AValue: variant);
- begin
- FValue:=AValue;
- end;
- constructor TJSONIntegerNumber.Create(AValue: Integer);
- begin
- FValue:=AValue;
- end;
- class function TJSONIntegerNumber.NumberType: TJSONNumberType;
- begin
- Result:=ntInteger;
- end;
- procedure TJSONIntegerNumber.Clear;
- begin
- FValue:=0;
- end;
- function TJSONIntegerNumber.Clone: TJSONData;
- begin
- Result:=TJSONIntegerNumberClass(ClassType).Create(Self.FValue);
- end;
- { TJSONInt64Number }
- function TJSONInt64Number.GetAsInt64: Int64;
- begin
- Result := FValue;
- end;
- function TJSONInt64Number.GetAsQWord: QWord;
- begin
- Result := FValue;
- end;
- procedure TJSONInt64Number.SetAsInt64(const AValue: Int64);
- begin
- FValue := AValue;
- end;
- procedure TJSONInt64Number.SetAsQword(const AValue: QWord);
- begin
- FValue := AValue;
- end;
- function TJSONInt64Number.GetAsBoolean: Boolean;
- begin
- Result:=FValue<>0;
- end;
- function TJSONInt64Number.GetAsFloat: TJSONFloat;
- begin
- Result:= FValue;
- end;
- function TJSONInt64Number.GetAsInteger: Integer;
- begin
- Result := FValue;
- end;
- procedure TJSONInt64Number.SetAsBoolean(const AValue: Boolean);
- begin
- FValue:=Ord(AValue);
- end;
- procedure TJSONInt64Number.SetAsFloat(const AValue: TJSONFloat);
- begin
- FValue:=Round(AValue);
- end;
- procedure TJSONInt64Number.SetAsInteger(const AValue: Integer);
- begin
- FValue:=AValue;
- end;
- function TJSONInt64Number.GetAsJSON: TJSONStringType;
- begin
- Result:=AsString;
- end;
- function TJSONInt64Number.GetAsString: TJSONStringType;
- begin
- Result:=IntToStr(FValue)
- end;
- procedure TJSONInt64Number.SetAsString(const AValue: TJSONStringType);
- begin
- FValue:=StrToInt64(AValue);
- end;
- function TJSONInt64Number.GetValue: variant;
- begin
- Result:=FValue;
- end;
- procedure TJSONInt64Number.SetValue(const AValue: variant);
- begin
- FValue:=AValue;
- end;
- constructor TJSONInt64Number.Create(AValue: Int64);
- begin
- FValue := AValue;
- end;
- class function TJSONInt64Number.NumberType: TJSONNumberType;
- begin
- Result:=ntInt64;
- end;
- procedure TJSONInt64Number.Clear;
- begin
- FValue:=0;
- end;
- function TJSONInt64Number.Clone: TJSONData;
- begin
- Result:=TJSONInt64NumberClass(ClassType).Create(Self.FValue);
- end;
- { TJSONArray }
- function TJSONArray.GetBooleans(Index : Integer): Boolean;
- begin
- Result:=Items[Index].AsBoolean;
- end;
- function TJSONArray.GetArrays(Index : Integer): TJSONArray;
- begin
- Result:=Items[Index] as TJSONArray;
- end;
- function TJSONArray.GetFloats(Index : Integer): TJSONFloat;
- begin
- Result:=Items[Index].AsFloat;
- end;
- function TJSONArray.GetIntegers(Index : Integer): Integer;
- begin
- Result:=Items[Index].AsInteger;
- end;
- function TJSONArray.GetInt64s(Index : Integer): Int64;
- begin
- Result:=Items[Index].AsInt64;
- end;
- function TJSONArray.GetNulls(Index : Integer): Boolean;
- begin
- Result:=Items[Index].IsNull;
- end;
- function TJSONArray.GetObjects(Index : Integer): TJSONObject;
- begin
- Result:=Items[Index] as TJSONObject;
- end;
- function TJSONArray.GetQWords(Index : Integer): QWord;
- begin
- Result:=Items[Index].AsQWord;
- end;
- function TJSONArray.GetStrings(Index : Integer): TJSONStringType;
- begin
- Result:=Items[Index].AsString;
- end;
- function TJSONArray.GetTypes(Index : Integer): TJSONType;
- begin
- Result:=Items[Index].JSONType;
- end;
- procedure TJSONArray.SetArrays(Index : Integer; const AValue: TJSONArray);
- begin
- Items[Index]:=AValue;
- end;
- procedure TJSONArray.SetBooleans(Index : Integer; const AValue: Boolean);
- begin
- Items[Index]:=CreateJSON(AValue);
- end;
- procedure TJSONArray.SetFloats(Index : Integer; const AValue: TJSONFloat);
- begin
- Items[Index]:=CreateJSON(AValue);
- end;
- procedure TJSONArray.SetIntegers(Index : Integer; const AValue: Integer);
- begin
- Items[Index]:=CreateJSON(AValue);
- end;
- procedure TJSONArray.SetInt64s(Index : Integer; const AValue: Int64);
- begin
- Items[Index]:=CreateJSON(AValue);
- end;
- procedure TJSONArray.SetObjects(Index : Integer; const AValue: TJSONObject);
- begin
- Items[Index]:=AValue;
- end;
- procedure TJSONArray.SetQWords(Index : Integer; AValue: QWord);
- begin
- Items[Index]:=CreateJSON(AValue);
- end;
- procedure TJSONArray.SetStrings(Index : Integer; const AValue: TJSONStringType);
- begin
- Items[Index]:=CreateJSON(AValue);
- end;
- function TJSONArray.DoFindPath(const APath: TJSONStringType; out
- NotFound: TJSONStringType): TJSONdata;
- Var
- P,I : integer;
- E : String;
- begin
- if (APath<>'') and (APath[1]='[') then
- begin
- P:=Pos(']',APath);
- I:=-1;
- If (P>2) then
- I:=StrToIntDef(Copy(APath,2,P-2),-1);
- If (I>=0) and (I<Count) then
- begin
- E:=APath;
- System.Delete(E,1,P);
- Result:=Items[i].DoFindPath(E,NotFound);
- end
- else
- begin
- Result:=Nil;
- If (P>0) then
- NotFound:=Copy(APath,1,P)
- else
- NotFound:=APath;
- end;
- end
- else
- Result:=inherited DoFindPath(APath, NotFound);
- end;
- procedure TJSONArray.Converterror(From: Boolean);
- begin
- If From then
- DoError(SErrCannotConvertFromArray)
- else
- DoError(SErrCannotConvertToArray);
- end;
- {$warnings off}
- function TJSONArray.GetAsBoolean: Boolean;
- begin
- ConvertError(True);
- end;
- function TJSONArray.GetAsFloat: TJSONFloat;
- begin
- ConvertError(True);
- end;
- function TJSONArray.GetAsInteger: Integer;
- begin
- ConvertError(True);
- end;
- function TJSONArray.GetAsInt64: Int64;
- begin
- ConvertError(True);
- end;
- function TJSONArray.GetAsQWord: QWord;
- begin
- ConvertError(True);
- end;
- procedure TJSONArray.SetAsBoolean(const AValue: Boolean);
- begin
- ConvertError(False);
- end;
- procedure TJSONArray.SetAsFloat(const AValue: TJSONFloat);
- begin
- ConvertError(False);
- end;
- procedure TJSONArray.SetAsInteger(const AValue: Integer);
- begin
- ConvertError(False);
- end;
- procedure TJSONArray.SetAsInt64(const AValue: Int64);
- begin
- ConvertError(False);
- end;
- procedure TJSONArray.SetAsQword(const AValue: QWord);
- begin
- ConvertError(False);
- end;
- {$warnings on}
- function TJSONArray.GetAsJSON: TJSONStringType;
- Var
- I : Integer;
- Sep : String;
- begin
- Sep:=TJSONData.FElementSep;
- Result:='[';
- For I:=0 to Count-1 do
- begin
- Result:=Result+Items[i].AsJSON;
- If (I<Count-1) then
- Result:=Result+Sep;
- end;
- Result:=Result+']';
- end;
- {$warnings off}
- Function IndentString(Options : TFormatOptions; Indent : Integer) : TJSONStringType;
- begin
- If (foUseTabChar in Options) then
- Result:=StringofChar(#9,Indent)
- else
- Result:=StringOfChar(' ',Indent);
- end;
- function TJSONArray.DoFormatJSON(Options: TFormatOptions; CurrentIndent,
- Indent: Integer): TJSONStringType;
- Var
- I : Integer;
- MultiLine : Boolean;
- SkipWhiteSpace : Boolean;
- Ind : String;
-
- begin
- Result:='[';
- MultiLine:=Not (foSingleLineArray in Options);
- SkipWhiteSpace:=foSkipWhiteSpace in Options;
- Ind:=IndentString(Options, CurrentIndent+Indent);
- if MultiLine then
- Result:=Result+sLineBreak;
- For I:=0 to Count-1 do
- begin
- if MultiLine then
- Result:=Result+Ind;
- Result:=Result+Items[i].DoFormatJSON(Options,CurrentIndent+Indent,Indent);
- If (I<Count-1) then
- if MultiLine then
- Result:=Result+','
- else
- Result:=Result+ElementSeps[SkipWhiteSpace];
- if MultiLine then
- Result:=Result+sLineBreak
- end;
- if MultiLine then
- Result:=Result+IndentString(Options, CurrentIndent);
- Result:=Result+']';
- end;
- function TJSONArray.GetAsString: TJSONStringType;
- begin
- ConvertError(True);
- end;
- procedure TJSONArray.SetAsString(const AValue: TJSONStringType);
- begin
- ConvertError(False);
- end;
- function TJSONArray.GetValue: variant;
- begin
- ConvertError(True);
- end;
- procedure TJSONArray.SetValue(const AValue: variant);
- begin
- ConvertError(False);
- end;
- {$warnings on}
- function TJSONArray.GetCount: Integer;
- begin
- Result:=Flist.Count;
- end;
- function TJSONArray.GetItem(Index: Integer): TJSONData;
- begin
- Result:=FList[Index] as TJSONData;
- end;
- procedure TJSONArray.SetItem(Index: Integer; const AValue: TJSONData);
- begin
- If (Index=FList.Count) then
- FList.Add(AValue)
- else
- FList[Index]:=AValue;
- end;
- constructor TJSONArray.Create;
- begin
- Flist:=TFPObjectList.Create(True);
- end;
- Function VarRecToJSON(Const Element : TVarRec; const SourceType : String) : TJSONData;
- begin
- Result:=Nil;
- With Element do
- case VType of
- vtInteger : Result:=CreateJSON(VInteger);
- vtBoolean : Result:=CreateJSON(VBoolean);
- vtChar : Result:=CreateJSON(VChar);
- vtExtended : Result:=CreateJSON(VExtended^);
- vtString : Result:=CreateJSON(vString^);
- vtAnsiString : Result:=CreateJSON(AnsiString(vAnsiString));
- vtPChar : Result:=CreateJSON(StrPas(VPChar));
- vtPointer : If (VPointer<>Nil) then
- TJSONData.DoError(SErrPointerNotNil,[SourceType])
- else
- Result:=CreateJSON();
- vtCurrency : Result:=CreateJSON(vCurrency^);
- vtInt64 : Result:=CreateJSON(vInt64^);
- vtObject : if (VObject is TJSONData) then
- Result:=TJSONData(VObject)
- else
- TJSONData.DoError(SErrNotJSONData,[VObject.ClassName,SourceType]);
- //vtVariant :
- else
- TJSONData.DoError(SErrUnknownTypeInConstructor,[SourceType,VType])
- end;
- end;
- constructor TJSONArray.Create(const Elements: array of const);
- Var
- I : integer;
- J : TJSONData;
- begin
- Create;
- For I:=Low(Elements) to High(Elements) do
- begin
- J:=VarRecToJSON(Elements[i],'Array');
- Add(J);
- end;
- end;
- destructor TJSONArray.Destroy;
- begin
- FreeAndNil(FList);
- inherited Destroy;
- end;
- class function TJSONArray.JSONType: TJSONType;
- begin
- Result:=jtArray;
- end;
- function TJSONArray.Clone: TJSONData;
- Var
- A : TJSONArray;
- I : Integer;
- begin
- A:=TJSONArrayClass(ClassType).Create;
- try
- For I:=0 to Count-1 do
- A.Add(Self.Items[I].Clone);
- Result:=A;
- except
- A.Free;
- Raise;
- end;
- end;
- procedure TJSONArray.Iterate(Iterator: TJSONArrayIterator; Data: TObject);
- Var
- I : Integer;
- Cont : Boolean;
-
- begin
- I:=0;
- Cont:=True;
- While (I<FList.Count) and cont do
- begin
- Iterator(Items[i],Data,Cont);
- Inc(I);
- end;
- end;
- function TJSONArray.IndexOf(obj: TJSONData): Integer;
- begin
- Result:=FList.IndexOf(Obj);
- end;
- function TJSONArray.GetEnumerator: TBaseJSONEnumerator;
- begin
- Result:=TJSONArrayEnumerator.Create(Self);
- end;
- procedure TJSONArray.Clear;
- begin
- FList.Clear;
- end;
- function TJSONArray.Add(Item: TJSONData): Integer;
- begin
- Result:=FList.Add(Item);
- end;
- function TJSONArray.Add(I: Integer): Integer;
- begin
- Result:=Add(CreateJSON(I));
- end;
- function TJSONArray.Add(I: Int64): Int64;
- begin
- Result:=Add(CreateJSON(I));
- end;
- function TJSONArray.Add(I: QWord): QWord;
- begin
- Result:=Add(CreateJSON(I));
- end;
- function TJSONArray.Add(const S: String): Integer;
- begin
- Result:=Add(CreateJSON(S));
- end;
- function TJSONArray.Add: Integer;
- begin
- Result:=Add(CreateJSON);
- end;
- function TJSONArray.Add(F: TJSONFloat): Integer;
- begin
- Result:=Add(CreateJSON(F));
- end;
- function TJSONArray.Add(B: Boolean): Integer;
- begin
- Result:=Add(CreateJSON(B));
- end;
- function TJSONArray.Add(AnArray: TJSONArray): Integer;
- begin
- If (IndexOf(AnArray)<>-1) then
- DoError(SErrCannotAddArrayTwice);
- Result:=Add(TJSONData(AnArray));
- end;
- function TJSONArray.Add(AnObject: TJSONObject): Integer;
- begin
- If (IndexOf(AnObject)<>-1) then
- DoError(SErrCannotAddObjectTwice);
- Result:=Add(TJSONData(AnObject));
- end;
- procedure TJSONArray.Delete(Index: Integer);
- begin
- FList.Delete(Index);
- end;
- procedure TJSONArray.Exchange(Index1, Index2: Integer);
- begin
- FList.Exchange(Index1, Index2);
- end;
- function TJSONArray.Extract(Item: TJSONData): TJSONData;
- begin
- Result := TJSONData(FList.Extract(Item));
- end;
- function TJSONArray.Extract(Index: Integer): TJSONData;
- begin
- Result := TJSONData(FList.Extract(FList.Items[Index]));
- end;
- procedure TJSONArray.Insert(Index: Integer);
- begin
- Insert(Index,CreateJSON);
- end;
- procedure TJSONArray.Insert(Index: Integer; Item: TJSONData);
- begin
- FList.Insert(Index, Item);
- end;
- procedure TJSONArray.Insert(Index: Integer; I: Integer);
- begin
- FList.Insert(Index, CreateJSON(I));
- end;
- procedure TJSONArray.Insert(Index: Integer; I: Int64);
- begin
- FList.Insert(Index, CreateJSON(I));
- end;
- procedure TJSONArray.Insert(Index: Integer; I: QWord);
- begin
- FList.Insert(Index, CreateJSON(I));
- end;
- procedure TJSONArray.Insert(Index: Integer; const S: String);
- begin
- FList.Insert(Index, CreateJSON(S));
- end;
- procedure TJSONArray.Insert(Index: Integer; F: TJSONFloat);
- begin
- FList.Insert(Index, CreateJSON(F));
- end;
- procedure TJSONArray.Insert(Index: Integer; B: Boolean);
- begin
- FList.Insert(Index, CreateJSON(B));
- end;
- procedure TJSONArray.Insert(Index: Integer; AnArray: TJSONArray);
- begin
- if (IndexOf(AnArray)<>-1) then
- DoError(SErrCannotAddArrayTwice);
- FList.Insert(Index, AnArray);
- end;
- procedure TJSONArray.Insert(Index: Integer; AnObject: TJSONObject);
- begin
- if (IndexOf(AnObject)<>-1) then
- DoError(SErrCannotAddObjectTwice);
- FList.Insert(Index, AnObject);
- end;
- procedure TJSONArray.Move(CurIndex, NewIndex: Integer);
- begin
- FList.Move(CurIndex, NewIndex);
- end;
- procedure TJSONArray.Remove(Item: TJSONData);
- begin
- FList.Remove(Item);
- end;
- { TJSONObject }
- function TJSONObject.GetArrays(const AName: String): TJSONArray;
- begin
- Result:=GetElements(AName) as TJSONArray;
- end;
- function TJSONObject.GetBooleans(const AName: String): Boolean;
- begin
- Result:=GetElements(AName).AsBoolean;
- end;
- function TJSONObject.GetElements(const AName: string): TJSONData;
- begin
- Result:=TJSONData(FHash.Find(AName));
- If (Result=Nil) then
- DoError(SErrNonexistentElement,[AName]);
- end;
- function TJSONObject.GetFloats(const AName: String): TJSONFloat;
- begin
- Result:=GetElements(AName).AsFloat;
- end;
- function TJSONObject.GetIntegers(const AName: String): Integer;
- begin
- Result:=GetElements(AName).AsInteger;
- end;
- function TJSONObject.GetInt64s(const AName: String): Int64;
- begin
- Result:=GetElements(AName).AsInt64;
- end;
- function TJSONObject.GetIsNull(const AName: String): Boolean;
- begin
- Result:=GetElements(AName).IsNull;
- end;
- function TJSONObject.GetNameOf(Index: Integer): TJSONStringType;
- begin
- Result:=FHash.NameOfIndex(Index);
- end;
- function TJSONObject.GetObjects(const AName : String): TJSONObject;
- begin
- Result:=GetElements(AName) as TJSONObject;
- end;
- function TJSONObject.GetQWords(AName : String): QWord;
- begin
- Result:=GetElements(AName).AsQWord;
- end;
- function TJSONObject.GetStrings(const AName : String): TJSONStringType;
- begin
- Result:=GetElements(AName).AsString;
- end;
- function TJSONObject.GetTypes(const AName : String): TJSONType;
- begin
- Result:=Getelements(Aname).JSONType;
- end;
- class function TJSONObject.GetUnquotedMemberNames: Boolean; static;
- begin
- Result:=FUnquotedMemberNames;
- end;
- procedure TJSONObject.SetArrays(const AName : String; const AValue: TJSONArray);
- begin
- SetElements(AName,AVAlue);
- end;
- procedure TJSONObject.SetBooleans(const AName : String; const AValue: Boolean);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- end;
- procedure TJSONObject.SetElements(const AName: string; const AValue: TJSONData);
- Var
- Index : Integer;
- begin
- Index:=FHash.FindIndexOf(AName);
- If (Index=-1) then
- FHash.Add(AName,AValue)
- else
- FHash.Items[Index]:=AValue; // Will free the previous value.
- end;
- procedure TJSONObject.SetFloats(const AName : String; const AValue: TJSONFloat);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- end;
- procedure TJSONObject.SetIntegers(const AName : String; const AValue: Integer);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- end;
- procedure TJSONObject.SetInt64s(const AName : String; const AValue: Int64);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- end;
- procedure TJSONObject.SetIsNull(const AName : String; const AValue: Boolean);
- begin
- If Not AValue then
- DoError(SErrCannotSetNotIsNull);
- SetElements(AName,CreateJSON);
- end;
- procedure TJSONObject.SetObjects(const AName : String; const AValue: TJSONObject);
- begin
- SetElements(AName,AValue);
- end;
- procedure TJSONObject.SetQWords(AName : String; AValue: QWord);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- end;
- procedure TJSONObject.SetStrings(const AName : String; const AValue: TJSONStringType);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- end;
- class procedure TJSONObject.DetermineElementQuotes;
- begin
- FObjStartSep:=ObjStartSeps[TJSONData.FCompressedJSON];
- FObjEndSep:=ObjEndSeps[TJSONData.FCompressedJSON];
- if TJSONData.FCompressedJSON then
- FElementEnd:=UnSpacedQuoted[FUnquotedMemberNames]
- else
- FElementEnd:=SpacedQuoted[FUnquotedMemberNames];
- FElementStart:=ElementStart[FUnquotedMemberNames]
- end;
- class procedure TJSONObject.SetUnquotedMemberNames(AValue: Boolean); static;
- begin
- if FUnquotedMemberNames=AValue then exit;
- FUnquotedMemberNames:=AValue;
- DetermineElementQuotes;
- end;
- function TJSONObject.DoFindPath(const APath: TJSONStringType; out
- NotFound: TJSONStringType): TJSONdata;
- Var
- N: TJSONStringType;
- L,P,P2 : Integer;
- begin
- If (APath='') then
- Exit(Self);
- N:=APath;
- L:=Length(N);
- P:=1;
- While (P<L) and (N[P]='.') do
- inc(P);
- P2:=P;
- While (P2<=L) and (Not (N[P2] in ['.','['])) do
- inc(P2);
- N:=Copy(APath,P,P2-P);
- If (N='') then
- Result:=Self
- else
- begin
- Result:=Find(N);
- If Result=Nil then
- NotFound:=N+Copy(APath,P2,L-P2)
- else
- begin
- N:=Copy(APath,P2,L-P2+1);
- Result:=Result.DoFindPath(N,NotFound);
- end;
- end;
- end;
- procedure TJSONObject.Converterror(From: Boolean);
- begin
- If From then
- DoError(SErrCannotConvertFromObject)
- else
- DoError(SErrCannotConvertToObject);
- end;
- {$warnings off}
- function TJSONObject.GetAsBoolean: Boolean;
- begin
- ConvertError(True);
- end;
- function TJSONObject.GetAsFloat: TJSONFloat;
- begin
- ConvertError(True);
- end;
- function TJSONObject.GetAsInteger: Integer;
- begin
- ConvertError(True);
- end;
- function TJSONObject.GetAsInt64: Int64;
- begin
- ConvertError(True);
- end;
- function TJSONObject.GetAsQWord: QWord;
- begin
- ConvertError(True);
- end;
- procedure TJSONObject.SetAsBoolean(const AValue: Boolean);
- begin
- ConvertError(False);
- end;
- procedure TJSONObject.SetAsFloat(const AValue: TJSONFloat);
- begin
- ConvertError(False);
- end;
- procedure TJSONObject.SetAsInteger(const AValue: Integer);
- begin
- ConvertError(False);
- end;
- procedure TJSONObject.SetAsInt64(const AValue: Int64);
- begin
- ConvertError(False);
- end;
- procedure TJSONObject.SetAsQword(const AValue: QWord);
- begin
- ConvertError(False);
- end;
- {$warnings on}
- function TJSONObject.GetAsJSON: TJSONStringType;
- Var
- I : Integer;
- Sep : String;
- begin
- Sep:=TJSONData.FElementSep;
- Result:='';
- For I:=0 to Count-1 do
- begin
- If (Result<>'') then
- Result:=Result+Sep;
- Result:=Result+FElementStart+StringToJSONString(Names[i])+FElementEnd+Items[I].AsJSON;
- end;
- If (Result<>'') then
- Result:=FObjStartSep+Result+FObjEndSep
- else
- Result:='{}';
- end;
- {$warnings off}
- function TJSONObject.GetAsString: TJSONStringType;
- begin
- ConvertError(True);
- end;
- procedure TJSONObject.SetAsString(const AValue: TJSONStringType);
- begin
- ConvertError(False);
- end;
- function TJSONObject.GetValue: variant;
- begin
- ConvertError(True);
- end;
- procedure TJSONObject.SetValue(const AValue: variant);
- begin
- ConvertError(False);
- end;
- {$warnings on}
- function TJSONObject.GetCount: Integer;
- begin
- Result:=FHash.Count;
- end;
- function TJSONObject.GetItem(Index: Integer): TJSONData;
- begin
- Result:=TJSONData(FHash.Items[Index]);
- end;
- procedure TJSONObject.SetItem(Index: Integer; const AValue: TJSONData);
- begin
- FHash.Items[Index]:=AValue;
- end;
- constructor TJSONObject.Create;
- begin
- FHash:=TFPHashObjectList.Create(True);
- end;
- constructor TJSONObject.Create(const Elements: array of const);
- Var
- I : integer;
- AName : String;
- J : TJSONData;
- begin
- Create;
- If ((High(Elements)-Low(Elements)) mod 2)=0 then
- DoError(SErrOddNumber);
- I:=Low(Elements);
- While I<=High(Elements) do
- begin
- With Elements[i] do
- Case VType of
- vtChar : AName:=VChar;
- vtString : AName:=vString^;
- vtAnsiString : AName:=(AnsiString(vAnsiString));
- vtPChar : AName:=StrPas(VPChar);
- else
- DoError(SErrNameMustBeString,[I+1]);
- end;
- If (ANAme='') then
- DoError(SErrNameMustBeString,[I+1]);
- Inc(I);
- J:=VarRecToJSON(Elements[i],'Object');
- Add(AName,J);
- Inc(I);
- end;
- end;
- destructor TJSONObject.Destroy;
- begin
- FreeAndNil(FHash);
- inherited Destroy;
- end;
- class function TJSONObject.JSONType: TJSONType;
- begin
- Result:=jtObject;
- end;
- function TJSONObject.Clone: TJSONData;
- Var
- O : TJSONObject;
- I: Integer;
- begin
- O:=TJSONObjectClass(ClassType).Create;
- try
- For I:=0 to Count-1 do
- O.Add(Self.Names[I],Self.Items[I].Clone);
- Result:=O;
- except
- FreeAndNil(O);
- Raise;
- end;
- end;
- function TJSONObject.GetEnumerator: TBaseJSONEnumerator;
- begin
- Result:=TJSONObjectEnumerator.Create(Self);
- end;
- function TJSONObject.DoFormatJSON(Options: TFormatOptions; CurrentIndent,
- Indent: Integer): TJSONStringType;
- Var
- i : Integer;
- S : TJSONStringType;
- MultiLine,UseQuotes, SkipWhiteSpace : Boolean;
- NSep,Sep,Ind : String;
- begin
- Result:='';
- UseQuotes:=Not (foDoNotQuoteMembers in options);
- MultiLine:=Not (foSingleLineObject in Options);
- SkipWhiteSpace:=foSkipWhiteSpace in Options;
- CurrentIndent:=CurrentIndent+Indent;
- Ind:=IndentString(Options, CurrentIndent);
- If SkipWhiteSpace then
- NSep:=':'
- else
- NSep:=' : ';
- If MultiLine then
- Sep:=','+SLineBreak+Ind
- else if SkipWhiteSpace then
- Sep:=','
- else
- Sep:=', ';
- For I:=0 to Count-1 do
- begin
- If (I>0) then
- Result:=Result+Sep
- else If MultiLine then
- Result:=Result+Ind;
- S:=StringToJSONString(Names[i]);
- If UseQuotes then
- S:='"'+S+'"';
- Result:=Result+S+NSep+Items[I].DoFormatJSON(Options,CurrentIndent,Indent);
- end;
- If (Result<>'') then
- begin
- if MultiLine then
- Result:='{'+sLineBreak+Result+sLineBreak+indentString(options,CurrentIndent-Indent)+'}'
- else
- Result:=ObjStartSeps[SkipWhiteSpace]+Result+ObjEndSeps[SkipWhiteSpace]
- end
- else
- Result:='{}';
- end;
- procedure TJSONObject.Iterate(Iterator: TJSONObjectIterator; Data: TObject);
- Var
- I : Integer;
- Cont : Boolean;
- begin
- I:=0;
- Cont:=True;
- While (I<FHash.Count) and cont do
- begin
- Iterator(Names[I],Items[i],Data,Cont);
- Inc(I);
- end;
- end;
- function TJSONObject.IndexOf(Item: TJSONData): Integer;
- begin
- Result:=FHash.IndexOf(Item);
- end;
- function TJSONObject.IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer;
- begin
- Result:=FHash.FindIndexOf(AName);
- if (Result=-1) and CaseInsensitive then
- begin
- Result:=Count-1;
- While (Result>=0) and (CompareText(Names[Result],AName)<>0) do
- Dec(Result);
- end;
- end;
- procedure TJSONObject.Clear;
- begin
- FHash.Clear;
- end;
- function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONData
- ): Integer;
- begin
- Result:=FHash.Add(AName,AValue);
- end;
- function TJSONObject.Add(const AName: TJSONStringType; AValue: Boolean
- ): Integer;
- begin
- Result:=Add(AName,CreateJSON(AValue));
- end;
- function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer;
- begin
- Result:=Add(AName,CreateJSON(AValue));
- end;
- function TJSONObject.Add(const AName, AValue: TJSONStringType): Integer;
- begin
- Result:=Add(AName,CreateJSON(AValue));
- end;
- function TJSONObject.Add(const AName: TJSONStringType; Avalue: Integer): Integer;
- begin
- Result:=Add(AName,CreateJSON(AValue));
- end;
- function TJSONObject.Add(const AName: TJSONStringType; Avalue: Int64): Integer;
- begin
- Result:=Add(AName,CreateJSON(AValue));
- end;
- function TJSONObject.Add(const AName: TJSONStringType; Avalue: QWord): Integer;
- begin
- Result:=Add(AName,CreateJSON(AValue));
- end;
- function TJSONObject.Add(const AName: TJSONStringType): Integer;
- begin
- Result:=Add(AName,CreateJSON);
- end;
- function TJSONObject.Add(const AName: TJSONStringType; AValue: TJSONArray
- ): Integer;
- begin
- Result:=Add(AName,TJSONData(AValue));
- end;
- procedure TJSONObject.Delete(Index: Integer);
- begin
- FHash.Delete(Index);
- end;
- procedure TJSONObject.Delete(const AName: string);
- Var
- I : Integer;
- begin
- I:=IndexOfName(AName);
- if (I<>-1) then
- Delete(I);
- end;
- procedure TJSONObject.Remove(Item: TJSONData);
- begin
- FHash.Remove(Item);
- end;
- function TJSONObject.Extract(Index: Integer): TJSONData;
- begin
- Result:=Items[Index];
- FHash.Extract(Result);
- end;
- function TJSONObject.Extract(const AName: string): TJSONData;
- Var
- I : Integer;
- begin
- I:=IndexOfName(AName);
- if (I<>-1) then
- Result:=Extract(I)
- else
- Result:=Nil
- end;
- function TJSONObject.Get(const AName: String): Variant;
- Var
- I : Integer;
- begin
- I:=IndexOfName(AName);
- If (I<>-1) then
- Result:=Items[i].Value
- else
- Result:=Null;
- end;
- function TJSONObject.Get(const AName: String; ADefault: TJSONFloat
- ): TJSONFloat;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtNumber);
- If D<>Nil then
- Result:=D.AsFloat
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: Integer
- ): Integer;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtNumber);
- If D<>Nil then
- Result:=D.AsInteger
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: Int64): Int64;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtNumber);
- If D<>Nil then
- Result:=D.AsInt64
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: QWord): QWord;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtNumber);
- If D<>Nil then
- Result:=D.AsQWord
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: Boolean
- ): Boolean;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtBoolean);
- If D<>Nil then
- Result:=D.AsBoolean
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: TJSONStringType
- ): TJSONStringTYpe;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtString);
- If (D<>Nil) then
- Result:=D.AsString
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: TJSONArray
- ): TJSONArray;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtArray);
- If (D<>Nil) then
- Result:=TJSONArray(D)
- else
- Result:=ADefault;
- end;
- function TJSONObject.Get(const AName: String; ADefault: TJSONObject
- ): TJSONObject;
- Var
- D : TJSONData;
- begin
- D:=Find(AName,jtObject);
- If (D<>Nil) then
- Result:=TJSONObject(D)
- else
- Result:=ADefault;
- end;
- function TJSONObject.Find(const AName: String): TJSONData;
- Var
- I : Integer;
- begin
- I:=IndexOfName(AName);
- If (I<>-1) then
- Result:=Items[i]
- else
- Result:=Nil;
- end;
- function TJSONObject.Find(const AName: String; AType: TJSONType): TJSONData;
- begin
- Result:=Find(AName);
- If Assigned(Result) and (Result.JSONType<>AType) then
- Result:=Nil;
- end;
- initialization
- // Need to force initialization;
- TJSONData.DetermineElementSeparators;
- TJSONObject.DetermineElementQuotes;
- end.
|