1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590 |
- {
- 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,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.
- TFormatOptions = set of TFormatOption;
- Const
- DefaultIndentSize = 2;
- DefaultFormat = [];
- AsJSONFormat = [foSingleLineArray,foSingleLineObject]; // These options make FormatJSON behave as AsJSON
-
- 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)
- protected
- Class Procedure DoError(Const Msg : String);
- Class Procedure DoError(Const Fmt : String; 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 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;
- 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
- Constructor Create; virtual;
- Class function JSONType: TJSONType; virtual;
- Procedure Clear; virtual; Abstract;
- // 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 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);
- 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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;
- { 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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 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;
- 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 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 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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(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; 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 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
- 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 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 SetStrings(const AName : String; 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;
- procedure SetAsBoolean(const AValue: Boolean); override;
- procedure SetAsFloat(const AValue: TJSONFloat); override;
- procedure SetAsInteger(const AValue: Integer); override;
- procedure SetAsInt64(const AValue: Int64); 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;
- 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 : 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): 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 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);
- Procedure SetJSONInstanceType(AType : TJSONInstanceType; AClass : 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 : 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;
- Procedure SetJSONParserHandler(AHandler : 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,TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
- TJSONObject);
- Const
- MinJSONInstanceTypes :
- Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber,
- TJSONInt64Number,TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
- TJSONObject);
- procedure SetJSONInstanceType(AType: TJSONInstanceType; AClass: 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]);
- 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: 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;
- procedure SetJSONParserHandler(AHandler: TJSONParserHandler);
- begin
- 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;
- 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;
- class procedure TJSONData.DoError(const Msg: String);
- begin
- Raise EJSON.Create(Msg);
- end;
- class procedure TJSONData.DoError(const Fmt: String; 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;
- 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;
- 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;
- 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;
- 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.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;
- 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;
- 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;
- 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;
- 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;
- 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;
- procedure TJSONInt64Number.SetAsInt64(const AValue: Int64);
- 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.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.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;
- 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;
- {$warnings on}
- function TJSONArray.GetAsJSON: TJSONStringType;
- Var
- I : Integer;
- begin
- Result:='[';
- For I:=0 to Count-1 do
- begin
- Result:=Result+Items[i].AsJSON;
- If (I<Count-1) then
- Result:=Result+', '
- 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;
-
- begin
- Result:='[';
- if not (foSingleLineArray in Options) then
- Result:=Result+sLineBreak;
- For I:=0 to Count-1 do
- begin
- if not (foSingleLineArray in Options) then
- Result:=Result+IndentString(Options, CurrentIndent+Indent);
- Result:=Result+Items[i].DoFormatJSON(Options,CurrentIndent+Indent,Indent);
- If (I<Count-1) then
- if (foSingleLineArray in Options) then
- Result:=Result+', '
- else
- Result:=Result+',';
- if not (foSingleLineArray in Options) then
- Result:=Result+sLineBreak
- end;
- if not (foSingleLineArray in Options) 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(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; 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.GetStrings(const AName : String): TJSONStringType;
- begin
- Result:=GetElements(AName).AsString;
- end;
- function TJSONObject.GetTypes(const AName : String): TJSONType;
- begin
- Result:=Getelements(Aname).JSONType;
- 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.SetStrings(const AName : String; const AValue: TJSONStringType);
- begin
- SetElements(AName,CreateJSON(AVAlue));
- 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;
- 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;
- {$warnings on}
- function TJSONObject.GetAsJSON: TJSONStringType;
- Var
- I : Integer;
- begin
- Result:='';
- For I:=0 to Count-1 do
- begin
- If (Result<>'') then
- Result:=Result+', ';
- Result:=Result+'"'+StringToJSONString(Names[i])+'" : '+Items[I].AsJSON;
- end;
- If (Result<>'') then
- Result:='{ '+Result+' }'
- 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;
- begin
- Result:='';
- CurrentIndent:=CurrentIndent+Indent;
- For I:=0 to Count-1 do
- begin
- If (Result<>'') then
- begin
- If (foSingleLineObject in Options) then
- Result:=Result+', '
- else
- Result:=Result+','+SLineBreak;
- end;
- If not (foSingleLineObject in Options) then
- Result:=Result+IndentString(Options,CurrentIndent);
- S:=StringToJSONString(Names[i]);
- If not (foDoNotQuoteMembers in options) then
- S:='"'+S+'"';
- Result:=Result+S+' : '+Items[I].DoFormatJSON(Options,CurrentIndent,Indent);
- end;
- If (Result<>'') then
- begin
- if (foSingleLineObject in Options) then
- Result:='{ '+Result+' }'
- else
- Result:='{'+sLineBreak+Result+sLineBreak+indentString(options,CurrentIndent-Indent)+'}'
- 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): 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: 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;
- end.
|