1
0

json.hpp 931 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.12.0
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. /****************************************************************************\
  9. * Note on documentation: The source files contain links to the online *
  10. * documentation of the public API at https://json.nlohmann.me. This URL *
  11. * contains the most recent documentation and should also be applicable to *
  12. * previous versions; documentation for deprecated functions is not *
  13. * removed, but marked deprecated. See "Generate documentation" section in *
  14. * file docs/README.md. *
  15. \****************************************************************************/
  16. #ifndef INCLUDE_NLOHMANN_JSON_HPP_
  17. #define INCLUDE_NLOHMANN_JSON_HPP_
  18. #include <algorithm> // all_of, find, for_each
  19. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  20. #include <functional> // hash, less
  21. #include <initializer_list> // initializer_list
  22. #ifndef JSON_NO_IO
  23. #include <iosfwd> // istream, ostream
  24. #endif // JSON_NO_IO
  25. #include <iterator> // random_access_iterator_tag
  26. #include <memory> // unique_ptr
  27. #include <string> // string, stoi, to_string
  28. #include <utility> // declval, forward, move, pair, swap
  29. #include <vector> // vector
  30. // #include <nlohmann/adl_serializer.hpp>
  31. // __ _____ _____ _____
  32. // __| | __| | | | JSON for Modern C++
  33. // | | |__ | | | | | | version 3.12.0
  34. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  35. //
  36. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  37. // SPDX-License-Identifier: MIT
  38. #include <utility>
  39. // #include <nlohmann/detail/abi_macros.hpp>
  40. // __ _____ _____ _____
  41. // __| | __| | | | JSON for Modern C++
  42. // | | |__ | | | | | | version 3.12.0
  43. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  44. //
  45. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  46. // SPDX-License-Identifier: MIT
  47. // This file contains all macro definitions affecting or depending on the ABI
  48. #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
  49. #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
  50. #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
  51. #warning "Already included a different version of the library!"
  52. #endif
  53. #endif
  54. #endif
  55. #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
  56. #define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
  57. #define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
  58. #ifndef JSON_DIAGNOSTICS
  59. #define JSON_DIAGNOSTICS 0
  60. #endif
  61. #ifndef JSON_DIAGNOSTIC_POSITIONS
  62. #define JSON_DIAGNOSTIC_POSITIONS 0
  63. #endif
  64. #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  65. #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
  66. #endif
  67. #if JSON_DIAGNOSTICS
  68. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
  69. #else
  70. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
  71. #endif
  72. #if JSON_DIAGNOSTIC_POSITIONS
  73. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
  74. #else
  75. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
  76. #endif
  77. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  78. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
  79. #else
  80. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
  81. #endif
  82. #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
  83. #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
  84. #endif
  85. // Construct the namespace ABI tags component
  86. #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi ## a ## b ## c
  87. #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
  88. NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)
  89. #define NLOHMANN_JSON_ABI_TAGS \
  90. NLOHMANN_JSON_ABI_TAGS_CONCAT( \
  91. NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
  92. NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
  93. NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)
  94. // Construct the namespace version component
  95. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
  96. _v ## major ## _ ## minor ## _ ## patch
  97. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
  98. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
  99. #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
  100. #define NLOHMANN_JSON_NAMESPACE_VERSION
  101. #else
  102. #define NLOHMANN_JSON_NAMESPACE_VERSION \
  103. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
  104. NLOHMANN_JSON_VERSION_MINOR, \
  105. NLOHMANN_JSON_VERSION_PATCH)
  106. #endif
  107. // Combine namespace components
  108. #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
  109. #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
  110. NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
  111. #ifndef NLOHMANN_JSON_NAMESPACE
  112. #define NLOHMANN_JSON_NAMESPACE \
  113. nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
  114. NLOHMANN_JSON_ABI_TAGS, \
  115. NLOHMANN_JSON_NAMESPACE_VERSION)
  116. #endif
  117. #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
  118. #define NLOHMANN_JSON_NAMESPACE_BEGIN \
  119. namespace nlohmann \
  120. { \
  121. inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
  122. NLOHMANN_JSON_ABI_TAGS, \
  123. NLOHMANN_JSON_NAMESPACE_VERSION) \
  124. {
  125. #endif
  126. #ifndef NLOHMANN_JSON_NAMESPACE_END
  127. #define NLOHMANN_JSON_NAMESPACE_END \
  128. } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
  129. } // namespace nlohmann
  130. #endif
  131. // #include <nlohmann/detail/conversions/from_json.hpp>
  132. // __ _____ _____ _____
  133. // __| | __| | | | JSON for Modern C++
  134. // | | |__ | | | | | | version 3.12.0
  135. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  136. //
  137. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  138. // SPDX-License-Identifier: MIT
  139. #include <algorithm> // transform
  140. #include <array> // array
  141. #include <forward_list> // forward_list
  142. #include <iterator> // inserter, front_inserter, end
  143. #include <map> // map
  144. #ifdef JSON_HAS_CPP_17
  145. #include <optional> // optional
  146. #endif
  147. #include <string> // string
  148. #include <tuple> // tuple, make_tuple
  149. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  150. #include <unordered_map> // unordered_map
  151. #include <utility> // pair, declval
  152. #include <valarray> // valarray
  153. // #include <nlohmann/detail/exceptions.hpp>
  154. // __ _____ _____ _____
  155. // __| | __| | | | JSON for Modern C++
  156. // | | |__ | | | | | | version 3.12.0
  157. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  158. //
  159. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  160. // SPDX-License-Identifier: MIT
  161. #include <cstddef> // nullptr_t
  162. #include <exception> // exception
  163. #if JSON_DIAGNOSTICS
  164. #include <numeric> // accumulate
  165. #endif
  166. #include <stdexcept> // runtime_error
  167. #include <string> // to_string
  168. #include <vector> // vector
  169. // #include <nlohmann/detail/value_t.hpp>
  170. // __ _____ _____ _____
  171. // __| | __| | | | JSON for Modern C++
  172. // | | |__ | | | | | | version 3.12.0
  173. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  174. //
  175. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  176. // SPDX-License-Identifier: MIT
  177. #include <array> // array
  178. #include <cstddef> // size_t
  179. #include <cstdint> // uint8_t
  180. #include <string> // string
  181. // #include <nlohmann/detail/macro_scope.hpp>
  182. // __ _____ _____ _____
  183. // __| | __| | | | JSON for Modern C++
  184. // | | |__ | | | | | | version 3.12.0
  185. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  186. //
  187. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  188. // SPDX-License-Identifier: MIT
  189. #include <utility> // declval, pair
  190. // #include <nlohmann/detail/meta/detected.hpp>
  191. // __ _____ _____ _____
  192. // __| | __| | | | JSON for Modern C++
  193. // | | |__ | | | | | | version 3.12.0
  194. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  195. //
  196. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  197. // SPDX-License-Identifier: MIT
  198. #include <type_traits>
  199. // #include <nlohmann/detail/meta/void_t.hpp>
  200. // __ _____ _____ _____
  201. // __| | __| | | | JSON for Modern C++
  202. // | | |__ | | | | | | version 3.12.0
  203. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  204. //
  205. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  206. // SPDX-License-Identifier: MIT
  207. // #include <nlohmann/detail/abi_macros.hpp>
  208. NLOHMANN_JSON_NAMESPACE_BEGIN
  209. namespace detail
  210. {
  211. template<typename ...Ts> struct make_void
  212. {
  213. using type = void;
  214. };
  215. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  216. } // namespace detail
  217. NLOHMANN_JSON_NAMESPACE_END
  218. NLOHMANN_JSON_NAMESPACE_BEGIN
  219. namespace detail
  220. {
  221. // https://en.cppreference.com/w/cpp/experimental/is_detected
  222. struct nonesuch
  223. {
  224. nonesuch() = delete;
  225. ~nonesuch() = delete;
  226. nonesuch(nonesuch const&) = delete;
  227. nonesuch(nonesuch const&&) = delete;
  228. void operator=(nonesuch const&) = delete;
  229. void operator=(nonesuch&&) = delete;
  230. };
  231. template<class Default,
  232. class AlwaysVoid,
  233. template<class...> class Op,
  234. class... Args>
  235. struct detector
  236. {
  237. using value_t = std::false_type;
  238. using type = Default;
  239. };
  240. template<class Default, template<class...> class Op, class... Args>
  241. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  242. {
  243. using value_t = std::true_type;
  244. using type = Op<Args...>;
  245. };
  246. template<template<class...> class Op, class... Args>
  247. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  248. template<template<class...> class Op, class... Args>
  249. struct is_detected_lazy : is_detected<Op, Args...> { };
  250. template<template<class...> class Op, class... Args>
  251. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  252. template<class Default, template<class...> class Op, class... Args>
  253. using detected_or = detector<Default, void, Op, Args...>;
  254. template<class Default, template<class...> class Op, class... Args>
  255. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  256. template<class Expected, template<class...> class Op, class... Args>
  257. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  258. template<class To, template<class...> class Op, class... Args>
  259. using is_detected_convertible =
  260. std::is_convertible<detected_t<Op, Args...>, To>;
  261. } // namespace detail
  262. NLOHMANN_JSON_NAMESPACE_END
  263. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  264. // __ _____ _____ _____
  265. // __| | __| | | | JSON for Modern C++
  266. // | | |__ | | | | | | version 3.12.0
  267. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  268. //
  269. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  270. // SPDX-FileCopyrightText: 2016 - 2021 Evan Nemerson <evan@nemerson.com>
  271. // SPDX-License-Identifier: MIT
  272. /* Hedley - https://nemequ.github.io/hedley
  273. * Created by Evan Nemerson <evan@nemerson.com>
  274. */
  275. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  276. #if defined(JSON_HEDLEY_VERSION)
  277. #undef JSON_HEDLEY_VERSION
  278. #endif
  279. #define JSON_HEDLEY_VERSION 15
  280. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  281. #undef JSON_HEDLEY_STRINGIFY_EX
  282. #endif
  283. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  284. #if defined(JSON_HEDLEY_STRINGIFY)
  285. #undef JSON_HEDLEY_STRINGIFY
  286. #endif
  287. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  288. #if defined(JSON_HEDLEY_CONCAT_EX)
  289. #undef JSON_HEDLEY_CONCAT_EX
  290. #endif
  291. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  292. #if defined(JSON_HEDLEY_CONCAT)
  293. #undef JSON_HEDLEY_CONCAT
  294. #endif
  295. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  296. #if defined(JSON_HEDLEY_CONCAT3_EX)
  297. #undef JSON_HEDLEY_CONCAT3_EX
  298. #endif
  299. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  300. #if defined(JSON_HEDLEY_CONCAT3)
  301. #undef JSON_HEDLEY_CONCAT3
  302. #endif
  303. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  304. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  305. #undef JSON_HEDLEY_VERSION_ENCODE
  306. #endif
  307. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  308. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  309. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  310. #endif
  311. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  312. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  313. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  314. #endif
  315. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  316. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  317. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  318. #endif
  319. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  320. #if defined(JSON_HEDLEY_GNUC_VERSION)
  321. #undef JSON_HEDLEY_GNUC_VERSION
  322. #endif
  323. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  324. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  325. #elif defined(__GNUC__)
  326. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  327. #endif
  328. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  329. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  330. #endif
  331. #if defined(JSON_HEDLEY_GNUC_VERSION)
  332. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  333. #else
  334. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  335. #endif
  336. #if defined(JSON_HEDLEY_MSVC_VERSION)
  337. #undef JSON_HEDLEY_MSVC_VERSION
  338. #endif
  339. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  340. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  341. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  342. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  343. #elif defined(_MSC_VER) && !defined(__ICL)
  344. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  345. #endif
  346. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  347. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  348. #endif
  349. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  350. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  351. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  352. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  353. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  354. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  355. #else
  356. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  357. #endif
  358. #if defined(JSON_HEDLEY_INTEL_VERSION)
  359. #undef JSON_HEDLEY_INTEL_VERSION
  360. #endif
  361. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  362. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  363. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  364. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  365. #endif
  366. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  367. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  368. #endif
  369. #if defined(JSON_HEDLEY_INTEL_VERSION)
  370. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  371. #else
  372. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  373. #endif
  374. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  375. #undef JSON_HEDLEY_INTEL_CL_VERSION
  376. #endif
  377. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  378. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  379. #endif
  380. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  381. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  382. #endif
  383. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  384. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  385. #else
  386. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  387. #endif
  388. #if defined(JSON_HEDLEY_PGI_VERSION)
  389. #undef JSON_HEDLEY_PGI_VERSION
  390. #endif
  391. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  392. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  393. #endif
  394. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  395. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  396. #endif
  397. #if defined(JSON_HEDLEY_PGI_VERSION)
  398. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  399. #else
  400. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  401. #endif
  402. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  403. #undef JSON_HEDLEY_SUNPRO_VERSION
  404. #endif
  405. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  406. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10)
  407. #elif defined(__SUNPRO_C)
  408. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  409. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  410. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10)
  411. #elif defined(__SUNPRO_CC)
  412. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  413. #endif
  414. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  415. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  416. #endif
  417. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  418. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  419. #else
  420. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  421. #endif
  422. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  423. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  424. #endif
  425. #if defined(__EMSCRIPTEN__)
  426. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  427. #endif
  428. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  429. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  430. #endif
  431. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  432. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  433. #else
  434. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  435. #endif
  436. #if defined(JSON_HEDLEY_ARM_VERSION)
  437. #undef JSON_HEDLEY_ARM_VERSION
  438. #endif
  439. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  440. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  441. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  442. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  443. #endif
  444. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  445. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  446. #endif
  447. #if defined(JSON_HEDLEY_ARM_VERSION)
  448. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  449. #else
  450. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  451. #endif
  452. #if defined(JSON_HEDLEY_IBM_VERSION)
  453. #undef JSON_HEDLEY_IBM_VERSION
  454. #endif
  455. #if defined(__ibmxl__)
  456. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  457. #elif defined(__xlC__) && defined(__xlC_ver__)
  458. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  459. #elif defined(__xlC__)
  460. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  461. #endif
  462. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  463. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  464. #endif
  465. #if defined(JSON_HEDLEY_IBM_VERSION)
  466. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  467. #else
  468. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  469. #endif
  470. #if defined(JSON_HEDLEY_TI_VERSION)
  471. #undef JSON_HEDLEY_TI_VERSION
  472. #endif
  473. #if \
  474. defined(__TI_COMPILER_VERSION__) && \
  475. ( \
  476. defined(__TMS470__) || defined(__TI_ARM__) || \
  477. defined(__MSP430__) || \
  478. defined(__TMS320C2000__) \
  479. )
  480. #if (__TI_COMPILER_VERSION__ >= 16000000)
  481. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  482. #endif
  483. #endif
  484. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  485. #undef JSON_HEDLEY_TI_VERSION_CHECK
  486. #endif
  487. #if defined(JSON_HEDLEY_TI_VERSION)
  488. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  489. #else
  490. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  491. #endif
  492. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  493. #undef JSON_HEDLEY_TI_CL2000_VERSION
  494. #endif
  495. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  496. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  497. #endif
  498. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  499. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  500. #endif
  501. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  502. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  503. #else
  504. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  505. #endif
  506. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  507. #undef JSON_HEDLEY_TI_CL430_VERSION
  508. #endif
  509. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  510. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  511. #endif
  512. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  513. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  514. #endif
  515. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  516. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  517. #else
  518. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  519. #endif
  520. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  521. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  522. #endif
  523. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  524. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  525. #endif
  526. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  527. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  528. #endif
  529. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  530. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  531. #else
  532. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  533. #endif
  534. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  535. #undef JSON_HEDLEY_TI_CL6X_VERSION
  536. #endif
  537. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  538. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  539. #endif
  540. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  541. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  542. #endif
  543. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  544. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  545. #else
  546. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  547. #endif
  548. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  549. #undef JSON_HEDLEY_TI_CL7X_VERSION
  550. #endif
  551. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  552. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  553. #endif
  554. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  555. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  556. #endif
  557. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  558. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  559. #else
  560. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  561. #endif
  562. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  563. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  564. #endif
  565. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  566. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  567. #endif
  568. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  569. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  570. #endif
  571. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  572. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  573. #else
  574. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  575. #endif
  576. #if defined(JSON_HEDLEY_CRAY_VERSION)
  577. #undef JSON_HEDLEY_CRAY_VERSION
  578. #endif
  579. #if defined(_CRAYC)
  580. #if defined(_RELEASE_PATCHLEVEL)
  581. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  582. #else
  583. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  584. #endif
  585. #endif
  586. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  587. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  588. #endif
  589. #if defined(JSON_HEDLEY_CRAY_VERSION)
  590. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  591. #else
  592. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  593. #endif
  594. #if defined(JSON_HEDLEY_IAR_VERSION)
  595. #undef JSON_HEDLEY_IAR_VERSION
  596. #endif
  597. #if defined(__IAR_SYSTEMS_ICC__)
  598. #if __VER__ > 1000
  599. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  600. #else
  601. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  602. #endif
  603. #endif
  604. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  605. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  606. #endif
  607. #if defined(JSON_HEDLEY_IAR_VERSION)
  608. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  609. #else
  610. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  611. #endif
  612. #if defined(JSON_HEDLEY_TINYC_VERSION)
  613. #undef JSON_HEDLEY_TINYC_VERSION
  614. #endif
  615. #if defined(__TINYC__)
  616. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  617. #endif
  618. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  619. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  620. #endif
  621. #if defined(JSON_HEDLEY_TINYC_VERSION)
  622. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  623. #else
  624. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  625. #endif
  626. #if defined(JSON_HEDLEY_DMC_VERSION)
  627. #undef JSON_HEDLEY_DMC_VERSION
  628. #endif
  629. #if defined(__DMC__)
  630. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  631. #endif
  632. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  633. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  634. #endif
  635. #if defined(JSON_HEDLEY_DMC_VERSION)
  636. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  637. #else
  638. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  639. #endif
  640. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  641. #undef JSON_HEDLEY_COMPCERT_VERSION
  642. #endif
  643. #if defined(__COMPCERT_VERSION__)
  644. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  645. #endif
  646. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  647. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  648. #endif
  649. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  650. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  651. #else
  652. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  653. #endif
  654. #if defined(JSON_HEDLEY_PELLES_VERSION)
  655. #undef JSON_HEDLEY_PELLES_VERSION
  656. #endif
  657. #if defined(__POCC__)
  658. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  659. #endif
  660. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  661. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  662. #endif
  663. #if defined(JSON_HEDLEY_PELLES_VERSION)
  664. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  665. #else
  666. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  667. #endif
  668. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  669. #undef JSON_HEDLEY_MCST_LCC_VERSION
  670. #endif
  671. #if defined(__LCC__) && defined(__LCC_MINOR__)
  672. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  673. #endif
  674. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  675. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  676. #endif
  677. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  678. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  679. #else
  680. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  681. #endif
  682. #if defined(JSON_HEDLEY_GCC_VERSION)
  683. #undef JSON_HEDLEY_GCC_VERSION
  684. #endif
  685. #if \
  686. defined(JSON_HEDLEY_GNUC_VERSION) && \
  687. !defined(__clang__) && \
  688. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  689. !defined(JSON_HEDLEY_PGI_VERSION) && \
  690. !defined(JSON_HEDLEY_ARM_VERSION) && \
  691. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  692. !defined(JSON_HEDLEY_TI_VERSION) && \
  693. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  694. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  695. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  696. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  697. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  698. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  699. !defined(__COMPCERT__) && \
  700. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  701. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  702. #endif
  703. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  704. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  705. #endif
  706. #if defined(JSON_HEDLEY_GCC_VERSION)
  707. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  708. #else
  709. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  710. #endif
  711. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  712. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  713. #endif
  714. #if \
  715. defined(__has_attribute) && \
  716. ( \
  717. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  718. )
  719. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  720. #else
  721. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  722. #endif
  723. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  724. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  725. #endif
  726. #if defined(__has_attribute)
  727. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  728. #else
  729. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  730. #endif
  731. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  732. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  733. #endif
  734. #if defined(__has_attribute)
  735. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  736. #else
  737. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  738. #endif
  739. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  740. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  741. #endif
  742. #if \
  743. defined(__has_cpp_attribute) && \
  744. defined(__cplusplus) && \
  745. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  746. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  747. #else
  748. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  749. #endif
  750. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  751. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  752. #endif
  753. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  754. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  755. #elif \
  756. !defined(JSON_HEDLEY_PGI_VERSION) && \
  757. !defined(JSON_HEDLEY_IAR_VERSION) && \
  758. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  759. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  760. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  761. #else
  762. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  763. #endif
  764. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  765. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  766. #endif
  767. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  768. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  769. #else
  770. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  771. #endif
  772. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  773. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  774. #endif
  775. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  776. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  777. #else
  778. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  779. #endif
  780. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  781. #undef JSON_HEDLEY_HAS_BUILTIN
  782. #endif
  783. #if defined(__has_builtin)
  784. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  785. #else
  786. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  787. #endif
  788. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  789. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  790. #endif
  791. #if defined(__has_builtin)
  792. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  793. #else
  794. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  795. #endif
  796. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  797. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  798. #endif
  799. #if defined(__has_builtin)
  800. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  801. #else
  802. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  803. #endif
  804. #if defined(JSON_HEDLEY_HAS_FEATURE)
  805. #undef JSON_HEDLEY_HAS_FEATURE
  806. #endif
  807. #if defined(__has_feature)
  808. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  809. #else
  810. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  811. #endif
  812. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  813. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  814. #endif
  815. #if defined(__has_feature)
  816. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  817. #else
  818. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  819. #endif
  820. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  821. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  822. #endif
  823. #if defined(__has_feature)
  824. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  825. #else
  826. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  827. #endif
  828. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  829. #undef JSON_HEDLEY_HAS_EXTENSION
  830. #endif
  831. #if defined(__has_extension)
  832. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  833. #else
  834. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  835. #endif
  836. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  837. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  838. #endif
  839. #if defined(__has_extension)
  840. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  841. #else
  842. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  843. #endif
  844. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  845. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  846. #endif
  847. #if defined(__has_extension)
  848. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  849. #else
  850. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  851. #endif
  852. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  853. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  854. #endif
  855. #if defined(__has_declspec_attribute)
  856. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  857. #else
  858. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  859. #endif
  860. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  861. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  862. #endif
  863. #if defined(__has_declspec_attribute)
  864. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  865. #else
  866. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  867. #endif
  868. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  869. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  870. #endif
  871. #if defined(__has_declspec_attribute)
  872. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  873. #else
  874. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  875. #endif
  876. #if defined(JSON_HEDLEY_HAS_WARNING)
  877. #undef JSON_HEDLEY_HAS_WARNING
  878. #endif
  879. #if defined(__has_warning)
  880. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  881. #else
  882. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  883. #endif
  884. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  885. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  886. #endif
  887. #if defined(__has_warning)
  888. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  889. #else
  890. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  891. #endif
  892. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  893. #undef JSON_HEDLEY_GCC_HAS_WARNING
  894. #endif
  895. #if defined(__has_warning)
  896. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  897. #else
  898. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  899. #endif
  900. #if \
  901. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  902. defined(__clang__) || \
  903. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  904. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  905. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  906. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  907. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  908. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  909. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  910. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  911. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  912. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  913. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  914. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  915. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  916. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  917. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  918. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  919. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  920. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  921. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  922. #else
  923. #define JSON_HEDLEY_PRAGMA(value)
  924. #endif
  925. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  926. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  927. #endif
  928. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  929. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  930. #endif
  931. #if defined(__clang__)
  932. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  933. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  934. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  935. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  936. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  937. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  938. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  939. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  940. #elif \
  941. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  942. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  943. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  944. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  945. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  946. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  947. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  948. #elif \
  949. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  950. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  951. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  952. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  953. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  954. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  955. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  956. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  957. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  958. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  959. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  960. #else
  961. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  962. #define JSON_HEDLEY_DIAGNOSTIC_POP
  963. #endif
  964. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  965. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  966. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  967. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  968. #endif
  969. #if defined(__cplusplus)
  970. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  971. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  972. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  973. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  974. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  975. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  976. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  977. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  978. xpr \
  979. JSON_HEDLEY_DIAGNOSTIC_POP
  980. # else
  981. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  982. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  983. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  984. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  985. xpr \
  986. JSON_HEDLEY_DIAGNOSTIC_POP
  987. # endif
  988. # else
  989. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  990. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  991. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  992. xpr \
  993. JSON_HEDLEY_DIAGNOSTIC_POP
  994. # endif
  995. # endif
  996. #endif
  997. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  998. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  999. #endif
  1000. #if defined(JSON_HEDLEY_CONST_CAST)
  1001. #undef JSON_HEDLEY_CONST_CAST
  1002. #endif
  1003. #if defined(__cplusplus)
  1004. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  1005. #elif \
  1006. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  1007. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  1008. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1009. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  1010. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1011. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  1012. ((T) (expr)); \
  1013. JSON_HEDLEY_DIAGNOSTIC_POP \
  1014. }))
  1015. #else
  1016. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  1017. #endif
  1018. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  1019. #undef JSON_HEDLEY_REINTERPRET_CAST
  1020. #endif
  1021. #if defined(__cplusplus)
  1022. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  1023. #else
  1024. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  1025. #endif
  1026. #if defined(JSON_HEDLEY_STATIC_CAST)
  1027. #undef JSON_HEDLEY_STATIC_CAST
  1028. #endif
  1029. #if defined(__cplusplus)
  1030. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  1031. #else
  1032. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  1033. #endif
  1034. #if defined(JSON_HEDLEY_CPP_CAST)
  1035. #undef JSON_HEDLEY_CPP_CAST
  1036. #endif
  1037. #if defined(__cplusplus)
  1038. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  1039. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1040. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1041. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  1042. ((T) (expr)) \
  1043. JSON_HEDLEY_DIAGNOSTIC_POP
  1044. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  1045. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1046. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1047. _Pragma("diag_suppress=Pe137") \
  1048. JSON_HEDLEY_DIAGNOSTIC_POP
  1049. # else
  1050. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  1051. # endif
  1052. #else
  1053. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  1054. #endif
  1055. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  1056. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1057. #endif
  1058. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  1059. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  1060. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1061. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  1062. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1063. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  1064. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1065. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  1066. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1067. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1068. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1069. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1070. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1071. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  1072. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1073. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1074. #elif \
  1075. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1076. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1077. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1078. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1079. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1080. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1081. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1082. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1083. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1084. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1085. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  1086. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  1087. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  1088. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  1089. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  1090. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  1091. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1092. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  1093. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  1094. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  1095. #else
  1096. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1097. #endif
  1098. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  1099. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1100. #endif
  1101. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1102. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  1103. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1104. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  1105. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1106. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  1107. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1108. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  1109. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1110. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  1111. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1112. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  1113. #elif \
  1114. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  1115. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1116. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1117. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  1118. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1119. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  1120. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1121. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1122. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  1123. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1124. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  1125. #else
  1126. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1127. #endif
  1128. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1129. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1130. #endif
  1131. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1132. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1133. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1134. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1135. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1136. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1137. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1138. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1139. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1140. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1141. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1142. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1143. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1144. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1145. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1146. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1147. #elif \
  1148. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1149. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1150. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1151. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1152. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1153. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1154. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1155. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1156. #else
  1157. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1158. #endif
  1159. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1160. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1161. #endif
  1162. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1163. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1164. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1165. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1166. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1167. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1168. #else
  1169. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1170. #endif
  1171. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1172. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1173. #endif
  1174. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1175. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1176. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1177. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1178. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1179. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1180. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1181. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1182. #else
  1183. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1184. #endif
  1185. #if defined(JSON_HEDLEY_DEPRECATED)
  1186. #undef JSON_HEDLEY_DEPRECATED
  1187. #endif
  1188. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1189. #undef JSON_HEDLEY_DEPRECATED_FOR
  1190. #endif
  1191. #if \
  1192. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1193. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1194. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1195. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1196. #elif \
  1197. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1198. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1199. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1200. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1201. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1202. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1203. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1204. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1205. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1206. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1207. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1208. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1209. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1210. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1211. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1212. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1213. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1214. #elif \
  1215. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1216. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1217. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1218. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1219. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1220. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1221. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1222. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1223. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1224. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1225. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1226. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1227. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1228. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1229. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1230. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1231. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1232. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1233. #elif \
  1234. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1235. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1236. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1237. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1238. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1239. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1240. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1241. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1242. #else
  1243. #define JSON_HEDLEY_DEPRECATED(since)
  1244. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1245. #endif
  1246. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1247. #undef JSON_HEDLEY_UNAVAILABLE
  1248. #endif
  1249. #if \
  1250. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1251. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1252. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1253. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1254. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1255. #else
  1256. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1257. #endif
  1258. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1259. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1260. #endif
  1261. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1262. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1263. #endif
  1264. #if \
  1265. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1266. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1267. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1268. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1269. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1270. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1271. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1272. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1273. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1274. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1275. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1276. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1277. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1278. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1279. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1280. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1281. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1282. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1283. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1284. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1285. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1286. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1287. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1288. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1289. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1290. #elif defined(_Check_return_) /* SAL */
  1291. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1292. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1293. #else
  1294. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1295. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1296. #endif
  1297. #if defined(JSON_HEDLEY_SENTINEL)
  1298. #undef JSON_HEDLEY_SENTINEL
  1299. #endif
  1300. #if \
  1301. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1302. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1303. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1304. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1305. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1306. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1307. #else
  1308. #define JSON_HEDLEY_SENTINEL(position)
  1309. #endif
  1310. #if defined(JSON_HEDLEY_NO_RETURN)
  1311. #undef JSON_HEDLEY_NO_RETURN
  1312. #endif
  1313. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1314. #define JSON_HEDLEY_NO_RETURN __noreturn
  1315. #elif \
  1316. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1317. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1318. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1319. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1320. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1321. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1322. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1323. #elif \
  1324. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1325. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1326. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1327. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1328. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1329. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1330. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1331. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1332. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1333. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1334. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1335. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1336. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1337. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1338. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1339. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1340. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1341. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1342. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1343. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1344. #elif \
  1345. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1346. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1347. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1348. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1349. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1350. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1351. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1352. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1353. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1354. #else
  1355. #define JSON_HEDLEY_NO_RETURN
  1356. #endif
  1357. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1358. #undef JSON_HEDLEY_NO_ESCAPE
  1359. #endif
  1360. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1361. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1362. #else
  1363. #define JSON_HEDLEY_NO_ESCAPE
  1364. #endif
  1365. #if defined(JSON_HEDLEY_UNREACHABLE)
  1366. #undef JSON_HEDLEY_UNREACHABLE
  1367. #endif
  1368. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1369. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1370. #endif
  1371. #if defined(JSON_HEDLEY_ASSUME)
  1372. #undef JSON_HEDLEY_ASSUME
  1373. #endif
  1374. #if \
  1375. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1376. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1377. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1378. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1379. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1380. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1381. #elif \
  1382. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1383. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1384. #if defined(__cplusplus)
  1385. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1386. #else
  1387. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1388. #endif
  1389. #endif
  1390. #if \
  1391. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1392. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1393. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1394. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1395. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1396. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1397. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1398. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1399. #elif defined(JSON_HEDLEY_ASSUME)
  1400. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1401. #endif
  1402. #if !defined(JSON_HEDLEY_ASSUME)
  1403. #if defined(JSON_HEDLEY_UNREACHABLE)
  1404. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1405. #else
  1406. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1407. #endif
  1408. #endif
  1409. #if defined(JSON_HEDLEY_UNREACHABLE)
  1410. #if \
  1411. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1412. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1413. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1414. #else
  1415. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1416. #endif
  1417. #else
  1418. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1419. #endif
  1420. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1421. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1422. #endif
  1423. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1424. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1425. #pragma clang diagnostic ignored "-Wpedantic"
  1426. #endif
  1427. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1428. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1429. #endif
  1430. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1431. #if defined(__clang__)
  1432. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1433. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1434. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1435. #endif
  1436. #endif
  1437. #if defined(JSON_HEDLEY_NON_NULL)
  1438. #undef JSON_HEDLEY_NON_NULL
  1439. #endif
  1440. #if \
  1441. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1442. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1443. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1444. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1445. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1446. #else
  1447. #define JSON_HEDLEY_NON_NULL(...)
  1448. #endif
  1449. JSON_HEDLEY_DIAGNOSTIC_POP
  1450. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1451. #undef JSON_HEDLEY_PRINTF_FORMAT
  1452. #endif
  1453. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1454. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1455. #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
  1456. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
  1457. #elif \
  1458. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1459. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1460. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1461. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1462. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1463. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1464. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1465. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1466. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1467. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1468. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1469. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1470. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1471. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1472. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1473. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1474. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1475. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1476. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1477. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1478. #else
  1479. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1480. #endif
  1481. #if defined(JSON_HEDLEY_CONSTEXPR)
  1482. #undef JSON_HEDLEY_CONSTEXPR
  1483. #endif
  1484. #if defined(__cplusplus)
  1485. #if __cplusplus >= 201103L
  1486. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1487. #endif
  1488. #endif
  1489. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1490. #define JSON_HEDLEY_CONSTEXPR
  1491. #endif
  1492. #if defined(JSON_HEDLEY_PREDICT)
  1493. #undef JSON_HEDLEY_PREDICT
  1494. #endif
  1495. #if defined(JSON_HEDLEY_LIKELY)
  1496. #undef JSON_HEDLEY_LIKELY
  1497. #endif
  1498. #if defined(JSON_HEDLEY_UNLIKELY)
  1499. #undef JSON_HEDLEY_UNLIKELY
  1500. #endif
  1501. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1502. #undef JSON_HEDLEY_UNPREDICTABLE
  1503. #endif
  1504. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1505. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1506. #endif
  1507. #if \
  1508. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1509. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1510. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1511. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1512. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1513. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1514. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1515. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1516. #elif \
  1517. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1518. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1519. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1520. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1521. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1522. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1523. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1524. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1525. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1526. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1527. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1528. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1529. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1530. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1531. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1532. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1533. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1534. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1535. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1536. (__extension__ ({ \
  1537. double hedley_probability_ = (probability); \
  1538. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1539. }))
  1540. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1541. (__extension__ ({ \
  1542. double hedley_probability_ = (probability); \
  1543. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1544. }))
  1545. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1546. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1547. #else
  1548. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1549. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1550. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1551. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1552. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1553. #endif
  1554. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1555. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1556. #endif
  1557. #if defined(JSON_HEDLEY_MALLOC)
  1558. #undef JSON_HEDLEY_MALLOC
  1559. #endif
  1560. #if \
  1561. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1562. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1563. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1564. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1565. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1566. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1567. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1568. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1569. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1570. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1571. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1572. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1573. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1574. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1575. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1576. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1577. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1578. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1579. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1580. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1581. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1582. #elif \
  1583. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1584. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1585. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1586. #else
  1587. #define JSON_HEDLEY_MALLOC
  1588. #endif
  1589. #if defined(JSON_HEDLEY_PURE)
  1590. #undef JSON_HEDLEY_PURE
  1591. #endif
  1592. #if \
  1593. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1594. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1595. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1596. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1597. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1598. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1599. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1600. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1601. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1602. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1603. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1604. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1605. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1606. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1607. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1608. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1609. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1610. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1611. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1612. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1613. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1614. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1615. #elif defined(__cplusplus) && \
  1616. ( \
  1617. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1618. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1619. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1620. )
  1621. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1622. #else
  1623. # define JSON_HEDLEY_PURE
  1624. #endif
  1625. #if defined(JSON_HEDLEY_CONST)
  1626. #undef JSON_HEDLEY_CONST
  1627. #endif
  1628. #if \
  1629. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1630. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1631. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1632. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1633. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1634. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1635. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1636. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1637. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1638. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1639. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1640. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1641. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1642. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1643. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1644. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1645. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1646. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1647. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1648. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1649. #elif \
  1650. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1651. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1652. #else
  1653. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1654. #endif
  1655. #if defined(JSON_HEDLEY_RESTRICT)
  1656. #undef JSON_HEDLEY_RESTRICT
  1657. #endif
  1658. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1659. #define JSON_HEDLEY_RESTRICT restrict
  1660. #elif \
  1661. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1662. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1663. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1664. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1665. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1666. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1667. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1668. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1669. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1670. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1671. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1672. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1673. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1674. defined(__clang__) || \
  1675. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1676. #define JSON_HEDLEY_RESTRICT __restrict
  1677. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1678. #define JSON_HEDLEY_RESTRICT _Restrict
  1679. #else
  1680. #define JSON_HEDLEY_RESTRICT
  1681. #endif
  1682. #if defined(JSON_HEDLEY_INLINE)
  1683. #undef JSON_HEDLEY_INLINE
  1684. #endif
  1685. #if \
  1686. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1687. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1688. #define JSON_HEDLEY_INLINE inline
  1689. #elif \
  1690. defined(JSON_HEDLEY_GCC_VERSION) || \
  1691. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1692. #define JSON_HEDLEY_INLINE __inline__
  1693. #elif \
  1694. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1695. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1696. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1697. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1698. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1699. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1700. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1701. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1702. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1703. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1704. #define JSON_HEDLEY_INLINE __inline
  1705. #else
  1706. #define JSON_HEDLEY_INLINE
  1707. #endif
  1708. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1709. #undef JSON_HEDLEY_ALWAYS_INLINE
  1710. #endif
  1711. #if \
  1712. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1713. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1714. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1715. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1716. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1717. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1718. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1719. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1720. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1721. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1722. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1723. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1724. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1725. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1726. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1727. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1728. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1729. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1730. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1731. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1732. #elif \
  1733. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1734. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1735. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1736. #elif defined(__cplusplus) && \
  1737. ( \
  1738. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1739. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1740. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1741. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1742. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1743. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1744. )
  1745. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1746. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1747. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1748. #else
  1749. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1750. #endif
  1751. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1752. #undef JSON_HEDLEY_NEVER_INLINE
  1753. #endif
  1754. #if \
  1755. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1756. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1757. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1758. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1759. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1760. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1761. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1762. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1763. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1764. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1765. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1766. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1767. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1768. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1769. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1770. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1771. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1772. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1773. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1774. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1775. #elif \
  1776. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1777. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1778. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1779. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1780. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1781. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1782. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1783. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1784. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1785. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1786. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1787. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1788. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1789. #else
  1790. #define JSON_HEDLEY_NEVER_INLINE
  1791. #endif
  1792. #if defined(JSON_HEDLEY_PRIVATE)
  1793. #undef JSON_HEDLEY_PRIVATE
  1794. #endif
  1795. #if defined(JSON_HEDLEY_PUBLIC)
  1796. #undef JSON_HEDLEY_PUBLIC
  1797. #endif
  1798. #if defined(JSON_HEDLEY_IMPORT)
  1799. #undef JSON_HEDLEY_IMPORT
  1800. #endif
  1801. #if defined(_WIN32) || defined(__CYGWIN__)
  1802. # define JSON_HEDLEY_PRIVATE
  1803. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1804. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1805. #else
  1806. # if \
  1807. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1808. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1809. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1810. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1811. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1812. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1813. ( \
  1814. defined(__TI_EABI__) && \
  1815. ( \
  1816. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1817. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1818. ) \
  1819. ) || \
  1820. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1821. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1822. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1823. # else
  1824. # define JSON_HEDLEY_PRIVATE
  1825. # define JSON_HEDLEY_PUBLIC
  1826. # endif
  1827. # define JSON_HEDLEY_IMPORT extern
  1828. #endif
  1829. #if defined(JSON_HEDLEY_NO_THROW)
  1830. #undef JSON_HEDLEY_NO_THROW
  1831. #endif
  1832. #if \
  1833. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1834. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1835. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1836. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1837. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1838. #elif \
  1839. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1840. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1841. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1842. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1843. #else
  1844. #define JSON_HEDLEY_NO_THROW
  1845. #endif
  1846. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1847. #undef JSON_HEDLEY_FALL_THROUGH
  1848. #endif
  1849. #if \
  1850. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1851. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1852. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1853. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1854. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1855. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1856. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1857. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1858. #elif defined(__fallthrough) /* SAL */
  1859. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1860. #else
  1861. #define JSON_HEDLEY_FALL_THROUGH
  1862. #endif
  1863. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1864. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1865. #endif
  1866. #if \
  1867. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1868. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1869. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1870. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1871. #elif defined(_Ret_notnull_) /* SAL */
  1872. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1873. #else
  1874. #define JSON_HEDLEY_RETURNS_NON_NULL
  1875. #endif
  1876. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1877. #undef JSON_HEDLEY_ARRAY_PARAM
  1878. #endif
  1879. #if \
  1880. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1881. !defined(__STDC_NO_VLA__) && \
  1882. !defined(__cplusplus) && \
  1883. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1884. !defined(JSON_HEDLEY_TINYC_VERSION)
  1885. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1886. #else
  1887. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1888. #endif
  1889. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1890. #undef JSON_HEDLEY_IS_CONSTANT
  1891. #endif
  1892. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1893. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1894. #endif
  1895. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1896. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1897. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1898. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1899. #endif
  1900. #if \
  1901. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1902. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1903. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1904. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1905. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1906. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1907. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1908. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1909. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1910. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1911. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1912. #endif
  1913. #if !defined(__cplusplus)
  1914. # if \
  1915. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1916. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1917. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1918. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1919. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1920. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1921. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1922. #if defined(__INTPTR_TYPE__)
  1923. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1924. #else
  1925. #include <stdint.h>
  1926. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1927. #endif
  1928. # elif \
  1929. ( \
  1930. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1931. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1932. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1933. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1934. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1935. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1936. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1937. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1938. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1939. #if defined(__INTPTR_TYPE__)
  1940. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1941. #else
  1942. #include <stdint.h>
  1943. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1944. #endif
  1945. # elif \
  1946. defined(JSON_HEDLEY_GCC_VERSION) || \
  1947. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1948. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1949. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1950. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1951. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1952. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1953. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1954. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1955. defined(__clang__)
  1956. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1957. sizeof(void) != \
  1958. sizeof(*( \
  1959. 1 ? \
  1960. ((void*) ((expr) * 0L) ) : \
  1961. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1962. ) \
  1963. ) \
  1964. )
  1965. # endif
  1966. #endif
  1967. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1968. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1969. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1970. #endif
  1971. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1972. #else
  1973. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1974. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1975. #endif
  1976. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1977. #endif
  1978. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1979. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1980. #endif
  1981. #if defined(JSON_HEDLEY_END_C_DECLS)
  1982. #undef JSON_HEDLEY_END_C_DECLS
  1983. #endif
  1984. #if defined(JSON_HEDLEY_C_DECL)
  1985. #undef JSON_HEDLEY_C_DECL
  1986. #endif
  1987. #if defined(__cplusplus)
  1988. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1989. #define JSON_HEDLEY_END_C_DECLS }
  1990. #define JSON_HEDLEY_C_DECL extern "C"
  1991. #else
  1992. #define JSON_HEDLEY_BEGIN_C_DECLS
  1993. #define JSON_HEDLEY_END_C_DECLS
  1994. #define JSON_HEDLEY_C_DECL
  1995. #endif
  1996. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1997. #undef JSON_HEDLEY_STATIC_ASSERT
  1998. #endif
  1999. #if \
  2000. !defined(__cplusplus) && ( \
  2001. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  2002. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  2003. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  2004. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  2005. defined(_Static_assert) \
  2006. )
  2007. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  2008. #elif \
  2009. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  2010. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  2011. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2012. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  2013. #else
  2014. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  2015. #endif
  2016. #if defined(JSON_HEDLEY_NULL)
  2017. #undef JSON_HEDLEY_NULL
  2018. #endif
  2019. #if defined(__cplusplus)
  2020. #if __cplusplus >= 201103L
  2021. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  2022. #elif defined(NULL)
  2023. #define JSON_HEDLEY_NULL NULL
  2024. #else
  2025. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  2026. #endif
  2027. #elif defined(NULL)
  2028. #define JSON_HEDLEY_NULL NULL
  2029. #else
  2030. #define JSON_HEDLEY_NULL ((void*) 0)
  2031. #endif
  2032. #if defined(JSON_HEDLEY_MESSAGE)
  2033. #undef JSON_HEDLEY_MESSAGE
  2034. #endif
  2035. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2036. # define JSON_HEDLEY_MESSAGE(msg) \
  2037. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2038. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2039. JSON_HEDLEY_PRAGMA(message msg) \
  2040. JSON_HEDLEY_DIAGNOSTIC_POP
  2041. #elif \
  2042. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  2043. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2044. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  2045. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  2046. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  2047. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  2048. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2049. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  2050. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2051. #else
  2052. # define JSON_HEDLEY_MESSAGE(msg)
  2053. #endif
  2054. #if defined(JSON_HEDLEY_WARNING)
  2055. #undef JSON_HEDLEY_WARNING
  2056. #endif
  2057. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2058. # define JSON_HEDLEY_WARNING(msg) \
  2059. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2060. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2061. JSON_HEDLEY_PRAGMA(clang warning msg) \
  2062. JSON_HEDLEY_DIAGNOSTIC_POP
  2063. #elif \
  2064. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  2065. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  2066. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2067. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  2068. #elif \
  2069. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  2070. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2071. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2072. #else
  2073. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  2074. #endif
  2075. #if defined(JSON_HEDLEY_REQUIRE)
  2076. #undef JSON_HEDLEY_REQUIRE
  2077. #endif
  2078. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  2079. #undef JSON_HEDLEY_REQUIRE_MSG
  2080. #endif
  2081. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  2082. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  2083. # define JSON_HEDLEY_REQUIRE(expr) \
  2084. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2085. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2086. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  2087. JSON_HEDLEY_DIAGNOSTIC_POP
  2088. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  2089. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2090. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2091. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  2092. JSON_HEDLEY_DIAGNOSTIC_POP
  2093. # else
  2094. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  2095. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  2096. # endif
  2097. #else
  2098. # define JSON_HEDLEY_REQUIRE(expr)
  2099. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  2100. #endif
  2101. #if defined(JSON_HEDLEY_FLAGS)
  2102. #undef JSON_HEDLEY_FLAGS
  2103. #endif
  2104. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  2105. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  2106. #else
  2107. #define JSON_HEDLEY_FLAGS
  2108. #endif
  2109. #if defined(JSON_HEDLEY_FLAGS_CAST)
  2110. #undef JSON_HEDLEY_FLAGS_CAST
  2111. #endif
  2112. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  2113. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  2114. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2115. _Pragma("warning(disable:188)") \
  2116. ((T) (expr)); \
  2117. JSON_HEDLEY_DIAGNOSTIC_POP \
  2118. }))
  2119. #else
  2120. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  2121. #endif
  2122. #if defined(JSON_HEDLEY_EMPTY_BASES)
  2123. #undef JSON_HEDLEY_EMPTY_BASES
  2124. #endif
  2125. #if \
  2126. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2127. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2128. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2129. #else
  2130. #define JSON_HEDLEY_EMPTY_BASES
  2131. #endif
  2132. /* Remaining macros are deprecated. */
  2133. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2134. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2135. #endif
  2136. #if defined(__clang__)
  2137. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2138. #else
  2139. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2140. #endif
  2141. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2142. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2143. #endif
  2144. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2145. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2146. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2147. #endif
  2148. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2149. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2150. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2151. #endif
  2152. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2153. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2154. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2155. #endif
  2156. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2157. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2158. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2159. #endif
  2160. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2161. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2162. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2163. #endif
  2164. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2165. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2166. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2167. #endif
  2168. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2169. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2170. // This file contains all internal macro definitions (except those affecting ABI)
  2171. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2172. // #include <nlohmann/detail/abi_macros.hpp>
  2173. // exclude unsupported compilers
  2174. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2175. #if defined(__clang__)
  2176. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2177. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2178. #endif
  2179. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2180. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2181. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2182. #endif
  2183. #endif
  2184. #endif
  2185. // C++ language standard detection
  2186. // if the user manually specified the used c++ version this is skipped
  2187. #if !defined(JSON_HAS_CPP_23) && !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2188. #if (defined(__cplusplus) && __cplusplus > 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202002L)
  2189. #define JSON_HAS_CPP_23
  2190. #define JSON_HAS_CPP_20
  2191. #define JSON_HAS_CPP_17
  2192. #define JSON_HAS_CPP_14
  2193. #elif (defined(__cplusplus) && __cplusplus > 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG > 201703L)
  2194. #define JSON_HAS_CPP_20
  2195. #define JSON_HAS_CPP_17
  2196. #define JSON_HAS_CPP_14
  2197. #elif (defined(__cplusplus) && __cplusplus > 201402L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2198. #define JSON_HAS_CPP_17
  2199. #define JSON_HAS_CPP_14
  2200. #elif (defined(__cplusplus) && __cplusplus > 201103L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2201. #define JSON_HAS_CPP_14
  2202. #endif
  2203. // the cpp 11 flag is always specified because it is the minimal required version
  2204. #define JSON_HAS_CPP_11
  2205. #endif
  2206. #ifdef __has_include
  2207. #if __has_include(<version>)
  2208. #include <version>
  2209. #endif
  2210. #endif
  2211. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2212. #ifdef JSON_HAS_CPP_17
  2213. #if defined(__cpp_lib_filesystem)
  2214. #define JSON_HAS_FILESYSTEM 1
  2215. #elif defined(__cpp_lib_experimental_filesystem)
  2216. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2217. #elif !defined(__has_include)
  2218. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2219. #elif __has_include(<filesystem>)
  2220. #define JSON_HAS_FILESYSTEM 1
  2221. #elif __has_include(<experimental/filesystem>)
  2222. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2223. #endif
  2224. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2225. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2226. #undef JSON_HAS_FILESYSTEM
  2227. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2228. #endif
  2229. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2230. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2231. #undef JSON_HAS_FILESYSTEM
  2232. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2233. #endif
  2234. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2235. #if defined(__clang_major__) && __clang_major__ < 7
  2236. #undef JSON_HAS_FILESYSTEM
  2237. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2238. #endif
  2239. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2240. #if defined(_MSC_VER) && _MSC_VER < 1914
  2241. #undef JSON_HAS_FILESYSTEM
  2242. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2243. #endif
  2244. // no filesystem support before iOS 13
  2245. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2246. #undef JSON_HAS_FILESYSTEM
  2247. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2248. #endif
  2249. // no filesystem support before macOS Catalina
  2250. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2251. #undef JSON_HAS_FILESYSTEM
  2252. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2253. #endif
  2254. #endif
  2255. #endif
  2256. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2257. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2258. #endif
  2259. #ifndef JSON_HAS_FILESYSTEM
  2260. #define JSON_HAS_FILESYSTEM 0
  2261. #endif
  2262. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2263. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2264. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2265. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2266. #else
  2267. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2268. #endif
  2269. #endif
  2270. #ifndef JSON_HAS_RANGES
  2271. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
  2272. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2273. #define JSON_HAS_RANGES 0
  2274. #elif defined(__cpp_lib_ranges)
  2275. #define JSON_HAS_RANGES 1
  2276. #else
  2277. #define JSON_HAS_RANGES 0
  2278. #endif
  2279. #endif
  2280. #ifndef JSON_HAS_STATIC_RTTI
  2281. #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
  2282. #define JSON_HAS_STATIC_RTTI 1
  2283. #else
  2284. #define JSON_HAS_STATIC_RTTI 0
  2285. #endif
  2286. #endif
  2287. #ifdef JSON_HAS_CPP_17
  2288. #define JSON_INLINE_VARIABLE inline
  2289. #else
  2290. #define JSON_INLINE_VARIABLE
  2291. #endif
  2292. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2293. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2294. #else
  2295. #define JSON_NO_UNIQUE_ADDRESS
  2296. #endif
  2297. // disable documentation warnings on clang
  2298. #if defined(__clang__)
  2299. #pragma clang diagnostic push
  2300. #pragma clang diagnostic ignored "-Wdocumentation"
  2301. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2302. #endif
  2303. // allow disabling exceptions
  2304. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2305. #define JSON_THROW(exception) throw exception
  2306. #define JSON_TRY try
  2307. #define JSON_CATCH(exception) catch(exception)
  2308. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2309. #else
  2310. #include <cstdlib>
  2311. #define JSON_THROW(exception) std::abort()
  2312. #define JSON_TRY if(true)
  2313. #define JSON_CATCH(exception) if(false)
  2314. #define JSON_INTERNAL_CATCH(exception) if(false)
  2315. #endif
  2316. // override exception macros
  2317. #if defined(JSON_THROW_USER)
  2318. #undef JSON_THROW
  2319. #define JSON_THROW JSON_THROW_USER
  2320. #endif
  2321. #if defined(JSON_TRY_USER)
  2322. #undef JSON_TRY
  2323. #define JSON_TRY JSON_TRY_USER
  2324. #endif
  2325. #if defined(JSON_CATCH_USER)
  2326. #undef JSON_CATCH
  2327. #define JSON_CATCH JSON_CATCH_USER
  2328. #undef JSON_INTERNAL_CATCH
  2329. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2330. #endif
  2331. #if defined(JSON_INTERNAL_CATCH_USER)
  2332. #undef JSON_INTERNAL_CATCH
  2333. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2334. #endif
  2335. // allow overriding assert
  2336. #if !defined(JSON_ASSERT)
  2337. #include <cassert> // assert
  2338. #define JSON_ASSERT(x) assert(x)
  2339. #endif
  2340. // allow to access some private functions (needed by the test suite)
  2341. #if defined(JSON_TESTS_PRIVATE)
  2342. #define JSON_PRIVATE_UNLESS_TESTED public
  2343. #else
  2344. #define JSON_PRIVATE_UNLESS_TESTED private
  2345. #endif
  2346. /*!
  2347. @brief macro to briefly define a mapping between an enum and JSON
  2348. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2349. @since version 3.4.0
  2350. */
  2351. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2352. template<typename BasicJsonType> \
  2353. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2354. { \
  2355. /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
  2356. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2357. /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
  2358. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2359. auto it = std::find_if(std::begin(m), std::end(m), \
  2360. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2361. { \
  2362. return ej_pair.first == e; \
  2363. }); \
  2364. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2365. } \
  2366. template<typename BasicJsonType> \
  2367. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2368. { \
  2369. /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
  2370. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2371. /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
  2372. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2373. auto it = std::find_if(std::begin(m), std::end(m), \
  2374. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2375. { \
  2376. return ej_pair.second == j; \
  2377. }); \
  2378. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2379. }
  2380. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2381. // may be removed in the future once the class is split.
  2382. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2383. template<template<typename, typename, typename...> class ObjectType, \
  2384. template<typename, typename...> class ArrayType, \
  2385. class StringType, class BooleanType, class NumberIntegerType, \
  2386. class NumberUnsignedType, class NumberFloatType, \
  2387. template<typename> class AllocatorType, \
  2388. template<typename, typename = void> class JSONSerializer, \
  2389. class BinaryType, \
  2390. class CustomBaseClass>
  2391. #define NLOHMANN_BASIC_JSON_TPL \
  2392. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2393. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2394. AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
  2395. // Macros to simplify conversion from/to types
  2396. #define NLOHMANN_JSON_EXPAND( x ) x
  2397. #define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME
  2398. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2399. NLOHMANN_JSON_PASTE64, \
  2400. NLOHMANN_JSON_PASTE63, \
  2401. NLOHMANN_JSON_PASTE62, \
  2402. NLOHMANN_JSON_PASTE61, \
  2403. NLOHMANN_JSON_PASTE60, \
  2404. NLOHMANN_JSON_PASTE59, \
  2405. NLOHMANN_JSON_PASTE58, \
  2406. NLOHMANN_JSON_PASTE57, \
  2407. NLOHMANN_JSON_PASTE56, \
  2408. NLOHMANN_JSON_PASTE55, \
  2409. NLOHMANN_JSON_PASTE54, \
  2410. NLOHMANN_JSON_PASTE53, \
  2411. NLOHMANN_JSON_PASTE52, \
  2412. NLOHMANN_JSON_PASTE51, \
  2413. NLOHMANN_JSON_PASTE50, \
  2414. NLOHMANN_JSON_PASTE49, \
  2415. NLOHMANN_JSON_PASTE48, \
  2416. NLOHMANN_JSON_PASTE47, \
  2417. NLOHMANN_JSON_PASTE46, \
  2418. NLOHMANN_JSON_PASTE45, \
  2419. NLOHMANN_JSON_PASTE44, \
  2420. NLOHMANN_JSON_PASTE43, \
  2421. NLOHMANN_JSON_PASTE42, \
  2422. NLOHMANN_JSON_PASTE41, \
  2423. NLOHMANN_JSON_PASTE40, \
  2424. NLOHMANN_JSON_PASTE39, \
  2425. NLOHMANN_JSON_PASTE38, \
  2426. NLOHMANN_JSON_PASTE37, \
  2427. NLOHMANN_JSON_PASTE36, \
  2428. NLOHMANN_JSON_PASTE35, \
  2429. NLOHMANN_JSON_PASTE34, \
  2430. NLOHMANN_JSON_PASTE33, \
  2431. NLOHMANN_JSON_PASTE32, \
  2432. NLOHMANN_JSON_PASTE31, \
  2433. NLOHMANN_JSON_PASTE30, \
  2434. NLOHMANN_JSON_PASTE29, \
  2435. NLOHMANN_JSON_PASTE28, \
  2436. NLOHMANN_JSON_PASTE27, \
  2437. NLOHMANN_JSON_PASTE26, \
  2438. NLOHMANN_JSON_PASTE25, \
  2439. NLOHMANN_JSON_PASTE24, \
  2440. NLOHMANN_JSON_PASTE23, \
  2441. NLOHMANN_JSON_PASTE22, \
  2442. NLOHMANN_JSON_PASTE21, \
  2443. NLOHMANN_JSON_PASTE20, \
  2444. NLOHMANN_JSON_PASTE19, \
  2445. NLOHMANN_JSON_PASTE18, \
  2446. NLOHMANN_JSON_PASTE17, \
  2447. NLOHMANN_JSON_PASTE16, \
  2448. NLOHMANN_JSON_PASTE15, \
  2449. NLOHMANN_JSON_PASTE14, \
  2450. NLOHMANN_JSON_PASTE13, \
  2451. NLOHMANN_JSON_PASTE12, \
  2452. NLOHMANN_JSON_PASTE11, \
  2453. NLOHMANN_JSON_PASTE10, \
  2454. NLOHMANN_JSON_PASTE9, \
  2455. NLOHMANN_JSON_PASTE8, \
  2456. NLOHMANN_JSON_PASTE7, \
  2457. NLOHMANN_JSON_PASTE6, \
  2458. NLOHMANN_JSON_PASTE5, \
  2459. NLOHMANN_JSON_PASTE4, \
  2460. NLOHMANN_JSON_PASTE3, \
  2461. NLOHMANN_JSON_PASTE2, \
  2462. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2463. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2464. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2465. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2466. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2467. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2468. #define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6)
  2469. #define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7)
  2470. #define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8)
  2471. #define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9)
  2472. #define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10)
  2473. #define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11)
  2474. #define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12)
  2475. #define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13)
  2476. #define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14)
  2477. #define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15)
  2478. #define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16)
  2479. #define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17)
  2480. #define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18)
  2481. #define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19)
  2482. #define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20)
  2483. #define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21)
  2484. #define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)
  2485. #define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23)
  2486. #define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24)
  2487. #define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25)
  2488. #define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26)
  2489. #define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27)
  2490. #define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28)
  2491. #define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29)
  2492. #define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30)
  2493. #define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31)
  2494. #define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32)
  2495. #define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33)
  2496. #define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34)
  2497. #define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35)
  2498. #define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36)
  2499. #define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37)
  2500. #define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38)
  2501. #define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39)
  2502. #define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40)
  2503. #define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41)
  2504. #define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42)
  2505. #define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43)
  2506. #define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44)
  2507. #define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45)
  2508. #define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46)
  2509. #define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47)
  2510. #define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48)
  2511. #define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49)
  2512. #define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50)
  2513. #define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51)
  2514. #define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52)
  2515. #define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53)
  2516. #define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54)
  2517. #define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55)
  2518. #define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56)
  2519. #define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57)
  2520. #define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58)
  2521. #define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59)
  2522. #define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60)
  2523. #define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61)
  2524. #define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62)
  2525. #define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63)
  2526. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2527. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2528. #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = !nlohmann_json_j.is_null() ? nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1) : nlohmann_json_default_obj.v1;
  2529. /*!
  2530. @brief macro
  2531. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2532. @since version 3.9.0
  2533. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
  2534. */
  2535. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2536. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2537. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2538. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2539. friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2540. /*!
  2541. @brief macro
  2542. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT
  2543. @since version 3.11.0
  2544. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
  2545. */
  2546. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2547. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2548. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2549. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2550. friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2551. /*!
  2552. @brief macro
  2553. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE
  2554. @since version 3.11.3
  2555. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
  2556. */
  2557. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2558. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2559. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2560. /*!
  2561. @brief macro
  2562. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2563. @since version 3.9.0
  2564. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
  2565. */
  2566. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2567. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2568. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2569. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2570. void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2571. /*!
  2572. @brief macro
  2573. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT
  2574. @since version 3.11.0
  2575. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
  2576. */
  2577. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2578. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2579. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2580. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2581. void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2582. /*!
  2583. @brief macro
  2584. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
  2585. @since version 3.11.3
  2586. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
  2587. */
  2588. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2589. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2590. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2591. /*!
  2592. @brief macro
  2593. @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE
  2594. @since version 3.12.0
  2595. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2596. */
  2597. #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Type, BaseType, ...) \
  2598. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2599. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2600. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2601. friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2602. /*!
  2603. @brief macro
  2604. @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT
  2605. @since version 3.12.0
  2606. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2607. */
  2608. #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
  2609. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2610. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2611. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2612. friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2613. /*!
  2614. @brief macro
  2615. @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE
  2616. @since version 3.12.0
  2617. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2618. */
  2619. #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
  2620. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2621. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2622. /*!
  2623. @brief macro
  2624. @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE
  2625. @since version 3.12.0
  2626. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2627. */
  2628. #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(Type, BaseType, ...) \
  2629. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2630. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2631. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2632. void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2633. /*!
  2634. @brief macro
  2635. @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT
  2636. @since version 3.12.0
  2637. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2638. */
  2639. #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
  2640. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2641. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2642. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2643. void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2644. /*!
  2645. @brief macro
  2646. @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
  2647. @since version 3.12.0
  2648. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2649. */
  2650. #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
  2651. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2652. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2653. // inspired from https://stackoverflow.com/a/26745591
  2654. // allows calling any std function as if (e.g., with begin):
  2655. // using std::begin; begin(x);
  2656. //
  2657. // it allows using the detected idiom to retrieve the return type
  2658. // of such an expression
  2659. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2660. namespace detail { \
  2661. using std::std_name; \
  2662. \
  2663. template<typename... T> \
  2664. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2665. } \
  2666. \
  2667. namespace detail2 { \
  2668. struct std_name##_tag \
  2669. { \
  2670. }; \
  2671. \
  2672. template<typename... T> \
  2673. std_name##_tag std_name(T&&...); \
  2674. \
  2675. template<typename... T> \
  2676. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2677. \
  2678. template<typename... T> \
  2679. struct would_call_std_##std_name \
  2680. { \
  2681. static constexpr auto const value = ::nlohmann::detail:: \
  2682. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2683. }; \
  2684. } /* namespace detail2 */ \
  2685. \
  2686. template<typename... T> \
  2687. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2688. { \
  2689. }
  2690. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2691. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2692. #endif
  2693. #if JSON_USE_IMPLICIT_CONVERSIONS
  2694. #define JSON_EXPLICIT
  2695. #else
  2696. #define JSON_EXPLICIT explicit
  2697. #endif
  2698. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2699. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2700. #endif
  2701. #ifndef JSON_USE_GLOBAL_UDLS
  2702. #define JSON_USE_GLOBAL_UDLS 1
  2703. #endif
  2704. #if JSON_HAS_THREE_WAY_COMPARISON
  2705. #include <compare> // partial_ordering
  2706. #endif
  2707. NLOHMANN_JSON_NAMESPACE_BEGIN
  2708. namespace detail
  2709. {
  2710. ///////////////////////////
  2711. // JSON type enumeration //
  2712. ///////////////////////////
  2713. /*!
  2714. @brief the JSON type enumeration
  2715. This enumeration collects the different JSON types. It is internally used to
  2716. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2717. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2718. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2719. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2720. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2721. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2722. @ref basic_json::is_structured() rely on it.
  2723. @note There are three enumeration entries (number_integer, number_unsigned, and
  2724. number_float), because the library distinguishes these three types for numbers:
  2725. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2726. @ref basic_json::number_integer_t is used for signed integers, and
  2727. @ref basic_json::number_float_t is used for floating-point numbers or to
  2728. approximate integers which do not fit in the limits of their respective type.
  2729. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2730. value with the default value for a given type
  2731. @since version 1.0.0
  2732. */
  2733. enum class value_t : std::uint8_t
  2734. {
  2735. null, ///< null value
  2736. object, ///< object (unordered set of name/value pairs)
  2737. array, ///< array (ordered collection of values)
  2738. string, ///< string value
  2739. boolean, ///< boolean value
  2740. number_integer, ///< number value (signed integer)
  2741. number_unsigned, ///< number value (unsigned integer)
  2742. number_float, ///< number value (floating-point)
  2743. binary, ///< binary array (ordered collection of bytes)
  2744. discarded ///< discarded by the parser callback function
  2745. };
  2746. /*!
  2747. @brief comparison operator for JSON types
  2748. Returns an ordering that is similar to Python:
  2749. - order: null < boolean < number < object < array < string < binary
  2750. - furthermore, each type is not smaller than itself
  2751. - discarded values are not comparable
  2752. - binary is represented as a b"" string in python and directly comparable to a
  2753. string; however, making a binary array directly comparable with a string would
  2754. be surprising behavior in a JSON file.
  2755. @since version 1.0.0
  2756. */
  2757. #if JSON_HAS_THREE_WAY_COMPARISON
  2758. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2759. #else
  2760. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2761. #endif
  2762. {
  2763. static constexpr std::array<std::uint8_t, 9> order = {{
  2764. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2765. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2766. 6 /* binary */
  2767. }
  2768. };
  2769. const auto l_index = static_cast<std::size_t>(lhs);
  2770. const auto r_index = static_cast<std::size_t>(rhs);
  2771. #if JSON_HAS_THREE_WAY_COMPARISON
  2772. if (l_index < order.size() && r_index < order.size())
  2773. {
  2774. return order[l_index] <=> order[r_index]; // *NOPAD*
  2775. }
  2776. return std::partial_ordering::unordered;
  2777. #else
  2778. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2779. #endif
  2780. }
  2781. // GCC selects the built-in operator< over an operator rewritten from
  2782. // a user-defined spaceship operator
  2783. // Clang, MSVC, and ICC select the rewritten candidate
  2784. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2785. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2786. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2787. {
  2788. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2789. }
  2790. #endif
  2791. } // namespace detail
  2792. NLOHMANN_JSON_NAMESPACE_END
  2793. // #include <nlohmann/detail/string_escape.hpp>
  2794. // __ _____ _____ _____
  2795. // __| | __| | | | JSON for Modern C++
  2796. // | | |__ | | | | | | version 3.12.0
  2797. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2798. //
  2799. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  2800. // SPDX-License-Identifier: MIT
  2801. // #include <nlohmann/detail/abi_macros.hpp>
  2802. NLOHMANN_JSON_NAMESPACE_BEGIN
  2803. namespace detail
  2804. {
  2805. /*!
  2806. @brief replace all occurrences of a substring by another string
  2807. @param[in,out] s the string to manipulate; changed so that all
  2808. occurrences of @a f are replaced with @a t
  2809. @param[in] f the substring to replace with @a t
  2810. @param[in] t the string to replace @a f
  2811. @pre The search string @a f must not be empty. **This precondition is
  2812. enforced with an assertion.**
  2813. @since version 2.0.0
  2814. */
  2815. template<typename StringType>
  2816. inline void replace_substring(StringType& s, const StringType& f,
  2817. const StringType& t)
  2818. {
  2819. JSON_ASSERT(!f.empty());
  2820. for (auto pos = s.find(f); // find first occurrence of f
  2821. pos != StringType::npos; // make sure f was found
  2822. s.replace(pos, f.size(), t), // replace with t, and
  2823. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2824. {}
  2825. }
  2826. /*!
  2827. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2828. * @param[in] s string to escape
  2829. * @return escaped string
  2830. *
  2831. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2832. */
  2833. template<typename StringType>
  2834. inline StringType escape(StringType s)
  2835. {
  2836. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2837. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2838. return s;
  2839. }
  2840. /*!
  2841. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2842. * @param[in] s string to unescape
  2843. * @return unescaped string
  2844. *
  2845. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2846. */
  2847. template<typename StringType>
  2848. static void unescape(StringType& s)
  2849. {
  2850. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2851. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2852. }
  2853. } // namespace detail
  2854. NLOHMANN_JSON_NAMESPACE_END
  2855. // #include <nlohmann/detail/input/position_t.hpp>
  2856. // __ _____ _____ _____
  2857. // __| | __| | | | JSON for Modern C++
  2858. // | | |__ | | | | | | version 3.12.0
  2859. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2860. //
  2861. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  2862. // SPDX-License-Identifier: MIT
  2863. #include <cstddef> // size_t
  2864. // #include <nlohmann/detail/abi_macros.hpp>
  2865. NLOHMANN_JSON_NAMESPACE_BEGIN
  2866. namespace detail
  2867. {
  2868. /// struct to capture the start position of the current token
  2869. struct position_t
  2870. {
  2871. /// the total number of characters read
  2872. std::size_t chars_read_total = 0;
  2873. /// the number of characters read in the current line
  2874. std::size_t chars_read_current_line = 0;
  2875. /// the number of lines read
  2876. std::size_t lines_read = 0;
  2877. /// conversion to size_t to preserve SAX interface
  2878. constexpr operator size_t() const
  2879. {
  2880. return chars_read_total;
  2881. }
  2882. };
  2883. } // namespace detail
  2884. NLOHMANN_JSON_NAMESPACE_END
  2885. // #include <nlohmann/detail/macro_scope.hpp>
  2886. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2887. // __ _____ _____ _____
  2888. // __| | __| | | | JSON for Modern C++
  2889. // | | |__ | | | | | | version 3.12.0
  2890. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2891. //
  2892. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  2893. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2894. // SPDX-License-Identifier: MIT
  2895. #include <array> // array
  2896. #include <cstddef> // size_t
  2897. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2898. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2899. // #include <nlohmann/detail/macro_scope.hpp>
  2900. NLOHMANN_JSON_NAMESPACE_BEGIN
  2901. namespace detail
  2902. {
  2903. template<typename T>
  2904. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2905. #ifdef JSON_HAS_CPP_14
  2906. // the following utilities are natively available in C++14
  2907. using std::enable_if_t;
  2908. using std::index_sequence;
  2909. using std::make_index_sequence;
  2910. using std::index_sequence_for;
  2911. #else
  2912. // alias templates to reduce boilerplate
  2913. template<bool B, typename T = void>
  2914. using enable_if_t = typename std::enable_if<B, T>::type;
  2915. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2916. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2917. //// START OF CODE FROM GOOGLE ABSEIL
  2918. // integer_sequence
  2919. //
  2920. // Class template representing a compile-time integer sequence. An instantiation
  2921. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2922. // type through its template arguments (which is a common need when
  2923. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2924. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2925. //
  2926. // Example:
  2927. //
  2928. // template< class T, T... Ints >
  2929. // void user_function(integer_sequence<T, Ints...>);
  2930. //
  2931. // int main()
  2932. // {
  2933. // // user_function's `T` will be deduced to `int` and `Ints...`
  2934. // // will be deduced to `0, 1, 2, 3, 4`.
  2935. // user_function(make_integer_sequence<int, 5>());
  2936. // }
  2937. template <typename T, T... Ints>
  2938. struct integer_sequence
  2939. {
  2940. using value_type = T;
  2941. static constexpr std::size_t size() noexcept
  2942. {
  2943. return sizeof...(Ints);
  2944. }
  2945. };
  2946. // index_sequence
  2947. //
  2948. // A helper template for an `integer_sequence` of `size_t`,
  2949. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2950. // `std::index_sequence`.
  2951. template <size_t... Ints>
  2952. using index_sequence = integer_sequence<size_t, Ints...>;
  2953. namespace utility_internal
  2954. {
  2955. template <typename Seq, size_t SeqSize, size_t Rem>
  2956. struct Extend;
  2957. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2958. template <typename T, T... Ints, size_t SeqSize>
  2959. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2960. {
  2961. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2962. };
  2963. template <typename T, T... Ints, size_t SeqSize>
  2964. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2965. {
  2966. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2967. };
  2968. // Recursion helper for 'make_integer_sequence<T, N>'.
  2969. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2970. template <typename T, size_t N>
  2971. struct Gen
  2972. {
  2973. using type =
  2974. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2975. };
  2976. template <typename T>
  2977. struct Gen<T, 0>
  2978. {
  2979. using type = integer_sequence<T>;
  2980. };
  2981. } // namespace utility_internal
  2982. // Compile-time sequences of integers
  2983. // make_integer_sequence
  2984. //
  2985. // This template alias is equivalent to
  2986. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2987. // replacement for C++14's `std::make_integer_sequence`.
  2988. template <typename T, T N>
  2989. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2990. // make_index_sequence
  2991. //
  2992. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2993. // and is designed to be a drop-in replacement for C++14's
  2994. // `std::make_index_sequence`.
  2995. template <size_t N>
  2996. using make_index_sequence = make_integer_sequence<size_t, N>;
  2997. // index_sequence_for
  2998. //
  2999. // Converts a typename pack into an index sequence of the same length, and
  3000. // is designed to be a drop-in replacement for C++14's
  3001. // `std::index_sequence_for()`
  3002. template <typename... Ts>
  3003. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  3004. //// END OF CODE FROM GOOGLE ABSEIL
  3005. #endif
  3006. // dispatch utility (taken from ranges-v3)
  3007. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  3008. template<> struct priority_tag<0> {};
  3009. // taken from ranges-v3
  3010. template<typename T>
  3011. struct static_const
  3012. {
  3013. static JSON_INLINE_VARIABLE constexpr T value{};
  3014. };
  3015. #ifndef JSON_HAS_CPP_17
  3016. template<typename T>
  3017. constexpr T static_const<T>::value;
  3018. #endif
  3019. template<typename T, typename... Args>
  3020. constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  3021. {
  3022. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  3023. }
  3024. } // namespace detail
  3025. NLOHMANN_JSON_NAMESPACE_END
  3026. // #include <nlohmann/detail/meta/type_traits.hpp>
  3027. // __ _____ _____ _____
  3028. // __| | __| | | | JSON for Modern C++
  3029. // | | |__ | | | | | | version 3.12.0
  3030. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3031. //
  3032. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  3033. // SPDX-License-Identifier: MIT
  3034. #include <limits> // numeric_limits
  3035. #include <string> // char_traits
  3036. #include <tuple> // tuple
  3037. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  3038. #include <utility> // declval
  3039. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  3040. // __ _____ _____ _____
  3041. // __| | __| | | | JSON for Modern C++
  3042. // | | |__ | | | | | | version 3.12.0
  3043. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3044. //
  3045. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  3046. // SPDX-License-Identifier: MIT
  3047. #include <iterator> // random_access_iterator_tag
  3048. // #include <nlohmann/detail/abi_macros.hpp>
  3049. // #include <nlohmann/detail/meta/void_t.hpp>
  3050. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3051. NLOHMANN_JSON_NAMESPACE_BEGIN
  3052. namespace detail
  3053. {
  3054. template<typename It, typename = void>
  3055. struct iterator_types {};
  3056. template<typename It>
  3057. struct iterator_types <
  3058. It,
  3059. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  3060. typename It::reference, typename It::iterator_category >>
  3061. {
  3062. using difference_type = typename It::difference_type;
  3063. using value_type = typename It::value_type;
  3064. using pointer = typename It::pointer;
  3065. using reference = typename It::reference;
  3066. using iterator_category = typename It::iterator_category;
  3067. };
  3068. // This is required as some compilers implement std::iterator_traits in a way that
  3069. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  3070. template<typename T, typename = void>
  3071. struct iterator_traits
  3072. {
  3073. };
  3074. template<typename T>
  3075. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  3076. : iterator_types<T>
  3077. {
  3078. };
  3079. template<typename T>
  3080. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  3081. {
  3082. using iterator_category = std::random_access_iterator_tag;
  3083. using value_type = T;
  3084. using difference_type = ptrdiff_t;
  3085. using pointer = T*;
  3086. using reference = T&;
  3087. };
  3088. } // namespace detail
  3089. NLOHMANN_JSON_NAMESPACE_END
  3090. // #include <nlohmann/detail/macro_scope.hpp>
  3091. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  3092. // __ _____ _____ _____
  3093. // __| | __| | | | JSON for Modern C++
  3094. // | | |__ | | | | | | version 3.12.0
  3095. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3096. //
  3097. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  3098. // SPDX-License-Identifier: MIT
  3099. // #include <nlohmann/detail/macro_scope.hpp>
  3100. NLOHMANN_JSON_NAMESPACE_BEGIN
  3101. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  3102. NLOHMANN_JSON_NAMESPACE_END
  3103. // #include <nlohmann/detail/meta/call_std/end.hpp>
  3104. // __ _____ _____ _____
  3105. // __| | __| | | | JSON for Modern C++
  3106. // | | |__ | | | | | | version 3.12.0
  3107. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3108. //
  3109. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  3110. // SPDX-License-Identifier: MIT
  3111. // #include <nlohmann/detail/macro_scope.hpp>
  3112. NLOHMANN_JSON_NAMESPACE_BEGIN
  3113. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  3114. NLOHMANN_JSON_NAMESPACE_END
  3115. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3116. // #include <nlohmann/detail/meta/detected.hpp>
  3117. // #include <nlohmann/json_fwd.hpp>
  3118. // __ _____ _____ _____
  3119. // __| | __| | | | JSON for Modern C++
  3120. // | | |__ | | | | | | version 3.12.0
  3121. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3122. //
  3123. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  3124. // SPDX-License-Identifier: MIT
  3125. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3126. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3127. #include <cstdint> // int64_t, uint64_t
  3128. #include <map> // map
  3129. #include <memory> // allocator
  3130. #include <string> // string
  3131. #include <vector> // vector
  3132. // #include <nlohmann/detail/abi_macros.hpp>
  3133. /*!
  3134. @brief namespace for Niels Lohmann
  3135. @see https://github.com/nlohmann
  3136. @since version 1.0.0
  3137. */
  3138. NLOHMANN_JSON_NAMESPACE_BEGIN
  3139. /*!
  3140. @brief default JSONSerializer template argument
  3141. This serializer ignores the template arguments and uses ADL
  3142. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3143. for serialization.
  3144. */
  3145. template<typename T = void, typename SFINAE = void>
  3146. struct adl_serializer;
  3147. /// a class to store JSON values
  3148. /// @sa https://json.nlohmann.me/api/basic_json/
  3149. template<template<typename U, typename V, typename... Args> class ObjectType =
  3150. std::map,
  3151. template<typename U, typename... Args> class ArrayType = std::vector,
  3152. class StringType = std::string, class BooleanType = bool,
  3153. class NumberIntegerType = std::int64_t,
  3154. class NumberUnsignedType = std::uint64_t,
  3155. class NumberFloatType = double,
  3156. template<typename U> class AllocatorType = std::allocator,
  3157. template<typename T, typename SFINAE = void> class JSONSerializer =
  3158. adl_serializer,
  3159. class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
  3160. class CustomBaseClass = void>
  3161. class basic_json;
  3162. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3163. /// @sa https://json.nlohmann.me/api/json_pointer/
  3164. template<typename RefStringType>
  3165. class json_pointer;
  3166. /*!
  3167. @brief default specialization
  3168. @sa https://json.nlohmann.me/api/json/
  3169. */
  3170. using json = basic_json<>;
  3171. /// @brief a minimal map-like container that preserves insertion order
  3172. /// @sa https://json.nlohmann.me/api/ordered_map/
  3173. template<class Key, class T, class IgnoredLess, class Allocator>
  3174. struct ordered_map;
  3175. /// @brief specialization that maintains the insertion order of object keys
  3176. /// @sa https://json.nlohmann.me/api/ordered_json/
  3177. using ordered_json = basic_json<nlohmann::ordered_map>;
  3178. NLOHMANN_JSON_NAMESPACE_END
  3179. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3180. NLOHMANN_JSON_NAMESPACE_BEGIN
  3181. /*!
  3182. @brief detail namespace with internal helper functions
  3183. This namespace collects functions that should not be exposed,
  3184. implementations of some @ref basic_json methods, and meta-programming helpers.
  3185. @since version 2.1.0
  3186. */
  3187. namespace detail
  3188. {
  3189. /////////////
  3190. // helpers //
  3191. /////////////
  3192. // Note to maintainers:
  3193. //
  3194. // Every trait in this file expects a non CV-qualified type.
  3195. // The only exceptions are in the 'aliases for detected' section
  3196. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3197. //
  3198. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3199. // (e.g. to_json(BasicJsonType&, const T&))
  3200. template<typename> struct is_basic_json : std::false_type {};
  3201. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3202. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3203. // used by exceptions create() member functions
  3204. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  3205. // false_type otherwise
  3206. template<typename BasicJsonContext>
  3207. struct is_basic_json_context :
  3208. std::integral_constant < bool,
  3209. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3210. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3211. {};
  3212. //////////////////////
  3213. // json_ref helpers //
  3214. //////////////////////
  3215. template<typename>
  3216. class json_ref;
  3217. template<typename>
  3218. struct is_json_ref : std::false_type {};
  3219. template<typename T>
  3220. struct is_json_ref<json_ref<T>> : std::true_type {};
  3221. //////////////////////////
  3222. // aliases for detected //
  3223. //////////////////////////
  3224. template<typename T>
  3225. using mapped_type_t = typename T::mapped_type;
  3226. template<typename T>
  3227. using key_type_t = typename T::key_type;
  3228. template<typename T>
  3229. using value_type_t = typename T::value_type;
  3230. template<typename T>
  3231. using difference_type_t = typename T::difference_type;
  3232. template<typename T>
  3233. using pointer_t = typename T::pointer;
  3234. template<typename T>
  3235. using reference_t = typename T::reference;
  3236. template<typename T>
  3237. using iterator_category_t = typename T::iterator_category;
  3238. template<typename T, typename... Args>
  3239. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3240. template<typename T, typename... Args>
  3241. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3242. template<typename T, typename U>
  3243. using get_template_function = decltype(std::declval<T>().template get<U>());
  3244. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3245. template<typename BasicJsonType, typename T, typename = void>
  3246. struct has_from_json : std::false_type {};
  3247. // trait checking if j.get<T> is valid
  3248. // use this trait instead of std::is_constructible or std::is_convertible,
  3249. // both rely on, or make use of implicit conversions, and thus fail when T
  3250. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3251. template <typename BasicJsonType, typename T>
  3252. struct is_getable
  3253. {
  3254. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3255. };
  3256. template<typename BasicJsonType, typename T>
  3257. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3258. {
  3259. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3260. static constexpr bool value =
  3261. is_detected_exact<void, from_json_function, serializer,
  3262. const BasicJsonType&, T&>::value;
  3263. };
  3264. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3265. // this overload is used for non-default-constructible user-defined-types
  3266. template<typename BasicJsonType, typename T, typename = void>
  3267. struct has_non_default_from_json : std::false_type {};
  3268. template<typename BasicJsonType, typename T>
  3269. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3270. {
  3271. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3272. static constexpr bool value =
  3273. is_detected_exact<T, from_json_function, serializer,
  3274. const BasicJsonType&>::value;
  3275. };
  3276. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3277. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3278. template<typename BasicJsonType, typename T, typename = void>
  3279. struct has_to_json : std::false_type {};
  3280. template<typename BasicJsonType, typename T>
  3281. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3282. {
  3283. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3284. static constexpr bool value =
  3285. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3286. T>::value;
  3287. };
  3288. template<typename T>
  3289. using detect_key_compare = typename T::key_compare;
  3290. template<typename T>
  3291. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3292. // obtains the actual object key comparator
  3293. template<typename BasicJsonType>
  3294. struct actual_object_comparator
  3295. {
  3296. using object_t = typename BasicJsonType::object_t;
  3297. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3298. using type = typename std::conditional < has_key_compare<object_t>::value,
  3299. typename object_t::key_compare, object_comparator_t>::type;
  3300. };
  3301. template<typename BasicJsonType>
  3302. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3303. /////////////////
  3304. // char_traits //
  3305. /////////////////
  3306. // Primary template of char_traits calls std char_traits
  3307. template<typename T>
  3308. struct char_traits : std::char_traits<T>
  3309. {};
  3310. // Explicitly define char traits for unsigned char since it is not standard
  3311. template<>
  3312. struct char_traits<unsigned char> : std::char_traits<char>
  3313. {
  3314. using char_type = unsigned char;
  3315. using int_type = uint64_t;
  3316. // Redefine to_int_type function
  3317. static int_type to_int_type(char_type c) noexcept
  3318. {
  3319. return static_cast<int_type>(c);
  3320. }
  3321. static char_type to_char_type(int_type i) noexcept
  3322. {
  3323. return static_cast<char_type>(i);
  3324. }
  3325. static constexpr int_type eof() noexcept
  3326. {
  3327. return static_cast<int_type>(std::char_traits<char>::eof());
  3328. }
  3329. };
  3330. // Explicitly define char traits for signed char since it is not standard
  3331. template<>
  3332. struct char_traits<signed char> : std::char_traits<char>
  3333. {
  3334. using char_type = signed char;
  3335. using int_type = uint64_t;
  3336. // Redefine to_int_type function
  3337. static int_type to_int_type(char_type c) noexcept
  3338. {
  3339. return static_cast<int_type>(c);
  3340. }
  3341. static char_type to_char_type(int_type i) noexcept
  3342. {
  3343. return static_cast<char_type>(i);
  3344. }
  3345. static constexpr int_type eof() noexcept
  3346. {
  3347. return static_cast<int_type>(std::char_traits<char>::eof());
  3348. }
  3349. };
  3350. ///////////////////
  3351. // is_ functions //
  3352. ///////////////////
  3353. // https://en.cppreference.com/w/cpp/types/conjunction
  3354. template<class...> struct conjunction : std::true_type { };
  3355. template<class B> struct conjunction<B> : B { };
  3356. template<class B, class... Bn>
  3357. struct conjunction<B, Bn...>
  3358. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3359. // https://en.cppreference.com/w/cpp/types/negation
  3360. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3361. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3362. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3363. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3364. template <typename T>
  3365. struct is_default_constructible : std::is_default_constructible<T> {};
  3366. template <typename T1, typename T2>
  3367. struct is_default_constructible<std::pair<T1, T2>>
  3368. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3369. template <typename T1, typename T2>
  3370. struct is_default_constructible<const std::pair<T1, T2>>
  3371. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3372. template <typename... Ts>
  3373. struct is_default_constructible<std::tuple<Ts...>>
  3374. : conjunction<is_default_constructible<Ts>...> {};
  3375. template <typename... Ts>
  3376. struct is_default_constructible<const std::tuple<Ts...>>
  3377. : conjunction<is_default_constructible<Ts>...> {};
  3378. template <typename T, typename... Args>
  3379. struct is_constructible : std::is_constructible<T, Args...> {};
  3380. template <typename T1, typename T2>
  3381. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3382. template <typename T1, typename T2>
  3383. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3384. template <typename... Ts>
  3385. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3386. template <typename... Ts>
  3387. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3388. template<typename T, typename = void>
  3389. struct is_iterator_traits : std::false_type {};
  3390. template<typename T>
  3391. struct is_iterator_traits<iterator_traits<T>>
  3392. {
  3393. private:
  3394. using traits = iterator_traits<T>;
  3395. public:
  3396. static constexpr auto value =
  3397. is_detected<value_type_t, traits>::value &&
  3398. is_detected<difference_type_t, traits>::value &&
  3399. is_detected<pointer_t, traits>::value &&
  3400. is_detected<iterator_category_t, traits>::value &&
  3401. is_detected<reference_t, traits>::value;
  3402. };
  3403. template<typename T>
  3404. struct is_range
  3405. {
  3406. private:
  3407. using t_ref = typename std::add_lvalue_reference<T>::type;
  3408. using iterator = detected_t<result_of_begin, t_ref>;
  3409. using sentinel = detected_t<result_of_end, t_ref>;
  3410. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3411. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3412. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3413. static constexpr auto is_iterator_begin =
  3414. is_iterator_traits<iterator_traits<iterator>>::value;
  3415. public:
  3416. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3417. };
  3418. template<typename R>
  3419. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3420. template<typename T>
  3421. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3422. // The following implementation of is_complete_type is taken from
  3423. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3424. // and is written by Xiang Fan who agreed to using it in this library.
  3425. template<typename T, typename = void>
  3426. struct is_complete_type : std::false_type {};
  3427. template<typename T>
  3428. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3429. template<typename BasicJsonType, typename CompatibleObjectType,
  3430. typename = void>
  3431. struct is_compatible_object_type_impl : std::false_type {};
  3432. template<typename BasicJsonType, typename CompatibleObjectType>
  3433. struct is_compatible_object_type_impl <
  3434. BasicJsonType, CompatibleObjectType,
  3435. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3436. is_detected<key_type_t, CompatibleObjectType>::value >>
  3437. {
  3438. using object_t = typename BasicJsonType::object_t;
  3439. // macOS's is_constructible does not play well with nonesuch...
  3440. static constexpr bool value =
  3441. is_constructible<typename object_t::key_type,
  3442. typename CompatibleObjectType::key_type>::value &&
  3443. is_constructible<typename object_t::mapped_type,
  3444. typename CompatibleObjectType::mapped_type>::value;
  3445. };
  3446. template<typename BasicJsonType, typename CompatibleObjectType>
  3447. struct is_compatible_object_type
  3448. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3449. template<typename BasicJsonType, typename ConstructibleObjectType,
  3450. typename = void>
  3451. struct is_constructible_object_type_impl : std::false_type {};
  3452. template<typename BasicJsonType, typename ConstructibleObjectType>
  3453. struct is_constructible_object_type_impl <
  3454. BasicJsonType, ConstructibleObjectType,
  3455. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3456. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3457. {
  3458. using object_t = typename BasicJsonType::object_t;
  3459. static constexpr bool value =
  3460. (is_default_constructible<ConstructibleObjectType>::value &&
  3461. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3462. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3463. (is_constructible<typename ConstructibleObjectType::key_type,
  3464. typename object_t::key_type>::value &&
  3465. std::is_same <
  3466. typename object_t::mapped_type,
  3467. typename ConstructibleObjectType::mapped_type >::value)) ||
  3468. (has_from_json<BasicJsonType,
  3469. typename ConstructibleObjectType::mapped_type>::value ||
  3470. has_non_default_from_json <
  3471. BasicJsonType,
  3472. typename ConstructibleObjectType::mapped_type >::value);
  3473. };
  3474. template<typename BasicJsonType, typename ConstructibleObjectType>
  3475. struct is_constructible_object_type
  3476. : is_constructible_object_type_impl<BasicJsonType,
  3477. ConstructibleObjectType> {};
  3478. template<typename BasicJsonType, typename CompatibleStringType>
  3479. struct is_compatible_string_type
  3480. {
  3481. static constexpr auto value =
  3482. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3483. };
  3484. template<typename BasicJsonType, typename ConstructibleStringType>
  3485. struct is_constructible_string_type
  3486. {
  3487. // launder type through decltype() to fix compilation failure on ICPC
  3488. #ifdef __INTEL_COMPILER
  3489. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3490. #else
  3491. using laundered_type = ConstructibleStringType;
  3492. #endif
  3493. static constexpr auto value =
  3494. conjunction <
  3495. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3496. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3497. value_type_t, laundered_type >>::value;
  3498. };
  3499. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3500. struct is_compatible_array_type_impl : std::false_type {};
  3501. template<typename BasicJsonType, typename CompatibleArrayType>
  3502. struct is_compatible_array_type_impl <
  3503. BasicJsonType, CompatibleArrayType,
  3504. enable_if_t <
  3505. is_detected<iterator_t, CompatibleArrayType>::value&&
  3506. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3507. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3508. // c.f. https://github.com/nlohmann/json/pull/3073
  3509. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3510. {
  3511. static constexpr bool value =
  3512. is_constructible<BasicJsonType,
  3513. range_value_t<CompatibleArrayType>>::value;
  3514. };
  3515. template<typename BasicJsonType, typename CompatibleArrayType>
  3516. struct is_compatible_array_type
  3517. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3518. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3519. struct is_constructible_array_type_impl : std::false_type {};
  3520. template<typename BasicJsonType, typename ConstructibleArrayType>
  3521. struct is_constructible_array_type_impl <
  3522. BasicJsonType, ConstructibleArrayType,
  3523. enable_if_t<std::is_same<ConstructibleArrayType,
  3524. typename BasicJsonType::value_type>::value >>
  3525. : std::true_type {};
  3526. template<typename BasicJsonType, typename ConstructibleArrayType>
  3527. struct is_constructible_array_type_impl <
  3528. BasicJsonType, ConstructibleArrayType,
  3529. enable_if_t < !std::is_same<ConstructibleArrayType,
  3530. typename BasicJsonType::value_type>::value&&
  3531. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3532. is_default_constructible<ConstructibleArrayType>::value&&
  3533. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3534. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3535. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3536. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3537. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3538. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3539. // c.f. https://github.com/nlohmann/json/pull/3073
  3540. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3541. is_complete_type <
  3542. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3543. {
  3544. using value_type = range_value_t<ConstructibleArrayType>;
  3545. static constexpr bool value =
  3546. std::is_same<value_type,
  3547. typename BasicJsonType::array_t::value_type>::value ||
  3548. has_from_json<BasicJsonType,
  3549. value_type>::value ||
  3550. has_non_default_from_json <
  3551. BasicJsonType,
  3552. value_type >::value;
  3553. };
  3554. template<typename BasicJsonType, typename ConstructibleArrayType>
  3555. struct is_constructible_array_type
  3556. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3557. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3558. typename = void>
  3559. struct is_compatible_integer_type_impl : std::false_type {};
  3560. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3561. struct is_compatible_integer_type_impl <
  3562. RealIntegerType, CompatibleNumberIntegerType,
  3563. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3564. std::is_integral<CompatibleNumberIntegerType>::value&&
  3565. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3566. {
  3567. // is there an assert somewhere on overflows?
  3568. using RealLimits = std::numeric_limits<RealIntegerType>;
  3569. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3570. static constexpr auto value =
  3571. is_constructible<RealIntegerType,
  3572. CompatibleNumberIntegerType>::value &&
  3573. CompatibleLimits::is_integer &&
  3574. RealLimits::is_signed == CompatibleLimits::is_signed;
  3575. };
  3576. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3577. struct is_compatible_integer_type
  3578. : is_compatible_integer_type_impl<RealIntegerType,
  3579. CompatibleNumberIntegerType> {};
  3580. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3581. struct is_compatible_type_impl: std::false_type {};
  3582. template<typename BasicJsonType, typename CompatibleType>
  3583. struct is_compatible_type_impl <
  3584. BasicJsonType, CompatibleType,
  3585. enable_if_t<is_complete_type<CompatibleType>::value >>
  3586. {
  3587. static constexpr bool value =
  3588. has_to_json<BasicJsonType, CompatibleType>::value;
  3589. };
  3590. template<typename BasicJsonType, typename CompatibleType>
  3591. struct is_compatible_type
  3592. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3593. template<typename T1, typename T2>
  3594. struct is_constructible_tuple : std::false_type {};
  3595. template<typename T1, typename... Args>
  3596. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3597. template<typename BasicJsonType, typename T>
  3598. struct is_json_iterator_of : std::false_type {};
  3599. template<typename BasicJsonType>
  3600. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3601. template<typename BasicJsonType>
  3602. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3603. {};
  3604. // checks if a given type T is a template specialization of Primary
  3605. template<template <typename...> class Primary, typename T>
  3606. struct is_specialization_of : std::false_type {};
  3607. template<template <typename...> class Primary, typename... Args>
  3608. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3609. template<typename T>
  3610. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3611. // checks if A and B are comparable using Compare functor
  3612. template<typename Compare, typename A, typename B, typename = void>
  3613. struct is_comparable : std::false_type {};
  3614. template<typename Compare, typename A, typename B>
  3615. struct is_comparable<Compare, A, B, void_t<
  3616. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3617. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3618. >> : std::true_type {};
  3619. template<typename T>
  3620. using detect_is_transparent = typename T::is_transparent;
  3621. // type trait to check if KeyType can be used as object key (without a BasicJsonType)
  3622. // see is_usable_as_basic_json_key_type below
  3623. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3624. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3625. using is_usable_as_key_type = typename std::conditional <
  3626. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3627. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3628. ObjectKeyType>::value)
  3629. && (!RequireTransparentComparator
  3630. || is_detected <detect_is_transparent, Comparator>::value)
  3631. && !is_json_pointer<KeyType>::value,
  3632. std::true_type,
  3633. std::false_type >::type;
  3634. // type trait to check if KeyType can be used as object key
  3635. // true if:
  3636. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3637. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3638. // - the comparator is transparent or RequireTransparentComparator is false
  3639. // - KeyType is not a JSON iterator or json_pointer
  3640. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3641. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3642. using is_usable_as_basic_json_key_type = typename std::conditional <
  3643. is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3644. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3645. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3646. && !is_json_iterator_of<BasicJsonType, KeyType>::value,
  3647. std::true_type,
  3648. std::false_type >::type;
  3649. template<typename ObjectType, typename KeyType>
  3650. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3651. // type trait to check if object_t has an erase() member functions accepting KeyType
  3652. template<typename BasicJsonType, typename KeyType>
  3653. using has_erase_with_key_type = typename std::conditional <
  3654. is_detected <
  3655. detect_erase_with_key_type,
  3656. typename BasicJsonType::object_t, KeyType >::value,
  3657. std::true_type,
  3658. std::false_type >::type;
  3659. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3660. // ordered_map inherits capacity() from std::vector)
  3661. template <typename T>
  3662. struct is_ordered_map
  3663. {
  3664. using one = char;
  3665. struct two
  3666. {
  3667. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3668. };
  3669. template <typename C> static one test( decltype(&C::capacity) ) ;
  3670. template <typename C> static two test(...);
  3671. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3672. };
  3673. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3674. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3675. T conditional_static_cast(U value)
  3676. {
  3677. return static_cast<T>(value);
  3678. }
  3679. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3680. T conditional_static_cast(U value)
  3681. {
  3682. return value;
  3683. }
  3684. template<typename... Types>
  3685. using all_integral = conjunction<std::is_integral<Types>...>;
  3686. template<typename... Types>
  3687. using all_signed = conjunction<std::is_signed<Types>...>;
  3688. template<typename... Types>
  3689. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3690. // there's a disjunction trait in another PR; replace when merged
  3691. template<typename... Types>
  3692. using same_sign = std::integral_constant < bool,
  3693. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3694. template<typename OfType, typename T>
  3695. using never_out_of_range = std::integral_constant < bool,
  3696. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3697. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3698. template<typename OfType, typename T,
  3699. bool OfTypeSigned = std::is_signed<OfType>::value,
  3700. bool TSigned = std::is_signed<T>::value>
  3701. struct value_in_range_of_impl2;
  3702. template<typename OfType, typename T>
  3703. struct value_in_range_of_impl2<OfType, T, false, false>
  3704. {
  3705. static constexpr bool test(T val)
  3706. {
  3707. using CommonType = typename std::common_type<OfType, T>::type;
  3708. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3709. }
  3710. };
  3711. template<typename OfType, typename T>
  3712. struct value_in_range_of_impl2<OfType, T, true, false>
  3713. {
  3714. static constexpr bool test(T val)
  3715. {
  3716. using CommonType = typename std::common_type<OfType, T>::type;
  3717. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3718. }
  3719. };
  3720. template<typename OfType, typename T>
  3721. struct value_in_range_of_impl2<OfType, T, false, true>
  3722. {
  3723. static constexpr bool test(T val)
  3724. {
  3725. using CommonType = typename std::common_type<OfType, T>::type;
  3726. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3727. }
  3728. };
  3729. template<typename OfType, typename T>
  3730. struct value_in_range_of_impl2<OfType, T, true, true>
  3731. {
  3732. static constexpr bool test(T val)
  3733. {
  3734. using CommonType = typename std::common_type<OfType, T>::type;
  3735. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3736. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3737. }
  3738. };
  3739. template<typename OfType, typename T,
  3740. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3741. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3742. struct value_in_range_of_impl1;
  3743. template<typename OfType, typename T>
  3744. struct value_in_range_of_impl1<OfType, T, false>
  3745. {
  3746. static constexpr bool test(T val)
  3747. {
  3748. return value_in_range_of_impl2<OfType, T>::test(val);
  3749. }
  3750. };
  3751. template<typename OfType, typename T>
  3752. struct value_in_range_of_impl1<OfType, T, true>
  3753. {
  3754. static constexpr bool test(T /*val*/)
  3755. {
  3756. return true;
  3757. }
  3758. };
  3759. template<typename OfType, typename T>
  3760. constexpr bool value_in_range_of(T val)
  3761. {
  3762. return value_in_range_of_impl1<OfType, T>::test(val);
  3763. }
  3764. template<bool Value>
  3765. using bool_constant = std::integral_constant<bool, Value>;
  3766. ///////////////////////////////////////////////////////////////////////////////
  3767. // is_c_string
  3768. ///////////////////////////////////////////////////////////////////////////////
  3769. namespace impl
  3770. {
  3771. template<typename T>
  3772. constexpr bool is_c_string()
  3773. {
  3774. using TUnExt = typename std::remove_extent<T>::type;
  3775. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3776. using TUnPtr = typename std::remove_pointer<T>::type;
  3777. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3778. return
  3779. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3780. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3781. }
  3782. } // namespace impl
  3783. // checks whether T is a [cv] char */[cv] char[] C string
  3784. template<typename T>
  3785. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3786. template<typename T>
  3787. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3788. ///////////////////////////////////////////////////////////////////////////////
  3789. // is_transparent
  3790. ///////////////////////////////////////////////////////////////////////////////
  3791. namespace impl
  3792. {
  3793. template<typename T>
  3794. constexpr bool is_transparent()
  3795. {
  3796. return is_detected<detect_is_transparent, T>::value;
  3797. }
  3798. } // namespace impl
  3799. // checks whether T has a member named is_transparent
  3800. template<typename T>
  3801. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3802. ///////////////////////////////////////////////////////////////////////////////
  3803. } // namespace detail
  3804. NLOHMANN_JSON_NAMESPACE_END
  3805. // #include <nlohmann/detail/string_concat.hpp>
  3806. // __ _____ _____ _____
  3807. // __| | __| | | | JSON for Modern C++
  3808. // | | |__ | | | | | | version 3.12.0
  3809. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3810. //
  3811. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  3812. // SPDX-License-Identifier: MIT
  3813. #include <cstring> // strlen
  3814. #include <string> // string
  3815. #include <utility> // forward
  3816. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3817. // #include <nlohmann/detail/meta/detected.hpp>
  3818. NLOHMANN_JSON_NAMESPACE_BEGIN
  3819. namespace detail
  3820. {
  3821. inline std::size_t concat_length()
  3822. {
  3823. return 0;
  3824. }
  3825. template<typename... Args>
  3826. inline std::size_t concat_length(const char* cstr, const Args& ... rest);
  3827. template<typename StringType, typename... Args>
  3828. inline std::size_t concat_length(const StringType& str, const Args& ... rest);
  3829. template<typename... Args>
  3830. inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
  3831. {
  3832. return 1 + concat_length(rest...);
  3833. }
  3834. template<typename... Args>
  3835. inline std::size_t concat_length(const char* cstr, const Args& ... rest)
  3836. {
  3837. // cppcheck-suppress ignoredReturnValue
  3838. return ::strlen(cstr) + concat_length(rest...);
  3839. }
  3840. template<typename StringType, typename... Args>
  3841. inline std::size_t concat_length(const StringType& str, const Args& ... rest)
  3842. {
  3843. return str.size() + concat_length(rest...);
  3844. }
  3845. template<typename OutStringType>
  3846. inline void concat_into(OutStringType& /*out*/)
  3847. {}
  3848. template<typename StringType, typename Arg>
  3849. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3850. template<typename StringType, typename Arg>
  3851. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3852. template<typename StringType, typename Arg>
  3853. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3854. template<typename StringType, typename Arg>
  3855. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3856. template<typename StringType, typename Arg>
  3857. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3858. template<typename StringType, typename Arg>
  3859. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3860. template<typename StringType, typename Arg>
  3861. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3862. template<typename StringType, typename Arg>
  3863. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3864. template < typename OutStringType, typename Arg, typename... Args,
  3865. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3866. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3867. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3868. template < typename OutStringType, typename Arg, typename... Args,
  3869. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3870. && !detect_string_can_append_op<OutStringType, Arg>::value
  3871. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3872. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3873. template < typename OutStringType, typename Arg, typename... Args,
  3874. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3875. && !detect_string_can_append_op<OutStringType, Arg>::value
  3876. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3877. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3878. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3879. template<typename OutStringType, typename Arg, typename... Args,
  3880. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3881. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3882. {
  3883. out.append(std::forward<Arg>(arg));
  3884. concat_into(out, std::forward<Args>(rest)...);
  3885. }
  3886. template < typename OutStringType, typename Arg, typename... Args,
  3887. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3888. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3889. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3890. {
  3891. out += std::forward<Arg>(arg);
  3892. concat_into(out, std::forward<Args>(rest)...);
  3893. }
  3894. template < typename OutStringType, typename Arg, typename... Args,
  3895. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3896. && !detect_string_can_append_op<OutStringType, Arg>::value
  3897. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3898. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3899. {
  3900. out.append(arg.begin(), arg.end());
  3901. concat_into(out, std::forward<Args>(rest)...);
  3902. }
  3903. template < typename OutStringType, typename Arg, typename... Args,
  3904. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3905. && !detect_string_can_append_op<OutStringType, Arg>::value
  3906. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3907. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3908. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3909. {
  3910. out.append(arg.data(), arg.size());
  3911. concat_into(out, std::forward<Args>(rest)...);
  3912. }
  3913. template<typename OutStringType = std::string, typename... Args>
  3914. inline OutStringType concat(Args && ... args)
  3915. {
  3916. OutStringType str;
  3917. str.reserve(concat_length(args...));
  3918. concat_into(str, std::forward<Args>(args)...);
  3919. return str;
  3920. }
  3921. } // namespace detail
  3922. NLOHMANN_JSON_NAMESPACE_END
  3923. // With -Wweak-vtables, Clang will complain about the exception classes as they
  3924. // have no out-of-line virtual method definitions and their vtable will be
  3925. // emitted in every translation unit. This issue cannot be fixed with a
  3926. // header-only library as there is no implementation file to move these
  3927. // functions to. As a result, we suppress this warning here to avoid client
  3928. // code to stumble over this. See https://github.com/nlohmann/json/issues/4087
  3929. // for a discussion.
  3930. #if defined(__clang__)
  3931. #pragma clang diagnostic push
  3932. #pragma clang diagnostic ignored "-Wweak-vtables"
  3933. #endif
  3934. NLOHMANN_JSON_NAMESPACE_BEGIN
  3935. namespace detail
  3936. {
  3937. ////////////////
  3938. // exceptions //
  3939. ////////////////
  3940. /// @brief general exception of the @ref basic_json class
  3941. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3942. class exception : public std::exception
  3943. {
  3944. public:
  3945. /// returns the explanatory string
  3946. const char* what() const noexcept override
  3947. {
  3948. return m.what();
  3949. }
  3950. /// the id of the exception
  3951. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3952. protected:
  3953. JSON_HEDLEY_NON_NULL(3)
  3954. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3955. static std::string name(const std::string& ename, int id_)
  3956. {
  3957. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3958. }
  3959. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3960. {
  3961. return "";
  3962. }
  3963. template<typename BasicJsonType>
  3964. static std::string diagnostics(const BasicJsonType* leaf_element)
  3965. {
  3966. #if JSON_DIAGNOSTICS
  3967. std::vector<std::string> tokens;
  3968. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3969. {
  3970. switch (current->m_parent->type())
  3971. {
  3972. case value_t::array:
  3973. {
  3974. for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
  3975. {
  3976. if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
  3977. {
  3978. tokens.emplace_back(std::to_string(i));
  3979. break;
  3980. }
  3981. }
  3982. break;
  3983. }
  3984. case value_t::object:
  3985. {
  3986. for (const auto& element : *current->m_parent->m_data.m_value.object)
  3987. {
  3988. if (&element.second == current)
  3989. {
  3990. tokens.emplace_back(element.first.c_str());
  3991. break;
  3992. }
  3993. }
  3994. break;
  3995. }
  3996. case value_t::null: // LCOV_EXCL_LINE
  3997. case value_t::string: // LCOV_EXCL_LINE
  3998. case value_t::boolean: // LCOV_EXCL_LINE
  3999. case value_t::number_integer: // LCOV_EXCL_LINE
  4000. case value_t::number_unsigned: // LCOV_EXCL_LINE
  4001. case value_t::number_float: // LCOV_EXCL_LINE
  4002. case value_t::binary: // LCOV_EXCL_LINE
  4003. case value_t::discarded: // LCOV_EXCL_LINE
  4004. default: // LCOV_EXCL_LINE
  4005. break; // LCOV_EXCL_LINE
  4006. }
  4007. }
  4008. if (tokens.empty())
  4009. {
  4010. return "";
  4011. }
  4012. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  4013. [](const std::string & a, const std::string & b)
  4014. {
  4015. return concat(a, '/', detail::escape(b));
  4016. });
  4017. return concat('(', str, ") ", get_byte_positions(leaf_element));
  4018. #else
  4019. return get_byte_positions(leaf_element);
  4020. #endif
  4021. }
  4022. private:
  4023. /// an exception object as storage for error messages
  4024. std::runtime_error m;
  4025. #if JSON_DIAGNOSTIC_POSITIONS
  4026. template<typename BasicJsonType>
  4027. static std::string get_byte_positions(const BasicJsonType* leaf_element)
  4028. {
  4029. if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
  4030. {
  4031. return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
  4032. }
  4033. return "";
  4034. }
  4035. #else
  4036. template<typename BasicJsonType>
  4037. static std::string get_byte_positions(const BasicJsonType* leaf_element)
  4038. {
  4039. static_cast<void>(leaf_element);
  4040. return "";
  4041. }
  4042. #endif
  4043. };
  4044. /// @brief exception indicating a parse error
  4045. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  4046. class parse_error : public exception
  4047. {
  4048. public:
  4049. /*!
  4050. @brief create a parse error exception
  4051. @param[in] id_ the id of the exception
  4052. @param[in] pos the position where the error occurred (or with
  4053. chars_read_total=0 if the position cannot be
  4054. determined)
  4055. @param[in] what_arg the explanatory string
  4056. @return parse_error object
  4057. */
  4058. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4059. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  4060. {
  4061. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  4062. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  4063. return {id_, pos.chars_read_total, w.c_str()};
  4064. }
  4065. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4066. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  4067. {
  4068. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  4069. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  4070. ": ", exception::diagnostics(context), what_arg);
  4071. return {id_, byte_, w.c_str()};
  4072. }
  4073. /*!
  4074. @brief byte index of the parse error
  4075. The byte index of the last read character in the input file.
  4076. @note For an input with n bytes, 1 is the index of the first character and
  4077. n+1 is the index of the terminating null byte or the end of file.
  4078. This also holds true when reading a byte vector (CBOR or MessagePack).
  4079. */
  4080. const std::size_t byte;
  4081. private:
  4082. parse_error(int id_, std::size_t byte_, const char* what_arg)
  4083. : exception(id_, what_arg), byte(byte_) {}
  4084. static std::string position_string(const position_t& pos)
  4085. {
  4086. return concat(" at line ", std::to_string(pos.lines_read + 1),
  4087. ", column ", std::to_string(pos.chars_read_current_line));
  4088. }
  4089. };
  4090. /// @brief exception indicating errors with iterators
  4091. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  4092. class invalid_iterator : public exception
  4093. {
  4094. public:
  4095. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4096. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  4097. {
  4098. const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  4099. return {id_, w.c_str()};
  4100. }
  4101. private:
  4102. JSON_HEDLEY_NON_NULL(3)
  4103. invalid_iterator(int id_, const char* what_arg)
  4104. : exception(id_, what_arg) {}
  4105. };
  4106. /// @brief exception indicating executing a member function with a wrong type
  4107. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  4108. class type_error : public exception
  4109. {
  4110. public:
  4111. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4112. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  4113. {
  4114. const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  4115. return {id_, w.c_str()};
  4116. }
  4117. private:
  4118. JSON_HEDLEY_NON_NULL(3)
  4119. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4120. };
  4121. /// @brief exception indicating access out of the defined range
  4122. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  4123. class out_of_range : public exception
  4124. {
  4125. public:
  4126. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4127. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  4128. {
  4129. const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  4130. return {id_, w.c_str()};
  4131. }
  4132. private:
  4133. JSON_HEDLEY_NON_NULL(3)
  4134. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4135. };
  4136. /// @brief exception indicating other library errors
  4137. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  4138. class other_error : public exception
  4139. {
  4140. public:
  4141. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4142. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  4143. {
  4144. const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  4145. return {id_, w.c_str()};
  4146. }
  4147. private:
  4148. JSON_HEDLEY_NON_NULL(3)
  4149. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4150. };
  4151. } // namespace detail
  4152. NLOHMANN_JSON_NAMESPACE_END
  4153. #if defined(__clang__)
  4154. #pragma clang diagnostic pop
  4155. #endif
  4156. // #include <nlohmann/detail/macro_scope.hpp>
  4157. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4158. // #include <nlohmann/detail/meta/identity_tag.hpp>
  4159. // __ _____ _____ _____
  4160. // __| | __| | | | JSON for Modern C++
  4161. // | | |__ | | | | | | version 3.12.0
  4162. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4163. //
  4164. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  4165. // SPDX-License-Identifier: MIT
  4166. // #include <nlohmann/detail/abi_macros.hpp>
  4167. NLOHMANN_JSON_NAMESPACE_BEGIN
  4168. namespace detail
  4169. {
  4170. // dispatching helper struct
  4171. template <class T> struct identity_tag {};
  4172. } // namespace detail
  4173. NLOHMANN_JSON_NAMESPACE_END
  4174. // #include <nlohmann/detail/meta/std_fs.hpp>
  4175. // __ _____ _____ _____
  4176. // __| | __| | | | JSON for Modern C++
  4177. // | | |__ | | | | | | version 3.12.0
  4178. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4179. //
  4180. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  4181. // SPDX-License-Identifier: MIT
  4182. // #include <nlohmann/detail/macro_scope.hpp>
  4183. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4184. #include <experimental/filesystem>
  4185. NLOHMANN_JSON_NAMESPACE_BEGIN
  4186. namespace detail
  4187. {
  4188. namespace std_fs = std::experimental::filesystem;
  4189. } // namespace detail
  4190. NLOHMANN_JSON_NAMESPACE_END
  4191. #elif JSON_HAS_FILESYSTEM
  4192. #include <filesystem> // NOLINT(build/c++17)
  4193. NLOHMANN_JSON_NAMESPACE_BEGIN
  4194. namespace detail
  4195. {
  4196. namespace std_fs = std::filesystem;
  4197. } // namespace detail
  4198. NLOHMANN_JSON_NAMESPACE_END
  4199. #endif
  4200. // #include <nlohmann/detail/meta/type_traits.hpp>
  4201. // #include <nlohmann/detail/string_concat.hpp>
  4202. // #include <nlohmann/detail/value_t.hpp>
  4203. NLOHMANN_JSON_NAMESPACE_BEGIN
  4204. namespace detail
  4205. {
  4206. template<typename BasicJsonType>
  4207. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  4208. {
  4209. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  4210. {
  4211. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  4212. }
  4213. n = nullptr;
  4214. }
  4215. #ifdef JSON_HAS_CPP_17
  4216. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  4217. template<typename BasicJsonType, typename T>
  4218. void from_json(const BasicJsonType& j, std::optional<T>& opt)
  4219. {
  4220. if (j.is_null())
  4221. {
  4222. opt = std::nullopt;
  4223. }
  4224. else
  4225. {
  4226. opt.emplace(j.template get<T>());
  4227. }
  4228. }
  4229. #endif // JSON_USE_IMPLICIT_CONVERSIONS
  4230. #endif // JSON_HAS_CPP_17
  4231. // overloads for basic_json template parameters
  4232. template < typename BasicJsonType, typename ArithmeticType,
  4233. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4234. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4235. int > = 0 >
  4236. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4237. {
  4238. switch (static_cast<value_t>(j))
  4239. {
  4240. case value_t::number_unsigned:
  4241. {
  4242. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4243. break;
  4244. }
  4245. case value_t::number_integer:
  4246. {
  4247. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4248. break;
  4249. }
  4250. case value_t::number_float:
  4251. {
  4252. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4253. break;
  4254. }
  4255. case value_t::null:
  4256. case value_t::object:
  4257. case value_t::array:
  4258. case value_t::string:
  4259. case value_t::boolean:
  4260. case value_t::binary:
  4261. case value_t::discarded:
  4262. default:
  4263. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4264. }
  4265. }
  4266. template<typename BasicJsonType>
  4267. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4268. {
  4269. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4270. {
  4271. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4272. }
  4273. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4274. }
  4275. template<typename BasicJsonType>
  4276. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4277. {
  4278. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4279. {
  4280. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4281. }
  4282. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4283. }
  4284. template <
  4285. typename BasicJsonType, typename StringType,
  4286. enable_if_t <
  4287. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4288. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4289. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4290. && !is_json_ref<StringType>::value, int > = 0 >
  4291. inline void from_json(const BasicJsonType& j, StringType& s)
  4292. {
  4293. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4294. {
  4295. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4296. }
  4297. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4298. }
  4299. template<typename BasicJsonType>
  4300. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4301. {
  4302. get_arithmetic_value(j, val);
  4303. }
  4304. template<typename BasicJsonType>
  4305. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4306. {
  4307. get_arithmetic_value(j, val);
  4308. }
  4309. template<typename BasicJsonType>
  4310. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4311. {
  4312. get_arithmetic_value(j, val);
  4313. }
  4314. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4315. template<typename BasicJsonType, typename EnumType,
  4316. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4317. inline void from_json(const BasicJsonType& j, EnumType& e)
  4318. {
  4319. typename std::underlying_type<EnumType>::type val;
  4320. get_arithmetic_value(j, val);
  4321. e = static_cast<EnumType>(val);
  4322. }
  4323. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4324. // forward_list doesn't have an insert method
  4325. template<typename BasicJsonType, typename T, typename Allocator,
  4326. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4327. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4328. {
  4329. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4330. {
  4331. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4332. }
  4333. l.clear();
  4334. std::transform(j.rbegin(), j.rend(),
  4335. std::front_inserter(l), [](const BasicJsonType & i)
  4336. {
  4337. return i.template get<T>();
  4338. });
  4339. }
  4340. // valarray doesn't have an insert method
  4341. template<typename BasicJsonType, typename T,
  4342. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4343. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4344. {
  4345. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4346. {
  4347. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4348. }
  4349. l.resize(j.size());
  4350. std::transform(j.begin(), j.end(), std::begin(l),
  4351. [](const BasicJsonType & elem)
  4352. {
  4353. return elem.template get<T>();
  4354. });
  4355. }
  4356. template<typename BasicJsonType, typename T, std::size_t N>
  4357. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4358. -> decltype(j.template get<T>(), void())
  4359. {
  4360. for (std::size_t i = 0; i < N; ++i)
  4361. {
  4362. arr[i] = j.at(i).template get<T>();
  4363. }
  4364. }
  4365. template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
  4366. auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4367. -> decltype(j.template get<T>(), void())
  4368. {
  4369. for (std::size_t i1 = 0; i1 < N1; ++i1)
  4370. {
  4371. for (std::size_t i2 = 0; i2 < N2; ++i2)
  4372. {
  4373. arr[i1][i2] = j.at(i1).at(i2).template get<T>();
  4374. }
  4375. }
  4376. }
  4377. template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
  4378. auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4379. -> decltype(j.template get<T>(), void())
  4380. {
  4381. for (std::size_t i1 = 0; i1 < N1; ++i1)
  4382. {
  4383. for (std::size_t i2 = 0; i2 < N2; ++i2)
  4384. {
  4385. for (std::size_t i3 = 0; i3 < N3; ++i3)
  4386. {
  4387. arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
  4388. }
  4389. }
  4390. }
  4391. }
  4392. template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
  4393. auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3][N4]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4394. -> decltype(j.template get<T>(), void())
  4395. {
  4396. for (std::size_t i1 = 0; i1 < N1; ++i1)
  4397. {
  4398. for (std::size_t i2 = 0; i2 < N2; ++i2)
  4399. {
  4400. for (std::size_t i3 = 0; i3 < N3; ++i3)
  4401. {
  4402. for (std::size_t i4 = 0; i4 < N4; ++i4)
  4403. {
  4404. arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
  4405. }
  4406. }
  4407. }
  4408. }
  4409. }
  4410. template<typename BasicJsonType>
  4411. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4412. {
  4413. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4414. }
  4415. template<typename BasicJsonType, typename T, std::size_t N>
  4416. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4417. priority_tag<2> /*unused*/)
  4418. -> decltype(j.template get<T>(), void())
  4419. {
  4420. for (std::size_t i = 0; i < N; ++i)
  4421. {
  4422. arr[i] = j.at(i).template get<T>();
  4423. }
  4424. }
  4425. template<typename BasicJsonType, typename ConstructibleArrayType,
  4426. enable_if_t<
  4427. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4428. int> = 0>
  4429. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4430. -> decltype(
  4431. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4432. j.template get<typename ConstructibleArrayType::value_type>(),
  4433. void())
  4434. {
  4435. using std::end;
  4436. ConstructibleArrayType ret;
  4437. ret.reserve(j.size());
  4438. std::transform(j.begin(), j.end(),
  4439. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4440. {
  4441. // get<BasicJsonType>() returns *this, this won't call a from_json
  4442. // method when value_type is BasicJsonType
  4443. return i.template get<typename ConstructibleArrayType::value_type>();
  4444. });
  4445. arr = std::move(ret);
  4446. }
  4447. template<typename BasicJsonType, typename ConstructibleArrayType,
  4448. enable_if_t<
  4449. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4450. int> = 0>
  4451. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4452. priority_tag<0> /*unused*/)
  4453. {
  4454. using std::end;
  4455. ConstructibleArrayType ret;
  4456. std::transform(
  4457. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4458. [](const BasicJsonType & i)
  4459. {
  4460. // get<BasicJsonType>() returns *this, this won't call a from_json
  4461. // method when value_type is BasicJsonType
  4462. return i.template get<typename ConstructibleArrayType::value_type>();
  4463. });
  4464. arr = std::move(ret);
  4465. }
  4466. template < typename BasicJsonType, typename ConstructibleArrayType,
  4467. enable_if_t <
  4468. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4469. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4470. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4471. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4472. !is_basic_json<ConstructibleArrayType>::value,
  4473. int > = 0 >
  4474. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4475. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4476. j.template get<typename ConstructibleArrayType::value_type>(),
  4477. void())
  4478. {
  4479. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4480. {
  4481. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4482. }
  4483. from_json_array_impl(j, arr, priority_tag<3> {});
  4484. }
  4485. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4486. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4487. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4488. {
  4489. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4490. }
  4491. template < typename BasicJsonType, typename T, std::size_t N >
  4492. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4493. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4494. {
  4495. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4496. {
  4497. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4498. }
  4499. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4500. }
  4501. template<typename BasicJsonType>
  4502. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4503. {
  4504. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4505. {
  4506. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4507. }
  4508. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4509. }
  4510. template<typename BasicJsonType, typename ConstructibleObjectType,
  4511. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4512. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4513. {
  4514. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4515. {
  4516. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4517. }
  4518. ConstructibleObjectType ret;
  4519. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4520. using value_type = typename ConstructibleObjectType::value_type;
  4521. std::transform(
  4522. inner_object->begin(), inner_object->end(),
  4523. std::inserter(ret, ret.begin()),
  4524. [](typename BasicJsonType::object_t::value_type const & p)
  4525. {
  4526. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4527. });
  4528. obj = std::move(ret);
  4529. }
  4530. // overload for arithmetic types, not chosen for basic_json template arguments
  4531. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4532. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4533. // an arithmetic type?
  4534. template < typename BasicJsonType, typename ArithmeticType,
  4535. enable_if_t <
  4536. std::is_arithmetic<ArithmeticType>::value&&
  4537. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4538. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4539. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4540. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4541. int > = 0 >
  4542. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4543. {
  4544. switch (static_cast<value_t>(j))
  4545. {
  4546. case value_t::number_unsigned:
  4547. {
  4548. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4549. break;
  4550. }
  4551. case value_t::number_integer:
  4552. {
  4553. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4554. break;
  4555. }
  4556. case value_t::number_float:
  4557. {
  4558. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4559. break;
  4560. }
  4561. case value_t::boolean:
  4562. {
  4563. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4564. break;
  4565. }
  4566. case value_t::null:
  4567. case value_t::object:
  4568. case value_t::array:
  4569. case value_t::string:
  4570. case value_t::binary:
  4571. case value_t::discarded:
  4572. default:
  4573. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4574. }
  4575. }
  4576. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4577. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4578. {
  4579. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4580. }
  4581. template<typename BasicJsonType>
  4582. std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/)
  4583. {
  4584. return {};
  4585. }
  4586. template < typename BasicJsonType, class A1, class A2 >
  4587. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4588. {
  4589. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4590. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4591. }
  4592. template<typename BasicJsonType, typename A1, typename A2>
  4593. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4594. {
  4595. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4596. }
  4597. template<typename BasicJsonType, typename... Args>
  4598. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4599. {
  4600. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4601. }
  4602. template<typename BasicJsonType, typename... Args>
  4603. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4604. {
  4605. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4606. }
  4607. template<typename BasicJsonType, typename TupleRelated>
  4608. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4609. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4610. {
  4611. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4612. {
  4613. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4614. }
  4615. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4616. }
  4617. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4618. typename = enable_if_t < !std::is_constructible <
  4619. typename BasicJsonType::string_t, Key >::value >>
  4620. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  4621. {
  4622. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4623. {
  4624. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4625. }
  4626. m.clear();
  4627. for (const auto& p : j)
  4628. {
  4629. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4630. {
  4631. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4632. }
  4633. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4634. }
  4635. }
  4636. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4637. typename = enable_if_t < !std::is_constructible <
  4638. typename BasicJsonType::string_t, Key >::value >>
  4639. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4640. {
  4641. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4642. {
  4643. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4644. }
  4645. m.clear();
  4646. for (const auto& p : j)
  4647. {
  4648. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4649. {
  4650. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4651. }
  4652. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4653. }
  4654. }
  4655. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4656. template<typename BasicJsonType>
  4657. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4658. {
  4659. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4660. {
  4661. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4662. }
  4663. const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4664. #ifdef JSON_HAS_CPP_20
  4665. p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
  4666. #else
  4667. p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
  4668. #endif
  4669. }
  4670. #endif
  4671. struct from_json_fn
  4672. {
  4673. template<typename BasicJsonType, typename T>
  4674. auto operator()(const BasicJsonType& j, T&& val) const
  4675. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4676. -> decltype(from_json(j, std::forward<T>(val)))
  4677. {
  4678. return from_json(j, std::forward<T>(val));
  4679. }
  4680. };
  4681. } // namespace detail
  4682. #ifndef JSON_HAS_CPP_17
  4683. /// namespace to hold default `from_json` function
  4684. /// to see why this is required:
  4685. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4686. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4687. {
  4688. #endif
  4689. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4690. detail::static_const<detail::from_json_fn>::value;
  4691. #ifndef JSON_HAS_CPP_17
  4692. } // namespace
  4693. #endif
  4694. NLOHMANN_JSON_NAMESPACE_END
  4695. // #include <nlohmann/detail/conversions/to_json.hpp>
  4696. // __ _____ _____ _____
  4697. // __| | __| | | | JSON for Modern C++
  4698. // | | |__ | | | | | | version 3.12.0
  4699. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4700. //
  4701. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  4702. // SPDX-License-Identifier: MIT
  4703. // #include <nlohmann/detail/macro_scope.hpp>
  4704. // JSON_HAS_CPP_17
  4705. #ifdef JSON_HAS_CPP_17
  4706. #include <optional> // optional
  4707. #endif
  4708. #include <algorithm> // copy
  4709. #include <iterator> // begin, end
  4710. #include <string> // string
  4711. #include <tuple> // tuple, get
  4712. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4713. #include <utility> // move, forward, declval, pair
  4714. #include <valarray> // valarray
  4715. #include <vector> // vector
  4716. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4717. // __ _____ _____ _____
  4718. // __| | __| | | | JSON for Modern C++
  4719. // | | |__ | | | | | | version 3.12.0
  4720. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4721. //
  4722. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  4723. // SPDX-License-Identifier: MIT
  4724. #include <cstddef> // size_t
  4725. #include <iterator> // forward_iterator_tag
  4726. #include <tuple> // tuple_size, get, tuple_element
  4727. #include <utility> // move
  4728. #if JSON_HAS_RANGES
  4729. #include <ranges> // enable_borrowed_range
  4730. #endif
  4731. // #include <nlohmann/detail/abi_macros.hpp>
  4732. // #include <nlohmann/detail/meta/type_traits.hpp>
  4733. // #include <nlohmann/detail/string_utils.hpp>
  4734. // __ _____ _____ _____
  4735. // __| | __| | | | JSON for Modern C++
  4736. // | | |__ | | | | | | version 3.12.0
  4737. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4738. //
  4739. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  4740. // SPDX-License-Identifier: MIT
  4741. #include <cstddef> // size_t
  4742. #include <string> // string, to_string
  4743. // #include <nlohmann/detail/abi_macros.hpp>
  4744. NLOHMANN_JSON_NAMESPACE_BEGIN
  4745. namespace detail
  4746. {
  4747. template<typename StringType>
  4748. void int_to_string(StringType& target, std::size_t value)
  4749. {
  4750. // For ADL
  4751. using std::to_string;
  4752. target = to_string(value);
  4753. }
  4754. template<typename StringType>
  4755. StringType to_string(std::size_t value)
  4756. {
  4757. StringType result;
  4758. int_to_string(result, value);
  4759. return result;
  4760. }
  4761. } // namespace detail
  4762. NLOHMANN_JSON_NAMESPACE_END
  4763. // #include <nlohmann/detail/value_t.hpp>
  4764. NLOHMANN_JSON_NAMESPACE_BEGIN
  4765. namespace detail
  4766. {
  4767. template<typename IteratorType> class iteration_proxy_value
  4768. {
  4769. public:
  4770. using difference_type = std::ptrdiff_t;
  4771. using value_type = iteration_proxy_value;
  4772. using pointer = value_type *;
  4773. using reference = value_type &;
  4774. using iterator_category = std::forward_iterator_tag;
  4775. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4776. private:
  4777. /// the iterator
  4778. IteratorType anchor{};
  4779. /// an index for arrays (used to create key names)
  4780. std::size_t array_index = 0;
  4781. /// last stringified array index
  4782. mutable std::size_t array_index_last = 0;
  4783. /// a string representation of the array index
  4784. mutable string_type array_index_str = "0";
  4785. /// an empty string (to return a reference for primitive values)
  4786. string_type empty_str{};
  4787. public:
  4788. explicit iteration_proxy_value() = default;
  4789. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4790. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4791. && std::is_nothrow_default_constructible<string_type>::value)
  4792. : anchor(std::move(it))
  4793. , array_index(array_index_)
  4794. {}
  4795. iteration_proxy_value(iteration_proxy_value const&) = default;
  4796. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4797. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4798. iteration_proxy_value(iteration_proxy_value&&)
  4799. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4800. && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4801. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4802. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4803. && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4804. ~iteration_proxy_value() = default;
  4805. /// dereference operator (needed for range-based for)
  4806. const iteration_proxy_value& operator*() const
  4807. {
  4808. return *this;
  4809. }
  4810. /// increment operator (needed for range-based for)
  4811. iteration_proxy_value& operator++()
  4812. {
  4813. ++anchor;
  4814. ++array_index;
  4815. return *this;
  4816. }
  4817. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4818. {
  4819. auto tmp = iteration_proxy_value(anchor, array_index);
  4820. ++anchor;
  4821. ++array_index;
  4822. return tmp;
  4823. }
  4824. /// equality operator (needed for InputIterator)
  4825. bool operator==(const iteration_proxy_value& o) const
  4826. {
  4827. return anchor == o.anchor;
  4828. }
  4829. /// inequality operator (needed for range-based for)
  4830. bool operator!=(const iteration_proxy_value& o) const
  4831. {
  4832. return anchor != o.anchor;
  4833. }
  4834. /// return key of the iterator
  4835. const string_type& key() const
  4836. {
  4837. JSON_ASSERT(anchor.m_object != nullptr);
  4838. switch (anchor.m_object->type())
  4839. {
  4840. // use integer array index as key
  4841. case value_t::array:
  4842. {
  4843. if (array_index != array_index_last)
  4844. {
  4845. int_to_string( array_index_str, array_index );
  4846. array_index_last = array_index;
  4847. }
  4848. return array_index_str;
  4849. }
  4850. // use key from the object
  4851. case value_t::object:
  4852. return anchor.key();
  4853. // use an empty key for all primitive types
  4854. case value_t::null:
  4855. case value_t::string:
  4856. case value_t::boolean:
  4857. case value_t::number_integer:
  4858. case value_t::number_unsigned:
  4859. case value_t::number_float:
  4860. case value_t::binary:
  4861. case value_t::discarded:
  4862. default:
  4863. return empty_str;
  4864. }
  4865. }
  4866. /// return value of the iterator
  4867. typename IteratorType::reference value() const
  4868. {
  4869. return anchor.value();
  4870. }
  4871. };
  4872. /// proxy class for the items() function
  4873. template<typename IteratorType> class iteration_proxy
  4874. {
  4875. private:
  4876. /// the container to iterate
  4877. typename IteratorType::pointer container = nullptr;
  4878. public:
  4879. explicit iteration_proxy() = default;
  4880. /// construct iteration proxy from a container
  4881. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4882. : container(&cont) {}
  4883. iteration_proxy(iteration_proxy const&) = default;
  4884. iteration_proxy& operator=(iteration_proxy const&) = default;
  4885. iteration_proxy(iteration_proxy&&) noexcept = default;
  4886. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4887. ~iteration_proxy() = default;
  4888. /// return iterator begin (needed for range-based for)
  4889. iteration_proxy_value<IteratorType> begin() const noexcept
  4890. {
  4891. return iteration_proxy_value<IteratorType>(container->begin());
  4892. }
  4893. /// return iterator end (needed for range-based for)
  4894. iteration_proxy_value<IteratorType> end() const noexcept
  4895. {
  4896. return iteration_proxy_value<IteratorType>(container->end());
  4897. }
  4898. };
  4899. // Structured Bindings Support
  4900. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4901. // And see https://github.com/nlohmann/json/pull/1391
  4902. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4903. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4904. {
  4905. return i.key();
  4906. }
  4907. // Structured Bindings Support
  4908. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4909. // And see https://github.com/nlohmann/json/pull/1391
  4910. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4911. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4912. {
  4913. return i.value();
  4914. }
  4915. } // namespace detail
  4916. NLOHMANN_JSON_NAMESPACE_END
  4917. // The Addition to the STD Namespace is required to add
  4918. // Structured Bindings Support to the iteration_proxy_value class
  4919. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4920. // And see https://github.com/nlohmann/json/pull/1391
  4921. namespace std
  4922. {
  4923. #if defined(__clang__)
  4924. // Fix: https://github.com/nlohmann/json/issues/1401
  4925. #pragma clang diagnostic push
  4926. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4927. #endif
  4928. template<typename IteratorType>
  4929. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  4930. : public std::integral_constant<std::size_t, 2> {};
  4931. template<std::size_t N, typename IteratorType>
  4932. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  4933. {
  4934. public:
  4935. using type = decltype(
  4936. get<N>(std::declval <
  4937. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4938. };
  4939. #if defined(__clang__)
  4940. #pragma clang diagnostic pop
  4941. #endif
  4942. } // namespace std
  4943. #if JSON_HAS_RANGES
  4944. template <typename IteratorType>
  4945. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4946. #endif
  4947. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4948. // #include <nlohmann/detail/meta/std_fs.hpp>
  4949. // #include <nlohmann/detail/meta/type_traits.hpp>
  4950. // #include <nlohmann/detail/value_t.hpp>
  4951. NLOHMANN_JSON_NAMESPACE_BEGIN
  4952. namespace detail
  4953. {
  4954. //////////////////
  4955. // constructors //
  4956. //////////////////
  4957. /*
  4958. * Note all external_constructor<>::construct functions need to call
  4959. * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
  4960. * allocated value (e.g., a string). See bug issue
  4961. * https://github.com/nlohmann/json/issues/2865 for more information.
  4962. */
  4963. template<value_t> struct external_constructor;
  4964. template<>
  4965. struct external_constructor<value_t::boolean>
  4966. {
  4967. template<typename BasicJsonType>
  4968. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4969. {
  4970. j.m_data.m_value.destroy(j.m_data.m_type);
  4971. j.m_data.m_type = value_t::boolean;
  4972. j.m_data.m_value = b;
  4973. j.assert_invariant();
  4974. }
  4975. };
  4976. template<>
  4977. struct external_constructor<value_t::string>
  4978. {
  4979. template<typename BasicJsonType>
  4980. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4981. {
  4982. j.m_data.m_value.destroy(j.m_data.m_type);
  4983. j.m_data.m_type = value_t::string;
  4984. j.m_data.m_value = s;
  4985. j.assert_invariant();
  4986. }
  4987. template<typename BasicJsonType>
  4988. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4989. {
  4990. j.m_data.m_value.destroy(j.m_data.m_type);
  4991. j.m_data.m_type = value_t::string;
  4992. j.m_data.m_value = std::move(s);
  4993. j.assert_invariant();
  4994. }
  4995. template < typename BasicJsonType, typename CompatibleStringType,
  4996. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4997. int > = 0 >
  4998. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4999. {
  5000. j.m_data.m_value.destroy(j.m_data.m_type);
  5001. j.m_data.m_type = value_t::string;
  5002. j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  5003. j.assert_invariant();
  5004. }
  5005. };
  5006. template<>
  5007. struct external_constructor<value_t::binary>
  5008. {
  5009. template<typename BasicJsonType>
  5010. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  5011. {
  5012. j.m_data.m_value.destroy(j.m_data.m_type);
  5013. j.m_data.m_type = value_t::binary;
  5014. j.m_data.m_value = typename BasicJsonType::binary_t(b);
  5015. j.assert_invariant();
  5016. }
  5017. template<typename BasicJsonType>
  5018. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  5019. {
  5020. j.m_data.m_value.destroy(j.m_data.m_type);
  5021. j.m_data.m_type = value_t::binary;
  5022. j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
  5023. j.assert_invariant();
  5024. }
  5025. };
  5026. template<>
  5027. struct external_constructor<value_t::number_float>
  5028. {
  5029. template<typename BasicJsonType>
  5030. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  5031. {
  5032. j.m_data.m_value.destroy(j.m_data.m_type);
  5033. j.m_data.m_type = value_t::number_float;
  5034. j.m_data.m_value = val;
  5035. j.assert_invariant();
  5036. }
  5037. };
  5038. template<>
  5039. struct external_constructor<value_t::number_unsigned>
  5040. {
  5041. template<typename BasicJsonType>
  5042. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  5043. {
  5044. j.m_data.m_value.destroy(j.m_data.m_type);
  5045. j.m_data.m_type = value_t::number_unsigned;
  5046. j.m_data.m_value = val;
  5047. j.assert_invariant();
  5048. }
  5049. };
  5050. template<>
  5051. struct external_constructor<value_t::number_integer>
  5052. {
  5053. template<typename BasicJsonType>
  5054. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  5055. {
  5056. j.m_data.m_value.destroy(j.m_data.m_type);
  5057. j.m_data.m_type = value_t::number_integer;
  5058. j.m_data.m_value = val;
  5059. j.assert_invariant();
  5060. }
  5061. };
  5062. template<>
  5063. struct external_constructor<value_t::array>
  5064. {
  5065. template<typename BasicJsonType>
  5066. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  5067. {
  5068. j.m_data.m_value.destroy(j.m_data.m_type);
  5069. j.m_data.m_type = value_t::array;
  5070. j.m_data.m_value = arr;
  5071. j.set_parents();
  5072. j.assert_invariant();
  5073. }
  5074. template<typename BasicJsonType>
  5075. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  5076. {
  5077. j.m_data.m_value.destroy(j.m_data.m_type);
  5078. j.m_data.m_type = value_t::array;
  5079. j.m_data.m_value = std::move(arr);
  5080. j.set_parents();
  5081. j.assert_invariant();
  5082. }
  5083. template < typename BasicJsonType, typename CompatibleArrayType,
  5084. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  5085. int > = 0 >
  5086. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  5087. {
  5088. using std::begin;
  5089. using std::end;
  5090. j.m_data.m_value.destroy(j.m_data.m_type);
  5091. j.m_data.m_type = value_t::array;
  5092. j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  5093. j.set_parents();
  5094. j.assert_invariant();
  5095. }
  5096. template<typename BasicJsonType>
  5097. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  5098. {
  5099. j.m_data.m_value.destroy(j.m_data.m_type);
  5100. j.m_data.m_type = value_t::array;
  5101. j.m_data.m_value = value_t::array;
  5102. j.m_data.m_value.array->reserve(arr.size());
  5103. for (const bool x : arr)
  5104. {
  5105. j.m_data.m_value.array->push_back(x);
  5106. j.set_parent(j.m_data.m_value.array->back());
  5107. }
  5108. j.assert_invariant();
  5109. }
  5110. template<typename BasicJsonType, typename T,
  5111. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  5112. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  5113. {
  5114. j.m_data.m_value.destroy(j.m_data.m_type);
  5115. j.m_data.m_type = value_t::array;
  5116. j.m_data.m_value = value_t::array;
  5117. j.m_data.m_value.array->resize(arr.size());
  5118. if (arr.size() > 0)
  5119. {
  5120. std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
  5121. }
  5122. j.set_parents();
  5123. j.assert_invariant();
  5124. }
  5125. };
  5126. template<>
  5127. struct external_constructor<value_t::object>
  5128. {
  5129. template<typename BasicJsonType>
  5130. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  5131. {
  5132. j.m_data.m_value.destroy(j.m_data.m_type);
  5133. j.m_data.m_type = value_t::object;
  5134. j.m_data.m_value = obj;
  5135. j.set_parents();
  5136. j.assert_invariant();
  5137. }
  5138. template<typename BasicJsonType>
  5139. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  5140. {
  5141. j.m_data.m_value.destroy(j.m_data.m_type);
  5142. j.m_data.m_type = value_t::object;
  5143. j.m_data.m_value = std::move(obj);
  5144. j.set_parents();
  5145. j.assert_invariant();
  5146. }
  5147. template < typename BasicJsonType, typename CompatibleObjectType,
  5148. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  5149. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  5150. {
  5151. using std::begin;
  5152. using std::end;
  5153. j.m_data.m_value.destroy(j.m_data.m_type);
  5154. j.m_data.m_type = value_t::object;
  5155. j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  5156. j.set_parents();
  5157. j.assert_invariant();
  5158. }
  5159. };
  5160. /////////////
  5161. // to_json //
  5162. /////////////
  5163. #ifdef JSON_HAS_CPP_17
  5164. template<typename BasicJsonType, typename T,
  5165. enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
  5166. void to_json(BasicJsonType& j, const std::optional<T>& opt)
  5167. {
  5168. if (opt.has_value())
  5169. {
  5170. j = *opt;
  5171. }
  5172. else
  5173. {
  5174. j = nullptr;
  5175. }
  5176. }
  5177. #endif
  5178. template<typename BasicJsonType, typename T,
  5179. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  5180. inline void to_json(BasicJsonType& j, T b) noexcept
  5181. {
  5182. external_constructor<value_t::boolean>::construct(j, b);
  5183. }
  5184. template < typename BasicJsonType, typename BoolRef,
  5185. enable_if_t <
  5186. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  5187. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  5188. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  5189. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  5190. typename BasicJsonType::boolean_t >::value))
  5191. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  5192. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  5193. {
  5194. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  5195. }
  5196. template<typename BasicJsonType, typename CompatibleString,
  5197. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  5198. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  5199. {
  5200. external_constructor<value_t::string>::construct(j, s);
  5201. }
  5202. template<typename BasicJsonType>
  5203. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  5204. {
  5205. external_constructor<value_t::string>::construct(j, std::move(s));
  5206. }
  5207. template<typename BasicJsonType, typename FloatType,
  5208. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  5209. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  5210. {
  5211. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  5212. }
  5213. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  5214. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  5215. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  5216. {
  5217. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  5218. }
  5219. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  5220. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  5221. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  5222. {
  5223. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  5224. }
  5225. #if !JSON_DISABLE_ENUM_SERIALIZATION
  5226. template<typename BasicJsonType, typename EnumType,
  5227. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  5228. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  5229. {
  5230. using underlying_type = typename std::underlying_type<EnumType>::type;
  5231. static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
  5232. external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
  5233. }
  5234. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  5235. template<typename BasicJsonType>
  5236. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  5237. {
  5238. external_constructor<value_t::array>::construct(j, e);
  5239. }
  5240. template < typename BasicJsonType, typename CompatibleArrayType,
  5241. enable_if_t < is_compatible_array_type<BasicJsonType,
  5242. CompatibleArrayType>::value&&
  5243. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  5244. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  5245. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  5246. !is_basic_json<CompatibleArrayType>::value,
  5247. int > = 0 >
  5248. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  5249. {
  5250. external_constructor<value_t::array>::construct(j, arr);
  5251. }
  5252. template<typename BasicJsonType>
  5253. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  5254. {
  5255. external_constructor<value_t::binary>::construct(j, bin);
  5256. }
  5257. template<typename BasicJsonType, typename T,
  5258. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  5259. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  5260. {
  5261. external_constructor<value_t::array>::construct(j, std::move(arr));
  5262. }
  5263. template<typename BasicJsonType>
  5264. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  5265. {
  5266. external_constructor<value_t::array>::construct(j, std::move(arr));
  5267. }
  5268. template < typename BasicJsonType, typename CompatibleObjectType,
  5269. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  5270. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  5271. {
  5272. external_constructor<value_t::object>::construct(j, obj);
  5273. }
  5274. template<typename BasicJsonType>
  5275. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  5276. {
  5277. external_constructor<value_t::object>::construct(j, std::move(obj));
  5278. }
  5279. template <
  5280. typename BasicJsonType, typename T, std::size_t N,
  5281. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  5282. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5283. int > = 0 >
  5284. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5285. {
  5286. external_constructor<value_t::array>::construct(j, arr);
  5287. }
  5288. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  5289. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  5290. {
  5291. j = { p.first, p.second };
  5292. }
  5293. // for https://github.com/nlohmann/json/pull/1134
  5294. template<typename BasicJsonType, typename T,
  5295. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  5296. inline void to_json(BasicJsonType& j, const T& b)
  5297. {
  5298. j = { {b.key(), b.value()} };
  5299. }
  5300. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  5301. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  5302. {
  5303. j = { std::get<Idx>(t)... };
  5304. }
  5305. template<typename BasicJsonType, typename Tuple>
  5306. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
  5307. {
  5308. using array_t = typename BasicJsonType::array_t;
  5309. j = array_t();
  5310. }
  5311. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  5312. inline void to_json(BasicJsonType& j, const T& t)
  5313. {
  5314. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  5315. }
  5316. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  5317. template<typename BasicJsonType>
  5318. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  5319. {
  5320. #ifdef JSON_HAS_CPP_20
  5321. const std::u8string s = p.u8string();
  5322. j = std::string(s.begin(), s.end());
  5323. #else
  5324. j = p.u8string(); // returns std::string in C++17
  5325. #endif
  5326. }
  5327. #endif
  5328. struct to_json_fn
  5329. {
  5330. template<typename BasicJsonType, typename T>
  5331. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  5332. -> decltype(to_json(j, std::forward<T>(val)), void())
  5333. {
  5334. return to_json(j, std::forward<T>(val));
  5335. }
  5336. };
  5337. } // namespace detail
  5338. #ifndef JSON_HAS_CPP_17
  5339. /// namespace to hold default `to_json` function
  5340. /// to see why this is required:
  5341. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5342. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5343. {
  5344. #endif
  5345. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5346. detail::static_const<detail::to_json_fn>::value;
  5347. #ifndef JSON_HAS_CPP_17
  5348. } // namespace
  5349. #endif
  5350. NLOHMANN_JSON_NAMESPACE_END
  5351. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5352. NLOHMANN_JSON_NAMESPACE_BEGIN
  5353. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5354. template<typename ValueType, typename>
  5355. struct adl_serializer
  5356. {
  5357. /// @brief convert a JSON value to any value type
  5358. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5359. template<typename BasicJsonType, typename TargetType = ValueType>
  5360. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5361. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5362. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5363. {
  5364. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5365. }
  5366. /// @brief convert a JSON value to any value type
  5367. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5368. template<typename BasicJsonType, typename TargetType = ValueType>
  5369. static auto from_json(BasicJsonType && j) noexcept(
  5370. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5371. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5372. {
  5373. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5374. }
  5375. /// @brief convert any value type to a JSON value
  5376. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5377. template<typename BasicJsonType, typename TargetType = ValueType>
  5378. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5379. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5380. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5381. {
  5382. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5383. }
  5384. };
  5385. NLOHMANN_JSON_NAMESPACE_END
  5386. // #include <nlohmann/byte_container_with_subtype.hpp>
  5387. // __ _____ _____ _____
  5388. // __| | __| | | | JSON for Modern C++
  5389. // | | |__ | | | | | | version 3.12.0
  5390. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5391. //
  5392. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  5393. // SPDX-License-Identifier: MIT
  5394. #include <cstdint> // uint8_t, uint64_t
  5395. #include <tuple> // tie
  5396. #include <utility> // move
  5397. // #include <nlohmann/detail/abi_macros.hpp>
  5398. NLOHMANN_JSON_NAMESPACE_BEGIN
  5399. /// @brief an internal type for a backed binary type
  5400. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5401. template<typename BinaryType>
  5402. class byte_container_with_subtype : public BinaryType
  5403. {
  5404. public:
  5405. using container_type = BinaryType;
  5406. using subtype_type = std::uint64_t;
  5407. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5408. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5409. : container_type()
  5410. {}
  5411. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5412. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5413. : container_type(b)
  5414. {}
  5415. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5416. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5417. : container_type(std::move(b))
  5418. {}
  5419. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5420. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5421. : container_type(b)
  5422. , m_subtype(subtype_)
  5423. , m_has_subtype(true)
  5424. {}
  5425. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5426. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5427. : container_type(std::move(b))
  5428. , m_subtype(subtype_)
  5429. , m_has_subtype(true)
  5430. {}
  5431. bool operator==(const byte_container_with_subtype& rhs) const
  5432. {
  5433. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5434. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5435. }
  5436. bool operator!=(const byte_container_with_subtype& rhs) const
  5437. {
  5438. return !(rhs == *this);
  5439. }
  5440. /// @brief sets the binary subtype
  5441. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5442. void set_subtype(subtype_type subtype_) noexcept
  5443. {
  5444. m_subtype = subtype_;
  5445. m_has_subtype = true;
  5446. }
  5447. /// @brief return the binary subtype
  5448. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5449. constexpr subtype_type subtype() const noexcept
  5450. {
  5451. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5452. }
  5453. /// @brief return whether the value has a subtype
  5454. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5455. constexpr bool has_subtype() const noexcept
  5456. {
  5457. return m_has_subtype;
  5458. }
  5459. /// @brief clears the binary subtype
  5460. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5461. void clear_subtype() noexcept
  5462. {
  5463. m_subtype = 0;
  5464. m_has_subtype = false;
  5465. }
  5466. private:
  5467. subtype_type m_subtype = 0;
  5468. bool m_has_subtype = false;
  5469. };
  5470. NLOHMANN_JSON_NAMESPACE_END
  5471. // #include <nlohmann/detail/conversions/from_json.hpp>
  5472. // #include <nlohmann/detail/conversions/to_json.hpp>
  5473. // #include <nlohmann/detail/exceptions.hpp>
  5474. // #include <nlohmann/detail/hash.hpp>
  5475. // __ _____ _____ _____
  5476. // __| | __| | | | JSON for Modern C++
  5477. // | | |__ | | | | | | version 3.12.0
  5478. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5479. //
  5480. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  5481. // SPDX-License-Identifier: MIT
  5482. #include <cstdint> // uint8_t
  5483. #include <cstddef> // size_t
  5484. #include <functional> // hash
  5485. // #include <nlohmann/detail/abi_macros.hpp>
  5486. // #include <nlohmann/detail/value_t.hpp>
  5487. NLOHMANN_JSON_NAMESPACE_BEGIN
  5488. namespace detail
  5489. {
  5490. // boost::hash_combine
  5491. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5492. {
  5493. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5494. return seed;
  5495. }
  5496. /*!
  5497. @brief hash a JSON value
  5498. The hash function tries to rely on std::hash where possible. Furthermore, the
  5499. type of the JSON value is taken into account to have different hash values for
  5500. null, 0, 0U, and false, etc.
  5501. @tparam BasicJsonType basic_json specialization
  5502. @param j JSON value to hash
  5503. @return hash value of j
  5504. */
  5505. template<typename BasicJsonType>
  5506. std::size_t hash(const BasicJsonType& j)
  5507. {
  5508. using string_t = typename BasicJsonType::string_t;
  5509. using number_integer_t = typename BasicJsonType::number_integer_t;
  5510. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5511. using number_float_t = typename BasicJsonType::number_float_t;
  5512. const auto type = static_cast<std::size_t>(j.type());
  5513. switch (j.type())
  5514. {
  5515. case BasicJsonType::value_t::null:
  5516. case BasicJsonType::value_t::discarded:
  5517. {
  5518. return combine(type, 0);
  5519. }
  5520. case BasicJsonType::value_t::object:
  5521. {
  5522. auto seed = combine(type, j.size());
  5523. for (const auto& element : j.items())
  5524. {
  5525. const auto h = std::hash<string_t> {}(element.key());
  5526. seed = combine(seed, h);
  5527. seed = combine(seed, hash(element.value()));
  5528. }
  5529. return seed;
  5530. }
  5531. case BasicJsonType::value_t::array:
  5532. {
  5533. auto seed = combine(type, j.size());
  5534. for (const auto& element : j)
  5535. {
  5536. seed = combine(seed, hash(element));
  5537. }
  5538. return seed;
  5539. }
  5540. case BasicJsonType::value_t::string:
  5541. {
  5542. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5543. return combine(type, h);
  5544. }
  5545. case BasicJsonType::value_t::boolean:
  5546. {
  5547. const auto h = std::hash<bool> {}(j.template get<bool>());
  5548. return combine(type, h);
  5549. }
  5550. case BasicJsonType::value_t::number_integer:
  5551. {
  5552. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5553. return combine(type, h);
  5554. }
  5555. case BasicJsonType::value_t::number_unsigned:
  5556. {
  5557. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5558. return combine(type, h);
  5559. }
  5560. case BasicJsonType::value_t::number_float:
  5561. {
  5562. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5563. return combine(type, h);
  5564. }
  5565. case BasicJsonType::value_t::binary:
  5566. {
  5567. auto seed = combine(type, j.get_binary().size());
  5568. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5569. seed = combine(seed, h);
  5570. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5571. for (const auto byte : j.get_binary())
  5572. {
  5573. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5574. }
  5575. return seed;
  5576. }
  5577. default: // LCOV_EXCL_LINE
  5578. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5579. return 0; // LCOV_EXCL_LINE
  5580. }
  5581. }
  5582. } // namespace detail
  5583. NLOHMANN_JSON_NAMESPACE_END
  5584. // #include <nlohmann/detail/input/binary_reader.hpp>
  5585. // __ _____ _____ _____
  5586. // __| | __| | | | JSON for Modern C++
  5587. // | | |__ | | | | | | version 3.12.0
  5588. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5589. //
  5590. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  5591. // SPDX-License-Identifier: MIT
  5592. #include <algorithm> // generate_n
  5593. #include <array> // array
  5594. #include <cmath> // ldexp
  5595. #include <cstddef> // size_t
  5596. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5597. #include <cstdio> // snprintf
  5598. #include <cstring> // memcpy
  5599. #include <iterator> // back_inserter
  5600. #include <limits> // numeric_limits
  5601. #include <string> // char_traits, string
  5602. #include <utility> // make_pair, move
  5603. #include <vector> // vector
  5604. #ifdef __cpp_lib_byteswap
  5605. #include <bit> //byteswap
  5606. #endif
  5607. // #include <nlohmann/detail/exceptions.hpp>
  5608. // #include <nlohmann/detail/input/input_adapters.hpp>
  5609. // __ _____ _____ _____
  5610. // __| | __| | | | JSON for Modern C++
  5611. // | | |__ | | | | | | version 3.12.0
  5612. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5613. //
  5614. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  5615. // SPDX-License-Identifier: MIT
  5616. #include <array> // array
  5617. #include <cstddef> // size_t
  5618. #include <cstring> // strlen
  5619. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5620. #include <memory> // shared_ptr, make_shared, addressof
  5621. #include <numeric> // accumulate
  5622. #include <string> // string, char_traits
  5623. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5624. #include <utility> // pair, declval
  5625. #ifndef JSON_NO_IO
  5626. #include <cstdio> // FILE *
  5627. #include <istream> // istream
  5628. #endif // JSON_NO_IO
  5629. // #include <nlohmann/detail/exceptions.hpp>
  5630. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5631. // #include <nlohmann/detail/macro_scope.hpp>
  5632. // #include <nlohmann/detail/meta/type_traits.hpp>
  5633. NLOHMANN_JSON_NAMESPACE_BEGIN
  5634. namespace detail
  5635. {
  5636. /// the supported input formats
  5637. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5638. ////////////////////
  5639. // input adapters //
  5640. ////////////////////
  5641. #ifndef JSON_NO_IO
  5642. /*!
  5643. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5644. buffer. This adapter is a very low level adapter.
  5645. */
  5646. class file_input_adapter
  5647. {
  5648. public:
  5649. using char_type = char;
  5650. JSON_HEDLEY_NON_NULL(2)
  5651. explicit file_input_adapter(std::FILE* f) noexcept
  5652. : m_file(f)
  5653. {
  5654. JSON_ASSERT(m_file != nullptr);
  5655. }
  5656. // make class move-only
  5657. file_input_adapter(const file_input_adapter&) = delete;
  5658. file_input_adapter(file_input_adapter&&) noexcept = default;
  5659. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5660. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5661. ~file_input_adapter() = default;
  5662. std::char_traits<char>::int_type get_character() noexcept
  5663. {
  5664. return std::fgetc(m_file);
  5665. }
  5666. // returns the number of characters successfully read
  5667. template<class T>
  5668. std::size_t get_elements(T* dest, std::size_t count = 1)
  5669. {
  5670. return fread(dest, 1, sizeof(T) * count, m_file);
  5671. }
  5672. private:
  5673. /// the file pointer to read from
  5674. std::FILE* m_file;
  5675. };
  5676. /*!
  5677. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5678. beginning of input. Does not support changing the underlying std::streambuf
  5679. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5680. subsequent use of standard std::istream operations to process any input
  5681. characters following those used in parsing the JSON input. Clears the
  5682. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5683. subsequent call for input from the std::istream.
  5684. */
  5685. class input_stream_adapter
  5686. {
  5687. public:
  5688. using char_type = char;
  5689. ~input_stream_adapter()
  5690. {
  5691. // clear stream flags; we use underlying streambuf I/O, do not
  5692. // maintain ifstream flags, except eof
  5693. if (is != nullptr)
  5694. {
  5695. is->clear(is->rdstate() & std::ios::eofbit);
  5696. }
  5697. }
  5698. explicit input_stream_adapter(std::istream& i)
  5699. : is(&i), sb(i.rdbuf())
  5700. {}
  5701. // delete because of pointer members
  5702. input_stream_adapter(const input_stream_adapter&) = delete;
  5703. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5704. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5705. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5706. : is(rhs.is), sb(rhs.sb)
  5707. {
  5708. rhs.is = nullptr;
  5709. rhs.sb = nullptr;
  5710. }
  5711. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5712. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5713. // end up as the same value, e.g. 0xFFFFFFFF.
  5714. std::char_traits<char>::int_type get_character()
  5715. {
  5716. auto res = sb->sbumpc();
  5717. // set eof manually, as we don't use the istream interface.
  5718. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5719. {
  5720. is->clear(is->rdstate() | std::ios::eofbit);
  5721. }
  5722. return res;
  5723. }
  5724. template<class T>
  5725. std::size_t get_elements(T* dest, std::size_t count = 1)
  5726. {
  5727. auto res = static_cast<std::size_t>(sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T))));
  5728. if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
  5729. {
  5730. is->clear(is->rdstate() | std::ios::eofbit);
  5731. }
  5732. return res;
  5733. }
  5734. private:
  5735. /// the associated input stream
  5736. std::istream* is = nullptr;
  5737. std::streambuf* sb = nullptr;
  5738. };
  5739. #endif // JSON_NO_IO
  5740. // General-purpose iterator-based adapter. It might not be as fast as
  5741. // theoretically possible for some containers, but it is extremely versatile.
  5742. template<typename IteratorType>
  5743. class iterator_input_adapter
  5744. {
  5745. public:
  5746. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5747. iterator_input_adapter(IteratorType first, IteratorType last)
  5748. : current(std::move(first)), end(std::move(last))
  5749. {}
  5750. typename char_traits<char_type>::int_type get_character()
  5751. {
  5752. if (JSON_HEDLEY_LIKELY(current != end))
  5753. {
  5754. auto result = char_traits<char_type>::to_int_type(*current);
  5755. std::advance(current, 1);
  5756. return result;
  5757. }
  5758. return char_traits<char_type>::eof();
  5759. }
  5760. // for general iterators, we cannot really do something better than falling back to processing the range one-by-one
  5761. template<class T>
  5762. std::size_t get_elements(T* dest, std::size_t count = 1)
  5763. {
  5764. auto* ptr = reinterpret_cast<char*>(dest);
  5765. for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
  5766. {
  5767. if (JSON_HEDLEY_LIKELY(current != end))
  5768. {
  5769. ptr[read_index] = static_cast<char>(*current);
  5770. std::advance(current, 1);
  5771. }
  5772. else
  5773. {
  5774. return read_index;
  5775. }
  5776. }
  5777. return count * sizeof(T);
  5778. }
  5779. private:
  5780. IteratorType current;
  5781. IteratorType end;
  5782. template<typename BaseInputAdapter, size_t T>
  5783. friend struct wide_string_input_helper;
  5784. bool empty() const
  5785. {
  5786. return current == end;
  5787. }
  5788. };
  5789. template<typename BaseInputAdapter, size_t T>
  5790. struct wide_string_input_helper;
  5791. template<typename BaseInputAdapter>
  5792. struct wide_string_input_helper<BaseInputAdapter, 4>
  5793. {
  5794. // UTF-32
  5795. static void fill_buffer(BaseInputAdapter& input,
  5796. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5797. size_t& utf8_bytes_index,
  5798. size_t& utf8_bytes_filled)
  5799. {
  5800. utf8_bytes_index = 0;
  5801. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5802. {
  5803. utf8_bytes[0] = std::char_traits<char>::eof();
  5804. utf8_bytes_filled = 1;
  5805. }
  5806. else
  5807. {
  5808. // get the current character
  5809. const auto wc = input.get_character();
  5810. // UTF-32 to UTF-8 encoding
  5811. if (wc < 0x80)
  5812. {
  5813. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5814. utf8_bytes_filled = 1;
  5815. }
  5816. else if (wc <= 0x7FF)
  5817. {
  5818. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5819. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5820. utf8_bytes_filled = 2;
  5821. }
  5822. else if (wc <= 0xFFFF)
  5823. {
  5824. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5825. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5826. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5827. utf8_bytes_filled = 3;
  5828. }
  5829. else if (wc <= 0x10FFFF)
  5830. {
  5831. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5832. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5833. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5834. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5835. utf8_bytes_filled = 4;
  5836. }
  5837. else
  5838. {
  5839. // unknown character
  5840. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5841. utf8_bytes_filled = 1;
  5842. }
  5843. }
  5844. }
  5845. };
  5846. template<typename BaseInputAdapter>
  5847. struct wide_string_input_helper<BaseInputAdapter, 2>
  5848. {
  5849. // UTF-16
  5850. static void fill_buffer(BaseInputAdapter& input,
  5851. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5852. size_t& utf8_bytes_index,
  5853. size_t& utf8_bytes_filled)
  5854. {
  5855. utf8_bytes_index = 0;
  5856. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5857. {
  5858. utf8_bytes[0] = std::char_traits<char>::eof();
  5859. utf8_bytes_filled = 1;
  5860. }
  5861. else
  5862. {
  5863. // get the current character
  5864. const auto wc = input.get_character();
  5865. // UTF-16 to UTF-8 encoding
  5866. if (wc < 0x80)
  5867. {
  5868. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5869. utf8_bytes_filled = 1;
  5870. }
  5871. else if (wc <= 0x7FF)
  5872. {
  5873. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5874. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5875. utf8_bytes_filled = 2;
  5876. }
  5877. else if (0xD800 > wc || wc >= 0xE000)
  5878. {
  5879. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5880. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5881. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5882. utf8_bytes_filled = 3;
  5883. }
  5884. else
  5885. {
  5886. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5887. {
  5888. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5889. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5890. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5891. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5892. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5893. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5894. utf8_bytes_filled = 4;
  5895. }
  5896. else
  5897. {
  5898. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5899. utf8_bytes_filled = 1;
  5900. }
  5901. }
  5902. }
  5903. }
  5904. };
  5905. // Wraps another input adapter to convert wide character types into individual bytes.
  5906. template<typename BaseInputAdapter, typename WideCharType>
  5907. class wide_string_input_adapter
  5908. {
  5909. public:
  5910. using char_type = char;
  5911. wide_string_input_adapter(BaseInputAdapter base)
  5912. : base_adapter(base) {}
  5913. typename std::char_traits<char>::int_type get_character() noexcept
  5914. {
  5915. // check if buffer needs to be filled
  5916. if (utf8_bytes_index == utf8_bytes_filled)
  5917. {
  5918. fill_buffer<sizeof(WideCharType)>();
  5919. JSON_ASSERT(utf8_bytes_filled > 0);
  5920. JSON_ASSERT(utf8_bytes_index == 0);
  5921. }
  5922. // use buffer
  5923. JSON_ASSERT(utf8_bytes_filled > 0);
  5924. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5925. return utf8_bytes[utf8_bytes_index++];
  5926. }
  5927. // parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
  5928. template<class T>
  5929. std::size_t get_elements(T* /*dest*/, std::size_t /*count*/ = 1)
  5930. {
  5931. JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
  5932. }
  5933. private:
  5934. BaseInputAdapter base_adapter;
  5935. template<size_t T>
  5936. void fill_buffer()
  5937. {
  5938. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5939. }
  5940. /// a buffer for UTF-8 bytes
  5941. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5942. /// index to the utf8_codes array for the next valid byte
  5943. std::size_t utf8_bytes_index = 0;
  5944. /// number of valid bytes in the utf8_codes array
  5945. std::size_t utf8_bytes_filled = 0;
  5946. };
  5947. template<typename IteratorType, typename Enable = void>
  5948. struct iterator_input_adapter_factory
  5949. {
  5950. using iterator_type = IteratorType;
  5951. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5952. using adapter_type = iterator_input_adapter<iterator_type>;
  5953. static adapter_type create(IteratorType first, IteratorType last)
  5954. {
  5955. return adapter_type(std::move(first), std::move(last));
  5956. }
  5957. };
  5958. template<typename T>
  5959. struct is_iterator_of_multibyte
  5960. {
  5961. using value_type = typename std::iterator_traits<T>::value_type;
  5962. enum
  5963. {
  5964. value = sizeof(value_type) > 1
  5965. };
  5966. };
  5967. template<typename IteratorType>
  5968. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5969. {
  5970. using iterator_type = IteratorType;
  5971. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5972. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5973. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5974. static adapter_type create(IteratorType first, IteratorType last)
  5975. {
  5976. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5977. }
  5978. };
  5979. // General purpose iterator-based input
  5980. template<typename IteratorType>
  5981. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5982. {
  5983. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5984. return factory_type::create(first, last);
  5985. }
  5986. // Convenience shorthand from container to iterator
  5987. // Enables ADL on begin(container) and end(container)
  5988. // Encloses the using declarations in namespace for not to leak them to outside scope
  5989. namespace container_input_adapter_factory_impl
  5990. {
  5991. using std::begin;
  5992. using std::end;
  5993. template<typename ContainerType, typename Enable = void>
  5994. struct container_input_adapter_factory {};
  5995. template<typename ContainerType>
  5996. struct container_input_adapter_factory< ContainerType,
  5997. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5998. {
  5999. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  6000. static adapter_type create(const ContainerType& container)
  6001. {
  6002. return input_adapter(begin(container), end(container));
  6003. }
  6004. };
  6005. } // namespace container_input_adapter_factory_impl
  6006. template<typename ContainerType>
  6007. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  6008. {
  6009. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  6010. }
  6011. // specialization for std::string
  6012. using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
  6013. #ifndef JSON_NO_IO
  6014. // Special cases with fast paths
  6015. inline file_input_adapter input_adapter(std::FILE* file)
  6016. {
  6017. if (file == nullptr)
  6018. {
  6019. JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
  6020. }
  6021. return file_input_adapter(file);
  6022. }
  6023. inline input_stream_adapter input_adapter(std::istream& stream)
  6024. {
  6025. return input_stream_adapter(stream);
  6026. }
  6027. inline input_stream_adapter input_adapter(std::istream&& stream)
  6028. {
  6029. return input_stream_adapter(stream);
  6030. }
  6031. #endif // JSON_NO_IO
  6032. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  6033. // Null-delimited strings, and the like.
  6034. template < typename CharT,
  6035. typename std::enable_if <
  6036. std::is_pointer<CharT>::value&&
  6037. !std::is_array<CharT>::value&&
  6038. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  6039. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  6040. int >::type = 0 >
  6041. contiguous_bytes_input_adapter input_adapter(CharT b)
  6042. {
  6043. if (b == nullptr)
  6044. {
  6045. JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
  6046. }
  6047. auto length = std::strlen(reinterpret_cast<const char*>(b));
  6048. const auto* ptr = reinterpret_cast<const char*>(b);
  6049. return input_adapter(ptr, ptr + length); // cppcheck-suppress[nullPointerArithmeticRedundantCheck]
  6050. }
  6051. template<typename T, std::size_t N>
  6052. auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  6053. {
  6054. return input_adapter(array, array + N);
  6055. }
  6056. // This class only handles inputs of input_buffer_adapter type.
  6057. // It's required so that expressions like {ptr, len} can be implicitly cast
  6058. // to the correct adapter.
  6059. class span_input_adapter
  6060. {
  6061. public:
  6062. template < typename CharT,
  6063. typename std::enable_if <
  6064. std::is_pointer<CharT>::value&&
  6065. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  6066. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  6067. int >::type = 0 >
  6068. span_input_adapter(CharT b, std::size_t l)
  6069. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  6070. template<class IteratorType,
  6071. typename std::enable_if<
  6072. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  6073. int>::type = 0>
  6074. span_input_adapter(IteratorType first, IteratorType last)
  6075. : ia(input_adapter(first, last)) {}
  6076. contiguous_bytes_input_adapter&& get()
  6077. {
  6078. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  6079. }
  6080. private:
  6081. contiguous_bytes_input_adapter ia;
  6082. };
  6083. } // namespace detail
  6084. NLOHMANN_JSON_NAMESPACE_END
  6085. // #include <nlohmann/detail/input/json_sax.hpp>
  6086. // __ _____ _____ _____
  6087. // __| | __| | | | JSON for Modern C++
  6088. // | | |__ | | | | | | version 3.12.0
  6089. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6090. //
  6091. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  6092. // SPDX-License-Identifier: MIT
  6093. #include <cstddef>
  6094. #include <string> // string
  6095. #include <type_traits> // enable_if_t
  6096. #include <utility> // move
  6097. #include <vector> // vector
  6098. // #include <nlohmann/detail/exceptions.hpp>
  6099. // #include <nlohmann/detail/input/lexer.hpp>
  6100. // __ _____ _____ _____
  6101. // __| | __| | | | JSON for Modern C++
  6102. // | | |__ | | | | | | version 3.12.0
  6103. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6104. //
  6105. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  6106. // SPDX-License-Identifier: MIT
  6107. #include <array> // array
  6108. #include <clocale> // localeconv
  6109. #include <cstddef> // size_t
  6110. #include <cstdio> // snprintf
  6111. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6112. #include <initializer_list> // initializer_list
  6113. #include <string> // char_traits, string
  6114. #include <utility> // move
  6115. #include <vector> // vector
  6116. // #include <nlohmann/detail/input/input_adapters.hpp>
  6117. // #include <nlohmann/detail/input/position_t.hpp>
  6118. // #include <nlohmann/detail/macro_scope.hpp>
  6119. // #include <nlohmann/detail/meta/type_traits.hpp>
  6120. NLOHMANN_JSON_NAMESPACE_BEGIN
  6121. namespace detail
  6122. {
  6123. ///////////
  6124. // lexer //
  6125. ///////////
  6126. template<typename BasicJsonType>
  6127. class lexer_base
  6128. {
  6129. public:
  6130. /// token types for the parser
  6131. enum class token_type
  6132. {
  6133. uninitialized, ///< indicating the scanner is uninitialized
  6134. literal_true, ///< the `true` literal
  6135. literal_false, ///< the `false` literal
  6136. literal_null, ///< the `null` literal
  6137. value_string, ///< a string -- use get_string() for actual value
  6138. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6139. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6140. value_float, ///< an floating point number -- use get_number_float() for actual value
  6141. begin_array, ///< the character for array begin `[`
  6142. begin_object, ///< the character for object begin `{`
  6143. end_array, ///< the character for array end `]`
  6144. end_object, ///< the character for object end `}`
  6145. name_separator, ///< the name separator `:`
  6146. value_separator, ///< the value separator `,`
  6147. parse_error, ///< indicating a parse error
  6148. end_of_input, ///< indicating the end of the input buffer
  6149. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6150. };
  6151. /// return name of values of type token_type (only used for errors)
  6152. JSON_HEDLEY_RETURNS_NON_NULL
  6153. JSON_HEDLEY_CONST
  6154. static const char* token_type_name(const token_type t) noexcept
  6155. {
  6156. switch (t)
  6157. {
  6158. case token_type::uninitialized:
  6159. return "<uninitialized>";
  6160. case token_type::literal_true:
  6161. return "true literal";
  6162. case token_type::literal_false:
  6163. return "false literal";
  6164. case token_type::literal_null:
  6165. return "null literal";
  6166. case token_type::value_string:
  6167. return "string literal";
  6168. case token_type::value_unsigned:
  6169. case token_type::value_integer:
  6170. case token_type::value_float:
  6171. return "number literal";
  6172. case token_type::begin_array:
  6173. return "'['";
  6174. case token_type::begin_object:
  6175. return "'{'";
  6176. case token_type::end_array:
  6177. return "']'";
  6178. case token_type::end_object:
  6179. return "'}'";
  6180. case token_type::name_separator:
  6181. return "':'";
  6182. case token_type::value_separator:
  6183. return "','";
  6184. case token_type::parse_error:
  6185. return "<parse error>";
  6186. case token_type::end_of_input:
  6187. return "end of input";
  6188. case token_type::literal_or_value:
  6189. return "'[', '{', or a literal";
  6190. // LCOV_EXCL_START
  6191. default: // catch non-enum values
  6192. return "unknown token";
  6193. // LCOV_EXCL_STOP
  6194. }
  6195. }
  6196. };
  6197. /*!
  6198. @brief lexical analysis
  6199. This class organizes the lexical analysis during JSON deserialization.
  6200. */
  6201. template<typename BasicJsonType, typename InputAdapterType>
  6202. class lexer : public lexer_base<BasicJsonType>
  6203. {
  6204. using number_integer_t = typename BasicJsonType::number_integer_t;
  6205. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6206. using number_float_t = typename BasicJsonType::number_float_t;
  6207. using string_t = typename BasicJsonType::string_t;
  6208. using char_type = typename InputAdapterType::char_type;
  6209. using char_int_type = typename char_traits<char_type>::int_type;
  6210. public:
  6211. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6212. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6213. : ia(std::move(adapter))
  6214. , ignore_comments(ignore_comments_)
  6215. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6216. {}
  6217. // delete because of pointer members
  6218. lexer(const lexer&) = delete;
  6219. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6220. lexer& operator=(lexer&) = delete;
  6221. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6222. ~lexer() = default;
  6223. private:
  6224. /////////////////////
  6225. // locales
  6226. /////////////////////
  6227. /// return the locale-dependent decimal point
  6228. JSON_HEDLEY_PURE
  6229. static char get_decimal_point() noexcept
  6230. {
  6231. const auto* loc = localeconv();
  6232. JSON_ASSERT(loc != nullptr);
  6233. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6234. }
  6235. /////////////////////
  6236. // scan functions
  6237. /////////////////////
  6238. /*!
  6239. @brief get codepoint from 4 hex characters following `\u`
  6240. For input "\u c1 c2 c3 c4" the codepoint is:
  6241. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6242. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6243. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6244. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6245. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6246. between the ASCII value of the character and the desired integer value.
  6247. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6248. non-hex character)
  6249. */
  6250. int get_codepoint()
  6251. {
  6252. // this function only makes sense after reading `\u`
  6253. JSON_ASSERT(current == 'u');
  6254. int codepoint = 0;
  6255. const auto factors = { 12u, 8u, 4u, 0u };
  6256. for (const auto factor : factors)
  6257. {
  6258. get();
  6259. if (current >= '0' && current <= '9')
  6260. {
  6261. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6262. }
  6263. else if (current >= 'A' && current <= 'F')
  6264. {
  6265. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6266. }
  6267. else if (current >= 'a' && current <= 'f')
  6268. {
  6269. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6270. }
  6271. else
  6272. {
  6273. return -1;
  6274. }
  6275. }
  6276. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6277. return codepoint;
  6278. }
  6279. /*!
  6280. @brief check if the next byte(s) are inside a given range
  6281. Adds the current byte and, for each passed range, reads a new byte and
  6282. checks if it is inside the range. If a violation was detected, set up an
  6283. error message and return false. Otherwise, return true.
  6284. @param[in] ranges list of integers; interpreted as list of pairs of
  6285. inclusive lower and upper bound, respectively
  6286. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6287. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6288. @return true if and only if no range violation was detected
  6289. */
  6290. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6291. {
  6292. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6293. add(current);
  6294. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6295. {
  6296. get();
  6297. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
  6298. {
  6299. add(current);
  6300. }
  6301. else
  6302. {
  6303. error_message = "invalid string: ill-formed UTF-8 byte";
  6304. return false;
  6305. }
  6306. }
  6307. return true;
  6308. }
  6309. /*!
  6310. @brief scan a string literal
  6311. This function scans a string according to Sect. 7 of RFC 8259. While
  6312. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6313. function returns successfully, token_buffer is *not* null-terminated (as it
  6314. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6315. string.
  6316. @return token_type::value_string if string could be successfully scanned,
  6317. token_type::parse_error otherwise
  6318. @note In case of errors, variable error_message contains a textual
  6319. description.
  6320. */
  6321. token_type scan_string()
  6322. {
  6323. // reset token_buffer (ignore opening quote)
  6324. reset();
  6325. // we entered the function by reading an open quote
  6326. JSON_ASSERT(current == '\"');
  6327. while (true)
  6328. {
  6329. // get next character
  6330. switch (get())
  6331. {
  6332. // end of file while parsing string
  6333. case char_traits<char_type>::eof():
  6334. {
  6335. error_message = "invalid string: missing closing quote";
  6336. return token_type::parse_error;
  6337. }
  6338. // closing quote
  6339. case '\"':
  6340. {
  6341. return token_type::value_string;
  6342. }
  6343. // escapes
  6344. case '\\':
  6345. {
  6346. switch (get())
  6347. {
  6348. // quotation mark
  6349. case '\"':
  6350. add('\"');
  6351. break;
  6352. // reverse solidus
  6353. case '\\':
  6354. add('\\');
  6355. break;
  6356. // solidus
  6357. case '/':
  6358. add('/');
  6359. break;
  6360. // backspace
  6361. case 'b':
  6362. add('\b');
  6363. break;
  6364. // form feed
  6365. case 'f':
  6366. add('\f');
  6367. break;
  6368. // line feed
  6369. case 'n':
  6370. add('\n');
  6371. break;
  6372. // carriage return
  6373. case 'r':
  6374. add('\r');
  6375. break;
  6376. // tab
  6377. case 't':
  6378. add('\t');
  6379. break;
  6380. // unicode escapes
  6381. case 'u':
  6382. {
  6383. const int codepoint1 = get_codepoint();
  6384. int codepoint = codepoint1; // start with codepoint1
  6385. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6386. {
  6387. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6388. return token_type::parse_error;
  6389. }
  6390. // check if code point is a high surrogate
  6391. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6392. {
  6393. // expect next \uxxxx entry
  6394. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6395. {
  6396. const int codepoint2 = get_codepoint();
  6397. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6398. {
  6399. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6400. return token_type::parse_error;
  6401. }
  6402. // check if codepoint2 is a low surrogate
  6403. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6404. {
  6405. // overwrite codepoint
  6406. codepoint = static_cast<int>(
  6407. // high surrogate occupies the most significant 22 bits
  6408. (static_cast<unsigned int>(codepoint1) << 10u)
  6409. // low surrogate occupies the least significant 15 bits
  6410. + static_cast<unsigned int>(codepoint2)
  6411. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6412. // in the result, so we have to subtract with:
  6413. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6414. - 0x35FDC00u);
  6415. }
  6416. else
  6417. {
  6418. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6419. return token_type::parse_error;
  6420. }
  6421. }
  6422. else
  6423. {
  6424. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6425. return token_type::parse_error;
  6426. }
  6427. }
  6428. else
  6429. {
  6430. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6431. {
  6432. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6433. return token_type::parse_error;
  6434. }
  6435. }
  6436. // result of the above calculation yields a proper codepoint
  6437. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6438. // translate codepoint into bytes
  6439. if (codepoint < 0x80)
  6440. {
  6441. // 1-byte characters: 0xxxxxxx (ASCII)
  6442. add(static_cast<char_int_type>(codepoint));
  6443. }
  6444. else if (codepoint <= 0x7FF)
  6445. {
  6446. // 2-byte characters: 110xxxxx 10xxxxxx
  6447. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6448. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6449. }
  6450. else if (codepoint <= 0xFFFF)
  6451. {
  6452. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6453. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6454. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6455. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6456. }
  6457. else
  6458. {
  6459. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6460. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6461. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6462. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6463. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6464. }
  6465. break;
  6466. }
  6467. // other characters after escape
  6468. default:
  6469. error_message = "invalid string: forbidden character after backslash";
  6470. return token_type::parse_error;
  6471. }
  6472. break;
  6473. }
  6474. // invalid control characters
  6475. case 0x00:
  6476. {
  6477. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6478. return token_type::parse_error;
  6479. }
  6480. case 0x01:
  6481. {
  6482. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6483. return token_type::parse_error;
  6484. }
  6485. case 0x02:
  6486. {
  6487. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6488. return token_type::parse_error;
  6489. }
  6490. case 0x03:
  6491. {
  6492. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6493. return token_type::parse_error;
  6494. }
  6495. case 0x04:
  6496. {
  6497. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6498. return token_type::parse_error;
  6499. }
  6500. case 0x05:
  6501. {
  6502. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6503. return token_type::parse_error;
  6504. }
  6505. case 0x06:
  6506. {
  6507. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6508. return token_type::parse_error;
  6509. }
  6510. case 0x07:
  6511. {
  6512. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6513. return token_type::parse_error;
  6514. }
  6515. case 0x08:
  6516. {
  6517. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6518. return token_type::parse_error;
  6519. }
  6520. case 0x09:
  6521. {
  6522. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6523. return token_type::parse_error;
  6524. }
  6525. case 0x0A:
  6526. {
  6527. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6528. return token_type::parse_error;
  6529. }
  6530. case 0x0B:
  6531. {
  6532. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6533. return token_type::parse_error;
  6534. }
  6535. case 0x0C:
  6536. {
  6537. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6538. return token_type::parse_error;
  6539. }
  6540. case 0x0D:
  6541. {
  6542. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6543. return token_type::parse_error;
  6544. }
  6545. case 0x0E:
  6546. {
  6547. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6548. return token_type::parse_error;
  6549. }
  6550. case 0x0F:
  6551. {
  6552. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6553. return token_type::parse_error;
  6554. }
  6555. case 0x10:
  6556. {
  6557. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6558. return token_type::parse_error;
  6559. }
  6560. case 0x11:
  6561. {
  6562. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6563. return token_type::parse_error;
  6564. }
  6565. case 0x12:
  6566. {
  6567. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6568. return token_type::parse_error;
  6569. }
  6570. case 0x13:
  6571. {
  6572. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6573. return token_type::parse_error;
  6574. }
  6575. case 0x14:
  6576. {
  6577. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6578. return token_type::parse_error;
  6579. }
  6580. case 0x15:
  6581. {
  6582. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6583. return token_type::parse_error;
  6584. }
  6585. case 0x16:
  6586. {
  6587. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6588. return token_type::parse_error;
  6589. }
  6590. case 0x17:
  6591. {
  6592. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6593. return token_type::parse_error;
  6594. }
  6595. case 0x18:
  6596. {
  6597. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6598. return token_type::parse_error;
  6599. }
  6600. case 0x19:
  6601. {
  6602. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6603. return token_type::parse_error;
  6604. }
  6605. case 0x1A:
  6606. {
  6607. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6608. return token_type::parse_error;
  6609. }
  6610. case 0x1B:
  6611. {
  6612. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6613. return token_type::parse_error;
  6614. }
  6615. case 0x1C:
  6616. {
  6617. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6618. return token_type::parse_error;
  6619. }
  6620. case 0x1D:
  6621. {
  6622. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6623. return token_type::parse_error;
  6624. }
  6625. case 0x1E:
  6626. {
  6627. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6628. return token_type::parse_error;
  6629. }
  6630. case 0x1F:
  6631. {
  6632. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6633. return token_type::parse_error;
  6634. }
  6635. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6636. case 0x20:
  6637. case 0x21:
  6638. case 0x23:
  6639. case 0x24:
  6640. case 0x25:
  6641. case 0x26:
  6642. case 0x27:
  6643. case 0x28:
  6644. case 0x29:
  6645. case 0x2A:
  6646. case 0x2B:
  6647. case 0x2C:
  6648. case 0x2D:
  6649. case 0x2E:
  6650. case 0x2F:
  6651. case 0x30:
  6652. case 0x31:
  6653. case 0x32:
  6654. case 0x33:
  6655. case 0x34:
  6656. case 0x35:
  6657. case 0x36:
  6658. case 0x37:
  6659. case 0x38:
  6660. case 0x39:
  6661. case 0x3A:
  6662. case 0x3B:
  6663. case 0x3C:
  6664. case 0x3D:
  6665. case 0x3E:
  6666. case 0x3F:
  6667. case 0x40:
  6668. case 0x41:
  6669. case 0x42:
  6670. case 0x43:
  6671. case 0x44:
  6672. case 0x45:
  6673. case 0x46:
  6674. case 0x47:
  6675. case 0x48:
  6676. case 0x49:
  6677. case 0x4A:
  6678. case 0x4B:
  6679. case 0x4C:
  6680. case 0x4D:
  6681. case 0x4E:
  6682. case 0x4F:
  6683. case 0x50:
  6684. case 0x51:
  6685. case 0x52:
  6686. case 0x53:
  6687. case 0x54:
  6688. case 0x55:
  6689. case 0x56:
  6690. case 0x57:
  6691. case 0x58:
  6692. case 0x59:
  6693. case 0x5A:
  6694. case 0x5B:
  6695. case 0x5D:
  6696. case 0x5E:
  6697. case 0x5F:
  6698. case 0x60:
  6699. case 0x61:
  6700. case 0x62:
  6701. case 0x63:
  6702. case 0x64:
  6703. case 0x65:
  6704. case 0x66:
  6705. case 0x67:
  6706. case 0x68:
  6707. case 0x69:
  6708. case 0x6A:
  6709. case 0x6B:
  6710. case 0x6C:
  6711. case 0x6D:
  6712. case 0x6E:
  6713. case 0x6F:
  6714. case 0x70:
  6715. case 0x71:
  6716. case 0x72:
  6717. case 0x73:
  6718. case 0x74:
  6719. case 0x75:
  6720. case 0x76:
  6721. case 0x77:
  6722. case 0x78:
  6723. case 0x79:
  6724. case 0x7A:
  6725. case 0x7B:
  6726. case 0x7C:
  6727. case 0x7D:
  6728. case 0x7E:
  6729. case 0x7F:
  6730. {
  6731. add(current);
  6732. break;
  6733. }
  6734. // U+0080..U+07FF: bytes C2..DF 80..BF
  6735. case 0xC2:
  6736. case 0xC3:
  6737. case 0xC4:
  6738. case 0xC5:
  6739. case 0xC6:
  6740. case 0xC7:
  6741. case 0xC8:
  6742. case 0xC9:
  6743. case 0xCA:
  6744. case 0xCB:
  6745. case 0xCC:
  6746. case 0xCD:
  6747. case 0xCE:
  6748. case 0xCF:
  6749. case 0xD0:
  6750. case 0xD1:
  6751. case 0xD2:
  6752. case 0xD3:
  6753. case 0xD4:
  6754. case 0xD5:
  6755. case 0xD6:
  6756. case 0xD7:
  6757. case 0xD8:
  6758. case 0xD9:
  6759. case 0xDA:
  6760. case 0xDB:
  6761. case 0xDC:
  6762. case 0xDD:
  6763. case 0xDE:
  6764. case 0xDF:
  6765. {
  6766. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6767. {
  6768. return token_type::parse_error;
  6769. }
  6770. break;
  6771. }
  6772. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6773. case 0xE0:
  6774. {
  6775. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6776. {
  6777. return token_type::parse_error;
  6778. }
  6779. break;
  6780. }
  6781. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6782. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6783. case 0xE1:
  6784. case 0xE2:
  6785. case 0xE3:
  6786. case 0xE4:
  6787. case 0xE5:
  6788. case 0xE6:
  6789. case 0xE7:
  6790. case 0xE8:
  6791. case 0xE9:
  6792. case 0xEA:
  6793. case 0xEB:
  6794. case 0xEC:
  6795. case 0xEE:
  6796. case 0xEF:
  6797. {
  6798. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  6799. {
  6800. return token_type::parse_error;
  6801. }
  6802. break;
  6803. }
  6804. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  6805. case 0xED:
  6806. {
  6807. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  6808. {
  6809. return token_type::parse_error;
  6810. }
  6811. break;
  6812. }
  6813. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  6814. case 0xF0:
  6815. {
  6816. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6817. {
  6818. return token_type::parse_error;
  6819. }
  6820. break;
  6821. }
  6822. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  6823. case 0xF1:
  6824. case 0xF2:
  6825. case 0xF3:
  6826. {
  6827. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6828. {
  6829. return token_type::parse_error;
  6830. }
  6831. break;
  6832. }
  6833. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  6834. case 0xF4:
  6835. {
  6836. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  6837. {
  6838. return token_type::parse_error;
  6839. }
  6840. break;
  6841. }
  6842. // remaining bytes (80..C1 and F5..FF) are ill-formed
  6843. default:
  6844. {
  6845. error_message = "invalid string: ill-formed UTF-8 byte";
  6846. return token_type::parse_error;
  6847. }
  6848. }
  6849. }
  6850. }
  6851. /*!
  6852. * @brief scan a comment
  6853. * @return whether comment could be scanned successfully
  6854. */
  6855. bool scan_comment()
  6856. {
  6857. switch (get())
  6858. {
  6859. // single-line comments skip input until a newline or EOF is read
  6860. case '/':
  6861. {
  6862. while (true)
  6863. {
  6864. switch (get())
  6865. {
  6866. case '\n':
  6867. case '\r':
  6868. case char_traits<char_type>::eof():
  6869. case '\0':
  6870. return true;
  6871. default:
  6872. break;
  6873. }
  6874. }
  6875. }
  6876. // multi-line comments skip input until */ is read
  6877. case '*':
  6878. {
  6879. while (true)
  6880. {
  6881. switch (get())
  6882. {
  6883. case char_traits<char_type>::eof():
  6884. case '\0':
  6885. {
  6886. error_message = "invalid comment; missing closing '*/'";
  6887. return false;
  6888. }
  6889. case '*':
  6890. {
  6891. switch (get())
  6892. {
  6893. case '/':
  6894. return true;
  6895. default:
  6896. {
  6897. unget();
  6898. continue;
  6899. }
  6900. }
  6901. }
  6902. default:
  6903. continue;
  6904. }
  6905. }
  6906. }
  6907. // unexpected character after reading '/'
  6908. default:
  6909. {
  6910. error_message = "invalid comment; expecting '/' or '*' after '/'";
  6911. return false;
  6912. }
  6913. }
  6914. }
  6915. JSON_HEDLEY_NON_NULL(2)
  6916. static void strtof(float& f, const char* str, char** endptr) noexcept
  6917. {
  6918. f = std::strtof(str, endptr);
  6919. }
  6920. JSON_HEDLEY_NON_NULL(2)
  6921. static void strtof(double& f, const char* str, char** endptr) noexcept
  6922. {
  6923. f = std::strtod(str, endptr);
  6924. }
  6925. JSON_HEDLEY_NON_NULL(2)
  6926. static void strtof(long double& f, const char* str, char** endptr) noexcept
  6927. {
  6928. f = std::strtold(str, endptr);
  6929. }
  6930. /*!
  6931. @brief scan a number literal
  6932. This function scans a string according to Sect. 6 of RFC 8259.
  6933. The function is realized with a deterministic finite state machine derived
  6934. from the grammar described in RFC 8259. Starting in state "init", the
  6935. input is read and used to determined the next state. Only state "done"
  6936. accepts the number. State "error" is a trap state to model errors. In the
  6937. table below, "anything" means any character but the ones listed before.
  6938. state | 0 | 1-9 | e E | + | - | . | anything
  6939. ---------|----------|----------|----------|---------|---------|----------|-----------
  6940. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  6941. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  6942. zero | done | done | exponent | done | done | decimal1 | done
  6943. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  6944. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  6945. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  6946. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  6947. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  6948. any2 | any2 | any2 | done | done | done | done | done
  6949. The state machine is realized with one label per state (prefixed with
  6950. "scan_number_") and `goto` statements between them. The state machine
  6951. contains cycles, but any cycle can be left when EOF is read. Therefore,
  6952. the function is guaranteed to terminate.
  6953. During scanning, the read bytes are stored in token_buffer. This string is
  6954. then converted to a signed integer, an unsigned integer, or a
  6955. floating-point number.
  6956. @return token_type::value_unsigned, token_type::value_integer, or
  6957. token_type::value_float if number could be successfully scanned,
  6958. token_type::parse_error otherwise
  6959. @note The scanner is independent of the current locale. Internally, the
  6960. locale's decimal point is used instead of `.` to work with the
  6961. locale-dependent converters.
  6962. */
  6963. token_type scan_number() // lgtm [cpp/use-of-goto] `goto` is used in this function to implement the number-parsing state machine described above. By design, any finite input will eventually reach the "done" state or return token_type::parse_error. In each intermediate state, 1 byte of the input is appended to the token_buffer vector, and only the already initialized variables token_buffer, number_type, and error_message are manipulated.
  6964. {
  6965. // reset token_buffer to store the number's bytes
  6966. reset();
  6967. // the type of the parsed number; initially set to unsigned; will be
  6968. // changed if minus sign, decimal point or exponent is read
  6969. token_type number_type = token_type::value_unsigned;
  6970. // state (init): we just found out we need to scan a number
  6971. switch (current)
  6972. {
  6973. case '-':
  6974. {
  6975. add(current);
  6976. goto scan_number_minus;
  6977. }
  6978. case '0':
  6979. {
  6980. add(current);
  6981. goto scan_number_zero;
  6982. }
  6983. case '1':
  6984. case '2':
  6985. case '3':
  6986. case '4':
  6987. case '5':
  6988. case '6':
  6989. case '7':
  6990. case '8':
  6991. case '9':
  6992. {
  6993. add(current);
  6994. goto scan_number_any1;
  6995. }
  6996. // all other characters are rejected outside scan_number()
  6997. default: // LCOV_EXCL_LINE
  6998. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  6999. }
  7000. scan_number_minus:
  7001. // state: we just parsed a leading minus sign
  7002. number_type = token_type::value_integer;
  7003. switch (get())
  7004. {
  7005. case '0':
  7006. {
  7007. add(current);
  7008. goto scan_number_zero;
  7009. }
  7010. case '1':
  7011. case '2':
  7012. case '3':
  7013. case '4':
  7014. case '5':
  7015. case '6':
  7016. case '7':
  7017. case '8':
  7018. case '9':
  7019. {
  7020. add(current);
  7021. goto scan_number_any1;
  7022. }
  7023. default:
  7024. {
  7025. error_message = "invalid number; expected digit after '-'";
  7026. return token_type::parse_error;
  7027. }
  7028. }
  7029. scan_number_zero:
  7030. // state: we just parse a zero (maybe with a leading minus sign)
  7031. switch (get())
  7032. {
  7033. case '.':
  7034. {
  7035. add(decimal_point_char);
  7036. decimal_point_position = token_buffer.size() - 1;
  7037. goto scan_number_decimal1;
  7038. }
  7039. case 'e':
  7040. case 'E':
  7041. {
  7042. add(current);
  7043. goto scan_number_exponent;
  7044. }
  7045. default:
  7046. goto scan_number_done;
  7047. }
  7048. scan_number_any1:
  7049. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7050. switch (get())
  7051. {
  7052. case '0':
  7053. case '1':
  7054. case '2':
  7055. case '3':
  7056. case '4':
  7057. case '5':
  7058. case '6':
  7059. case '7':
  7060. case '8':
  7061. case '9':
  7062. {
  7063. add(current);
  7064. goto scan_number_any1;
  7065. }
  7066. case '.':
  7067. {
  7068. add(decimal_point_char);
  7069. decimal_point_position = token_buffer.size() - 1;
  7070. goto scan_number_decimal1;
  7071. }
  7072. case 'e':
  7073. case 'E':
  7074. {
  7075. add(current);
  7076. goto scan_number_exponent;
  7077. }
  7078. default:
  7079. goto scan_number_done;
  7080. }
  7081. scan_number_decimal1:
  7082. // state: we just parsed a decimal point
  7083. number_type = token_type::value_float;
  7084. switch (get())
  7085. {
  7086. case '0':
  7087. case '1':
  7088. case '2':
  7089. case '3':
  7090. case '4':
  7091. case '5':
  7092. case '6':
  7093. case '7':
  7094. case '8':
  7095. case '9':
  7096. {
  7097. add(current);
  7098. goto scan_number_decimal2;
  7099. }
  7100. default:
  7101. {
  7102. error_message = "invalid number; expected digit after '.'";
  7103. return token_type::parse_error;
  7104. }
  7105. }
  7106. scan_number_decimal2:
  7107. // we just parsed at least one number after a decimal point
  7108. switch (get())
  7109. {
  7110. case '0':
  7111. case '1':
  7112. case '2':
  7113. case '3':
  7114. case '4':
  7115. case '5':
  7116. case '6':
  7117. case '7':
  7118. case '8':
  7119. case '9':
  7120. {
  7121. add(current);
  7122. goto scan_number_decimal2;
  7123. }
  7124. case 'e':
  7125. case 'E':
  7126. {
  7127. add(current);
  7128. goto scan_number_exponent;
  7129. }
  7130. default:
  7131. goto scan_number_done;
  7132. }
  7133. scan_number_exponent:
  7134. // we just parsed an exponent
  7135. number_type = token_type::value_float;
  7136. switch (get())
  7137. {
  7138. case '+':
  7139. case '-':
  7140. {
  7141. add(current);
  7142. goto scan_number_sign;
  7143. }
  7144. case '0':
  7145. case '1':
  7146. case '2':
  7147. case '3':
  7148. case '4':
  7149. case '5':
  7150. case '6':
  7151. case '7':
  7152. case '8':
  7153. case '9':
  7154. {
  7155. add(current);
  7156. goto scan_number_any2;
  7157. }
  7158. default:
  7159. {
  7160. error_message =
  7161. "invalid number; expected '+', '-', or digit after exponent";
  7162. return token_type::parse_error;
  7163. }
  7164. }
  7165. scan_number_sign:
  7166. // we just parsed an exponent sign
  7167. switch (get())
  7168. {
  7169. case '0':
  7170. case '1':
  7171. case '2':
  7172. case '3':
  7173. case '4':
  7174. case '5':
  7175. case '6':
  7176. case '7':
  7177. case '8':
  7178. case '9':
  7179. {
  7180. add(current);
  7181. goto scan_number_any2;
  7182. }
  7183. default:
  7184. {
  7185. error_message = "invalid number; expected digit after exponent sign";
  7186. return token_type::parse_error;
  7187. }
  7188. }
  7189. scan_number_any2:
  7190. // we just parsed a number after the exponent or exponent sign
  7191. switch (get())
  7192. {
  7193. case '0':
  7194. case '1':
  7195. case '2':
  7196. case '3':
  7197. case '4':
  7198. case '5':
  7199. case '6':
  7200. case '7':
  7201. case '8':
  7202. case '9':
  7203. {
  7204. add(current);
  7205. goto scan_number_any2;
  7206. }
  7207. default:
  7208. goto scan_number_done;
  7209. }
  7210. scan_number_done:
  7211. // unget the character after the number (we only read it to know that
  7212. // we are done scanning a number)
  7213. unget();
  7214. char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7215. errno = 0;
  7216. // try to parse integers first and fall back to floats
  7217. if (number_type == token_type::value_unsigned)
  7218. {
  7219. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7220. // we checked the number format before
  7221. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7222. if (errno != ERANGE)
  7223. {
  7224. value_unsigned = static_cast<number_unsigned_t>(x);
  7225. if (value_unsigned == x)
  7226. {
  7227. return token_type::value_unsigned;
  7228. }
  7229. }
  7230. }
  7231. else if (number_type == token_type::value_integer)
  7232. {
  7233. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7234. // we checked the number format before
  7235. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7236. if (errno != ERANGE)
  7237. {
  7238. value_integer = static_cast<number_integer_t>(x);
  7239. if (value_integer == x)
  7240. {
  7241. return token_type::value_integer;
  7242. }
  7243. }
  7244. }
  7245. // this code is reached if we parse a floating-point number or if an
  7246. // integer conversion above failed
  7247. strtof(value_float, token_buffer.data(), &endptr);
  7248. // we checked the number format before
  7249. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7250. return token_type::value_float;
  7251. }
  7252. /*!
  7253. @param[in] literal_text the literal text to expect
  7254. @param[in] length the length of the passed literal text
  7255. @param[in] return_type the token type to return on success
  7256. */
  7257. JSON_HEDLEY_NON_NULL(2)
  7258. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7259. token_type return_type)
  7260. {
  7261. JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7262. for (std::size_t i = 1; i < length; ++i)
  7263. {
  7264. if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7265. {
  7266. error_message = "invalid literal";
  7267. return token_type::parse_error;
  7268. }
  7269. }
  7270. return return_type;
  7271. }
  7272. /////////////////////
  7273. // input management
  7274. /////////////////////
  7275. /// reset token_buffer; current character is beginning of token
  7276. void reset() noexcept
  7277. {
  7278. token_buffer.clear();
  7279. token_string.clear();
  7280. decimal_point_position = std::string::npos;
  7281. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7282. }
  7283. /*
  7284. @brief get next character from the input
  7285. This function provides the interface to the used input adapter. It does
  7286. not throw in case the input reached EOF, but returns a
  7287. `char_traits<char>::eof()` in that case. Stores the scanned characters
  7288. for use in error messages.
  7289. @return character read from the input
  7290. */
  7291. char_int_type get()
  7292. {
  7293. ++position.chars_read_total;
  7294. ++position.chars_read_current_line;
  7295. if (next_unget)
  7296. {
  7297. // just reset the next_unget variable and work with current
  7298. next_unget = false;
  7299. }
  7300. else
  7301. {
  7302. current = ia.get_character();
  7303. }
  7304. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7305. {
  7306. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7307. }
  7308. if (current == '\n')
  7309. {
  7310. ++position.lines_read;
  7311. position.chars_read_current_line = 0;
  7312. }
  7313. return current;
  7314. }
  7315. /*!
  7316. @brief unget current character (read it again on next get)
  7317. We implement unget by setting variable next_unget to true. The input is not
  7318. changed - we just simulate ungetting by modifying chars_read_total,
  7319. chars_read_current_line, and token_string. The next call to get() will
  7320. behave as if the unget character is read again.
  7321. */
  7322. void unget()
  7323. {
  7324. next_unget = true;
  7325. --position.chars_read_total;
  7326. // in case we "unget" a newline, we have to also decrement the lines_read
  7327. if (position.chars_read_current_line == 0)
  7328. {
  7329. if (position.lines_read > 0)
  7330. {
  7331. --position.lines_read;
  7332. }
  7333. }
  7334. else
  7335. {
  7336. --position.chars_read_current_line;
  7337. }
  7338. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7339. {
  7340. JSON_ASSERT(!token_string.empty());
  7341. token_string.pop_back();
  7342. }
  7343. }
  7344. /// add a character to token_buffer
  7345. void add(char_int_type c)
  7346. {
  7347. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7348. }
  7349. public:
  7350. /////////////////////
  7351. // value getters
  7352. /////////////////////
  7353. /// return integer value
  7354. constexpr number_integer_t get_number_integer() const noexcept
  7355. {
  7356. return value_integer;
  7357. }
  7358. /// return unsigned integer value
  7359. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7360. {
  7361. return value_unsigned;
  7362. }
  7363. /// return floating-point value
  7364. constexpr number_float_t get_number_float() const noexcept
  7365. {
  7366. return value_float;
  7367. }
  7368. /// return current string value (implicitly resets the token; useful only once)
  7369. string_t& get_string()
  7370. {
  7371. // translate decimal points from locale back to '.' (#4084)
  7372. if (decimal_point_char != '.' && decimal_point_position != std::string::npos)
  7373. {
  7374. token_buffer[decimal_point_position] = '.';
  7375. }
  7376. return token_buffer;
  7377. }
  7378. /////////////////////
  7379. // diagnostics
  7380. /////////////////////
  7381. /// return position of last read token
  7382. constexpr position_t get_position() const noexcept
  7383. {
  7384. return position;
  7385. }
  7386. /// return the last read token (for errors only). Will never contain EOF
  7387. /// (an arbitrary value that is not a valid char value, often -1), because
  7388. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7389. std::string get_token_string() const
  7390. {
  7391. // escape control characters
  7392. std::string result;
  7393. for (const auto c : token_string)
  7394. {
  7395. if (static_cast<unsigned char>(c) <= '\x1F')
  7396. {
  7397. // escape control characters
  7398. std::array<char, 9> cs{{}};
  7399. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7400. result += cs.data();
  7401. }
  7402. else
  7403. {
  7404. // add character as is
  7405. result.push_back(static_cast<std::string::value_type>(c));
  7406. }
  7407. }
  7408. return result;
  7409. }
  7410. /// return syntax error message
  7411. JSON_HEDLEY_RETURNS_NON_NULL
  7412. constexpr const char* get_error_message() const noexcept
  7413. {
  7414. return error_message;
  7415. }
  7416. /////////////////////
  7417. // actual scanner
  7418. /////////////////////
  7419. /*!
  7420. @brief skip the UTF-8 byte order mark
  7421. @return true iff there is no BOM or the correct BOM has been skipped
  7422. */
  7423. bool skip_bom()
  7424. {
  7425. if (get() == 0xEF)
  7426. {
  7427. // check if we completely parse the BOM
  7428. return get() == 0xBB && get() == 0xBF;
  7429. }
  7430. // the first character is not the beginning of the BOM; unget it to
  7431. // process is later
  7432. unget();
  7433. return true;
  7434. }
  7435. void skip_whitespace()
  7436. {
  7437. do
  7438. {
  7439. get();
  7440. }
  7441. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7442. }
  7443. token_type scan()
  7444. {
  7445. // initially, skip the BOM
  7446. if (position.chars_read_total == 0 && !skip_bom())
  7447. {
  7448. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7449. return token_type::parse_error;
  7450. }
  7451. // read next character and ignore whitespace
  7452. skip_whitespace();
  7453. // ignore comments
  7454. while (ignore_comments && current == '/')
  7455. {
  7456. if (!scan_comment())
  7457. {
  7458. return token_type::parse_error;
  7459. }
  7460. // skip following whitespace
  7461. skip_whitespace();
  7462. }
  7463. switch (current)
  7464. {
  7465. // structural characters
  7466. case '[':
  7467. return token_type::begin_array;
  7468. case ']':
  7469. return token_type::end_array;
  7470. case '{':
  7471. return token_type::begin_object;
  7472. case '}':
  7473. return token_type::end_object;
  7474. case ':':
  7475. return token_type::name_separator;
  7476. case ',':
  7477. return token_type::value_separator;
  7478. // literals
  7479. case 't':
  7480. {
  7481. std::array<char_type, 4> true_literal = {{static_cast<char_type>('t'), static_cast<char_type>('r'), static_cast<char_type>('u'), static_cast<char_type>('e')}};
  7482. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7483. }
  7484. case 'f':
  7485. {
  7486. std::array<char_type, 5> false_literal = {{static_cast<char_type>('f'), static_cast<char_type>('a'), static_cast<char_type>('l'), static_cast<char_type>('s'), static_cast<char_type>('e')}};
  7487. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7488. }
  7489. case 'n':
  7490. {
  7491. std::array<char_type, 4> null_literal = {{static_cast<char_type>('n'), static_cast<char_type>('u'), static_cast<char_type>('l'), static_cast<char_type>('l')}};
  7492. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7493. }
  7494. // string
  7495. case '\"':
  7496. return scan_string();
  7497. // number
  7498. case '-':
  7499. case '0':
  7500. case '1':
  7501. case '2':
  7502. case '3':
  7503. case '4':
  7504. case '5':
  7505. case '6':
  7506. case '7':
  7507. case '8':
  7508. case '9':
  7509. return scan_number();
  7510. // end of input (the null byte is needed when parsing from
  7511. // string literals)
  7512. case '\0':
  7513. case char_traits<char_type>::eof():
  7514. return token_type::end_of_input;
  7515. // error
  7516. default:
  7517. error_message = "invalid literal";
  7518. return token_type::parse_error;
  7519. }
  7520. }
  7521. private:
  7522. /// input adapter
  7523. InputAdapterType ia;
  7524. /// whether comments should be ignored (true) or signaled as errors (false)
  7525. const bool ignore_comments = false;
  7526. /// the current character
  7527. char_int_type current = char_traits<char_type>::eof();
  7528. /// whether the next get() call should just return current
  7529. bool next_unget = false;
  7530. /// the start position of the current token
  7531. position_t position {};
  7532. /// raw input token string (for error messages)
  7533. std::vector<char_type> token_string {};
  7534. /// buffer for variable-length tokens (numbers, strings)
  7535. string_t token_buffer {};
  7536. /// a description of occurred lexer errors
  7537. const char* error_message = "";
  7538. // number values
  7539. number_integer_t value_integer = 0;
  7540. number_unsigned_t value_unsigned = 0;
  7541. number_float_t value_float = 0;
  7542. /// the decimal point
  7543. const char_int_type decimal_point_char = '.';
  7544. /// the position of the decimal point in the input
  7545. std::size_t decimal_point_position = std::string::npos;
  7546. };
  7547. } // namespace detail
  7548. NLOHMANN_JSON_NAMESPACE_END
  7549. // #include <nlohmann/detail/macro_scope.hpp>
  7550. // #include <nlohmann/detail/string_concat.hpp>
  7551. NLOHMANN_JSON_NAMESPACE_BEGIN
  7552. /*!
  7553. @brief SAX interface
  7554. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  7555. Each function is called in different situations while the input is parsed. The
  7556. boolean return value informs the parser whether to continue processing the
  7557. input.
  7558. */
  7559. template<typename BasicJsonType>
  7560. struct json_sax
  7561. {
  7562. using number_integer_t = typename BasicJsonType::number_integer_t;
  7563. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7564. using number_float_t = typename BasicJsonType::number_float_t;
  7565. using string_t = typename BasicJsonType::string_t;
  7566. using binary_t = typename BasicJsonType::binary_t;
  7567. /*!
  7568. @brief a null value was read
  7569. @return whether parsing should proceed
  7570. */
  7571. virtual bool null() = 0;
  7572. /*!
  7573. @brief a boolean value was read
  7574. @param[in] val boolean value
  7575. @return whether parsing should proceed
  7576. */
  7577. virtual bool boolean(bool val) = 0;
  7578. /*!
  7579. @brief an integer number was read
  7580. @param[in] val integer value
  7581. @return whether parsing should proceed
  7582. */
  7583. virtual bool number_integer(number_integer_t val) = 0;
  7584. /*!
  7585. @brief an unsigned integer number was read
  7586. @param[in] val unsigned integer value
  7587. @return whether parsing should proceed
  7588. */
  7589. virtual bool number_unsigned(number_unsigned_t val) = 0;
  7590. /*!
  7591. @brief a floating-point number was read
  7592. @param[in] val floating-point value
  7593. @param[in] s raw token value
  7594. @return whether parsing should proceed
  7595. */
  7596. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  7597. /*!
  7598. @brief a string value was read
  7599. @param[in] val string value
  7600. @return whether parsing should proceed
  7601. @note It is safe to move the passed string value.
  7602. */
  7603. virtual bool string(string_t& val) = 0;
  7604. /*!
  7605. @brief a binary value was read
  7606. @param[in] val binary value
  7607. @return whether parsing should proceed
  7608. @note It is safe to move the passed binary value.
  7609. */
  7610. virtual bool binary(binary_t& val) = 0;
  7611. /*!
  7612. @brief the beginning of an object was read
  7613. @param[in] elements number of object elements or -1 if unknown
  7614. @return whether parsing should proceed
  7615. @note binary formats may report the number of elements
  7616. */
  7617. virtual bool start_object(std::size_t elements) = 0;
  7618. /*!
  7619. @brief an object key was read
  7620. @param[in] val object key
  7621. @return whether parsing should proceed
  7622. @note It is safe to move the passed string.
  7623. */
  7624. virtual bool key(string_t& val) = 0;
  7625. /*!
  7626. @brief the end of an object was read
  7627. @return whether parsing should proceed
  7628. */
  7629. virtual bool end_object() = 0;
  7630. /*!
  7631. @brief the beginning of an array was read
  7632. @param[in] elements number of array elements or -1 if unknown
  7633. @return whether parsing should proceed
  7634. @note binary formats may report the number of elements
  7635. */
  7636. virtual bool start_array(std::size_t elements) = 0;
  7637. /*!
  7638. @brief the end of an array was read
  7639. @return whether parsing should proceed
  7640. */
  7641. virtual bool end_array() = 0;
  7642. /*!
  7643. @brief a parse error occurred
  7644. @param[in] position the position in the input where the error occurs
  7645. @param[in] last_token the last read token
  7646. @param[in] ex an exception object describing the error
  7647. @return whether parsing should proceed (must return false)
  7648. */
  7649. virtual bool parse_error(std::size_t position,
  7650. const std::string& last_token,
  7651. const detail::exception& ex) = 0;
  7652. json_sax() = default;
  7653. json_sax(const json_sax&) = default;
  7654. json_sax(json_sax&&) noexcept = default;
  7655. json_sax& operator=(const json_sax&) = default;
  7656. json_sax& operator=(json_sax&&) noexcept = default;
  7657. virtual ~json_sax() = default;
  7658. };
  7659. namespace detail
  7660. {
  7661. constexpr std::size_t unknown_size()
  7662. {
  7663. return (std::numeric_limits<std::size_t>::max)();
  7664. }
  7665. /*!
  7666. @brief SAX implementation to create a JSON value from SAX events
  7667. This class implements the @ref json_sax interface and processes the SAX events
  7668. to create a JSON value which makes it basically a DOM parser. The structure or
  7669. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  7670. a pointer to the respective array or object for each recursion depth.
  7671. After successful parsing, the value that is passed by reference to the
  7672. constructor contains the parsed value.
  7673. @tparam BasicJsonType the JSON type
  7674. */
  7675. template<typename BasicJsonType, typename InputAdapterType>
  7676. class json_sax_dom_parser
  7677. {
  7678. public:
  7679. using number_integer_t = typename BasicJsonType::number_integer_t;
  7680. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7681. using number_float_t = typename BasicJsonType::number_float_t;
  7682. using string_t = typename BasicJsonType::string_t;
  7683. using binary_t = typename BasicJsonType::binary_t;
  7684. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  7685. /*!
  7686. @param[in,out] r reference to a JSON value that is manipulated while
  7687. parsing
  7688. @param[in] allow_exceptions_ whether parse errors yield exceptions
  7689. */
  7690. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr)
  7691. : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
  7692. {}
  7693. // make class move-only
  7694. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  7695. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7696. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  7697. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7698. ~json_sax_dom_parser() = default;
  7699. bool null()
  7700. {
  7701. handle_value(nullptr);
  7702. return true;
  7703. }
  7704. bool boolean(bool val)
  7705. {
  7706. handle_value(val);
  7707. return true;
  7708. }
  7709. bool number_integer(number_integer_t val)
  7710. {
  7711. handle_value(val);
  7712. return true;
  7713. }
  7714. bool number_unsigned(number_unsigned_t val)
  7715. {
  7716. handle_value(val);
  7717. return true;
  7718. }
  7719. bool number_float(number_float_t val, const string_t& /*unused*/)
  7720. {
  7721. handle_value(val);
  7722. return true;
  7723. }
  7724. bool string(string_t& val)
  7725. {
  7726. handle_value(val);
  7727. return true;
  7728. }
  7729. bool binary(binary_t& val)
  7730. {
  7731. handle_value(std::move(val));
  7732. return true;
  7733. }
  7734. bool start_object(std::size_t len)
  7735. {
  7736. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  7737. #if JSON_DIAGNOSTIC_POSITIONS
  7738. // Manually set the start position of the object here.
  7739. // Ensure this is after the call to handle_value to ensure correct start position.
  7740. if (m_lexer_ref)
  7741. {
  7742. // Lexer has read the first character of the object, so
  7743. // subtract 1 from the position to get the correct start position.
  7744. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  7745. }
  7746. #endif
  7747. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  7748. {
  7749. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  7750. }
  7751. return true;
  7752. }
  7753. bool key(string_t& val)
  7754. {
  7755. JSON_ASSERT(!ref_stack.empty());
  7756. JSON_ASSERT(ref_stack.back()->is_object());
  7757. // add null at given key and store the reference for later
  7758. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
  7759. return true;
  7760. }
  7761. bool end_object()
  7762. {
  7763. JSON_ASSERT(!ref_stack.empty());
  7764. JSON_ASSERT(ref_stack.back()->is_object());
  7765. #if JSON_DIAGNOSTIC_POSITIONS
  7766. if (m_lexer_ref)
  7767. {
  7768. // Lexer's position is past the closing brace, so set that as the end position.
  7769. ref_stack.back()->end_position = m_lexer_ref->get_position();
  7770. }
  7771. #endif
  7772. ref_stack.back()->set_parents();
  7773. ref_stack.pop_back();
  7774. return true;
  7775. }
  7776. bool start_array(std::size_t len)
  7777. {
  7778. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  7779. #if JSON_DIAGNOSTIC_POSITIONS
  7780. // Manually set the start position of the array here.
  7781. // Ensure this is after the call to handle_value to ensure correct start position.
  7782. if (m_lexer_ref)
  7783. {
  7784. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  7785. }
  7786. #endif
  7787. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  7788. {
  7789. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  7790. }
  7791. return true;
  7792. }
  7793. bool end_array()
  7794. {
  7795. JSON_ASSERT(!ref_stack.empty());
  7796. JSON_ASSERT(ref_stack.back()->is_array());
  7797. #if JSON_DIAGNOSTIC_POSITIONS
  7798. if (m_lexer_ref)
  7799. {
  7800. // Lexer's position is past the closing bracket, so set that as the end position.
  7801. ref_stack.back()->end_position = m_lexer_ref->get_position();
  7802. }
  7803. #endif
  7804. ref_stack.back()->set_parents();
  7805. ref_stack.pop_back();
  7806. return true;
  7807. }
  7808. template<class Exception>
  7809. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  7810. const Exception& ex)
  7811. {
  7812. errored = true;
  7813. static_cast<void>(ex);
  7814. if (allow_exceptions)
  7815. {
  7816. JSON_THROW(ex);
  7817. }
  7818. return false;
  7819. }
  7820. constexpr bool is_errored() const
  7821. {
  7822. return errored;
  7823. }
  7824. private:
  7825. #if JSON_DIAGNOSTIC_POSITIONS
  7826. void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
  7827. {
  7828. if (m_lexer_ref)
  7829. {
  7830. // Lexer has read past the current field value, so set the end position to the current position.
  7831. // The start position will be set below based on the length of the string representation
  7832. // of the value.
  7833. v.end_position = m_lexer_ref->get_position();
  7834. switch (v.type())
  7835. {
  7836. case value_t::boolean:
  7837. {
  7838. // 4 and 5 are the string length of "true" and "false"
  7839. v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
  7840. break;
  7841. }
  7842. case value_t::null:
  7843. {
  7844. // 4 is the string length of "null"
  7845. v.start_position = v.end_position - 4;
  7846. break;
  7847. }
  7848. case value_t::string:
  7849. {
  7850. // include the length of the quotes, which is 2
  7851. v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
  7852. break;
  7853. }
  7854. // As we handle the start and end positions for values created during parsing,
  7855. // we do not expect the following value type to be called. Regardless, set the positions
  7856. // in case this is created manually or through a different constructor. Exclude from lcov
  7857. // since the exact condition of this switch is esoteric.
  7858. // LCOV_EXCL_START
  7859. case value_t::discarded:
  7860. {
  7861. v.end_position = std::string::npos;
  7862. v.start_position = v.end_position;
  7863. break;
  7864. }
  7865. // LCOV_EXCL_STOP
  7866. case value_t::binary:
  7867. case value_t::number_integer:
  7868. case value_t::number_unsigned:
  7869. case value_t::number_float:
  7870. {
  7871. v.start_position = v.end_position - m_lexer_ref->get_string().size();
  7872. break;
  7873. }
  7874. case value_t::object:
  7875. case value_t::array:
  7876. {
  7877. // object and array are handled in start_object() and start_array() handlers
  7878. // skip setting the values here.
  7879. break;
  7880. }
  7881. default: // LCOV_EXCL_LINE
  7882. // Handle all possible types discretely, default handler should never be reached.
  7883. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
  7884. }
  7885. }
  7886. }
  7887. #endif
  7888. /*!
  7889. @invariant If the ref stack is empty, then the passed value will be the new
  7890. root.
  7891. @invariant If the ref stack contains a value, then it is an array or an
  7892. object to which we can add elements
  7893. */
  7894. template<typename Value>
  7895. JSON_HEDLEY_RETURNS_NON_NULL
  7896. BasicJsonType* handle_value(Value&& v)
  7897. {
  7898. if (ref_stack.empty())
  7899. {
  7900. root = BasicJsonType(std::forward<Value>(v));
  7901. #if JSON_DIAGNOSTIC_POSITIONS
  7902. handle_diagnostic_positions_for_json_value(root);
  7903. #endif
  7904. return &root;
  7905. }
  7906. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  7907. if (ref_stack.back()->is_array())
  7908. {
  7909. ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
  7910. #if JSON_DIAGNOSTIC_POSITIONS
  7911. handle_diagnostic_positions_for_json_value(ref_stack.back()->m_data.m_value.array->back());
  7912. #endif
  7913. return &(ref_stack.back()->m_data.m_value.array->back());
  7914. }
  7915. JSON_ASSERT(ref_stack.back()->is_object());
  7916. JSON_ASSERT(object_element);
  7917. *object_element = BasicJsonType(std::forward<Value>(v));
  7918. #if JSON_DIAGNOSTIC_POSITIONS
  7919. handle_diagnostic_positions_for_json_value(*object_element);
  7920. #endif
  7921. return object_element;
  7922. }
  7923. /// the parsed JSON value
  7924. BasicJsonType& root;
  7925. /// stack to model hierarchy of values
  7926. std::vector<BasicJsonType*> ref_stack {};
  7927. /// helper to hold the reference for the next object element
  7928. BasicJsonType* object_element = nullptr;
  7929. /// whether a syntax error occurred
  7930. bool errored = false;
  7931. /// whether to throw exceptions in case of errors
  7932. const bool allow_exceptions = true;
  7933. /// the lexer reference to obtain the current position
  7934. lexer_t* m_lexer_ref = nullptr;
  7935. };
  7936. template<typename BasicJsonType, typename InputAdapterType>
  7937. class json_sax_dom_callback_parser
  7938. {
  7939. public:
  7940. using number_integer_t = typename BasicJsonType::number_integer_t;
  7941. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7942. using number_float_t = typename BasicJsonType::number_float_t;
  7943. using string_t = typename BasicJsonType::string_t;
  7944. using binary_t = typename BasicJsonType::binary_t;
  7945. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  7946. using parse_event_t = typename BasicJsonType::parse_event_t;
  7947. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  7948. json_sax_dom_callback_parser(BasicJsonType& r,
  7949. parser_callback_t cb,
  7950. const bool allow_exceptions_ = true,
  7951. lexer_t* lexer_ = nullptr)
  7952. : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
  7953. {
  7954. keep_stack.push_back(true);
  7955. }
  7956. // make class move-only
  7957. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  7958. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7959. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  7960. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7961. ~json_sax_dom_callback_parser() = default;
  7962. bool null()
  7963. {
  7964. handle_value(nullptr);
  7965. return true;
  7966. }
  7967. bool boolean(bool val)
  7968. {
  7969. handle_value(val);
  7970. return true;
  7971. }
  7972. bool number_integer(number_integer_t val)
  7973. {
  7974. handle_value(val);
  7975. return true;
  7976. }
  7977. bool number_unsigned(number_unsigned_t val)
  7978. {
  7979. handle_value(val);
  7980. return true;
  7981. }
  7982. bool number_float(number_float_t val, const string_t& /*unused*/)
  7983. {
  7984. handle_value(val);
  7985. return true;
  7986. }
  7987. bool string(string_t& val)
  7988. {
  7989. handle_value(val);
  7990. return true;
  7991. }
  7992. bool binary(binary_t& val)
  7993. {
  7994. handle_value(std::move(val));
  7995. return true;
  7996. }
  7997. bool start_object(std::size_t len)
  7998. {
  7999. // check callback for object start
  8000. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  8001. keep_stack.push_back(keep);
  8002. auto val = handle_value(BasicJsonType::value_t::object, true);
  8003. ref_stack.push_back(val.second);
  8004. if (ref_stack.back())
  8005. {
  8006. #if JSON_DIAGNOSTIC_POSITIONS
  8007. // Manually set the start position of the object here.
  8008. // Ensure this is after the call to handle_value to ensure correct start position.
  8009. if (m_lexer_ref)
  8010. {
  8011. // Lexer has read the first character of the object, so
  8012. // subtract 1 from the position to get the correct start position.
  8013. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  8014. }
  8015. #endif
  8016. // check object limit
  8017. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  8018. {
  8019. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  8020. }
  8021. }
  8022. return true;
  8023. }
  8024. bool key(string_t& val)
  8025. {
  8026. BasicJsonType k = BasicJsonType(val);
  8027. // check callback for key
  8028. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  8029. key_keep_stack.push_back(keep);
  8030. // add discarded value at given key and store the reference for later
  8031. if (keep && ref_stack.back())
  8032. {
  8033. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
  8034. }
  8035. return true;
  8036. }
  8037. bool end_object()
  8038. {
  8039. if (ref_stack.back())
  8040. {
  8041. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  8042. {
  8043. // discard object
  8044. *ref_stack.back() = discarded;
  8045. #if JSON_DIAGNOSTIC_POSITIONS
  8046. // Set start/end positions for discarded object.
  8047. handle_diagnostic_positions_for_json_value(*ref_stack.back());
  8048. #endif
  8049. }
  8050. else
  8051. {
  8052. #if JSON_DIAGNOSTIC_POSITIONS
  8053. if (m_lexer_ref)
  8054. {
  8055. // Lexer's position is past the closing brace, so set that as the end position.
  8056. ref_stack.back()->end_position = m_lexer_ref->get_position();
  8057. }
  8058. #endif
  8059. ref_stack.back()->set_parents();
  8060. }
  8061. }
  8062. JSON_ASSERT(!ref_stack.empty());
  8063. JSON_ASSERT(!keep_stack.empty());
  8064. ref_stack.pop_back();
  8065. keep_stack.pop_back();
  8066. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  8067. {
  8068. // remove discarded value
  8069. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  8070. {
  8071. if (it->is_discarded())
  8072. {
  8073. ref_stack.back()->erase(it);
  8074. break;
  8075. }
  8076. }
  8077. }
  8078. return true;
  8079. }
  8080. bool start_array(std::size_t len)
  8081. {
  8082. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  8083. keep_stack.push_back(keep);
  8084. auto val = handle_value(BasicJsonType::value_t::array, true);
  8085. ref_stack.push_back(val.second);
  8086. if (ref_stack.back())
  8087. {
  8088. #if JSON_DIAGNOSTIC_POSITIONS
  8089. // Manually set the start position of the array here.
  8090. // Ensure this is after the call to handle_value to ensure correct start position.
  8091. if (m_lexer_ref)
  8092. {
  8093. // Lexer has read the first character of the array, so
  8094. // subtract 1 from the position to get the correct start position.
  8095. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  8096. }
  8097. #endif
  8098. // check array limit
  8099. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  8100. {
  8101. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  8102. }
  8103. }
  8104. return true;
  8105. }
  8106. bool end_array()
  8107. {
  8108. bool keep = true;
  8109. if (ref_stack.back())
  8110. {
  8111. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  8112. if (keep)
  8113. {
  8114. #if JSON_DIAGNOSTIC_POSITIONS
  8115. if (m_lexer_ref)
  8116. {
  8117. // Lexer's position is past the closing bracket, so set that as the end position.
  8118. ref_stack.back()->end_position = m_lexer_ref->get_position();
  8119. }
  8120. #endif
  8121. ref_stack.back()->set_parents();
  8122. }
  8123. else
  8124. {
  8125. // discard array
  8126. *ref_stack.back() = discarded;
  8127. #if JSON_DIAGNOSTIC_POSITIONS
  8128. // Set start/end positions for discarded array.
  8129. handle_diagnostic_positions_for_json_value(*ref_stack.back());
  8130. #endif
  8131. }
  8132. }
  8133. JSON_ASSERT(!ref_stack.empty());
  8134. JSON_ASSERT(!keep_stack.empty());
  8135. ref_stack.pop_back();
  8136. keep_stack.pop_back();
  8137. // remove discarded value
  8138. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  8139. {
  8140. ref_stack.back()->m_data.m_value.array->pop_back();
  8141. }
  8142. return true;
  8143. }
  8144. template<class Exception>
  8145. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  8146. const Exception& ex)
  8147. {
  8148. errored = true;
  8149. static_cast<void>(ex);
  8150. if (allow_exceptions)
  8151. {
  8152. JSON_THROW(ex);
  8153. }
  8154. return false;
  8155. }
  8156. constexpr bool is_errored() const
  8157. {
  8158. return errored;
  8159. }
  8160. private:
  8161. #if JSON_DIAGNOSTIC_POSITIONS
  8162. void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
  8163. {
  8164. if (m_lexer_ref)
  8165. {
  8166. // Lexer has read past the current field value, so set the end position to the current position.
  8167. // The start position will be set below based on the length of the string representation
  8168. // of the value.
  8169. v.end_position = m_lexer_ref->get_position();
  8170. switch (v.type())
  8171. {
  8172. case value_t::boolean:
  8173. {
  8174. // 4 and 5 are the string length of "true" and "false"
  8175. v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
  8176. break;
  8177. }
  8178. case value_t::null:
  8179. {
  8180. // 4 is the string length of "null"
  8181. v.start_position = v.end_position - 4;
  8182. break;
  8183. }
  8184. case value_t::string:
  8185. {
  8186. // include the length of the quotes, which is 2
  8187. v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
  8188. break;
  8189. }
  8190. case value_t::discarded:
  8191. {
  8192. v.end_position = std::string::npos;
  8193. v.start_position = v.end_position;
  8194. break;
  8195. }
  8196. case value_t::binary:
  8197. case value_t::number_integer:
  8198. case value_t::number_unsigned:
  8199. case value_t::number_float:
  8200. {
  8201. v.start_position = v.end_position - m_lexer_ref->get_string().size();
  8202. break;
  8203. }
  8204. case value_t::object:
  8205. case value_t::array:
  8206. {
  8207. // object and array are handled in start_object() and start_array() handlers
  8208. // skip setting the values here.
  8209. break;
  8210. }
  8211. default: // LCOV_EXCL_LINE
  8212. // Handle all possible types discretely, default handler should never be reached.
  8213. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
  8214. }
  8215. }
  8216. }
  8217. #endif
  8218. /*!
  8219. @param[in] v value to add to the JSON value we build during parsing
  8220. @param[in] skip_callback whether we should skip calling the callback
  8221. function; this is required after start_array() and
  8222. start_object() SAX events, because otherwise we would call the
  8223. callback function with an empty array or object, respectively.
  8224. @invariant If the ref stack is empty, then the passed value will be the new
  8225. root.
  8226. @invariant If the ref stack contains a value, then it is an array or an
  8227. object to which we can add elements
  8228. @return pair of boolean (whether value should be kept) and pointer (to the
  8229. passed value in the ref_stack hierarchy; nullptr if not kept)
  8230. */
  8231. template<typename Value>
  8232. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  8233. {
  8234. JSON_ASSERT(!keep_stack.empty());
  8235. // do not handle this value if we know it would be added to a discarded
  8236. // container
  8237. if (!keep_stack.back())
  8238. {
  8239. return {false, nullptr};
  8240. }
  8241. // create value
  8242. auto value = BasicJsonType(std::forward<Value>(v));
  8243. #if JSON_DIAGNOSTIC_POSITIONS
  8244. handle_diagnostic_positions_for_json_value(value);
  8245. #endif
  8246. // check callback
  8247. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  8248. // do not handle this value if we just learnt it shall be discarded
  8249. if (!keep)
  8250. {
  8251. return {false, nullptr};
  8252. }
  8253. if (ref_stack.empty())
  8254. {
  8255. root = std::move(value);
  8256. return {true, & root};
  8257. }
  8258. // skip this value if we already decided to skip the parent
  8259. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  8260. if (!ref_stack.back())
  8261. {
  8262. return {false, nullptr};
  8263. }
  8264. // we now only expect arrays and objects
  8265. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  8266. // array
  8267. if (ref_stack.back()->is_array())
  8268. {
  8269. ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
  8270. return {true, & (ref_stack.back()->m_data.m_value.array->back())};
  8271. }
  8272. // object
  8273. JSON_ASSERT(ref_stack.back()->is_object());
  8274. // check if we should store an element for the current key
  8275. JSON_ASSERT(!key_keep_stack.empty());
  8276. const bool store_element = key_keep_stack.back();
  8277. key_keep_stack.pop_back();
  8278. if (!store_element)
  8279. {
  8280. return {false, nullptr};
  8281. }
  8282. JSON_ASSERT(object_element);
  8283. *object_element = std::move(value);
  8284. return {true, object_element};
  8285. }
  8286. /// the parsed JSON value
  8287. BasicJsonType& root;
  8288. /// stack to model hierarchy of values
  8289. std::vector<BasicJsonType*> ref_stack {};
  8290. /// stack to manage which values to keep
  8291. std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
  8292. /// stack to manage which object keys to keep
  8293. std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
  8294. /// helper to hold the reference for the next object element
  8295. BasicJsonType* object_element = nullptr;
  8296. /// whether a syntax error occurred
  8297. bool errored = false;
  8298. /// callback function
  8299. const parser_callback_t callback = nullptr;
  8300. /// whether to throw exceptions in case of errors
  8301. const bool allow_exceptions = true;
  8302. /// a discarded value for the callback
  8303. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  8304. /// the lexer reference to obtain the current position
  8305. lexer_t* m_lexer_ref = nullptr;
  8306. };
  8307. template<typename BasicJsonType>
  8308. class json_sax_acceptor
  8309. {
  8310. public:
  8311. using number_integer_t = typename BasicJsonType::number_integer_t;
  8312. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8313. using number_float_t = typename BasicJsonType::number_float_t;
  8314. using string_t = typename BasicJsonType::string_t;
  8315. using binary_t = typename BasicJsonType::binary_t;
  8316. bool null()
  8317. {
  8318. return true;
  8319. }
  8320. bool boolean(bool /*unused*/)
  8321. {
  8322. return true;
  8323. }
  8324. bool number_integer(number_integer_t /*unused*/)
  8325. {
  8326. return true;
  8327. }
  8328. bool number_unsigned(number_unsigned_t /*unused*/)
  8329. {
  8330. return true;
  8331. }
  8332. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  8333. {
  8334. return true;
  8335. }
  8336. bool string(string_t& /*unused*/)
  8337. {
  8338. return true;
  8339. }
  8340. bool binary(binary_t& /*unused*/)
  8341. {
  8342. return true;
  8343. }
  8344. bool start_object(std::size_t /*unused*/ = detail::unknown_size())
  8345. {
  8346. return true;
  8347. }
  8348. bool key(string_t& /*unused*/)
  8349. {
  8350. return true;
  8351. }
  8352. bool end_object()
  8353. {
  8354. return true;
  8355. }
  8356. bool start_array(std::size_t /*unused*/ = detail::unknown_size())
  8357. {
  8358. return true;
  8359. }
  8360. bool end_array()
  8361. {
  8362. return true;
  8363. }
  8364. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  8365. {
  8366. return false;
  8367. }
  8368. };
  8369. } // namespace detail
  8370. NLOHMANN_JSON_NAMESPACE_END
  8371. // #include <nlohmann/detail/input/lexer.hpp>
  8372. // #include <nlohmann/detail/macro_scope.hpp>
  8373. // #include <nlohmann/detail/meta/is_sax.hpp>
  8374. // __ _____ _____ _____
  8375. // __| | __| | | | JSON for Modern C++
  8376. // | | |__ | | | | | | version 3.12.0
  8377. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  8378. //
  8379. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  8380. // SPDX-License-Identifier: MIT
  8381. #include <cstdint> // size_t
  8382. #include <utility> // declval
  8383. #include <string> // string
  8384. // #include <nlohmann/detail/abi_macros.hpp>
  8385. // #include <nlohmann/detail/meta/detected.hpp>
  8386. // #include <nlohmann/detail/meta/type_traits.hpp>
  8387. NLOHMANN_JSON_NAMESPACE_BEGIN
  8388. namespace detail
  8389. {
  8390. template<typename T>
  8391. using null_function_t = decltype(std::declval<T&>().null());
  8392. template<typename T>
  8393. using boolean_function_t =
  8394. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  8395. template<typename T, typename Integer>
  8396. using number_integer_function_t =
  8397. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  8398. template<typename T, typename Unsigned>
  8399. using number_unsigned_function_t =
  8400. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  8401. template<typename T, typename Float, typename String>
  8402. using number_float_function_t = decltype(std::declval<T&>().number_float(
  8403. std::declval<Float>(), std::declval<const String&>()));
  8404. template<typename T, typename String>
  8405. using string_function_t =
  8406. decltype(std::declval<T&>().string(std::declval<String&>()));
  8407. template<typename T, typename Binary>
  8408. using binary_function_t =
  8409. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  8410. template<typename T>
  8411. using start_object_function_t =
  8412. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  8413. template<typename T, typename String>
  8414. using key_function_t =
  8415. decltype(std::declval<T&>().key(std::declval<String&>()));
  8416. template<typename T>
  8417. using end_object_function_t = decltype(std::declval<T&>().end_object());
  8418. template<typename T>
  8419. using start_array_function_t =
  8420. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  8421. template<typename T>
  8422. using end_array_function_t = decltype(std::declval<T&>().end_array());
  8423. template<typename T, typename Exception>
  8424. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  8425. std::declval<std::size_t>(), std::declval<const std::string&>(),
  8426. std::declval<const Exception&>()));
  8427. template<typename SAX, typename BasicJsonType>
  8428. struct is_sax
  8429. {
  8430. private:
  8431. static_assert(is_basic_json<BasicJsonType>::value,
  8432. "BasicJsonType must be of type basic_json<...>");
  8433. using number_integer_t = typename BasicJsonType::number_integer_t;
  8434. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8435. using number_float_t = typename BasicJsonType::number_float_t;
  8436. using string_t = typename BasicJsonType::string_t;
  8437. using binary_t = typename BasicJsonType::binary_t;
  8438. using exception_t = typename BasicJsonType::exception;
  8439. public:
  8440. static constexpr bool value =
  8441. is_detected_exact<bool, null_function_t, SAX>::value &&
  8442. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  8443. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  8444. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  8445. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  8446. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  8447. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  8448. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  8449. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  8450. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  8451. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  8452. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  8453. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  8454. };
  8455. template<typename SAX, typename BasicJsonType>
  8456. struct is_sax_static_asserts
  8457. {
  8458. private:
  8459. static_assert(is_basic_json<BasicJsonType>::value,
  8460. "BasicJsonType must be of type basic_json<...>");
  8461. using number_integer_t = typename BasicJsonType::number_integer_t;
  8462. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8463. using number_float_t = typename BasicJsonType::number_float_t;
  8464. using string_t = typename BasicJsonType::string_t;
  8465. using binary_t = typename BasicJsonType::binary_t;
  8466. using exception_t = typename BasicJsonType::exception;
  8467. public:
  8468. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  8469. "Missing/invalid function: bool null()");
  8470. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  8471. "Missing/invalid function: bool boolean(bool)");
  8472. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  8473. "Missing/invalid function: bool boolean(bool)");
  8474. static_assert(
  8475. is_detected_exact<bool, number_integer_function_t, SAX,
  8476. number_integer_t>::value,
  8477. "Missing/invalid function: bool number_integer(number_integer_t)");
  8478. static_assert(
  8479. is_detected_exact<bool, number_unsigned_function_t, SAX,
  8480. number_unsigned_t>::value,
  8481. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  8482. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  8483. number_float_t, string_t>::value,
  8484. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  8485. static_assert(
  8486. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  8487. "Missing/invalid function: bool string(string_t&)");
  8488. static_assert(
  8489. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  8490. "Missing/invalid function: bool binary(binary_t&)");
  8491. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  8492. "Missing/invalid function: bool start_object(std::size_t)");
  8493. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  8494. "Missing/invalid function: bool key(string_t&)");
  8495. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  8496. "Missing/invalid function: bool end_object()");
  8497. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  8498. "Missing/invalid function: bool start_array(std::size_t)");
  8499. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  8500. "Missing/invalid function: bool end_array()");
  8501. static_assert(
  8502. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  8503. "Missing/invalid function: bool parse_error(std::size_t, const "
  8504. "std::string&, const exception&)");
  8505. };
  8506. } // namespace detail
  8507. NLOHMANN_JSON_NAMESPACE_END
  8508. // #include <nlohmann/detail/meta/type_traits.hpp>
  8509. // #include <nlohmann/detail/string_concat.hpp>
  8510. // #include <nlohmann/detail/value_t.hpp>
  8511. NLOHMANN_JSON_NAMESPACE_BEGIN
  8512. namespace detail
  8513. {
  8514. /// how to treat CBOR tags
  8515. enum class cbor_tag_handler_t
  8516. {
  8517. error, ///< throw a parse_error exception in case of a tag
  8518. ignore, ///< ignore tags
  8519. store ///< store tags as binary type
  8520. };
  8521. /*!
  8522. @brief determine system byte order
  8523. @return true if and only if system's byte order is little endian
  8524. @note from https://stackoverflow.com/a/1001328/266378
  8525. */
  8526. static inline bool little_endianness(int num = 1) noexcept
  8527. {
  8528. return *reinterpret_cast<char*>(&num) == 1;
  8529. }
  8530. ///////////////////
  8531. // binary reader //
  8532. ///////////////////
  8533. /*!
  8534. @brief deserialization of CBOR, MessagePack, and UBJSON values
  8535. */
  8536. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
  8537. class binary_reader
  8538. {
  8539. using number_integer_t = typename BasicJsonType::number_integer_t;
  8540. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8541. using number_float_t = typename BasicJsonType::number_float_t;
  8542. using string_t = typename BasicJsonType::string_t;
  8543. using binary_t = typename BasicJsonType::binary_t;
  8544. using json_sax_t = SAX;
  8545. using char_type = typename InputAdapterType::char_type;
  8546. using char_int_type = typename char_traits<char_type>::int_type;
  8547. public:
  8548. /*!
  8549. @brief create a binary reader
  8550. @param[in] adapter input adapter to read from
  8551. */
  8552. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  8553. {
  8554. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  8555. }
  8556. // make class move-only
  8557. binary_reader(const binary_reader&) = delete;
  8558. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  8559. binary_reader& operator=(const binary_reader&) = delete;
  8560. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  8561. ~binary_reader() = default;
  8562. /*!
  8563. @param[in] format the binary format to parse
  8564. @param[in] sax_ a SAX event processor
  8565. @param[in] strict whether to expect the input to be consumed completed
  8566. @param[in] tag_handler how to treat CBOR tags
  8567. @return whether parsing was successful
  8568. */
  8569. JSON_HEDLEY_NON_NULL(3)
  8570. bool sax_parse(const input_format_t format,
  8571. json_sax_t* sax_,
  8572. const bool strict = true,
  8573. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  8574. {
  8575. sax = sax_;
  8576. bool result = false;
  8577. switch (format)
  8578. {
  8579. case input_format_t::bson:
  8580. result = parse_bson_internal();
  8581. break;
  8582. case input_format_t::cbor:
  8583. result = parse_cbor_internal(true, tag_handler);
  8584. break;
  8585. case input_format_t::msgpack:
  8586. result = parse_msgpack_internal();
  8587. break;
  8588. case input_format_t::ubjson:
  8589. case input_format_t::bjdata:
  8590. result = parse_ubjson_internal();
  8591. break;
  8592. case input_format_t::json: // LCOV_EXCL_LINE
  8593. default: // LCOV_EXCL_LINE
  8594. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8595. }
  8596. // strict mode: next byte must be EOF
  8597. if (result && strict)
  8598. {
  8599. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  8600. {
  8601. get_ignore_noop();
  8602. }
  8603. else
  8604. {
  8605. get();
  8606. }
  8607. if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
  8608. {
  8609. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  8610. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  8611. }
  8612. }
  8613. return result;
  8614. }
  8615. private:
  8616. //////////
  8617. // BSON //
  8618. //////////
  8619. /*!
  8620. @brief Reads in a BSON-object and passes it to the SAX-parser.
  8621. @return whether a valid BSON-value was passed to the SAX parser
  8622. */
  8623. bool parse_bson_internal()
  8624. {
  8625. std::int32_t document_size{};
  8626. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8627. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
  8628. {
  8629. return false;
  8630. }
  8631. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8632. {
  8633. return false;
  8634. }
  8635. return sax->end_object();
  8636. }
  8637. /*!
  8638. @brief Parses a C-style string from the BSON input.
  8639. @param[in,out] result A reference to the string variable where the read
  8640. string is to be stored.
  8641. @return `true` if the \x00-byte indicating the end of the string was
  8642. encountered before the EOF; false` indicates an unexpected EOF.
  8643. */
  8644. bool get_bson_cstr(string_t& result)
  8645. {
  8646. auto out = std::back_inserter(result);
  8647. while (true)
  8648. {
  8649. get();
  8650. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8651. {
  8652. return false;
  8653. }
  8654. if (current == 0x00)
  8655. {
  8656. return true;
  8657. }
  8658. *out++ = static_cast<typename string_t::value_type>(current);
  8659. }
  8660. }
  8661. /*!
  8662. @brief Parses a zero-terminated string of length @a len from the BSON
  8663. input.
  8664. @param[in] len The length (including the zero-byte at the end) of the
  8665. string to be read.
  8666. @param[in,out] result A reference to the string variable where the read
  8667. string is to be stored.
  8668. @tparam NumberType The type of the length @a len
  8669. @pre len >= 1
  8670. @return `true` if the string was successfully parsed
  8671. */
  8672. template<typename NumberType>
  8673. bool get_bson_string(const NumberType len, string_t& result)
  8674. {
  8675. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8676. {
  8677. auto last_token = get_token_string();
  8678. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8679. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8680. }
  8681. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
  8682. }
  8683. /*!
  8684. @brief Parses a byte array input of length @a len from the BSON input.
  8685. @param[in] len The length of the byte array to be read.
  8686. @param[in,out] result A reference to the binary variable where the read
  8687. array is to be stored.
  8688. @tparam NumberType The type of the length @a len
  8689. @pre len >= 0
  8690. @return `true` if the byte array was successfully parsed
  8691. */
  8692. template<typename NumberType>
  8693. bool get_bson_binary(const NumberType len, binary_t& result)
  8694. {
  8695. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8696. {
  8697. auto last_token = get_token_string();
  8698. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8699. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8700. }
  8701. // All BSON binary values have a subtype
  8702. std::uint8_t subtype{};
  8703. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8704. result.set_subtype(subtype);
  8705. return get_binary(input_format_t::bson, len, result);
  8706. }
  8707. /*!
  8708. @brief Read a BSON document element of the given @a element_type.
  8709. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8710. @param[in] element_type_parse_position The position in the input stream,
  8711. where the `element_type` was read.
  8712. @warning Not all BSON element types are supported yet. An unsupported
  8713. @a element_type will give rise to a parse_error.114:
  8714. Unsupported BSON record type 0x...
  8715. @return whether a valid BSON-object/array was passed to the SAX parser
  8716. */
  8717. bool parse_bson_element_internal(const char_int_type element_type,
  8718. const std::size_t element_type_parse_position)
  8719. {
  8720. switch (element_type)
  8721. {
  8722. case 0x01: // double
  8723. {
  8724. double number{};
  8725. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8726. }
  8727. case 0x02: // string
  8728. {
  8729. std::int32_t len{};
  8730. string_t value;
  8731. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8732. }
  8733. case 0x03: // object
  8734. {
  8735. return parse_bson_internal();
  8736. }
  8737. case 0x04: // array
  8738. {
  8739. return parse_bson_array();
  8740. }
  8741. case 0x05: // binary
  8742. {
  8743. std::int32_t len{};
  8744. binary_t value;
  8745. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8746. }
  8747. case 0x08: // boolean
  8748. {
  8749. return sax->boolean(get() != 0);
  8750. }
  8751. case 0x0A: // null
  8752. {
  8753. return sax->null();
  8754. }
  8755. case 0x10: // int32
  8756. {
  8757. std::int32_t value{};
  8758. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8759. }
  8760. case 0x12: // int64
  8761. {
  8762. std::int64_t value{};
  8763. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8764. }
  8765. case 0x11: // uint64
  8766. {
  8767. std::uint64_t value{};
  8768. return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
  8769. }
  8770. default: // anything else not supported (yet)
  8771. {
  8772. std::array<char, 3> cr{{}};
  8773. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8774. const std::string cr_str{cr.data()};
  8775. return sax->parse_error(element_type_parse_position, cr_str,
  8776. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8777. }
  8778. }
  8779. }
  8780. /*!
  8781. @brief Read a BSON element list (as specified in the BSON-spec)
  8782. The same binary layout is used for objects and arrays, hence it must be
  8783. indicated with the argument @a is_array which one is expected
  8784. (true --> array, false --> object).
  8785. @param[in] is_array Determines if the element list being read is to be
  8786. treated as an object (@a is_array == false), or as an
  8787. array (@a is_array == true).
  8788. @return whether a valid BSON-object/array was passed to the SAX parser
  8789. */
  8790. bool parse_bson_element_list(const bool is_array)
  8791. {
  8792. string_t key;
  8793. while (auto element_type = get())
  8794. {
  8795. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8796. {
  8797. return false;
  8798. }
  8799. const std::size_t element_type_parse_position = chars_read;
  8800. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8801. {
  8802. return false;
  8803. }
  8804. if (!is_array && !sax->key(key))
  8805. {
  8806. return false;
  8807. }
  8808. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8809. {
  8810. return false;
  8811. }
  8812. // get_bson_cstr only appends
  8813. key.clear();
  8814. }
  8815. return true;
  8816. }
  8817. /*!
  8818. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8819. @return whether a valid BSON-array was passed to the SAX parser
  8820. */
  8821. bool parse_bson_array()
  8822. {
  8823. std::int32_t document_size{};
  8824. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8825. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
  8826. {
  8827. return false;
  8828. }
  8829. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8830. {
  8831. return false;
  8832. }
  8833. return sax->end_array();
  8834. }
  8835. //////////
  8836. // CBOR //
  8837. //////////
  8838. /*!
  8839. @param[in] get_char whether a new character should be retrieved from the
  8840. input (true) or whether the last read character should
  8841. be considered instead (false)
  8842. @param[in] tag_handler how CBOR tags should be treated
  8843. @return whether a valid CBOR value was passed to the SAX parser
  8844. */
  8845. bool parse_cbor_internal(const bool get_char,
  8846. const cbor_tag_handler_t tag_handler)
  8847. {
  8848. switch (get_char ? get() : current)
  8849. {
  8850. // EOF
  8851. case char_traits<char_type>::eof():
  8852. return unexpect_eof(input_format_t::cbor, "value");
  8853. // Integer 0x00..0x17 (0..23)
  8854. case 0x00:
  8855. case 0x01:
  8856. case 0x02:
  8857. case 0x03:
  8858. case 0x04:
  8859. case 0x05:
  8860. case 0x06:
  8861. case 0x07:
  8862. case 0x08:
  8863. case 0x09:
  8864. case 0x0A:
  8865. case 0x0B:
  8866. case 0x0C:
  8867. case 0x0D:
  8868. case 0x0E:
  8869. case 0x0F:
  8870. case 0x10:
  8871. case 0x11:
  8872. case 0x12:
  8873. case 0x13:
  8874. case 0x14:
  8875. case 0x15:
  8876. case 0x16:
  8877. case 0x17:
  8878. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8879. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  8880. {
  8881. std::uint8_t number{};
  8882. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8883. }
  8884. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  8885. {
  8886. std::uint16_t number{};
  8887. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8888. }
  8889. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  8890. {
  8891. std::uint32_t number{};
  8892. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8893. }
  8894. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  8895. {
  8896. std::uint64_t number{};
  8897. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8898. }
  8899. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  8900. case 0x20:
  8901. case 0x21:
  8902. case 0x22:
  8903. case 0x23:
  8904. case 0x24:
  8905. case 0x25:
  8906. case 0x26:
  8907. case 0x27:
  8908. case 0x28:
  8909. case 0x29:
  8910. case 0x2A:
  8911. case 0x2B:
  8912. case 0x2C:
  8913. case 0x2D:
  8914. case 0x2E:
  8915. case 0x2F:
  8916. case 0x30:
  8917. case 0x31:
  8918. case 0x32:
  8919. case 0x33:
  8920. case 0x34:
  8921. case 0x35:
  8922. case 0x36:
  8923. case 0x37:
  8924. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8925. case 0x38: // Negative integer (one-byte uint8_t follows)
  8926. {
  8927. std::uint8_t number{};
  8928. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8929. }
  8930. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8931. {
  8932. std::uint16_t number{};
  8933. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8934. }
  8935. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8936. {
  8937. std::uint32_t number{};
  8938. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8939. }
  8940. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8941. {
  8942. std::uint64_t number{};
  8943. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8944. - static_cast<number_integer_t>(number));
  8945. }
  8946. // Binary data (0x00..0x17 bytes follow)
  8947. case 0x40:
  8948. case 0x41:
  8949. case 0x42:
  8950. case 0x43:
  8951. case 0x44:
  8952. case 0x45:
  8953. case 0x46:
  8954. case 0x47:
  8955. case 0x48:
  8956. case 0x49:
  8957. case 0x4A:
  8958. case 0x4B:
  8959. case 0x4C:
  8960. case 0x4D:
  8961. case 0x4E:
  8962. case 0x4F:
  8963. case 0x50:
  8964. case 0x51:
  8965. case 0x52:
  8966. case 0x53:
  8967. case 0x54:
  8968. case 0x55:
  8969. case 0x56:
  8970. case 0x57:
  8971. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8972. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8973. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8974. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8975. case 0x5F: // Binary data (indefinite length)
  8976. {
  8977. binary_t b;
  8978. return get_cbor_binary(b) && sax->binary(b);
  8979. }
  8980. // UTF-8 string (0x00..0x17 bytes follow)
  8981. case 0x60:
  8982. case 0x61:
  8983. case 0x62:
  8984. case 0x63:
  8985. case 0x64:
  8986. case 0x65:
  8987. case 0x66:
  8988. case 0x67:
  8989. case 0x68:
  8990. case 0x69:
  8991. case 0x6A:
  8992. case 0x6B:
  8993. case 0x6C:
  8994. case 0x6D:
  8995. case 0x6E:
  8996. case 0x6F:
  8997. case 0x70:
  8998. case 0x71:
  8999. case 0x72:
  9000. case 0x73:
  9001. case 0x74:
  9002. case 0x75:
  9003. case 0x76:
  9004. case 0x77:
  9005. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  9006. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  9007. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  9008. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  9009. case 0x7F: // UTF-8 string (indefinite length)
  9010. {
  9011. string_t s;
  9012. return get_cbor_string(s) && sax->string(s);
  9013. }
  9014. // array (0x00..0x17 data items follow)
  9015. case 0x80:
  9016. case 0x81:
  9017. case 0x82:
  9018. case 0x83:
  9019. case 0x84:
  9020. case 0x85:
  9021. case 0x86:
  9022. case 0x87:
  9023. case 0x88:
  9024. case 0x89:
  9025. case 0x8A:
  9026. case 0x8B:
  9027. case 0x8C:
  9028. case 0x8D:
  9029. case 0x8E:
  9030. case 0x8F:
  9031. case 0x90:
  9032. case 0x91:
  9033. case 0x92:
  9034. case 0x93:
  9035. case 0x94:
  9036. case 0x95:
  9037. case 0x96:
  9038. case 0x97:
  9039. return get_cbor_array(
  9040. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  9041. case 0x98: // array (one-byte uint8_t for n follows)
  9042. {
  9043. std::uint8_t len{};
  9044. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  9045. }
  9046. case 0x99: // array (two-byte uint16_t for n follow)
  9047. {
  9048. std::uint16_t len{};
  9049. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  9050. }
  9051. case 0x9A: // array (four-byte uint32_t for n follow)
  9052. {
  9053. std::uint32_t len{};
  9054. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  9055. }
  9056. case 0x9B: // array (eight-byte uint64_t for n follow)
  9057. {
  9058. std::uint64_t len{};
  9059. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  9060. }
  9061. case 0x9F: // array (indefinite length)
  9062. return get_cbor_array(detail::unknown_size(), tag_handler);
  9063. // map (0x00..0x17 pairs of data items follow)
  9064. case 0xA0:
  9065. case 0xA1:
  9066. case 0xA2:
  9067. case 0xA3:
  9068. case 0xA4:
  9069. case 0xA5:
  9070. case 0xA6:
  9071. case 0xA7:
  9072. case 0xA8:
  9073. case 0xA9:
  9074. case 0xAA:
  9075. case 0xAB:
  9076. case 0xAC:
  9077. case 0xAD:
  9078. case 0xAE:
  9079. case 0xAF:
  9080. case 0xB0:
  9081. case 0xB1:
  9082. case 0xB2:
  9083. case 0xB3:
  9084. case 0xB4:
  9085. case 0xB5:
  9086. case 0xB6:
  9087. case 0xB7:
  9088. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  9089. case 0xB8: // map (one-byte uint8_t for n follows)
  9090. {
  9091. std::uint8_t len{};
  9092. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  9093. }
  9094. case 0xB9: // map (two-byte uint16_t for n follow)
  9095. {
  9096. std::uint16_t len{};
  9097. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  9098. }
  9099. case 0xBA: // map (four-byte uint32_t for n follow)
  9100. {
  9101. std::uint32_t len{};
  9102. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  9103. }
  9104. case 0xBB: // map (eight-byte uint64_t for n follow)
  9105. {
  9106. std::uint64_t len{};
  9107. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  9108. }
  9109. case 0xBF: // map (indefinite length)
  9110. return get_cbor_object(detail::unknown_size(), tag_handler);
  9111. case 0xC6: // tagged item
  9112. case 0xC7:
  9113. case 0xC8:
  9114. case 0xC9:
  9115. case 0xCA:
  9116. case 0xCB:
  9117. case 0xCC:
  9118. case 0xCD:
  9119. case 0xCE:
  9120. case 0xCF:
  9121. case 0xD0:
  9122. case 0xD1:
  9123. case 0xD2:
  9124. case 0xD3:
  9125. case 0xD4:
  9126. case 0xD8: // tagged item (1 bytes follow)
  9127. case 0xD9: // tagged item (2 bytes follow)
  9128. case 0xDA: // tagged item (4 bytes follow)
  9129. case 0xDB: // tagged item (8 bytes follow)
  9130. {
  9131. switch (tag_handler)
  9132. {
  9133. case cbor_tag_handler_t::error:
  9134. {
  9135. auto last_token = get_token_string();
  9136. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9137. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9138. }
  9139. case cbor_tag_handler_t::ignore:
  9140. {
  9141. // ignore binary subtype
  9142. switch (current)
  9143. {
  9144. case 0xD8:
  9145. {
  9146. std::uint8_t subtype_to_ignore{};
  9147. get_number(input_format_t::cbor, subtype_to_ignore);
  9148. break;
  9149. }
  9150. case 0xD9:
  9151. {
  9152. std::uint16_t subtype_to_ignore{};
  9153. get_number(input_format_t::cbor, subtype_to_ignore);
  9154. break;
  9155. }
  9156. case 0xDA:
  9157. {
  9158. std::uint32_t subtype_to_ignore{};
  9159. get_number(input_format_t::cbor, subtype_to_ignore);
  9160. break;
  9161. }
  9162. case 0xDB:
  9163. {
  9164. std::uint64_t subtype_to_ignore{};
  9165. get_number(input_format_t::cbor, subtype_to_ignore);
  9166. break;
  9167. }
  9168. default:
  9169. break;
  9170. }
  9171. return parse_cbor_internal(true, tag_handler);
  9172. }
  9173. case cbor_tag_handler_t::store:
  9174. {
  9175. binary_t b;
  9176. // use binary subtype and store in binary container
  9177. switch (current)
  9178. {
  9179. case 0xD8:
  9180. {
  9181. std::uint8_t subtype{};
  9182. get_number(input_format_t::cbor, subtype);
  9183. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9184. break;
  9185. }
  9186. case 0xD9:
  9187. {
  9188. std::uint16_t subtype{};
  9189. get_number(input_format_t::cbor, subtype);
  9190. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9191. break;
  9192. }
  9193. case 0xDA:
  9194. {
  9195. std::uint32_t subtype{};
  9196. get_number(input_format_t::cbor, subtype);
  9197. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9198. break;
  9199. }
  9200. case 0xDB:
  9201. {
  9202. std::uint64_t subtype{};
  9203. get_number(input_format_t::cbor, subtype);
  9204. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9205. break;
  9206. }
  9207. default:
  9208. return parse_cbor_internal(true, tag_handler);
  9209. }
  9210. get();
  9211. return get_cbor_binary(b) && sax->binary(b);
  9212. }
  9213. default: // LCOV_EXCL_LINE
  9214. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  9215. return false; // LCOV_EXCL_LINE
  9216. }
  9217. }
  9218. case 0xF4: // false
  9219. return sax->boolean(false);
  9220. case 0xF5: // true
  9221. return sax->boolean(true);
  9222. case 0xF6: // null
  9223. return sax->null();
  9224. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  9225. {
  9226. const auto byte1_raw = get();
  9227. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  9228. {
  9229. return false;
  9230. }
  9231. const auto byte2_raw = get();
  9232. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  9233. {
  9234. return false;
  9235. }
  9236. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  9237. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  9238. // code from RFC 7049, Appendix D, Figure 3:
  9239. // As half-precision floating-point numbers were only added
  9240. // to IEEE 754 in 2008, today's programming platforms often
  9241. // still only have limited support for them. It is very
  9242. // easy to include at least decoding support for them even
  9243. // without such support. An example of a small decoder for
  9244. // half-precision floating-point numbers in the C language
  9245. // is shown in Fig. 3.
  9246. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  9247. const double val = [&half]
  9248. {
  9249. const int exp = (half >> 10u) & 0x1Fu;
  9250. const unsigned int mant = half & 0x3FFu;
  9251. JSON_ASSERT(0 <= exp&& exp <= 32);
  9252. JSON_ASSERT(mant <= 1024);
  9253. switch (exp)
  9254. {
  9255. case 0:
  9256. return std::ldexp(mant, -24);
  9257. case 31:
  9258. return (mant == 0)
  9259. ? std::numeric_limits<double>::infinity()
  9260. : std::numeric_limits<double>::quiet_NaN();
  9261. default:
  9262. return std::ldexp(mant + 1024, exp - 25);
  9263. }
  9264. }();
  9265. return sax->number_float((half & 0x8000u) != 0
  9266. ? static_cast<number_float_t>(-val)
  9267. : static_cast<number_float_t>(val), "");
  9268. }
  9269. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  9270. {
  9271. float number{};
  9272. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9273. }
  9274. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  9275. {
  9276. double number{};
  9277. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9278. }
  9279. default: // anything else (0xFF is handled inside the other types)
  9280. {
  9281. auto last_token = get_token_string();
  9282. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9283. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9284. }
  9285. }
  9286. }
  9287. /*!
  9288. @brief reads a CBOR string
  9289. This function first reads starting bytes to determine the expected
  9290. string length and then copies this number of bytes into a string.
  9291. Additionally, CBOR's strings with indefinite lengths are supported.
  9292. @param[out] result created string
  9293. @return whether string creation completed
  9294. */
  9295. bool get_cbor_string(string_t& result)
  9296. {
  9297. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  9298. {
  9299. return false;
  9300. }
  9301. switch (current)
  9302. {
  9303. // UTF-8 string (0x00..0x17 bytes follow)
  9304. case 0x60:
  9305. case 0x61:
  9306. case 0x62:
  9307. case 0x63:
  9308. case 0x64:
  9309. case 0x65:
  9310. case 0x66:
  9311. case 0x67:
  9312. case 0x68:
  9313. case 0x69:
  9314. case 0x6A:
  9315. case 0x6B:
  9316. case 0x6C:
  9317. case 0x6D:
  9318. case 0x6E:
  9319. case 0x6F:
  9320. case 0x70:
  9321. case 0x71:
  9322. case 0x72:
  9323. case 0x73:
  9324. case 0x74:
  9325. case 0x75:
  9326. case 0x76:
  9327. case 0x77:
  9328. {
  9329. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  9330. }
  9331. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  9332. {
  9333. std::uint8_t len{};
  9334. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9335. }
  9336. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  9337. {
  9338. std::uint16_t len{};
  9339. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9340. }
  9341. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  9342. {
  9343. std::uint32_t len{};
  9344. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9345. }
  9346. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  9347. {
  9348. std::uint64_t len{};
  9349. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9350. }
  9351. case 0x7F: // UTF-8 string (indefinite length)
  9352. {
  9353. while (get() != 0xFF)
  9354. {
  9355. string_t chunk;
  9356. if (!get_cbor_string(chunk))
  9357. {
  9358. return false;
  9359. }
  9360. result.append(chunk);
  9361. }
  9362. return true;
  9363. }
  9364. default:
  9365. {
  9366. auto last_token = get_token_string();
  9367. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9368. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  9369. }
  9370. }
  9371. }
  9372. /*!
  9373. @brief reads a CBOR byte array
  9374. This function first reads starting bytes to determine the expected
  9375. byte array length and then copies this number of bytes into the byte array.
  9376. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  9377. @param[out] result created byte array
  9378. @return whether byte array creation completed
  9379. */
  9380. bool get_cbor_binary(binary_t& result)
  9381. {
  9382. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  9383. {
  9384. return false;
  9385. }
  9386. switch (current)
  9387. {
  9388. // Binary data (0x00..0x17 bytes follow)
  9389. case 0x40:
  9390. case 0x41:
  9391. case 0x42:
  9392. case 0x43:
  9393. case 0x44:
  9394. case 0x45:
  9395. case 0x46:
  9396. case 0x47:
  9397. case 0x48:
  9398. case 0x49:
  9399. case 0x4A:
  9400. case 0x4B:
  9401. case 0x4C:
  9402. case 0x4D:
  9403. case 0x4E:
  9404. case 0x4F:
  9405. case 0x50:
  9406. case 0x51:
  9407. case 0x52:
  9408. case 0x53:
  9409. case 0x54:
  9410. case 0x55:
  9411. case 0x56:
  9412. case 0x57:
  9413. {
  9414. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  9415. }
  9416. case 0x58: // Binary data (one-byte uint8_t for n follows)
  9417. {
  9418. std::uint8_t len{};
  9419. return get_number(input_format_t::cbor, len) &&
  9420. get_binary(input_format_t::cbor, len, result);
  9421. }
  9422. case 0x59: // Binary data (two-byte uint16_t for n follow)
  9423. {
  9424. std::uint16_t len{};
  9425. return get_number(input_format_t::cbor, len) &&
  9426. get_binary(input_format_t::cbor, len, result);
  9427. }
  9428. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  9429. {
  9430. std::uint32_t len{};
  9431. return get_number(input_format_t::cbor, len) &&
  9432. get_binary(input_format_t::cbor, len, result);
  9433. }
  9434. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  9435. {
  9436. std::uint64_t len{};
  9437. return get_number(input_format_t::cbor, len) &&
  9438. get_binary(input_format_t::cbor, len, result);
  9439. }
  9440. case 0x5F: // Binary data (indefinite length)
  9441. {
  9442. while (get() != 0xFF)
  9443. {
  9444. binary_t chunk;
  9445. if (!get_cbor_binary(chunk))
  9446. {
  9447. return false;
  9448. }
  9449. result.insert(result.end(), chunk.begin(), chunk.end());
  9450. }
  9451. return true;
  9452. }
  9453. default:
  9454. {
  9455. auto last_token = get_token_string();
  9456. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9457. exception_message(input_format_t::cbor, concat("expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x", last_token), "binary"), nullptr));
  9458. }
  9459. }
  9460. }
  9461. /*!
  9462. @param[in] len the length of the array or detail::unknown_size() for an
  9463. array of indefinite size
  9464. @param[in] tag_handler how CBOR tags should be treated
  9465. @return whether array creation completed
  9466. */
  9467. bool get_cbor_array(const std::size_t len,
  9468. const cbor_tag_handler_t tag_handler)
  9469. {
  9470. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9471. {
  9472. return false;
  9473. }
  9474. if (len != detail::unknown_size())
  9475. {
  9476. for (std::size_t i = 0; i < len; ++i)
  9477. {
  9478. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  9479. {
  9480. return false;
  9481. }
  9482. }
  9483. }
  9484. else
  9485. {
  9486. while (get() != 0xFF)
  9487. {
  9488. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  9489. {
  9490. return false;
  9491. }
  9492. }
  9493. }
  9494. return sax->end_array();
  9495. }
  9496. /*!
  9497. @param[in] len the length of the object or detail::unknown_size() for an
  9498. object of indefinite size
  9499. @param[in] tag_handler how CBOR tags should be treated
  9500. @return whether object creation completed
  9501. */
  9502. bool get_cbor_object(const std::size_t len,
  9503. const cbor_tag_handler_t tag_handler)
  9504. {
  9505. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9506. {
  9507. return false;
  9508. }
  9509. if (len != 0)
  9510. {
  9511. string_t key;
  9512. if (len != detail::unknown_size())
  9513. {
  9514. for (std::size_t i = 0; i < len; ++i)
  9515. {
  9516. get();
  9517. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  9518. {
  9519. return false;
  9520. }
  9521. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  9522. {
  9523. return false;
  9524. }
  9525. key.clear();
  9526. }
  9527. }
  9528. else
  9529. {
  9530. while (get() != 0xFF)
  9531. {
  9532. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  9533. {
  9534. return false;
  9535. }
  9536. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  9537. {
  9538. return false;
  9539. }
  9540. key.clear();
  9541. }
  9542. }
  9543. }
  9544. return sax->end_object();
  9545. }
  9546. /////////////
  9547. // MsgPack //
  9548. /////////////
  9549. /*!
  9550. @return whether a valid MessagePack value was passed to the SAX parser
  9551. */
  9552. bool parse_msgpack_internal()
  9553. {
  9554. switch (get())
  9555. {
  9556. // EOF
  9557. case char_traits<char_type>::eof():
  9558. return unexpect_eof(input_format_t::msgpack, "value");
  9559. // positive fixint
  9560. case 0x00:
  9561. case 0x01:
  9562. case 0x02:
  9563. case 0x03:
  9564. case 0x04:
  9565. case 0x05:
  9566. case 0x06:
  9567. case 0x07:
  9568. case 0x08:
  9569. case 0x09:
  9570. case 0x0A:
  9571. case 0x0B:
  9572. case 0x0C:
  9573. case 0x0D:
  9574. case 0x0E:
  9575. case 0x0F:
  9576. case 0x10:
  9577. case 0x11:
  9578. case 0x12:
  9579. case 0x13:
  9580. case 0x14:
  9581. case 0x15:
  9582. case 0x16:
  9583. case 0x17:
  9584. case 0x18:
  9585. case 0x19:
  9586. case 0x1A:
  9587. case 0x1B:
  9588. case 0x1C:
  9589. case 0x1D:
  9590. case 0x1E:
  9591. case 0x1F:
  9592. case 0x20:
  9593. case 0x21:
  9594. case 0x22:
  9595. case 0x23:
  9596. case 0x24:
  9597. case 0x25:
  9598. case 0x26:
  9599. case 0x27:
  9600. case 0x28:
  9601. case 0x29:
  9602. case 0x2A:
  9603. case 0x2B:
  9604. case 0x2C:
  9605. case 0x2D:
  9606. case 0x2E:
  9607. case 0x2F:
  9608. case 0x30:
  9609. case 0x31:
  9610. case 0x32:
  9611. case 0x33:
  9612. case 0x34:
  9613. case 0x35:
  9614. case 0x36:
  9615. case 0x37:
  9616. case 0x38:
  9617. case 0x39:
  9618. case 0x3A:
  9619. case 0x3B:
  9620. case 0x3C:
  9621. case 0x3D:
  9622. case 0x3E:
  9623. case 0x3F:
  9624. case 0x40:
  9625. case 0x41:
  9626. case 0x42:
  9627. case 0x43:
  9628. case 0x44:
  9629. case 0x45:
  9630. case 0x46:
  9631. case 0x47:
  9632. case 0x48:
  9633. case 0x49:
  9634. case 0x4A:
  9635. case 0x4B:
  9636. case 0x4C:
  9637. case 0x4D:
  9638. case 0x4E:
  9639. case 0x4F:
  9640. case 0x50:
  9641. case 0x51:
  9642. case 0x52:
  9643. case 0x53:
  9644. case 0x54:
  9645. case 0x55:
  9646. case 0x56:
  9647. case 0x57:
  9648. case 0x58:
  9649. case 0x59:
  9650. case 0x5A:
  9651. case 0x5B:
  9652. case 0x5C:
  9653. case 0x5D:
  9654. case 0x5E:
  9655. case 0x5F:
  9656. case 0x60:
  9657. case 0x61:
  9658. case 0x62:
  9659. case 0x63:
  9660. case 0x64:
  9661. case 0x65:
  9662. case 0x66:
  9663. case 0x67:
  9664. case 0x68:
  9665. case 0x69:
  9666. case 0x6A:
  9667. case 0x6B:
  9668. case 0x6C:
  9669. case 0x6D:
  9670. case 0x6E:
  9671. case 0x6F:
  9672. case 0x70:
  9673. case 0x71:
  9674. case 0x72:
  9675. case 0x73:
  9676. case 0x74:
  9677. case 0x75:
  9678. case 0x76:
  9679. case 0x77:
  9680. case 0x78:
  9681. case 0x79:
  9682. case 0x7A:
  9683. case 0x7B:
  9684. case 0x7C:
  9685. case 0x7D:
  9686. case 0x7E:
  9687. case 0x7F:
  9688. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9689. // fixmap
  9690. case 0x80:
  9691. case 0x81:
  9692. case 0x82:
  9693. case 0x83:
  9694. case 0x84:
  9695. case 0x85:
  9696. case 0x86:
  9697. case 0x87:
  9698. case 0x88:
  9699. case 0x89:
  9700. case 0x8A:
  9701. case 0x8B:
  9702. case 0x8C:
  9703. case 0x8D:
  9704. case 0x8E:
  9705. case 0x8F:
  9706. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9707. // fixarray
  9708. case 0x90:
  9709. case 0x91:
  9710. case 0x92:
  9711. case 0x93:
  9712. case 0x94:
  9713. case 0x95:
  9714. case 0x96:
  9715. case 0x97:
  9716. case 0x98:
  9717. case 0x99:
  9718. case 0x9A:
  9719. case 0x9B:
  9720. case 0x9C:
  9721. case 0x9D:
  9722. case 0x9E:
  9723. case 0x9F:
  9724. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9725. // fixstr
  9726. case 0xA0:
  9727. case 0xA1:
  9728. case 0xA2:
  9729. case 0xA3:
  9730. case 0xA4:
  9731. case 0xA5:
  9732. case 0xA6:
  9733. case 0xA7:
  9734. case 0xA8:
  9735. case 0xA9:
  9736. case 0xAA:
  9737. case 0xAB:
  9738. case 0xAC:
  9739. case 0xAD:
  9740. case 0xAE:
  9741. case 0xAF:
  9742. case 0xB0:
  9743. case 0xB1:
  9744. case 0xB2:
  9745. case 0xB3:
  9746. case 0xB4:
  9747. case 0xB5:
  9748. case 0xB6:
  9749. case 0xB7:
  9750. case 0xB8:
  9751. case 0xB9:
  9752. case 0xBA:
  9753. case 0xBB:
  9754. case 0xBC:
  9755. case 0xBD:
  9756. case 0xBE:
  9757. case 0xBF:
  9758. case 0xD9: // str 8
  9759. case 0xDA: // str 16
  9760. case 0xDB: // str 32
  9761. {
  9762. string_t s;
  9763. return get_msgpack_string(s) && sax->string(s);
  9764. }
  9765. case 0xC0: // nil
  9766. return sax->null();
  9767. case 0xC2: // false
  9768. return sax->boolean(false);
  9769. case 0xC3: // true
  9770. return sax->boolean(true);
  9771. case 0xC4: // bin 8
  9772. case 0xC5: // bin 16
  9773. case 0xC6: // bin 32
  9774. case 0xC7: // ext 8
  9775. case 0xC8: // ext 16
  9776. case 0xC9: // ext 32
  9777. case 0xD4: // fixext 1
  9778. case 0xD5: // fixext 2
  9779. case 0xD6: // fixext 4
  9780. case 0xD7: // fixext 8
  9781. case 0xD8: // fixext 16
  9782. {
  9783. binary_t b;
  9784. return get_msgpack_binary(b) && sax->binary(b);
  9785. }
  9786. case 0xCA: // float 32
  9787. {
  9788. float number{};
  9789. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9790. }
  9791. case 0xCB: // float 64
  9792. {
  9793. double number{};
  9794. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9795. }
  9796. case 0xCC: // uint 8
  9797. {
  9798. std::uint8_t number{};
  9799. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9800. }
  9801. case 0xCD: // uint 16
  9802. {
  9803. std::uint16_t number{};
  9804. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9805. }
  9806. case 0xCE: // uint 32
  9807. {
  9808. std::uint32_t number{};
  9809. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9810. }
  9811. case 0xCF: // uint 64
  9812. {
  9813. std::uint64_t number{};
  9814. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9815. }
  9816. case 0xD0: // int 8
  9817. {
  9818. std::int8_t number{};
  9819. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9820. }
  9821. case 0xD1: // int 16
  9822. {
  9823. std::int16_t number{};
  9824. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9825. }
  9826. case 0xD2: // int 32
  9827. {
  9828. std::int32_t number{};
  9829. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9830. }
  9831. case 0xD3: // int 64
  9832. {
  9833. std::int64_t number{};
  9834. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9835. }
  9836. case 0xDC: // array 16
  9837. {
  9838. std::uint16_t len{};
  9839. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9840. }
  9841. case 0xDD: // array 32
  9842. {
  9843. std::uint32_t len{};
  9844. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9845. }
  9846. case 0xDE: // map 16
  9847. {
  9848. std::uint16_t len{};
  9849. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9850. }
  9851. case 0xDF: // map 32
  9852. {
  9853. std::uint32_t len{};
  9854. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9855. }
  9856. // negative fixint
  9857. case 0xE0:
  9858. case 0xE1:
  9859. case 0xE2:
  9860. case 0xE3:
  9861. case 0xE4:
  9862. case 0xE5:
  9863. case 0xE6:
  9864. case 0xE7:
  9865. case 0xE8:
  9866. case 0xE9:
  9867. case 0xEA:
  9868. case 0xEB:
  9869. case 0xEC:
  9870. case 0xED:
  9871. case 0xEE:
  9872. case 0xEF:
  9873. case 0xF0:
  9874. case 0xF1:
  9875. case 0xF2:
  9876. case 0xF3:
  9877. case 0xF4:
  9878. case 0xF5:
  9879. case 0xF6:
  9880. case 0xF7:
  9881. case 0xF8:
  9882. case 0xF9:
  9883. case 0xFA:
  9884. case 0xFB:
  9885. case 0xFC:
  9886. case 0xFD:
  9887. case 0xFE:
  9888. case 0xFF:
  9889. return sax->number_integer(static_cast<std::int8_t>(current));
  9890. default: // anything else
  9891. {
  9892. auto last_token = get_token_string();
  9893. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9894. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9895. }
  9896. }
  9897. }
  9898. /*!
  9899. @brief reads a MessagePack string
  9900. This function first reads starting bytes to determine the expected
  9901. string length and then copies this number of bytes into a string.
  9902. @param[out] result created string
  9903. @return whether string creation completed
  9904. */
  9905. bool get_msgpack_string(string_t& result)
  9906. {
  9907. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  9908. {
  9909. return false;
  9910. }
  9911. switch (current)
  9912. {
  9913. // fixstr
  9914. case 0xA0:
  9915. case 0xA1:
  9916. case 0xA2:
  9917. case 0xA3:
  9918. case 0xA4:
  9919. case 0xA5:
  9920. case 0xA6:
  9921. case 0xA7:
  9922. case 0xA8:
  9923. case 0xA9:
  9924. case 0xAA:
  9925. case 0xAB:
  9926. case 0xAC:
  9927. case 0xAD:
  9928. case 0xAE:
  9929. case 0xAF:
  9930. case 0xB0:
  9931. case 0xB1:
  9932. case 0xB2:
  9933. case 0xB3:
  9934. case 0xB4:
  9935. case 0xB5:
  9936. case 0xB6:
  9937. case 0xB7:
  9938. case 0xB8:
  9939. case 0xB9:
  9940. case 0xBA:
  9941. case 0xBB:
  9942. case 0xBC:
  9943. case 0xBD:
  9944. case 0xBE:
  9945. case 0xBF:
  9946. {
  9947. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9948. }
  9949. case 0xD9: // str 8
  9950. {
  9951. std::uint8_t len{};
  9952. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9953. }
  9954. case 0xDA: // str 16
  9955. {
  9956. std::uint16_t len{};
  9957. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9958. }
  9959. case 0xDB: // str 32
  9960. {
  9961. std::uint32_t len{};
  9962. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9963. }
  9964. default:
  9965. {
  9966. auto last_token = get_token_string();
  9967. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9968. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9969. }
  9970. }
  9971. }
  9972. /*!
  9973. @brief reads a MessagePack byte array
  9974. This function first reads starting bytes to determine the expected
  9975. byte array length and then copies this number of bytes into a byte array.
  9976. @param[out] result created byte array
  9977. @return whether byte array creation completed
  9978. */
  9979. bool get_msgpack_binary(binary_t& result)
  9980. {
  9981. // helper function to set the subtype
  9982. auto assign_and_return_true = [&result](std::int8_t subtype)
  9983. {
  9984. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9985. return true;
  9986. };
  9987. switch (current)
  9988. {
  9989. case 0xC4: // bin 8
  9990. {
  9991. std::uint8_t len{};
  9992. return get_number(input_format_t::msgpack, len) &&
  9993. get_binary(input_format_t::msgpack, len, result);
  9994. }
  9995. case 0xC5: // bin 16
  9996. {
  9997. std::uint16_t len{};
  9998. return get_number(input_format_t::msgpack, len) &&
  9999. get_binary(input_format_t::msgpack, len, result);
  10000. }
  10001. case 0xC6: // bin 32
  10002. {
  10003. std::uint32_t len{};
  10004. return get_number(input_format_t::msgpack, len) &&
  10005. get_binary(input_format_t::msgpack, len, result);
  10006. }
  10007. case 0xC7: // ext 8
  10008. {
  10009. std::uint8_t len{};
  10010. std::int8_t subtype{};
  10011. return get_number(input_format_t::msgpack, len) &&
  10012. get_number(input_format_t::msgpack, subtype) &&
  10013. get_binary(input_format_t::msgpack, len, result) &&
  10014. assign_and_return_true(subtype);
  10015. }
  10016. case 0xC8: // ext 16
  10017. {
  10018. std::uint16_t len{};
  10019. std::int8_t subtype{};
  10020. return get_number(input_format_t::msgpack, len) &&
  10021. get_number(input_format_t::msgpack, subtype) &&
  10022. get_binary(input_format_t::msgpack, len, result) &&
  10023. assign_and_return_true(subtype);
  10024. }
  10025. case 0xC9: // ext 32
  10026. {
  10027. std::uint32_t len{};
  10028. std::int8_t subtype{};
  10029. return get_number(input_format_t::msgpack, len) &&
  10030. get_number(input_format_t::msgpack, subtype) &&
  10031. get_binary(input_format_t::msgpack, len, result) &&
  10032. assign_and_return_true(subtype);
  10033. }
  10034. case 0xD4: // fixext 1
  10035. {
  10036. std::int8_t subtype{};
  10037. return get_number(input_format_t::msgpack, subtype) &&
  10038. get_binary(input_format_t::msgpack, 1, result) &&
  10039. assign_and_return_true(subtype);
  10040. }
  10041. case 0xD5: // fixext 2
  10042. {
  10043. std::int8_t subtype{};
  10044. return get_number(input_format_t::msgpack, subtype) &&
  10045. get_binary(input_format_t::msgpack, 2, result) &&
  10046. assign_and_return_true(subtype);
  10047. }
  10048. case 0xD6: // fixext 4
  10049. {
  10050. std::int8_t subtype{};
  10051. return get_number(input_format_t::msgpack, subtype) &&
  10052. get_binary(input_format_t::msgpack, 4, result) &&
  10053. assign_and_return_true(subtype);
  10054. }
  10055. case 0xD7: // fixext 8
  10056. {
  10057. std::int8_t subtype{};
  10058. return get_number(input_format_t::msgpack, subtype) &&
  10059. get_binary(input_format_t::msgpack, 8, result) &&
  10060. assign_and_return_true(subtype);
  10061. }
  10062. case 0xD8: // fixext 16
  10063. {
  10064. std::int8_t subtype{};
  10065. return get_number(input_format_t::msgpack, subtype) &&
  10066. get_binary(input_format_t::msgpack, 16, result) &&
  10067. assign_and_return_true(subtype);
  10068. }
  10069. default: // LCOV_EXCL_LINE
  10070. return false; // LCOV_EXCL_LINE
  10071. }
  10072. }
  10073. /*!
  10074. @param[in] len the length of the array
  10075. @return whether array creation completed
  10076. */
  10077. bool get_msgpack_array(const std::size_t len)
  10078. {
  10079. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  10080. {
  10081. return false;
  10082. }
  10083. for (std::size_t i = 0; i < len; ++i)
  10084. {
  10085. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  10086. {
  10087. return false;
  10088. }
  10089. }
  10090. return sax->end_array();
  10091. }
  10092. /*!
  10093. @param[in] len the length of the object
  10094. @return whether object creation completed
  10095. */
  10096. bool get_msgpack_object(const std::size_t len)
  10097. {
  10098. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  10099. {
  10100. return false;
  10101. }
  10102. string_t key;
  10103. for (std::size_t i = 0; i < len; ++i)
  10104. {
  10105. get();
  10106. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  10107. {
  10108. return false;
  10109. }
  10110. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  10111. {
  10112. return false;
  10113. }
  10114. key.clear();
  10115. }
  10116. return sax->end_object();
  10117. }
  10118. ////////////
  10119. // UBJSON //
  10120. ////////////
  10121. /*!
  10122. @param[in] get_char whether a new character should be retrieved from the
  10123. input (true, default) or whether the last read
  10124. character should be considered instead
  10125. @return whether a valid UBJSON value was passed to the SAX parser
  10126. */
  10127. bool parse_ubjson_internal(const bool get_char = true)
  10128. {
  10129. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  10130. }
  10131. /*!
  10132. @brief reads a UBJSON string
  10133. This function is either called after reading the 'S' byte explicitly
  10134. indicating a string, or in case of an object key where the 'S' byte can be
  10135. left out.
  10136. @param[out] result created string
  10137. @param[in] get_char whether a new character should be retrieved from the
  10138. input (true, default) or whether the last read
  10139. character should be considered instead
  10140. @return whether string creation completed
  10141. */
  10142. bool get_ubjson_string(string_t& result, const bool get_char = true)
  10143. {
  10144. if (get_char)
  10145. {
  10146. get(); // TODO(niels): may we ignore N here?
  10147. }
  10148. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  10149. {
  10150. return false;
  10151. }
  10152. switch (current)
  10153. {
  10154. case 'U':
  10155. {
  10156. std::uint8_t len{};
  10157. return get_number(input_format, len) && get_string(input_format, len, result);
  10158. }
  10159. case 'i':
  10160. {
  10161. std::int8_t len{};
  10162. return get_number(input_format, len) && get_string(input_format, len, result);
  10163. }
  10164. case 'I':
  10165. {
  10166. std::int16_t len{};
  10167. return get_number(input_format, len) && get_string(input_format, len, result);
  10168. }
  10169. case 'l':
  10170. {
  10171. std::int32_t len{};
  10172. return get_number(input_format, len) && get_string(input_format, len, result);
  10173. }
  10174. case 'L':
  10175. {
  10176. std::int64_t len{};
  10177. return get_number(input_format, len) && get_string(input_format, len, result);
  10178. }
  10179. case 'u':
  10180. {
  10181. if (input_format != input_format_t::bjdata)
  10182. {
  10183. break;
  10184. }
  10185. std::uint16_t len{};
  10186. return get_number(input_format, len) && get_string(input_format, len, result);
  10187. }
  10188. case 'm':
  10189. {
  10190. if (input_format != input_format_t::bjdata)
  10191. {
  10192. break;
  10193. }
  10194. std::uint32_t len{};
  10195. return get_number(input_format, len) && get_string(input_format, len, result);
  10196. }
  10197. case 'M':
  10198. {
  10199. if (input_format != input_format_t::bjdata)
  10200. {
  10201. break;
  10202. }
  10203. std::uint64_t len{};
  10204. return get_number(input_format, len) && get_string(input_format, len, result);
  10205. }
  10206. default:
  10207. break;
  10208. }
  10209. auto last_token = get_token_string();
  10210. std::string message;
  10211. if (input_format != input_format_t::bjdata)
  10212. {
  10213. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  10214. }
  10215. else
  10216. {
  10217. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  10218. }
  10219. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  10220. }
  10221. /*!
  10222. @param[out] dim an integer vector storing the ND array dimensions
  10223. @return whether reading ND array size vector is successful
  10224. */
  10225. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  10226. {
  10227. std::pair<std::size_t, char_int_type> size_and_type;
  10228. size_t dimlen = 0;
  10229. bool no_ndarray = true;
  10230. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  10231. {
  10232. return false;
  10233. }
  10234. if (size_and_type.first != npos)
  10235. {
  10236. if (size_and_type.second != 0)
  10237. {
  10238. if (size_and_type.second != 'N')
  10239. {
  10240. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10241. {
  10242. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  10243. {
  10244. return false;
  10245. }
  10246. dim.push_back(dimlen);
  10247. }
  10248. }
  10249. }
  10250. else
  10251. {
  10252. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10253. {
  10254. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  10255. {
  10256. return false;
  10257. }
  10258. dim.push_back(dimlen);
  10259. }
  10260. }
  10261. }
  10262. else
  10263. {
  10264. while (current != ']')
  10265. {
  10266. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  10267. {
  10268. return false;
  10269. }
  10270. dim.push_back(dimlen);
  10271. get_ignore_noop();
  10272. }
  10273. }
  10274. return true;
  10275. }
  10276. /*!
  10277. @param[out] result determined size
  10278. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  10279. or ndarray dimension is not allowed; `false` means ndarray
  10280. is allowed; for output, `true` means an ndarray is found;
  10281. is_ndarray can only return `true` when its initial value
  10282. is `false`
  10283. @param[in] prefix type marker if already read, otherwise set to 0
  10284. @return whether size determination completed
  10285. */
  10286. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  10287. {
  10288. if (prefix == 0)
  10289. {
  10290. prefix = get_ignore_noop();
  10291. }
  10292. switch (prefix)
  10293. {
  10294. case 'U':
  10295. {
  10296. std::uint8_t number{};
  10297. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10298. {
  10299. return false;
  10300. }
  10301. result = static_cast<std::size_t>(number);
  10302. return true;
  10303. }
  10304. case 'i':
  10305. {
  10306. std::int8_t number{};
  10307. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10308. {
  10309. return false;
  10310. }
  10311. if (number < 0)
  10312. {
  10313. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10314. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10315. }
  10316. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  10317. return true;
  10318. }
  10319. case 'I':
  10320. {
  10321. std::int16_t number{};
  10322. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10323. {
  10324. return false;
  10325. }
  10326. if (number < 0)
  10327. {
  10328. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10329. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10330. }
  10331. result = static_cast<std::size_t>(number);
  10332. return true;
  10333. }
  10334. case 'l':
  10335. {
  10336. std::int32_t number{};
  10337. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10338. {
  10339. return false;
  10340. }
  10341. if (number < 0)
  10342. {
  10343. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10344. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10345. }
  10346. result = static_cast<std::size_t>(number);
  10347. return true;
  10348. }
  10349. case 'L':
  10350. {
  10351. std::int64_t number{};
  10352. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10353. {
  10354. return false;
  10355. }
  10356. if (number < 0)
  10357. {
  10358. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10359. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10360. }
  10361. if (!value_in_range_of<std::size_t>(number))
  10362. {
  10363. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  10364. exception_message(input_format, "integer value overflow", "size"), nullptr));
  10365. }
  10366. result = static_cast<std::size_t>(number);
  10367. return true;
  10368. }
  10369. case 'u':
  10370. {
  10371. if (input_format != input_format_t::bjdata)
  10372. {
  10373. break;
  10374. }
  10375. std::uint16_t number{};
  10376. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10377. {
  10378. return false;
  10379. }
  10380. result = static_cast<std::size_t>(number);
  10381. return true;
  10382. }
  10383. case 'm':
  10384. {
  10385. if (input_format != input_format_t::bjdata)
  10386. {
  10387. break;
  10388. }
  10389. std::uint32_t number{};
  10390. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10391. {
  10392. return false;
  10393. }
  10394. result = conditional_static_cast<std::size_t>(number);
  10395. return true;
  10396. }
  10397. case 'M':
  10398. {
  10399. if (input_format != input_format_t::bjdata)
  10400. {
  10401. break;
  10402. }
  10403. std::uint64_t number{};
  10404. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10405. {
  10406. return false;
  10407. }
  10408. if (!value_in_range_of<std::size_t>(number))
  10409. {
  10410. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  10411. exception_message(input_format, "integer value overflow", "size"), nullptr));
  10412. }
  10413. result = detail::conditional_static_cast<std::size_t>(number);
  10414. return true;
  10415. }
  10416. case '[':
  10417. {
  10418. if (input_format != input_format_t::bjdata)
  10419. {
  10420. break;
  10421. }
  10422. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  10423. {
  10424. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimensional vector is not allowed", "size"), nullptr));
  10425. }
  10426. std::vector<size_t> dim;
  10427. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  10428. {
  10429. return false;
  10430. }
  10431. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  10432. {
  10433. result = dim.at(dim.size() - 1);
  10434. return true;
  10435. }
  10436. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  10437. {
  10438. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  10439. {
  10440. if ( i == 0 )
  10441. {
  10442. result = 0;
  10443. return true;
  10444. }
  10445. }
  10446. string_t key = "_ArraySize_";
  10447. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  10448. {
  10449. return false;
  10450. }
  10451. result = 1;
  10452. for (auto i : dim)
  10453. {
  10454. result *= i;
  10455. if (result == 0 || result == npos) // because dim elements shall not have zeros, result = 0 means overflow happened; it also can't be npos as it is used to initialize size in get_ubjson_size_type()
  10456. {
  10457. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408, exception_message(input_format, "excessive ndarray size caused overflow", "size"), nullptr));
  10458. }
  10459. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  10460. {
  10461. return false;
  10462. }
  10463. }
  10464. is_ndarray = true;
  10465. return sax->end_array();
  10466. }
  10467. result = 0;
  10468. return true;
  10469. }
  10470. default:
  10471. break;
  10472. }
  10473. auto last_token = get_token_string();
  10474. std::string message;
  10475. if (input_format != input_format_t::bjdata)
  10476. {
  10477. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  10478. }
  10479. else
  10480. {
  10481. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  10482. }
  10483. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  10484. }
  10485. /*!
  10486. @brief determine the type and size for a container
  10487. In the optimized UBJSON format, a type and a size can be provided to allow
  10488. for a more compact representation.
  10489. @param[out] result pair of the size and the type
  10490. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  10491. @return whether pair creation completed
  10492. */
  10493. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  10494. {
  10495. result.first = npos; // size
  10496. result.second = 0; // type
  10497. bool is_ndarray = false;
  10498. get_ignore_noop();
  10499. if (current == '$')
  10500. {
  10501. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  10502. if (input_format == input_format_t::bjdata
  10503. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  10504. {
  10505. auto last_token = get_token_string();
  10506. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10507. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  10508. }
  10509. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  10510. {
  10511. return false;
  10512. }
  10513. get_ignore_noop();
  10514. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  10515. {
  10516. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  10517. {
  10518. return false;
  10519. }
  10520. auto last_token = get_token_string();
  10521. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10522. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  10523. }
  10524. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  10525. if (input_format == input_format_t::bjdata && is_ndarray)
  10526. {
  10527. if (inside_ndarray)
  10528. {
  10529. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  10530. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  10531. }
  10532. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  10533. }
  10534. return is_error;
  10535. }
  10536. if (current == '#')
  10537. {
  10538. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  10539. if (input_format == input_format_t::bjdata && is_ndarray)
  10540. {
  10541. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  10542. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  10543. }
  10544. return is_error;
  10545. }
  10546. return true;
  10547. }
  10548. /*!
  10549. @param prefix the previously read or set type prefix
  10550. @return whether value creation completed
  10551. */
  10552. bool get_ubjson_value(const char_int_type prefix)
  10553. {
  10554. switch (prefix)
  10555. {
  10556. case char_traits<char_type>::eof(): // EOF
  10557. return unexpect_eof(input_format, "value");
  10558. case 'T': // true
  10559. return sax->boolean(true);
  10560. case 'F': // false
  10561. return sax->boolean(false);
  10562. case 'Z': // null
  10563. return sax->null();
  10564. case 'B': // byte
  10565. {
  10566. if (input_format != input_format_t::bjdata)
  10567. {
  10568. break;
  10569. }
  10570. std::uint8_t number{};
  10571. return get_number(input_format, number) && sax->number_unsigned(number);
  10572. }
  10573. case 'U':
  10574. {
  10575. std::uint8_t number{};
  10576. return get_number(input_format, number) && sax->number_unsigned(number);
  10577. }
  10578. case 'i':
  10579. {
  10580. std::int8_t number{};
  10581. return get_number(input_format, number) && sax->number_integer(number);
  10582. }
  10583. case 'I':
  10584. {
  10585. std::int16_t number{};
  10586. return get_number(input_format, number) && sax->number_integer(number);
  10587. }
  10588. case 'l':
  10589. {
  10590. std::int32_t number{};
  10591. return get_number(input_format, number) && sax->number_integer(number);
  10592. }
  10593. case 'L':
  10594. {
  10595. std::int64_t number{};
  10596. return get_number(input_format, number) && sax->number_integer(number);
  10597. }
  10598. case 'u':
  10599. {
  10600. if (input_format != input_format_t::bjdata)
  10601. {
  10602. break;
  10603. }
  10604. std::uint16_t number{};
  10605. return get_number(input_format, number) && sax->number_unsigned(number);
  10606. }
  10607. case 'm':
  10608. {
  10609. if (input_format != input_format_t::bjdata)
  10610. {
  10611. break;
  10612. }
  10613. std::uint32_t number{};
  10614. return get_number(input_format, number) && sax->number_unsigned(number);
  10615. }
  10616. case 'M':
  10617. {
  10618. if (input_format != input_format_t::bjdata)
  10619. {
  10620. break;
  10621. }
  10622. std::uint64_t number{};
  10623. return get_number(input_format, number) && sax->number_unsigned(number);
  10624. }
  10625. case 'h':
  10626. {
  10627. if (input_format != input_format_t::bjdata)
  10628. {
  10629. break;
  10630. }
  10631. const auto byte1_raw = get();
  10632. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10633. {
  10634. return false;
  10635. }
  10636. const auto byte2_raw = get();
  10637. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10638. {
  10639. return false;
  10640. }
  10641. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  10642. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10643. // code from RFC 7049, Appendix D, Figure 3:
  10644. // As half-precision floating-point numbers were only added
  10645. // to IEEE 754 in 2008, today's programming platforms often
  10646. // still only have limited support for them. It is very
  10647. // easy to include at least decoding support for them even
  10648. // without such support. An example of a small decoder for
  10649. // half-precision floating-point numbers in the C language
  10650. // is shown in Fig. 3.
  10651. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10652. const double val = [&half]
  10653. {
  10654. const int exp = (half >> 10u) & 0x1Fu;
  10655. const unsigned int mant = half & 0x3FFu;
  10656. JSON_ASSERT(0 <= exp&& exp <= 32);
  10657. JSON_ASSERT(mant <= 1024);
  10658. switch (exp)
  10659. {
  10660. case 0:
  10661. return std::ldexp(mant, -24);
  10662. case 31:
  10663. return (mant == 0)
  10664. ? std::numeric_limits<double>::infinity()
  10665. : std::numeric_limits<double>::quiet_NaN();
  10666. default:
  10667. return std::ldexp(mant + 1024, exp - 25);
  10668. }
  10669. }();
  10670. return sax->number_float((half & 0x8000u) != 0
  10671. ? static_cast<number_float_t>(-val)
  10672. : static_cast<number_float_t>(val), "");
  10673. }
  10674. case 'd':
  10675. {
  10676. float number{};
  10677. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10678. }
  10679. case 'D':
  10680. {
  10681. double number{};
  10682. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10683. }
  10684. case 'H':
  10685. {
  10686. return get_ubjson_high_precision_number();
  10687. }
  10688. case 'C': // char
  10689. {
  10690. get();
  10691. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10692. {
  10693. return false;
  10694. }
  10695. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10696. {
  10697. auto last_token = get_token_string();
  10698. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10699. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10700. }
  10701. string_t s(1, static_cast<typename string_t::value_type>(current));
  10702. return sax->string(s);
  10703. }
  10704. case 'S': // string
  10705. {
  10706. string_t s;
  10707. return get_ubjson_string(s) && sax->string(s);
  10708. }
  10709. case '[': // array
  10710. return get_ubjson_array();
  10711. case '{': // object
  10712. return get_ubjson_object();
  10713. default: // anything else
  10714. break;
  10715. }
  10716. auto last_token = get_token_string();
  10717. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), nullptr));
  10718. }
  10719. /*!
  10720. @return whether array creation completed
  10721. */
  10722. bool get_ubjson_array()
  10723. {
  10724. std::pair<std::size_t, char_int_type> size_and_type;
  10725. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10726. {
  10727. return false;
  10728. }
  10729. // if bit-8 of size_and_type.second is set to 1, encode bjdata ndarray as an object in JData annotated array format (https://github.com/NeuroJSON/jdata):
  10730. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10731. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10732. {
  10733. size_and_type.second &= ~(static_cast<char_int_type>(1) << 8); // use bit 8 to indicate ndarray, here we remove the bit to restore the type marker
  10734. auto it = std::lower_bound(bjd_types_map.begin(), bjd_types_map.end(), size_and_type.second, [](const bjd_type & p, char_int_type t)
  10735. {
  10736. return p.first < t;
  10737. });
  10738. string_t key = "_ArrayType_";
  10739. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10740. {
  10741. auto last_token = get_token_string();
  10742. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10743. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10744. }
  10745. string_t type = it->second; // sax->string() takes a reference
  10746. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10747. {
  10748. return false;
  10749. }
  10750. if (size_and_type.second == 'C' || size_and_type.second == 'B')
  10751. {
  10752. size_and_type.second = 'U';
  10753. }
  10754. key = "_ArrayData_";
  10755. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10756. {
  10757. return false;
  10758. }
  10759. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10760. {
  10761. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10762. {
  10763. return false;
  10764. }
  10765. }
  10766. return (sax->end_array() && sax->end_object());
  10767. }
  10768. // If BJData type marker is 'B' decode as binary
  10769. if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
  10770. {
  10771. binary_t result;
  10772. return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
  10773. }
  10774. if (size_and_type.first != npos)
  10775. {
  10776. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10777. {
  10778. return false;
  10779. }
  10780. if (size_and_type.second != 0)
  10781. {
  10782. if (size_and_type.second != 'N')
  10783. {
  10784. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10785. {
  10786. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10787. {
  10788. return false;
  10789. }
  10790. }
  10791. }
  10792. }
  10793. else
  10794. {
  10795. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10796. {
  10797. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10798. {
  10799. return false;
  10800. }
  10801. }
  10802. }
  10803. }
  10804. else
  10805. {
  10806. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
  10807. {
  10808. return false;
  10809. }
  10810. while (current != ']')
  10811. {
  10812. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10813. {
  10814. return false;
  10815. }
  10816. get_ignore_noop();
  10817. }
  10818. }
  10819. return sax->end_array();
  10820. }
  10821. /*!
  10822. @return whether object creation completed
  10823. */
  10824. bool get_ubjson_object()
  10825. {
  10826. std::pair<std::size_t, char_int_type> size_and_type;
  10827. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10828. {
  10829. return false;
  10830. }
  10831. // do not accept ND-array size in objects in BJData
  10832. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10833. {
  10834. auto last_token = get_token_string();
  10835. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10836. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10837. }
  10838. string_t key;
  10839. if (size_and_type.first != npos)
  10840. {
  10841. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10842. {
  10843. return false;
  10844. }
  10845. if (size_and_type.second != 0)
  10846. {
  10847. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10848. {
  10849. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10850. {
  10851. return false;
  10852. }
  10853. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10854. {
  10855. return false;
  10856. }
  10857. key.clear();
  10858. }
  10859. }
  10860. else
  10861. {
  10862. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10863. {
  10864. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10865. {
  10866. return false;
  10867. }
  10868. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10869. {
  10870. return false;
  10871. }
  10872. key.clear();
  10873. }
  10874. }
  10875. }
  10876. else
  10877. {
  10878. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
  10879. {
  10880. return false;
  10881. }
  10882. while (current != '}')
  10883. {
  10884. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  10885. {
  10886. return false;
  10887. }
  10888. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10889. {
  10890. return false;
  10891. }
  10892. get_ignore_noop();
  10893. key.clear();
  10894. }
  10895. }
  10896. return sax->end_object();
  10897. }
  10898. // Note, no reader for UBJSON binary types is implemented because they do
  10899. // not exist
  10900. bool get_ubjson_high_precision_number()
  10901. {
  10902. // get size of following number string
  10903. std::size_t size{};
  10904. bool no_ndarray = true;
  10905. auto res = get_ubjson_size_value(size, no_ndarray);
  10906. if (JSON_HEDLEY_UNLIKELY(!res))
  10907. {
  10908. return res;
  10909. }
  10910. // get number string
  10911. std::vector<char> number_vector;
  10912. for (std::size_t i = 0; i < size; ++i)
  10913. {
  10914. get();
  10915. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10916. {
  10917. return false;
  10918. }
  10919. number_vector.push_back(static_cast<char>(current));
  10920. }
  10921. // parse number string
  10922. using ia_type = decltype(detail::input_adapter(number_vector));
  10923. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  10924. const auto result_number = number_lexer.scan();
  10925. const auto number_string = number_lexer.get_token_string();
  10926. const auto result_remainder = number_lexer.scan();
  10927. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10928. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10929. {
  10930. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10931. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10932. }
  10933. switch (result_number)
  10934. {
  10935. case token_type::value_integer:
  10936. return sax->number_integer(number_lexer.get_number_integer());
  10937. case token_type::value_unsigned:
  10938. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10939. case token_type::value_float:
  10940. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10941. case token_type::uninitialized:
  10942. case token_type::literal_true:
  10943. case token_type::literal_false:
  10944. case token_type::literal_null:
  10945. case token_type::value_string:
  10946. case token_type::begin_array:
  10947. case token_type::begin_object:
  10948. case token_type::end_array:
  10949. case token_type::end_object:
  10950. case token_type::name_separator:
  10951. case token_type::value_separator:
  10952. case token_type::parse_error:
  10953. case token_type::end_of_input:
  10954. case token_type::literal_or_value:
  10955. default:
  10956. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10957. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10958. }
  10959. }
  10960. ///////////////////////
  10961. // Utility functions //
  10962. ///////////////////////
  10963. /*!
  10964. @brief get next character from the input
  10965. This function provides the interface to the used input adapter. It does
  10966. not throw in case the input reached EOF, but returns a -'ve valued
  10967. `char_traits<char_type>::eof()` in that case.
  10968. @return character read from the input
  10969. */
  10970. char_int_type get()
  10971. {
  10972. ++chars_read;
  10973. return current = ia.get_character();
  10974. }
  10975. /*!
  10976. @brief get_to read into a primitive type
  10977. This function provides the interface to the used input adapter. It does
  10978. not throw in case the input reached EOF, but returns false instead
  10979. @return bool, whether the read was successful
  10980. */
  10981. template<class T>
  10982. bool get_to(T& dest, const input_format_t format, const char* context)
  10983. {
  10984. auto new_chars_read = ia.get_elements(&dest);
  10985. chars_read += new_chars_read;
  10986. if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
  10987. {
  10988. // in case of failure, advance position by 1 to report failing location
  10989. ++chars_read;
  10990. sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10991. return false;
  10992. }
  10993. return true;
  10994. }
  10995. /*!
  10996. @return character read from the input after ignoring all 'N' entries
  10997. */
  10998. char_int_type get_ignore_noop()
  10999. {
  11000. do
  11001. {
  11002. get();
  11003. }
  11004. while (current == 'N');
  11005. return current;
  11006. }
  11007. template<class NumberType>
  11008. static void byte_swap(NumberType& number)
  11009. {
  11010. constexpr std::size_t sz = sizeof(number);
  11011. #ifdef __cpp_lib_byteswap
  11012. if constexpr (sz == 1)
  11013. {
  11014. return;
  11015. }
  11016. if constexpr(std::is_integral_v<NumberType>)
  11017. {
  11018. number = std::byteswap(number);
  11019. return;
  11020. }
  11021. #endif
  11022. auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
  11023. for (std::size_t i = 0; i < sz / 2; ++i)
  11024. {
  11025. std::swap(ptr[i], ptr[sz - i - 1]);
  11026. }
  11027. }
  11028. /*
  11029. @brief read a number from the input
  11030. @tparam NumberType the type of the number
  11031. @param[in] format the current format (for diagnostics)
  11032. @param[out] result number of type @a NumberType
  11033. @return whether conversion completed
  11034. @note This function needs to respect the system's endianness, because
  11035. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  11036. (big endian) and therefore need reordering on little endian systems.
  11037. On the other hand, BSON and BJData use little endian and should reorder
  11038. on big endian systems.
  11039. */
  11040. template<typename NumberType, bool InputIsLittleEndian = false>
  11041. bool get_number(const input_format_t format, NumberType& result)
  11042. {
  11043. // read in the original format
  11044. if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
  11045. {
  11046. return false;
  11047. }
  11048. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  11049. {
  11050. byte_swap(result);
  11051. }
  11052. return true;
  11053. }
  11054. /*!
  11055. @brief create a string by reading characters from the input
  11056. @tparam NumberType the type of the number
  11057. @param[in] format the current format (for diagnostics)
  11058. @param[in] len number of characters to read
  11059. @param[out] result string created by reading @a len bytes
  11060. @return whether string creation completed
  11061. @note We can not reserve @a len bytes for the result, because @a len
  11062. may be too large. Usually, @ref unexpect_eof() detects the end of
  11063. the input before we run out of string memory.
  11064. */
  11065. template<typename NumberType>
  11066. bool get_string(const input_format_t format,
  11067. const NumberType len,
  11068. string_t& result)
  11069. {
  11070. bool success = true;
  11071. for (NumberType i = 0; i < len; i++)
  11072. {
  11073. get();
  11074. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  11075. {
  11076. success = false;
  11077. break;
  11078. }
  11079. result.push_back(static_cast<typename string_t::value_type>(current));
  11080. }
  11081. return success;
  11082. }
  11083. /*!
  11084. @brief create a byte array by reading bytes from the input
  11085. @tparam NumberType the type of the number
  11086. @param[in] format the current format (for diagnostics)
  11087. @param[in] len number of bytes to read
  11088. @param[out] result byte array created by reading @a len bytes
  11089. @return whether byte array creation completed
  11090. @note We can not reserve @a len bytes for the result, because @a len
  11091. may be too large. Usually, @ref unexpect_eof() detects the end of
  11092. the input before we run out of memory.
  11093. */
  11094. template<typename NumberType>
  11095. bool get_binary(const input_format_t format,
  11096. const NumberType len,
  11097. binary_t& result)
  11098. {
  11099. bool success = true;
  11100. for (NumberType i = 0; i < len; i++)
  11101. {
  11102. get();
  11103. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  11104. {
  11105. success = false;
  11106. break;
  11107. }
  11108. result.push_back(static_cast<std::uint8_t>(current));
  11109. }
  11110. return success;
  11111. }
  11112. /*!
  11113. @param[in] format the current format (for diagnostics)
  11114. @param[in] context further context information (for diagnostics)
  11115. @return whether the last read character is not EOF
  11116. */
  11117. JSON_HEDLEY_NON_NULL(3)
  11118. bool unexpect_eof(const input_format_t format, const char* context) const
  11119. {
  11120. if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
  11121. {
  11122. return sax->parse_error(chars_read, "<end of file>",
  11123. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  11124. }
  11125. return true;
  11126. }
  11127. /*!
  11128. @return a string representation of the last read byte
  11129. */
  11130. std::string get_token_string() const
  11131. {
  11132. std::array<char, 3> cr{{}};
  11133. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  11134. return std::string{cr.data()};
  11135. }
  11136. /*!
  11137. @param[in] format the current format
  11138. @param[in] detail a detailed error message
  11139. @param[in] context further context information
  11140. @return a message string to use in the parse_error exceptions
  11141. */
  11142. std::string exception_message(const input_format_t format,
  11143. const std::string& detail,
  11144. const std::string& context) const
  11145. {
  11146. std::string error_msg = "syntax error while parsing ";
  11147. switch (format)
  11148. {
  11149. case input_format_t::cbor:
  11150. error_msg += "CBOR";
  11151. break;
  11152. case input_format_t::msgpack:
  11153. error_msg += "MessagePack";
  11154. break;
  11155. case input_format_t::ubjson:
  11156. error_msg += "UBJSON";
  11157. break;
  11158. case input_format_t::bson:
  11159. error_msg += "BSON";
  11160. break;
  11161. case input_format_t::bjdata:
  11162. error_msg += "BJData";
  11163. break;
  11164. case input_format_t::json: // LCOV_EXCL_LINE
  11165. default: // LCOV_EXCL_LINE
  11166. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  11167. }
  11168. return concat(error_msg, ' ', context, ": ", detail);
  11169. }
  11170. private:
  11171. static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
  11172. /// input adapter
  11173. InputAdapterType ia;
  11174. /// the current character
  11175. char_int_type current = char_traits<char_type>::eof();
  11176. /// the number of characters read
  11177. std::size_t chars_read = 0;
  11178. /// whether we can assume little endianness
  11179. const bool is_little_endian = little_endianness();
  11180. /// input format
  11181. const input_format_t input_format = input_format_t::json;
  11182. /// the SAX parser
  11183. json_sax_t* sax = nullptr;
  11184. // excluded markers in bjdata optimized type
  11185. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  11186. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  11187. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  11188. make_array<bjd_type>( \
  11189. bjd_type{'B', "byte"}, \
  11190. bjd_type{'C', "char"}, \
  11191. bjd_type{'D', "double"}, \
  11192. bjd_type{'I', "int16"}, \
  11193. bjd_type{'L', "int64"}, \
  11194. bjd_type{'M', "uint64"}, \
  11195. bjd_type{'U', "uint8"}, \
  11196. bjd_type{'d', "single"}, \
  11197. bjd_type{'i', "int8"}, \
  11198. bjd_type{'l', "int32"}, \
  11199. bjd_type{'m', "uint32"}, \
  11200. bjd_type{'u', "uint16"})
  11201. JSON_PRIVATE_UNLESS_TESTED:
  11202. // lookup tables
  11203. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  11204. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  11205. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  11206. using bjd_type = std::pair<char_int_type, string_t>;
  11207. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  11208. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  11209. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  11210. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  11211. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  11212. };
  11213. #ifndef JSON_HAS_CPP_17
  11214. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  11215. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  11216. #endif
  11217. } // namespace detail
  11218. NLOHMANN_JSON_NAMESPACE_END
  11219. // #include <nlohmann/detail/input/input_adapters.hpp>
  11220. // #include <nlohmann/detail/input/lexer.hpp>
  11221. // #include <nlohmann/detail/input/parser.hpp>
  11222. // __ _____ _____ _____
  11223. // __| | __| | | | JSON for Modern C++
  11224. // | | |__ | | | | | | version 3.12.0
  11225. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11226. //
  11227. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  11228. // SPDX-License-Identifier: MIT
  11229. #include <cmath> // isfinite
  11230. #include <cstdint> // uint8_t
  11231. #include <functional> // function
  11232. #include <string> // string
  11233. #include <utility> // move
  11234. #include <vector> // vector
  11235. // #include <nlohmann/detail/exceptions.hpp>
  11236. // #include <nlohmann/detail/input/input_adapters.hpp>
  11237. // #include <nlohmann/detail/input/json_sax.hpp>
  11238. // #include <nlohmann/detail/input/lexer.hpp>
  11239. // #include <nlohmann/detail/macro_scope.hpp>
  11240. // #include <nlohmann/detail/meta/is_sax.hpp>
  11241. // #include <nlohmann/detail/string_concat.hpp>
  11242. // #include <nlohmann/detail/value_t.hpp>
  11243. NLOHMANN_JSON_NAMESPACE_BEGIN
  11244. namespace detail
  11245. {
  11246. ////////////
  11247. // parser //
  11248. ////////////
  11249. enum class parse_event_t : std::uint8_t
  11250. {
  11251. /// the parser read `{` and started to process a JSON object
  11252. object_start,
  11253. /// the parser read `}` and finished processing a JSON object
  11254. object_end,
  11255. /// the parser read `[` and started to process a JSON array
  11256. array_start,
  11257. /// the parser read `]` and finished processing a JSON array
  11258. array_end,
  11259. /// the parser read a key of a value in an object
  11260. key,
  11261. /// the parser finished reading a JSON value
  11262. value
  11263. };
  11264. template<typename BasicJsonType>
  11265. using parser_callback_t =
  11266. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  11267. /*!
  11268. @brief syntax analysis
  11269. This class implements a recursive descent parser.
  11270. */
  11271. template<typename BasicJsonType, typename InputAdapterType>
  11272. class parser
  11273. {
  11274. using number_integer_t = typename BasicJsonType::number_integer_t;
  11275. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  11276. using number_float_t = typename BasicJsonType::number_float_t;
  11277. using string_t = typename BasicJsonType::string_t;
  11278. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  11279. using token_type = typename lexer_t::token_type;
  11280. public:
  11281. /// a parser reading from an input adapter
  11282. explicit parser(InputAdapterType&& adapter,
  11283. parser_callback_t<BasicJsonType> cb = nullptr,
  11284. const bool allow_exceptions_ = true,
  11285. const bool skip_comments = false)
  11286. : callback(std::move(cb))
  11287. , m_lexer(std::move(adapter), skip_comments)
  11288. , allow_exceptions(allow_exceptions_)
  11289. {
  11290. // read first token
  11291. get_token();
  11292. }
  11293. /*!
  11294. @brief public parser interface
  11295. @param[in] strict whether to expect the last token to be EOF
  11296. @param[in,out] result parsed JSON value
  11297. @throw parse_error.101 in case of an unexpected token
  11298. @throw parse_error.102 if to_unicode fails or surrogate error
  11299. @throw parse_error.103 if to_unicode fails
  11300. */
  11301. void parse(const bool strict, BasicJsonType& result)
  11302. {
  11303. if (callback)
  11304. {
  11305. json_sax_dom_callback_parser<BasicJsonType, InputAdapterType> sdp(result, callback, allow_exceptions, &m_lexer);
  11306. sax_parse_internal(&sdp);
  11307. // in strict mode, input must be completely read
  11308. if (strict && (get_token() != token_type::end_of_input))
  11309. {
  11310. sdp.parse_error(m_lexer.get_position(),
  11311. m_lexer.get_token_string(),
  11312. parse_error::create(101, m_lexer.get_position(),
  11313. exception_message(token_type::end_of_input, "value"), nullptr));
  11314. }
  11315. // in case of an error, return discarded value
  11316. if (sdp.is_errored())
  11317. {
  11318. result = value_t::discarded;
  11319. return;
  11320. }
  11321. // set top-level value to null if it was discarded by the callback
  11322. // function
  11323. if (result.is_discarded())
  11324. {
  11325. result = nullptr;
  11326. }
  11327. }
  11328. else
  11329. {
  11330. json_sax_dom_parser<BasicJsonType, InputAdapterType> sdp(result, allow_exceptions, &m_lexer);
  11331. sax_parse_internal(&sdp);
  11332. // in strict mode, input must be completely read
  11333. if (strict && (get_token() != token_type::end_of_input))
  11334. {
  11335. sdp.parse_error(m_lexer.get_position(),
  11336. m_lexer.get_token_string(),
  11337. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  11338. }
  11339. // in case of an error, return discarded value
  11340. if (sdp.is_errored())
  11341. {
  11342. result = value_t::discarded;
  11343. return;
  11344. }
  11345. }
  11346. result.assert_invariant();
  11347. }
  11348. /*!
  11349. @brief public accept interface
  11350. @param[in] strict whether to expect the last token to be EOF
  11351. @return whether the input is a proper JSON text
  11352. */
  11353. bool accept(const bool strict = true)
  11354. {
  11355. json_sax_acceptor<BasicJsonType> sax_acceptor;
  11356. return sax_parse(&sax_acceptor, strict);
  11357. }
  11358. template<typename SAX>
  11359. JSON_HEDLEY_NON_NULL(2)
  11360. bool sax_parse(SAX* sax, const bool strict = true)
  11361. {
  11362. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  11363. const bool result = sax_parse_internal(sax);
  11364. // strict mode: next byte must be EOF
  11365. if (result && strict && (get_token() != token_type::end_of_input))
  11366. {
  11367. return sax->parse_error(m_lexer.get_position(),
  11368. m_lexer.get_token_string(),
  11369. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  11370. }
  11371. return result;
  11372. }
  11373. private:
  11374. template<typename SAX>
  11375. JSON_HEDLEY_NON_NULL(2)
  11376. bool sax_parse_internal(SAX* sax)
  11377. {
  11378. // stack to remember the hierarchy of structured values we are parsing
  11379. // true = array; false = object
  11380. std::vector<bool> states;
  11381. // value to avoid a goto (see comment where set to true)
  11382. bool skip_to_state_evaluation = false;
  11383. while (true)
  11384. {
  11385. if (!skip_to_state_evaluation)
  11386. {
  11387. // invariant: get_token() was called before each iteration
  11388. switch (last_token)
  11389. {
  11390. case token_type::begin_object:
  11391. {
  11392. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
  11393. {
  11394. return false;
  11395. }
  11396. // closing } -> we are done
  11397. if (get_token() == token_type::end_object)
  11398. {
  11399. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  11400. {
  11401. return false;
  11402. }
  11403. break;
  11404. }
  11405. // parse key
  11406. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  11407. {
  11408. return sax->parse_error(m_lexer.get_position(),
  11409. m_lexer.get_token_string(),
  11410. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  11411. }
  11412. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  11413. {
  11414. return false;
  11415. }
  11416. // parse separator (:)
  11417. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  11418. {
  11419. return sax->parse_error(m_lexer.get_position(),
  11420. m_lexer.get_token_string(),
  11421. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  11422. }
  11423. // remember we are now inside an object
  11424. states.push_back(false);
  11425. // parse values
  11426. get_token();
  11427. continue;
  11428. }
  11429. case token_type::begin_array:
  11430. {
  11431. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
  11432. {
  11433. return false;
  11434. }
  11435. // closing ] -> we are done
  11436. if (get_token() == token_type::end_array)
  11437. {
  11438. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  11439. {
  11440. return false;
  11441. }
  11442. break;
  11443. }
  11444. // remember we are now inside an array
  11445. states.push_back(true);
  11446. // parse values (no need to call get_token)
  11447. continue;
  11448. }
  11449. case token_type::value_float:
  11450. {
  11451. const auto res = m_lexer.get_number_float();
  11452. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  11453. {
  11454. return sax->parse_error(m_lexer.get_position(),
  11455. m_lexer.get_token_string(),
  11456. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  11457. }
  11458. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  11459. {
  11460. return false;
  11461. }
  11462. break;
  11463. }
  11464. case token_type::literal_false:
  11465. {
  11466. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  11467. {
  11468. return false;
  11469. }
  11470. break;
  11471. }
  11472. case token_type::literal_null:
  11473. {
  11474. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  11475. {
  11476. return false;
  11477. }
  11478. break;
  11479. }
  11480. case token_type::literal_true:
  11481. {
  11482. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  11483. {
  11484. return false;
  11485. }
  11486. break;
  11487. }
  11488. case token_type::value_integer:
  11489. {
  11490. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  11491. {
  11492. return false;
  11493. }
  11494. break;
  11495. }
  11496. case token_type::value_string:
  11497. {
  11498. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  11499. {
  11500. return false;
  11501. }
  11502. break;
  11503. }
  11504. case token_type::value_unsigned:
  11505. {
  11506. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  11507. {
  11508. return false;
  11509. }
  11510. break;
  11511. }
  11512. case token_type::parse_error:
  11513. {
  11514. // using "uninitialized" to avoid "expected" message
  11515. return sax->parse_error(m_lexer.get_position(),
  11516. m_lexer.get_token_string(),
  11517. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  11518. }
  11519. case token_type::end_of_input:
  11520. {
  11521. if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
  11522. {
  11523. return sax->parse_error(m_lexer.get_position(),
  11524. m_lexer.get_token_string(),
  11525. parse_error::create(101, m_lexer.get_position(),
  11526. "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
  11527. }
  11528. return sax->parse_error(m_lexer.get_position(),
  11529. m_lexer.get_token_string(),
  11530. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  11531. }
  11532. case token_type::uninitialized:
  11533. case token_type::end_array:
  11534. case token_type::end_object:
  11535. case token_type::name_separator:
  11536. case token_type::value_separator:
  11537. case token_type::literal_or_value:
  11538. default: // the last token was unexpected
  11539. {
  11540. return sax->parse_error(m_lexer.get_position(),
  11541. m_lexer.get_token_string(),
  11542. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  11543. }
  11544. }
  11545. }
  11546. else
  11547. {
  11548. skip_to_state_evaluation = false;
  11549. }
  11550. // we reached this line after we successfully parsed a value
  11551. if (states.empty())
  11552. {
  11553. // empty stack: we reached the end of the hierarchy: done
  11554. return true;
  11555. }
  11556. if (states.back()) // array
  11557. {
  11558. // comma -> next value
  11559. if (get_token() == token_type::value_separator)
  11560. {
  11561. // parse a new value
  11562. get_token();
  11563. continue;
  11564. }
  11565. // closing ]
  11566. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  11567. {
  11568. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  11569. {
  11570. return false;
  11571. }
  11572. // We are done with this array. Before we can parse a
  11573. // new value, we need to evaluate the new state first.
  11574. // By setting skip_to_state_evaluation to false, we
  11575. // are effectively jumping to the beginning of this if.
  11576. JSON_ASSERT(!states.empty());
  11577. states.pop_back();
  11578. skip_to_state_evaluation = true;
  11579. continue;
  11580. }
  11581. return sax->parse_error(m_lexer.get_position(),
  11582. m_lexer.get_token_string(),
  11583. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  11584. }
  11585. // states.back() is false -> object
  11586. // comma -> next value
  11587. if (get_token() == token_type::value_separator)
  11588. {
  11589. // parse key
  11590. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  11591. {
  11592. return sax->parse_error(m_lexer.get_position(),
  11593. m_lexer.get_token_string(),
  11594. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  11595. }
  11596. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  11597. {
  11598. return false;
  11599. }
  11600. // parse separator (:)
  11601. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  11602. {
  11603. return sax->parse_error(m_lexer.get_position(),
  11604. m_lexer.get_token_string(),
  11605. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  11606. }
  11607. // parse values
  11608. get_token();
  11609. continue;
  11610. }
  11611. // closing }
  11612. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  11613. {
  11614. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  11615. {
  11616. return false;
  11617. }
  11618. // We are done with this object. Before we can parse a
  11619. // new value, we need to evaluate the new state first.
  11620. // By setting skip_to_state_evaluation to false, we
  11621. // are effectively jumping to the beginning of this if.
  11622. JSON_ASSERT(!states.empty());
  11623. states.pop_back();
  11624. skip_to_state_evaluation = true;
  11625. continue;
  11626. }
  11627. return sax->parse_error(m_lexer.get_position(),
  11628. m_lexer.get_token_string(),
  11629. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  11630. }
  11631. }
  11632. /// get next token from lexer
  11633. token_type get_token()
  11634. {
  11635. return last_token = m_lexer.scan();
  11636. }
  11637. std::string exception_message(const token_type expected, const std::string& context)
  11638. {
  11639. std::string error_msg = "syntax error ";
  11640. if (!context.empty())
  11641. {
  11642. error_msg += concat("while parsing ", context, ' ');
  11643. }
  11644. error_msg += "- ";
  11645. if (last_token == token_type::parse_error)
  11646. {
  11647. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  11648. m_lexer.get_token_string(), '\'');
  11649. }
  11650. else
  11651. {
  11652. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  11653. }
  11654. if (expected != token_type::uninitialized)
  11655. {
  11656. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  11657. }
  11658. return error_msg;
  11659. }
  11660. private:
  11661. /// callback function
  11662. const parser_callback_t<BasicJsonType> callback = nullptr;
  11663. /// the type of the last read token
  11664. token_type last_token = token_type::uninitialized;
  11665. /// the lexer
  11666. lexer_t m_lexer;
  11667. /// whether to throw exceptions in case of errors
  11668. const bool allow_exceptions = true;
  11669. };
  11670. } // namespace detail
  11671. NLOHMANN_JSON_NAMESPACE_END
  11672. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11673. // __ _____ _____ _____
  11674. // __| | __| | | | JSON for Modern C++
  11675. // | | |__ | | | | | | version 3.12.0
  11676. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11677. //
  11678. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  11679. // SPDX-License-Identifier: MIT
  11680. // #include <nlohmann/detail/abi_macros.hpp>
  11681. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11682. // __ _____ _____ _____
  11683. // __| | __| | | | JSON for Modern C++
  11684. // | | |__ | | | | | | version 3.12.0
  11685. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11686. //
  11687. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  11688. // SPDX-License-Identifier: MIT
  11689. #include <cstddef> // ptrdiff_t
  11690. #include <limits> // numeric_limits
  11691. // #include <nlohmann/detail/macro_scope.hpp>
  11692. NLOHMANN_JSON_NAMESPACE_BEGIN
  11693. namespace detail
  11694. {
  11695. /*
  11696. @brief an iterator for primitive JSON types
  11697. This class models an iterator for primitive JSON types (boolean, number,
  11698. string). It's only purpose is to allow the iterator/const_iterator classes
  11699. to "iterate" over primitive values. Internally, the iterator is modeled by
  11700. a `difference_type` variable. Value begin_value (`0`) models the begin,
  11701. end_value (`1`) models past the end.
  11702. */
  11703. class primitive_iterator_t
  11704. {
  11705. private:
  11706. using difference_type = std::ptrdiff_t;
  11707. static constexpr difference_type begin_value = 0;
  11708. static constexpr difference_type end_value = begin_value + 1;
  11709. JSON_PRIVATE_UNLESS_TESTED:
  11710. /// iterator as signed integer type
  11711. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11712. public:
  11713. constexpr difference_type get_value() const noexcept
  11714. {
  11715. return m_it;
  11716. }
  11717. /// set iterator to a defined beginning
  11718. void set_begin() noexcept
  11719. {
  11720. m_it = begin_value;
  11721. }
  11722. /// set iterator to a defined past the end
  11723. void set_end() noexcept
  11724. {
  11725. m_it = end_value;
  11726. }
  11727. /// return whether the iterator can be dereferenced
  11728. constexpr bool is_begin() const noexcept
  11729. {
  11730. return m_it == begin_value;
  11731. }
  11732. /// return whether the iterator is at end
  11733. constexpr bool is_end() const noexcept
  11734. {
  11735. return m_it == end_value;
  11736. }
  11737. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11738. {
  11739. return lhs.m_it == rhs.m_it;
  11740. }
  11741. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11742. {
  11743. return lhs.m_it < rhs.m_it;
  11744. }
  11745. primitive_iterator_t operator+(difference_type n) noexcept
  11746. {
  11747. auto result = *this;
  11748. result += n;
  11749. return result;
  11750. }
  11751. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11752. {
  11753. return lhs.m_it - rhs.m_it;
  11754. }
  11755. primitive_iterator_t& operator++() noexcept
  11756. {
  11757. ++m_it;
  11758. return *this;
  11759. }
  11760. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11761. {
  11762. auto result = *this;
  11763. ++m_it;
  11764. return result;
  11765. }
  11766. primitive_iterator_t& operator--() noexcept
  11767. {
  11768. --m_it;
  11769. return *this;
  11770. }
  11771. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11772. {
  11773. auto result = *this;
  11774. --m_it;
  11775. return result;
  11776. }
  11777. primitive_iterator_t& operator+=(difference_type n) noexcept
  11778. {
  11779. m_it += n;
  11780. return *this;
  11781. }
  11782. primitive_iterator_t& operator-=(difference_type n) noexcept
  11783. {
  11784. m_it -= n;
  11785. return *this;
  11786. }
  11787. };
  11788. } // namespace detail
  11789. NLOHMANN_JSON_NAMESPACE_END
  11790. NLOHMANN_JSON_NAMESPACE_BEGIN
  11791. namespace detail
  11792. {
  11793. /*!
  11794. @brief an iterator value
  11795. @note This structure could easily be a union, but MSVC currently does not allow
  11796. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11797. */
  11798. template<typename BasicJsonType> struct internal_iterator
  11799. {
  11800. /// iterator for JSON objects
  11801. typename BasicJsonType::object_t::iterator object_iterator {};
  11802. /// iterator for JSON arrays
  11803. typename BasicJsonType::array_t::iterator array_iterator {};
  11804. /// generic iterator for all other types
  11805. primitive_iterator_t primitive_iterator {};
  11806. };
  11807. } // namespace detail
  11808. NLOHMANN_JSON_NAMESPACE_END
  11809. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11810. // __ _____ _____ _____
  11811. // __| | __| | | | JSON for Modern C++
  11812. // | | |__ | | | | | | version 3.12.0
  11813. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11814. //
  11815. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  11816. // SPDX-License-Identifier: MIT
  11817. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11818. #include <type_traits> // conditional, is_const, remove_const
  11819. // #include <nlohmann/detail/exceptions.hpp>
  11820. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11821. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11822. // #include <nlohmann/detail/macro_scope.hpp>
  11823. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11824. // #include <nlohmann/detail/meta/type_traits.hpp>
  11825. // #include <nlohmann/detail/value_t.hpp>
  11826. NLOHMANN_JSON_NAMESPACE_BEGIN
  11827. namespace detail
  11828. {
  11829. // forward declare, to be able to friend it later on
  11830. template<typename IteratorType> class iteration_proxy;
  11831. template<typename IteratorType> class iteration_proxy_value;
  11832. /*!
  11833. @brief a template for a bidirectional iterator for the @ref basic_json class
  11834. This class implements a both iterators (iterator and const_iterator) for the
  11835. @ref basic_json class.
  11836. @note An iterator is called *initialized* when a pointer to a JSON value has
  11837. been set (e.g., by a constructor or a copy assignment). If the iterator is
  11838. default-constructed, it is *uninitialized* and most methods are undefined.
  11839. **The library uses assertions to detect calls on uninitialized iterators.**
  11840. @requirement The class satisfies the following concept requirements:
  11841. -
  11842. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11843. The iterator that can be moved can be moved in both directions (i.e.
  11844. incremented and decremented).
  11845. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  11846. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  11847. */
  11848. template<typename BasicJsonType>
  11849. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  11850. {
  11851. /// the iterator with BasicJsonType of different const-ness
  11852. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  11853. /// allow basic_json to access private members
  11854. friend other_iter_impl;
  11855. friend BasicJsonType;
  11856. friend iteration_proxy<iter_impl>;
  11857. friend iteration_proxy_value<iter_impl>;
  11858. using object_t = typename BasicJsonType::object_t;
  11859. using array_t = typename BasicJsonType::array_t;
  11860. // make sure BasicJsonType is basic_json or const basic_json
  11861. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  11862. "iter_impl only accepts (const) basic_json");
  11863. // superficial check for the LegacyBidirectionalIterator named requirement
  11864. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  11865. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  11866. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  11867. public:
  11868. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  11869. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  11870. /// A user-defined iterator should provide publicly accessible typedefs named
  11871. /// iterator_category, value_type, difference_type, pointer, and reference.
  11872. /// Note that value_type is required to be non-const, even for constant iterators.
  11873. using iterator_category = std::bidirectional_iterator_tag;
  11874. /// the type of the values when the iterator is dereferenced
  11875. using value_type = typename BasicJsonType::value_type;
  11876. /// a type to represent differences between iterators
  11877. using difference_type = typename BasicJsonType::difference_type;
  11878. /// defines a pointer to the type iterated over (value_type)
  11879. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  11880. typename BasicJsonType::const_pointer,
  11881. typename BasicJsonType::pointer>::type;
  11882. /// defines a reference to the type iterated over (value_type)
  11883. using reference =
  11884. typename std::conditional<std::is_const<BasicJsonType>::value,
  11885. typename BasicJsonType::const_reference,
  11886. typename BasicJsonType::reference>::type;
  11887. iter_impl() = default;
  11888. ~iter_impl() = default;
  11889. iter_impl(iter_impl&&) noexcept = default;
  11890. iter_impl& operator=(iter_impl&&) noexcept = default;
  11891. /*!
  11892. @brief constructor for a given JSON instance
  11893. @param[in] object pointer to a JSON object for this iterator
  11894. @pre object != nullptr
  11895. @post The iterator is initialized; i.e. `m_object != nullptr`.
  11896. */
  11897. explicit iter_impl(pointer object) noexcept : m_object(object)
  11898. {
  11899. JSON_ASSERT(m_object != nullptr);
  11900. switch (m_object->m_data.m_type)
  11901. {
  11902. case value_t::object:
  11903. {
  11904. m_it.object_iterator = typename object_t::iterator();
  11905. break;
  11906. }
  11907. case value_t::array:
  11908. {
  11909. m_it.array_iterator = typename array_t::iterator();
  11910. break;
  11911. }
  11912. case value_t::null:
  11913. case value_t::string:
  11914. case value_t::boolean:
  11915. case value_t::number_integer:
  11916. case value_t::number_unsigned:
  11917. case value_t::number_float:
  11918. case value_t::binary:
  11919. case value_t::discarded:
  11920. default:
  11921. {
  11922. m_it.primitive_iterator = primitive_iterator_t();
  11923. break;
  11924. }
  11925. }
  11926. }
  11927. /*!
  11928. @note The conventional copy constructor and copy assignment are implicitly
  11929. defined. Combined with the following converting constructor and
  11930. assignment, they support: (1) copy from iterator to iterator, (2)
  11931. copy from const iterator to const iterator, and (3) conversion from
  11932. iterator to const iterator. However conversion from const iterator
  11933. to iterator is not defined.
  11934. */
  11935. /*!
  11936. @brief const copy constructor
  11937. @param[in] other const iterator to copy from
  11938. @note This copy constructor had to be defined explicitly to circumvent a bug
  11939. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  11940. information refer to: https://github.com/nlohmann/json/issues/1608
  11941. */
  11942. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  11943. : m_object(other.m_object), m_it(other.m_it)
  11944. {}
  11945. /*!
  11946. @brief converting assignment
  11947. @param[in] other const iterator to copy from
  11948. @return const/non-const iterator
  11949. @note It is not checked whether @a other is initialized.
  11950. */
  11951. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  11952. {
  11953. if (&other != this)
  11954. {
  11955. m_object = other.m_object;
  11956. m_it = other.m_it;
  11957. }
  11958. return *this;
  11959. }
  11960. /*!
  11961. @brief converting constructor
  11962. @param[in] other non-const iterator to copy from
  11963. @note It is not checked whether @a other is initialized.
  11964. */
  11965. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  11966. : m_object(other.m_object), m_it(other.m_it)
  11967. {}
  11968. /*!
  11969. @brief converting assignment
  11970. @param[in] other non-const iterator to copy from
  11971. @return const/non-const iterator
  11972. @note It is not checked whether @a other is initialized.
  11973. */
  11974. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  11975. {
  11976. m_object = other.m_object;
  11977. m_it = other.m_it;
  11978. return *this;
  11979. }
  11980. JSON_PRIVATE_UNLESS_TESTED:
  11981. /*!
  11982. @brief set the iterator to the first value
  11983. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11984. */
  11985. void set_begin() noexcept
  11986. {
  11987. JSON_ASSERT(m_object != nullptr);
  11988. switch (m_object->m_data.m_type)
  11989. {
  11990. case value_t::object:
  11991. {
  11992. m_it.object_iterator = m_object->m_data.m_value.object->begin();
  11993. break;
  11994. }
  11995. case value_t::array:
  11996. {
  11997. m_it.array_iterator = m_object->m_data.m_value.array->begin();
  11998. break;
  11999. }
  12000. case value_t::null:
  12001. {
  12002. // set to end so begin()==end() is true: null is empty
  12003. m_it.primitive_iterator.set_end();
  12004. break;
  12005. }
  12006. case value_t::string:
  12007. case value_t::boolean:
  12008. case value_t::number_integer:
  12009. case value_t::number_unsigned:
  12010. case value_t::number_float:
  12011. case value_t::binary:
  12012. case value_t::discarded:
  12013. default:
  12014. {
  12015. m_it.primitive_iterator.set_begin();
  12016. break;
  12017. }
  12018. }
  12019. }
  12020. /*!
  12021. @brief set the iterator past the last value
  12022. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12023. */
  12024. void set_end() noexcept
  12025. {
  12026. JSON_ASSERT(m_object != nullptr);
  12027. switch (m_object->m_data.m_type)
  12028. {
  12029. case value_t::object:
  12030. {
  12031. m_it.object_iterator = m_object->m_data.m_value.object->end();
  12032. break;
  12033. }
  12034. case value_t::array:
  12035. {
  12036. m_it.array_iterator = m_object->m_data.m_value.array->end();
  12037. break;
  12038. }
  12039. case value_t::null:
  12040. case value_t::string:
  12041. case value_t::boolean:
  12042. case value_t::number_integer:
  12043. case value_t::number_unsigned:
  12044. case value_t::number_float:
  12045. case value_t::binary:
  12046. case value_t::discarded:
  12047. default:
  12048. {
  12049. m_it.primitive_iterator.set_end();
  12050. break;
  12051. }
  12052. }
  12053. }
  12054. public:
  12055. /*!
  12056. @brief return a reference to the value pointed to by the iterator
  12057. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12058. */
  12059. reference operator*() const
  12060. {
  12061. JSON_ASSERT(m_object != nullptr);
  12062. switch (m_object->m_data.m_type)
  12063. {
  12064. case value_t::object:
  12065. {
  12066. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  12067. return m_it.object_iterator->second;
  12068. }
  12069. case value_t::array:
  12070. {
  12071. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  12072. return *m_it.array_iterator;
  12073. }
  12074. case value_t::null:
  12075. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12076. case value_t::string:
  12077. case value_t::boolean:
  12078. case value_t::number_integer:
  12079. case value_t::number_unsigned:
  12080. case value_t::number_float:
  12081. case value_t::binary:
  12082. case value_t::discarded:
  12083. default:
  12084. {
  12085. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  12086. {
  12087. return *m_object;
  12088. }
  12089. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12090. }
  12091. }
  12092. }
  12093. /*!
  12094. @brief dereference the iterator
  12095. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12096. */
  12097. pointer operator->() const
  12098. {
  12099. JSON_ASSERT(m_object != nullptr);
  12100. switch (m_object->m_data.m_type)
  12101. {
  12102. case value_t::object:
  12103. {
  12104. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  12105. return &(m_it.object_iterator->second);
  12106. }
  12107. case value_t::array:
  12108. {
  12109. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  12110. return &*m_it.array_iterator;
  12111. }
  12112. case value_t::null:
  12113. case value_t::string:
  12114. case value_t::boolean:
  12115. case value_t::number_integer:
  12116. case value_t::number_unsigned:
  12117. case value_t::number_float:
  12118. case value_t::binary:
  12119. case value_t::discarded:
  12120. default:
  12121. {
  12122. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  12123. {
  12124. return m_object;
  12125. }
  12126. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12127. }
  12128. }
  12129. }
  12130. /*!
  12131. @brief post-increment (it++)
  12132. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12133. */
  12134. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  12135. {
  12136. auto result = *this;
  12137. ++(*this);
  12138. return result;
  12139. }
  12140. /*!
  12141. @brief pre-increment (++it)
  12142. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12143. */
  12144. iter_impl& operator++()
  12145. {
  12146. JSON_ASSERT(m_object != nullptr);
  12147. switch (m_object->m_data.m_type)
  12148. {
  12149. case value_t::object:
  12150. {
  12151. std::advance(m_it.object_iterator, 1);
  12152. break;
  12153. }
  12154. case value_t::array:
  12155. {
  12156. std::advance(m_it.array_iterator, 1);
  12157. break;
  12158. }
  12159. case value_t::null:
  12160. case value_t::string:
  12161. case value_t::boolean:
  12162. case value_t::number_integer:
  12163. case value_t::number_unsigned:
  12164. case value_t::number_float:
  12165. case value_t::binary:
  12166. case value_t::discarded:
  12167. default:
  12168. {
  12169. ++m_it.primitive_iterator;
  12170. break;
  12171. }
  12172. }
  12173. return *this;
  12174. }
  12175. /*!
  12176. @brief post-decrement (it--)
  12177. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12178. */
  12179. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  12180. {
  12181. auto result = *this;
  12182. --(*this);
  12183. return result;
  12184. }
  12185. /*!
  12186. @brief pre-decrement (--it)
  12187. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12188. */
  12189. iter_impl& operator--()
  12190. {
  12191. JSON_ASSERT(m_object != nullptr);
  12192. switch (m_object->m_data.m_type)
  12193. {
  12194. case value_t::object:
  12195. {
  12196. std::advance(m_it.object_iterator, -1);
  12197. break;
  12198. }
  12199. case value_t::array:
  12200. {
  12201. std::advance(m_it.array_iterator, -1);
  12202. break;
  12203. }
  12204. case value_t::null:
  12205. case value_t::string:
  12206. case value_t::boolean:
  12207. case value_t::number_integer:
  12208. case value_t::number_unsigned:
  12209. case value_t::number_float:
  12210. case value_t::binary:
  12211. case value_t::discarded:
  12212. default:
  12213. {
  12214. --m_it.primitive_iterator;
  12215. break;
  12216. }
  12217. }
  12218. return *this;
  12219. }
  12220. /*!
  12221. @brief comparison: equal
  12222. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12223. */
  12224. template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
  12225. bool operator==(const IterImpl& other) const
  12226. {
  12227. // if objects are not the same, the comparison is undefined
  12228. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  12229. {
  12230. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  12231. }
  12232. // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
  12233. if (m_object == nullptr)
  12234. {
  12235. return true;
  12236. }
  12237. switch (m_object->m_data.m_type)
  12238. {
  12239. case value_t::object:
  12240. return (m_it.object_iterator == other.m_it.object_iterator);
  12241. case value_t::array:
  12242. return (m_it.array_iterator == other.m_it.array_iterator);
  12243. case value_t::null:
  12244. case value_t::string:
  12245. case value_t::boolean:
  12246. case value_t::number_integer:
  12247. case value_t::number_unsigned:
  12248. case value_t::number_float:
  12249. case value_t::binary:
  12250. case value_t::discarded:
  12251. default:
  12252. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  12253. }
  12254. }
  12255. /*!
  12256. @brief comparison: not equal
  12257. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12258. */
  12259. template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
  12260. bool operator!=(const IterImpl& other) const
  12261. {
  12262. return !operator==(other);
  12263. }
  12264. /*!
  12265. @brief comparison: smaller
  12266. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12267. */
  12268. bool operator<(const iter_impl& other) const
  12269. {
  12270. // if objects are not the same, the comparison is undefined
  12271. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  12272. {
  12273. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  12274. }
  12275. // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
  12276. if (m_object == nullptr)
  12277. {
  12278. // the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
  12279. return false;
  12280. }
  12281. switch (m_object->m_data.m_type)
  12282. {
  12283. case value_t::object:
  12284. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  12285. case value_t::array:
  12286. return (m_it.array_iterator < other.m_it.array_iterator);
  12287. case value_t::null:
  12288. case value_t::string:
  12289. case value_t::boolean:
  12290. case value_t::number_integer:
  12291. case value_t::number_unsigned:
  12292. case value_t::number_float:
  12293. case value_t::binary:
  12294. case value_t::discarded:
  12295. default:
  12296. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  12297. }
  12298. }
  12299. /*!
  12300. @brief comparison: less than or equal
  12301. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12302. */
  12303. bool operator<=(const iter_impl& other) const
  12304. {
  12305. return !other.operator < (*this);
  12306. }
  12307. /*!
  12308. @brief comparison: greater than
  12309. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12310. */
  12311. bool operator>(const iter_impl& other) const
  12312. {
  12313. return !operator<=(other);
  12314. }
  12315. /*!
  12316. @brief comparison: greater than or equal
  12317. @pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
  12318. */
  12319. bool operator>=(const iter_impl& other) const
  12320. {
  12321. return !operator<(other);
  12322. }
  12323. /*!
  12324. @brief add to iterator
  12325. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12326. */
  12327. iter_impl& operator+=(difference_type i)
  12328. {
  12329. JSON_ASSERT(m_object != nullptr);
  12330. switch (m_object->m_data.m_type)
  12331. {
  12332. case value_t::object:
  12333. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  12334. case value_t::array:
  12335. {
  12336. std::advance(m_it.array_iterator, i);
  12337. break;
  12338. }
  12339. case value_t::null:
  12340. case value_t::string:
  12341. case value_t::boolean:
  12342. case value_t::number_integer:
  12343. case value_t::number_unsigned:
  12344. case value_t::number_float:
  12345. case value_t::binary:
  12346. case value_t::discarded:
  12347. default:
  12348. {
  12349. m_it.primitive_iterator += i;
  12350. break;
  12351. }
  12352. }
  12353. return *this;
  12354. }
  12355. /*!
  12356. @brief subtract from iterator
  12357. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12358. */
  12359. iter_impl& operator-=(difference_type i)
  12360. {
  12361. return operator+=(-i);
  12362. }
  12363. /*!
  12364. @brief add to iterator
  12365. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12366. */
  12367. iter_impl operator+(difference_type i) const
  12368. {
  12369. auto result = *this;
  12370. result += i;
  12371. return result;
  12372. }
  12373. /*!
  12374. @brief addition of distance and iterator
  12375. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12376. */
  12377. friend iter_impl operator+(difference_type i, const iter_impl& it)
  12378. {
  12379. auto result = it;
  12380. result += i;
  12381. return result;
  12382. }
  12383. /*!
  12384. @brief subtract from iterator
  12385. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12386. */
  12387. iter_impl operator-(difference_type i) const
  12388. {
  12389. auto result = *this;
  12390. result -= i;
  12391. return result;
  12392. }
  12393. /*!
  12394. @brief return difference
  12395. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12396. */
  12397. difference_type operator-(const iter_impl& other) const
  12398. {
  12399. JSON_ASSERT(m_object != nullptr);
  12400. switch (m_object->m_data.m_type)
  12401. {
  12402. case value_t::object:
  12403. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  12404. case value_t::array:
  12405. return m_it.array_iterator - other.m_it.array_iterator;
  12406. case value_t::null:
  12407. case value_t::string:
  12408. case value_t::boolean:
  12409. case value_t::number_integer:
  12410. case value_t::number_unsigned:
  12411. case value_t::number_float:
  12412. case value_t::binary:
  12413. case value_t::discarded:
  12414. default:
  12415. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  12416. }
  12417. }
  12418. /*!
  12419. @brief access to successor
  12420. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12421. */
  12422. reference operator[](difference_type n) const
  12423. {
  12424. JSON_ASSERT(m_object != nullptr);
  12425. switch (m_object->m_data.m_type)
  12426. {
  12427. case value_t::object:
  12428. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  12429. case value_t::array:
  12430. return *std::next(m_it.array_iterator, n);
  12431. case value_t::null:
  12432. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12433. case value_t::string:
  12434. case value_t::boolean:
  12435. case value_t::number_integer:
  12436. case value_t::number_unsigned:
  12437. case value_t::number_float:
  12438. case value_t::binary:
  12439. case value_t::discarded:
  12440. default:
  12441. {
  12442. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  12443. {
  12444. return *m_object;
  12445. }
  12446. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12447. }
  12448. }
  12449. }
  12450. /*!
  12451. @brief return the key of an object iterator
  12452. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12453. */
  12454. const typename object_t::key_type& key() const
  12455. {
  12456. JSON_ASSERT(m_object != nullptr);
  12457. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  12458. {
  12459. return m_it.object_iterator->first;
  12460. }
  12461. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  12462. }
  12463. /*!
  12464. @brief return the value of an iterator
  12465. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12466. */
  12467. reference value() const
  12468. {
  12469. return operator*();
  12470. }
  12471. JSON_PRIVATE_UNLESS_TESTED:
  12472. /// associated JSON instance
  12473. pointer m_object = nullptr;
  12474. /// the actual iterator of the associated instance
  12475. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  12476. };
  12477. } // namespace detail
  12478. NLOHMANN_JSON_NAMESPACE_END
  12479. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  12480. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  12481. // __ _____ _____ _____
  12482. // __| | __| | | | JSON for Modern C++
  12483. // | | |__ | | | | | | version 3.12.0
  12484. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12485. //
  12486. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  12487. // SPDX-License-Identifier: MIT
  12488. #include <cstddef> // ptrdiff_t
  12489. #include <iterator> // reverse_iterator
  12490. #include <utility> // declval
  12491. // #include <nlohmann/detail/abi_macros.hpp>
  12492. NLOHMANN_JSON_NAMESPACE_BEGIN
  12493. namespace detail
  12494. {
  12495. //////////////////////
  12496. // reverse_iterator //
  12497. //////////////////////
  12498. /*!
  12499. @brief a template for a reverse iterator class
  12500. @tparam Base the base iterator type to reverse. Valid types are @ref
  12501. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  12502. create @ref const_reverse_iterator).
  12503. @requirement The class satisfies the following concept requirements:
  12504. -
  12505. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  12506. The iterator that can be moved can be moved in both directions (i.e.
  12507. incremented and decremented).
  12508. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  12509. It is possible to write to the pointed-to element (only if @a Base is
  12510. @ref iterator).
  12511. @since version 1.0.0
  12512. */
  12513. template<typename Base>
  12514. class json_reverse_iterator : public std::reverse_iterator<Base>
  12515. {
  12516. public:
  12517. using difference_type = std::ptrdiff_t;
  12518. /// shortcut to the reverse iterator adapter
  12519. using base_iterator = std::reverse_iterator<Base>;
  12520. /// the reference type for the pointed-to element
  12521. using reference = typename Base::reference;
  12522. /// create reverse iterator from iterator
  12523. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  12524. : base_iterator(it) {}
  12525. /// create reverse iterator from base class
  12526. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  12527. /// post-increment (it++)
  12528. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  12529. {
  12530. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  12531. }
  12532. /// pre-increment (++it)
  12533. json_reverse_iterator& operator++()
  12534. {
  12535. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  12536. }
  12537. /// post-decrement (it--)
  12538. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  12539. {
  12540. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  12541. }
  12542. /// pre-decrement (--it)
  12543. json_reverse_iterator& operator--()
  12544. {
  12545. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  12546. }
  12547. /// add to iterator
  12548. json_reverse_iterator& operator+=(difference_type i)
  12549. {
  12550. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  12551. }
  12552. /// add to iterator
  12553. json_reverse_iterator operator+(difference_type i) const
  12554. {
  12555. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  12556. }
  12557. /// subtract from iterator
  12558. json_reverse_iterator operator-(difference_type i) const
  12559. {
  12560. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  12561. }
  12562. /// return difference
  12563. difference_type operator-(const json_reverse_iterator& other) const
  12564. {
  12565. return base_iterator(*this) - base_iterator(other);
  12566. }
  12567. /// access to successor
  12568. reference operator[](difference_type n) const
  12569. {
  12570. return *(this->operator+(n));
  12571. }
  12572. /// return the key of an object iterator
  12573. auto key() const -> decltype(std::declval<Base>().key())
  12574. {
  12575. auto it = --this->base();
  12576. return it.key();
  12577. }
  12578. /// return the value of an iterator
  12579. reference value() const
  12580. {
  12581. auto it = --this->base();
  12582. return it.operator * ();
  12583. }
  12584. };
  12585. } // namespace detail
  12586. NLOHMANN_JSON_NAMESPACE_END
  12587. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  12588. // #include <nlohmann/detail/json_custom_base_class.hpp>
  12589. // __ _____ _____ _____
  12590. // __| | __| | | | JSON for Modern C++
  12591. // | | |__ | | | | | | version 3.12.0
  12592. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12593. //
  12594. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  12595. // SPDX-License-Identifier: MIT
  12596. #include <type_traits> // conditional, is_same
  12597. // #include <nlohmann/detail/abi_macros.hpp>
  12598. NLOHMANN_JSON_NAMESPACE_BEGIN
  12599. namespace detail
  12600. {
  12601. /*!
  12602. @brief Default base class of the @ref basic_json class.
  12603. So that the correct implementations of the copy / move ctors / assign operators
  12604. of @ref basic_json do not require complex case distinctions
  12605. (no base class / custom base class used as customization point),
  12606. @ref basic_json always has a base class.
  12607. By default, this class is used because it is empty and thus has no effect
  12608. on the behavior of @ref basic_json.
  12609. */
  12610. struct json_default_base {};
  12611. template<class T>
  12612. using json_base_class = typename std::conditional <
  12613. std::is_same<T, void>::value,
  12614. json_default_base,
  12615. T
  12616. >::type;
  12617. } // namespace detail
  12618. NLOHMANN_JSON_NAMESPACE_END
  12619. // #include <nlohmann/detail/json_pointer.hpp>
  12620. // __ _____ _____ _____
  12621. // __| | __| | | | JSON for Modern C++
  12622. // | | |__ | | | | | | version 3.12.0
  12623. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12624. //
  12625. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  12626. // SPDX-License-Identifier: MIT
  12627. #include <algorithm> // all_of
  12628. #include <cctype> // isdigit
  12629. #include <cerrno> // errno, ERANGE
  12630. #include <cstdlib> // strtoull
  12631. #ifndef JSON_NO_IO
  12632. #include <iosfwd> // ostream
  12633. #endif // JSON_NO_IO
  12634. #include <limits> // max
  12635. #include <numeric> // accumulate
  12636. #include <string> // string
  12637. #include <utility> // move
  12638. #include <vector> // vector
  12639. // #include <nlohmann/detail/exceptions.hpp>
  12640. // #include <nlohmann/detail/macro_scope.hpp>
  12641. // #include <nlohmann/detail/string_concat.hpp>
  12642. // #include <nlohmann/detail/string_escape.hpp>
  12643. // #include <nlohmann/detail/value_t.hpp>
  12644. NLOHMANN_JSON_NAMESPACE_BEGIN
  12645. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  12646. /// @sa https://json.nlohmann.me/api/json_pointer/
  12647. template<typename RefStringType>
  12648. class json_pointer
  12649. {
  12650. // allow basic_json to access private members
  12651. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12652. friend class basic_json;
  12653. template<typename>
  12654. friend class json_pointer;
  12655. template<typename T>
  12656. struct string_t_helper
  12657. {
  12658. using type = T;
  12659. };
  12660. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12661. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  12662. {
  12663. using type = StringType;
  12664. };
  12665. public:
  12666. // for backwards compatibility accept BasicJsonType
  12667. using string_t = typename string_t_helper<RefStringType>::type;
  12668. /// @brief create JSON pointer
  12669. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  12670. explicit json_pointer(const string_t& s = "")
  12671. : reference_tokens(split(s))
  12672. {}
  12673. /// @brief return a string representation of the JSON pointer
  12674. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  12675. string_t to_string() const
  12676. {
  12677. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  12678. string_t{},
  12679. [](const string_t& a, const string_t& b)
  12680. {
  12681. return detail::concat(a, '/', detail::escape(b));
  12682. });
  12683. }
  12684. /// @brief return a string representation of the JSON pointer
  12685. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  12686. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  12687. operator string_t() const
  12688. {
  12689. return to_string();
  12690. }
  12691. #ifndef JSON_NO_IO
  12692. /// @brief write string representation of the JSON pointer to stream
  12693. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  12694. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  12695. {
  12696. o << ptr.to_string();
  12697. return o;
  12698. }
  12699. #endif
  12700. /// @brief append another JSON pointer at the end of this JSON pointer
  12701. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12702. json_pointer& operator/=(const json_pointer& ptr)
  12703. {
  12704. reference_tokens.insert(reference_tokens.end(),
  12705. ptr.reference_tokens.begin(),
  12706. ptr.reference_tokens.end());
  12707. return *this;
  12708. }
  12709. /// @brief append an unescaped reference token at the end of this JSON pointer
  12710. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12711. json_pointer& operator/=(string_t token)
  12712. {
  12713. push_back(std::move(token));
  12714. return *this;
  12715. }
  12716. /// @brief append an array index at the end of this JSON pointer
  12717. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12718. json_pointer& operator/=(std::size_t array_idx)
  12719. {
  12720. return *this /= std::to_string(array_idx);
  12721. }
  12722. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  12723. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12724. friend json_pointer operator/(const json_pointer& lhs,
  12725. const json_pointer& rhs)
  12726. {
  12727. return json_pointer(lhs) /= rhs;
  12728. }
  12729. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  12730. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12731. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12732. {
  12733. return json_pointer(lhs) /= std::move(token);
  12734. }
  12735. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12736. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12737. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12738. {
  12739. return json_pointer(lhs) /= array_idx;
  12740. }
  12741. /// @brief returns the parent of this JSON pointer
  12742. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12743. json_pointer parent_pointer() const
  12744. {
  12745. if (empty())
  12746. {
  12747. return *this;
  12748. }
  12749. json_pointer res = *this;
  12750. res.pop_back();
  12751. return res;
  12752. }
  12753. /// @brief remove last reference token
  12754. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12755. void pop_back()
  12756. {
  12757. if (JSON_HEDLEY_UNLIKELY(empty()))
  12758. {
  12759. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12760. }
  12761. reference_tokens.pop_back();
  12762. }
  12763. /// @brief return last reference token
  12764. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12765. const string_t& back() const
  12766. {
  12767. if (JSON_HEDLEY_UNLIKELY(empty()))
  12768. {
  12769. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12770. }
  12771. return reference_tokens.back();
  12772. }
  12773. /// @brief append an unescaped token at the end of the reference pointer
  12774. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12775. void push_back(const string_t& token)
  12776. {
  12777. reference_tokens.push_back(token);
  12778. }
  12779. /// @brief append an unescaped token at the end of the reference pointer
  12780. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12781. void push_back(string_t&& token)
  12782. {
  12783. reference_tokens.push_back(std::move(token));
  12784. }
  12785. /// @brief return whether pointer points to the root document
  12786. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12787. bool empty() const noexcept
  12788. {
  12789. return reference_tokens.empty();
  12790. }
  12791. private:
  12792. /*!
  12793. @param[in] s reference token to be converted into an array index
  12794. @return integer representation of @a s
  12795. @throw parse_error.106 if an array index begins with '0'
  12796. @throw parse_error.109 if an array index begins not with a digit
  12797. @throw out_of_range.404 if string @a s could not be converted to an integer
  12798. @throw out_of_range.410 if an array index exceeds size_type
  12799. */
  12800. template<typename BasicJsonType>
  12801. static typename BasicJsonType::size_type array_index(const string_t& s)
  12802. {
  12803. using size_type = typename BasicJsonType::size_type;
  12804. // error condition (cf. RFC 6901, Sect. 4)
  12805. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12806. {
  12807. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12808. }
  12809. // error condition (cf. RFC 6901, Sect. 4)
  12810. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12811. {
  12812. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12813. }
  12814. const char* p = s.c_str();
  12815. char* p_end = nullptr; // NOLINT(misc-const-correctness)
  12816. errno = 0; // strtoull doesn't reset errno
  12817. const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12818. if (p == p_end // invalid input or empty string
  12819. || errno == ERANGE // out of range
  12820. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12821. {
  12822. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12823. }
  12824. // only triggered on special platforms (like 32bit), see also
  12825. // https://github.com/nlohmann/json/pull/2203
  12826. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12827. {
  12828. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12829. }
  12830. return static_cast<size_type>(res);
  12831. }
  12832. JSON_PRIVATE_UNLESS_TESTED:
  12833. json_pointer top() const
  12834. {
  12835. if (JSON_HEDLEY_UNLIKELY(empty()))
  12836. {
  12837. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12838. }
  12839. json_pointer result = *this;
  12840. result.reference_tokens = {reference_tokens[0]};
  12841. return result;
  12842. }
  12843. private:
  12844. /*!
  12845. @brief create and return a reference to the pointed to value
  12846. @complexity Linear in the number of reference tokens.
  12847. @throw parse_error.109 if array index is not a number
  12848. @throw type_error.313 if value cannot be unflattened
  12849. */
  12850. template<typename BasicJsonType>
  12851. BasicJsonType& get_and_create(BasicJsonType& j) const
  12852. {
  12853. auto* result = &j;
  12854. // in case no reference tokens exist, return a reference to the JSON value
  12855. // j which will be overwritten by a primitive value
  12856. for (const auto& reference_token : reference_tokens)
  12857. {
  12858. switch (result->type())
  12859. {
  12860. case detail::value_t::null:
  12861. {
  12862. if (reference_token == "0")
  12863. {
  12864. // start a new array if reference token is 0
  12865. result = &result->operator[](0);
  12866. }
  12867. else
  12868. {
  12869. // start a new object otherwise
  12870. result = &result->operator[](reference_token);
  12871. }
  12872. break;
  12873. }
  12874. case detail::value_t::object:
  12875. {
  12876. // create an entry in the object
  12877. result = &result->operator[](reference_token);
  12878. break;
  12879. }
  12880. case detail::value_t::array:
  12881. {
  12882. // create an entry in the array
  12883. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  12884. break;
  12885. }
  12886. /*
  12887. The following code is only reached if there exists a reference
  12888. token _and_ the current value is primitive. In this case, we have
  12889. an error situation, because primitive values may only occur as
  12890. single value; that is, with an empty list of reference tokens.
  12891. */
  12892. case detail::value_t::string:
  12893. case detail::value_t::boolean:
  12894. case detail::value_t::number_integer:
  12895. case detail::value_t::number_unsigned:
  12896. case detail::value_t::number_float:
  12897. case detail::value_t::binary:
  12898. case detail::value_t::discarded:
  12899. default:
  12900. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  12901. }
  12902. }
  12903. return *result;
  12904. }
  12905. /*!
  12906. @brief return a reference to the pointed to value
  12907. @note This version does not throw if a value is not present, but tries to
  12908. create nested values instead. For instance, calling this function
  12909. with pointer `"/this/that"` on a null value is equivalent to calling
  12910. `operator[]("this").operator[]("that")` on that value, effectively
  12911. changing the null value to an object.
  12912. @param[in] ptr a JSON value
  12913. @return reference to the JSON value pointed to by the JSON pointer
  12914. @complexity Linear in the length of the JSON pointer.
  12915. @throw parse_error.106 if an array index begins with '0'
  12916. @throw parse_error.109 if an array index was not a number
  12917. @throw out_of_range.404 if the JSON pointer can not be resolved
  12918. */
  12919. template<typename BasicJsonType>
  12920. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  12921. {
  12922. for (const auto& reference_token : reference_tokens)
  12923. {
  12924. // convert null values to arrays or objects before continuing
  12925. if (ptr->is_null())
  12926. {
  12927. // check if reference token is a number
  12928. const bool nums =
  12929. std::all_of(reference_token.begin(), reference_token.end(),
  12930. [](const unsigned char x)
  12931. {
  12932. return std::isdigit(x);
  12933. });
  12934. // change value to array for numbers or "-" or to object otherwise
  12935. *ptr = (nums || reference_token == "-")
  12936. ? detail::value_t::array
  12937. : detail::value_t::object;
  12938. }
  12939. switch (ptr->type())
  12940. {
  12941. case detail::value_t::object:
  12942. {
  12943. // use unchecked object access
  12944. ptr = &ptr->operator[](reference_token);
  12945. break;
  12946. }
  12947. case detail::value_t::array:
  12948. {
  12949. if (reference_token == "-")
  12950. {
  12951. // explicitly treat "-" as index beyond the end
  12952. ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
  12953. }
  12954. else
  12955. {
  12956. // convert array index to number; unchecked access
  12957. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12958. }
  12959. break;
  12960. }
  12961. case detail::value_t::null:
  12962. case detail::value_t::string:
  12963. case detail::value_t::boolean:
  12964. case detail::value_t::number_integer:
  12965. case detail::value_t::number_unsigned:
  12966. case detail::value_t::number_float:
  12967. case detail::value_t::binary:
  12968. case detail::value_t::discarded:
  12969. default:
  12970. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12971. }
  12972. }
  12973. return *ptr;
  12974. }
  12975. /*!
  12976. @throw parse_error.106 if an array index begins with '0'
  12977. @throw parse_error.109 if an array index was not a number
  12978. @throw out_of_range.402 if the array index '-' is used
  12979. @throw out_of_range.404 if the JSON pointer can not be resolved
  12980. */
  12981. template<typename BasicJsonType>
  12982. BasicJsonType& get_checked(BasicJsonType* ptr) const
  12983. {
  12984. for (const auto& reference_token : reference_tokens)
  12985. {
  12986. switch (ptr->type())
  12987. {
  12988. case detail::value_t::object:
  12989. {
  12990. // note: at performs range check
  12991. ptr = &ptr->at(reference_token);
  12992. break;
  12993. }
  12994. case detail::value_t::array:
  12995. {
  12996. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12997. {
  12998. // "-" always fails the range check
  12999. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  13000. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  13001. ") is out of range"), ptr));
  13002. }
  13003. // note: at performs range check
  13004. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  13005. break;
  13006. }
  13007. case detail::value_t::null:
  13008. case detail::value_t::string:
  13009. case detail::value_t::boolean:
  13010. case detail::value_t::number_integer:
  13011. case detail::value_t::number_unsigned:
  13012. case detail::value_t::number_float:
  13013. case detail::value_t::binary:
  13014. case detail::value_t::discarded:
  13015. default:
  13016. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13017. }
  13018. }
  13019. return *ptr;
  13020. }
  13021. /*!
  13022. @brief return a const reference to the pointed to value
  13023. @param[in] ptr a JSON value
  13024. @return const reference to the JSON value pointed to by the JSON
  13025. pointer
  13026. @throw parse_error.106 if an array index begins with '0'
  13027. @throw parse_error.109 if an array index was not a number
  13028. @throw out_of_range.402 if the array index '-' is used
  13029. @throw out_of_range.404 if the JSON pointer can not be resolved
  13030. */
  13031. template<typename BasicJsonType>
  13032. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  13033. {
  13034. for (const auto& reference_token : reference_tokens)
  13035. {
  13036. switch (ptr->type())
  13037. {
  13038. case detail::value_t::object:
  13039. {
  13040. // use unchecked object access
  13041. ptr = &ptr->operator[](reference_token);
  13042. break;
  13043. }
  13044. case detail::value_t::array:
  13045. {
  13046. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13047. {
  13048. // "-" cannot be used for const access
  13049. JSON_THROW(detail::out_of_range::create(402, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr));
  13050. }
  13051. // use unchecked array access
  13052. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  13053. break;
  13054. }
  13055. case detail::value_t::null:
  13056. case detail::value_t::string:
  13057. case detail::value_t::boolean:
  13058. case detail::value_t::number_integer:
  13059. case detail::value_t::number_unsigned:
  13060. case detail::value_t::number_float:
  13061. case detail::value_t::binary:
  13062. case detail::value_t::discarded:
  13063. default:
  13064. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13065. }
  13066. }
  13067. return *ptr;
  13068. }
  13069. /*!
  13070. @throw parse_error.106 if an array index begins with '0'
  13071. @throw parse_error.109 if an array index was not a number
  13072. @throw out_of_range.402 if the array index '-' is used
  13073. @throw out_of_range.404 if the JSON pointer can not be resolved
  13074. */
  13075. template<typename BasicJsonType>
  13076. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  13077. {
  13078. for (const auto& reference_token : reference_tokens)
  13079. {
  13080. switch (ptr->type())
  13081. {
  13082. case detail::value_t::object:
  13083. {
  13084. // note: at performs range check
  13085. ptr = &ptr->at(reference_token);
  13086. break;
  13087. }
  13088. case detail::value_t::array:
  13089. {
  13090. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13091. {
  13092. // "-" always fails the range check
  13093. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  13094. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  13095. ") is out of range"), ptr));
  13096. }
  13097. // note: at performs range check
  13098. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  13099. break;
  13100. }
  13101. case detail::value_t::null:
  13102. case detail::value_t::string:
  13103. case detail::value_t::boolean:
  13104. case detail::value_t::number_integer:
  13105. case detail::value_t::number_unsigned:
  13106. case detail::value_t::number_float:
  13107. case detail::value_t::binary:
  13108. case detail::value_t::discarded:
  13109. default:
  13110. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13111. }
  13112. }
  13113. return *ptr;
  13114. }
  13115. /*!
  13116. @throw parse_error.106 if an array index begins with '0'
  13117. @throw parse_error.109 if an array index was not a number
  13118. */
  13119. template<typename BasicJsonType>
  13120. bool contains(const BasicJsonType* ptr) const
  13121. {
  13122. for (const auto& reference_token : reference_tokens)
  13123. {
  13124. switch (ptr->type())
  13125. {
  13126. case detail::value_t::object:
  13127. {
  13128. if (!ptr->contains(reference_token))
  13129. {
  13130. // we did not find the key in the object
  13131. return false;
  13132. }
  13133. ptr = &ptr->operator[](reference_token);
  13134. break;
  13135. }
  13136. case detail::value_t::array:
  13137. {
  13138. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13139. {
  13140. // "-" always fails the range check
  13141. return false;
  13142. }
  13143. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  13144. {
  13145. // invalid char
  13146. return false;
  13147. }
  13148. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  13149. {
  13150. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  13151. {
  13152. // first char should be between '1' and '9'
  13153. return false;
  13154. }
  13155. for (std::size_t i = 1; i < reference_token.size(); i++)
  13156. {
  13157. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  13158. {
  13159. // other char should be between '0' and '9'
  13160. return false;
  13161. }
  13162. }
  13163. }
  13164. const auto idx = array_index<BasicJsonType>(reference_token);
  13165. if (idx >= ptr->size())
  13166. {
  13167. // index out of range
  13168. return false;
  13169. }
  13170. ptr = &ptr->operator[](idx);
  13171. break;
  13172. }
  13173. case detail::value_t::null:
  13174. case detail::value_t::string:
  13175. case detail::value_t::boolean:
  13176. case detail::value_t::number_integer:
  13177. case detail::value_t::number_unsigned:
  13178. case detail::value_t::number_float:
  13179. case detail::value_t::binary:
  13180. case detail::value_t::discarded:
  13181. default:
  13182. {
  13183. // we do not expect primitive values if there is still a
  13184. // reference token to process
  13185. return false;
  13186. }
  13187. }
  13188. }
  13189. // no reference token left means we found a primitive value
  13190. return true;
  13191. }
  13192. /*!
  13193. @brief split the string input to reference tokens
  13194. @note This function is only called by the json_pointer constructor.
  13195. All exceptions below are documented there.
  13196. @throw parse_error.107 if the pointer is not empty or begins with '/'
  13197. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  13198. */
  13199. static std::vector<string_t> split(const string_t& reference_string)
  13200. {
  13201. std::vector<string_t> result;
  13202. // special case: empty reference string -> no reference tokens
  13203. if (reference_string.empty())
  13204. {
  13205. return result;
  13206. }
  13207. // check if nonempty reference string begins with slash
  13208. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  13209. {
  13210. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  13211. }
  13212. // extract the reference tokens:
  13213. // - slash: position of the last read slash (or end of string)
  13214. // - start: position after the previous slash
  13215. for (
  13216. // search for the first slash after the first character
  13217. std::size_t slash = reference_string.find_first_of('/', 1),
  13218. // set the beginning of the first reference token
  13219. start = 1;
  13220. // we can stop if start == 0 (if slash == string_t::npos)
  13221. start != 0;
  13222. // set the beginning of the next reference token
  13223. // (will eventually be 0 if slash == string_t::npos)
  13224. start = (slash == string_t::npos) ? 0 : slash + 1,
  13225. // find next slash
  13226. slash = reference_string.find_first_of('/', start))
  13227. {
  13228. // use the text between the beginning of the reference token
  13229. // (start) and the last slash (slash).
  13230. auto reference_token = reference_string.substr(start, slash - start);
  13231. // check reference tokens are properly escaped
  13232. for (std::size_t pos = reference_token.find_first_of('~');
  13233. pos != string_t::npos;
  13234. pos = reference_token.find_first_of('~', pos + 1))
  13235. {
  13236. JSON_ASSERT(reference_token[pos] == '~');
  13237. // ~ must be followed by 0 or 1
  13238. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  13239. (reference_token[pos + 1] != '0' &&
  13240. reference_token[pos + 1] != '1')))
  13241. {
  13242. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  13243. }
  13244. }
  13245. // finally, store the reference token
  13246. detail::unescape(reference_token);
  13247. result.push_back(reference_token);
  13248. }
  13249. return result;
  13250. }
  13251. private:
  13252. /*!
  13253. @param[in] reference_string the reference string to the current value
  13254. @param[in] value the value to consider
  13255. @param[in,out] result the result object to insert values to
  13256. @note Empty objects or arrays are flattened to `null`.
  13257. */
  13258. template<typename BasicJsonType>
  13259. static void flatten(const string_t& reference_string,
  13260. const BasicJsonType& value,
  13261. BasicJsonType& result)
  13262. {
  13263. switch (value.type())
  13264. {
  13265. case detail::value_t::array:
  13266. {
  13267. if (value.m_data.m_value.array->empty())
  13268. {
  13269. // flatten empty array as null
  13270. result[reference_string] = nullptr;
  13271. }
  13272. else
  13273. {
  13274. // iterate array and use index as reference string
  13275. for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
  13276. {
  13277. flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
  13278. value.m_data.m_value.array->operator[](i), result);
  13279. }
  13280. }
  13281. break;
  13282. }
  13283. case detail::value_t::object:
  13284. {
  13285. if (value.m_data.m_value.object->empty())
  13286. {
  13287. // flatten empty object as null
  13288. result[reference_string] = nullptr;
  13289. }
  13290. else
  13291. {
  13292. // iterate object and use keys as reference string
  13293. for (const auto& element : *value.m_data.m_value.object)
  13294. {
  13295. flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
  13296. }
  13297. }
  13298. break;
  13299. }
  13300. case detail::value_t::null:
  13301. case detail::value_t::string:
  13302. case detail::value_t::boolean:
  13303. case detail::value_t::number_integer:
  13304. case detail::value_t::number_unsigned:
  13305. case detail::value_t::number_float:
  13306. case detail::value_t::binary:
  13307. case detail::value_t::discarded:
  13308. default:
  13309. {
  13310. // add primitive value with its reference string
  13311. result[reference_string] = value;
  13312. break;
  13313. }
  13314. }
  13315. }
  13316. /*!
  13317. @param[in] value flattened JSON
  13318. @return unflattened JSON
  13319. @throw parse_error.109 if array index is not a number
  13320. @throw type_error.314 if value is not an object
  13321. @throw type_error.315 if object values are not primitive
  13322. @throw type_error.313 if value cannot be unflattened
  13323. */
  13324. template<typename BasicJsonType>
  13325. static BasicJsonType
  13326. unflatten(const BasicJsonType& value)
  13327. {
  13328. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  13329. {
  13330. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  13331. }
  13332. BasicJsonType result;
  13333. // iterate the JSON object values
  13334. for (const auto& element : *value.m_data.m_value.object)
  13335. {
  13336. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  13337. {
  13338. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  13339. }
  13340. // assign value to reference pointed to by JSON pointer; Note that if
  13341. // the JSON pointer is "" (i.e., points to the whole value), function
  13342. // get_and_create returns a reference to result itself. An assignment
  13343. // will then create a primitive value.
  13344. json_pointer(element.first).get_and_create(result) = element.second;
  13345. }
  13346. return result;
  13347. }
  13348. // can't use conversion operator because of ambiguity
  13349. json_pointer<string_t> convert() const&
  13350. {
  13351. json_pointer<string_t> result;
  13352. result.reference_tokens = reference_tokens;
  13353. return result;
  13354. }
  13355. json_pointer<string_t> convert()&&
  13356. {
  13357. json_pointer<string_t> result;
  13358. result.reference_tokens = std::move(reference_tokens);
  13359. return result;
  13360. }
  13361. public:
  13362. #if JSON_HAS_THREE_WAY_COMPARISON
  13363. /// @brief compares two JSON pointers for equality
  13364. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13365. template<typename RefStringTypeRhs>
  13366. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  13367. {
  13368. return reference_tokens == rhs.reference_tokens;
  13369. }
  13370. /// @brief compares JSON pointer and string for equality
  13371. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13372. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  13373. bool operator==(const string_t& rhs) const
  13374. {
  13375. return *this == json_pointer(rhs);
  13376. }
  13377. /// @brief 3-way compares two JSON pointers
  13378. template<typename RefStringTypeRhs>
  13379. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  13380. {
  13381. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  13382. }
  13383. #else
  13384. /// @brief compares two JSON pointers for equality
  13385. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13386. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13387. // NOLINTNEXTLINE(readability-redundant-declaration)
  13388. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13389. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  13390. /// @brief compares JSON pointer and string for equality
  13391. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13392. template<typename RefStringTypeLhs, typename StringType>
  13393. // NOLINTNEXTLINE(readability-redundant-declaration)
  13394. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13395. const StringType& rhs);
  13396. /// @brief compares string and JSON pointer for equality
  13397. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13398. template<typename RefStringTypeRhs, typename StringType>
  13399. // NOLINTNEXTLINE(readability-redundant-declaration)
  13400. friend bool operator==(const StringType& lhs,
  13401. const json_pointer<RefStringTypeRhs>& rhs);
  13402. /// @brief compares two JSON pointers for inequality
  13403. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  13404. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13405. // NOLINTNEXTLINE(readability-redundant-declaration)
  13406. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13407. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  13408. /// @brief compares JSON pointer and string for inequality
  13409. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  13410. template<typename RefStringTypeLhs, typename StringType>
  13411. // NOLINTNEXTLINE(readability-redundant-declaration)
  13412. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13413. const StringType& rhs);
  13414. /// @brief compares string and JSON pointer for inequality
  13415. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  13416. template<typename RefStringTypeRhs, typename StringType>
  13417. // NOLINTNEXTLINE(readability-redundant-declaration)
  13418. friend bool operator!=(const StringType& lhs,
  13419. const json_pointer<RefStringTypeRhs>& rhs);
  13420. /// @brief compares two JSON pointer for less-than
  13421. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13422. // NOLINTNEXTLINE(readability-redundant-declaration)
  13423. friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  13424. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  13425. #endif
  13426. private:
  13427. /// the reference tokens
  13428. std::vector<string_t> reference_tokens;
  13429. };
  13430. #if !JSON_HAS_THREE_WAY_COMPARISON
  13431. // functions cannot be defined inside class due to ODR violations
  13432. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13433. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13434. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  13435. {
  13436. return lhs.reference_tokens == rhs.reference_tokens;
  13437. }
  13438. template<typename RefStringTypeLhs,
  13439. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  13440. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  13441. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13442. const StringType& rhs)
  13443. {
  13444. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  13445. }
  13446. template<typename RefStringTypeRhs,
  13447. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  13448. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  13449. inline bool operator==(const StringType& lhs,
  13450. const json_pointer<RefStringTypeRhs>& rhs)
  13451. {
  13452. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  13453. }
  13454. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13455. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13456. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  13457. {
  13458. return !(lhs == rhs);
  13459. }
  13460. template<typename RefStringTypeLhs,
  13461. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  13462. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  13463. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13464. const StringType& rhs)
  13465. {
  13466. return !(lhs == rhs);
  13467. }
  13468. template<typename RefStringTypeRhs,
  13469. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  13470. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  13471. inline bool operator!=(const StringType& lhs,
  13472. const json_pointer<RefStringTypeRhs>& rhs)
  13473. {
  13474. return !(lhs == rhs);
  13475. }
  13476. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13477. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  13478. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  13479. {
  13480. return lhs.reference_tokens < rhs.reference_tokens;
  13481. }
  13482. #endif
  13483. NLOHMANN_JSON_NAMESPACE_END
  13484. // #include <nlohmann/detail/json_ref.hpp>
  13485. // __ _____ _____ _____
  13486. // __| | __| | | | JSON for Modern C++
  13487. // | | |__ | | | | | | version 3.12.0
  13488. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  13489. //
  13490. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  13491. // SPDX-License-Identifier: MIT
  13492. #include <initializer_list>
  13493. #include <utility>
  13494. // #include <nlohmann/detail/abi_macros.hpp>
  13495. // #include <nlohmann/detail/meta/type_traits.hpp>
  13496. NLOHMANN_JSON_NAMESPACE_BEGIN
  13497. namespace detail
  13498. {
  13499. template<typename BasicJsonType>
  13500. class json_ref
  13501. {
  13502. public:
  13503. using value_type = BasicJsonType;
  13504. json_ref(value_type&& value)
  13505. : owned_value(std::move(value))
  13506. {}
  13507. json_ref(const value_type& value)
  13508. : value_ref(&value)
  13509. {}
  13510. json_ref(std::initializer_list<json_ref> init)
  13511. : owned_value(init)
  13512. {}
  13513. template <
  13514. class... Args,
  13515. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  13516. json_ref(Args && ... args)
  13517. : owned_value(std::forward<Args>(args)...)
  13518. {}
  13519. // class should be movable only
  13520. json_ref(json_ref&&) noexcept = default;
  13521. json_ref(const json_ref&) = delete;
  13522. json_ref& operator=(const json_ref&) = delete;
  13523. json_ref& operator=(json_ref&&) = delete;
  13524. ~json_ref() = default;
  13525. value_type moved_or_copied() const
  13526. {
  13527. if (value_ref == nullptr)
  13528. {
  13529. return std::move(owned_value);
  13530. }
  13531. return *value_ref;
  13532. }
  13533. value_type const& operator*() const
  13534. {
  13535. return value_ref ? *value_ref : owned_value;
  13536. }
  13537. value_type const* operator->() const
  13538. {
  13539. return &** this;
  13540. }
  13541. private:
  13542. mutable value_type owned_value = nullptr;
  13543. value_type const* value_ref = nullptr;
  13544. };
  13545. } // namespace detail
  13546. NLOHMANN_JSON_NAMESPACE_END
  13547. // #include <nlohmann/detail/macro_scope.hpp>
  13548. // #include <nlohmann/detail/string_concat.hpp>
  13549. // #include <nlohmann/detail/string_escape.hpp>
  13550. // #include <nlohmann/detail/string_utils.hpp>
  13551. // #include <nlohmann/detail/meta/cpp_future.hpp>
  13552. // #include <nlohmann/detail/meta/type_traits.hpp>
  13553. // #include <nlohmann/detail/output/binary_writer.hpp>
  13554. // __ _____ _____ _____
  13555. // __| | __| | | | JSON for Modern C++
  13556. // | | |__ | | | | | | version 3.12.0
  13557. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  13558. //
  13559. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  13560. // SPDX-License-Identifier: MIT
  13561. #include <algorithm> // reverse
  13562. #include <array> // array
  13563. #include <map> // map
  13564. #include <cmath> // isnan, isinf
  13565. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  13566. #include <cstring> // memcpy
  13567. #include <limits> // numeric_limits
  13568. #include <string> // string
  13569. #include <utility> // move
  13570. #include <vector> // vector
  13571. // #include <nlohmann/detail/input/binary_reader.hpp>
  13572. // #include <nlohmann/detail/macro_scope.hpp>
  13573. // #include <nlohmann/detail/output/output_adapters.hpp>
  13574. // __ _____ _____ _____
  13575. // __| | __| | | | JSON for Modern C++
  13576. // | | |__ | | | | | | version 3.12.0
  13577. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  13578. //
  13579. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  13580. // SPDX-License-Identifier: MIT
  13581. #include <algorithm> // copy
  13582. #include <cstddef> // size_t
  13583. #include <iterator> // back_inserter
  13584. #include <memory> // shared_ptr, make_shared
  13585. #include <string> // basic_string
  13586. #include <vector> // vector
  13587. #ifndef JSON_NO_IO
  13588. #include <ios> // streamsize
  13589. #include <ostream> // basic_ostream
  13590. #endif // JSON_NO_IO
  13591. // #include <nlohmann/detail/macro_scope.hpp>
  13592. NLOHMANN_JSON_NAMESPACE_BEGIN
  13593. namespace detail
  13594. {
  13595. /// abstract output adapter interface
  13596. template<typename CharType> struct output_adapter_protocol
  13597. {
  13598. virtual void write_character(CharType c) = 0;
  13599. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  13600. virtual ~output_adapter_protocol() = default;
  13601. output_adapter_protocol() = default;
  13602. output_adapter_protocol(const output_adapter_protocol&) = default;
  13603. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  13604. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  13605. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  13606. };
  13607. /// a type to simplify interfaces
  13608. template<typename CharType>
  13609. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  13610. /// output adapter for byte vectors
  13611. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  13612. class output_vector_adapter : public output_adapter_protocol<CharType>
  13613. {
  13614. public:
  13615. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  13616. : v(vec)
  13617. {}
  13618. void write_character(CharType c) override
  13619. {
  13620. v.push_back(c);
  13621. }
  13622. JSON_HEDLEY_NON_NULL(2)
  13623. void write_characters(const CharType* s, std::size_t length) override
  13624. {
  13625. v.insert(v.end(), s, s + length);
  13626. }
  13627. private:
  13628. std::vector<CharType, AllocatorType>& v;
  13629. };
  13630. #ifndef JSON_NO_IO
  13631. /// output adapter for output streams
  13632. template<typename CharType>
  13633. class output_stream_adapter : public output_adapter_protocol<CharType>
  13634. {
  13635. public:
  13636. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  13637. : stream(s)
  13638. {}
  13639. void write_character(CharType c) override
  13640. {
  13641. stream.put(c);
  13642. }
  13643. JSON_HEDLEY_NON_NULL(2)
  13644. void write_characters(const CharType* s, std::size_t length) override
  13645. {
  13646. stream.write(s, static_cast<std::streamsize>(length));
  13647. }
  13648. private:
  13649. std::basic_ostream<CharType>& stream;
  13650. };
  13651. #endif // JSON_NO_IO
  13652. /// output adapter for basic_string
  13653. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13654. class output_string_adapter : public output_adapter_protocol<CharType>
  13655. {
  13656. public:
  13657. explicit output_string_adapter(StringType& s) noexcept
  13658. : str(s)
  13659. {}
  13660. void write_character(CharType c) override
  13661. {
  13662. str.push_back(c);
  13663. }
  13664. JSON_HEDLEY_NON_NULL(2)
  13665. void write_characters(const CharType* s, std::size_t length) override
  13666. {
  13667. str.append(s, length);
  13668. }
  13669. private:
  13670. StringType& str;
  13671. };
  13672. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13673. class output_adapter
  13674. {
  13675. public:
  13676. template<typename AllocatorType = std::allocator<CharType>>
  13677. output_adapter(std::vector<CharType, AllocatorType>& vec)
  13678. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  13679. #ifndef JSON_NO_IO
  13680. output_adapter(std::basic_ostream<CharType>& s)
  13681. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  13682. #endif // JSON_NO_IO
  13683. output_adapter(StringType& s)
  13684. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  13685. operator output_adapter_t<CharType>()
  13686. {
  13687. return oa;
  13688. }
  13689. private:
  13690. output_adapter_t<CharType> oa = nullptr;
  13691. };
  13692. } // namespace detail
  13693. NLOHMANN_JSON_NAMESPACE_END
  13694. // #include <nlohmann/detail/string_concat.hpp>
  13695. NLOHMANN_JSON_NAMESPACE_BEGIN
  13696. namespace detail
  13697. {
  13698. /// how to encode BJData
  13699. enum class bjdata_version_t
  13700. {
  13701. draft2,
  13702. draft3,
  13703. };
  13704. ///////////////////
  13705. // binary writer //
  13706. ///////////////////
  13707. /*!
  13708. @brief serialization to CBOR and MessagePack values
  13709. */
  13710. template<typename BasicJsonType, typename CharType>
  13711. class binary_writer
  13712. {
  13713. using string_t = typename BasicJsonType::string_t;
  13714. using binary_t = typename BasicJsonType::binary_t;
  13715. using number_float_t = typename BasicJsonType::number_float_t;
  13716. public:
  13717. /*!
  13718. @brief create a binary writer
  13719. @param[in] adapter output adapter to write to
  13720. */
  13721. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  13722. {
  13723. JSON_ASSERT(oa);
  13724. }
  13725. /*!
  13726. @param[in] j JSON value to serialize
  13727. @pre j.type() == value_t::object
  13728. */
  13729. void write_bson(const BasicJsonType& j)
  13730. {
  13731. switch (j.type())
  13732. {
  13733. case value_t::object:
  13734. {
  13735. write_bson_object(*j.m_data.m_value.object);
  13736. break;
  13737. }
  13738. case value_t::null:
  13739. case value_t::array:
  13740. case value_t::string:
  13741. case value_t::boolean:
  13742. case value_t::number_integer:
  13743. case value_t::number_unsigned:
  13744. case value_t::number_float:
  13745. case value_t::binary:
  13746. case value_t::discarded:
  13747. default:
  13748. {
  13749. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13750. }
  13751. }
  13752. }
  13753. /*!
  13754. @param[in] j JSON value to serialize
  13755. */
  13756. void write_cbor(const BasicJsonType& j)
  13757. {
  13758. switch (j.type())
  13759. {
  13760. case value_t::null:
  13761. {
  13762. oa->write_character(to_char_type(0xF6));
  13763. break;
  13764. }
  13765. case value_t::boolean:
  13766. {
  13767. oa->write_character(j.m_data.m_value.boolean
  13768. ? to_char_type(0xF5)
  13769. : to_char_type(0xF4));
  13770. break;
  13771. }
  13772. case value_t::number_integer:
  13773. {
  13774. if (j.m_data.m_value.number_integer >= 0)
  13775. {
  13776. // CBOR does not differentiate between positive signed
  13777. // integers and unsigned integers. Therefore, we used the
  13778. // code from the value_t::number_unsigned case here.
  13779. if (j.m_data.m_value.number_integer <= 0x17)
  13780. {
  13781. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13782. }
  13783. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13784. {
  13785. oa->write_character(to_char_type(0x18));
  13786. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13787. }
  13788. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13789. {
  13790. oa->write_character(to_char_type(0x19));
  13791. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13792. }
  13793. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13794. {
  13795. oa->write_character(to_char_type(0x1A));
  13796. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13797. }
  13798. else
  13799. {
  13800. oa->write_character(to_char_type(0x1B));
  13801. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13802. }
  13803. }
  13804. else
  13805. {
  13806. // The conversions below encode the sign in the first
  13807. // byte, and the value is converted to a positive number.
  13808. const auto positive_number = -1 - j.m_data.m_value.number_integer;
  13809. if (j.m_data.m_value.number_integer >= -24)
  13810. {
  13811. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13812. }
  13813. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13814. {
  13815. oa->write_character(to_char_type(0x38));
  13816. write_number(static_cast<std::uint8_t>(positive_number));
  13817. }
  13818. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13819. {
  13820. oa->write_character(to_char_type(0x39));
  13821. write_number(static_cast<std::uint16_t>(positive_number));
  13822. }
  13823. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13824. {
  13825. oa->write_character(to_char_type(0x3A));
  13826. write_number(static_cast<std::uint32_t>(positive_number));
  13827. }
  13828. else
  13829. {
  13830. oa->write_character(to_char_type(0x3B));
  13831. write_number(static_cast<std::uint64_t>(positive_number));
  13832. }
  13833. }
  13834. break;
  13835. }
  13836. case value_t::number_unsigned:
  13837. {
  13838. if (j.m_data.m_value.number_unsigned <= 0x17)
  13839. {
  13840. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13841. }
  13842. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13843. {
  13844. oa->write_character(to_char_type(0x18));
  13845. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13846. }
  13847. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13848. {
  13849. oa->write_character(to_char_type(0x19));
  13850. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
  13851. }
  13852. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13853. {
  13854. oa->write_character(to_char_type(0x1A));
  13855. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
  13856. }
  13857. else
  13858. {
  13859. oa->write_character(to_char_type(0x1B));
  13860. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
  13861. }
  13862. break;
  13863. }
  13864. case value_t::number_float:
  13865. {
  13866. if (std::isnan(j.m_data.m_value.number_float))
  13867. {
  13868. // NaN is 0xf97e00 in CBOR
  13869. oa->write_character(to_char_type(0xF9));
  13870. oa->write_character(to_char_type(0x7E));
  13871. oa->write_character(to_char_type(0x00));
  13872. }
  13873. else if (std::isinf(j.m_data.m_value.number_float))
  13874. {
  13875. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  13876. oa->write_character(to_char_type(0xf9));
  13877. oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  13878. oa->write_character(to_char_type(0x00));
  13879. }
  13880. else
  13881. {
  13882. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
  13883. }
  13884. break;
  13885. }
  13886. case value_t::string:
  13887. {
  13888. // step 1: write control byte and the string length
  13889. const auto N = j.m_data.m_value.string->size();
  13890. if (N <= 0x17)
  13891. {
  13892. write_number(static_cast<std::uint8_t>(0x60 + N));
  13893. }
  13894. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13895. {
  13896. oa->write_character(to_char_type(0x78));
  13897. write_number(static_cast<std::uint8_t>(N));
  13898. }
  13899. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13900. {
  13901. oa->write_character(to_char_type(0x79));
  13902. write_number(static_cast<std::uint16_t>(N));
  13903. }
  13904. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13905. {
  13906. oa->write_character(to_char_type(0x7A));
  13907. write_number(static_cast<std::uint32_t>(N));
  13908. }
  13909. // LCOV_EXCL_START
  13910. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13911. {
  13912. oa->write_character(to_char_type(0x7B));
  13913. write_number(static_cast<std::uint64_t>(N));
  13914. }
  13915. // LCOV_EXCL_STOP
  13916. // step 2: write the string
  13917. oa->write_characters(
  13918. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13919. j.m_data.m_value.string->size());
  13920. break;
  13921. }
  13922. case value_t::array:
  13923. {
  13924. // step 1: write control byte and the array size
  13925. const auto N = j.m_data.m_value.array->size();
  13926. if (N <= 0x17)
  13927. {
  13928. write_number(static_cast<std::uint8_t>(0x80 + N));
  13929. }
  13930. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13931. {
  13932. oa->write_character(to_char_type(0x98));
  13933. write_number(static_cast<std::uint8_t>(N));
  13934. }
  13935. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13936. {
  13937. oa->write_character(to_char_type(0x99));
  13938. write_number(static_cast<std::uint16_t>(N));
  13939. }
  13940. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13941. {
  13942. oa->write_character(to_char_type(0x9A));
  13943. write_number(static_cast<std::uint32_t>(N));
  13944. }
  13945. // LCOV_EXCL_START
  13946. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13947. {
  13948. oa->write_character(to_char_type(0x9B));
  13949. write_number(static_cast<std::uint64_t>(N));
  13950. }
  13951. // LCOV_EXCL_STOP
  13952. // step 2: write each element
  13953. for (const auto& el : *j.m_data.m_value.array)
  13954. {
  13955. write_cbor(el);
  13956. }
  13957. break;
  13958. }
  13959. case value_t::binary:
  13960. {
  13961. if (j.m_data.m_value.binary->has_subtype())
  13962. {
  13963. if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  13964. {
  13965. write_number(static_cast<std::uint8_t>(0xd8));
  13966. write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
  13967. }
  13968. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  13969. {
  13970. write_number(static_cast<std::uint8_t>(0xd9));
  13971. write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
  13972. }
  13973. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  13974. {
  13975. write_number(static_cast<std::uint8_t>(0xda));
  13976. write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
  13977. }
  13978. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  13979. {
  13980. write_number(static_cast<std::uint8_t>(0xdb));
  13981. write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
  13982. }
  13983. }
  13984. // step 1: write control byte and the binary array size
  13985. const auto N = j.m_data.m_value.binary->size();
  13986. if (N <= 0x17)
  13987. {
  13988. write_number(static_cast<std::uint8_t>(0x40 + N));
  13989. }
  13990. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13991. {
  13992. oa->write_character(to_char_type(0x58));
  13993. write_number(static_cast<std::uint8_t>(N));
  13994. }
  13995. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13996. {
  13997. oa->write_character(to_char_type(0x59));
  13998. write_number(static_cast<std::uint16_t>(N));
  13999. }
  14000. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14001. {
  14002. oa->write_character(to_char_type(0x5A));
  14003. write_number(static_cast<std::uint32_t>(N));
  14004. }
  14005. // LCOV_EXCL_START
  14006. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  14007. {
  14008. oa->write_character(to_char_type(0x5B));
  14009. write_number(static_cast<std::uint64_t>(N));
  14010. }
  14011. // LCOV_EXCL_STOP
  14012. // step 2: write each element
  14013. oa->write_characters(
  14014. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  14015. N);
  14016. break;
  14017. }
  14018. case value_t::object:
  14019. {
  14020. // step 1: write control byte and the object size
  14021. const auto N = j.m_data.m_value.object->size();
  14022. if (N <= 0x17)
  14023. {
  14024. write_number(static_cast<std::uint8_t>(0xA0 + N));
  14025. }
  14026. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14027. {
  14028. oa->write_character(to_char_type(0xB8));
  14029. write_number(static_cast<std::uint8_t>(N));
  14030. }
  14031. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14032. {
  14033. oa->write_character(to_char_type(0xB9));
  14034. write_number(static_cast<std::uint16_t>(N));
  14035. }
  14036. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14037. {
  14038. oa->write_character(to_char_type(0xBA));
  14039. write_number(static_cast<std::uint32_t>(N));
  14040. }
  14041. // LCOV_EXCL_START
  14042. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  14043. {
  14044. oa->write_character(to_char_type(0xBB));
  14045. write_number(static_cast<std::uint64_t>(N));
  14046. }
  14047. // LCOV_EXCL_STOP
  14048. // step 2: write each element
  14049. for (const auto& el : *j.m_data.m_value.object)
  14050. {
  14051. write_cbor(el.first);
  14052. write_cbor(el.second);
  14053. }
  14054. break;
  14055. }
  14056. case value_t::discarded:
  14057. default:
  14058. break;
  14059. }
  14060. }
  14061. /*!
  14062. @param[in] j JSON value to serialize
  14063. */
  14064. void write_msgpack(const BasicJsonType& j)
  14065. {
  14066. switch (j.type())
  14067. {
  14068. case value_t::null: // nil
  14069. {
  14070. oa->write_character(to_char_type(0xC0));
  14071. break;
  14072. }
  14073. case value_t::boolean: // true and false
  14074. {
  14075. oa->write_character(j.m_data.m_value.boolean
  14076. ? to_char_type(0xC3)
  14077. : to_char_type(0xC2));
  14078. break;
  14079. }
  14080. case value_t::number_integer:
  14081. {
  14082. if (j.m_data.m_value.number_integer >= 0)
  14083. {
  14084. // MessagePack does not differentiate between positive
  14085. // signed integers and unsigned integers. Therefore, we used
  14086. // the code from the value_t::number_unsigned case here.
  14087. if (j.m_data.m_value.number_unsigned < 128)
  14088. {
  14089. // positive fixnum
  14090. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14091. }
  14092. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  14093. {
  14094. // uint 8
  14095. oa->write_character(to_char_type(0xCC));
  14096. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14097. }
  14098. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  14099. {
  14100. // uint 16
  14101. oa->write_character(to_char_type(0xCD));
  14102. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  14103. }
  14104. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  14105. {
  14106. // uint 32
  14107. oa->write_character(to_char_type(0xCE));
  14108. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  14109. }
  14110. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14111. {
  14112. // uint 64
  14113. oa->write_character(to_char_type(0xCF));
  14114. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  14115. }
  14116. }
  14117. else
  14118. {
  14119. if (j.m_data.m_value.number_integer >= -32)
  14120. {
  14121. // negative fixnum
  14122. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  14123. }
  14124. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  14125. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  14126. {
  14127. // int 8
  14128. oa->write_character(to_char_type(0xD0));
  14129. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  14130. }
  14131. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  14132. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  14133. {
  14134. // int 16
  14135. oa->write_character(to_char_type(0xD1));
  14136. write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
  14137. }
  14138. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  14139. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  14140. {
  14141. // int 32
  14142. oa->write_character(to_char_type(0xD2));
  14143. write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
  14144. }
  14145. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  14146. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  14147. {
  14148. // int 64
  14149. oa->write_character(to_char_type(0xD3));
  14150. write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
  14151. }
  14152. }
  14153. break;
  14154. }
  14155. case value_t::number_unsigned:
  14156. {
  14157. if (j.m_data.m_value.number_unsigned < 128)
  14158. {
  14159. // positive fixnum
  14160. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14161. }
  14162. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  14163. {
  14164. // uint 8
  14165. oa->write_character(to_char_type(0xCC));
  14166. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14167. }
  14168. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  14169. {
  14170. // uint 16
  14171. oa->write_character(to_char_type(0xCD));
  14172. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  14173. }
  14174. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  14175. {
  14176. // uint 32
  14177. oa->write_character(to_char_type(0xCE));
  14178. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  14179. }
  14180. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14181. {
  14182. // uint 64
  14183. oa->write_character(to_char_type(0xCF));
  14184. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  14185. }
  14186. break;
  14187. }
  14188. case value_t::number_float:
  14189. {
  14190. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
  14191. break;
  14192. }
  14193. case value_t::string:
  14194. {
  14195. // step 1: write control byte and the string length
  14196. const auto N = j.m_data.m_value.string->size();
  14197. if (N <= 31)
  14198. {
  14199. // fixstr
  14200. write_number(static_cast<std::uint8_t>(0xA0 | N));
  14201. }
  14202. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14203. {
  14204. // str 8
  14205. oa->write_character(to_char_type(0xD9));
  14206. write_number(static_cast<std::uint8_t>(N));
  14207. }
  14208. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14209. {
  14210. // str 16
  14211. oa->write_character(to_char_type(0xDA));
  14212. write_number(static_cast<std::uint16_t>(N));
  14213. }
  14214. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14215. {
  14216. // str 32
  14217. oa->write_character(to_char_type(0xDB));
  14218. write_number(static_cast<std::uint32_t>(N));
  14219. }
  14220. // step 2: write the string
  14221. oa->write_characters(
  14222. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  14223. j.m_data.m_value.string->size());
  14224. break;
  14225. }
  14226. case value_t::array:
  14227. {
  14228. // step 1: write control byte and the array size
  14229. const auto N = j.m_data.m_value.array->size();
  14230. if (N <= 15)
  14231. {
  14232. // fixarray
  14233. write_number(static_cast<std::uint8_t>(0x90 | N));
  14234. }
  14235. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14236. {
  14237. // array 16
  14238. oa->write_character(to_char_type(0xDC));
  14239. write_number(static_cast<std::uint16_t>(N));
  14240. }
  14241. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14242. {
  14243. // array 32
  14244. oa->write_character(to_char_type(0xDD));
  14245. write_number(static_cast<std::uint32_t>(N));
  14246. }
  14247. // step 2: write each element
  14248. for (const auto& el : *j.m_data.m_value.array)
  14249. {
  14250. write_msgpack(el);
  14251. }
  14252. break;
  14253. }
  14254. case value_t::binary:
  14255. {
  14256. // step 0: determine if the binary type has a set subtype to
  14257. // determine whether to use the ext or fixext types
  14258. const bool use_ext = j.m_data.m_value.binary->has_subtype();
  14259. // step 1: write control byte and the byte string length
  14260. const auto N = j.m_data.m_value.binary->size();
  14261. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14262. {
  14263. std::uint8_t output_type{};
  14264. bool fixed = true;
  14265. if (use_ext)
  14266. {
  14267. switch (N)
  14268. {
  14269. case 1:
  14270. output_type = 0xD4; // fixext 1
  14271. break;
  14272. case 2:
  14273. output_type = 0xD5; // fixext 2
  14274. break;
  14275. case 4:
  14276. output_type = 0xD6; // fixext 4
  14277. break;
  14278. case 8:
  14279. output_type = 0xD7; // fixext 8
  14280. break;
  14281. case 16:
  14282. output_type = 0xD8; // fixext 16
  14283. break;
  14284. default:
  14285. output_type = 0xC7; // ext 8
  14286. fixed = false;
  14287. break;
  14288. }
  14289. }
  14290. else
  14291. {
  14292. output_type = 0xC4; // bin 8
  14293. fixed = false;
  14294. }
  14295. oa->write_character(to_char_type(output_type));
  14296. if (!fixed)
  14297. {
  14298. write_number(static_cast<std::uint8_t>(N));
  14299. }
  14300. }
  14301. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14302. {
  14303. const std::uint8_t output_type = use_ext
  14304. ? 0xC8 // ext 16
  14305. : 0xC5; // bin 16
  14306. oa->write_character(to_char_type(output_type));
  14307. write_number(static_cast<std::uint16_t>(N));
  14308. }
  14309. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14310. {
  14311. const std::uint8_t output_type = use_ext
  14312. ? 0xC9 // ext 32
  14313. : 0xC6; // bin 32
  14314. oa->write_character(to_char_type(output_type));
  14315. write_number(static_cast<std::uint32_t>(N));
  14316. }
  14317. // step 1.5: if this is an ext type, write the subtype
  14318. if (use_ext)
  14319. {
  14320. write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
  14321. }
  14322. // step 2: write the byte string
  14323. oa->write_characters(
  14324. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  14325. N);
  14326. break;
  14327. }
  14328. case value_t::object:
  14329. {
  14330. // step 1: write control byte and the object size
  14331. const auto N = j.m_data.m_value.object->size();
  14332. if (N <= 15)
  14333. {
  14334. // fixmap
  14335. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  14336. }
  14337. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14338. {
  14339. // map 16
  14340. oa->write_character(to_char_type(0xDE));
  14341. write_number(static_cast<std::uint16_t>(N));
  14342. }
  14343. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14344. {
  14345. // map 32
  14346. oa->write_character(to_char_type(0xDF));
  14347. write_number(static_cast<std::uint32_t>(N));
  14348. }
  14349. // step 2: write each element
  14350. for (const auto& el : *j.m_data.m_value.object)
  14351. {
  14352. write_msgpack(el.first);
  14353. write_msgpack(el.second);
  14354. }
  14355. break;
  14356. }
  14357. case value_t::discarded:
  14358. default:
  14359. break;
  14360. }
  14361. }
  14362. /*!
  14363. @param[in] j JSON value to serialize
  14364. @param[in] use_count whether to use '#' prefixes (optimized format)
  14365. @param[in] use_type whether to use '$' prefixes (optimized format)
  14366. @param[in] add_prefix whether prefixes need to be used for this value
  14367. @param[in] use_bjdata whether write in BJData format, default is false
  14368. @param[in] bjdata_version which BJData version to use, default is draft2
  14369. */
  14370. void write_ubjson(const BasicJsonType& j, const bool use_count,
  14371. const bool use_type, const bool add_prefix = true,
  14372. const bool use_bjdata = false, const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
  14373. {
  14374. const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
  14375. switch (j.type())
  14376. {
  14377. case value_t::null:
  14378. {
  14379. if (add_prefix)
  14380. {
  14381. oa->write_character(to_char_type('Z'));
  14382. }
  14383. break;
  14384. }
  14385. case value_t::boolean:
  14386. {
  14387. if (add_prefix)
  14388. {
  14389. oa->write_character(j.m_data.m_value.boolean
  14390. ? to_char_type('T')
  14391. : to_char_type('F'));
  14392. }
  14393. break;
  14394. }
  14395. case value_t::number_integer:
  14396. {
  14397. write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
  14398. break;
  14399. }
  14400. case value_t::number_unsigned:
  14401. {
  14402. write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
  14403. break;
  14404. }
  14405. case value_t::number_float:
  14406. {
  14407. write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
  14408. break;
  14409. }
  14410. case value_t::string:
  14411. {
  14412. if (add_prefix)
  14413. {
  14414. oa->write_character(to_char_type('S'));
  14415. }
  14416. write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
  14417. oa->write_characters(
  14418. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  14419. j.m_data.m_value.string->size());
  14420. break;
  14421. }
  14422. case value_t::array:
  14423. {
  14424. if (add_prefix)
  14425. {
  14426. oa->write_character(to_char_type('['));
  14427. }
  14428. bool prefix_required = true;
  14429. if (use_type && !j.m_data.m_value.array->empty())
  14430. {
  14431. JSON_ASSERT(use_count);
  14432. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  14433. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  14434. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  14435. {
  14436. return ubjson_prefix(v, use_bjdata) == first_prefix;
  14437. });
  14438. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  14439. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  14440. {
  14441. prefix_required = false;
  14442. oa->write_character(to_char_type('$'));
  14443. oa->write_character(first_prefix);
  14444. }
  14445. }
  14446. if (use_count)
  14447. {
  14448. oa->write_character(to_char_type('#'));
  14449. write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
  14450. }
  14451. for (const auto& el : *j.m_data.m_value.array)
  14452. {
  14453. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
  14454. }
  14455. if (!use_count)
  14456. {
  14457. oa->write_character(to_char_type(']'));
  14458. }
  14459. break;
  14460. }
  14461. case value_t::binary:
  14462. {
  14463. if (add_prefix)
  14464. {
  14465. oa->write_character(to_char_type('['));
  14466. }
  14467. if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
  14468. {
  14469. JSON_ASSERT(use_count);
  14470. oa->write_character(to_char_type('$'));
  14471. oa->write_character(bjdata_draft3 ? 'B' : 'U');
  14472. }
  14473. if (use_count)
  14474. {
  14475. oa->write_character(to_char_type('#'));
  14476. write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
  14477. }
  14478. if (use_type)
  14479. {
  14480. oa->write_characters(
  14481. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  14482. j.m_data.m_value.binary->size());
  14483. }
  14484. else
  14485. {
  14486. for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
  14487. {
  14488. oa->write_character(to_char_type(bjdata_draft3 ? 'B' : 'U'));
  14489. oa->write_character(j.m_data.m_value.binary->data()[i]);
  14490. }
  14491. }
  14492. if (!use_count)
  14493. {
  14494. oa->write_character(to_char_type(']'));
  14495. }
  14496. break;
  14497. }
  14498. case value_t::object:
  14499. {
  14500. if (use_bjdata && j.m_data.m_value.object->size() == 3 && j.m_data.m_value.object->find("_ArrayType_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArraySize_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArrayData_") != j.m_data.m_value.object->end())
  14501. {
  14502. if (!write_bjdata_ndarray(*j.m_data.m_value.object, use_count, use_type, bjdata_version)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  14503. {
  14504. break;
  14505. }
  14506. }
  14507. if (add_prefix)
  14508. {
  14509. oa->write_character(to_char_type('{'));
  14510. }
  14511. bool prefix_required = true;
  14512. if (use_type && !j.m_data.m_value.object->empty())
  14513. {
  14514. JSON_ASSERT(use_count);
  14515. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  14516. const bool same_prefix = std::all_of(j.begin(), j.end(),
  14517. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  14518. {
  14519. return ubjson_prefix(v, use_bjdata) == first_prefix;
  14520. });
  14521. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  14522. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  14523. {
  14524. prefix_required = false;
  14525. oa->write_character(to_char_type('$'));
  14526. oa->write_character(first_prefix);
  14527. }
  14528. }
  14529. if (use_count)
  14530. {
  14531. oa->write_character(to_char_type('#'));
  14532. write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
  14533. }
  14534. for (const auto& el : *j.m_data.m_value.object)
  14535. {
  14536. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  14537. oa->write_characters(
  14538. reinterpret_cast<const CharType*>(el.first.c_str()),
  14539. el.first.size());
  14540. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
  14541. }
  14542. if (!use_count)
  14543. {
  14544. oa->write_character(to_char_type('}'));
  14545. }
  14546. break;
  14547. }
  14548. case value_t::discarded:
  14549. default:
  14550. break;
  14551. }
  14552. }
  14553. private:
  14554. //////////
  14555. // BSON //
  14556. //////////
  14557. /*!
  14558. @return The size of a BSON document entry header, including the id marker
  14559. and the entry name size (and its null-terminator).
  14560. */
  14561. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  14562. {
  14563. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  14564. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  14565. {
  14566. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  14567. static_cast<void>(j);
  14568. }
  14569. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  14570. }
  14571. /*!
  14572. @brief Writes the given @a element_type and @a name to the output adapter
  14573. */
  14574. void write_bson_entry_header(const string_t& name,
  14575. const std::uint8_t element_type)
  14576. {
  14577. oa->write_character(to_char_type(element_type)); // boolean
  14578. oa->write_characters(
  14579. reinterpret_cast<const CharType*>(name.c_str()),
  14580. name.size() + 1u);
  14581. }
  14582. /*!
  14583. @brief Writes a BSON element with key @a name and boolean value @a value
  14584. */
  14585. void write_bson_boolean(const string_t& name,
  14586. const bool value)
  14587. {
  14588. write_bson_entry_header(name, 0x08);
  14589. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  14590. }
  14591. /*!
  14592. @brief Writes a BSON element with key @a name and double value @a value
  14593. */
  14594. void write_bson_double(const string_t& name,
  14595. const double value)
  14596. {
  14597. write_bson_entry_header(name, 0x01);
  14598. write_number<double>(value, true);
  14599. }
  14600. /*!
  14601. @return The size of the BSON-encoded string in @a value
  14602. */
  14603. static std::size_t calc_bson_string_size(const string_t& value)
  14604. {
  14605. return sizeof(std::int32_t) + value.size() + 1ul;
  14606. }
  14607. /*!
  14608. @brief Writes a BSON element with key @a name and string value @a value
  14609. */
  14610. void write_bson_string(const string_t& name,
  14611. const string_t& value)
  14612. {
  14613. write_bson_entry_header(name, 0x02);
  14614. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  14615. oa->write_characters(
  14616. reinterpret_cast<const CharType*>(value.c_str()),
  14617. value.size() + 1);
  14618. }
  14619. /*!
  14620. @brief Writes a BSON element with key @a name and null value
  14621. */
  14622. void write_bson_null(const string_t& name)
  14623. {
  14624. write_bson_entry_header(name, 0x0A);
  14625. }
  14626. /*!
  14627. @return The size of the BSON-encoded integer @a value
  14628. */
  14629. static std::size_t calc_bson_integer_size(const std::int64_t value)
  14630. {
  14631. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  14632. ? sizeof(std::int32_t)
  14633. : sizeof(std::int64_t);
  14634. }
  14635. /*!
  14636. @brief Writes a BSON element with key @a name and integer @a value
  14637. */
  14638. void write_bson_integer(const string_t& name,
  14639. const std::int64_t value)
  14640. {
  14641. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  14642. {
  14643. write_bson_entry_header(name, 0x10); // int32
  14644. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  14645. }
  14646. else
  14647. {
  14648. write_bson_entry_header(name, 0x12); // int64
  14649. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  14650. }
  14651. }
  14652. /*!
  14653. @return The size of the BSON-encoded unsigned integer in @a j
  14654. */
  14655. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  14656. {
  14657. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14658. ? sizeof(std::int32_t)
  14659. : sizeof(std::int64_t);
  14660. }
  14661. /*!
  14662. @brief Writes a BSON element with key @a name and unsigned @a value
  14663. */
  14664. void write_bson_unsigned(const string_t& name,
  14665. const BasicJsonType& j)
  14666. {
  14667. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14668. {
  14669. write_bson_entry_header(name, 0x10 /* int32 */);
  14670. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
  14671. }
  14672. else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14673. {
  14674. write_bson_entry_header(name, 0x12 /* int64 */);
  14675. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
  14676. }
  14677. else
  14678. {
  14679. write_bson_entry_header(name, 0x11 /* uint64 */);
  14680. write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true);
  14681. }
  14682. }
  14683. /*!
  14684. @brief Writes a BSON element with key @a name and object @a value
  14685. */
  14686. void write_bson_object_entry(const string_t& name,
  14687. const typename BasicJsonType::object_t& value)
  14688. {
  14689. write_bson_entry_header(name, 0x03); // object
  14690. write_bson_object(value);
  14691. }
  14692. /*!
  14693. @return The size of the BSON-encoded array @a value
  14694. */
  14695. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  14696. {
  14697. std::size_t array_index = 0ul;
  14698. const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), static_cast<std::size_t>(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
  14699. {
  14700. return result + calc_bson_element_size(std::to_string(array_index++), el);
  14701. });
  14702. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  14703. }
  14704. /*!
  14705. @return The size of the BSON-encoded binary array @a value
  14706. */
  14707. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  14708. {
  14709. return sizeof(std::int32_t) + value.size() + 1ul;
  14710. }
  14711. /*!
  14712. @brief Writes a BSON element with key @a name and array @a value
  14713. */
  14714. void write_bson_array(const string_t& name,
  14715. const typename BasicJsonType::array_t& value)
  14716. {
  14717. write_bson_entry_header(name, 0x04); // array
  14718. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  14719. std::size_t array_index = 0ul;
  14720. for (const auto& el : value)
  14721. {
  14722. write_bson_element(std::to_string(array_index++), el);
  14723. }
  14724. oa->write_character(to_char_type(0x00));
  14725. }
  14726. /*!
  14727. @brief Writes a BSON element with key @a name and binary value @a value
  14728. */
  14729. void write_bson_binary(const string_t& name,
  14730. const binary_t& value)
  14731. {
  14732. write_bson_entry_header(name, 0x05);
  14733. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  14734. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  14735. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  14736. }
  14737. /*!
  14738. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  14739. @return The calculated size for the BSON document entry for @a j with the given @a name.
  14740. */
  14741. static std::size_t calc_bson_element_size(const string_t& name,
  14742. const BasicJsonType& j)
  14743. {
  14744. const auto header_size = calc_bson_entry_header_size(name, j);
  14745. switch (j.type())
  14746. {
  14747. case value_t::object:
  14748. return header_size + calc_bson_object_size(*j.m_data.m_value.object);
  14749. case value_t::array:
  14750. return header_size + calc_bson_array_size(*j.m_data.m_value.array);
  14751. case value_t::binary:
  14752. return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
  14753. case value_t::boolean:
  14754. return header_size + 1ul;
  14755. case value_t::number_float:
  14756. return header_size + 8ul;
  14757. case value_t::number_integer:
  14758. return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
  14759. case value_t::number_unsigned:
  14760. return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
  14761. case value_t::string:
  14762. return header_size + calc_bson_string_size(*j.m_data.m_value.string);
  14763. case value_t::null:
  14764. return header_size + 0ul;
  14765. // LCOV_EXCL_START
  14766. case value_t::discarded:
  14767. default:
  14768. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14769. return 0ul;
  14770. // LCOV_EXCL_STOP
  14771. }
  14772. }
  14773. /*!
  14774. @brief Serializes the JSON value @a j to BSON and associates it with the
  14775. key @a name.
  14776. @param name The name to associate with the JSON entity @a j within the
  14777. current BSON document
  14778. */
  14779. void write_bson_element(const string_t& name,
  14780. const BasicJsonType& j)
  14781. {
  14782. switch (j.type())
  14783. {
  14784. case value_t::object:
  14785. return write_bson_object_entry(name, *j.m_data.m_value.object);
  14786. case value_t::array:
  14787. return write_bson_array(name, *j.m_data.m_value.array);
  14788. case value_t::binary:
  14789. return write_bson_binary(name, *j.m_data.m_value.binary);
  14790. case value_t::boolean:
  14791. return write_bson_boolean(name, j.m_data.m_value.boolean);
  14792. case value_t::number_float:
  14793. return write_bson_double(name, j.m_data.m_value.number_float);
  14794. case value_t::number_integer:
  14795. return write_bson_integer(name, j.m_data.m_value.number_integer);
  14796. case value_t::number_unsigned:
  14797. return write_bson_unsigned(name, j);
  14798. case value_t::string:
  14799. return write_bson_string(name, *j.m_data.m_value.string);
  14800. case value_t::null:
  14801. return write_bson_null(name);
  14802. // LCOV_EXCL_START
  14803. case value_t::discarded:
  14804. default:
  14805. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14806. return;
  14807. // LCOV_EXCL_STOP
  14808. }
  14809. }
  14810. /*!
  14811. @brief Calculates the size of the BSON serialization of the given
  14812. JSON-object @a j.
  14813. @param[in] value JSON value to serialize
  14814. @pre value.type() == value_t::object
  14815. */
  14816. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14817. {
  14818. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14819. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14820. {
  14821. return result += calc_bson_element_size(el.first, el.second);
  14822. });
  14823. return sizeof(std::int32_t) + document_size + 1ul;
  14824. }
  14825. /*!
  14826. @param[in] value JSON value to serialize
  14827. @pre value.type() == value_t::object
  14828. */
  14829. void write_bson_object(const typename BasicJsonType::object_t& value)
  14830. {
  14831. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  14832. for (const auto& el : value)
  14833. {
  14834. write_bson_element(el.first, el.second);
  14835. }
  14836. oa->write_character(to_char_type(0x00));
  14837. }
  14838. //////////
  14839. // CBOR //
  14840. //////////
  14841. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  14842. {
  14843. return to_char_type(0xFA); // Single-Precision Float
  14844. }
  14845. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  14846. {
  14847. return to_char_type(0xFB); // Double-Precision Float
  14848. }
  14849. /////////////
  14850. // MsgPack //
  14851. /////////////
  14852. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  14853. {
  14854. return to_char_type(0xCA); // float 32
  14855. }
  14856. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  14857. {
  14858. return to_char_type(0xCB); // float 64
  14859. }
  14860. ////////////
  14861. // UBJSON //
  14862. ////////////
  14863. // UBJSON: write number (floating point)
  14864. template<typename NumberType, typename std::enable_if<
  14865. std::is_floating_point<NumberType>::value, int>::type = 0>
  14866. void write_number_with_ubjson_prefix(const NumberType n,
  14867. const bool add_prefix,
  14868. const bool use_bjdata)
  14869. {
  14870. if (add_prefix)
  14871. {
  14872. oa->write_character(get_ubjson_float_prefix(n));
  14873. }
  14874. write_number(n, use_bjdata);
  14875. }
  14876. // UBJSON: write number (unsigned integer)
  14877. template<typename NumberType, typename std::enable_if<
  14878. std::is_unsigned<NumberType>::value, int>::type = 0>
  14879. void write_number_with_ubjson_prefix(const NumberType n,
  14880. const bool add_prefix,
  14881. const bool use_bjdata)
  14882. {
  14883. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14884. {
  14885. if (add_prefix)
  14886. {
  14887. oa->write_character(to_char_type('i')); // int8
  14888. }
  14889. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14890. }
  14891. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  14892. {
  14893. if (add_prefix)
  14894. {
  14895. oa->write_character(to_char_type('U')); // uint8
  14896. }
  14897. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14898. }
  14899. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14900. {
  14901. if (add_prefix)
  14902. {
  14903. oa->write_character(to_char_type('I')); // int16
  14904. }
  14905. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14906. }
  14907. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  14908. {
  14909. if (add_prefix)
  14910. {
  14911. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14912. }
  14913. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  14914. }
  14915. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14916. {
  14917. if (add_prefix)
  14918. {
  14919. oa->write_character(to_char_type('l')); // int32
  14920. }
  14921. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14922. }
  14923. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  14924. {
  14925. if (add_prefix)
  14926. {
  14927. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14928. }
  14929. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  14930. }
  14931. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14932. {
  14933. if (add_prefix)
  14934. {
  14935. oa->write_character(to_char_type('L')); // int64
  14936. }
  14937. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14938. }
  14939. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  14940. {
  14941. if (add_prefix)
  14942. {
  14943. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  14944. }
  14945. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  14946. }
  14947. else
  14948. {
  14949. if (add_prefix)
  14950. {
  14951. oa->write_character(to_char_type('H')); // high-precision number
  14952. }
  14953. const auto number = BasicJsonType(n).dump();
  14954. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14955. for (std::size_t i = 0; i < number.size(); ++i)
  14956. {
  14957. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14958. }
  14959. }
  14960. }
  14961. // UBJSON: write number (signed integer)
  14962. template < typename NumberType, typename std::enable_if <
  14963. std::is_signed<NumberType>::value&&
  14964. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  14965. void write_number_with_ubjson_prefix(const NumberType n,
  14966. const bool add_prefix,
  14967. const bool use_bjdata)
  14968. {
  14969. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  14970. {
  14971. if (add_prefix)
  14972. {
  14973. oa->write_character(to_char_type('i')); // int8
  14974. }
  14975. write_number(static_cast<std::int8_t>(n), use_bjdata);
  14976. }
  14977. else if (static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::max)()))
  14978. {
  14979. if (add_prefix)
  14980. {
  14981. oa->write_character(to_char_type('U')); // uint8
  14982. }
  14983. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14984. }
  14985. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  14986. {
  14987. if (add_prefix)
  14988. {
  14989. oa->write_character(to_char_type('I')); // int16
  14990. }
  14991. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14992. }
  14993. else if (use_bjdata && (static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::max)())))
  14994. {
  14995. if (add_prefix)
  14996. {
  14997. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14998. }
  14999. write_number(static_cast<uint16_t>(n), use_bjdata);
  15000. }
  15001. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  15002. {
  15003. if (add_prefix)
  15004. {
  15005. oa->write_character(to_char_type('l')); // int32
  15006. }
  15007. write_number(static_cast<std::int32_t>(n), use_bjdata);
  15008. }
  15009. else if (use_bjdata && (static_cast<std::int64_t>((std::numeric_limits<std::uint32_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint32_t>::max)())))
  15010. {
  15011. if (add_prefix)
  15012. {
  15013. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  15014. }
  15015. write_number(static_cast<uint32_t>(n), use_bjdata);
  15016. }
  15017. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  15018. {
  15019. if (add_prefix)
  15020. {
  15021. oa->write_character(to_char_type('L')); // int64
  15022. }
  15023. write_number(static_cast<std::int64_t>(n), use_bjdata);
  15024. }
  15025. // LCOV_EXCL_START
  15026. else
  15027. {
  15028. if (add_prefix)
  15029. {
  15030. oa->write_character(to_char_type('H')); // high-precision number
  15031. }
  15032. const auto number = BasicJsonType(n).dump();
  15033. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  15034. for (std::size_t i = 0; i < number.size(); ++i)
  15035. {
  15036. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  15037. }
  15038. }
  15039. // LCOV_EXCL_STOP
  15040. }
  15041. /*!
  15042. @brief determine the type prefix of container values
  15043. */
  15044. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  15045. {
  15046. switch (j.type())
  15047. {
  15048. case value_t::null:
  15049. return 'Z';
  15050. case value_t::boolean:
  15051. return j.m_data.m_value.boolean ? 'T' : 'F';
  15052. case value_t::number_integer:
  15053. {
  15054. if ((std::numeric_limits<std::int8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  15055. {
  15056. return 'i';
  15057. }
  15058. if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  15059. {
  15060. return 'U';
  15061. }
  15062. if ((std::numeric_limits<std::int16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  15063. {
  15064. return 'I';
  15065. }
  15066. if (use_bjdata && ((std::numeric_limits<std::uint16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)()))
  15067. {
  15068. return 'u';
  15069. }
  15070. if ((std::numeric_limits<std::int32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  15071. {
  15072. return 'l';
  15073. }
  15074. if (use_bjdata && ((std::numeric_limits<std::uint32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)()))
  15075. {
  15076. return 'm';
  15077. }
  15078. if ((std::numeric_limits<std::int64_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  15079. {
  15080. return 'L';
  15081. }
  15082. // anything else is treated as high-precision number
  15083. return 'H'; // LCOV_EXCL_LINE
  15084. }
  15085. case value_t::number_unsigned:
  15086. {
  15087. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  15088. {
  15089. return 'i';
  15090. }
  15091. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  15092. {
  15093. return 'U';
  15094. }
  15095. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  15096. {
  15097. return 'I';
  15098. }
  15099. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  15100. {
  15101. return 'u';
  15102. }
  15103. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  15104. {
  15105. return 'l';
  15106. }
  15107. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  15108. {
  15109. return 'm';
  15110. }
  15111. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  15112. {
  15113. return 'L';
  15114. }
  15115. if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  15116. {
  15117. return 'M';
  15118. }
  15119. // anything else is treated as high-precision number
  15120. return 'H'; // LCOV_EXCL_LINE
  15121. }
  15122. case value_t::number_float:
  15123. return get_ubjson_float_prefix(j.m_data.m_value.number_float);
  15124. case value_t::string:
  15125. return 'S';
  15126. case value_t::array: // fallthrough
  15127. case value_t::binary:
  15128. return '[';
  15129. case value_t::object:
  15130. return '{';
  15131. case value_t::discarded:
  15132. default: // discarded values
  15133. return 'N';
  15134. }
  15135. }
  15136. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  15137. {
  15138. return 'd'; // float 32
  15139. }
  15140. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  15141. {
  15142. return 'D'; // float 64
  15143. }
  15144. /*!
  15145. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  15146. */
  15147. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type, const bjdata_version_t bjdata_version)
  15148. {
  15149. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  15150. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'},
  15151. {"char", 'C'}, {"byte", 'B'}
  15152. };
  15153. string_t key = "_ArrayType_";
  15154. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  15155. if (it == bjdtype.end())
  15156. {
  15157. return true;
  15158. }
  15159. CharType dtype = it->second;
  15160. key = "_ArraySize_";
  15161. std::size_t len = (value.at(key).empty() ? 0 : 1);
  15162. for (const auto& el : value.at(key))
  15163. {
  15164. len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
  15165. }
  15166. key = "_ArrayData_";
  15167. if (value.at(key).size() != len)
  15168. {
  15169. return true;
  15170. }
  15171. oa->write_character('[');
  15172. oa->write_character('$');
  15173. oa->write_character(dtype);
  15174. oa->write_character('#');
  15175. key = "_ArraySize_";
  15176. write_ubjson(value.at(key), use_count, use_type, true, true, bjdata_version);
  15177. key = "_ArrayData_";
  15178. if (dtype == 'U' || dtype == 'C' || dtype == 'B')
  15179. {
  15180. for (const auto& el : value.at(key))
  15181. {
  15182. write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
  15183. }
  15184. }
  15185. else if (dtype == 'i')
  15186. {
  15187. for (const auto& el : value.at(key))
  15188. {
  15189. write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
  15190. }
  15191. }
  15192. else if (dtype == 'u')
  15193. {
  15194. for (const auto& el : value.at(key))
  15195. {
  15196. write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
  15197. }
  15198. }
  15199. else if (dtype == 'I')
  15200. {
  15201. for (const auto& el : value.at(key))
  15202. {
  15203. write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
  15204. }
  15205. }
  15206. else if (dtype == 'm')
  15207. {
  15208. for (const auto& el : value.at(key))
  15209. {
  15210. write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
  15211. }
  15212. }
  15213. else if (dtype == 'l')
  15214. {
  15215. for (const auto& el : value.at(key))
  15216. {
  15217. write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
  15218. }
  15219. }
  15220. else if (dtype == 'M')
  15221. {
  15222. for (const auto& el : value.at(key))
  15223. {
  15224. write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
  15225. }
  15226. }
  15227. else if (dtype == 'L')
  15228. {
  15229. for (const auto& el : value.at(key))
  15230. {
  15231. write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
  15232. }
  15233. }
  15234. else if (dtype == 'd')
  15235. {
  15236. for (const auto& el : value.at(key))
  15237. {
  15238. write_number(static_cast<float>(el.m_data.m_value.number_float), true);
  15239. }
  15240. }
  15241. else if (dtype == 'D')
  15242. {
  15243. for (const auto& el : value.at(key))
  15244. {
  15245. write_number(static_cast<double>(el.m_data.m_value.number_float), true);
  15246. }
  15247. }
  15248. return false;
  15249. }
  15250. ///////////////////////
  15251. // Utility functions //
  15252. ///////////////////////
  15253. /*
  15254. @brief write a number to output input
  15255. @param[in] n number of type @a NumberType
  15256. @param[in] OutputIsLittleEndian Set to true if output data is
  15257. required to be little endian
  15258. @tparam NumberType the type of the number
  15259. @note This function needs to respect the system's endianness, because bytes
  15260. in CBOR, MessagePack, and UBJSON are stored in network order (big
  15261. endian) and therefore need reordering on little endian systems.
  15262. On the other hand, BSON and BJData use little endian and should reorder
  15263. on big endian systems.
  15264. */
  15265. template<typename NumberType>
  15266. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  15267. {
  15268. // step 1: write number to array of length NumberType
  15269. std::array<CharType, sizeof(NumberType)> vec{};
  15270. std::memcpy(vec.data(), &n, sizeof(NumberType));
  15271. // step 2: write array to output (with possible reordering)
  15272. if (is_little_endian != OutputIsLittleEndian)
  15273. {
  15274. // reverse byte order prior to conversion if necessary
  15275. std::reverse(vec.begin(), vec.end());
  15276. }
  15277. oa->write_characters(vec.data(), sizeof(NumberType));
  15278. }
  15279. void write_compact_float(const number_float_t n, detail::input_format_t format)
  15280. {
  15281. #ifdef __GNUC__
  15282. #pragma GCC diagnostic push
  15283. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15284. #endif
  15285. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  15286. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  15287. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  15288. {
  15289. oa->write_character(format == detail::input_format_t::cbor
  15290. ? get_cbor_float_prefix(static_cast<float>(n))
  15291. : get_msgpack_float_prefix(static_cast<float>(n)));
  15292. write_number(static_cast<float>(n));
  15293. }
  15294. else
  15295. {
  15296. oa->write_character(format == detail::input_format_t::cbor
  15297. ? get_cbor_float_prefix(n)
  15298. : get_msgpack_float_prefix(n));
  15299. write_number(n);
  15300. }
  15301. #ifdef __GNUC__
  15302. #pragma GCC diagnostic pop
  15303. #endif
  15304. }
  15305. public:
  15306. // The following to_char_type functions are implement the conversion
  15307. // between uint8_t and CharType. In case CharType is not unsigned,
  15308. // such a conversion is required to allow values greater than 128.
  15309. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  15310. template < typename C = CharType,
  15311. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  15312. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  15313. {
  15314. return *reinterpret_cast<char*>(&x);
  15315. }
  15316. template < typename C = CharType,
  15317. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  15318. static CharType to_char_type(std::uint8_t x) noexcept
  15319. {
  15320. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  15321. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  15322. CharType result;
  15323. std::memcpy(&result, &x, sizeof(x));
  15324. return result;
  15325. }
  15326. template<typename C = CharType,
  15327. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  15328. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  15329. {
  15330. return x;
  15331. }
  15332. template < typename InputCharType, typename C = CharType,
  15333. enable_if_t <
  15334. std::is_signed<C>::value &&
  15335. std::is_signed<char>::value &&
  15336. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  15337. > * = nullptr >
  15338. static constexpr CharType to_char_type(InputCharType x) noexcept
  15339. {
  15340. return x;
  15341. }
  15342. private:
  15343. /// whether we can assume little endianness
  15344. const bool is_little_endian = little_endianness();
  15345. /// the output
  15346. output_adapter_t<CharType> oa = nullptr;
  15347. };
  15348. } // namespace detail
  15349. NLOHMANN_JSON_NAMESPACE_END
  15350. // #include <nlohmann/detail/output/output_adapters.hpp>
  15351. // #include <nlohmann/detail/output/serializer.hpp>
  15352. // __ _____ _____ _____
  15353. // __| | __| | | | JSON for Modern C++
  15354. // | | |__ | | | | | | version 3.12.0
  15355. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  15356. //
  15357. // SPDX-FileCopyrightText: 2008 - 2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  15358. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  15359. // SPDX-License-Identifier: MIT
  15360. #include <algorithm> // reverse, remove, fill, find, none_of
  15361. #include <array> // array
  15362. #include <clocale> // localeconv, lconv
  15363. #include <cmath> // labs, isfinite, isnan, signbit
  15364. #include <cstddef> // size_t, ptrdiff_t
  15365. #include <cstdint> // uint8_t
  15366. #include <cstdio> // snprintf
  15367. #include <limits> // numeric_limits
  15368. #include <string> // string, char_traits
  15369. #include <iomanip> // setfill, setw
  15370. #include <type_traits> // is_same
  15371. #include <utility> // move
  15372. // #include <nlohmann/detail/conversions/to_chars.hpp>
  15373. // __ _____ _____ _____
  15374. // __| | __| | | | JSON for Modern C++
  15375. // | | |__ | | | | | | version 3.12.0
  15376. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  15377. //
  15378. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  15379. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  15380. // SPDX-License-Identifier: MIT
  15381. #include <array> // array
  15382. #include <cmath> // signbit, isfinite
  15383. #include <cstdint> // intN_t, uintN_t
  15384. #include <cstring> // memcpy, memmove
  15385. #include <limits> // numeric_limits
  15386. #include <type_traits> // conditional
  15387. // #include <nlohmann/detail/macro_scope.hpp>
  15388. NLOHMANN_JSON_NAMESPACE_BEGIN
  15389. namespace detail
  15390. {
  15391. /*!
  15392. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  15393. conversion.
  15394. This implementation is a slightly modified version of the reference
  15395. implementation which may be obtained from
  15396. http://florian.loitsch.com/publications (bench.tar.gz).
  15397. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  15398. For a detailed description of the algorithm see:
  15399. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  15400. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  15401. Language Design and Implementation, PLDI 2010
  15402. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  15403. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  15404. Design and Implementation, PLDI 1996
  15405. */
  15406. namespace dtoa_impl
  15407. {
  15408. template<typename Target, typename Source>
  15409. Target reinterpret_bits(const Source source)
  15410. {
  15411. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  15412. Target target;
  15413. std::memcpy(&target, &source, sizeof(Source));
  15414. return target;
  15415. }
  15416. struct diyfp // f * 2^e
  15417. {
  15418. static constexpr int kPrecision = 64; // = q
  15419. std::uint64_t f = 0;
  15420. int e = 0;
  15421. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  15422. /*!
  15423. @brief returns x - y
  15424. @pre x.e == y.e and x.f >= y.f
  15425. */
  15426. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  15427. {
  15428. JSON_ASSERT(x.e == y.e);
  15429. JSON_ASSERT(x.f >= y.f);
  15430. return {x.f - y.f, x.e};
  15431. }
  15432. /*!
  15433. @brief returns x * y
  15434. @note The result is rounded. (Only the upper q bits are returned.)
  15435. */
  15436. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  15437. {
  15438. static_assert(kPrecision == 64, "internal error");
  15439. // Computes:
  15440. // f = round((x.f * y.f) / 2^q)
  15441. // e = x.e + y.e + q
  15442. // Emulate the 64-bit * 64-bit multiplication:
  15443. //
  15444. // p = u * v
  15445. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  15446. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  15447. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  15448. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  15449. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  15450. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  15451. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  15452. //
  15453. // (Since Q might be larger than 2^32 - 1)
  15454. //
  15455. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  15456. //
  15457. // (Q_hi + H does not overflow a 64-bit int)
  15458. //
  15459. // = p_lo + 2^64 p_hi
  15460. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  15461. const std::uint64_t u_hi = x.f >> 32u;
  15462. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  15463. const std::uint64_t v_hi = y.f >> 32u;
  15464. const std::uint64_t p0 = u_lo * v_lo;
  15465. const std::uint64_t p1 = u_lo * v_hi;
  15466. const std::uint64_t p2 = u_hi * v_lo;
  15467. const std::uint64_t p3 = u_hi * v_hi;
  15468. const std::uint64_t p0_hi = p0 >> 32u;
  15469. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  15470. const std::uint64_t p1_hi = p1 >> 32u;
  15471. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  15472. const std::uint64_t p2_hi = p2 >> 32u;
  15473. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  15474. // The full product might now be computed as
  15475. //
  15476. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  15477. // p_lo = p0_lo + (Q << 32)
  15478. //
  15479. // But in this particular case here, the full p_lo is not required.
  15480. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  15481. // Q_hi + 1 does not overflow).
  15482. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  15483. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  15484. return {h, x.e + y.e + 64};
  15485. }
  15486. /*!
  15487. @brief normalize x such that the significand is >= 2^(q-1)
  15488. @pre x.f != 0
  15489. */
  15490. static diyfp normalize(diyfp x) noexcept
  15491. {
  15492. JSON_ASSERT(x.f != 0);
  15493. while ((x.f >> 63u) == 0)
  15494. {
  15495. x.f <<= 1u;
  15496. x.e--;
  15497. }
  15498. return x;
  15499. }
  15500. /*!
  15501. @brief normalize x such that the result has the exponent E
  15502. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  15503. */
  15504. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  15505. {
  15506. const int delta = x.e - target_exponent;
  15507. JSON_ASSERT(delta >= 0);
  15508. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  15509. return {x.f << delta, target_exponent};
  15510. }
  15511. };
  15512. struct boundaries
  15513. {
  15514. diyfp w;
  15515. diyfp minus;
  15516. diyfp plus;
  15517. };
  15518. /*!
  15519. Compute the (normalized) diyfp representing the input number 'value' and its
  15520. boundaries.
  15521. @pre value must be finite and positive
  15522. */
  15523. template<typename FloatType>
  15524. boundaries compute_boundaries(FloatType value)
  15525. {
  15526. JSON_ASSERT(std::isfinite(value));
  15527. JSON_ASSERT(value > 0);
  15528. // Convert the IEEE representation into a diyfp.
  15529. //
  15530. // If v is denormal:
  15531. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  15532. // If v is normalized:
  15533. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  15534. static_assert(std::numeric_limits<FloatType>::is_iec559,
  15535. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  15536. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  15537. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  15538. constexpr int kMinExp = 1 - kBias;
  15539. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  15540. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  15541. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  15542. const std::uint64_t E = bits >> (kPrecision - 1);
  15543. const std::uint64_t F = bits & (kHiddenBit - 1);
  15544. const bool is_denormal = E == 0;
  15545. const diyfp v = is_denormal
  15546. ? diyfp(F, kMinExp)
  15547. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  15548. // Compute the boundaries m- and m+ of the floating-point value
  15549. // v = f * 2^e.
  15550. //
  15551. // Determine v- and v+, the floating-point predecessor and successor if v,
  15552. // respectively.
  15553. //
  15554. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  15555. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  15556. //
  15557. // v+ = v + 2^e
  15558. //
  15559. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  15560. // between m- and m+ round to v, regardless of how the input rounding
  15561. // algorithm breaks ties.
  15562. //
  15563. // ---+-------------+-------------+-------------+-------------+--- (A)
  15564. // v- m- v m+ v+
  15565. //
  15566. // -----------------+------+------+-------------+-------------+--- (B)
  15567. // v- m- v m+ v+
  15568. const bool lower_boundary_is_closer = F == 0 && E > 1;
  15569. const diyfp m_plus = diyfp((2 * v.f) + 1, v.e - 1);
  15570. const diyfp m_minus = lower_boundary_is_closer
  15571. ? diyfp((4 * v.f) - 1, v.e - 2) // (B)
  15572. : diyfp((2 * v.f) - 1, v.e - 1); // (A)
  15573. // Determine the normalized w+ = m+.
  15574. const diyfp w_plus = diyfp::normalize(m_plus);
  15575. // Determine w- = m- such that e_(w-) = e_(w+).
  15576. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  15577. return {diyfp::normalize(v), w_minus, w_plus};
  15578. }
  15579. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  15580. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  15581. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  15582. //
  15583. // alpha <= e = e_c + e_w + q <= gamma
  15584. //
  15585. // or
  15586. //
  15587. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  15588. // <= f_c * f_w * 2^gamma
  15589. //
  15590. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  15591. //
  15592. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  15593. //
  15594. // or
  15595. //
  15596. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  15597. //
  15598. // The choice of (alpha,gamma) determines the size of the table and the form of
  15599. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  15600. // in practice:
  15601. //
  15602. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  15603. // processed independently: An integral part p1, and a fractional part p2:
  15604. //
  15605. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  15606. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  15607. // = p1 + p2 * 2^e
  15608. //
  15609. // The conversion of p1 into decimal form requires a series of divisions and
  15610. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  15611. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  15612. // achieved by choosing
  15613. //
  15614. // -e >= 32 or e <= -32 := gamma
  15615. //
  15616. // In order to convert the fractional part
  15617. //
  15618. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15619. //
  15620. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  15621. // d[-i] are extracted in order:
  15622. //
  15623. // (10 * p2) div 2^-e = d[-1]
  15624. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  15625. //
  15626. // The multiplication by 10 must not overflow. It is sufficient to choose
  15627. //
  15628. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  15629. //
  15630. // Since p2 = f mod 2^-e < 2^-e,
  15631. //
  15632. // -e <= 60 or e >= -60 := alpha
  15633. constexpr int kAlpha = -60;
  15634. constexpr int kGamma = -32;
  15635. struct cached_power // c = f * 2^e ~= 10^k
  15636. {
  15637. std::uint64_t f;
  15638. int e;
  15639. int k;
  15640. };
  15641. /*!
  15642. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  15643. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  15644. satisfies (Definition 3.2 from [1])
  15645. alpha <= e_c + e + q <= gamma.
  15646. */
  15647. inline cached_power get_cached_power_for_binary_exponent(int e)
  15648. {
  15649. // Now
  15650. //
  15651. // alpha <= e_c + e + q <= gamma (1)
  15652. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  15653. //
  15654. // and since the c's are normalized, 2^(q-1) <= f_c,
  15655. //
  15656. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  15657. // ==> 2^(alpha - e - 1) <= c
  15658. //
  15659. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  15660. //
  15661. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  15662. // = ceil( (alpha - e - 1) * log_10(2) )
  15663. //
  15664. // From the paper:
  15665. // "In theory the result of the procedure could be wrong since c is rounded,
  15666. // and the computation itself is approximated [...]. In practice, however,
  15667. // this simple function is sufficient."
  15668. //
  15669. // For IEEE double precision floating-point numbers converted into
  15670. // normalized diyfp's w = f * 2^e, with q = 64,
  15671. //
  15672. // e >= -1022 (min IEEE exponent)
  15673. // -52 (p - 1)
  15674. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  15675. // -11 (normalize the diyfp)
  15676. // = -1137
  15677. //
  15678. // and
  15679. //
  15680. // e <= +1023 (max IEEE exponent)
  15681. // -52 (p - 1)
  15682. // -11 (normalize the diyfp)
  15683. // = 960
  15684. //
  15685. // This binary exponent range [-1137,960] results in a decimal exponent
  15686. // range [-307,324]. One does not need to store a cached power for each
  15687. // k in this range. For each such k it suffices to find a cached power
  15688. // such that the exponent of the product lies in [alpha,gamma].
  15689. // This implies that the difference of the decimal exponents of adjacent
  15690. // table entries must be less than or equal to
  15691. //
  15692. // floor( (gamma - alpha) * log_10(2) ) = 8.
  15693. //
  15694. // (A smaller distance gamma-alpha would require a larger table.)
  15695. // NB:
  15696. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  15697. constexpr int kCachedPowersMinDecExp = -300;
  15698. constexpr int kCachedPowersDecStep = 8;
  15699. static constexpr std::array<cached_power, 79> kCachedPowers =
  15700. {
  15701. {
  15702. { 0xAB70FE17C79AC6CA, -1060, -300 },
  15703. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  15704. { 0xBE5691EF416BD60C, -1007, -284 },
  15705. { 0x8DD01FAD907FFC3C, -980, -276 },
  15706. { 0xD3515C2831559A83, -954, -268 },
  15707. { 0x9D71AC8FADA6C9B5, -927, -260 },
  15708. { 0xEA9C227723EE8BCB, -901, -252 },
  15709. { 0xAECC49914078536D, -874, -244 },
  15710. { 0x823C12795DB6CE57, -847, -236 },
  15711. { 0xC21094364DFB5637, -821, -228 },
  15712. { 0x9096EA6F3848984F, -794, -220 },
  15713. { 0xD77485CB25823AC7, -768, -212 },
  15714. { 0xA086CFCD97BF97F4, -741, -204 },
  15715. { 0xEF340A98172AACE5, -715, -196 },
  15716. { 0xB23867FB2A35B28E, -688, -188 },
  15717. { 0x84C8D4DFD2C63F3B, -661, -180 },
  15718. { 0xC5DD44271AD3CDBA, -635, -172 },
  15719. { 0x936B9FCEBB25C996, -608, -164 },
  15720. { 0xDBAC6C247D62A584, -582, -156 },
  15721. { 0xA3AB66580D5FDAF6, -555, -148 },
  15722. { 0xF3E2F893DEC3F126, -529, -140 },
  15723. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  15724. { 0x87625F056C7C4A8B, -475, -124 },
  15725. { 0xC9BCFF6034C13053, -449, -116 },
  15726. { 0x964E858C91BA2655, -422, -108 },
  15727. { 0xDFF9772470297EBD, -396, -100 },
  15728. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  15729. { 0xF8A95FCF88747D94, -343, -84 },
  15730. { 0xB94470938FA89BCF, -316, -76 },
  15731. { 0x8A08F0F8BF0F156B, -289, -68 },
  15732. { 0xCDB02555653131B6, -263, -60 },
  15733. { 0x993FE2C6D07B7FAC, -236, -52 },
  15734. { 0xE45C10C42A2B3B06, -210, -44 },
  15735. { 0xAA242499697392D3, -183, -36 },
  15736. { 0xFD87B5F28300CA0E, -157, -28 },
  15737. { 0xBCE5086492111AEB, -130, -20 },
  15738. { 0x8CBCCC096F5088CC, -103, -12 },
  15739. { 0xD1B71758E219652C, -77, -4 },
  15740. { 0x9C40000000000000, -50, 4 },
  15741. { 0xE8D4A51000000000, -24, 12 },
  15742. { 0xAD78EBC5AC620000, 3, 20 },
  15743. { 0x813F3978F8940984, 30, 28 },
  15744. { 0xC097CE7BC90715B3, 56, 36 },
  15745. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15746. { 0xD5D238A4ABE98068, 109, 52 },
  15747. { 0x9F4F2726179A2245, 136, 60 },
  15748. { 0xED63A231D4C4FB27, 162, 68 },
  15749. { 0xB0DE65388CC8ADA8, 189, 76 },
  15750. { 0x83C7088E1AAB65DB, 216, 84 },
  15751. { 0xC45D1DF942711D9A, 242, 92 },
  15752. { 0x924D692CA61BE758, 269, 100 },
  15753. { 0xDA01EE641A708DEA, 295, 108 },
  15754. { 0xA26DA3999AEF774A, 322, 116 },
  15755. { 0xF209787BB47D6B85, 348, 124 },
  15756. { 0xB454E4A179DD1877, 375, 132 },
  15757. { 0x865B86925B9BC5C2, 402, 140 },
  15758. { 0xC83553C5C8965D3D, 428, 148 },
  15759. { 0x952AB45CFA97A0B3, 455, 156 },
  15760. { 0xDE469FBD99A05FE3, 481, 164 },
  15761. { 0xA59BC234DB398C25, 508, 172 },
  15762. { 0xF6C69A72A3989F5C, 534, 180 },
  15763. { 0xB7DCBF5354E9BECE, 561, 188 },
  15764. { 0x88FCF317F22241E2, 588, 196 },
  15765. { 0xCC20CE9BD35C78A5, 614, 204 },
  15766. { 0x98165AF37B2153DF, 641, 212 },
  15767. { 0xE2A0B5DC971F303A, 667, 220 },
  15768. { 0xA8D9D1535CE3B396, 694, 228 },
  15769. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15770. { 0xBB764C4CA7A44410, 747, 244 },
  15771. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15772. { 0xD01FEF10A657842C, 800, 260 },
  15773. { 0x9B10A4E5E9913129, 827, 268 },
  15774. { 0xE7109BFBA19C0C9D, 853, 276 },
  15775. { 0xAC2820D9623BF429, 880, 284 },
  15776. { 0x80444B5E7AA7CF85, 907, 292 },
  15777. { 0xBF21E44003ACDD2D, 933, 300 },
  15778. { 0x8E679C2F5E44FF8F, 960, 308 },
  15779. { 0xD433179D9C8CB841, 986, 316 },
  15780. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15781. }
  15782. };
  15783. // This computation gives exactly the same results for k as
  15784. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15785. // for |e| <= 1500, but doesn't require floating-point operations.
  15786. // NB: log_10(2) ~= 78913 / 2^18
  15787. JSON_ASSERT(e >= -1500);
  15788. JSON_ASSERT(e <= 1500);
  15789. const int f = kAlpha - e - 1;
  15790. const int k = ((f * 78913) / (1 << 18)) + static_cast<int>(f > 0);
  15791. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15792. JSON_ASSERT(index >= 0);
  15793. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15794. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15795. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15796. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15797. return cached;
  15798. }
  15799. /*!
  15800. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15801. For n == 0, returns 1 and sets pow10 := 1.
  15802. */
  15803. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15804. {
  15805. // LCOV_EXCL_START
  15806. if (n >= 1000000000)
  15807. {
  15808. pow10 = 1000000000;
  15809. return 10;
  15810. }
  15811. // LCOV_EXCL_STOP
  15812. if (n >= 100000000)
  15813. {
  15814. pow10 = 100000000;
  15815. return 9;
  15816. }
  15817. if (n >= 10000000)
  15818. {
  15819. pow10 = 10000000;
  15820. return 8;
  15821. }
  15822. if (n >= 1000000)
  15823. {
  15824. pow10 = 1000000;
  15825. return 7;
  15826. }
  15827. if (n >= 100000)
  15828. {
  15829. pow10 = 100000;
  15830. return 6;
  15831. }
  15832. if (n >= 10000)
  15833. {
  15834. pow10 = 10000;
  15835. return 5;
  15836. }
  15837. if (n >= 1000)
  15838. {
  15839. pow10 = 1000;
  15840. return 4;
  15841. }
  15842. if (n >= 100)
  15843. {
  15844. pow10 = 100;
  15845. return 3;
  15846. }
  15847. if (n >= 10)
  15848. {
  15849. pow10 = 10;
  15850. return 2;
  15851. }
  15852. pow10 = 1;
  15853. return 1;
  15854. }
  15855. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  15856. std::uint64_t rest, std::uint64_t ten_k)
  15857. {
  15858. JSON_ASSERT(len >= 1);
  15859. JSON_ASSERT(dist <= delta);
  15860. JSON_ASSERT(rest <= delta);
  15861. JSON_ASSERT(ten_k > 0);
  15862. // <--------------------------- delta ---->
  15863. // <---- dist --------->
  15864. // --------------[------------------+-------------------]--------------
  15865. // M- w M+
  15866. //
  15867. // ten_k
  15868. // <------>
  15869. // <---- rest ---->
  15870. // --------------[------------------+----+--------------]--------------
  15871. // w V
  15872. // = buf * 10^k
  15873. //
  15874. // ten_k represents a unit-in-the-last-place in the decimal representation
  15875. // stored in buf.
  15876. // Decrement buf by ten_k while this takes buf closer to w.
  15877. // The tests are written in this order to avoid overflow in unsigned
  15878. // integer arithmetic.
  15879. while (rest < dist
  15880. && delta - rest >= ten_k
  15881. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  15882. {
  15883. JSON_ASSERT(buf[len - 1] != '0');
  15884. buf[len - 1]--;
  15885. rest += ten_k;
  15886. }
  15887. }
  15888. /*!
  15889. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  15890. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  15891. */
  15892. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  15893. diyfp M_minus, diyfp w, diyfp M_plus)
  15894. {
  15895. static_assert(kAlpha >= -60, "internal error");
  15896. static_assert(kGamma <= -32, "internal error");
  15897. // Generates the digits (and the exponent) of a decimal floating-point
  15898. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  15899. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  15900. //
  15901. // <--------------------------- delta ---->
  15902. // <---- dist --------->
  15903. // --------------[------------------+-------------------]--------------
  15904. // M- w M+
  15905. //
  15906. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  15907. // V is in [M-,M+].
  15908. JSON_ASSERT(M_plus.e >= kAlpha);
  15909. JSON_ASSERT(M_plus.e <= kGamma);
  15910. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  15911. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  15912. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  15913. //
  15914. // M+ = f * 2^e
  15915. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  15916. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  15917. // = p1 + p2 * 2^e
  15918. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  15919. auto p1 = static_cast<std::uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
  15920. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  15921. // 1)
  15922. //
  15923. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  15924. JSON_ASSERT(p1 > 0);
  15925. std::uint32_t pow10{};
  15926. const int k = find_largest_pow10(p1, pow10);
  15927. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  15928. //
  15929. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  15930. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  15931. //
  15932. // M+ = p1 + p2 * 2^e
  15933. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  15934. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  15935. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  15936. //
  15937. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  15938. //
  15939. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  15940. //
  15941. // but stop as soon as
  15942. //
  15943. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  15944. int n = k;
  15945. while (n > 0)
  15946. {
  15947. // Invariants:
  15948. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  15949. // pow10 = 10^(n-1) <= p1 < 10^n
  15950. //
  15951. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  15952. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  15953. //
  15954. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  15955. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  15956. //
  15957. JSON_ASSERT(d <= 9);
  15958. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15959. //
  15960. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  15961. //
  15962. p1 = r;
  15963. n--;
  15964. //
  15965. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  15966. // pow10 = 10^n
  15967. //
  15968. // Now check if enough digits have been generated.
  15969. // Compute
  15970. //
  15971. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  15972. //
  15973. // Note:
  15974. // Since rest and delta share the same exponent e, it suffices to
  15975. // compare the significands.
  15976. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  15977. if (rest <= delta)
  15978. {
  15979. // V = buffer * 10^n, with M- <= V <= M+.
  15980. decimal_exponent += n;
  15981. // We may now just stop. But instead look if the buffer could be
  15982. // decremented to bring V closer to w.
  15983. //
  15984. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  15985. // The rounding procedure works with diyfp's with an implicit
  15986. // exponent of e.
  15987. //
  15988. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  15989. //
  15990. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  15991. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  15992. return;
  15993. }
  15994. pow10 /= 10;
  15995. //
  15996. // pow10 = 10^(n-1) <= p1 < 10^n
  15997. // Invariants restored.
  15998. }
  15999. // 2)
  16000. //
  16001. // The digits of the integral part have been generated:
  16002. //
  16003. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  16004. // = buffer + p2 * 2^e
  16005. //
  16006. // Now generate the digits of the fractional part p2 * 2^e.
  16007. //
  16008. // Note:
  16009. // No decimal point is generated: the exponent is adjusted instead.
  16010. //
  16011. // p2 actually represents the fraction
  16012. //
  16013. // p2 * 2^e
  16014. // = p2 / 2^-e
  16015. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  16016. //
  16017. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  16018. //
  16019. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  16020. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  16021. //
  16022. // using
  16023. //
  16024. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  16025. // = ( d) * 2^-e + ( r)
  16026. //
  16027. // or
  16028. // 10^m * p2 * 2^e = d + r * 2^e
  16029. //
  16030. // i.e.
  16031. //
  16032. // M+ = buffer + p2 * 2^e
  16033. // = buffer + 10^-m * (d + r * 2^e)
  16034. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  16035. //
  16036. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  16037. JSON_ASSERT(p2 > delta);
  16038. int m = 0;
  16039. for (;;)
  16040. {
  16041. // Invariant:
  16042. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  16043. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  16044. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  16045. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  16046. //
  16047. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  16048. p2 *= 10;
  16049. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  16050. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  16051. //
  16052. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  16053. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  16054. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  16055. //
  16056. JSON_ASSERT(d <= 9);
  16057. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  16058. //
  16059. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  16060. //
  16061. p2 = r;
  16062. m++;
  16063. //
  16064. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  16065. // Invariant restored.
  16066. // Check if enough digits have been generated.
  16067. //
  16068. // 10^-m * p2 * 2^e <= delta * 2^e
  16069. // p2 * 2^e <= 10^m * delta * 2^e
  16070. // p2 <= 10^m * delta
  16071. delta *= 10;
  16072. dist *= 10;
  16073. if (p2 <= delta)
  16074. {
  16075. break;
  16076. }
  16077. }
  16078. // V = buffer * 10^-m, with M- <= V <= M+.
  16079. decimal_exponent -= m;
  16080. // 1 ulp in the decimal representation is now 10^-m.
  16081. // Since delta and dist are now scaled by 10^m, we need to do the
  16082. // same with ulp in order to keep the units in sync.
  16083. //
  16084. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  16085. //
  16086. const std::uint64_t ten_m = one.f;
  16087. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  16088. // By construction this algorithm generates the shortest possible decimal
  16089. // number (Loitsch, Theorem 6.2) which rounds back to w.
  16090. // For an input number of precision p, at least
  16091. //
  16092. // N = 1 + ceil(p * log_10(2))
  16093. //
  16094. // decimal digits are sufficient to identify all binary floating-point
  16095. // numbers (Matula, "In-and-Out conversions").
  16096. // This implies that the algorithm does not produce more than N decimal
  16097. // digits.
  16098. //
  16099. // N = 17 for p = 53 (IEEE double precision)
  16100. // N = 9 for p = 24 (IEEE single precision)
  16101. }
  16102. /*!
  16103. v = buf * 10^decimal_exponent
  16104. len is the length of the buffer (number of decimal digits)
  16105. The buffer must be large enough, i.e. >= max_digits10.
  16106. */
  16107. JSON_HEDLEY_NON_NULL(1)
  16108. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  16109. diyfp m_minus, diyfp v, diyfp m_plus)
  16110. {
  16111. JSON_ASSERT(m_plus.e == m_minus.e);
  16112. JSON_ASSERT(m_plus.e == v.e);
  16113. // --------(-----------------------+-----------------------)-------- (A)
  16114. // m- v m+
  16115. //
  16116. // --------------------(-----------+-----------------------)-------- (B)
  16117. // m- v m+
  16118. //
  16119. // First scale v (and m- and m+) such that the exponent is in the range
  16120. // [alpha, gamma].
  16121. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  16122. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  16123. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  16124. const diyfp w = diyfp::mul(v, c_minus_k);
  16125. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  16126. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  16127. // ----(---+---)---------------(---+---)---------------(---+---)----
  16128. // w- w w+
  16129. // = c*m- = c*v = c*m+
  16130. //
  16131. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  16132. // w+ are now off by a small amount.
  16133. // In fact:
  16134. //
  16135. // w - v * 10^k < 1 ulp
  16136. //
  16137. // To account for this inaccuracy, add resp. subtract 1 ulp.
  16138. //
  16139. // --------+---[---------------(---+---)---------------]---+--------
  16140. // w- M- w M+ w+
  16141. //
  16142. // Now any number in [M-, M+] (bounds included) will round to w when input,
  16143. // regardless of how the input rounding algorithm breaks ties.
  16144. //
  16145. // And digit_gen generates the shortest possible such number in [M-, M+].
  16146. // Note that this does not mean that Grisu2 always generates the shortest
  16147. // possible number in the interval (m-, m+).
  16148. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  16149. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  16150. decimal_exponent = -cached.k; // = -(-k) = k
  16151. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  16152. }
  16153. /*!
  16154. v = buf * 10^decimal_exponent
  16155. len is the length of the buffer (number of decimal digits)
  16156. The buffer must be large enough, i.e. >= max_digits10.
  16157. */
  16158. template<typename FloatType>
  16159. JSON_HEDLEY_NON_NULL(1)
  16160. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  16161. {
  16162. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  16163. "internal error: not enough precision");
  16164. JSON_ASSERT(std::isfinite(value));
  16165. JSON_ASSERT(value > 0);
  16166. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  16167. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  16168. // decimal representations are not exactly "short".
  16169. //
  16170. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  16171. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  16172. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  16173. // does.
  16174. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  16175. // representation using the corresponding std::from_chars function recovers value exactly". That
  16176. // indicates that single precision floating-point numbers should be recovered using
  16177. // 'std::strtof'.
  16178. //
  16179. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  16180. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  16181. // value is off by 1 ulp.
  16182. #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
  16183. const boundaries w = compute_boundaries(static_cast<double>(value));
  16184. #else
  16185. const boundaries w = compute_boundaries(value);
  16186. #endif
  16187. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  16188. }
  16189. /*!
  16190. @brief appends a decimal representation of e to buf
  16191. @return a pointer to the element following the exponent.
  16192. @pre -1000 < e < 1000
  16193. */
  16194. JSON_HEDLEY_NON_NULL(1)
  16195. JSON_HEDLEY_RETURNS_NON_NULL
  16196. inline char* append_exponent(char* buf, int e)
  16197. {
  16198. JSON_ASSERT(e > -1000);
  16199. JSON_ASSERT(e < 1000);
  16200. if (e < 0)
  16201. {
  16202. e = -e;
  16203. *buf++ = '-';
  16204. }
  16205. else
  16206. {
  16207. *buf++ = '+';
  16208. }
  16209. auto k = static_cast<std::uint32_t>(e);
  16210. if (k < 10)
  16211. {
  16212. // Always print at least two digits in the exponent.
  16213. // This is for compatibility with printf("%g").
  16214. *buf++ = '0';
  16215. *buf++ = static_cast<char>('0' + k);
  16216. }
  16217. else if (k < 100)
  16218. {
  16219. *buf++ = static_cast<char>('0' + (k / 10));
  16220. k %= 10;
  16221. *buf++ = static_cast<char>('0' + k);
  16222. }
  16223. else
  16224. {
  16225. *buf++ = static_cast<char>('0' + (k / 100));
  16226. k %= 100;
  16227. *buf++ = static_cast<char>('0' + (k / 10));
  16228. k %= 10;
  16229. *buf++ = static_cast<char>('0' + k);
  16230. }
  16231. return buf;
  16232. }
  16233. /*!
  16234. @brief prettify v = buf * 10^decimal_exponent
  16235. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  16236. notation. Otherwise it will be printed in exponential notation.
  16237. @pre min_exp < 0
  16238. @pre max_exp > 0
  16239. */
  16240. JSON_HEDLEY_NON_NULL(1)
  16241. JSON_HEDLEY_RETURNS_NON_NULL
  16242. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  16243. int min_exp, int max_exp)
  16244. {
  16245. JSON_ASSERT(min_exp < 0);
  16246. JSON_ASSERT(max_exp > 0);
  16247. const int k = len;
  16248. const int n = len + decimal_exponent;
  16249. // v = buf * 10^(n-k)
  16250. // k is the length of the buffer (number of decimal digits)
  16251. // n is the position of the decimal point relative to the start of the buffer.
  16252. if (k <= n && n <= max_exp)
  16253. {
  16254. // digits[000]
  16255. // len <= max_exp + 2
  16256. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  16257. // Make it look like a floating-point number (#362, #378)
  16258. buf[n + 0] = '.';
  16259. buf[n + 1] = '0';
  16260. return buf + (static_cast<size_t>(n) + 2);
  16261. }
  16262. if (0 < n && n <= max_exp)
  16263. {
  16264. // dig.its
  16265. // len <= max_digits10 + 1
  16266. JSON_ASSERT(k > n);
  16267. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  16268. buf[n] = '.';
  16269. return buf + (static_cast<size_t>(k) + 1U);
  16270. }
  16271. if (min_exp < n && n <= 0)
  16272. {
  16273. // 0.[000]digits
  16274. // len <= 2 + (-min_exp - 1) + max_digits10
  16275. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  16276. buf[0] = '0';
  16277. buf[1] = '.';
  16278. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  16279. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  16280. }
  16281. if (k == 1)
  16282. {
  16283. // dE+123
  16284. // len <= 1 + 5
  16285. buf += 1;
  16286. }
  16287. else
  16288. {
  16289. // d.igitsE+123
  16290. // len <= max_digits10 + 1 + 5
  16291. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  16292. buf[1] = '.';
  16293. buf += 1 + static_cast<size_t>(k);
  16294. }
  16295. *buf++ = 'e';
  16296. return append_exponent(buf, n - 1);
  16297. }
  16298. } // namespace dtoa_impl
  16299. /*!
  16300. @brief generates a decimal representation of the floating-point number value in [first, last).
  16301. The format of the resulting decimal representation is similar to printf's %g
  16302. format. Returns an iterator pointing past-the-end of the decimal representation.
  16303. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  16304. @note The buffer must be large enough.
  16305. @note The result is NOT null-terminated.
  16306. */
  16307. template<typename FloatType>
  16308. JSON_HEDLEY_NON_NULL(1, 2)
  16309. JSON_HEDLEY_RETURNS_NON_NULL
  16310. char* to_chars(char* first, const char* last, FloatType value)
  16311. {
  16312. static_cast<void>(last); // maybe unused - fix warning
  16313. JSON_ASSERT(std::isfinite(value));
  16314. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  16315. if (std::signbit(value))
  16316. {
  16317. value = -value;
  16318. *first++ = '-';
  16319. }
  16320. #ifdef __GNUC__
  16321. #pragma GCC diagnostic push
  16322. #pragma GCC diagnostic ignored "-Wfloat-equal"
  16323. #endif
  16324. if (value == 0) // +-0
  16325. {
  16326. *first++ = '0';
  16327. // Make it look like a floating-point number (#362, #378)
  16328. *first++ = '.';
  16329. *first++ = '0';
  16330. return first;
  16331. }
  16332. #ifdef __GNUC__
  16333. #pragma GCC diagnostic pop
  16334. #endif
  16335. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  16336. // Compute v = buffer * 10^decimal_exponent.
  16337. // The decimal digits are stored in the buffer, which needs to be interpreted
  16338. // as an unsigned decimal integer.
  16339. // len is the length of the buffer, i.e. the number of decimal digits.
  16340. int len = 0;
  16341. int decimal_exponent = 0;
  16342. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  16343. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  16344. // Format the buffer like printf("%.*g", prec, value)
  16345. constexpr int kMinExp = -4;
  16346. // Use digits10 here to increase compatibility with version 2.
  16347. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  16348. JSON_ASSERT(last - first >= kMaxExp + 2);
  16349. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  16350. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  16351. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  16352. }
  16353. } // namespace detail
  16354. NLOHMANN_JSON_NAMESPACE_END
  16355. // #include <nlohmann/detail/exceptions.hpp>
  16356. // #include <nlohmann/detail/macro_scope.hpp>
  16357. // #include <nlohmann/detail/meta/cpp_future.hpp>
  16358. // #include <nlohmann/detail/output/binary_writer.hpp>
  16359. // #include <nlohmann/detail/output/output_adapters.hpp>
  16360. // #include <nlohmann/detail/string_concat.hpp>
  16361. // #include <nlohmann/detail/value_t.hpp>
  16362. NLOHMANN_JSON_NAMESPACE_BEGIN
  16363. namespace detail
  16364. {
  16365. ///////////////////
  16366. // serialization //
  16367. ///////////////////
  16368. /// how to treat decoding errors
  16369. enum class error_handler_t
  16370. {
  16371. strict, ///< throw a type_error exception in case of invalid UTF-8
  16372. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  16373. ignore ///< ignore invalid UTF-8 sequences
  16374. };
  16375. template<typename BasicJsonType>
  16376. class serializer
  16377. {
  16378. using string_t = typename BasicJsonType::string_t;
  16379. using number_float_t = typename BasicJsonType::number_float_t;
  16380. using number_integer_t = typename BasicJsonType::number_integer_t;
  16381. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  16382. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  16383. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  16384. static constexpr std::uint8_t UTF8_REJECT = 1;
  16385. public:
  16386. /*!
  16387. @param[in] s output stream to serialize to
  16388. @param[in] ichar indentation character to use
  16389. @param[in] error_handler_ how to react on decoding errors
  16390. */
  16391. serializer(output_adapter_t<char> s, const char ichar,
  16392. error_handler_t error_handler_ = error_handler_t::strict)
  16393. : o(std::move(s))
  16394. , loc(std::localeconv())
  16395. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  16396. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  16397. , indent_char(ichar)
  16398. , indent_string(512, indent_char)
  16399. , error_handler(error_handler_)
  16400. {}
  16401. // delete because of pointer members
  16402. serializer(const serializer&) = delete;
  16403. serializer& operator=(const serializer&) = delete;
  16404. serializer(serializer&&) = delete;
  16405. serializer& operator=(serializer&&) = delete;
  16406. ~serializer() = default;
  16407. /*!
  16408. @brief internal implementation of the serialization function
  16409. This function is called by the public member function dump and organizes
  16410. the serialization internally. The indentation level is propagated as
  16411. additional parameter. In case of arrays and objects, the function is
  16412. called recursively.
  16413. - strings and object keys are escaped using `escape_string()`
  16414. - integer numbers are converted implicitly via `operator<<`
  16415. - floating-point numbers are converted to a string using `"%g"` format
  16416. - binary values are serialized as objects containing the subtype and the
  16417. byte array
  16418. @param[in] val value to serialize
  16419. @param[in] pretty_print whether the output shall be pretty-printed
  16420. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  16421. in the output are escaped with `\uXXXX` sequences, and the result consists
  16422. of ASCII characters only.
  16423. @param[in] indent_step the indent level
  16424. @param[in] current_indent the current indent level (only used internally)
  16425. */
  16426. void dump(const BasicJsonType& val,
  16427. const bool pretty_print,
  16428. const bool ensure_ascii,
  16429. const unsigned int indent_step,
  16430. const unsigned int current_indent = 0)
  16431. {
  16432. switch (val.m_data.m_type)
  16433. {
  16434. case value_t::object:
  16435. {
  16436. if (val.m_data.m_value.object->empty())
  16437. {
  16438. o->write_characters("{}", 2);
  16439. return;
  16440. }
  16441. if (pretty_print)
  16442. {
  16443. o->write_characters("{\n", 2);
  16444. // variable to hold indentation for recursive calls
  16445. const auto new_indent = current_indent + indent_step;
  16446. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  16447. {
  16448. indent_string.resize(indent_string.size() * 2, ' ');
  16449. }
  16450. // first n-1 elements
  16451. auto i = val.m_data.m_value.object->cbegin();
  16452. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  16453. {
  16454. o->write_characters(indent_string.c_str(), new_indent);
  16455. o->write_character('\"');
  16456. dump_escaped(i->first, ensure_ascii);
  16457. o->write_characters("\": ", 3);
  16458. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  16459. o->write_characters(",\n", 2);
  16460. }
  16461. // last element
  16462. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  16463. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  16464. o->write_characters(indent_string.c_str(), new_indent);
  16465. o->write_character('\"');
  16466. dump_escaped(i->first, ensure_ascii);
  16467. o->write_characters("\": ", 3);
  16468. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  16469. o->write_character('\n');
  16470. o->write_characters(indent_string.c_str(), current_indent);
  16471. o->write_character('}');
  16472. }
  16473. else
  16474. {
  16475. o->write_character('{');
  16476. // first n-1 elements
  16477. auto i = val.m_data.m_value.object->cbegin();
  16478. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  16479. {
  16480. o->write_character('\"');
  16481. dump_escaped(i->first, ensure_ascii);
  16482. o->write_characters("\":", 2);
  16483. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  16484. o->write_character(',');
  16485. }
  16486. // last element
  16487. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  16488. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  16489. o->write_character('\"');
  16490. dump_escaped(i->first, ensure_ascii);
  16491. o->write_characters("\":", 2);
  16492. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  16493. o->write_character('}');
  16494. }
  16495. return;
  16496. }
  16497. case value_t::array:
  16498. {
  16499. if (val.m_data.m_value.array->empty())
  16500. {
  16501. o->write_characters("[]", 2);
  16502. return;
  16503. }
  16504. if (pretty_print)
  16505. {
  16506. o->write_characters("[\n", 2);
  16507. // variable to hold indentation for recursive calls
  16508. const auto new_indent = current_indent + indent_step;
  16509. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  16510. {
  16511. indent_string.resize(indent_string.size() * 2, ' ');
  16512. }
  16513. // first n-1 elements
  16514. for (auto i = val.m_data.m_value.array->cbegin();
  16515. i != val.m_data.m_value.array->cend() - 1; ++i)
  16516. {
  16517. o->write_characters(indent_string.c_str(), new_indent);
  16518. dump(*i, true, ensure_ascii, indent_step, new_indent);
  16519. o->write_characters(",\n", 2);
  16520. }
  16521. // last element
  16522. JSON_ASSERT(!val.m_data.m_value.array->empty());
  16523. o->write_characters(indent_string.c_str(), new_indent);
  16524. dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  16525. o->write_character('\n');
  16526. o->write_characters(indent_string.c_str(), current_indent);
  16527. o->write_character(']');
  16528. }
  16529. else
  16530. {
  16531. o->write_character('[');
  16532. // first n-1 elements
  16533. for (auto i = val.m_data.m_value.array->cbegin();
  16534. i != val.m_data.m_value.array->cend() - 1; ++i)
  16535. {
  16536. dump(*i, false, ensure_ascii, indent_step, current_indent);
  16537. o->write_character(',');
  16538. }
  16539. // last element
  16540. JSON_ASSERT(!val.m_data.m_value.array->empty());
  16541. dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  16542. o->write_character(']');
  16543. }
  16544. return;
  16545. }
  16546. case value_t::string:
  16547. {
  16548. o->write_character('\"');
  16549. dump_escaped(*val.m_data.m_value.string, ensure_ascii);
  16550. o->write_character('\"');
  16551. return;
  16552. }
  16553. case value_t::binary:
  16554. {
  16555. if (pretty_print)
  16556. {
  16557. o->write_characters("{\n", 2);
  16558. // variable to hold indentation for recursive calls
  16559. const auto new_indent = current_indent + indent_step;
  16560. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  16561. {
  16562. indent_string.resize(indent_string.size() * 2, ' ');
  16563. }
  16564. o->write_characters(indent_string.c_str(), new_indent);
  16565. o->write_characters("\"bytes\": [", 10);
  16566. if (!val.m_data.m_value.binary->empty())
  16567. {
  16568. for (auto i = val.m_data.m_value.binary->cbegin();
  16569. i != val.m_data.m_value.binary->cend() - 1; ++i)
  16570. {
  16571. dump_integer(*i);
  16572. o->write_characters(", ", 2);
  16573. }
  16574. dump_integer(val.m_data.m_value.binary->back());
  16575. }
  16576. o->write_characters("],\n", 3);
  16577. o->write_characters(indent_string.c_str(), new_indent);
  16578. o->write_characters("\"subtype\": ", 11);
  16579. if (val.m_data.m_value.binary->has_subtype())
  16580. {
  16581. dump_integer(val.m_data.m_value.binary->subtype());
  16582. }
  16583. else
  16584. {
  16585. o->write_characters("null", 4);
  16586. }
  16587. o->write_character('\n');
  16588. o->write_characters(indent_string.c_str(), current_indent);
  16589. o->write_character('}');
  16590. }
  16591. else
  16592. {
  16593. o->write_characters("{\"bytes\":[", 10);
  16594. if (!val.m_data.m_value.binary->empty())
  16595. {
  16596. for (auto i = val.m_data.m_value.binary->cbegin();
  16597. i != val.m_data.m_value.binary->cend() - 1; ++i)
  16598. {
  16599. dump_integer(*i);
  16600. o->write_character(',');
  16601. }
  16602. dump_integer(val.m_data.m_value.binary->back());
  16603. }
  16604. o->write_characters("],\"subtype\":", 12);
  16605. if (val.m_data.m_value.binary->has_subtype())
  16606. {
  16607. dump_integer(val.m_data.m_value.binary->subtype());
  16608. o->write_character('}');
  16609. }
  16610. else
  16611. {
  16612. o->write_characters("null}", 5);
  16613. }
  16614. }
  16615. return;
  16616. }
  16617. case value_t::boolean:
  16618. {
  16619. if (val.m_data.m_value.boolean)
  16620. {
  16621. o->write_characters("true", 4);
  16622. }
  16623. else
  16624. {
  16625. o->write_characters("false", 5);
  16626. }
  16627. return;
  16628. }
  16629. case value_t::number_integer:
  16630. {
  16631. dump_integer(val.m_data.m_value.number_integer);
  16632. return;
  16633. }
  16634. case value_t::number_unsigned:
  16635. {
  16636. dump_integer(val.m_data.m_value.number_unsigned);
  16637. return;
  16638. }
  16639. case value_t::number_float:
  16640. {
  16641. dump_float(val.m_data.m_value.number_float);
  16642. return;
  16643. }
  16644. case value_t::discarded:
  16645. {
  16646. o->write_characters("<discarded>", 11);
  16647. return;
  16648. }
  16649. case value_t::null:
  16650. {
  16651. o->write_characters("null", 4);
  16652. return;
  16653. }
  16654. default: // LCOV_EXCL_LINE
  16655. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16656. }
  16657. }
  16658. JSON_PRIVATE_UNLESS_TESTED:
  16659. /*!
  16660. @brief dump escaped string
  16661. Escape a string by replacing certain special characters by a sequence of an
  16662. escape character (backslash) and another character and other control
  16663. characters by a sequence of "\u" followed by a four-digit hex
  16664. representation. The escaped string is written to output stream @a o.
  16665. @param[in] s the string to escape
  16666. @param[in] ensure_ascii whether to escape non-ASCII characters with
  16667. \uXXXX sequences
  16668. @complexity Linear in the length of string @a s.
  16669. */
  16670. void dump_escaped(const string_t& s, const bool ensure_ascii)
  16671. {
  16672. std::uint32_t codepoint{};
  16673. std::uint8_t state = UTF8_ACCEPT;
  16674. std::size_t bytes = 0; // number of bytes written to string_buffer
  16675. // number of bytes written at the point of the last valid byte
  16676. std::size_t bytes_after_last_accept = 0;
  16677. std::size_t undumped_chars = 0;
  16678. for (std::size_t i = 0; i < s.size(); ++i)
  16679. {
  16680. const auto byte = static_cast<std::uint8_t>(s[i]);
  16681. switch (decode(state, codepoint, byte))
  16682. {
  16683. case UTF8_ACCEPT: // decode found a new code point
  16684. {
  16685. switch (codepoint)
  16686. {
  16687. case 0x08: // backspace
  16688. {
  16689. string_buffer[bytes++] = '\\';
  16690. string_buffer[bytes++] = 'b';
  16691. break;
  16692. }
  16693. case 0x09: // horizontal tab
  16694. {
  16695. string_buffer[bytes++] = '\\';
  16696. string_buffer[bytes++] = 't';
  16697. break;
  16698. }
  16699. case 0x0A: // newline
  16700. {
  16701. string_buffer[bytes++] = '\\';
  16702. string_buffer[bytes++] = 'n';
  16703. break;
  16704. }
  16705. case 0x0C: // formfeed
  16706. {
  16707. string_buffer[bytes++] = '\\';
  16708. string_buffer[bytes++] = 'f';
  16709. break;
  16710. }
  16711. case 0x0D: // carriage return
  16712. {
  16713. string_buffer[bytes++] = '\\';
  16714. string_buffer[bytes++] = 'r';
  16715. break;
  16716. }
  16717. case 0x22: // quotation mark
  16718. {
  16719. string_buffer[bytes++] = '\\';
  16720. string_buffer[bytes++] = '\"';
  16721. break;
  16722. }
  16723. case 0x5C: // reverse solidus
  16724. {
  16725. string_buffer[bytes++] = '\\';
  16726. string_buffer[bytes++] = '\\';
  16727. break;
  16728. }
  16729. default:
  16730. {
  16731. // escape control characters (0x00..0x1F) or, if
  16732. // ensure_ascii parameter is used, non-ASCII characters
  16733. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  16734. {
  16735. if (codepoint <= 0xFFFF)
  16736. {
  16737. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16738. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  16739. static_cast<std::uint16_t>(codepoint)));
  16740. bytes += 6;
  16741. }
  16742. else
  16743. {
  16744. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16745. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16746. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16747. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16748. bytes += 12;
  16749. }
  16750. }
  16751. else
  16752. {
  16753. // copy byte to buffer (all previous bytes
  16754. // been copied have in default case above)
  16755. string_buffer[bytes++] = s[i];
  16756. }
  16757. break;
  16758. }
  16759. }
  16760. // write buffer and reset index; there must be 13 bytes
  16761. // left, as this is the maximal number of bytes to be
  16762. // written ("\uxxxx\uxxxx\0") for one code point
  16763. if (string_buffer.size() - bytes < 13)
  16764. {
  16765. o->write_characters(string_buffer.data(), bytes);
  16766. bytes = 0;
  16767. }
  16768. // remember the byte position of this accept
  16769. bytes_after_last_accept = bytes;
  16770. undumped_chars = 0;
  16771. break;
  16772. }
  16773. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16774. {
  16775. switch (error_handler)
  16776. {
  16777. case error_handler_t::strict:
  16778. {
  16779. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16780. }
  16781. case error_handler_t::ignore:
  16782. case error_handler_t::replace:
  16783. {
  16784. // in case we saw this character the first time, we
  16785. // would like to read it again, because the byte
  16786. // may be OK for itself, but just not OK for the
  16787. // previous sequence
  16788. if (undumped_chars > 0)
  16789. {
  16790. --i;
  16791. }
  16792. // reset length buffer to the last accepted index;
  16793. // thus removing/ignoring the invalid characters
  16794. bytes = bytes_after_last_accept;
  16795. if (error_handler == error_handler_t::replace)
  16796. {
  16797. // add a replacement character
  16798. if (ensure_ascii)
  16799. {
  16800. string_buffer[bytes++] = '\\';
  16801. string_buffer[bytes++] = 'u';
  16802. string_buffer[bytes++] = 'f';
  16803. string_buffer[bytes++] = 'f';
  16804. string_buffer[bytes++] = 'f';
  16805. string_buffer[bytes++] = 'd';
  16806. }
  16807. else
  16808. {
  16809. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16810. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16811. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16812. }
  16813. // write buffer and reset index; there must be 13 bytes
  16814. // left, as this is the maximal number of bytes to be
  16815. // written ("\uxxxx\uxxxx\0") for one code point
  16816. if (string_buffer.size() - bytes < 13)
  16817. {
  16818. o->write_characters(string_buffer.data(), bytes);
  16819. bytes = 0;
  16820. }
  16821. bytes_after_last_accept = bytes;
  16822. }
  16823. undumped_chars = 0;
  16824. // continue processing the string
  16825. state = UTF8_ACCEPT;
  16826. break;
  16827. }
  16828. default: // LCOV_EXCL_LINE
  16829. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16830. }
  16831. break;
  16832. }
  16833. default: // decode found yet incomplete multi-byte code point
  16834. {
  16835. if (!ensure_ascii)
  16836. {
  16837. // code point will not be escaped - copy byte to buffer
  16838. string_buffer[bytes++] = s[i];
  16839. }
  16840. ++undumped_chars;
  16841. break;
  16842. }
  16843. }
  16844. }
  16845. // we finished processing the string
  16846. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  16847. {
  16848. // write buffer
  16849. if (bytes > 0)
  16850. {
  16851. o->write_characters(string_buffer.data(), bytes);
  16852. }
  16853. }
  16854. else
  16855. {
  16856. // we finish reading, but do not accept: string was incomplete
  16857. switch (error_handler)
  16858. {
  16859. case error_handler_t::strict:
  16860. {
  16861. JSON_THROW(type_error::create(316, concat("incomplete UTF-8 string; last byte: 0x", hex_bytes(static_cast<std::uint8_t>(s.back() | 0))), nullptr));
  16862. }
  16863. case error_handler_t::ignore:
  16864. {
  16865. // write all accepted bytes
  16866. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16867. break;
  16868. }
  16869. case error_handler_t::replace:
  16870. {
  16871. // write all accepted bytes
  16872. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16873. // add a replacement character
  16874. if (ensure_ascii)
  16875. {
  16876. o->write_characters("\\ufffd", 6);
  16877. }
  16878. else
  16879. {
  16880. o->write_characters("\xEF\xBF\xBD", 3);
  16881. }
  16882. break;
  16883. }
  16884. default: // LCOV_EXCL_LINE
  16885. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16886. }
  16887. }
  16888. }
  16889. private:
  16890. /*!
  16891. @brief count digits
  16892. Count the number of decimal (base 10) digits for an input unsigned integer.
  16893. @param[in] x unsigned integer number to count its digits
  16894. @return number of decimal digits
  16895. */
  16896. unsigned int count_digits(number_unsigned_t x) noexcept
  16897. {
  16898. unsigned int n_digits = 1;
  16899. for (;;)
  16900. {
  16901. if (x < 10)
  16902. {
  16903. return n_digits;
  16904. }
  16905. if (x < 100)
  16906. {
  16907. return n_digits + 1;
  16908. }
  16909. if (x < 1000)
  16910. {
  16911. return n_digits + 2;
  16912. }
  16913. if (x < 10000)
  16914. {
  16915. return n_digits + 3;
  16916. }
  16917. x = x / 10000u;
  16918. n_digits += 4;
  16919. }
  16920. }
  16921. /*!
  16922. * @brief convert a byte to a uppercase hex representation
  16923. * @param[in] byte byte to represent
  16924. * @return representation ("00".."FF")
  16925. */
  16926. static std::string hex_bytes(std::uint8_t byte)
  16927. {
  16928. std::string result = "FF";
  16929. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  16930. result[0] = nibble_to_hex[byte / 16];
  16931. result[1] = nibble_to_hex[byte % 16];
  16932. return result;
  16933. }
  16934. // templates to avoid warnings about useless casts
  16935. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  16936. bool is_negative_number(NumberType x)
  16937. {
  16938. return x < 0;
  16939. }
  16940. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  16941. bool is_negative_number(NumberType /*unused*/)
  16942. {
  16943. return false;
  16944. }
  16945. /*!
  16946. @brief dump an integer
  16947. Dump a given integer to output stream @a o. Works internally with
  16948. @a number_buffer.
  16949. @param[in] x integer number (signed or unsigned) to dump
  16950. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  16951. */
  16952. template < typename NumberType, detail::enable_if_t <
  16953. std::is_integral<NumberType>::value ||
  16954. std::is_same<NumberType, number_unsigned_t>::value ||
  16955. std::is_same<NumberType, number_integer_t>::value ||
  16956. std::is_same<NumberType, binary_char_t>::value,
  16957. int > = 0 >
  16958. void dump_integer(NumberType x)
  16959. {
  16960. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  16961. {
  16962. {
  16963. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  16964. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  16965. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  16966. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  16967. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  16968. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  16969. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  16970. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  16971. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  16972. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  16973. }
  16974. };
  16975. // special case for "0"
  16976. if (x == 0)
  16977. {
  16978. o->write_character('0');
  16979. return;
  16980. }
  16981. // use a pointer to fill the buffer
  16982. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16983. number_unsigned_t abs_value;
  16984. unsigned int n_chars{};
  16985. if (is_negative_number(x))
  16986. {
  16987. *buffer_ptr = '-';
  16988. abs_value = remove_sign(static_cast<number_integer_t>(x));
  16989. // account one more byte for the minus sign
  16990. n_chars = 1 + count_digits(abs_value);
  16991. }
  16992. else
  16993. {
  16994. abs_value = static_cast<number_unsigned_t>(x);
  16995. n_chars = count_digits(abs_value);
  16996. }
  16997. // spare 1 byte for '\0'
  16998. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  16999. // jump to the end to generate the string from backward,
  17000. // so we later avoid reversing the result
  17001. buffer_ptr += n_chars;
  17002. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  17003. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  17004. while (abs_value >= 100)
  17005. {
  17006. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  17007. abs_value /= 100;
  17008. *(--buffer_ptr) = digits_to_99[digits_index][1];
  17009. *(--buffer_ptr) = digits_to_99[digits_index][0];
  17010. }
  17011. if (abs_value >= 10)
  17012. {
  17013. const auto digits_index = static_cast<unsigned>(abs_value);
  17014. *(--buffer_ptr) = digits_to_99[digits_index][1];
  17015. *(--buffer_ptr) = digits_to_99[digits_index][0];
  17016. }
  17017. else
  17018. {
  17019. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  17020. }
  17021. o->write_characters(number_buffer.data(), n_chars);
  17022. }
  17023. /*!
  17024. @brief dump a floating-point number
  17025. Dump a given floating-point number to output stream @a o. Works internally
  17026. with @a number_buffer.
  17027. @param[in] x floating-point number to dump
  17028. */
  17029. void dump_float(number_float_t x)
  17030. {
  17031. // NaN / inf
  17032. if (!std::isfinite(x))
  17033. {
  17034. o->write_characters("null", 4);
  17035. return;
  17036. }
  17037. // If number_float_t is an IEEE-754 single or double precision number,
  17038. // use the Grisu2 algorithm to produce short numbers which are
  17039. // guaranteed to round-trip, using strtof and strtod, resp.
  17040. //
  17041. // NB: The test below works if <long double> == <double>.
  17042. static constexpr bool is_ieee_single_or_double
  17043. = (std::numeric_limits<number_float_t>::is_iec559 && std::numeric_limits<number_float_t>::digits == 24 && std::numeric_limits<number_float_t>::max_exponent == 128) ||
  17044. (std::numeric_limits<number_float_t>::is_iec559 && std::numeric_limits<number_float_t>::digits == 53 && std::numeric_limits<number_float_t>::max_exponent == 1024);
  17045. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  17046. }
  17047. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  17048. {
  17049. auto* begin = number_buffer.data();
  17050. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  17051. o->write_characters(begin, static_cast<size_t>(end - begin));
  17052. }
  17053. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  17054. {
  17055. // get number of digits for a float -> text -> float round-trip
  17056. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  17057. // the actual conversion
  17058. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  17059. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  17060. // negative value indicates an error
  17061. JSON_ASSERT(len > 0);
  17062. // check if buffer was large enough
  17063. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  17064. // erase thousands separator
  17065. if (thousands_sep != '\0')
  17066. {
  17067. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  17068. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  17069. std::fill(end, number_buffer.end(), '\0');
  17070. JSON_ASSERT((end - number_buffer.begin()) <= len);
  17071. len = (end - number_buffer.begin());
  17072. }
  17073. // convert decimal point to '.'
  17074. if (decimal_point != '\0' && decimal_point != '.')
  17075. {
  17076. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  17077. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  17078. if (dec_pos != number_buffer.end())
  17079. {
  17080. *dec_pos = '.';
  17081. }
  17082. }
  17083. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  17084. // determine if we need to append ".0"
  17085. const bool value_is_int_like =
  17086. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  17087. [](char c)
  17088. {
  17089. return c == '.' || c == 'e';
  17090. });
  17091. if (value_is_int_like)
  17092. {
  17093. o->write_characters(".0", 2);
  17094. }
  17095. }
  17096. /*!
  17097. @brief check whether a string is UTF-8 encoded
  17098. The function checks each byte of a string whether it is UTF-8 encoded. The
  17099. result of the check is stored in the @a state parameter. The function must
  17100. be called initially with state 0 (accept). State 1 means the string must
  17101. be rejected, because the current byte is not allowed. If the string is
  17102. completely processed, but the state is non-zero, the string ended
  17103. prematurely; that is, the last byte indicated more bytes should have
  17104. followed.
  17105. @param[in,out] state the state of the decoding
  17106. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  17107. @param[in] byte next byte to decode
  17108. @return new state
  17109. @note The function has been edited: a std::array is used.
  17110. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  17111. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  17112. */
  17113. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  17114. {
  17115. static const std::array<std::uint8_t, 400> utf8d =
  17116. {
  17117. {
  17118. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1F
  17119. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3F
  17120. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5F
  17121. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7F
  17122. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9F
  17123. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // A0..BF
  17124. 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0..DF
  17125. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  17126. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  17127. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  17128. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
  17129. 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
  17130. 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
  17131. 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8
  17132. }
  17133. };
  17134. JSON_ASSERT(byte < utf8d.size());
  17135. const std::uint8_t type = utf8d[byte];
  17136. codep = (state != UTF8_ACCEPT)
  17137. ? (byte & 0x3fu) | (codep << 6u)
  17138. : (0xFFu >> type) & (byte);
  17139. const std::size_t index = 256u + (static_cast<size_t>(state) * 16u) + static_cast<size_t>(type);
  17140. JSON_ASSERT(index < utf8d.size());
  17141. state = utf8d[index];
  17142. return state;
  17143. }
  17144. /*
  17145. * Overload to make the compiler happy while it is instantiating
  17146. * dump_integer for number_unsigned_t.
  17147. * Must never be called.
  17148. */
  17149. number_unsigned_t remove_sign(number_unsigned_t x)
  17150. {
  17151. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17152. return x; // LCOV_EXCL_LINE
  17153. }
  17154. /*
  17155. * Helper function for dump_integer
  17156. *
  17157. * This function takes a negative signed integer and returns its absolute
  17158. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  17159. * not directly remove the sign of an arbitrary signed integer as the
  17160. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  17161. * #1708 for details.
  17162. */
  17163. number_unsigned_t remove_sign(number_integer_t x) noexcept
  17164. {
  17165. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  17166. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  17167. }
  17168. private:
  17169. /// the output of the serializer
  17170. output_adapter_t<char> o = nullptr;
  17171. /// a (hopefully) large enough character buffer
  17172. std::array<char, 64> number_buffer{{}};
  17173. /// the locale
  17174. const std::lconv* loc = nullptr;
  17175. /// the locale's thousand separator character
  17176. const char thousands_sep = '\0';
  17177. /// the locale's decimal point character
  17178. const char decimal_point = '\0';
  17179. /// string buffer
  17180. std::array<char, 512> string_buffer{{}};
  17181. /// the indentation character
  17182. const char indent_char;
  17183. /// the indentation string
  17184. string_t indent_string;
  17185. /// error_handler how to react on decoding errors
  17186. const error_handler_t error_handler;
  17187. };
  17188. } // namespace detail
  17189. NLOHMANN_JSON_NAMESPACE_END
  17190. // #include <nlohmann/detail/value_t.hpp>
  17191. // #include <nlohmann/json_fwd.hpp>
  17192. // #include <nlohmann/ordered_map.hpp>
  17193. // __ _____ _____ _____
  17194. // __| | __| | | | JSON for Modern C++
  17195. // | | |__ | | | | | | version 3.12.0
  17196. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  17197. //
  17198. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  17199. // SPDX-License-Identifier: MIT
  17200. #include <functional> // equal_to, less
  17201. #include <initializer_list> // initializer_list
  17202. #include <iterator> // input_iterator_tag, iterator_traits
  17203. #include <memory> // allocator
  17204. #include <stdexcept> // for out_of_range
  17205. #include <type_traits> // enable_if, is_convertible
  17206. #include <utility> // pair
  17207. #include <vector> // vector
  17208. // #include <nlohmann/detail/macro_scope.hpp>
  17209. // #include <nlohmann/detail/meta/type_traits.hpp>
  17210. NLOHMANN_JSON_NAMESPACE_BEGIN
  17211. /// ordered_map: a minimal map-like container that preserves insertion order
  17212. /// for use within nlohmann::basic_json<ordered_map>
  17213. template <class Key, class T, class IgnoredLess = std::less<Key>,
  17214. class Allocator = std::allocator<std::pair<const Key, T>>>
  17215. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  17216. {
  17217. using key_type = Key;
  17218. using mapped_type = T;
  17219. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  17220. using iterator = typename Container::iterator;
  17221. using const_iterator = typename Container::const_iterator;
  17222. using size_type = typename Container::size_type;
  17223. using value_type = typename Container::value_type;
  17224. #ifdef JSON_HAS_CPP_14
  17225. using key_compare = std::equal_to<>;
  17226. #else
  17227. using key_compare = std::equal_to<Key>;
  17228. #endif
  17229. // Explicit constructors instead of `using Container::Container`
  17230. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  17231. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  17232. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  17233. template <class It>
  17234. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  17235. : Container{first, last, alloc} {}
  17236. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  17237. : Container{init, alloc} {}
  17238. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  17239. {
  17240. for (auto it = this->begin(); it != this->end(); ++it)
  17241. {
  17242. if (m_compare(it->first, key))
  17243. {
  17244. return {it, false};
  17245. }
  17246. }
  17247. Container::emplace_back(key, std::forward<T>(t));
  17248. return {std::prev(this->end()), true};
  17249. }
  17250. template<class KeyType, detail::enable_if_t<
  17251. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17252. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  17253. {
  17254. for (auto it = this->begin(); it != this->end(); ++it)
  17255. {
  17256. if (m_compare(it->first, key))
  17257. {
  17258. return {it, false};
  17259. }
  17260. }
  17261. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  17262. return {std::prev(this->end()), true};
  17263. }
  17264. T& operator[](const key_type& key)
  17265. {
  17266. return emplace(key, T{}).first->second;
  17267. }
  17268. template<class KeyType, detail::enable_if_t<
  17269. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17270. T & operator[](KeyType && key)
  17271. {
  17272. return emplace(std::forward<KeyType>(key), T{}).first->second;
  17273. }
  17274. const T& operator[](const key_type& key) const
  17275. {
  17276. return at(key);
  17277. }
  17278. template<class KeyType, detail::enable_if_t<
  17279. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17280. const T & operator[](KeyType && key) const
  17281. {
  17282. return at(std::forward<KeyType>(key));
  17283. }
  17284. T& at(const key_type& key)
  17285. {
  17286. for (auto it = this->begin(); it != this->end(); ++it)
  17287. {
  17288. if (m_compare(it->first, key))
  17289. {
  17290. return it->second;
  17291. }
  17292. }
  17293. JSON_THROW(std::out_of_range("key not found"));
  17294. }
  17295. template<class KeyType, detail::enable_if_t<
  17296. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17297. T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  17298. {
  17299. for (auto it = this->begin(); it != this->end(); ++it)
  17300. {
  17301. if (m_compare(it->first, key))
  17302. {
  17303. return it->second;
  17304. }
  17305. }
  17306. JSON_THROW(std::out_of_range("key not found"));
  17307. }
  17308. const T& at(const key_type& key) const
  17309. {
  17310. for (auto it = this->begin(); it != this->end(); ++it)
  17311. {
  17312. if (m_compare(it->first, key))
  17313. {
  17314. return it->second;
  17315. }
  17316. }
  17317. JSON_THROW(std::out_of_range("key not found"));
  17318. }
  17319. template<class KeyType, detail::enable_if_t<
  17320. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17321. const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  17322. {
  17323. for (auto it = this->begin(); it != this->end(); ++it)
  17324. {
  17325. if (m_compare(it->first, key))
  17326. {
  17327. return it->second;
  17328. }
  17329. }
  17330. JSON_THROW(std::out_of_range("key not found"));
  17331. }
  17332. size_type erase(const key_type& key)
  17333. {
  17334. for (auto it = this->begin(); it != this->end(); ++it)
  17335. {
  17336. if (m_compare(it->first, key))
  17337. {
  17338. // Since we cannot move const Keys, re-construct them in place
  17339. for (auto next = it; ++next != this->end(); ++it)
  17340. {
  17341. it->~value_type(); // Destroy but keep allocation
  17342. new (&*it) value_type{std::move(*next)};
  17343. }
  17344. Container::pop_back();
  17345. return 1;
  17346. }
  17347. }
  17348. return 0;
  17349. }
  17350. template<class KeyType, detail::enable_if_t<
  17351. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17352. size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  17353. {
  17354. for (auto it = this->begin(); it != this->end(); ++it)
  17355. {
  17356. if (m_compare(it->first, key))
  17357. {
  17358. // Since we cannot move const Keys, re-construct them in place
  17359. for (auto next = it; ++next != this->end(); ++it)
  17360. {
  17361. it->~value_type(); // Destroy but keep allocation
  17362. new (&*it) value_type{std::move(*next)};
  17363. }
  17364. Container::pop_back();
  17365. return 1;
  17366. }
  17367. }
  17368. return 0;
  17369. }
  17370. iterator erase(iterator pos)
  17371. {
  17372. return erase(pos, std::next(pos));
  17373. }
  17374. iterator erase(iterator first, iterator last)
  17375. {
  17376. if (first == last)
  17377. {
  17378. return first;
  17379. }
  17380. const auto elements_affected = std::distance(first, last);
  17381. const auto offset = std::distance(Container::begin(), first);
  17382. // This is the start situation. We need to delete elements_affected
  17383. // elements (3 in this example: e, f, g), and need to return an
  17384. // iterator past the last deleted element (h in this example).
  17385. // Note that offset is the distance from the start of the vector
  17386. // to first. We will need this later.
  17387. // [ a, b, c, d, e, f, g, h, i, j ]
  17388. // ^ ^
  17389. // first last
  17390. // Since we cannot move const Keys, we re-construct them in place.
  17391. // We start at first and re-construct (viz. copy) the elements from
  17392. // the back of the vector. Example for first iteration:
  17393. // ,--------.
  17394. // v | destroy e and re-construct with h
  17395. // [ a, b, c, d, e, f, g, h, i, j ]
  17396. // ^ ^
  17397. // it it + elements_affected
  17398. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  17399. {
  17400. it->~value_type(); // destroy but keep allocation
  17401. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  17402. }
  17403. // [ a, b, c, d, h, i, j, h, i, j ]
  17404. // ^ ^
  17405. // first last
  17406. // remove the unneeded elements at the end of the vector
  17407. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  17408. // [ a, b, c, d, h, i, j ]
  17409. // ^ ^
  17410. // first last
  17411. // first is now pointing past the last deleted element, but we cannot
  17412. // use this iterator, because it may have been invalidated by the
  17413. // resize call. Instead, we can return begin() + offset.
  17414. return Container::begin() + offset;
  17415. }
  17416. size_type count(const key_type& key) const
  17417. {
  17418. for (auto it = this->begin(); it != this->end(); ++it)
  17419. {
  17420. if (m_compare(it->first, key))
  17421. {
  17422. return 1;
  17423. }
  17424. }
  17425. return 0;
  17426. }
  17427. template<class KeyType, detail::enable_if_t<
  17428. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17429. size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  17430. {
  17431. for (auto it = this->begin(); it != this->end(); ++it)
  17432. {
  17433. if (m_compare(it->first, key))
  17434. {
  17435. return 1;
  17436. }
  17437. }
  17438. return 0;
  17439. }
  17440. iterator find(const key_type& key)
  17441. {
  17442. for (auto it = this->begin(); it != this->end(); ++it)
  17443. {
  17444. if (m_compare(it->first, key))
  17445. {
  17446. return it;
  17447. }
  17448. }
  17449. return Container::end();
  17450. }
  17451. template<class KeyType, detail::enable_if_t<
  17452. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17453. iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  17454. {
  17455. for (auto it = this->begin(); it != this->end(); ++it)
  17456. {
  17457. if (m_compare(it->first, key))
  17458. {
  17459. return it;
  17460. }
  17461. }
  17462. return Container::end();
  17463. }
  17464. const_iterator find(const key_type& key) const
  17465. {
  17466. for (auto it = this->begin(); it != this->end(); ++it)
  17467. {
  17468. if (m_compare(it->first, key))
  17469. {
  17470. return it;
  17471. }
  17472. }
  17473. return Container::end();
  17474. }
  17475. std::pair<iterator, bool> insert( value_type&& value )
  17476. {
  17477. return emplace(value.first, std::move(value.second));
  17478. }
  17479. std::pair<iterator, bool> insert( const value_type& value )
  17480. {
  17481. for (auto it = this->begin(); it != this->end(); ++it)
  17482. {
  17483. if (m_compare(it->first, value.first))
  17484. {
  17485. return {it, false};
  17486. }
  17487. }
  17488. Container::push_back(value);
  17489. return {--this->end(), true};
  17490. }
  17491. template<typename InputIt>
  17492. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  17493. std::input_iterator_tag>::value>::type;
  17494. template<typename InputIt, typename = require_input_iter<InputIt>>
  17495. void insert(InputIt first, InputIt last)
  17496. {
  17497. for (auto it = first; it != last; ++it)
  17498. {
  17499. insert(*it);
  17500. }
  17501. }
  17502. private:
  17503. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  17504. };
  17505. NLOHMANN_JSON_NAMESPACE_END
  17506. #if defined(JSON_HAS_CPP_17)
  17507. #if JSON_HAS_STATIC_RTTI
  17508. #include <any>
  17509. #endif
  17510. #include <string_view>
  17511. #endif
  17512. /*!
  17513. @brief namespace for Niels Lohmann
  17514. @see https://github.com/nlohmann
  17515. @since version 1.0.0
  17516. */
  17517. NLOHMANN_JSON_NAMESPACE_BEGIN
  17518. /*!
  17519. @brief a class to store JSON values
  17520. @internal
  17521. @invariant The member variables @a m_value and @a m_type have the following
  17522. relationship:
  17523. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  17524. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  17525. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  17526. The invariants are checked by member function assert_invariant().
  17527. @note ObjectType trick from https://stackoverflow.com/a/9860911
  17528. @endinternal
  17529. @since version 1.0.0
  17530. @nosubgrouping
  17531. */
  17532. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  17533. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  17534. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  17535. {
  17536. private:
  17537. template<detail::value_t> friend struct detail::external_constructor;
  17538. template<typename>
  17539. friend class ::nlohmann::json_pointer;
  17540. // can be restored when json_pointer backwards compatibility is removed
  17541. // friend ::nlohmann::json_pointer<StringType>;
  17542. template<typename BasicJsonType, typename InputType>
  17543. friend class ::nlohmann::detail::parser;
  17544. friend ::nlohmann::detail::serializer<basic_json>;
  17545. template<typename BasicJsonType>
  17546. friend class ::nlohmann::detail::iter_impl;
  17547. template<typename BasicJsonType, typename CharType>
  17548. friend class ::nlohmann::detail::binary_writer;
  17549. template<typename BasicJsonType, typename InputType, typename SAX>
  17550. friend class ::nlohmann::detail::binary_reader;
  17551. template<typename BasicJsonType, typename InputAdapterType>
  17552. friend class ::nlohmann::detail::json_sax_dom_parser;
  17553. template<typename BasicJsonType, typename InputAdapterType>
  17554. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  17555. friend class ::nlohmann::detail::exception;
  17556. /// workaround type for MSVC
  17557. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  17558. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  17559. JSON_PRIVATE_UNLESS_TESTED:
  17560. // convenience aliases for types residing in namespace detail;
  17561. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  17562. template<typename InputAdapterType>
  17563. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  17564. InputAdapterType adapter,
  17565. detail::parser_callback_t<basic_json>cb = nullptr,
  17566. const bool allow_exceptions = true,
  17567. const bool ignore_comments = false
  17568. )
  17569. {
  17570. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  17571. std::move(cb), allow_exceptions, ignore_comments);
  17572. }
  17573. private:
  17574. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  17575. template<typename BasicJsonType>
  17576. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  17577. template<typename BasicJsonType>
  17578. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  17579. template<typename Iterator>
  17580. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  17581. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  17582. template<typename CharType>
  17583. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  17584. template<typename InputType>
  17585. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  17586. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  17587. JSON_PRIVATE_UNLESS_TESTED:
  17588. using serializer = ::nlohmann::detail::serializer<basic_json>;
  17589. public:
  17590. using value_t = detail::value_t;
  17591. /// JSON Pointer, see @ref nlohmann::json_pointer
  17592. using json_pointer = ::nlohmann::json_pointer<StringType>;
  17593. template<typename T, typename SFINAE>
  17594. using json_serializer = JSONSerializer<T, SFINAE>;
  17595. /// how to treat decoding errors
  17596. using error_handler_t = detail::error_handler_t;
  17597. /// how to treat CBOR tags
  17598. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  17599. /// how to encode BJData
  17600. using bjdata_version_t = detail::bjdata_version_t;
  17601. /// helper type for initializer lists of basic_json values
  17602. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  17603. using input_format_t = detail::input_format_t;
  17604. /// SAX interface type, see @ref nlohmann::json_sax
  17605. using json_sax_t = json_sax<basic_json>;
  17606. ////////////////
  17607. // exceptions //
  17608. ////////////////
  17609. /// @name exceptions
  17610. /// Classes to implement user-defined exceptions.
  17611. /// @{
  17612. using exception = detail::exception;
  17613. using parse_error = detail::parse_error;
  17614. using invalid_iterator = detail::invalid_iterator;
  17615. using type_error = detail::type_error;
  17616. using out_of_range = detail::out_of_range;
  17617. using other_error = detail::other_error;
  17618. /// @}
  17619. /////////////////////
  17620. // container types //
  17621. /////////////////////
  17622. /// @name container types
  17623. /// The canonic container types to use @ref basic_json like any other STL
  17624. /// container.
  17625. /// @{
  17626. /// the type of elements in a basic_json container
  17627. using value_type = basic_json;
  17628. /// the type of an element reference
  17629. using reference = value_type&;
  17630. /// the type of an element const reference
  17631. using const_reference = const value_type&;
  17632. /// a type to represent differences between iterators
  17633. using difference_type = std::ptrdiff_t;
  17634. /// a type to represent container sizes
  17635. using size_type = std::size_t;
  17636. /// the allocator type
  17637. using allocator_type = AllocatorType<basic_json>;
  17638. /// the type of an element pointer
  17639. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  17640. /// the type of an element const pointer
  17641. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  17642. /// an iterator for a basic_json container
  17643. using iterator = iter_impl<basic_json>;
  17644. /// a const iterator for a basic_json container
  17645. using const_iterator = iter_impl<const basic_json>;
  17646. /// a reverse iterator for a basic_json container
  17647. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  17648. /// a const reverse iterator for a basic_json container
  17649. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  17650. /// @}
  17651. /// @brief returns the allocator associated with the container
  17652. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  17653. static allocator_type get_allocator()
  17654. {
  17655. return allocator_type();
  17656. }
  17657. /// @brief returns version information on the library
  17658. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  17659. JSON_HEDLEY_WARN_UNUSED_RESULT
  17660. static basic_json meta()
  17661. {
  17662. basic_json result;
  17663. result["copyright"] = "(C) 2013-2025 Niels Lohmann";
  17664. result["name"] = "JSON for Modern C++";
  17665. result["url"] = "https://github.com/nlohmann/json";
  17666. result["version"]["string"] =
  17667. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  17668. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  17669. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  17670. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  17671. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  17672. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  17673. #ifdef _WIN32
  17674. result["platform"] = "win32";
  17675. #elif defined __linux__
  17676. result["platform"] = "linux";
  17677. #elif defined __APPLE__
  17678. result["platform"] = "apple";
  17679. #elif defined __unix__
  17680. result["platform"] = "unix";
  17681. #else
  17682. result["platform"] = "unknown";
  17683. #endif
  17684. #if defined(__ICC) || defined(__INTEL_COMPILER)
  17685. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  17686. #elif defined(__clang__)
  17687. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  17688. #elif defined(__GNUC__) || defined(__GNUG__)
  17689. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  17690. std::to_string(__GNUC__), '.',
  17691. std::to_string(__GNUC_MINOR__), '.',
  17692. std::to_string(__GNUC_PATCHLEVEL__))
  17693. }
  17694. };
  17695. #elif defined(__HP_cc) || defined(__HP_aCC)
  17696. result["compiler"] = "hp"
  17697. #elif defined(__IBMCPP__)
  17698. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  17699. #elif defined(_MSC_VER)
  17700. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  17701. #elif defined(__PGI)
  17702. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  17703. #elif defined(__SUNPRO_CC)
  17704. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  17705. #else
  17706. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  17707. #endif
  17708. #if defined(_MSVC_LANG)
  17709. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  17710. #elif defined(__cplusplus)
  17711. result["compiler"]["c++"] = std::to_string(__cplusplus);
  17712. #else
  17713. result["compiler"]["c++"] = "unknown";
  17714. #endif
  17715. return result;
  17716. }
  17717. ///////////////////////////
  17718. // JSON value data types //
  17719. ///////////////////////////
  17720. /// @name JSON value data types
  17721. /// The data types to store a JSON value. These types are derived from
  17722. /// the template arguments passed to class @ref basic_json.
  17723. /// @{
  17724. /// @brief default object key comparator type
  17725. /// The actual object key comparator type (@ref object_comparator_t) may be
  17726. /// different.
  17727. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  17728. #if defined(JSON_HAS_CPP_14)
  17729. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  17730. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  17731. using default_object_comparator_t = std::less<>;
  17732. #else
  17733. using default_object_comparator_t = std::less<StringType>;
  17734. #endif
  17735. /// @brief a type for an object
  17736. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  17737. using object_t = ObjectType<StringType,
  17738. basic_json,
  17739. default_object_comparator_t,
  17740. AllocatorType<std::pair<const StringType,
  17741. basic_json>>>;
  17742. /// @brief a type for an array
  17743. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  17744. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  17745. /// @brief a type for a string
  17746. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  17747. using string_t = StringType;
  17748. /// @brief a type for a boolean
  17749. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17750. using boolean_t = BooleanType;
  17751. /// @brief a type for a number (integer)
  17752. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17753. using number_integer_t = NumberIntegerType;
  17754. /// @brief a type for a number (unsigned)
  17755. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17756. using number_unsigned_t = NumberUnsignedType;
  17757. /// @brief a type for a number (floating-point)
  17758. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17759. using number_float_t = NumberFloatType;
  17760. /// @brief a type for a packed binary type
  17761. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17762. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17763. /// @brief object key comparator type
  17764. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17765. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17766. /// @}
  17767. private:
  17768. /// helper for exception-safe object creation
  17769. template<typename T, typename... Args>
  17770. JSON_HEDLEY_RETURNS_NON_NULL
  17771. static T* create(Args&& ... args)
  17772. {
  17773. AllocatorType<T> alloc;
  17774. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17775. auto deleter = [&](T * obj)
  17776. {
  17777. AllocatorTraits::deallocate(alloc, obj, 1);
  17778. };
  17779. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17780. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17781. JSON_ASSERT(obj != nullptr);
  17782. return obj.release();
  17783. }
  17784. ////////////////////////
  17785. // JSON value storage //
  17786. ////////////////////////
  17787. JSON_PRIVATE_UNLESS_TESTED:
  17788. /*!
  17789. @brief a JSON value
  17790. The actual storage for a JSON value of the @ref basic_json class. This
  17791. union combines the different storage types for the JSON value types
  17792. defined in @ref value_t.
  17793. JSON type | value_t type | used type
  17794. --------- | --------------- | ------------------------
  17795. object | object | pointer to @ref object_t
  17796. array | array | pointer to @ref array_t
  17797. string | string | pointer to @ref string_t
  17798. boolean | boolean | @ref boolean_t
  17799. number | number_integer | @ref number_integer_t
  17800. number | number_unsigned | @ref number_unsigned_t
  17801. number | number_float | @ref number_float_t
  17802. binary | binary | pointer to @ref binary_t
  17803. null | null | *no value is stored*
  17804. @note Variable-length types (objects, arrays, and strings) are stored as
  17805. pointers. The size of the union should not exceed 64 bits if the default
  17806. value types are used.
  17807. @since version 1.0.0
  17808. */
  17809. union json_value
  17810. {
  17811. /// object (stored with pointer to save storage)
  17812. object_t* object;
  17813. /// array (stored with pointer to save storage)
  17814. array_t* array;
  17815. /// string (stored with pointer to save storage)
  17816. string_t* string;
  17817. /// binary (stored with pointer to save storage)
  17818. binary_t* binary;
  17819. /// boolean
  17820. boolean_t boolean;
  17821. /// number (integer)
  17822. number_integer_t number_integer;
  17823. /// number (unsigned integer)
  17824. number_unsigned_t number_unsigned;
  17825. /// number (floating-point)
  17826. number_float_t number_float;
  17827. /// default constructor (for null values)
  17828. json_value() = default;
  17829. /// constructor for booleans
  17830. json_value(boolean_t v) noexcept : boolean(v) {}
  17831. /// constructor for numbers (integer)
  17832. json_value(number_integer_t v) noexcept : number_integer(v) {}
  17833. /// constructor for numbers (unsigned)
  17834. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  17835. /// constructor for numbers (floating-point)
  17836. json_value(number_float_t v) noexcept : number_float(v) {}
  17837. /// constructor for empty values of a given type
  17838. json_value(value_t t)
  17839. {
  17840. switch (t)
  17841. {
  17842. case value_t::object:
  17843. {
  17844. object = create<object_t>();
  17845. break;
  17846. }
  17847. case value_t::array:
  17848. {
  17849. array = create<array_t>();
  17850. break;
  17851. }
  17852. case value_t::string:
  17853. {
  17854. string = create<string_t>("");
  17855. break;
  17856. }
  17857. case value_t::binary:
  17858. {
  17859. binary = create<binary_t>();
  17860. break;
  17861. }
  17862. case value_t::boolean:
  17863. {
  17864. boolean = static_cast<boolean_t>(false);
  17865. break;
  17866. }
  17867. case value_t::number_integer:
  17868. {
  17869. number_integer = static_cast<number_integer_t>(0);
  17870. break;
  17871. }
  17872. case value_t::number_unsigned:
  17873. {
  17874. number_unsigned = static_cast<number_unsigned_t>(0);
  17875. break;
  17876. }
  17877. case value_t::number_float:
  17878. {
  17879. number_float = static_cast<number_float_t>(0.0);
  17880. break;
  17881. }
  17882. case value_t::null:
  17883. {
  17884. object = nullptr; // silence warning, see #821
  17885. break;
  17886. }
  17887. case value_t::discarded:
  17888. default:
  17889. {
  17890. object = nullptr; // silence warning, see #821
  17891. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  17892. {
  17893. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.12.0", nullptr)); // LCOV_EXCL_LINE
  17894. }
  17895. break;
  17896. }
  17897. }
  17898. }
  17899. /// constructor for strings
  17900. json_value(const string_t& value) : string(create<string_t>(value)) {}
  17901. /// constructor for rvalue strings
  17902. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  17903. /// constructor for objects
  17904. json_value(const object_t& value) : object(create<object_t>(value)) {}
  17905. /// constructor for rvalue objects
  17906. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  17907. /// constructor for arrays
  17908. json_value(const array_t& value) : array(create<array_t>(value)) {}
  17909. /// constructor for rvalue arrays
  17910. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  17911. /// constructor for binary arrays
  17912. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  17913. /// constructor for rvalue binary arrays
  17914. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  17915. /// constructor for binary arrays (internal type)
  17916. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  17917. /// constructor for rvalue binary arrays (internal type)
  17918. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  17919. void destroy(value_t t)
  17920. {
  17921. if (
  17922. (t == value_t::object && object == nullptr) ||
  17923. (t == value_t::array && array == nullptr) ||
  17924. (t == value_t::string && string == nullptr) ||
  17925. (t == value_t::binary && binary == nullptr)
  17926. )
  17927. {
  17928. //not initialized (e.g. due to exception in the ctor)
  17929. return;
  17930. }
  17931. if (t == value_t::array || t == value_t::object)
  17932. {
  17933. // flatten the current json_value to a heap-allocated stack
  17934. std::vector<basic_json> stack;
  17935. // move the top-level items to stack
  17936. if (t == value_t::array)
  17937. {
  17938. stack.reserve(array->size());
  17939. std::move(array->begin(), array->end(), std::back_inserter(stack));
  17940. }
  17941. else
  17942. {
  17943. stack.reserve(object->size());
  17944. for (auto&& it : *object)
  17945. {
  17946. stack.push_back(std::move(it.second));
  17947. }
  17948. }
  17949. while (!stack.empty())
  17950. {
  17951. // move the last item to local variable to be processed
  17952. basic_json current_item(std::move(stack.back()));
  17953. stack.pop_back();
  17954. // if current_item is array/object, move
  17955. // its children to the stack to be processed later
  17956. if (current_item.is_array())
  17957. {
  17958. std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
  17959. current_item.m_data.m_value.array->clear();
  17960. }
  17961. else if (current_item.is_object())
  17962. {
  17963. for (auto&& it : *current_item.m_data.m_value.object)
  17964. {
  17965. stack.push_back(std::move(it.second));
  17966. }
  17967. current_item.m_data.m_value.object->clear();
  17968. }
  17969. // it's now safe that current_item get destructed
  17970. // since it doesn't have any children
  17971. }
  17972. }
  17973. switch (t)
  17974. {
  17975. case value_t::object:
  17976. {
  17977. AllocatorType<object_t> alloc;
  17978. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  17979. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  17980. break;
  17981. }
  17982. case value_t::array:
  17983. {
  17984. AllocatorType<array_t> alloc;
  17985. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  17986. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  17987. break;
  17988. }
  17989. case value_t::string:
  17990. {
  17991. AllocatorType<string_t> alloc;
  17992. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  17993. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  17994. break;
  17995. }
  17996. case value_t::binary:
  17997. {
  17998. AllocatorType<binary_t> alloc;
  17999. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  18000. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  18001. break;
  18002. }
  18003. case value_t::null:
  18004. case value_t::boolean:
  18005. case value_t::number_integer:
  18006. case value_t::number_unsigned:
  18007. case value_t::number_float:
  18008. case value_t::discarded:
  18009. default:
  18010. {
  18011. break;
  18012. }
  18013. }
  18014. }
  18015. };
  18016. private:
  18017. /*!
  18018. @brief checks the class invariants
  18019. This function asserts the class invariants. It needs to be called at the
  18020. end of every constructor to make sure that created objects respect the
  18021. invariant. Furthermore, it has to be called each time the type of a JSON
  18022. value is changed, because the invariant expresses a relationship between
  18023. @a m_type and @a m_value.
  18024. Furthermore, the parent relation is checked for arrays and objects: If
  18025. @a check_parents true and the value is an array or object, then the
  18026. container's elements must have the current value as parent.
  18027. @param[in] check_parents whether the parent relation should be checked.
  18028. The value is true by default and should only be set to false
  18029. during destruction of objects when the invariant does not
  18030. need to hold.
  18031. */
  18032. void assert_invariant(bool check_parents = true) const noexcept
  18033. {
  18034. JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
  18035. JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
  18036. JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
  18037. JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
  18038. #if JSON_DIAGNOSTICS
  18039. JSON_TRY
  18040. {
  18041. // cppcheck-suppress assertWithSideEffect
  18042. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  18043. {
  18044. return j.m_parent == this;
  18045. }));
  18046. }
  18047. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  18048. #endif
  18049. static_cast<void>(check_parents);
  18050. }
  18051. void set_parents()
  18052. {
  18053. #if JSON_DIAGNOSTICS
  18054. switch (m_data.m_type)
  18055. {
  18056. case value_t::array:
  18057. {
  18058. for (auto& element : *m_data.m_value.array)
  18059. {
  18060. element.m_parent = this;
  18061. }
  18062. break;
  18063. }
  18064. case value_t::object:
  18065. {
  18066. for (auto& element : *m_data.m_value.object)
  18067. {
  18068. element.second.m_parent = this;
  18069. }
  18070. break;
  18071. }
  18072. case value_t::null:
  18073. case value_t::string:
  18074. case value_t::boolean:
  18075. case value_t::number_integer:
  18076. case value_t::number_unsigned:
  18077. case value_t::number_float:
  18078. case value_t::binary:
  18079. case value_t::discarded:
  18080. default:
  18081. break;
  18082. }
  18083. #endif
  18084. }
  18085. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  18086. {
  18087. #if JSON_DIAGNOSTICS
  18088. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  18089. {
  18090. (it + i)->m_parent = this;
  18091. }
  18092. #else
  18093. static_cast<void>(count_set_parents);
  18094. #endif
  18095. return it;
  18096. }
  18097. reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
  18098. {
  18099. #if JSON_DIAGNOSTICS
  18100. if (old_capacity != detail::unknown_size())
  18101. {
  18102. // see https://github.com/nlohmann/json/issues/2838
  18103. JSON_ASSERT(type() == value_t::array);
  18104. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  18105. {
  18106. // capacity has changed: update all parents
  18107. set_parents();
  18108. return j;
  18109. }
  18110. }
  18111. // ordered_json uses a vector internally, so pointers could have
  18112. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  18113. #ifdef JSON_HEDLEY_MSVC_VERSION
  18114. #pragma warning(push )
  18115. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  18116. #endif
  18117. if (detail::is_ordered_map<object_t>::value)
  18118. {
  18119. set_parents();
  18120. return j;
  18121. }
  18122. #ifdef JSON_HEDLEY_MSVC_VERSION
  18123. #pragma warning( pop )
  18124. #endif
  18125. j.m_parent = this;
  18126. #else
  18127. static_cast<void>(j);
  18128. static_cast<void>(old_capacity);
  18129. #endif
  18130. return j;
  18131. }
  18132. public:
  18133. //////////////////////////
  18134. // JSON parser callback //
  18135. //////////////////////////
  18136. /// @brief parser event types
  18137. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  18138. using parse_event_t = detail::parse_event_t;
  18139. /// @brief per-element parser callback type
  18140. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  18141. using parser_callback_t = detail::parser_callback_t<basic_json>;
  18142. //////////////////
  18143. // constructors //
  18144. //////////////////
  18145. /// @name constructors and destructors
  18146. /// Constructors of class @ref basic_json, copy/move constructor, copy
  18147. /// assignment, static functions creating objects, and the destructor.
  18148. /// @{
  18149. /// @brief create an empty value with a given type
  18150. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18151. basic_json(const value_t v)
  18152. : m_data(v)
  18153. {
  18154. assert_invariant();
  18155. }
  18156. /// @brief create a null object
  18157. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18158. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  18159. : basic_json(value_t::null)
  18160. {
  18161. assert_invariant();
  18162. }
  18163. /// @brief create a JSON value from compatible types
  18164. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18165. template < typename CompatibleType,
  18166. typename U = detail::uncvref_t<CompatibleType>,
  18167. detail::enable_if_t <
  18168. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  18169. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  18170. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  18171. std::forward<CompatibleType>(val))))
  18172. {
  18173. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  18174. set_parents();
  18175. assert_invariant();
  18176. }
  18177. /// @brief create a JSON value from an existing one
  18178. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18179. template < typename BasicJsonType,
  18180. detail::enable_if_t <
  18181. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  18182. basic_json(const BasicJsonType& val)
  18183. #if JSON_DIAGNOSTIC_POSITIONS
  18184. : start_position(val.start_pos()),
  18185. end_position(val.end_pos())
  18186. #endif
  18187. {
  18188. using other_boolean_t = typename BasicJsonType::boolean_t;
  18189. using other_number_float_t = typename BasicJsonType::number_float_t;
  18190. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  18191. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  18192. using other_string_t = typename BasicJsonType::string_t;
  18193. using other_object_t = typename BasicJsonType::object_t;
  18194. using other_array_t = typename BasicJsonType::array_t;
  18195. using other_binary_t = typename BasicJsonType::binary_t;
  18196. switch (val.type())
  18197. {
  18198. case value_t::boolean:
  18199. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  18200. break;
  18201. case value_t::number_float:
  18202. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  18203. break;
  18204. case value_t::number_integer:
  18205. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  18206. break;
  18207. case value_t::number_unsigned:
  18208. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  18209. break;
  18210. case value_t::string:
  18211. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  18212. break;
  18213. case value_t::object:
  18214. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  18215. break;
  18216. case value_t::array:
  18217. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  18218. break;
  18219. case value_t::binary:
  18220. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  18221. break;
  18222. case value_t::null:
  18223. *this = nullptr;
  18224. break;
  18225. case value_t::discarded:
  18226. m_data.m_type = value_t::discarded;
  18227. break;
  18228. default: // LCOV_EXCL_LINE
  18229. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  18230. }
  18231. JSON_ASSERT(m_data.m_type == val.type());
  18232. set_parents();
  18233. assert_invariant();
  18234. }
  18235. /// @brief create a container (array or object) from an initializer list
  18236. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18237. basic_json(initializer_list_t init,
  18238. bool type_deduction = true,
  18239. value_t manual_type = value_t::array)
  18240. {
  18241. // check if each element is an array with two elements whose first
  18242. // element is a string
  18243. bool is_an_object = std::all_of(init.begin(), init.end(),
  18244. [](const detail::json_ref<basic_json>& element_ref)
  18245. {
  18246. // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
  18247. // (many string types can be constructed from 0 via its null-pointer guise, so we get a
  18248. // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
  18249. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
  18250. });
  18251. // adjust type if type deduction is not wanted
  18252. if (!type_deduction)
  18253. {
  18254. // if array is wanted, do not create an object though possible
  18255. if (manual_type == value_t::array)
  18256. {
  18257. is_an_object = false;
  18258. }
  18259. // if object is wanted but impossible, throw an exception
  18260. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  18261. {
  18262. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  18263. }
  18264. }
  18265. if (is_an_object)
  18266. {
  18267. // the initializer list is a list of pairs -> create object
  18268. m_data.m_type = value_t::object;
  18269. m_data.m_value = value_t::object;
  18270. for (auto& element_ref : init)
  18271. {
  18272. auto element = element_ref.moved_or_copied();
  18273. m_data.m_value.object->emplace(
  18274. std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
  18275. std::move((*element.m_data.m_value.array)[1]));
  18276. }
  18277. }
  18278. else
  18279. {
  18280. // the initializer list describes an array -> create array
  18281. m_data.m_type = value_t::array;
  18282. m_data.m_value.array = create<array_t>(init.begin(), init.end());
  18283. }
  18284. set_parents();
  18285. assert_invariant();
  18286. }
  18287. /// @brief explicitly create a binary array (without subtype)
  18288. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18289. JSON_HEDLEY_WARN_UNUSED_RESULT
  18290. static basic_json binary(const typename binary_t::container_type& init)
  18291. {
  18292. auto res = basic_json();
  18293. res.m_data.m_type = value_t::binary;
  18294. res.m_data.m_value = init;
  18295. return res;
  18296. }
  18297. /// @brief explicitly create a binary array (with subtype)
  18298. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18299. JSON_HEDLEY_WARN_UNUSED_RESULT
  18300. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  18301. {
  18302. auto res = basic_json();
  18303. res.m_data.m_type = value_t::binary;
  18304. res.m_data.m_value = binary_t(init, subtype);
  18305. return res;
  18306. }
  18307. /// @brief explicitly create a binary array
  18308. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18309. JSON_HEDLEY_WARN_UNUSED_RESULT
  18310. static basic_json binary(typename binary_t::container_type&& init)
  18311. {
  18312. auto res = basic_json();
  18313. res.m_data.m_type = value_t::binary;
  18314. res.m_data.m_value = std::move(init);
  18315. return res;
  18316. }
  18317. /// @brief explicitly create a binary array (with subtype)
  18318. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18319. JSON_HEDLEY_WARN_UNUSED_RESULT
  18320. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  18321. {
  18322. auto res = basic_json();
  18323. res.m_data.m_type = value_t::binary;
  18324. res.m_data.m_value = binary_t(std::move(init), subtype);
  18325. return res;
  18326. }
  18327. /// @brief explicitly create an array from an initializer list
  18328. /// @sa https://json.nlohmann.me/api/basic_json/array/
  18329. JSON_HEDLEY_WARN_UNUSED_RESULT
  18330. static basic_json array(initializer_list_t init = {})
  18331. {
  18332. return basic_json(init, false, value_t::array);
  18333. }
  18334. /// @brief explicitly create an object from an initializer list
  18335. /// @sa https://json.nlohmann.me/api/basic_json/object/
  18336. JSON_HEDLEY_WARN_UNUSED_RESULT
  18337. static basic_json object(initializer_list_t init = {})
  18338. {
  18339. return basic_json(init, false, value_t::object);
  18340. }
  18341. /// @brief construct an array with count copies of given value
  18342. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18343. basic_json(size_type cnt, const basic_json& val):
  18344. m_data{cnt, val}
  18345. {
  18346. set_parents();
  18347. assert_invariant();
  18348. }
  18349. /// @brief construct a JSON container given an iterator range
  18350. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18351. template < class InputIT, typename std::enable_if <
  18352. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  18353. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  18354. basic_json(InputIT first, InputIT last) // NOLINT(performance-unnecessary-value-param)
  18355. {
  18356. JSON_ASSERT(first.m_object != nullptr);
  18357. JSON_ASSERT(last.m_object != nullptr);
  18358. // make sure iterator fits the current value
  18359. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  18360. {
  18361. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  18362. }
  18363. // copy type from first iterator
  18364. m_data.m_type = first.m_object->m_data.m_type;
  18365. // check if iterator range is complete for primitive values
  18366. switch (m_data.m_type)
  18367. {
  18368. case value_t::boolean:
  18369. case value_t::number_float:
  18370. case value_t::number_integer:
  18371. case value_t::number_unsigned:
  18372. case value_t::string:
  18373. {
  18374. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  18375. || !last.m_it.primitive_iterator.is_end()))
  18376. {
  18377. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  18378. }
  18379. break;
  18380. }
  18381. case value_t::null:
  18382. case value_t::object:
  18383. case value_t::array:
  18384. case value_t::binary:
  18385. case value_t::discarded:
  18386. default:
  18387. break;
  18388. }
  18389. switch (m_data.m_type)
  18390. {
  18391. case value_t::number_integer:
  18392. {
  18393. m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
  18394. break;
  18395. }
  18396. case value_t::number_unsigned:
  18397. {
  18398. m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
  18399. break;
  18400. }
  18401. case value_t::number_float:
  18402. {
  18403. m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
  18404. break;
  18405. }
  18406. case value_t::boolean:
  18407. {
  18408. m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
  18409. break;
  18410. }
  18411. case value_t::string:
  18412. {
  18413. m_data.m_value = *first.m_object->m_data.m_value.string;
  18414. break;
  18415. }
  18416. case value_t::object:
  18417. {
  18418. m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
  18419. last.m_it.object_iterator);
  18420. break;
  18421. }
  18422. case value_t::array:
  18423. {
  18424. m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
  18425. last.m_it.array_iterator);
  18426. break;
  18427. }
  18428. case value_t::binary:
  18429. {
  18430. m_data.m_value = *first.m_object->m_data.m_value.binary;
  18431. break;
  18432. }
  18433. case value_t::null:
  18434. case value_t::discarded:
  18435. default:
  18436. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  18437. }
  18438. set_parents();
  18439. assert_invariant();
  18440. }
  18441. ///////////////////////////////////////
  18442. // other constructors and destructor //
  18443. ///////////////////////////////////////
  18444. template<typename JsonRef,
  18445. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  18446. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  18447. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  18448. /// @brief copy constructor
  18449. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18450. basic_json(const basic_json& other)
  18451. : json_base_class_t(other)
  18452. #if JSON_DIAGNOSTIC_POSITIONS
  18453. , start_position(other.start_position)
  18454. , end_position(other.end_position)
  18455. #endif
  18456. {
  18457. m_data.m_type = other.m_data.m_type;
  18458. // check of passed value is valid
  18459. other.assert_invariant();
  18460. switch (m_data.m_type)
  18461. {
  18462. case value_t::object:
  18463. {
  18464. m_data.m_value = *other.m_data.m_value.object;
  18465. break;
  18466. }
  18467. case value_t::array:
  18468. {
  18469. m_data.m_value = *other.m_data.m_value.array;
  18470. break;
  18471. }
  18472. case value_t::string:
  18473. {
  18474. m_data.m_value = *other.m_data.m_value.string;
  18475. break;
  18476. }
  18477. case value_t::boolean:
  18478. {
  18479. m_data.m_value = other.m_data.m_value.boolean;
  18480. break;
  18481. }
  18482. case value_t::number_integer:
  18483. {
  18484. m_data.m_value = other.m_data.m_value.number_integer;
  18485. break;
  18486. }
  18487. case value_t::number_unsigned:
  18488. {
  18489. m_data.m_value = other.m_data.m_value.number_unsigned;
  18490. break;
  18491. }
  18492. case value_t::number_float:
  18493. {
  18494. m_data.m_value = other.m_data.m_value.number_float;
  18495. break;
  18496. }
  18497. case value_t::binary:
  18498. {
  18499. m_data.m_value = *other.m_data.m_value.binary;
  18500. break;
  18501. }
  18502. case value_t::null:
  18503. case value_t::discarded:
  18504. default:
  18505. break;
  18506. }
  18507. set_parents();
  18508. assert_invariant();
  18509. }
  18510. /// @brief move constructor
  18511. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18512. basic_json(basic_json&& other) noexcept
  18513. : json_base_class_t(std::forward<json_base_class_t>(other)),
  18514. m_data(std::move(other.m_data)) // cppcheck-suppress[accessForwarded] TODO check
  18515. #if JSON_DIAGNOSTIC_POSITIONS
  18516. , start_position(other.start_position) // cppcheck-suppress[accessForwarded] TODO check
  18517. , end_position(other.end_position) // cppcheck-suppress[accessForwarded] TODO check
  18518. #endif
  18519. {
  18520. // check that passed value is valid
  18521. other.assert_invariant(false); // cppcheck-suppress[accessForwarded]
  18522. // invalidate payload
  18523. other.m_data.m_type = value_t::null;
  18524. other.m_data.m_value = {};
  18525. #if JSON_DIAGNOSTIC_POSITIONS
  18526. other.start_position = std::string::npos;
  18527. other.end_position = std::string::npos;
  18528. #endif
  18529. set_parents();
  18530. assert_invariant();
  18531. }
  18532. /// @brief copy assignment
  18533. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  18534. basic_json& operator=(basic_json other) noexcept (
  18535. std::is_nothrow_move_constructible<value_t>::value&&
  18536. std::is_nothrow_move_assignable<value_t>::value&&
  18537. std::is_nothrow_move_constructible<json_value>::value&&
  18538. std::is_nothrow_move_assignable<json_value>::value&&
  18539. std::is_nothrow_move_assignable<json_base_class_t>::value
  18540. )
  18541. {
  18542. // check that passed value is valid
  18543. other.assert_invariant();
  18544. using std::swap;
  18545. swap(m_data.m_type, other.m_data.m_type);
  18546. swap(m_data.m_value, other.m_data.m_value);
  18547. #if JSON_DIAGNOSTIC_POSITIONS
  18548. swap(start_position, other.start_position);
  18549. swap(end_position, other.end_position);
  18550. #endif
  18551. json_base_class_t::operator=(std::move(other));
  18552. set_parents();
  18553. assert_invariant();
  18554. return *this;
  18555. }
  18556. /// @brief destructor
  18557. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  18558. ~basic_json() noexcept
  18559. {
  18560. assert_invariant(false);
  18561. }
  18562. /// @}
  18563. public:
  18564. ///////////////////////
  18565. // object inspection //
  18566. ///////////////////////
  18567. /// @name object inspection
  18568. /// Functions to inspect the type of a JSON value.
  18569. /// @{
  18570. /// @brief serialization
  18571. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  18572. string_t dump(const int indent = -1,
  18573. const char indent_char = ' ',
  18574. const bool ensure_ascii = false,
  18575. const error_handler_t error_handler = error_handler_t::strict) const
  18576. {
  18577. string_t result;
  18578. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  18579. if (indent >= 0)
  18580. {
  18581. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  18582. }
  18583. else
  18584. {
  18585. s.dump(*this, false, ensure_ascii, 0);
  18586. }
  18587. return result;
  18588. }
  18589. /// @brief return the type of the JSON value (explicit)
  18590. /// @sa https://json.nlohmann.me/api/basic_json/type/
  18591. constexpr value_t type() const noexcept
  18592. {
  18593. return m_data.m_type;
  18594. }
  18595. /// @brief return whether type is primitive
  18596. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  18597. constexpr bool is_primitive() const noexcept
  18598. {
  18599. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  18600. }
  18601. /// @brief return whether type is structured
  18602. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  18603. constexpr bool is_structured() const noexcept
  18604. {
  18605. return is_array() || is_object();
  18606. }
  18607. /// @brief return whether value is null
  18608. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  18609. constexpr bool is_null() const noexcept
  18610. {
  18611. return m_data.m_type == value_t::null;
  18612. }
  18613. /// @brief return whether value is a boolean
  18614. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  18615. constexpr bool is_boolean() const noexcept
  18616. {
  18617. return m_data.m_type == value_t::boolean;
  18618. }
  18619. /// @brief return whether value is a number
  18620. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  18621. constexpr bool is_number() const noexcept
  18622. {
  18623. return is_number_integer() || is_number_float();
  18624. }
  18625. /// @brief return whether value is an integer number
  18626. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  18627. constexpr bool is_number_integer() const noexcept
  18628. {
  18629. return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
  18630. }
  18631. /// @brief return whether value is an unsigned integer number
  18632. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  18633. constexpr bool is_number_unsigned() const noexcept
  18634. {
  18635. return m_data.m_type == value_t::number_unsigned;
  18636. }
  18637. /// @brief return whether value is a floating-point number
  18638. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  18639. constexpr bool is_number_float() const noexcept
  18640. {
  18641. return m_data.m_type == value_t::number_float;
  18642. }
  18643. /// @brief return whether value is an object
  18644. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  18645. constexpr bool is_object() const noexcept
  18646. {
  18647. return m_data.m_type == value_t::object;
  18648. }
  18649. /// @brief return whether value is an array
  18650. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  18651. constexpr bool is_array() const noexcept
  18652. {
  18653. return m_data.m_type == value_t::array;
  18654. }
  18655. /// @brief return whether value is a string
  18656. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  18657. constexpr bool is_string() const noexcept
  18658. {
  18659. return m_data.m_type == value_t::string;
  18660. }
  18661. /// @brief return whether value is a binary array
  18662. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  18663. constexpr bool is_binary() const noexcept
  18664. {
  18665. return m_data.m_type == value_t::binary;
  18666. }
  18667. /// @brief return whether value is discarded
  18668. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  18669. constexpr bool is_discarded() const noexcept
  18670. {
  18671. return m_data.m_type == value_t::discarded;
  18672. }
  18673. /// @brief return the type of the JSON value (implicit)
  18674. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  18675. constexpr operator value_t() const noexcept
  18676. {
  18677. return m_data.m_type;
  18678. }
  18679. /// @}
  18680. private:
  18681. //////////////////
  18682. // value access //
  18683. //////////////////
  18684. /// get a boolean (explicit)
  18685. boolean_t get_impl(boolean_t* /*unused*/) const
  18686. {
  18687. if (JSON_HEDLEY_LIKELY(is_boolean()))
  18688. {
  18689. return m_data.m_value.boolean;
  18690. }
  18691. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  18692. }
  18693. /// get a pointer to the value (object)
  18694. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  18695. {
  18696. return is_object() ? m_data.m_value.object : nullptr;
  18697. }
  18698. /// get a pointer to the value (object)
  18699. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  18700. {
  18701. return is_object() ? m_data.m_value.object : nullptr;
  18702. }
  18703. /// get a pointer to the value (array)
  18704. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  18705. {
  18706. return is_array() ? m_data.m_value.array : nullptr;
  18707. }
  18708. /// get a pointer to the value (array)
  18709. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  18710. {
  18711. return is_array() ? m_data.m_value.array : nullptr;
  18712. }
  18713. /// get a pointer to the value (string)
  18714. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  18715. {
  18716. return is_string() ? m_data.m_value.string : nullptr;
  18717. }
  18718. /// get a pointer to the value (string)
  18719. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  18720. {
  18721. return is_string() ? m_data.m_value.string : nullptr;
  18722. }
  18723. /// get a pointer to the value (boolean)
  18724. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  18725. {
  18726. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18727. }
  18728. /// get a pointer to the value (boolean)
  18729. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  18730. {
  18731. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18732. }
  18733. /// get a pointer to the value (integer number)
  18734. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  18735. {
  18736. return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
  18737. }
  18738. /// get a pointer to the value (integer number)
  18739. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  18740. {
  18741. return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
  18742. }
  18743. /// get a pointer to the value (unsigned number)
  18744. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  18745. {
  18746. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18747. }
  18748. /// get a pointer to the value (unsigned number)
  18749. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  18750. {
  18751. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18752. }
  18753. /// get a pointer to the value (floating-point number)
  18754. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  18755. {
  18756. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18757. }
  18758. /// get a pointer to the value (floating-point number)
  18759. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  18760. {
  18761. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18762. }
  18763. /// get a pointer to the value (binary)
  18764. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  18765. {
  18766. return is_binary() ? m_data.m_value.binary : nullptr;
  18767. }
  18768. /// get a pointer to the value (binary)
  18769. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  18770. {
  18771. return is_binary() ? m_data.m_value.binary : nullptr;
  18772. }
  18773. /*!
  18774. @brief helper function to implement get_ref()
  18775. This function helps to implement get_ref() without code duplication for
  18776. const and non-const overloads
  18777. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  18778. @throw type_error.303 if ReferenceType does not match underlying value
  18779. type of the current JSON
  18780. */
  18781. template<typename ReferenceType, typename ThisType>
  18782. static ReferenceType get_ref_impl(ThisType& obj)
  18783. {
  18784. // delegate the call to get_ptr<>()
  18785. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18786. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18787. {
  18788. return *ptr;
  18789. }
  18790. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18791. }
  18792. public:
  18793. /// @name value access
  18794. /// Direct access to the stored value of a JSON value.
  18795. /// @{
  18796. /// @brief get a pointer value (implicit)
  18797. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18798. template<typename PointerType, typename std::enable_if<
  18799. std::is_pointer<PointerType>::value, int>::type = 0>
  18800. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18801. {
  18802. // delegate the call to get_impl_ptr<>()
  18803. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18804. }
  18805. /// @brief get a pointer value (implicit)
  18806. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18807. template < typename PointerType, typename std::enable_if <
  18808. std::is_pointer<PointerType>::value&&
  18809. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18810. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18811. {
  18812. // delegate the call to get_impl_ptr<>() const
  18813. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18814. }
  18815. private:
  18816. /*!
  18817. @brief get a value (explicit)
  18818. Explicit type conversion between the JSON value and a compatible value
  18819. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18820. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18821. The value is converted by calling the @ref json_serializer<ValueType>
  18822. `from_json()` method.
  18823. The function is equivalent to executing
  18824. @code {.cpp}
  18825. ValueType ret;
  18826. JSONSerializer<ValueType>::from_json(*this, ret);
  18827. return ret;
  18828. @endcode
  18829. This overloads is chosen if:
  18830. - @a ValueType is not @ref basic_json,
  18831. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18832. `void from_json(const basic_json&, ValueType&)`, and
  18833. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  18834. the form `ValueType from_json(const basic_json&)`
  18835. @tparam ValueType the returned value type
  18836. @return copy of the JSON value, converted to @a ValueType
  18837. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18838. @liveexample{The example below shows several conversions from JSON values
  18839. to other types. There a few things to note: (1) Floating-point numbers can
  18840. be converted to integers\, (2) A JSON array can be converted to a standard
  18841. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18842. associative containers such as `std::unordered_map<std::string\,
  18843. json>`.,get__ValueType_const}
  18844. @since version 2.1.0
  18845. */
  18846. template < typename ValueType,
  18847. detail::enable_if_t <
  18848. detail::is_default_constructible<ValueType>::value&&
  18849. detail::has_from_json<basic_json_t, ValueType>::value,
  18850. int > = 0 >
  18851. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  18852. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  18853. {
  18854. auto ret = ValueType();
  18855. JSONSerializer<ValueType>::from_json(*this, ret);
  18856. return ret;
  18857. }
  18858. /*!
  18859. @brief get a value (explicit); special case
  18860. Explicit type conversion between the JSON value and a compatible value
  18861. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18862. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18863. The value is converted by calling the @ref json_serializer<ValueType>
  18864. `from_json()` method.
  18865. The function is equivalent to executing
  18866. @code {.cpp}
  18867. return JSONSerializer<ValueType>::from_json(*this);
  18868. @endcode
  18869. This overloads is chosen if:
  18870. - @a ValueType is not @ref basic_json and
  18871. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18872. `ValueType from_json(const basic_json&)`
  18873. @note If @ref json_serializer<ValueType> has both overloads of
  18874. `from_json()`, this one is chosen.
  18875. @tparam ValueType the returned value type
  18876. @return copy of the JSON value, converted to @a ValueType
  18877. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18878. @since version 2.1.0
  18879. */
  18880. template < typename ValueType,
  18881. detail::enable_if_t <
  18882. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  18883. int > = 0 >
  18884. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  18885. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  18886. {
  18887. return JSONSerializer<ValueType>::from_json(*this);
  18888. }
  18889. /*!
  18890. @brief get special-case overload
  18891. This overloads converts the current @ref basic_json in a different
  18892. @ref basic_json type
  18893. @tparam BasicJsonType == @ref basic_json
  18894. @return a copy of *this, converted into @a BasicJsonType
  18895. @complexity Depending on the implementation of the called `from_json()`
  18896. method.
  18897. @since version 3.2.0
  18898. */
  18899. template < typename BasicJsonType,
  18900. detail::enable_if_t <
  18901. detail::is_basic_json<BasicJsonType>::value,
  18902. int > = 0 >
  18903. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  18904. {
  18905. return *this;
  18906. }
  18907. /*!
  18908. @brief get special-case overload
  18909. This overloads avoids a lot of template boilerplate, it can be seen as the
  18910. identity method
  18911. @tparam BasicJsonType == @ref basic_json
  18912. @return a copy of *this
  18913. @complexity Constant.
  18914. @since version 2.1.0
  18915. */
  18916. template<typename BasicJsonType,
  18917. detail::enable_if_t<
  18918. std::is_same<BasicJsonType, basic_json_t>::value,
  18919. int> = 0>
  18920. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  18921. {
  18922. return *this;
  18923. }
  18924. /*!
  18925. @brief get a pointer value (explicit)
  18926. @copydoc get()
  18927. */
  18928. template<typename PointerType,
  18929. detail::enable_if_t<
  18930. std::is_pointer<PointerType>::value,
  18931. int> = 0>
  18932. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  18933. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  18934. {
  18935. // delegate the call to get_ptr
  18936. return get_ptr<PointerType>();
  18937. }
  18938. public:
  18939. /*!
  18940. @brief get a (pointer) value (explicit)
  18941. Performs explicit type conversion between the JSON value and a compatible value if required.
  18942. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  18943. No copies are made.
  18944. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  18945. from the current @ref basic_json.
  18946. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  18947. method.
  18948. @tparam ValueTypeCV the provided value type
  18949. @tparam ValueType the returned value type
  18950. @return copy of the JSON value, converted to @tparam ValueType if necessary
  18951. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  18952. @since version 2.1.0
  18953. */
  18954. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  18955. #if defined(JSON_HAS_CPP_14)
  18956. constexpr
  18957. #endif
  18958. auto get() const noexcept(
  18959. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  18960. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  18961. {
  18962. // we cannot static_assert on ValueTypeCV being non-const, because
  18963. // there is support for get<const basic_json_t>(), which is why we
  18964. // still need the uncvref
  18965. static_assert(!std::is_reference<ValueTypeCV>::value,
  18966. "get() cannot be used with reference types, you might want to use get_ref()");
  18967. return get_impl<ValueType>(detail::priority_tag<4> {});
  18968. }
  18969. /*!
  18970. @brief get a pointer value (explicit)
  18971. Explicit pointer access to the internally stored JSON value. No copies are
  18972. made.
  18973. @warning The pointer becomes invalid if the underlying JSON object
  18974. changes.
  18975. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  18976. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  18977. @ref number_unsigned_t, or @ref number_float_t.
  18978. @return pointer to the internally stored JSON value if the requested
  18979. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  18980. @complexity Constant.
  18981. @liveexample{The example below shows how pointers to internal values of a
  18982. JSON value can be requested. Note that no type conversions are made and a
  18983. `nullptr` is returned if the value and the requested pointer type does not
  18984. match.,get__PointerType}
  18985. @sa see @ref get_ptr() for explicit pointer-member access
  18986. @since version 1.0.0
  18987. */
  18988. template<typename PointerType, typename std::enable_if<
  18989. std::is_pointer<PointerType>::value, int>::type = 0>
  18990. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  18991. {
  18992. // delegate the call to get_ptr
  18993. return get_ptr<PointerType>();
  18994. }
  18995. /// @brief get a value (explicit)
  18996. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  18997. template < typename ValueType,
  18998. detail::enable_if_t <
  18999. !detail::is_basic_json<ValueType>::value&&
  19000. detail::has_from_json<basic_json_t, ValueType>::value,
  19001. int > = 0 >
  19002. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  19003. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  19004. {
  19005. JSONSerializer<ValueType>::from_json(*this, v);
  19006. return v;
  19007. }
  19008. // specialization to allow calling get_to with a basic_json value
  19009. // see https://github.com/nlohmann/json/issues/2175
  19010. template<typename ValueType,
  19011. detail::enable_if_t <
  19012. detail::is_basic_json<ValueType>::value,
  19013. int> = 0>
  19014. ValueType & get_to(ValueType& v) const
  19015. {
  19016. v = *this;
  19017. return v;
  19018. }
  19019. template <
  19020. typename T, std::size_t N,
  19021. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  19022. detail::enable_if_t <
  19023. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  19024. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  19025. noexcept(noexcept(JSONSerializer<Array>::from_json(
  19026. std::declval<const basic_json_t&>(), v)))
  19027. {
  19028. JSONSerializer<Array>::from_json(*this, v);
  19029. return v;
  19030. }
  19031. /// @brief get a reference value (implicit)
  19032. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  19033. template<typename ReferenceType, typename std::enable_if<
  19034. std::is_reference<ReferenceType>::value, int>::type = 0>
  19035. ReferenceType get_ref()
  19036. {
  19037. // delegate call to get_ref_impl
  19038. return get_ref_impl<ReferenceType>(*this);
  19039. }
  19040. /// @brief get a reference value (implicit)
  19041. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  19042. template < typename ReferenceType, typename std::enable_if <
  19043. std::is_reference<ReferenceType>::value&&
  19044. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  19045. ReferenceType get_ref() const
  19046. {
  19047. // delegate call to get_ref_impl
  19048. return get_ref_impl<ReferenceType>(*this);
  19049. }
  19050. /*!
  19051. @brief get a value (implicit)
  19052. Implicit type conversion between the JSON value and a compatible value.
  19053. The call is realized by calling @ref get() const.
  19054. @tparam ValueType non-pointer type compatible to the JSON value, for
  19055. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  19056. `std::vector` types for JSON arrays. The character type of @ref string_t
  19057. as well as an initializer list of this type is excluded to avoid
  19058. ambiguities as these types implicitly convert to `std::string`.
  19059. @return copy of the JSON value, converted to type @a ValueType
  19060. @throw type_error.302 in case passed type @a ValueType is incompatible
  19061. to the JSON value type (e.g., the JSON value is of type boolean, but a
  19062. string is requested); see example below
  19063. @complexity Linear in the size of the JSON value.
  19064. @liveexample{The example below shows several conversions from JSON values
  19065. to other types. There a few things to note: (1) Floating-point numbers can
  19066. be converted to integers\, (2) A JSON array can be converted to a standard
  19067. `std::vector<short>`\, (3) A JSON object can be converted to C++
  19068. associative containers such as `std::unordered_map<std::string\,
  19069. json>`.,operator__ValueType}
  19070. @since version 1.0.0
  19071. */
  19072. template < typename ValueType, typename std::enable_if <
  19073. detail::conjunction <
  19074. detail::negation<std::is_pointer<ValueType>>,
  19075. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  19076. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  19077. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  19078. detail::negation<detail::is_basic_json<ValueType>>,
  19079. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  19080. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  19081. detail::negation<std::is_same<ValueType, std::string_view>>,
  19082. #endif
  19083. #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
  19084. detail::negation<std::is_same<ValueType, std::any>>,
  19085. #endif
  19086. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  19087. >::value, int >::type = 0 >
  19088. JSON_EXPLICIT operator ValueType() const
  19089. {
  19090. // delegate the call to get<>() const
  19091. return get<ValueType>();
  19092. }
  19093. /// @brief get a binary value
  19094. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  19095. binary_t& get_binary()
  19096. {
  19097. if (!is_binary())
  19098. {
  19099. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  19100. }
  19101. return *get_ptr<binary_t*>();
  19102. }
  19103. /// @brief get a binary value
  19104. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  19105. const binary_t& get_binary() const
  19106. {
  19107. if (!is_binary())
  19108. {
  19109. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  19110. }
  19111. return *get_ptr<const binary_t*>();
  19112. }
  19113. /// @}
  19114. ////////////////////
  19115. // element access //
  19116. ////////////////////
  19117. /// @name element access
  19118. /// Access to the JSON value.
  19119. /// @{
  19120. /// @brief access specified array element with bounds checking
  19121. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19122. reference at(size_type idx)
  19123. {
  19124. // at only works for arrays
  19125. if (JSON_HEDLEY_LIKELY(is_array()))
  19126. {
  19127. JSON_TRY
  19128. {
  19129. return set_parent(m_data.m_value.array->at(idx));
  19130. }
  19131. JSON_CATCH (std::out_of_range&)
  19132. {
  19133. // create better exception explanation
  19134. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19135. } // cppcheck-suppress[missingReturn]
  19136. }
  19137. else
  19138. {
  19139. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19140. }
  19141. }
  19142. /// @brief access specified array element with bounds checking
  19143. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19144. const_reference at(size_type idx) const
  19145. {
  19146. // at only works for arrays
  19147. if (JSON_HEDLEY_LIKELY(is_array()))
  19148. {
  19149. JSON_TRY
  19150. {
  19151. return m_data.m_value.array->at(idx);
  19152. }
  19153. JSON_CATCH (std::out_of_range&)
  19154. {
  19155. // create better exception explanation
  19156. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19157. } // cppcheck-suppress[missingReturn]
  19158. }
  19159. else
  19160. {
  19161. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19162. }
  19163. }
  19164. /// @brief access specified object element with bounds checking
  19165. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19166. reference at(const typename object_t::key_type& key)
  19167. {
  19168. // at only works for objects
  19169. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19170. {
  19171. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19172. }
  19173. auto it = m_data.m_value.object->find(key);
  19174. if (it == m_data.m_value.object->end())
  19175. {
  19176. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  19177. }
  19178. return set_parent(it->second);
  19179. }
  19180. /// @brief access specified object element with bounds checking
  19181. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19182. template<class KeyType, detail::enable_if_t<
  19183. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19184. reference at(KeyType && key)
  19185. {
  19186. // at only works for objects
  19187. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19188. {
  19189. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19190. }
  19191. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19192. if (it == m_data.m_value.object->end())
  19193. {
  19194. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  19195. }
  19196. return set_parent(it->second);
  19197. }
  19198. /// @brief access specified object element with bounds checking
  19199. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19200. const_reference at(const typename object_t::key_type& key) const
  19201. {
  19202. // at only works for objects
  19203. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19204. {
  19205. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19206. }
  19207. auto it = m_data.m_value.object->find(key);
  19208. if (it == m_data.m_value.object->end())
  19209. {
  19210. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  19211. }
  19212. return it->second;
  19213. }
  19214. /// @brief access specified object element with bounds checking
  19215. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19216. template<class KeyType, detail::enable_if_t<
  19217. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19218. const_reference at(KeyType && key) const
  19219. {
  19220. // at only works for objects
  19221. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19222. {
  19223. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19224. }
  19225. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19226. if (it == m_data.m_value.object->end())
  19227. {
  19228. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  19229. }
  19230. return it->second;
  19231. }
  19232. /// @brief access specified array element
  19233. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19234. reference operator[](size_type idx)
  19235. {
  19236. // implicitly convert null value to an empty array
  19237. if (is_null())
  19238. {
  19239. m_data.m_type = value_t::array;
  19240. m_data.m_value.array = create<array_t>();
  19241. assert_invariant();
  19242. }
  19243. // operator[] only works for arrays
  19244. if (JSON_HEDLEY_LIKELY(is_array()))
  19245. {
  19246. // fill up array with null values if given idx is outside range
  19247. if (idx >= m_data.m_value.array->size())
  19248. {
  19249. #if JSON_DIAGNOSTICS
  19250. // remember array size & capacity before resizing
  19251. const auto old_size = m_data.m_value.array->size();
  19252. const auto old_capacity = m_data.m_value.array->capacity();
  19253. #endif
  19254. m_data.m_value.array->resize(idx + 1);
  19255. #if JSON_DIAGNOSTICS
  19256. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  19257. {
  19258. // capacity has changed: update all parents
  19259. set_parents();
  19260. }
  19261. else
  19262. {
  19263. // set parent for values added above
  19264. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  19265. }
  19266. #endif
  19267. assert_invariant();
  19268. }
  19269. return m_data.m_value.array->operator[](idx);
  19270. }
  19271. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  19272. }
  19273. /// @brief access specified array element
  19274. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19275. const_reference operator[](size_type idx) const
  19276. {
  19277. // const operator[] only works for arrays
  19278. if (JSON_HEDLEY_LIKELY(is_array()))
  19279. {
  19280. return m_data.m_value.array->operator[](idx);
  19281. }
  19282. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  19283. }
  19284. /// @brief access specified object element
  19285. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19286. reference operator[](typename object_t::key_type key) // NOLINT(performance-unnecessary-value-param)
  19287. {
  19288. // implicitly convert null value to an empty object
  19289. if (is_null())
  19290. {
  19291. m_data.m_type = value_t::object;
  19292. m_data.m_value.object = create<object_t>();
  19293. assert_invariant();
  19294. }
  19295. // operator[] only works for objects
  19296. if (JSON_HEDLEY_LIKELY(is_object()))
  19297. {
  19298. auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
  19299. return set_parent(result.first->second);
  19300. }
  19301. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19302. }
  19303. /// @brief access specified object element
  19304. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19305. const_reference operator[](const typename object_t::key_type& key) const
  19306. {
  19307. // const operator[] only works for objects
  19308. if (JSON_HEDLEY_LIKELY(is_object()))
  19309. {
  19310. auto it = m_data.m_value.object->find(key);
  19311. JSON_ASSERT(it != m_data.m_value.object->end());
  19312. return it->second;
  19313. }
  19314. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19315. }
  19316. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  19317. // (they seemingly cannot be constrained to resolve the ambiguity)
  19318. template<typename T>
  19319. reference operator[](T* key)
  19320. {
  19321. return operator[](typename object_t::key_type(key));
  19322. }
  19323. template<typename T>
  19324. const_reference operator[](T* key) const
  19325. {
  19326. return operator[](typename object_t::key_type(key));
  19327. }
  19328. /// @brief access specified object element
  19329. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19330. template<class KeyType, detail::enable_if_t<
  19331. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19332. reference operator[](KeyType && key)
  19333. {
  19334. // implicitly convert null value to an empty object
  19335. if (is_null())
  19336. {
  19337. m_data.m_type = value_t::object;
  19338. m_data.m_value.object = create<object_t>();
  19339. assert_invariant();
  19340. }
  19341. // operator[] only works for objects
  19342. if (JSON_HEDLEY_LIKELY(is_object()))
  19343. {
  19344. auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  19345. return set_parent(result.first->second);
  19346. }
  19347. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19348. }
  19349. /// @brief access specified object element
  19350. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19351. template<class KeyType, detail::enable_if_t<
  19352. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19353. const_reference operator[](KeyType && key) const
  19354. {
  19355. // const operator[] only works for objects
  19356. if (JSON_HEDLEY_LIKELY(is_object()))
  19357. {
  19358. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19359. JSON_ASSERT(it != m_data.m_value.object->end());
  19360. return it->second;
  19361. }
  19362. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19363. }
  19364. private:
  19365. template<typename KeyType>
  19366. using is_comparable_with_object_key = detail::is_comparable <
  19367. object_comparator_t, const typename object_t::key_type&, KeyType >;
  19368. template<typename ValueType>
  19369. using value_return_type = std::conditional <
  19370. detail::is_c_string_uncvref<ValueType>::value,
  19371. string_t, typename std::decay<ValueType>::type >;
  19372. public:
  19373. /// @brief access specified object element with default value
  19374. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19375. template < class ValueType, detail::enable_if_t <
  19376. !detail::is_transparent<object_comparator_t>::value
  19377. && detail::is_getable<basic_json_t, ValueType>::value
  19378. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19379. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  19380. {
  19381. // value only works for objects
  19382. if (JSON_HEDLEY_LIKELY(is_object()))
  19383. {
  19384. // if key is found, return value and given default value otherwise
  19385. const auto it = find(key);
  19386. if (it != end())
  19387. {
  19388. return it->template get<ValueType>();
  19389. }
  19390. return default_value;
  19391. }
  19392. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19393. }
  19394. /// @brief access specified object element with default value
  19395. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19396. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  19397. detail::enable_if_t <
  19398. !detail::is_transparent<object_comparator_t>::value
  19399. && detail::is_getable<basic_json_t, ReturnType>::value
  19400. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19401. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  19402. {
  19403. // value only works for objects
  19404. if (JSON_HEDLEY_LIKELY(is_object()))
  19405. {
  19406. // if key is found, return value and given default value otherwise
  19407. const auto it = find(key);
  19408. if (it != end())
  19409. {
  19410. return it->template get<ReturnType>();
  19411. }
  19412. return std::forward<ValueType>(default_value);
  19413. }
  19414. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19415. }
  19416. /// @brief access specified object element with default value
  19417. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19418. template < class ValueType, class KeyType, detail::enable_if_t <
  19419. detail::is_transparent<object_comparator_t>::value
  19420. && !detail::is_json_pointer<KeyType>::value
  19421. && is_comparable_with_object_key<KeyType>::value
  19422. && detail::is_getable<basic_json_t, ValueType>::value
  19423. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19424. ValueType value(KeyType && key, const ValueType& default_value) const
  19425. {
  19426. // value only works for objects
  19427. if (JSON_HEDLEY_LIKELY(is_object()))
  19428. {
  19429. // if key is found, return value and given default value otherwise
  19430. const auto it = find(std::forward<KeyType>(key));
  19431. if (it != end())
  19432. {
  19433. return it->template get<ValueType>();
  19434. }
  19435. return default_value;
  19436. }
  19437. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19438. }
  19439. /// @brief access specified object element via JSON Pointer with default value
  19440. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19441. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  19442. detail::enable_if_t <
  19443. detail::is_transparent<object_comparator_t>::value
  19444. && !detail::is_json_pointer<KeyType>::value
  19445. && is_comparable_with_object_key<KeyType>::value
  19446. && detail::is_getable<basic_json_t, ReturnType>::value
  19447. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19448. ReturnType value(KeyType && key, ValueType && default_value) const
  19449. {
  19450. // value only works for objects
  19451. if (JSON_HEDLEY_LIKELY(is_object()))
  19452. {
  19453. // if key is found, return value and given default value otherwise
  19454. const auto it = find(std::forward<KeyType>(key));
  19455. if (it != end())
  19456. {
  19457. return it->template get<ReturnType>();
  19458. }
  19459. return std::forward<ValueType>(default_value);
  19460. }
  19461. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19462. }
  19463. /// @brief access specified object element via JSON Pointer with default value
  19464. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19465. template < class ValueType, detail::enable_if_t <
  19466. detail::is_getable<basic_json_t, ValueType>::value
  19467. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19468. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  19469. {
  19470. // value only works for objects
  19471. if (JSON_HEDLEY_LIKELY(is_object()))
  19472. {
  19473. // if pointer resolves a value, return it or use default value
  19474. JSON_TRY
  19475. {
  19476. return ptr.get_checked(this).template get<ValueType>();
  19477. }
  19478. JSON_INTERNAL_CATCH (out_of_range&)
  19479. {
  19480. return default_value;
  19481. }
  19482. }
  19483. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19484. }
  19485. /// @brief access specified object element via JSON Pointer with default value
  19486. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19487. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  19488. detail::enable_if_t <
  19489. detail::is_getable<basic_json_t, ReturnType>::value
  19490. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19491. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  19492. {
  19493. // value only works for objects
  19494. if (JSON_HEDLEY_LIKELY(is_object()))
  19495. {
  19496. // if pointer resolves a value, return it or use default value
  19497. JSON_TRY
  19498. {
  19499. return ptr.get_checked(this).template get<ReturnType>();
  19500. }
  19501. JSON_INTERNAL_CATCH (out_of_range&)
  19502. {
  19503. return std::forward<ValueType>(default_value);
  19504. }
  19505. }
  19506. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19507. }
  19508. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  19509. detail::is_basic_json<BasicJsonType>::value
  19510. && detail::is_getable<basic_json_t, ValueType>::value
  19511. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19512. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19513. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  19514. {
  19515. return value(ptr.convert(), default_value);
  19516. }
  19517. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  19518. detail::enable_if_t <
  19519. detail::is_basic_json<BasicJsonType>::value
  19520. && detail::is_getable<basic_json_t, ReturnType>::value
  19521. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19522. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19523. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  19524. {
  19525. return value(ptr.convert(), std::forward<ValueType>(default_value));
  19526. }
  19527. /// @brief access the first element
  19528. /// @sa https://json.nlohmann.me/api/basic_json/front/
  19529. reference front()
  19530. {
  19531. return *begin();
  19532. }
  19533. /// @brief access the first element
  19534. /// @sa https://json.nlohmann.me/api/basic_json/front/
  19535. const_reference front() const
  19536. {
  19537. return *cbegin();
  19538. }
  19539. /// @brief access the last element
  19540. /// @sa https://json.nlohmann.me/api/basic_json/back/
  19541. reference back()
  19542. {
  19543. auto tmp = end();
  19544. --tmp;
  19545. return *tmp;
  19546. }
  19547. /// @brief access the last element
  19548. /// @sa https://json.nlohmann.me/api/basic_json/back/
  19549. const_reference back() const
  19550. {
  19551. auto tmp = cend();
  19552. --tmp;
  19553. return *tmp;
  19554. }
  19555. /// @brief remove element given an iterator
  19556. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19557. template < class IteratorType, detail::enable_if_t <
  19558. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  19559. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  19560. IteratorType erase(IteratorType pos) // NOLINT(performance-unnecessary-value-param)
  19561. {
  19562. // make sure iterator fits the current value
  19563. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  19564. {
  19565. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19566. }
  19567. IteratorType result = end();
  19568. switch (m_data.m_type)
  19569. {
  19570. case value_t::boolean:
  19571. case value_t::number_float:
  19572. case value_t::number_integer:
  19573. case value_t::number_unsigned:
  19574. case value_t::string:
  19575. case value_t::binary:
  19576. {
  19577. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  19578. {
  19579. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  19580. }
  19581. if (is_string())
  19582. {
  19583. AllocatorType<string_t> alloc;
  19584. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  19585. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  19586. m_data.m_value.string = nullptr;
  19587. }
  19588. else if (is_binary())
  19589. {
  19590. AllocatorType<binary_t> alloc;
  19591. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  19592. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  19593. m_data.m_value.binary = nullptr;
  19594. }
  19595. m_data.m_type = value_t::null;
  19596. assert_invariant();
  19597. break;
  19598. }
  19599. case value_t::object:
  19600. {
  19601. result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
  19602. break;
  19603. }
  19604. case value_t::array:
  19605. {
  19606. result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
  19607. break;
  19608. }
  19609. case value_t::null:
  19610. case value_t::discarded:
  19611. default:
  19612. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19613. }
  19614. return result;
  19615. }
  19616. /// @brief remove elements given an iterator range
  19617. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19618. template < class IteratorType, detail::enable_if_t <
  19619. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  19620. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  19621. IteratorType erase(IteratorType first, IteratorType last) // NOLINT(performance-unnecessary-value-param)
  19622. {
  19623. // make sure iterator fits the current value
  19624. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  19625. {
  19626. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  19627. }
  19628. IteratorType result = end();
  19629. switch (m_data.m_type)
  19630. {
  19631. case value_t::boolean:
  19632. case value_t::number_float:
  19633. case value_t::number_integer:
  19634. case value_t::number_unsigned:
  19635. case value_t::string:
  19636. case value_t::binary:
  19637. {
  19638. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  19639. || !last.m_it.primitive_iterator.is_end()))
  19640. {
  19641. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  19642. }
  19643. if (is_string())
  19644. {
  19645. AllocatorType<string_t> alloc;
  19646. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  19647. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  19648. m_data.m_value.string = nullptr;
  19649. }
  19650. else if (is_binary())
  19651. {
  19652. AllocatorType<binary_t> alloc;
  19653. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  19654. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  19655. m_data.m_value.binary = nullptr;
  19656. }
  19657. m_data.m_type = value_t::null;
  19658. assert_invariant();
  19659. break;
  19660. }
  19661. case value_t::object:
  19662. {
  19663. result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
  19664. last.m_it.object_iterator);
  19665. break;
  19666. }
  19667. case value_t::array:
  19668. {
  19669. result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
  19670. last.m_it.array_iterator);
  19671. break;
  19672. }
  19673. case value_t::null:
  19674. case value_t::discarded:
  19675. default:
  19676. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19677. }
  19678. return result;
  19679. }
  19680. private:
  19681. template < typename KeyType, detail::enable_if_t <
  19682. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19683. size_type erase_internal(KeyType && key)
  19684. {
  19685. // this erase only works for objects
  19686. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19687. {
  19688. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19689. }
  19690. return m_data.m_value.object->erase(std::forward<KeyType>(key));
  19691. }
  19692. template < typename KeyType, detail::enable_if_t <
  19693. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19694. size_type erase_internal(KeyType && key)
  19695. {
  19696. // this erase only works for objects
  19697. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19698. {
  19699. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19700. }
  19701. const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19702. if (it != m_data.m_value.object->end())
  19703. {
  19704. m_data.m_value.object->erase(it);
  19705. return 1;
  19706. }
  19707. return 0;
  19708. }
  19709. public:
  19710. /// @brief remove element from a JSON object given a key
  19711. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19712. size_type erase(const typename object_t::key_type& key)
  19713. {
  19714. // the indirection via erase_internal() is added to avoid making this
  19715. // function a template and thus de-rank it during overload resolution
  19716. return erase_internal(key);
  19717. }
  19718. /// @brief remove element from a JSON object given a key
  19719. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19720. template<class KeyType, detail::enable_if_t<
  19721. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19722. size_type erase(KeyType && key)
  19723. {
  19724. return erase_internal(std::forward<KeyType>(key));
  19725. }
  19726. /// @brief remove element from a JSON array given an index
  19727. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19728. void erase(const size_type idx)
  19729. {
  19730. // this erase only works for arrays
  19731. if (JSON_HEDLEY_LIKELY(is_array()))
  19732. {
  19733. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  19734. {
  19735. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19736. }
  19737. m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
  19738. }
  19739. else
  19740. {
  19741. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19742. }
  19743. }
  19744. /// @}
  19745. ////////////
  19746. // lookup //
  19747. ////////////
  19748. /// @name lookup
  19749. /// @{
  19750. /// @brief find an element in a JSON object
  19751. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19752. iterator find(const typename object_t::key_type& key)
  19753. {
  19754. auto result = end();
  19755. if (is_object())
  19756. {
  19757. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19758. }
  19759. return result;
  19760. }
  19761. /// @brief find an element in a JSON object
  19762. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19763. const_iterator find(const typename object_t::key_type& key) const
  19764. {
  19765. auto result = cend();
  19766. if (is_object())
  19767. {
  19768. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19769. }
  19770. return result;
  19771. }
  19772. /// @brief find an element in a JSON object
  19773. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19774. template<class KeyType, detail::enable_if_t<
  19775. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19776. iterator find(KeyType && key)
  19777. {
  19778. auto result = end();
  19779. if (is_object())
  19780. {
  19781. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19782. }
  19783. return result;
  19784. }
  19785. /// @brief find an element in a JSON object
  19786. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19787. template<class KeyType, detail::enable_if_t<
  19788. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19789. const_iterator find(KeyType && key) const
  19790. {
  19791. auto result = cend();
  19792. if (is_object())
  19793. {
  19794. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19795. }
  19796. return result;
  19797. }
  19798. /// @brief returns the number of occurrences of a key in a JSON object
  19799. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19800. size_type count(const typename object_t::key_type& key) const
  19801. {
  19802. // return 0 for all nonobject types
  19803. return is_object() ? m_data.m_value.object->count(key) : 0;
  19804. }
  19805. /// @brief returns the number of occurrences of a key in a JSON object
  19806. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19807. template<class KeyType, detail::enable_if_t<
  19808. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19809. size_type count(KeyType && key) const
  19810. {
  19811. // return 0 for all nonobject types
  19812. return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
  19813. }
  19814. /// @brief check the existence of an element in a JSON object
  19815. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19816. bool contains(const typename object_t::key_type& key) const
  19817. {
  19818. return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
  19819. }
  19820. /// @brief check the existence of an element in a JSON object
  19821. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19822. template<class KeyType, detail::enable_if_t<
  19823. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19824. bool contains(KeyType && key) const
  19825. {
  19826. return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
  19827. }
  19828. /// @brief check the existence of an element in a JSON object given a JSON pointer
  19829. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19830. bool contains(const json_pointer& ptr) const
  19831. {
  19832. return ptr.contains(this);
  19833. }
  19834. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  19835. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19836. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  19837. {
  19838. return ptr.contains(this);
  19839. }
  19840. /// @}
  19841. ///////////////
  19842. // iterators //
  19843. ///////////////
  19844. /// @name iterators
  19845. /// @{
  19846. /// @brief returns an iterator to the first element
  19847. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19848. iterator begin() noexcept
  19849. {
  19850. iterator result(this);
  19851. result.set_begin();
  19852. return result;
  19853. }
  19854. /// @brief returns an iterator to the first element
  19855. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19856. const_iterator begin() const noexcept
  19857. {
  19858. return cbegin();
  19859. }
  19860. /// @brief returns a const iterator to the first element
  19861. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  19862. const_iterator cbegin() const noexcept
  19863. {
  19864. const_iterator result(this);
  19865. result.set_begin();
  19866. return result;
  19867. }
  19868. /// @brief returns an iterator to one past the last element
  19869. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19870. iterator end() noexcept
  19871. {
  19872. iterator result(this);
  19873. result.set_end();
  19874. return result;
  19875. }
  19876. /// @brief returns an iterator to one past the last element
  19877. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19878. const_iterator end() const noexcept
  19879. {
  19880. return cend();
  19881. }
  19882. /// @brief returns an iterator to one past the last element
  19883. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  19884. const_iterator cend() const noexcept
  19885. {
  19886. const_iterator result(this);
  19887. result.set_end();
  19888. return result;
  19889. }
  19890. /// @brief returns an iterator to the reverse-beginning
  19891. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19892. reverse_iterator rbegin() noexcept
  19893. {
  19894. return reverse_iterator(end());
  19895. }
  19896. /// @brief returns an iterator to the reverse-beginning
  19897. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19898. const_reverse_iterator rbegin() const noexcept
  19899. {
  19900. return crbegin();
  19901. }
  19902. /// @brief returns an iterator to the reverse-end
  19903. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19904. reverse_iterator rend() noexcept
  19905. {
  19906. return reverse_iterator(begin());
  19907. }
  19908. /// @brief returns an iterator to the reverse-end
  19909. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19910. const_reverse_iterator rend() const noexcept
  19911. {
  19912. return crend();
  19913. }
  19914. /// @brief returns a const reverse iterator to the last element
  19915. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  19916. const_reverse_iterator crbegin() const noexcept
  19917. {
  19918. return const_reverse_iterator(cend());
  19919. }
  19920. /// @brief returns a const reverse iterator to one before the first
  19921. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  19922. const_reverse_iterator crend() const noexcept
  19923. {
  19924. return const_reverse_iterator(cbegin());
  19925. }
  19926. public:
  19927. /// @brief wrapper to access iterator member functions in range-based for
  19928. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19929. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19930. /// version 4.0.0 of the library. Please use @ref items() instead;
  19931. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19932. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19933. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19934. {
  19935. return ref.items();
  19936. }
  19937. /// @brief wrapper to access iterator member functions in range-based for
  19938. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19939. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19940. /// version 4.0.0 of the library. Please use @ref items() instead;
  19941. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19942. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19943. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19944. {
  19945. return ref.items();
  19946. }
  19947. /// @brief helper to access iterator member functions in range-based for
  19948. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19949. iteration_proxy<iterator> items() noexcept
  19950. {
  19951. return iteration_proxy<iterator>(*this);
  19952. }
  19953. /// @brief helper to access iterator member functions in range-based for
  19954. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19955. iteration_proxy<const_iterator> items() const noexcept
  19956. {
  19957. return iteration_proxy<const_iterator>(*this);
  19958. }
  19959. /// @}
  19960. //////////////
  19961. // capacity //
  19962. //////////////
  19963. /// @name capacity
  19964. /// @{
  19965. /// @brief checks whether the container is empty.
  19966. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  19967. bool empty() const noexcept
  19968. {
  19969. switch (m_data.m_type)
  19970. {
  19971. case value_t::null:
  19972. {
  19973. // null values are empty
  19974. return true;
  19975. }
  19976. case value_t::array:
  19977. {
  19978. // delegate call to array_t::empty()
  19979. return m_data.m_value.array->empty();
  19980. }
  19981. case value_t::object:
  19982. {
  19983. // delegate call to object_t::empty()
  19984. return m_data.m_value.object->empty();
  19985. }
  19986. case value_t::string:
  19987. case value_t::boolean:
  19988. case value_t::number_integer:
  19989. case value_t::number_unsigned:
  19990. case value_t::number_float:
  19991. case value_t::binary:
  19992. case value_t::discarded:
  19993. default:
  19994. {
  19995. // all other types are nonempty
  19996. return false;
  19997. }
  19998. }
  19999. }
  20000. /// @brief returns the number of elements
  20001. /// @sa https://json.nlohmann.me/api/basic_json/size/
  20002. size_type size() const noexcept
  20003. {
  20004. switch (m_data.m_type)
  20005. {
  20006. case value_t::null:
  20007. {
  20008. // null values are empty
  20009. return 0;
  20010. }
  20011. case value_t::array:
  20012. {
  20013. // delegate call to array_t::size()
  20014. return m_data.m_value.array->size();
  20015. }
  20016. case value_t::object:
  20017. {
  20018. // delegate call to object_t::size()
  20019. return m_data.m_value.object->size();
  20020. }
  20021. case value_t::string:
  20022. case value_t::boolean:
  20023. case value_t::number_integer:
  20024. case value_t::number_unsigned:
  20025. case value_t::number_float:
  20026. case value_t::binary:
  20027. case value_t::discarded:
  20028. default:
  20029. {
  20030. // all other types have size 1
  20031. return 1;
  20032. }
  20033. }
  20034. }
  20035. /// @brief returns the maximum possible number of elements
  20036. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  20037. size_type max_size() const noexcept
  20038. {
  20039. switch (m_data.m_type)
  20040. {
  20041. case value_t::array:
  20042. {
  20043. // delegate call to array_t::max_size()
  20044. return m_data.m_value.array->max_size();
  20045. }
  20046. case value_t::object:
  20047. {
  20048. // delegate call to object_t::max_size()
  20049. return m_data.m_value.object->max_size();
  20050. }
  20051. case value_t::null:
  20052. case value_t::string:
  20053. case value_t::boolean:
  20054. case value_t::number_integer:
  20055. case value_t::number_unsigned:
  20056. case value_t::number_float:
  20057. case value_t::binary:
  20058. case value_t::discarded:
  20059. default:
  20060. {
  20061. // all other types have max_size() == size()
  20062. return size();
  20063. }
  20064. }
  20065. }
  20066. /// @}
  20067. ///////////////
  20068. // modifiers //
  20069. ///////////////
  20070. /// @name modifiers
  20071. /// @{
  20072. /// @brief clears the contents
  20073. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  20074. void clear() noexcept
  20075. {
  20076. switch (m_data.m_type)
  20077. {
  20078. case value_t::number_integer:
  20079. {
  20080. m_data.m_value.number_integer = 0;
  20081. break;
  20082. }
  20083. case value_t::number_unsigned:
  20084. {
  20085. m_data.m_value.number_unsigned = 0;
  20086. break;
  20087. }
  20088. case value_t::number_float:
  20089. {
  20090. m_data.m_value.number_float = 0.0;
  20091. break;
  20092. }
  20093. case value_t::boolean:
  20094. {
  20095. m_data.m_value.boolean = false;
  20096. break;
  20097. }
  20098. case value_t::string:
  20099. {
  20100. m_data.m_value.string->clear();
  20101. break;
  20102. }
  20103. case value_t::binary:
  20104. {
  20105. m_data.m_value.binary->clear();
  20106. break;
  20107. }
  20108. case value_t::array:
  20109. {
  20110. m_data.m_value.array->clear();
  20111. break;
  20112. }
  20113. case value_t::object:
  20114. {
  20115. m_data.m_value.object->clear();
  20116. break;
  20117. }
  20118. case value_t::null:
  20119. case value_t::discarded:
  20120. default:
  20121. break;
  20122. }
  20123. }
  20124. /// @brief add an object to an array
  20125. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20126. void push_back(basic_json&& val)
  20127. {
  20128. // push_back only works for null objects or arrays
  20129. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  20130. {
  20131. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  20132. }
  20133. // transform null object into an array
  20134. if (is_null())
  20135. {
  20136. m_data.m_type = value_t::array;
  20137. m_data.m_value = value_t::array;
  20138. assert_invariant();
  20139. }
  20140. // add element to array (move semantics)
  20141. const auto old_capacity = m_data.m_value.array->capacity();
  20142. m_data.m_value.array->push_back(std::move(val));
  20143. set_parent(m_data.m_value.array->back(), old_capacity);
  20144. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  20145. }
  20146. /// @brief add an object to an array
  20147. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20148. reference operator+=(basic_json&& val)
  20149. {
  20150. push_back(std::move(val));
  20151. return *this;
  20152. }
  20153. /// @brief add an object to an array
  20154. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20155. void push_back(const basic_json& val)
  20156. {
  20157. // push_back only works for null objects or arrays
  20158. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  20159. {
  20160. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  20161. }
  20162. // transform null object into an array
  20163. if (is_null())
  20164. {
  20165. m_data.m_type = value_t::array;
  20166. m_data.m_value = value_t::array;
  20167. assert_invariant();
  20168. }
  20169. // add element to array
  20170. const auto old_capacity = m_data.m_value.array->capacity();
  20171. m_data.m_value.array->push_back(val);
  20172. set_parent(m_data.m_value.array->back(), old_capacity);
  20173. }
  20174. /// @brief add an object to an array
  20175. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20176. reference operator+=(const basic_json& val)
  20177. {
  20178. push_back(val);
  20179. return *this;
  20180. }
  20181. /// @brief add an object to an object
  20182. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20183. void push_back(const typename object_t::value_type& val)
  20184. {
  20185. // push_back only works for null objects or objects
  20186. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  20187. {
  20188. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  20189. }
  20190. // transform null object into an object
  20191. if (is_null())
  20192. {
  20193. m_data.m_type = value_t::object;
  20194. m_data.m_value = value_t::object;
  20195. assert_invariant();
  20196. }
  20197. // add element to object
  20198. auto res = m_data.m_value.object->insert(val);
  20199. set_parent(res.first->second);
  20200. }
  20201. /// @brief add an object to an object
  20202. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20203. reference operator+=(const typename object_t::value_type& val)
  20204. {
  20205. push_back(val);
  20206. return *this;
  20207. }
  20208. /// @brief add an object to an object
  20209. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20210. void push_back(initializer_list_t init)
  20211. {
  20212. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  20213. {
  20214. basic_json&& key = init.begin()->moved_or_copied();
  20215. push_back(typename object_t::value_type(
  20216. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  20217. }
  20218. else
  20219. {
  20220. push_back(basic_json(init));
  20221. }
  20222. }
  20223. /// @brief add an object to an object
  20224. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20225. reference operator+=(initializer_list_t init)
  20226. {
  20227. push_back(init);
  20228. return *this;
  20229. }
  20230. /// @brief add an object to an array
  20231. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  20232. template<class... Args>
  20233. reference emplace_back(Args&& ... args)
  20234. {
  20235. // emplace_back only works for null objects or arrays
  20236. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  20237. {
  20238. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  20239. }
  20240. // transform null object into an array
  20241. if (is_null())
  20242. {
  20243. m_data.m_type = value_t::array;
  20244. m_data.m_value = value_t::array;
  20245. assert_invariant();
  20246. }
  20247. // add element to array (perfect forwarding)
  20248. const auto old_capacity = m_data.m_value.array->capacity();
  20249. m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
  20250. return set_parent(m_data.m_value.array->back(), old_capacity);
  20251. }
  20252. /// @brief add an object to an object if key does not exist
  20253. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  20254. template<class... Args>
  20255. std::pair<iterator, bool> emplace(Args&& ... args)
  20256. {
  20257. // emplace only works for null objects or arrays
  20258. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  20259. {
  20260. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  20261. }
  20262. // transform null object into an object
  20263. if (is_null())
  20264. {
  20265. m_data.m_type = value_t::object;
  20266. m_data.m_value = value_t::object;
  20267. assert_invariant();
  20268. }
  20269. // add element to array (perfect forwarding)
  20270. auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
  20271. set_parent(res.first->second);
  20272. // create result iterator and set iterator to the result of emplace
  20273. auto it = begin();
  20274. it.m_it.object_iterator = res.first;
  20275. // return pair of iterator and boolean
  20276. return {it, res.second};
  20277. }
  20278. /// Helper for insertion of an iterator
  20279. /// @note: This uses std::distance to support GCC 4.8,
  20280. /// see https://github.com/nlohmann/json/pull/1257
  20281. template<typename... Args>
  20282. iterator insert_iterator(const_iterator pos, Args&& ... args) // NOLINT(performance-unnecessary-value-param)
  20283. {
  20284. iterator result(this);
  20285. JSON_ASSERT(m_data.m_value.array != nullptr);
  20286. auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
  20287. m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  20288. result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
  20289. // This could have been written as:
  20290. // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  20291. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  20292. set_parents();
  20293. return result;
  20294. }
  20295. /// @brief inserts element into array
  20296. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20297. iterator insert(const_iterator pos, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
  20298. {
  20299. // insert only works for arrays
  20300. if (JSON_HEDLEY_LIKELY(is_array()))
  20301. {
  20302. // check if iterator pos fits to this JSON value
  20303. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20304. {
  20305. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20306. }
  20307. // insert to array and return iterator
  20308. return insert_iterator(pos, val);
  20309. }
  20310. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20311. }
  20312. /// @brief inserts element into array
  20313. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20314. iterator insert(const_iterator pos, basic_json&& val) // NOLINT(performance-unnecessary-value-param)
  20315. {
  20316. return insert(pos, val);
  20317. }
  20318. /// @brief inserts copies of element into array
  20319. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20320. iterator insert(const_iterator pos, size_type cnt, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
  20321. {
  20322. // insert only works for arrays
  20323. if (JSON_HEDLEY_LIKELY(is_array()))
  20324. {
  20325. // check if iterator pos fits to this JSON value
  20326. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20327. {
  20328. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20329. }
  20330. // insert to array and return iterator
  20331. return insert_iterator(pos, cnt, val);
  20332. }
  20333. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20334. }
  20335. /// @brief inserts range of elements into array
  20336. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20337. iterator insert(const_iterator pos, const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
  20338. {
  20339. // insert only works for arrays
  20340. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  20341. {
  20342. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20343. }
  20344. // check if iterator pos fits to this JSON value
  20345. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20346. {
  20347. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20348. }
  20349. // check if range iterators belong to the same JSON object
  20350. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  20351. {
  20352. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  20353. }
  20354. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  20355. {
  20356. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  20357. }
  20358. // insert to array and return iterator
  20359. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  20360. }
  20361. /// @brief inserts elements from initializer list into array
  20362. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20363. iterator insert(const_iterator pos, initializer_list_t ilist) // NOLINT(performance-unnecessary-value-param)
  20364. {
  20365. // insert only works for arrays
  20366. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  20367. {
  20368. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20369. }
  20370. // check if iterator pos fits to this JSON value
  20371. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20372. {
  20373. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20374. }
  20375. // insert to array and return iterator
  20376. return insert_iterator(pos, ilist.begin(), ilist.end());
  20377. }
  20378. /// @brief inserts range of elements into object
  20379. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20380. void insert(const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
  20381. {
  20382. // insert only works for objects
  20383. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  20384. {
  20385. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20386. }
  20387. // check if range iterators belong to the same JSON object
  20388. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  20389. {
  20390. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  20391. }
  20392. // passed iterators must belong to objects
  20393. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  20394. {
  20395. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  20396. }
  20397. m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  20398. set_parents();
  20399. }
  20400. /// @brief updates a JSON object from another object, overwriting existing keys
  20401. /// @sa https://json.nlohmann.me/api/basic_json/update/
  20402. void update(const_reference j, bool merge_objects = false)
  20403. {
  20404. update(j.begin(), j.end(), merge_objects);
  20405. }
  20406. /// @brief updates a JSON object from another object, overwriting existing keys
  20407. /// @sa https://json.nlohmann.me/api/basic_json/update/
  20408. void update(const_iterator first, const_iterator last, bool merge_objects = false) // NOLINT(performance-unnecessary-value-param)
  20409. {
  20410. // implicitly convert null value to an empty object
  20411. if (is_null())
  20412. {
  20413. m_data.m_type = value_t::object;
  20414. m_data.m_value.object = create<object_t>();
  20415. assert_invariant();
  20416. }
  20417. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  20418. {
  20419. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  20420. }
  20421. // check if range iterators belong to the same JSON object
  20422. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  20423. {
  20424. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  20425. }
  20426. // passed iterators must belong to objects
  20427. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  20428. {
  20429. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  20430. }
  20431. for (auto it = first; it != last; ++it)
  20432. {
  20433. if (merge_objects && it.value().is_object())
  20434. {
  20435. auto it2 = m_data.m_value.object->find(it.key());
  20436. if (it2 != m_data.m_value.object->end())
  20437. {
  20438. it2->second.update(it.value(), true);
  20439. continue;
  20440. }
  20441. }
  20442. m_data.m_value.object->operator[](it.key()) = it.value();
  20443. #if JSON_DIAGNOSTICS
  20444. m_data.m_value.object->operator[](it.key()).m_parent = this;
  20445. #endif
  20446. }
  20447. }
  20448. /// @brief exchanges the values
  20449. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20450. void swap(reference other) noexcept (
  20451. std::is_nothrow_move_constructible<value_t>::value&&
  20452. std::is_nothrow_move_assignable<value_t>::value&&
  20453. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20454. std::is_nothrow_move_assignable<json_value>::value
  20455. )
  20456. {
  20457. std::swap(m_data.m_type, other.m_data.m_type);
  20458. std::swap(m_data.m_value, other.m_data.m_value);
  20459. set_parents();
  20460. other.set_parents();
  20461. assert_invariant();
  20462. }
  20463. /// @brief exchanges the values
  20464. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20465. friend void swap(reference left, reference right) noexcept (
  20466. std::is_nothrow_move_constructible<value_t>::value&&
  20467. std::is_nothrow_move_assignable<value_t>::value&&
  20468. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20469. std::is_nothrow_move_assignable<json_value>::value
  20470. )
  20471. {
  20472. left.swap(right);
  20473. }
  20474. /// @brief exchanges the values
  20475. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20476. void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20477. {
  20478. // swap only works for arrays
  20479. if (JSON_HEDLEY_LIKELY(is_array()))
  20480. {
  20481. using std::swap;
  20482. swap(*(m_data.m_value.array), other);
  20483. }
  20484. else
  20485. {
  20486. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  20487. }
  20488. }
  20489. /// @brief exchanges the values
  20490. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20491. void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20492. {
  20493. // swap only works for objects
  20494. if (JSON_HEDLEY_LIKELY(is_object()))
  20495. {
  20496. using std::swap;
  20497. swap(*(m_data.m_value.object), other);
  20498. }
  20499. else
  20500. {
  20501. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  20502. }
  20503. }
  20504. /// @brief exchanges the values
  20505. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20506. void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20507. {
  20508. // swap only works for strings
  20509. if (JSON_HEDLEY_LIKELY(is_string()))
  20510. {
  20511. using std::swap;
  20512. swap(*(m_data.m_value.string), other);
  20513. }
  20514. else
  20515. {
  20516. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  20517. }
  20518. }
  20519. /// @brief exchanges the values
  20520. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20521. void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20522. {
  20523. // swap only works for strings
  20524. if (JSON_HEDLEY_LIKELY(is_binary()))
  20525. {
  20526. using std::swap;
  20527. swap(*(m_data.m_value.binary), other);
  20528. }
  20529. else
  20530. {
  20531. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  20532. }
  20533. }
  20534. /// @brief exchanges the values
  20535. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20536. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  20537. {
  20538. // swap only works for strings
  20539. if (JSON_HEDLEY_LIKELY(is_binary()))
  20540. {
  20541. using std::swap;
  20542. swap(*(m_data.m_value.binary), other);
  20543. }
  20544. else
  20545. {
  20546. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  20547. }
  20548. }
  20549. /// @}
  20550. //////////////////////////////////////////
  20551. // lexicographical comparison operators //
  20552. //////////////////////////////////////////
  20553. /// @name lexicographical comparison operators
  20554. /// @{
  20555. // note parentheses around operands are necessary; see
  20556. // https://github.com/nlohmann/json/issues/1530
  20557. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  20558. const auto lhs_type = lhs.type(); \
  20559. const auto rhs_type = rhs.type(); \
  20560. \
  20561. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  20562. { \
  20563. switch (lhs_type) \
  20564. { \
  20565. case value_t::array: \
  20566. return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
  20567. \
  20568. case value_t::object: \
  20569. return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
  20570. \
  20571. case value_t::null: \
  20572. return (null_result); \
  20573. \
  20574. case value_t::string: \
  20575. return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
  20576. \
  20577. case value_t::boolean: \
  20578. return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
  20579. \
  20580. case value_t::number_integer: \
  20581. return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
  20582. \
  20583. case value_t::number_unsigned: \
  20584. return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
  20585. \
  20586. case value_t::number_float: \
  20587. return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
  20588. \
  20589. case value_t::binary: \
  20590. return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
  20591. \
  20592. case value_t::discarded: \
  20593. default: \
  20594. return (unordered_result); \
  20595. } \
  20596. } \
  20597. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  20598. { \
  20599. return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
  20600. } \
  20601. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  20602. { \
  20603. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
  20604. } \
  20605. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  20606. { \
  20607. return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
  20608. } \
  20609. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  20610. { \
  20611. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
  20612. } \
  20613. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  20614. { \
  20615. return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
  20616. } \
  20617. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  20618. { \
  20619. return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
  20620. } \
  20621. else if(compares_unordered(lhs, rhs))\
  20622. {\
  20623. return (unordered_result);\
  20624. }\
  20625. \
  20626. return (default_result);
  20627. JSON_PRIVATE_UNLESS_TESTED:
  20628. // returns true if:
  20629. // - any operand is NaN and the other operand is of number type
  20630. // - any operand is discarded
  20631. // in legacy mode, discarded values are considered ordered if
  20632. // an operation is computed as an odd number of inverses of others
  20633. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  20634. {
  20635. if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
  20636. || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
  20637. {
  20638. return true;
  20639. }
  20640. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20641. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  20642. #else
  20643. static_cast<void>(inverse);
  20644. return lhs.is_discarded() || rhs.is_discarded();
  20645. #endif
  20646. }
  20647. private:
  20648. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  20649. {
  20650. return compares_unordered(*this, rhs, inverse);
  20651. }
  20652. public:
  20653. #if JSON_HAS_THREE_WAY_COMPARISON
  20654. /// @brief comparison: equal
  20655. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20656. bool operator==(const_reference rhs) const noexcept
  20657. {
  20658. #ifdef __GNUC__
  20659. #pragma GCC diagnostic push
  20660. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20661. #endif
  20662. const_reference lhs = *this;
  20663. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20664. #ifdef __GNUC__
  20665. #pragma GCC diagnostic pop
  20666. #endif
  20667. }
  20668. /// @brief comparison: equal
  20669. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20670. template<typename ScalarType>
  20671. requires std::is_scalar_v<ScalarType>
  20672. bool operator==(ScalarType rhs) const noexcept
  20673. {
  20674. return *this == basic_json(rhs);
  20675. }
  20676. /// @brief comparison: not equal
  20677. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20678. bool operator!=(const_reference rhs) const noexcept
  20679. {
  20680. if (compares_unordered(rhs, true))
  20681. {
  20682. return false;
  20683. }
  20684. return !operator==(rhs);
  20685. }
  20686. /// @brief comparison: 3-way
  20687. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20688. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  20689. {
  20690. const_reference lhs = *this;
  20691. // default_result is used if we cannot compare values. In that case,
  20692. // we compare types.
  20693. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  20694. std::partial_ordering::equivalent,
  20695. std::partial_ordering::unordered,
  20696. lhs_type <=> rhs_type) // *NOPAD*
  20697. }
  20698. /// @brief comparison: 3-way
  20699. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20700. template<typename ScalarType>
  20701. requires std::is_scalar_v<ScalarType>
  20702. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  20703. {
  20704. return *this <=> basic_json(rhs); // *NOPAD*
  20705. }
  20706. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20707. // all operators that are computed as an odd number of inverses of others
  20708. // need to be overloaded to emulate the legacy comparison behavior
  20709. /// @brief comparison: less than or equal
  20710. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20711. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20712. bool operator<=(const_reference rhs) const noexcept
  20713. {
  20714. if (compares_unordered(rhs, true))
  20715. {
  20716. return false;
  20717. }
  20718. return !(rhs < *this);
  20719. }
  20720. /// @brief comparison: less than or equal
  20721. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20722. template<typename ScalarType>
  20723. requires std::is_scalar_v<ScalarType>
  20724. bool operator<=(ScalarType rhs) const noexcept
  20725. {
  20726. return *this <= basic_json(rhs);
  20727. }
  20728. /// @brief comparison: greater than or equal
  20729. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20730. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20731. bool operator>=(const_reference rhs) const noexcept
  20732. {
  20733. if (compares_unordered(rhs, true))
  20734. {
  20735. return false;
  20736. }
  20737. return !(*this < rhs);
  20738. }
  20739. /// @brief comparison: greater than or equal
  20740. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20741. template<typename ScalarType>
  20742. requires std::is_scalar_v<ScalarType>
  20743. bool operator>=(ScalarType rhs) const noexcept
  20744. {
  20745. return *this >= basic_json(rhs);
  20746. }
  20747. #endif
  20748. #else
  20749. /// @brief comparison: equal
  20750. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20751. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20752. {
  20753. #ifdef __GNUC__
  20754. #pragma GCC diagnostic push
  20755. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20756. #endif
  20757. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20758. #ifdef __GNUC__
  20759. #pragma GCC diagnostic pop
  20760. #endif
  20761. }
  20762. /// @brief comparison: equal
  20763. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20764. template<typename ScalarType, typename std::enable_if<
  20765. std::is_scalar<ScalarType>::value, int>::type = 0>
  20766. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20767. {
  20768. return lhs == basic_json(rhs);
  20769. }
  20770. /// @brief comparison: equal
  20771. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20772. template<typename ScalarType, typename std::enable_if<
  20773. std::is_scalar<ScalarType>::value, int>::type = 0>
  20774. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20775. {
  20776. return basic_json(lhs) == rhs;
  20777. }
  20778. /// @brief comparison: not equal
  20779. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20780. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20781. {
  20782. if (compares_unordered(lhs, rhs, true))
  20783. {
  20784. return false;
  20785. }
  20786. return !(lhs == rhs);
  20787. }
  20788. /// @brief comparison: not equal
  20789. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20790. template<typename ScalarType, typename std::enable_if<
  20791. std::is_scalar<ScalarType>::value, int>::type = 0>
  20792. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20793. {
  20794. return lhs != basic_json(rhs);
  20795. }
  20796. /// @brief comparison: not equal
  20797. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20798. template<typename ScalarType, typename std::enable_if<
  20799. std::is_scalar<ScalarType>::value, int>::type = 0>
  20800. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20801. {
  20802. return basic_json(lhs) != rhs;
  20803. }
  20804. /// @brief comparison: less than
  20805. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20806. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20807. {
  20808. // default_result is used if we cannot compare values. In that case,
  20809. // we compare types. Note we have to call the operator explicitly,
  20810. // because MSVC has problems otherwise.
  20811. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20812. }
  20813. /// @brief comparison: less than
  20814. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20815. template<typename ScalarType, typename std::enable_if<
  20816. std::is_scalar<ScalarType>::value, int>::type = 0>
  20817. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20818. {
  20819. return lhs < basic_json(rhs);
  20820. }
  20821. /// @brief comparison: less than
  20822. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20823. template<typename ScalarType, typename std::enable_if<
  20824. std::is_scalar<ScalarType>::value, int>::type = 0>
  20825. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20826. {
  20827. return basic_json(lhs) < rhs;
  20828. }
  20829. /// @brief comparison: less than or equal
  20830. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20831. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20832. {
  20833. if (compares_unordered(lhs, rhs, true))
  20834. {
  20835. return false;
  20836. }
  20837. return !(rhs < lhs);
  20838. }
  20839. /// @brief comparison: less than or equal
  20840. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20841. template<typename ScalarType, typename std::enable_if<
  20842. std::is_scalar<ScalarType>::value, int>::type = 0>
  20843. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20844. {
  20845. return lhs <= basic_json(rhs);
  20846. }
  20847. /// @brief comparison: less than or equal
  20848. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20849. template<typename ScalarType, typename std::enable_if<
  20850. std::is_scalar<ScalarType>::value, int>::type = 0>
  20851. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20852. {
  20853. return basic_json(lhs) <= rhs;
  20854. }
  20855. /// @brief comparison: greater than
  20856. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20857. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20858. {
  20859. // double inverse
  20860. if (compares_unordered(lhs, rhs))
  20861. {
  20862. return false;
  20863. }
  20864. return !(lhs <= rhs);
  20865. }
  20866. /// @brief comparison: greater than
  20867. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20868. template<typename ScalarType, typename std::enable_if<
  20869. std::is_scalar<ScalarType>::value, int>::type = 0>
  20870. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  20871. {
  20872. return lhs > basic_json(rhs);
  20873. }
  20874. /// @brief comparison: greater than
  20875. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20876. template<typename ScalarType, typename std::enable_if<
  20877. std::is_scalar<ScalarType>::value, int>::type = 0>
  20878. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  20879. {
  20880. return basic_json(lhs) > rhs;
  20881. }
  20882. /// @brief comparison: greater than or equal
  20883. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20884. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  20885. {
  20886. if (compares_unordered(lhs, rhs, true))
  20887. {
  20888. return false;
  20889. }
  20890. return !(lhs < rhs);
  20891. }
  20892. /// @brief comparison: greater than or equal
  20893. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20894. template<typename ScalarType, typename std::enable_if<
  20895. std::is_scalar<ScalarType>::value, int>::type = 0>
  20896. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  20897. {
  20898. return lhs >= basic_json(rhs);
  20899. }
  20900. /// @brief comparison: greater than or equal
  20901. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20902. template<typename ScalarType, typename std::enable_if<
  20903. std::is_scalar<ScalarType>::value, int>::type = 0>
  20904. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  20905. {
  20906. return basic_json(lhs) >= rhs;
  20907. }
  20908. #endif
  20909. #undef JSON_IMPLEMENT_OPERATOR
  20910. /// @}
  20911. ///////////////////
  20912. // serialization //
  20913. ///////////////////
  20914. /// @name serialization
  20915. /// @{
  20916. #ifndef JSON_NO_IO
  20917. /// @brief serialize to stream
  20918. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20919. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20920. {
  20921. // read width member and use it as indentation parameter if nonzero
  20922. const bool pretty_print = o.width() > 0;
  20923. const auto indentation = pretty_print ? o.width() : 0;
  20924. // reset width to 0 for subsequent calls to this stream
  20925. o.width(0);
  20926. // do the actual serialization
  20927. serializer s(detail::output_adapter<char>(o), o.fill());
  20928. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20929. return o;
  20930. }
  20931. /// @brief serialize to stream
  20932. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20933. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  20934. /// version 4.0.0 of the library. Please use
  20935. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  20936. /// replace calls like `j >> o;` with `o << j;`.
  20937. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20938. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20939. {
  20940. return o << j;
  20941. }
  20942. #endif // JSON_NO_IO
  20943. /// @}
  20944. /////////////////////
  20945. // deserialization //
  20946. /////////////////////
  20947. /// @name deserialization
  20948. /// @{
  20949. /// @brief deserialize from a compatible input
  20950. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20951. template<typename InputType>
  20952. JSON_HEDLEY_WARN_UNUSED_RESULT
  20953. static basic_json parse(InputType&& i,
  20954. parser_callback_t cb = nullptr,
  20955. const bool allow_exceptions = true,
  20956. const bool ignore_comments = false)
  20957. {
  20958. basic_json result;
  20959. parser(detail::input_adapter(std::forward<InputType>(i)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved,accessForwarded]
  20960. return result;
  20961. }
  20962. /// @brief deserialize from a pair of character iterators
  20963. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20964. template<typename IteratorType>
  20965. JSON_HEDLEY_WARN_UNUSED_RESULT
  20966. static basic_json parse(IteratorType first,
  20967. IteratorType last,
  20968. parser_callback_t cb = nullptr,
  20969. const bool allow_exceptions = true,
  20970. const bool ignore_comments = false)
  20971. {
  20972. basic_json result;
  20973. parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
  20974. return result;
  20975. }
  20976. JSON_HEDLEY_WARN_UNUSED_RESULT
  20977. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20978. static basic_json parse(detail::span_input_adapter&& i,
  20979. parser_callback_t cb = nullptr,
  20980. const bool allow_exceptions = true,
  20981. const bool ignore_comments = false)
  20982. {
  20983. basic_json result;
  20984. parser(i.get(), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
  20985. return result;
  20986. }
  20987. /// @brief check if the input is valid JSON
  20988. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20989. template<typename InputType>
  20990. static bool accept(InputType&& i,
  20991. const bool ignore_comments = false)
  20992. {
  20993. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20994. }
  20995. /// @brief check if the input is valid JSON
  20996. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20997. template<typename IteratorType>
  20998. static bool accept(IteratorType first, IteratorType last,
  20999. const bool ignore_comments = false)
  21000. {
  21001. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  21002. }
  21003. JSON_HEDLEY_WARN_UNUSED_RESULT
  21004. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  21005. static bool accept(detail::span_input_adapter&& i,
  21006. const bool ignore_comments = false)
  21007. {
  21008. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  21009. }
  21010. /// @brief generate SAX events
  21011. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  21012. template <typename InputType, typename SAX>
  21013. JSON_HEDLEY_NON_NULL(2)
  21014. static bool sax_parse(InputType&& i, SAX* sax,
  21015. input_format_t format = input_format_t::json,
  21016. const bool strict = true,
  21017. const bool ignore_comments = false)
  21018. {
  21019. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21020. return format == input_format_t::json
  21021. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  21022. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  21023. }
  21024. /// @brief generate SAX events
  21025. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  21026. template<class IteratorType, class SAX>
  21027. JSON_HEDLEY_NON_NULL(3)
  21028. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  21029. input_format_t format = input_format_t::json,
  21030. const bool strict = true,
  21031. const bool ignore_comments = false)
  21032. {
  21033. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21034. return format == input_format_t::json
  21035. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  21036. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  21037. }
  21038. /// @brief generate SAX events
  21039. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  21040. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  21041. /// version 4.0.0 of the library. Please use
  21042. /// sax_parse(ptr, ptr + len) instead.
  21043. template <typename SAX>
  21044. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  21045. JSON_HEDLEY_NON_NULL(2)
  21046. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  21047. input_format_t format = input_format_t::json,
  21048. const bool strict = true,
  21049. const bool ignore_comments = false)
  21050. {
  21051. auto ia = i.get();
  21052. return format == input_format_t::json
  21053. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21054. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  21055. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21056. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  21057. }
  21058. #ifndef JSON_NO_IO
  21059. /// @brief deserialize from stream
  21060. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  21061. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  21062. /// version 4.0.0 of the library. Please use
  21063. /// operator>>(std::istream&, basic_json&) instead; that is,
  21064. /// replace calls like `j << i;` with `i >> j;`.
  21065. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  21066. friend std::istream& operator<<(basic_json& j, std::istream& i)
  21067. {
  21068. return operator>>(i, j);
  21069. }
  21070. /// @brief deserialize from stream
  21071. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  21072. friend std::istream& operator>>(std::istream& i, basic_json& j)
  21073. {
  21074. parser(detail::input_adapter(i)).parse(false, j);
  21075. return i;
  21076. }
  21077. #endif // JSON_NO_IO
  21078. /// @}
  21079. ///////////////////////////
  21080. // convenience functions //
  21081. ///////////////////////////
  21082. /// @brief return the type as string
  21083. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  21084. JSON_HEDLEY_RETURNS_NON_NULL
  21085. const char* type_name() const noexcept
  21086. {
  21087. switch (m_data.m_type)
  21088. {
  21089. case value_t::null:
  21090. return "null";
  21091. case value_t::object:
  21092. return "object";
  21093. case value_t::array:
  21094. return "array";
  21095. case value_t::string:
  21096. return "string";
  21097. case value_t::boolean:
  21098. return "boolean";
  21099. case value_t::binary:
  21100. return "binary";
  21101. case value_t::discarded:
  21102. return "discarded";
  21103. case value_t::number_integer:
  21104. case value_t::number_unsigned:
  21105. case value_t::number_float:
  21106. default:
  21107. return "number";
  21108. }
  21109. }
  21110. JSON_PRIVATE_UNLESS_TESTED:
  21111. //////////////////////
  21112. // member variables //
  21113. //////////////////////
  21114. struct data
  21115. {
  21116. /// the type of the current element
  21117. value_t m_type = value_t::null;
  21118. /// the value of the current element
  21119. json_value m_value = {};
  21120. data(const value_t v)
  21121. : m_type(v), m_value(v)
  21122. {
  21123. }
  21124. data(size_type cnt, const basic_json& val)
  21125. : m_type(value_t::array)
  21126. {
  21127. m_value.array = create<array_t>(cnt, val);
  21128. }
  21129. data() noexcept = default;
  21130. data(data&&) noexcept = default;
  21131. data(const data&) noexcept = delete;
  21132. data& operator=(data&&) noexcept = delete;
  21133. data& operator=(const data&) noexcept = delete;
  21134. ~data() noexcept
  21135. {
  21136. m_value.destroy(m_type);
  21137. }
  21138. };
  21139. data m_data = {};
  21140. #if JSON_DIAGNOSTICS
  21141. /// a pointer to a parent value (for debugging purposes)
  21142. basic_json* m_parent = nullptr;
  21143. #endif
  21144. #if JSON_DIAGNOSTIC_POSITIONS
  21145. /// the start position of the value
  21146. std::size_t start_position = std::string::npos;
  21147. /// the end position of the value
  21148. std::size_t end_position = std::string::npos;
  21149. public:
  21150. constexpr std::size_t start_pos() const noexcept
  21151. {
  21152. return start_position;
  21153. }
  21154. constexpr std::size_t end_pos() const noexcept
  21155. {
  21156. return end_position;
  21157. }
  21158. #endif
  21159. //////////////////////////////////////////
  21160. // binary serialization/deserialization //
  21161. //////////////////////////////////////////
  21162. /// @name binary serialization/deserialization support
  21163. /// @{
  21164. public:
  21165. /// @brief create a CBOR serialization of a given JSON value
  21166. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  21167. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  21168. {
  21169. std::vector<std::uint8_t> result;
  21170. to_cbor(j, result);
  21171. return result;
  21172. }
  21173. /// @brief create a CBOR serialization of a given JSON value
  21174. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  21175. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21176. {
  21177. binary_writer<std::uint8_t>(o).write_cbor(j);
  21178. }
  21179. /// @brief create a CBOR serialization of a given JSON value
  21180. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  21181. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  21182. {
  21183. binary_writer<char>(o).write_cbor(j);
  21184. }
  21185. /// @brief create a MessagePack serialization of a given JSON value
  21186. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  21187. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  21188. {
  21189. std::vector<std::uint8_t> result;
  21190. to_msgpack(j, result);
  21191. return result;
  21192. }
  21193. /// @brief create a MessagePack serialization of a given JSON value
  21194. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  21195. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21196. {
  21197. binary_writer<std::uint8_t>(o).write_msgpack(j);
  21198. }
  21199. /// @brief create a MessagePack serialization of a given JSON value
  21200. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  21201. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  21202. {
  21203. binary_writer<char>(o).write_msgpack(j);
  21204. }
  21205. /// @brief create a UBJSON serialization of a given JSON value
  21206. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  21207. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  21208. const bool use_size = false,
  21209. const bool use_type = false)
  21210. {
  21211. std::vector<std::uint8_t> result;
  21212. to_ubjson(j, result, use_size, use_type);
  21213. return result;
  21214. }
  21215. /// @brief create a UBJSON serialization of a given JSON value
  21216. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  21217. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  21218. const bool use_size = false, const bool use_type = false)
  21219. {
  21220. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  21221. }
  21222. /// @brief create a UBJSON serialization of a given JSON value
  21223. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  21224. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  21225. const bool use_size = false, const bool use_type = false)
  21226. {
  21227. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  21228. }
  21229. /// @brief create a BJData serialization of a given JSON value
  21230. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  21231. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  21232. const bool use_size = false,
  21233. const bool use_type = false,
  21234. const bjdata_version_t version = bjdata_version_t::draft2)
  21235. {
  21236. std::vector<std::uint8_t> result;
  21237. to_bjdata(j, result, use_size, use_type, version);
  21238. return result;
  21239. }
  21240. /// @brief create a BJData serialization of a given JSON value
  21241. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  21242. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  21243. const bool use_size = false, const bool use_type = false,
  21244. const bjdata_version_t version = bjdata_version_t::draft2)
  21245. {
  21246. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true, version);
  21247. }
  21248. /// @brief create a BJData serialization of a given JSON value
  21249. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  21250. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  21251. const bool use_size = false, const bool use_type = false,
  21252. const bjdata_version_t version = bjdata_version_t::draft2)
  21253. {
  21254. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true, version);
  21255. }
  21256. /// @brief create a BSON serialization of a given JSON value
  21257. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  21258. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  21259. {
  21260. std::vector<std::uint8_t> result;
  21261. to_bson(j, result);
  21262. return result;
  21263. }
  21264. /// @brief create a BSON serialization of a given JSON value
  21265. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  21266. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21267. {
  21268. binary_writer<std::uint8_t>(o).write_bson(j);
  21269. }
  21270. /// @brief create a BSON serialization of a given JSON value
  21271. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  21272. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  21273. {
  21274. binary_writer<char>(o).write_bson(j);
  21275. }
  21276. /// @brief create a JSON value from an input in CBOR format
  21277. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  21278. template<typename InputType>
  21279. JSON_HEDLEY_WARN_UNUSED_RESULT
  21280. static basic_json from_cbor(InputType&& i,
  21281. const bool strict = true,
  21282. const bool allow_exceptions = true,
  21283. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21284. {
  21285. basic_json result;
  21286. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21287. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21288. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
  21289. return res ? result : basic_json(value_t::discarded);
  21290. }
  21291. /// @brief create a JSON value from an input in CBOR format
  21292. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  21293. template<typename IteratorType>
  21294. JSON_HEDLEY_WARN_UNUSED_RESULT
  21295. static basic_json from_cbor(IteratorType first, IteratorType last,
  21296. const bool strict = true,
  21297. const bool allow_exceptions = true,
  21298. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21299. {
  21300. basic_json result;
  21301. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21302. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21303. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
  21304. return res ? result : basic_json(value_t::discarded);
  21305. }
  21306. template<typename T>
  21307. JSON_HEDLEY_WARN_UNUSED_RESULT
  21308. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  21309. static basic_json from_cbor(const T* ptr, std::size_t len,
  21310. const bool strict = true,
  21311. const bool allow_exceptions = true,
  21312. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21313. {
  21314. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  21315. }
  21316. JSON_HEDLEY_WARN_UNUSED_RESULT
  21317. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  21318. static basic_json from_cbor(detail::span_input_adapter&& i,
  21319. const bool strict = true,
  21320. const bool allow_exceptions = true,
  21321. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21322. {
  21323. basic_json result;
  21324. auto ia = i.get();
  21325. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21326. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21327. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
  21328. return res ? result : basic_json(value_t::discarded);
  21329. }
  21330. /// @brief create a JSON value from an input in MessagePack format
  21331. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  21332. template<typename InputType>
  21333. JSON_HEDLEY_WARN_UNUSED_RESULT
  21334. static basic_json from_msgpack(InputType&& i,
  21335. const bool strict = true,
  21336. const bool allow_exceptions = true)
  21337. {
  21338. basic_json result;
  21339. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21340. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21341. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
  21342. return res ? result : basic_json(value_t::discarded);
  21343. }
  21344. /// @brief create a JSON value from an input in MessagePack format
  21345. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  21346. template<typename IteratorType>
  21347. JSON_HEDLEY_WARN_UNUSED_RESULT
  21348. static basic_json from_msgpack(IteratorType first, IteratorType last,
  21349. const bool strict = true,
  21350. const bool allow_exceptions = true)
  21351. {
  21352. basic_json result;
  21353. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21354. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21355. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
  21356. return res ? result : basic_json(value_t::discarded);
  21357. }
  21358. template<typename T>
  21359. JSON_HEDLEY_WARN_UNUSED_RESULT
  21360. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  21361. static basic_json from_msgpack(const T* ptr, std::size_t len,
  21362. const bool strict = true,
  21363. const bool allow_exceptions = true)
  21364. {
  21365. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  21366. }
  21367. JSON_HEDLEY_WARN_UNUSED_RESULT
  21368. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  21369. static basic_json from_msgpack(detail::span_input_adapter&& i,
  21370. const bool strict = true,
  21371. const bool allow_exceptions = true)
  21372. {
  21373. basic_json result;
  21374. auto ia = i.get();
  21375. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21376. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21377. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
  21378. return res ? result : basic_json(value_t::discarded);
  21379. }
  21380. /// @brief create a JSON value from an input in UBJSON format
  21381. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  21382. template<typename InputType>
  21383. JSON_HEDLEY_WARN_UNUSED_RESULT
  21384. static basic_json from_ubjson(InputType&& i,
  21385. const bool strict = true,
  21386. const bool allow_exceptions = true)
  21387. {
  21388. basic_json result;
  21389. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21390. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21391. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
  21392. return res ? result : basic_json(value_t::discarded);
  21393. }
  21394. /// @brief create a JSON value from an input in UBJSON format
  21395. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  21396. template<typename IteratorType>
  21397. JSON_HEDLEY_WARN_UNUSED_RESULT
  21398. static basic_json from_ubjson(IteratorType first, IteratorType last,
  21399. const bool strict = true,
  21400. const bool allow_exceptions = true)
  21401. {
  21402. basic_json result;
  21403. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21404. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21405. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
  21406. return res ? result : basic_json(value_t::discarded);
  21407. }
  21408. template<typename T>
  21409. JSON_HEDLEY_WARN_UNUSED_RESULT
  21410. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  21411. static basic_json from_ubjson(const T* ptr, std::size_t len,
  21412. const bool strict = true,
  21413. const bool allow_exceptions = true)
  21414. {
  21415. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  21416. }
  21417. JSON_HEDLEY_WARN_UNUSED_RESULT
  21418. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  21419. static basic_json from_ubjson(detail::span_input_adapter&& i,
  21420. const bool strict = true,
  21421. const bool allow_exceptions = true)
  21422. {
  21423. basic_json result;
  21424. auto ia = i.get();
  21425. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21426. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21427. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
  21428. return res ? result : basic_json(value_t::discarded);
  21429. }
  21430. /// @brief create a JSON value from an input in BJData format
  21431. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  21432. template<typename InputType>
  21433. JSON_HEDLEY_WARN_UNUSED_RESULT
  21434. static basic_json from_bjdata(InputType&& i,
  21435. const bool strict = true,
  21436. const bool allow_exceptions = true)
  21437. {
  21438. basic_json result;
  21439. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21440. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21441. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
  21442. return res ? result : basic_json(value_t::discarded);
  21443. }
  21444. /// @brief create a JSON value from an input in BJData format
  21445. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  21446. template<typename IteratorType>
  21447. JSON_HEDLEY_WARN_UNUSED_RESULT
  21448. static basic_json from_bjdata(IteratorType first, IteratorType last,
  21449. const bool strict = true,
  21450. const bool allow_exceptions = true)
  21451. {
  21452. basic_json result;
  21453. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21454. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21455. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
  21456. return res ? result : basic_json(value_t::discarded);
  21457. }
  21458. /// @brief create a JSON value from an input in BSON format
  21459. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  21460. template<typename InputType>
  21461. JSON_HEDLEY_WARN_UNUSED_RESULT
  21462. static basic_json from_bson(InputType&& i,
  21463. const bool strict = true,
  21464. const bool allow_exceptions = true)
  21465. {
  21466. basic_json result;
  21467. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21468. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21469. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
  21470. return res ? result : basic_json(value_t::discarded);
  21471. }
  21472. /// @brief create a JSON value from an input in BSON format
  21473. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  21474. template<typename IteratorType>
  21475. JSON_HEDLEY_WARN_UNUSED_RESULT
  21476. static basic_json from_bson(IteratorType first, IteratorType last,
  21477. const bool strict = true,
  21478. const bool allow_exceptions = true)
  21479. {
  21480. basic_json result;
  21481. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21482. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21483. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
  21484. return res ? result : basic_json(value_t::discarded);
  21485. }
  21486. template<typename T>
  21487. JSON_HEDLEY_WARN_UNUSED_RESULT
  21488. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  21489. static basic_json from_bson(const T* ptr, std::size_t len,
  21490. const bool strict = true,
  21491. const bool allow_exceptions = true)
  21492. {
  21493. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  21494. }
  21495. JSON_HEDLEY_WARN_UNUSED_RESULT
  21496. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  21497. static basic_json from_bson(detail::span_input_adapter&& i,
  21498. const bool strict = true,
  21499. const bool allow_exceptions = true)
  21500. {
  21501. basic_json result;
  21502. auto ia = i.get();
  21503. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21504. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21505. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
  21506. return res ? result : basic_json(value_t::discarded);
  21507. }
  21508. /// @}
  21509. //////////////////////////
  21510. // JSON Pointer support //
  21511. //////////////////////////
  21512. /// @name JSON Pointer functions
  21513. /// @{
  21514. /// @brief access specified element via JSON Pointer
  21515. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  21516. reference operator[](const json_pointer& ptr)
  21517. {
  21518. return ptr.get_unchecked(this);
  21519. }
  21520. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21521. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21522. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  21523. {
  21524. return ptr.get_unchecked(this);
  21525. }
  21526. /// @brief access specified element via JSON Pointer
  21527. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  21528. const_reference operator[](const json_pointer& ptr) const
  21529. {
  21530. return ptr.get_unchecked(this);
  21531. }
  21532. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21533. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21534. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  21535. {
  21536. return ptr.get_unchecked(this);
  21537. }
  21538. /// @brief access specified element via JSON Pointer
  21539. /// @sa https://json.nlohmann.me/api/basic_json/at/
  21540. reference at(const json_pointer& ptr)
  21541. {
  21542. return ptr.get_checked(this);
  21543. }
  21544. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21545. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21546. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  21547. {
  21548. return ptr.get_checked(this);
  21549. }
  21550. /// @brief access specified element via JSON Pointer
  21551. /// @sa https://json.nlohmann.me/api/basic_json/at/
  21552. const_reference at(const json_pointer& ptr) const
  21553. {
  21554. return ptr.get_checked(this);
  21555. }
  21556. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21557. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21558. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  21559. {
  21560. return ptr.get_checked(this);
  21561. }
  21562. /// @brief return flattened JSON value
  21563. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  21564. basic_json flatten() const
  21565. {
  21566. basic_json result(value_t::object);
  21567. json_pointer::flatten("", *this, result);
  21568. return result;
  21569. }
  21570. /// @brief unflatten a previously flattened JSON value
  21571. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  21572. basic_json unflatten() const
  21573. {
  21574. return json_pointer::unflatten(*this);
  21575. }
  21576. /// @}
  21577. //////////////////////////
  21578. // JSON Patch functions //
  21579. //////////////////////////
  21580. /// @name JSON Patch functions
  21581. /// @{
  21582. /// @brief applies a JSON patch in-place without copying the object
  21583. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21584. void patch_inplace(const basic_json& json_patch)
  21585. {
  21586. basic_json& result = *this;
  21587. // the valid JSON Patch operations
  21588. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  21589. const auto get_op = [](const string_t& op)
  21590. {
  21591. if (op == "add")
  21592. {
  21593. return patch_operations::add;
  21594. }
  21595. if (op == "remove")
  21596. {
  21597. return patch_operations::remove;
  21598. }
  21599. if (op == "replace")
  21600. {
  21601. return patch_operations::replace;
  21602. }
  21603. if (op == "move")
  21604. {
  21605. return patch_operations::move;
  21606. }
  21607. if (op == "copy")
  21608. {
  21609. return patch_operations::copy;
  21610. }
  21611. if (op == "test")
  21612. {
  21613. return patch_operations::test;
  21614. }
  21615. return patch_operations::invalid;
  21616. };
  21617. // wrapper for "add" operation; add value at ptr
  21618. const auto operation_add = [&result](json_pointer & ptr, const basic_json & val)
  21619. {
  21620. // adding to the root of the target document means replacing it
  21621. if (ptr.empty())
  21622. {
  21623. result = val;
  21624. return;
  21625. }
  21626. // make sure the top element of the pointer exists
  21627. json_pointer const top_pointer = ptr.top();
  21628. if (top_pointer != ptr)
  21629. {
  21630. result.at(top_pointer);
  21631. }
  21632. // get reference to parent of JSON pointer ptr
  21633. const auto last_path = ptr.back();
  21634. ptr.pop_back();
  21635. // parent must exist when performing patch add per RFC6902 specs
  21636. basic_json& parent = result.at(ptr);
  21637. switch (parent.m_data.m_type)
  21638. {
  21639. case value_t::null:
  21640. case value_t::object:
  21641. {
  21642. // use operator[] to add value
  21643. parent[last_path] = val;
  21644. break;
  21645. }
  21646. case value_t::array:
  21647. {
  21648. if (last_path == "-")
  21649. {
  21650. // special case: append to back
  21651. parent.push_back(val);
  21652. }
  21653. else
  21654. {
  21655. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  21656. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  21657. {
  21658. // avoid undefined behavior
  21659. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  21660. }
  21661. // default case: insert add offset
  21662. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  21663. }
  21664. break;
  21665. }
  21666. // if there exists a parent it cannot be primitive
  21667. case value_t::string: // LCOV_EXCL_LINE
  21668. case value_t::boolean: // LCOV_EXCL_LINE
  21669. case value_t::number_integer: // LCOV_EXCL_LINE
  21670. case value_t::number_unsigned: // LCOV_EXCL_LINE
  21671. case value_t::number_float: // LCOV_EXCL_LINE
  21672. case value_t::binary: // LCOV_EXCL_LINE
  21673. case value_t::discarded: // LCOV_EXCL_LINE
  21674. default: // LCOV_EXCL_LINE
  21675. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  21676. }
  21677. };
  21678. // wrapper for "remove" operation; remove value at ptr
  21679. const auto operation_remove = [this, & result](json_pointer & ptr)
  21680. {
  21681. // get reference to parent of JSON pointer ptr
  21682. const auto last_path = ptr.back();
  21683. ptr.pop_back();
  21684. basic_json& parent = result.at(ptr);
  21685. // remove child
  21686. if (parent.is_object())
  21687. {
  21688. // perform range check
  21689. auto it = parent.find(last_path);
  21690. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  21691. {
  21692. parent.erase(it);
  21693. }
  21694. else
  21695. {
  21696. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  21697. }
  21698. }
  21699. else if (parent.is_array())
  21700. {
  21701. // note erase performs range check
  21702. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  21703. }
  21704. };
  21705. // type check: top level value must be an array
  21706. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  21707. {
  21708. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  21709. }
  21710. // iterate and apply the operations
  21711. for (const auto& val : json_patch)
  21712. {
  21713. // wrapper to get a value for an operation
  21714. const auto get_value = [&val](const string_t& op,
  21715. const string_t& member,
  21716. bool string_type) -> basic_json &
  21717. {
  21718. // find value
  21719. auto it = val.m_data.m_value.object->find(member);
  21720. // context-sensitive error message
  21721. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
  21722. // check if desired value is present
  21723. if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
  21724. {
  21725. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21726. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  21727. }
  21728. // check if result is of type string
  21729. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  21730. {
  21731. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21732. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  21733. }
  21734. // no error: return value
  21735. return it->second;
  21736. };
  21737. // type check: every element of the array must be an object
  21738. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  21739. {
  21740. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  21741. }
  21742. // collect mandatory members
  21743. const auto op = get_value("op", "op", true).template get<string_t>();
  21744. const auto path = get_value(op, "path", true).template get<string_t>();
  21745. json_pointer ptr(path);
  21746. switch (get_op(op))
  21747. {
  21748. case patch_operations::add:
  21749. {
  21750. operation_add(ptr, get_value("add", "value", false));
  21751. break;
  21752. }
  21753. case patch_operations::remove:
  21754. {
  21755. operation_remove(ptr);
  21756. break;
  21757. }
  21758. case patch_operations::replace:
  21759. {
  21760. // the "path" location must exist - use at()
  21761. result.at(ptr) = get_value("replace", "value", false);
  21762. break;
  21763. }
  21764. case patch_operations::move:
  21765. {
  21766. const auto from_path = get_value("move", "from", true).template get<string_t>();
  21767. json_pointer from_ptr(from_path);
  21768. // the "from" location must exist - use at()
  21769. basic_json const v = result.at(from_ptr);
  21770. // The move operation is functionally identical to a
  21771. // "remove" operation on the "from" location, followed
  21772. // immediately by an "add" operation at the target
  21773. // location with the value that was just removed.
  21774. operation_remove(from_ptr);
  21775. operation_add(ptr, v);
  21776. break;
  21777. }
  21778. case patch_operations::copy:
  21779. {
  21780. const auto from_path = get_value("copy", "from", true).template get<string_t>();
  21781. const json_pointer from_ptr(from_path);
  21782. // the "from" location must exist - use at()
  21783. basic_json const v = result.at(from_ptr);
  21784. // The copy is functionally identical to an "add"
  21785. // operation at the target location using the value
  21786. // specified in the "from" member.
  21787. operation_add(ptr, v);
  21788. break;
  21789. }
  21790. case patch_operations::test:
  21791. {
  21792. bool success = false;
  21793. JSON_TRY
  21794. {
  21795. // check if "value" matches the one at "path"
  21796. // the "path" location must exist - use at()
  21797. success = (result.at(ptr) == get_value("test", "value", false));
  21798. }
  21799. JSON_INTERNAL_CATCH (out_of_range&)
  21800. {
  21801. // ignore out of range errors: success remains false
  21802. }
  21803. // throw an exception if test fails
  21804. if (JSON_HEDLEY_UNLIKELY(!success))
  21805. {
  21806. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  21807. }
  21808. break;
  21809. }
  21810. case patch_operations::invalid:
  21811. default:
  21812. {
  21813. // op must be "add", "remove", "replace", "move", "copy", or
  21814. // "test"
  21815. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  21816. }
  21817. }
  21818. }
  21819. }
  21820. /// @brief applies a JSON patch to a copy of the current object
  21821. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21822. basic_json patch(const basic_json& json_patch) const
  21823. {
  21824. basic_json result = *this;
  21825. result.patch_inplace(json_patch);
  21826. return result;
  21827. }
  21828. /// @brief creates a diff as a JSON patch
  21829. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  21830. JSON_HEDLEY_WARN_UNUSED_RESULT
  21831. static basic_json diff(const basic_json& source, const basic_json& target,
  21832. const string_t& path = "")
  21833. {
  21834. // the patch
  21835. basic_json result(value_t::array);
  21836. // if the values are the same, return empty patch
  21837. if (source == target)
  21838. {
  21839. return result;
  21840. }
  21841. if (source.type() != target.type())
  21842. {
  21843. // different types: replace value
  21844. result.push_back(
  21845. {
  21846. {"op", "replace"}, {"path", path}, {"value", target}
  21847. });
  21848. return result;
  21849. }
  21850. switch (source.type())
  21851. {
  21852. case value_t::array:
  21853. {
  21854. // first pass: traverse common elements
  21855. std::size_t i = 0;
  21856. while (i < source.size() && i < target.size())
  21857. {
  21858. // recursive call to compare array values at index i
  21859. auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
  21860. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21861. ++i;
  21862. }
  21863. // We now reached the end of at least one array
  21864. // in a second pass, traverse the remaining elements
  21865. // remove my remaining elements
  21866. const auto end_index = static_cast<difference_type>(result.size());
  21867. while (i < source.size())
  21868. {
  21869. // add operations in reverse order to avoid invalid
  21870. // indices
  21871. result.insert(result.begin() + end_index, object(
  21872. {
  21873. {"op", "remove"},
  21874. {"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(i))}
  21875. }));
  21876. ++i;
  21877. }
  21878. // add other remaining elements
  21879. while (i < target.size())
  21880. {
  21881. result.push_back(
  21882. {
  21883. {"op", "add"},
  21884. {"path", detail::concat<string_t>(path, "/-")},
  21885. {"value", target[i]}
  21886. });
  21887. ++i;
  21888. }
  21889. break;
  21890. }
  21891. case value_t::object:
  21892. {
  21893. // first pass: traverse this object's elements
  21894. for (auto it = source.cbegin(); it != source.cend(); ++it)
  21895. {
  21896. // escape the key name to be used in a JSON patch
  21897. const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
  21898. if (target.find(it.key()) != target.end())
  21899. {
  21900. // recursive call to compare object values at key it
  21901. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  21902. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21903. }
  21904. else
  21905. {
  21906. // found a key that is not in o -> remove it
  21907. result.push_back(object(
  21908. {
  21909. {"op", "remove"}, {"path", path_key}
  21910. }));
  21911. }
  21912. }
  21913. // second pass: traverse other object's elements
  21914. for (auto it = target.cbegin(); it != target.cend(); ++it)
  21915. {
  21916. if (source.find(it.key()) == source.end())
  21917. {
  21918. // found a key that is not in this -> add it
  21919. const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
  21920. result.push_back(
  21921. {
  21922. {"op", "add"}, {"path", path_key},
  21923. {"value", it.value()}
  21924. });
  21925. }
  21926. }
  21927. break;
  21928. }
  21929. case value_t::null:
  21930. case value_t::string:
  21931. case value_t::boolean:
  21932. case value_t::number_integer:
  21933. case value_t::number_unsigned:
  21934. case value_t::number_float:
  21935. case value_t::binary:
  21936. case value_t::discarded:
  21937. default:
  21938. {
  21939. // both primitive type: replace value
  21940. result.push_back(
  21941. {
  21942. {"op", "replace"}, {"path", path}, {"value", target}
  21943. });
  21944. break;
  21945. }
  21946. }
  21947. return result;
  21948. }
  21949. /// @}
  21950. ////////////////////////////////
  21951. // JSON Merge Patch functions //
  21952. ////////////////////////////////
  21953. /// @name JSON Merge Patch functions
  21954. /// @{
  21955. /// @brief applies a JSON Merge Patch
  21956. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  21957. void merge_patch(const basic_json& apply_patch)
  21958. {
  21959. if (apply_patch.is_object())
  21960. {
  21961. if (!is_object())
  21962. {
  21963. *this = object();
  21964. }
  21965. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  21966. {
  21967. if (it.value().is_null())
  21968. {
  21969. erase(it.key());
  21970. }
  21971. else
  21972. {
  21973. operator[](it.key()).merge_patch(it.value());
  21974. }
  21975. }
  21976. }
  21977. else
  21978. {
  21979. *this = apply_patch;
  21980. }
  21981. }
  21982. /// @}
  21983. };
  21984. /// @brief user-defined to_string function for JSON values
  21985. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  21986. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21987. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  21988. {
  21989. return j.dump();
  21990. }
  21991. inline namespace literals
  21992. {
  21993. inline namespace json_literals
  21994. {
  21995. /// @brief user-defined string literal for JSON values
  21996. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  21997. JSON_HEDLEY_NON_NULL(1)
  21998. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21999. inline nlohmann::json operator ""_json(const char* s, std::size_t n)
  22000. #else
  22001. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  22002. #endif
  22003. {
  22004. return nlohmann::json::parse(s, s + n);
  22005. }
  22006. /// @brief user-defined string literal for JSON pointer
  22007. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  22008. JSON_HEDLEY_NON_NULL(1)
  22009. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  22010. inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
  22011. #else
  22012. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  22013. #endif
  22014. {
  22015. return nlohmann::json::json_pointer(std::string(s, n));
  22016. }
  22017. } // namespace json_literals
  22018. } // namespace literals
  22019. NLOHMANN_JSON_NAMESPACE_END
  22020. ///////////////////////
  22021. // nonmember support //
  22022. ///////////////////////
  22023. namespace std // NOLINT(cert-dcl58-cpp)
  22024. {
  22025. /// @brief hash value for JSON objects
  22026. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  22027. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22028. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  22029. {
  22030. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  22031. {
  22032. return nlohmann::detail::hash(j);
  22033. }
  22034. };
  22035. // specialization for std::less<value_t>
  22036. template<>
  22037. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  22038. {
  22039. /*!
  22040. @brief compare two value_t enum values
  22041. @since version 3.0.0
  22042. */
  22043. bool operator()(::nlohmann::detail::value_t lhs,
  22044. ::nlohmann::detail::value_t rhs) const noexcept
  22045. {
  22046. #if JSON_HAS_THREE_WAY_COMPARISON
  22047. return std::is_lt(lhs <=> rhs); // *NOPAD*
  22048. #else
  22049. return ::nlohmann::detail::operator<(lhs, rhs);
  22050. #endif
  22051. }
  22052. };
  22053. // C++20 prohibit function specialization in the std namespace.
  22054. #ifndef JSON_HAS_CPP_20
  22055. /// @brief exchanges the values of two JSON objects
  22056. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  22057. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22058. inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC_JSON_TPL& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp)
  22059. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  22060. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  22061. {
  22062. j1.swap(j2);
  22063. }
  22064. #endif
  22065. } // namespace std
  22066. #if JSON_USE_GLOBAL_UDLS
  22067. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  22068. using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22069. using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22070. #else
  22071. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22072. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22073. #endif
  22074. #endif
  22075. // #include <nlohmann/detail/macro_unscope.hpp>
  22076. // __ _____ _____ _____
  22077. // __| | __| | | | JSON for Modern C++
  22078. // | | |__ | | | | | | version 3.12.0
  22079. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  22080. //
  22081. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  22082. // SPDX-License-Identifier: MIT
  22083. // restore clang diagnostic settings
  22084. #if defined(__clang__)
  22085. #pragma clang diagnostic pop
  22086. #endif
  22087. // clean up
  22088. #undef JSON_ASSERT
  22089. #undef JSON_INTERNAL_CATCH
  22090. #undef JSON_THROW
  22091. #undef JSON_PRIVATE_UNLESS_TESTED
  22092. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22093. #undef NLOHMANN_BASIC_JSON_TPL
  22094. #undef JSON_EXPLICIT
  22095. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  22096. #undef JSON_INLINE_VARIABLE
  22097. #undef JSON_NO_UNIQUE_ADDRESS
  22098. #undef JSON_DISABLE_ENUM_SERIALIZATION
  22099. #undef JSON_USE_GLOBAL_UDLS
  22100. #ifndef JSON_TEST_KEEP_MACROS
  22101. #undef JSON_CATCH
  22102. #undef JSON_TRY
  22103. #undef JSON_HAS_CPP_11
  22104. #undef JSON_HAS_CPP_14
  22105. #undef JSON_HAS_CPP_17
  22106. #undef JSON_HAS_CPP_20
  22107. #undef JSON_HAS_CPP_23
  22108. #undef JSON_HAS_FILESYSTEM
  22109. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  22110. #undef JSON_HAS_THREE_WAY_COMPARISON
  22111. #undef JSON_HAS_RANGES
  22112. #undef JSON_HAS_STATIC_RTTI
  22113. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  22114. #endif
  22115. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  22116. // __ _____ _____ _____
  22117. // __| | __| | | | JSON for Modern C++
  22118. // | | |__ | | | | | | version 3.12.0
  22119. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  22120. //
  22121. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  22122. // SPDX-License-Identifier: MIT
  22123. #undef JSON_HEDLEY_ALWAYS_INLINE
  22124. #undef JSON_HEDLEY_ARM_VERSION
  22125. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  22126. #undef JSON_HEDLEY_ARRAY_PARAM
  22127. #undef JSON_HEDLEY_ASSUME
  22128. #undef JSON_HEDLEY_BEGIN_C_DECLS
  22129. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  22130. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  22131. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  22132. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  22133. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  22134. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  22135. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  22136. #undef JSON_HEDLEY_COMPCERT_VERSION
  22137. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  22138. #undef JSON_HEDLEY_CONCAT
  22139. #undef JSON_HEDLEY_CONCAT3
  22140. #undef JSON_HEDLEY_CONCAT3_EX
  22141. #undef JSON_HEDLEY_CONCAT_EX
  22142. #undef JSON_HEDLEY_CONST
  22143. #undef JSON_HEDLEY_CONSTEXPR
  22144. #undef JSON_HEDLEY_CONST_CAST
  22145. #undef JSON_HEDLEY_CPP_CAST
  22146. #undef JSON_HEDLEY_CRAY_VERSION
  22147. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  22148. #undef JSON_HEDLEY_C_DECL
  22149. #undef JSON_HEDLEY_DEPRECATED
  22150. #undef JSON_HEDLEY_DEPRECATED_FOR
  22151. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  22152. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  22153. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  22154. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  22155. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  22156. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  22157. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  22158. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  22159. #undef JSON_HEDLEY_DMC_VERSION
  22160. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  22161. #undef JSON_HEDLEY_EMPTY_BASES
  22162. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  22163. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  22164. #undef JSON_HEDLEY_END_C_DECLS
  22165. #undef JSON_HEDLEY_FLAGS
  22166. #undef JSON_HEDLEY_FLAGS_CAST
  22167. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  22168. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  22169. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  22170. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  22171. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  22172. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  22173. #undef JSON_HEDLEY_GCC_HAS_WARNING
  22174. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  22175. #undef JSON_HEDLEY_GCC_VERSION
  22176. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  22177. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  22178. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  22179. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  22180. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  22181. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  22182. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  22183. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  22184. #undef JSON_HEDLEY_GNUC_VERSION
  22185. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  22186. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  22187. #undef JSON_HEDLEY_HAS_BUILTIN
  22188. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  22189. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  22190. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  22191. #undef JSON_HEDLEY_HAS_EXTENSION
  22192. #undef JSON_HEDLEY_HAS_FEATURE
  22193. #undef JSON_HEDLEY_HAS_WARNING
  22194. #undef JSON_HEDLEY_IAR_VERSION
  22195. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  22196. #undef JSON_HEDLEY_IBM_VERSION
  22197. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  22198. #undef JSON_HEDLEY_IMPORT
  22199. #undef JSON_HEDLEY_INLINE
  22200. #undef JSON_HEDLEY_INTEL_CL_VERSION
  22201. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  22202. #undef JSON_HEDLEY_INTEL_VERSION
  22203. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  22204. #undef JSON_HEDLEY_IS_CONSTANT
  22205. #undef JSON_HEDLEY_IS_CONSTEXPR_
  22206. #undef JSON_HEDLEY_LIKELY
  22207. #undef JSON_HEDLEY_MALLOC
  22208. #undef JSON_HEDLEY_MCST_LCC_VERSION
  22209. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  22210. #undef JSON_HEDLEY_MESSAGE
  22211. #undef JSON_HEDLEY_MSVC_VERSION
  22212. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  22213. #undef JSON_HEDLEY_NEVER_INLINE
  22214. #undef JSON_HEDLEY_NON_NULL
  22215. #undef JSON_HEDLEY_NO_ESCAPE
  22216. #undef JSON_HEDLEY_NO_RETURN
  22217. #undef JSON_HEDLEY_NO_THROW
  22218. #undef JSON_HEDLEY_NULL
  22219. #undef JSON_HEDLEY_PELLES_VERSION
  22220. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  22221. #undef JSON_HEDLEY_PGI_VERSION
  22222. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  22223. #undef JSON_HEDLEY_PREDICT
  22224. #undef JSON_HEDLEY_PRINTF_FORMAT
  22225. #undef JSON_HEDLEY_PRIVATE
  22226. #undef JSON_HEDLEY_PUBLIC
  22227. #undef JSON_HEDLEY_PURE
  22228. #undef JSON_HEDLEY_REINTERPRET_CAST
  22229. #undef JSON_HEDLEY_REQUIRE
  22230. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  22231. #undef JSON_HEDLEY_REQUIRE_MSG
  22232. #undef JSON_HEDLEY_RESTRICT
  22233. #undef JSON_HEDLEY_RETURNS_NON_NULL
  22234. #undef JSON_HEDLEY_SENTINEL
  22235. #undef JSON_HEDLEY_STATIC_ASSERT
  22236. #undef JSON_HEDLEY_STATIC_CAST
  22237. #undef JSON_HEDLEY_STRINGIFY
  22238. #undef JSON_HEDLEY_STRINGIFY_EX
  22239. #undef JSON_HEDLEY_SUNPRO_VERSION
  22240. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  22241. #undef JSON_HEDLEY_TINYC_VERSION
  22242. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  22243. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  22244. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  22245. #undef JSON_HEDLEY_TI_CL2000_VERSION
  22246. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  22247. #undef JSON_HEDLEY_TI_CL430_VERSION
  22248. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  22249. #undef JSON_HEDLEY_TI_CL6X_VERSION
  22250. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  22251. #undef JSON_HEDLEY_TI_CL7X_VERSION
  22252. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  22253. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  22254. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  22255. #undef JSON_HEDLEY_TI_VERSION
  22256. #undef JSON_HEDLEY_TI_VERSION_CHECK
  22257. #undef JSON_HEDLEY_UNAVAILABLE
  22258. #undef JSON_HEDLEY_UNLIKELY
  22259. #undef JSON_HEDLEY_UNPREDICTABLE
  22260. #undef JSON_HEDLEY_UNREACHABLE
  22261. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  22262. #undef JSON_HEDLEY_VERSION
  22263. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  22264. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  22265. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  22266. #undef JSON_HEDLEY_VERSION_ENCODE
  22267. #undef JSON_HEDLEY_WARNING
  22268. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  22269. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  22270. #undef JSON_HEDLEY_FALL_THROUGH
  22271. #endif // INCLUDE_NLOHMANN_JSON_HPP_