json.hpp 945 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.12.0
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2026 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-2026 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-2026 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-2026 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. #include <string> // string
  145. #include <tuple> // tuple, make_tuple
  146. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  147. #include <unordered_map> // unordered_map
  148. #include <utility> // pair, declval
  149. #include <valarray> // valarray
  150. // #include <nlohmann/detail/exceptions.hpp>
  151. // __ _____ _____ _____
  152. // __| | __| | | | JSON for Modern C++
  153. // | | |__ | | | | | | version 3.12.0
  154. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  155. //
  156. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  157. // SPDX-License-Identifier: MIT
  158. #include <cstddef> // nullptr_t
  159. #include <exception> // exception
  160. #if JSON_DIAGNOSTICS
  161. #include <numeric> // accumulate
  162. #endif
  163. #include <stdexcept> // runtime_error
  164. #include <string> // to_string
  165. #include <vector> // vector
  166. // #include <nlohmann/detail/value_t.hpp>
  167. // __ _____ _____ _____
  168. // __| | __| | | | JSON for Modern C++
  169. // | | |__ | | | | | | version 3.12.0
  170. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  171. //
  172. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  173. // SPDX-License-Identifier: MIT
  174. #include <array> // array
  175. #include <cstddef> // size_t
  176. #include <cstdint> // uint8_t
  177. #include <string> // string
  178. // #include <nlohmann/detail/macro_scope.hpp>
  179. // __ _____ _____ _____
  180. // __| | __| | | | JSON for Modern C++
  181. // | | |__ | | | | | | version 3.12.0
  182. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  183. //
  184. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  185. // SPDX-License-Identifier: MIT
  186. #include <utility> // declval, pair
  187. // #include <nlohmann/detail/meta/detected.hpp>
  188. // __ _____ _____ _____
  189. // __| | __| | | | JSON for Modern C++
  190. // | | |__ | | | | | | version 3.12.0
  191. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  192. //
  193. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  194. // SPDX-License-Identifier: MIT
  195. #include <type_traits>
  196. // #include <nlohmann/detail/meta/void_t.hpp>
  197. // __ _____ _____ _____
  198. // __| | __| | | | JSON for Modern C++
  199. // | | |__ | | | | | | version 3.12.0
  200. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  201. //
  202. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  203. // SPDX-License-Identifier: MIT
  204. // #include <nlohmann/detail/abi_macros.hpp>
  205. NLOHMANN_JSON_NAMESPACE_BEGIN
  206. namespace detail
  207. {
  208. template<typename ...Ts> struct make_void
  209. {
  210. using type = void;
  211. };
  212. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  213. } // namespace detail
  214. NLOHMANN_JSON_NAMESPACE_END
  215. NLOHMANN_JSON_NAMESPACE_BEGIN
  216. namespace detail
  217. {
  218. // https://en.cppreference.com/w/cpp/experimental/is_detected
  219. struct nonesuch
  220. {
  221. nonesuch() = delete;
  222. ~nonesuch() = delete;
  223. nonesuch(nonesuch const&) = delete;
  224. nonesuch(nonesuch const&&) = delete;
  225. void operator=(nonesuch const&) = delete;
  226. void operator=(nonesuch&&) = delete;
  227. };
  228. template<class Default,
  229. class AlwaysVoid,
  230. template<class...> class Op,
  231. class... Args>
  232. struct detector
  233. {
  234. using value_t = std::false_type;
  235. using type = Default;
  236. };
  237. template<class Default, template<class...> class Op, class... Args>
  238. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  239. {
  240. using value_t = std::true_type;
  241. using type = Op<Args...>;
  242. };
  243. template<template<class...> class Op, class... Args>
  244. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  245. template<template<class...> class Op, class... Args>
  246. struct is_detected_lazy : is_detected<Op, Args...> { };
  247. template<template<class...> class Op, class... Args>
  248. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  249. template<class Default, template<class...> class Op, class... Args>
  250. using detected_or = detector<Default, void, Op, Args...>;
  251. template<class Default, template<class...> class Op, class... Args>
  252. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  253. template<class Expected, template<class...> class Op, class... Args>
  254. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  255. template<class To, template<class...> class Op, class... Args>
  256. using is_detected_convertible =
  257. std::is_convertible<detected_t<Op, Args...>, To>;
  258. } // namespace detail
  259. NLOHMANN_JSON_NAMESPACE_END
  260. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  261. // __ _____ _____ _____
  262. // __| | __| | | | JSON for Modern C++
  263. // | | |__ | | | | | | version 3.12.0
  264. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  265. //
  266. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  267. // SPDX-FileCopyrightText: 2016-2021 Evan Nemerson <evan@nemerson.com>
  268. // SPDX-License-Identifier: MIT
  269. /* Hedley - https://nemequ.github.io/hedley
  270. * Created by Evan Nemerson <evan@nemerson.com>
  271. * SPDX-License-Identifier: CC0-1.0
  272. */
  273. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  274. #if defined(JSON_HEDLEY_VERSION)
  275. #undef JSON_HEDLEY_VERSION
  276. #endif
  277. #define JSON_HEDLEY_VERSION 15
  278. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  279. #undef JSON_HEDLEY_STRINGIFY_EX
  280. #endif
  281. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  282. #if defined(JSON_HEDLEY_STRINGIFY)
  283. #undef JSON_HEDLEY_STRINGIFY
  284. #endif
  285. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  286. #if defined(JSON_HEDLEY_CONCAT_EX)
  287. #undef JSON_HEDLEY_CONCAT_EX
  288. #endif
  289. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  290. #if defined(JSON_HEDLEY_CONCAT)
  291. #undef JSON_HEDLEY_CONCAT
  292. #endif
  293. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  294. #if defined(JSON_HEDLEY_CONCAT3_EX)
  295. #undef JSON_HEDLEY_CONCAT3_EX
  296. #endif
  297. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  298. #if defined(JSON_HEDLEY_CONCAT3)
  299. #undef JSON_HEDLEY_CONCAT3
  300. #endif
  301. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  302. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  303. #undef JSON_HEDLEY_VERSION_ENCODE
  304. #endif
  305. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  306. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  307. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  308. #endif
  309. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  310. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  311. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  312. #endif
  313. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  314. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  315. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  316. #endif
  317. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  318. #if defined(JSON_HEDLEY_GNUC_VERSION)
  319. #undef JSON_HEDLEY_GNUC_VERSION
  320. #endif
  321. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  322. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  323. #elif defined(__GNUC__)
  324. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  325. #endif
  326. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  327. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  328. #endif
  329. #if defined(JSON_HEDLEY_GNUC_VERSION)
  330. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  331. #else
  332. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  333. #endif
  334. #if defined(JSON_HEDLEY_MSVC_VERSION)
  335. #undef JSON_HEDLEY_MSVC_VERSION
  336. #endif
  337. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  338. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  339. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  340. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  341. #elif defined(_MSC_VER) && !defined(__ICL)
  342. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  343. #endif
  344. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  345. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  346. #endif
  347. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  348. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  349. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  350. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  351. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  352. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  353. #else
  354. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  355. #endif
  356. #if defined(JSON_HEDLEY_INTEL_VERSION)
  357. #undef JSON_HEDLEY_INTEL_VERSION
  358. #endif
  359. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  360. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  361. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  362. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  363. #endif
  364. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  365. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  366. #endif
  367. #if defined(JSON_HEDLEY_INTEL_VERSION)
  368. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  369. #else
  370. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  371. #endif
  372. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  373. #undef JSON_HEDLEY_INTEL_CL_VERSION
  374. #endif
  375. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  376. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  377. #endif
  378. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  379. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  380. #endif
  381. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  382. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  383. #else
  384. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  385. #endif
  386. #if defined(JSON_HEDLEY_PGI_VERSION)
  387. #undef JSON_HEDLEY_PGI_VERSION
  388. #endif
  389. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  390. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  391. #endif
  392. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  393. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  394. #endif
  395. #if defined(JSON_HEDLEY_PGI_VERSION)
  396. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  397. #else
  398. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  399. #endif
  400. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  401. #undef JSON_HEDLEY_SUNPRO_VERSION
  402. #endif
  403. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  404. #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)
  405. #elif defined(__SUNPRO_C)
  406. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  407. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  408. #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)
  409. #elif defined(__SUNPRO_CC)
  410. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  411. #endif
  412. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  413. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  414. #endif
  415. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  416. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  417. #else
  418. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  419. #endif
  420. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  421. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  422. #endif
  423. #if defined(__EMSCRIPTEN__)
  424. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  425. #endif
  426. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  427. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  428. #endif
  429. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  430. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  431. #else
  432. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  433. #endif
  434. #if defined(JSON_HEDLEY_ARM_VERSION)
  435. #undef JSON_HEDLEY_ARM_VERSION
  436. #endif
  437. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  438. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  439. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  440. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  441. #endif
  442. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  443. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  444. #endif
  445. #if defined(JSON_HEDLEY_ARM_VERSION)
  446. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  447. #else
  448. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  449. #endif
  450. #if defined(JSON_HEDLEY_IBM_VERSION)
  451. #undef JSON_HEDLEY_IBM_VERSION
  452. #endif
  453. #if defined(__ibmxl__)
  454. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  455. #elif defined(__xlC__) && defined(__xlC_ver__)
  456. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  457. #elif defined(__xlC__)
  458. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  459. #endif
  460. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  461. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  462. #endif
  463. #if defined(JSON_HEDLEY_IBM_VERSION)
  464. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  465. #else
  466. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  467. #endif
  468. #if defined(JSON_HEDLEY_TI_VERSION)
  469. #undef JSON_HEDLEY_TI_VERSION
  470. #endif
  471. #if \
  472. defined(__TI_COMPILER_VERSION__) && \
  473. ( \
  474. defined(__TMS470__) || defined(__TI_ARM__) || \
  475. defined(__MSP430__) || \
  476. defined(__TMS320C2000__) \
  477. )
  478. #if (__TI_COMPILER_VERSION__ >= 16000000)
  479. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  480. #endif
  481. #endif
  482. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  483. #undef JSON_HEDLEY_TI_VERSION_CHECK
  484. #endif
  485. #if defined(JSON_HEDLEY_TI_VERSION)
  486. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  487. #else
  488. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  489. #endif
  490. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  491. #undef JSON_HEDLEY_TI_CL2000_VERSION
  492. #endif
  493. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  494. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  495. #endif
  496. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  497. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  498. #endif
  499. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  500. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  501. #else
  502. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  503. #endif
  504. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  505. #undef JSON_HEDLEY_TI_CL430_VERSION
  506. #endif
  507. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  508. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  509. #endif
  510. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  511. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  512. #endif
  513. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  514. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  515. #else
  516. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  517. #endif
  518. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  519. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  520. #endif
  521. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  522. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  523. #endif
  524. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  525. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  526. #endif
  527. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  528. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  529. #else
  530. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  531. #endif
  532. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  533. #undef JSON_HEDLEY_TI_CL6X_VERSION
  534. #endif
  535. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  536. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  537. #endif
  538. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  539. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  540. #endif
  541. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  542. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  543. #else
  544. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  545. #endif
  546. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  547. #undef JSON_HEDLEY_TI_CL7X_VERSION
  548. #endif
  549. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  550. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  551. #endif
  552. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  553. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  554. #endif
  555. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  556. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  557. #else
  558. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  559. #endif
  560. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  561. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  562. #endif
  563. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  564. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  565. #endif
  566. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  567. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  568. #endif
  569. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  570. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  571. #else
  572. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  573. #endif
  574. #if defined(JSON_HEDLEY_CRAY_VERSION)
  575. #undef JSON_HEDLEY_CRAY_VERSION
  576. #endif
  577. #if defined(_CRAYC)
  578. #if defined(_RELEASE_PATCHLEVEL)
  579. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  580. #else
  581. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  582. #endif
  583. #endif
  584. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  585. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  586. #endif
  587. #if defined(JSON_HEDLEY_CRAY_VERSION)
  588. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  589. #else
  590. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  591. #endif
  592. #if defined(JSON_HEDLEY_IAR_VERSION)
  593. #undef JSON_HEDLEY_IAR_VERSION
  594. #endif
  595. #if defined(__IAR_SYSTEMS_ICC__)
  596. #if __VER__ > 1000
  597. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  598. #else
  599. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  600. #endif
  601. #endif
  602. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  603. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  604. #endif
  605. #if defined(JSON_HEDLEY_IAR_VERSION)
  606. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  607. #else
  608. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  609. #endif
  610. #if defined(JSON_HEDLEY_TINYC_VERSION)
  611. #undef JSON_HEDLEY_TINYC_VERSION
  612. #endif
  613. #if defined(__TINYC__)
  614. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  615. #endif
  616. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  617. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  618. #endif
  619. #if defined(JSON_HEDLEY_TINYC_VERSION)
  620. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  621. #else
  622. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  623. #endif
  624. #if defined(JSON_HEDLEY_DMC_VERSION)
  625. #undef JSON_HEDLEY_DMC_VERSION
  626. #endif
  627. #if defined(__DMC__)
  628. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  629. #endif
  630. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  631. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  632. #endif
  633. #if defined(JSON_HEDLEY_DMC_VERSION)
  634. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  635. #else
  636. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  637. #endif
  638. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  639. #undef JSON_HEDLEY_COMPCERT_VERSION
  640. #endif
  641. #if defined(__COMPCERT_VERSION__)
  642. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  643. #endif
  644. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  645. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  646. #endif
  647. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  648. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  649. #else
  650. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  651. #endif
  652. #if defined(JSON_HEDLEY_PELLES_VERSION)
  653. #undef JSON_HEDLEY_PELLES_VERSION
  654. #endif
  655. #if defined(__POCC__)
  656. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  657. #endif
  658. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  659. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  660. #endif
  661. #if defined(JSON_HEDLEY_PELLES_VERSION)
  662. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  663. #else
  664. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  665. #endif
  666. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  667. #undef JSON_HEDLEY_MCST_LCC_VERSION
  668. #endif
  669. #if defined(__LCC__) && defined(__LCC_MINOR__)
  670. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  671. #endif
  672. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  673. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  674. #endif
  675. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  676. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  677. #else
  678. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  679. #endif
  680. #if defined(JSON_HEDLEY_GCC_VERSION)
  681. #undef JSON_HEDLEY_GCC_VERSION
  682. #endif
  683. #if \
  684. defined(JSON_HEDLEY_GNUC_VERSION) && \
  685. !defined(__clang__) && \
  686. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  687. !defined(JSON_HEDLEY_PGI_VERSION) && \
  688. !defined(JSON_HEDLEY_ARM_VERSION) && \
  689. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  690. !defined(JSON_HEDLEY_TI_VERSION) && \
  691. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  692. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  693. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  694. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  695. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  696. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  697. !defined(__COMPCERT__) && \
  698. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  699. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  700. #endif
  701. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  702. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  703. #endif
  704. #if defined(JSON_HEDLEY_GCC_VERSION)
  705. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  706. #else
  707. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  708. #endif
  709. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  710. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  711. #endif
  712. #if \
  713. defined(__has_attribute) && \
  714. ( \
  715. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  716. )
  717. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  718. #else
  719. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  720. #endif
  721. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  722. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  723. #endif
  724. #if defined(__has_attribute)
  725. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  726. #else
  727. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  728. #endif
  729. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  730. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  731. #endif
  732. #if defined(__has_attribute)
  733. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  734. #else
  735. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  736. #endif
  737. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  738. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  739. #endif
  740. #if \
  741. defined(__has_cpp_attribute) && \
  742. defined(__cplusplus) && \
  743. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  744. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  745. #else
  746. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  747. #endif
  748. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  749. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  750. #endif
  751. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  752. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  753. #elif \
  754. !defined(JSON_HEDLEY_PGI_VERSION) && \
  755. !defined(JSON_HEDLEY_IAR_VERSION) && \
  756. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  757. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  758. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  759. #else
  760. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  761. #endif
  762. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  763. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  764. #endif
  765. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  766. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  767. #else
  768. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  769. #endif
  770. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  771. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  772. #endif
  773. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  774. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  775. #else
  776. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  777. #endif
  778. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  779. #undef JSON_HEDLEY_HAS_BUILTIN
  780. #endif
  781. #if defined(__has_builtin)
  782. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  783. #else
  784. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  785. #endif
  786. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  787. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  788. #endif
  789. #if defined(__has_builtin)
  790. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  791. #else
  792. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  793. #endif
  794. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  795. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  796. #endif
  797. #if defined(__has_builtin)
  798. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  799. #else
  800. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  801. #endif
  802. #if defined(JSON_HEDLEY_HAS_FEATURE)
  803. #undef JSON_HEDLEY_HAS_FEATURE
  804. #endif
  805. #if defined(__has_feature)
  806. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  807. #else
  808. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  809. #endif
  810. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  811. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  812. #endif
  813. #if defined(__has_feature)
  814. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  815. #else
  816. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  817. #endif
  818. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  819. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  820. #endif
  821. #if defined(__has_feature)
  822. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  823. #else
  824. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  825. #endif
  826. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  827. #undef JSON_HEDLEY_HAS_EXTENSION
  828. #endif
  829. #if defined(__has_extension)
  830. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  831. #else
  832. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  833. #endif
  834. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  835. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  836. #endif
  837. #if defined(__has_extension)
  838. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  839. #else
  840. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  841. #endif
  842. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  843. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  844. #endif
  845. #if defined(__has_extension)
  846. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  847. #else
  848. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  849. #endif
  850. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  851. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  852. #endif
  853. #if defined(__has_declspec_attribute)
  854. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  855. #else
  856. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  857. #endif
  858. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  859. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  860. #endif
  861. #if defined(__has_declspec_attribute)
  862. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  863. #else
  864. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  865. #endif
  866. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  867. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  868. #endif
  869. #if defined(__has_declspec_attribute)
  870. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  871. #else
  872. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  873. #endif
  874. #if defined(JSON_HEDLEY_HAS_WARNING)
  875. #undef JSON_HEDLEY_HAS_WARNING
  876. #endif
  877. #if defined(__has_warning)
  878. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  879. #else
  880. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  881. #endif
  882. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  883. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  884. #endif
  885. #if defined(__has_warning)
  886. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  887. #else
  888. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  889. #endif
  890. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  891. #undef JSON_HEDLEY_GCC_HAS_WARNING
  892. #endif
  893. #if defined(__has_warning)
  894. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  895. #else
  896. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  897. #endif
  898. #if \
  899. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  900. defined(__clang__) || \
  901. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  902. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  903. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  904. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  905. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  906. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  907. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  908. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  909. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  910. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  911. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  912. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  913. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  914. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  915. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  916. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  917. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  918. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  919. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  920. #else
  921. #define JSON_HEDLEY_PRAGMA(value)
  922. #endif
  923. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  924. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  925. #endif
  926. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  927. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  928. #endif
  929. #if defined(__clang__)
  930. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  931. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  932. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  933. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  934. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  935. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  936. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  937. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  938. #elif \
  939. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  940. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  941. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  942. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  943. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  944. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  945. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  946. #elif \
  947. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  948. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  949. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  950. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  951. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  952. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  953. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  954. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  955. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  956. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  957. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  958. #else
  959. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  960. #define JSON_HEDLEY_DIAGNOSTIC_POP
  961. #endif
  962. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  963. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  964. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  965. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  966. #endif
  967. #if defined(__cplusplus)
  968. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  969. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  970. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  971. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  972. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  973. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  974. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  975. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  976. xpr \
  977. JSON_HEDLEY_DIAGNOSTIC_POP
  978. # else
  979. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  980. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  981. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  982. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  983. xpr \
  984. JSON_HEDLEY_DIAGNOSTIC_POP
  985. # endif
  986. # else
  987. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  988. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  989. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  990. xpr \
  991. JSON_HEDLEY_DIAGNOSTIC_POP
  992. # endif
  993. # endif
  994. #endif
  995. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  996. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  997. #endif
  998. #if defined(JSON_HEDLEY_CONST_CAST)
  999. #undef JSON_HEDLEY_CONST_CAST
  1000. #endif
  1001. #if defined(__cplusplus)
  1002. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  1003. #elif \
  1004. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  1005. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  1006. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1007. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  1008. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1009. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  1010. ((T) (expr)); \
  1011. JSON_HEDLEY_DIAGNOSTIC_POP \
  1012. }))
  1013. #else
  1014. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  1015. #endif
  1016. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  1017. #undef JSON_HEDLEY_REINTERPRET_CAST
  1018. #endif
  1019. #if defined(__cplusplus)
  1020. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  1021. #else
  1022. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  1023. #endif
  1024. #if defined(JSON_HEDLEY_STATIC_CAST)
  1025. #undef JSON_HEDLEY_STATIC_CAST
  1026. #endif
  1027. #if defined(__cplusplus)
  1028. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  1029. #else
  1030. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  1031. #endif
  1032. #if defined(JSON_HEDLEY_CPP_CAST)
  1033. #undef JSON_HEDLEY_CPP_CAST
  1034. #endif
  1035. #if defined(__cplusplus)
  1036. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  1037. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1038. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1039. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  1040. ((T) (expr)) \
  1041. JSON_HEDLEY_DIAGNOSTIC_POP
  1042. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  1043. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1044. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1045. _Pragma("diag_suppress=Pe137") \
  1046. JSON_HEDLEY_DIAGNOSTIC_POP
  1047. # else
  1048. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  1049. # endif
  1050. #else
  1051. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  1052. #endif
  1053. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  1054. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1055. #endif
  1056. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  1057. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  1058. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1059. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  1060. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1061. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  1062. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1063. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  1064. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1065. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1066. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1067. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1068. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1069. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  1070. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1071. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1072. #elif \
  1073. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1074. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1075. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1076. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1077. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1078. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1079. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1080. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1081. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1082. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1083. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  1084. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  1085. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  1086. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  1087. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  1088. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  1089. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1090. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  1091. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  1092. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  1093. #else
  1094. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1095. #endif
  1096. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  1097. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1098. #endif
  1099. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1100. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  1101. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1102. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  1103. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1104. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  1105. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1106. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  1107. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1108. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  1109. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1110. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  1111. #elif \
  1112. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  1113. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1114. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1115. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  1116. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1117. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  1118. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1119. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1120. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  1121. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1122. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  1123. #else
  1124. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1125. #endif
  1126. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1127. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1128. #endif
  1129. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1130. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1131. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1132. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1133. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1134. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1135. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1136. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1137. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1138. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1139. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1140. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1141. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1142. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1143. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1144. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1145. #elif \
  1146. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1147. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1148. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1149. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1150. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1151. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1152. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1153. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1154. #else
  1155. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1156. #endif
  1157. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1158. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1159. #endif
  1160. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1161. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1162. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1163. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1164. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1165. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1166. #else
  1167. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1168. #endif
  1169. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1170. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1171. #endif
  1172. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1173. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1174. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1175. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1176. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1177. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1178. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1179. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1180. #else
  1181. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1182. #endif
  1183. #if defined(JSON_HEDLEY_DEPRECATED)
  1184. #undef JSON_HEDLEY_DEPRECATED
  1185. #endif
  1186. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1187. #undef JSON_HEDLEY_DEPRECATED_FOR
  1188. #endif
  1189. #if \
  1190. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1191. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1192. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1193. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1194. #elif \
  1195. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1196. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1197. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1198. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1199. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1200. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1201. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1202. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1203. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1204. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1205. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1206. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1207. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1208. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1209. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1210. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1211. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1212. #elif \
  1213. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1214. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1215. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1216. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1217. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1218. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1219. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1220. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1221. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1222. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1223. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1224. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1225. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1226. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1227. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1228. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1229. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1230. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1231. #elif \
  1232. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1233. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1234. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1235. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1236. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1237. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1238. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1239. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1240. #else
  1241. #define JSON_HEDLEY_DEPRECATED(since)
  1242. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1243. #endif
  1244. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1245. #undef JSON_HEDLEY_UNAVAILABLE
  1246. #endif
  1247. #if \
  1248. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1249. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1250. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1251. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1252. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1253. #else
  1254. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1255. #endif
  1256. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1257. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1258. #endif
  1259. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1260. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1261. #endif
  1262. #if \
  1263. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1264. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1265. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1266. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1267. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1268. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1269. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1270. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1271. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1272. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1273. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1274. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1275. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1276. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1277. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1278. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1279. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1280. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1281. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1282. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1283. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1284. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1285. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1286. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1287. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1288. #elif defined(_Check_return_) /* SAL */
  1289. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1290. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1291. #else
  1292. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1293. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1294. #endif
  1295. #if defined(JSON_HEDLEY_SENTINEL)
  1296. #undef JSON_HEDLEY_SENTINEL
  1297. #endif
  1298. #if \
  1299. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1300. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1301. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1302. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1303. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1304. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1305. #else
  1306. #define JSON_HEDLEY_SENTINEL(position)
  1307. #endif
  1308. #if defined(JSON_HEDLEY_NO_RETURN)
  1309. #undef JSON_HEDLEY_NO_RETURN
  1310. #endif
  1311. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1312. #define JSON_HEDLEY_NO_RETURN __noreturn
  1313. #elif \
  1314. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1315. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1316. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1317. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1318. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1319. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1320. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1321. #elif \
  1322. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1323. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1324. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1325. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1326. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1327. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1328. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1329. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1330. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1331. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1332. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1333. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1334. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1335. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1336. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1337. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1338. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1339. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1340. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1341. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1342. #elif \
  1343. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1344. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1345. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1346. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1347. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1348. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1349. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1350. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1351. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1352. #else
  1353. #define JSON_HEDLEY_NO_RETURN
  1354. #endif
  1355. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1356. #undef JSON_HEDLEY_NO_ESCAPE
  1357. #endif
  1358. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1359. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1360. #else
  1361. #define JSON_HEDLEY_NO_ESCAPE
  1362. #endif
  1363. #if defined(JSON_HEDLEY_UNREACHABLE)
  1364. #undef JSON_HEDLEY_UNREACHABLE
  1365. #endif
  1366. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1367. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1368. #endif
  1369. #if defined(JSON_HEDLEY_ASSUME)
  1370. #undef JSON_HEDLEY_ASSUME
  1371. #endif
  1372. #if \
  1373. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1374. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1375. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1376. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1377. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1378. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1379. #elif \
  1380. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1381. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1382. #if defined(__cplusplus)
  1383. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1384. #else
  1385. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1386. #endif
  1387. #endif
  1388. #if \
  1389. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1390. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1391. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1392. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1393. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1394. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1395. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1396. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1397. #elif defined(JSON_HEDLEY_ASSUME)
  1398. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1399. #endif
  1400. #if !defined(JSON_HEDLEY_ASSUME)
  1401. #if defined(JSON_HEDLEY_UNREACHABLE)
  1402. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1403. #else
  1404. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1405. #endif
  1406. #endif
  1407. #if defined(JSON_HEDLEY_UNREACHABLE)
  1408. #if \
  1409. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1410. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1411. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1412. #else
  1413. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1414. #endif
  1415. #else
  1416. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1417. #endif
  1418. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1419. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1420. #endif
  1421. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1422. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1423. #pragma clang diagnostic ignored "-Wpedantic"
  1424. #endif
  1425. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1426. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1427. #endif
  1428. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1429. #if defined(__clang__)
  1430. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1431. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1432. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1433. #endif
  1434. #endif
  1435. #if defined(JSON_HEDLEY_NON_NULL)
  1436. #undef JSON_HEDLEY_NON_NULL
  1437. #endif
  1438. #if \
  1439. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1440. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1441. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1442. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1443. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1444. #else
  1445. #define JSON_HEDLEY_NON_NULL(...)
  1446. #endif
  1447. JSON_HEDLEY_DIAGNOSTIC_POP
  1448. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1449. #undef JSON_HEDLEY_PRINTF_FORMAT
  1450. #endif
  1451. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1452. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1453. #elif 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__(gnu_printf, string_idx, first_to_check)))
  1455. #elif \
  1456. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1457. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1458. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1459. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1460. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1461. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1462. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1463. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1464. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1465. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1466. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1467. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1468. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1469. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1470. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1471. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1472. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1473. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1474. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1475. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1476. #else
  1477. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1478. #endif
  1479. #if defined(JSON_HEDLEY_CONSTEXPR)
  1480. #undef JSON_HEDLEY_CONSTEXPR
  1481. #endif
  1482. #if defined(__cplusplus)
  1483. #if __cplusplus >= 201103L
  1484. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1485. #endif
  1486. #endif
  1487. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1488. #define JSON_HEDLEY_CONSTEXPR
  1489. #endif
  1490. #if defined(JSON_HEDLEY_PREDICT)
  1491. #undef JSON_HEDLEY_PREDICT
  1492. #endif
  1493. #if defined(JSON_HEDLEY_LIKELY)
  1494. #undef JSON_HEDLEY_LIKELY
  1495. #endif
  1496. #if defined(JSON_HEDLEY_UNLIKELY)
  1497. #undef JSON_HEDLEY_UNLIKELY
  1498. #endif
  1499. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1500. #undef JSON_HEDLEY_UNPREDICTABLE
  1501. #endif
  1502. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1503. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1504. #endif
  1505. #if \
  1506. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1507. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1508. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1509. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1510. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1511. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1512. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1513. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1514. #elif \
  1515. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1516. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1517. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1518. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1519. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1520. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1521. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1522. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1523. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1524. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1525. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1526. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1527. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1528. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1529. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1530. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1531. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1532. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1533. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1534. (__extension__ ({ \
  1535. double hedley_probability_ = (probability); \
  1536. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1537. }))
  1538. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1539. (__extension__ ({ \
  1540. double hedley_probability_ = (probability); \
  1541. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1542. }))
  1543. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1544. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1545. #else
  1546. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1547. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1548. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1549. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1550. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1551. #endif
  1552. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1553. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1554. #endif
  1555. #if defined(JSON_HEDLEY_MALLOC)
  1556. #undef JSON_HEDLEY_MALLOC
  1557. #endif
  1558. #if \
  1559. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1560. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1561. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1562. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1563. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1564. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1565. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1566. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1567. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1568. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1569. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1570. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1571. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1572. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1573. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1574. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1575. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1576. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1577. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1578. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1579. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1580. #elif \
  1581. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1582. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1583. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1584. #else
  1585. #define JSON_HEDLEY_MALLOC
  1586. #endif
  1587. #if defined(JSON_HEDLEY_PURE)
  1588. #undef JSON_HEDLEY_PURE
  1589. #endif
  1590. #if \
  1591. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1592. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1593. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1594. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1595. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1596. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1597. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1598. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1599. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1600. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1601. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1602. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1603. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1604. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1605. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1606. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1607. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1608. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1609. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1610. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1611. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1612. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1613. #elif defined(__cplusplus) && \
  1614. ( \
  1615. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1616. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1617. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1618. )
  1619. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1620. #else
  1621. # define JSON_HEDLEY_PURE
  1622. #endif
  1623. #if defined(JSON_HEDLEY_CONST)
  1624. #undef JSON_HEDLEY_CONST
  1625. #endif
  1626. #if \
  1627. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1628. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1629. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1630. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1631. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1632. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1633. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1634. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1635. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1636. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1637. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1638. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1639. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1640. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1641. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1642. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1643. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1644. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1645. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1646. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1647. #elif \
  1648. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1649. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1650. #else
  1651. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1652. #endif
  1653. #if defined(JSON_HEDLEY_RESTRICT)
  1654. #undef JSON_HEDLEY_RESTRICT
  1655. #endif
  1656. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1657. #define JSON_HEDLEY_RESTRICT restrict
  1658. #elif \
  1659. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1660. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1661. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1662. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1663. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1664. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1665. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1666. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1667. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1668. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1669. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1670. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1671. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1672. defined(__clang__) || \
  1673. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1674. #define JSON_HEDLEY_RESTRICT __restrict
  1675. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1676. #define JSON_HEDLEY_RESTRICT _Restrict
  1677. #else
  1678. #define JSON_HEDLEY_RESTRICT
  1679. #endif
  1680. #if defined(JSON_HEDLEY_INLINE)
  1681. #undef JSON_HEDLEY_INLINE
  1682. #endif
  1683. #if \
  1684. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1685. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1686. #define JSON_HEDLEY_INLINE inline
  1687. #elif \
  1688. defined(JSON_HEDLEY_GCC_VERSION) || \
  1689. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1690. #define JSON_HEDLEY_INLINE __inline__
  1691. #elif \
  1692. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1693. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1694. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1695. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1696. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1697. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1698. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1699. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1700. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1701. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1702. #define JSON_HEDLEY_INLINE __inline
  1703. #else
  1704. #define JSON_HEDLEY_INLINE
  1705. #endif
  1706. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1707. #undef JSON_HEDLEY_ALWAYS_INLINE
  1708. #endif
  1709. #if \
  1710. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1711. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1712. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1713. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1714. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1715. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1716. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1717. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1718. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1719. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1720. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1721. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1722. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1723. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1724. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1725. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1726. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1727. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1728. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1729. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1730. #elif \
  1731. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1732. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1733. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1734. #elif defined(__cplusplus) && \
  1735. ( \
  1736. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1737. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1738. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1739. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1740. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1741. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1742. )
  1743. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1744. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1745. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1746. #else
  1747. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1748. #endif
  1749. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1750. #undef JSON_HEDLEY_NEVER_INLINE
  1751. #endif
  1752. #if \
  1753. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1754. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1755. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1756. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1757. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1758. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1759. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1760. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1761. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1762. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1763. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1764. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1765. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1766. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1767. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1768. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1769. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1770. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1771. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1772. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1773. #elif \
  1774. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1775. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1776. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1777. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1778. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1779. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1780. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1781. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1782. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1783. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1784. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1785. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1786. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1787. #else
  1788. #define JSON_HEDLEY_NEVER_INLINE
  1789. #endif
  1790. #if defined(JSON_HEDLEY_PRIVATE)
  1791. #undef JSON_HEDLEY_PRIVATE
  1792. #endif
  1793. #if defined(JSON_HEDLEY_PUBLIC)
  1794. #undef JSON_HEDLEY_PUBLIC
  1795. #endif
  1796. #if defined(JSON_HEDLEY_IMPORT)
  1797. #undef JSON_HEDLEY_IMPORT
  1798. #endif
  1799. #if defined(_WIN32) || defined(__CYGWIN__)
  1800. # define JSON_HEDLEY_PRIVATE
  1801. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1802. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1803. #else
  1804. # if \
  1805. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1806. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1807. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1808. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1809. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1810. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1811. ( \
  1812. defined(__TI_EABI__) && \
  1813. ( \
  1814. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1815. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1816. ) \
  1817. ) || \
  1818. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1819. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1820. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1821. # else
  1822. # define JSON_HEDLEY_PRIVATE
  1823. # define JSON_HEDLEY_PUBLIC
  1824. # endif
  1825. # define JSON_HEDLEY_IMPORT extern
  1826. #endif
  1827. #if defined(JSON_HEDLEY_NO_THROW)
  1828. #undef JSON_HEDLEY_NO_THROW
  1829. #endif
  1830. #if \
  1831. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1832. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1833. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1834. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1835. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1836. #elif \
  1837. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1838. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1839. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1840. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1841. #else
  1842. #define JSON_HEDLEY_NO_THROW
  1843. #endif
  1844. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1845. #undef JSON_HEDLEY_FALL_THROUGH
  1846. #endif
  1847. #if \
  1848. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1849. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1850. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1851. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1852. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1853. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1854. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1855. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1856. #elif defined(__fallthrough) /* SAL */
  1857. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1858. #else
  1859. #define JSON_HEDLEY_FALL_THROUGH
  1860. #endif
  1861. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1862. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1863. #endif
  1864. #if \
  1865. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1866. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1867. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1868. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1869. #elif defined(_Ret_notnull_) /* SAL */
  1870. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1871. #else
  1872. #define JSON_HEDLEY_RETURNS_NON_NULL
  1873. #endif
  1874. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1875. #undef JSON_HEDLEY_ARRAY_PARAM
  1876. #endif
  1877. #if \
  1878. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1879. !defined(__STDC_NO_VLA__) && \
  1880. !defined(__cplusplus) && \
  1881. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1882. !defined(JSON_HEDLEY_TINYC_VERSION)
  1883. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1884. #else
  1885. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1886. #endif
  1887. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1888. #undef JSON_HEDLEY_IS_CONSTANT
  1889. #endif
  1890. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1891. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1892. #endif
  1893. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1894. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1895. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1896. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1897. #endif
  1898. #if \
  1899. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1900. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1901. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1902. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1903. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1904. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1905. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1906. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1907. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1908. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1909. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1910. #endif
  1911. #if !defined(__cplusplus)
  1912. # if \
  1913. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1914. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1915. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1916. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1917. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1918. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1919. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1920. #if defined(__INTPTR_TYPE__)
  1921. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1922. #else
  1923. #include <stdint.h>
  1924. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1925. #endif
  1926. # elif \
  1927. ( \
  1928. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1929. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1930. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1931. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1932. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1933. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1934. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1935. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1936. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1937. #if defined(__INTPTR_TYPE__)
  1938. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1939. #else
  1940. #include <stdint.h>
  1941. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1942. #endif
  1943. # elif \
  1944. defined(JSON_HEDLEY_GCC_VERSION) || \
  1945. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1946. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1947. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1948. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1949. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1950. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1951. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1952. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1953. defined(__clang__)
  1954. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1955. sizeof(void) != \
  1956. sizeof(*( \
  1957. 1 ? \
  1958. ((void*) ((expr) * 0L) ) : \
  1959. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1960. ) \
  1961. ) \
  1962. )
  1963. # endif
  1964. #endif
  1965. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1966. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1967. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1968. #endif
  1969. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1970. #else
  1971. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1972. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1973. #endif
  1974. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1975. #endif
  1976. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1977. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1978. #endif
  1979. #if defined(JSON_HEDLEY_END_C_DECLS)
  1980. #undef JSON_HEDLEY_END_C_DECLS
  1981. #endif
  1982. #if defined(JSON_HEDLEY_C_DECL)
  1983. #undef JSON_HEDLEY_C_DECL
  1984. #endif
  1985. #if defined(__cplusplus)
  1986. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1987. #define JSON_HEDLEY_END_C_DECLS }
  1988. #define JSON_HEDLEY_C_DECL extern "C"
  1989. #else
  1990. #define JSON_HEDLEY_BEGIN_C_DECLS
  1991. #define JSON_HEDLEY_END_C_DECLS
  1992. #define JSON_HEDLEY_C_DECL
  1993. #endif
  1994. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1995. #undef JSON_HEDLEY_STATIC_ASSERT
  1996. #endif
  1997. #if \
  1998. !defined(__cplusplus) && ( \
  1999. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  2000. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  2001. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  2002. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  2003. defined(_Static_assert) \
  2004. )
  2005. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  2006. #elif \
  2007. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  2008. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  2009. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2010. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  2011. #else
  2012. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  2013. #endif
  2014. #if defined(JSON_HEDLEY_NULL)
  2015. #undef JSON_HEDLEY_NULL
  2016. #endif
  2017. #if defined(__cplusplus)
  2018. #if __cplusplus >= 201103L
  2019. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  2020. #elif defined(NULL)
  2021. #define JSON_HEDLEY_NULL NULL
  2022. #else
  2023. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  2024. #endif
  2025. #elif defined(NULL)
  2026. #define JSON_HEDLEY_NULL NULL
  2027. #else
  2028. #define JSON_HEDLEY_NULL ((void*) 0)
  2029. #endif
  2030. #if defined(JSON_HEDLEY_MESSAGE)
  2031. #undef JSON_HEDLEY_MESSAGE
  2032. #endif
  2033. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2034. # define JSON_HEDLEY_MESSAGE(msg) \
  2035. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2036. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2037. JSON_HEDLEY_PRAGMA(message msg) \
  2038. JSON_HEDLEY_DIAGNOSTIC_POP
  2039. #elif \
  2040. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  2041. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2042. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  2043. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  2044. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  2045. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  2046. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2047. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  2048. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2049. #else
  2050. # define JSON_HEDLEY_MESSAGE(msg)
  2051. #endif
  2052. #if defined(JSON_HEDLEY_WARNING)
  2053. #undef JSON_HEDLEY_WARNING
  2054. #endif
  2055. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2056. # define JSON_HEDLEY_WARNING(msg) \
  2057. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2058. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2059. JSON_HEDLEY_PRAGMA(clang warning msg) \
  2060. JSON_HEDLEY_DIAGNOSTIC_POP
  2061. #elif \
  2062. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  2063. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  2064. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2065. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  2066. #elif \
  2067. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  2068. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2069. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2070. #else
  2071. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  2072. #endif
  2073. #if defined(JSON_HEDLEY_REQUIRE)
  2074. #undef JSON_HEDLEY_REQUIRE
  2075. #endif
  2076. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  2077. #undef JSON_HEDLEY_REQUIRE_MSG
  2078. #endif
  2079. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  2080. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  2081. # define JSON_HEDLEY_REQUIRE(expr) \
  2082. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2083. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2084. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  2085. JSON_HEDLEY_DIAGNOSTIC_POP
  2086. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  2087. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2088. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2089. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  2090. JSON_HEDLEY_DIAGNOSTIC_POP
  2091. # else
  2092. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  2093. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  2094. # endif
  2095. #else
  2096. # define JSON_HEDLEY_REQUIRE(expr)
  2097. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  2098. #endif
  2099. #if defined(JSON_HEDLEY_FLAGS)
  2100. #undef JSON_HEDLEY_FLAGS
  2101. #endif
  2102. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  2103. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  2104. #else
  2105. #define JSON_HEDLEY_FLAGS
  2106. #endif
  2107. #if defined(JSON_HEDLEY_FLAGS_CAST)
  2108. #undef JSON_HEDLEY_FLAGS_CAST
  2109. #endif
  2110. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  2111. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  2112. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2113. _Pragma("warning(disable:188)") \
  2114. ((T) (expr)); \
  2115. JSON_HEDLEY_DIAGNOSTIC_POP \
  2116. }))
  2117. #else
  2118. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  2119. #endif
  2120. #if defined(JSON_HEDLEY_EMPTY_BASES)
  2121. #undef JSON_HEDLEY_EMPTY_BASES
  2122. #endif
  2123. #if \
  2124. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2125. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2126. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2127. #else
  2128. #define JSON_HEDLEY_EMPTY_BASES
  2129. #endif
  2130. /* Remaining macros are deprecated. */
  2131. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2132. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2133. #endif
  2134. #if defined(__clang__)
  2135. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2136. #else
  2137. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2138. #endif
  2139. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2140. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2141. #endif
  2142. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2143. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2144. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2145. #endif
  2146. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2147. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2148. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2149. #endif
  2150. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2151. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2152. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2153. #endif
  2154. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2155. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2156. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2157. #endif
  2158. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2159. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2160. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2161. #endif
  2162. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2163. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2164. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2165. #endif
  2166. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2167. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2168. // This file contains all internal macro definitions (except those affecting ABI)
  2169. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2170. // #include <nlohmann/detail/abi_macros.hpp>
  2171. // exclude unsupported compilers
  2172. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2173. #if defined(__clang__)
  2174. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2175. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2176. #endif
  2177. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2178. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2179. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2180. #endif
  2181. #endif
  2182. #endif
  2183. // C++ language standard detection
  2184. // if the user manually specified the used C++ version, this is skipped
  2185. #if !defined(JSON_HAS_CPP_26) && !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)
  2186. #if (defined(__cplusplus) && __cplusplus > 202302L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202302L)
  2187. #define JSON_HAS_CPP_26
  2188. #define JSON_HAS_CPP_23
  2189. #define JSON_HAS_CPP_20
  2190. #define JSON_HAS_CPP_17
  2191. #define JSON_HAS_CPP_14
  2192. #elif (defined(__cplusplus) && __cplusplus > 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202002L)
  2193. #define JSON_HAS_CPP_23
  2194. #define JSON_HAS_CPP_20
  2195. #define JSON_HAS_CPP_17
  2196. #define JSON_HAS_CPP_14
  2197. #elif (defined(__cplusplus) && __cplusplus > 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG > 201703L)
  2198. #define JSON_HAS_CPP_20
  2199. #define JSON_HAS_CPP_17
  2200. #define JSON_HAS_CPP_14
  2201. #elif (defined(__cplusplus) && __cplusplus > 201402L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2202. #define JSON_HAS_CPP_17
  2203. #define JSON_HAS_CPP_14
  2204. #elif (defined(__cplusplus) && __cplusplus > 201103L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2205. #define JSON_HAS_CPP_14
  2206. #endif
  2207. // the cpp 11 flag is always specified because it is the minimal required version
  2208. #define JSON_HAS_CPP_11
  2209. #endif
  2210. #ifdef __has_include
  2211. #if __has_include(<version>)
  2212. #include <version>
  2213. #endif
  2214. #endif
  2215. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2216. #ifdef JSON_HAS_CPP_17
  2217. #if defined(__cpp_lib_filesystem)
  2218. #define JSON_HAS_FILESYSTEM 1
  2219. #elif defined(__cpp_lib_experimental_filesystem)
  2220. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2221. #elif !defined(__has_include)
  2222. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2223. #elif __has_include(<filesystem>)
  2224. #define JSON_HAS_FILESYSTEM 1
  2225. #elif __has_include(<experimental/filesystem>)
  2226. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2227. #endif
  2228. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2229. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2230. #undef JSON_HAS_FILESYSTEM
  2231. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2232. #endif
  2233. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2234. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2235. #undef JSON_HAS_FILESYSTEM
  2236. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2237. #endif
  2238. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2239. #if defined(__clang_major__) && __clang_major__ < 7
  2240. #undef JSON_HAS_FILESYSTEM
  2241. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2242. #endif
  2243. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2244. #if defined(_MSC_VER) && _MSC_VER < 1914
  2245. #undef JSON_HAS_FILESYSTEM
  2246. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2247. #endif
  2248. // no filesystem support before iOS 13
  2249. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2250. #undef JSON_HAS_FILESYSTEM
  2251. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2252. #endif
  2253. // no filesystem support before macOS Catalina
  2254. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2255. #undef JSON_HAS_FILESYSTEM
  2256. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2257. #endif
  2258. #endif
  2259. #endif
  2260. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2261. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2262. #endif
  2263. #ifndef JSON_HAS_FILESYSTEM
  2264. #define JSON_HAS_FILESYSTEM 0
  2265. #endif
  2266. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2267. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2268. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2269. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2270. #else
  2271. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2272. #endif
  2273. #endif
  2274. #ifndef JSON_HAS_RANGES
  2275. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has a syntax error
  2276. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2277. #define JSON_HAS_RANGES 0
  2278. #elif defined(__cpp_lib_ranges)
  2279. #define JSON_HAS_RANGES 1
  2280. #else
  2281. #define JSON_HAS_RANGES 0
  2282. #endif
  2283. #endif
  2284. #ifndef JSON_HAS_STATIC_RTTI
  2285. #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
  2286. #define JSON_HAS_STATIC_RTTI 1
  2287. #else
  2288. #define JSON_HAS_STATIC_RTTI 0
  2289. #endif
  2290. #endif
  2291. #ifdef JSON_HAS_CPP_17
  2292. #define JSON_INLINE_VARIABLE inline
  2293. #else
  2294. #define JSON_INLINE_VARIABLE
  2295. #endif
  2296. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2297. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2298. #else
  2299. #define JSON_NO_UNIQUE_ADDRESS
  2300. #endif
  2301. // disable documentation warnings on clang
  2302. #if defined(__clang__)
  2303. #pragma clang diagnostic push
  2304. #pragma clang diagnostic ignored "-Wdocumentation"
  2305. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2306. #endif
  2307. // allow disabling exceptions
  2308. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2309. #define JSON_THROW(exception) throw exception
  2310. #define JSON_TRY try
  2311. #define JSON_CATCH(exception) catch(exception)
  2312. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2313. #else
  2314. #include <cstdlib>
  2315. #define JSON_THROW(exception) std::abort()
  2316. #define JSON_TRY if(true)
  2317. #define JSON_CATCH(exception) if(false)
  2318. #define JSON_INTERNAL_CATCH(exception) if(false)
  2319. #endif
  2320. // override exception macros
  2321. #if defined(JSON_THROW_USER)
  2322. #undef JSON_THROW
  2323. #define JSON_THROW JSON_THROW_USER
  2324. #endif
  2325. #if defined(JSON_TRY_USER)
  2326. #undef JSON_TRY
  2327. #define JSON_TRY JSON_TRY_USER
  2328. #endif
  2329. #if defined(JSON_CATCH_USER)
  2330. #undef JSON_CATCH
  2331. #define JSON_CATCH JSON_CATCH_USER
  2332. #undef JSON_INTERNAL_CATCH
  2333. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2334. #endif
  2335. #if defined(JSON_INTERNAL_CATCH_USER)
  2336. #undef JSON_INTERNAL_CATCH
  2337. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2338. #endif
  2339. // allow overriding assert
  2340. #if !defined(JSON_ASSERT)
  2341. #include <cassert> // assert
  2342. #define JSON_ASSERT(x) assert(x)
  2343. #endif
  2344. // allow accessing some private functions (needed by the test suite)
  2345. #if defined(JSON_TESTS_PRIVATE)
  2346. #define JSON_PRIVATE_UNLESS_TESTED public
  2347. #else
  2348. #define JSON_PRIVATE_UNLESS_TESTED private
  2349. #endif
  2350. /*!
  2351. @brief macro to briefly define a mapping between an enum and JSON
  2352. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2353. @since version 3.4.0
  2354. */
  2355. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2356. template<typename BasicJsonType> \
  2357. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2358. { \
  2359. /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
  2360. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2361. /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
  2362. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2363. auto it = std::find_if(std::begin(m), std::end(m), \
  2364. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2365. { \
  2366. return ej_pair.first == e; \
  2367. }); \
  2368. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2369. } \
  2370. template<typename BasicJsonType> \
  2371. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2372. { \
  2373. /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
  2374. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2375. /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
  2376. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2377. auto it = std::find_if(std::begin(m), std::end(m), \
  2378. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2379. { \
  2380. return ej_pair.second == j; \
  2381. }); \
  2382. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2383. }
  2384. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2385. // may be removed in the future once the class is split.
  2386. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2387. template<template<typename, typename, typename...> class ObjectType, \
  2388. template<typename, typename...> class ArrayType, \
  2389. class StringType, class BooleanType, class NumberIntegerType, \
  2390. class NumberUnsignedType, class NumberFloatType, \
  2391. template<typename> class AllocatorType, \
  2392. template<typename, typename = void> class JSONSerializer, \
  2393. class BinaryType, \
  2394. class CustomBaseClass>
  2395. #define NLOHMANN_BASIC_JSON_TPL \
  2396. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2397. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2398. AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
  2399. // Macros to simplify conversion from/to types
  2400. #define NLOHMANN_JSON_EXPAND( x ) x
  2401. #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
  2402. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2403. NLOHMANN_JSON_PASTE64, \
  2404. NLOHMANN_JSON_PASTE63, \
  2405. NLOHMANN_JSON_PASTE62, \
  2406. NLOHMANN_JSON_PASTE61, \
  2407. NLOHMANN_JSON_PASTE60, \
  2408. NLOHMANN_JSON_PASTE59, \
  2409. NLOHMANN_JSON_PASTE58, \
  2410. NLOHMANN_JSON_PASTE57, \
  2411. NLOHMANN_JSON_PASTE56, \
  2412. NLOHMANN_JSON_PASTE55, \
  2413. NLOHMANN_JSON_PASTE54, \
  2414. NLOHMANN_JSON_PASTE53, \
  2415. NLOHMANN_JSON_PASTE52, \
  2416. NLOHMANN_JSON_PASTE51, \
  2417. NLOHMANN_JSON_PASTE50, \
  2418. NLOHMANN_JSON_PASTE49, \
  2419. NLOHMANN_JSON_PASTE48, \
  2420. NLOHMANN_JSON_PASTE47, \
  2421. NLOHMANN_JSON_PASTE46, \
  2422. NLOHMANN_JSON_PASTE45, \
  2423. NLOHMANN_JSON_PASTE44, \
  2424. NLOHMANN_JSON_PASTE43, \
  2425. NLOHMANN_JSON_PASTE42, \
  2426. NLOHMANN_JSON_PASTE41, \
  2427. NLOHMANN_JSON_PASTE40, \
  2428. NLOHMANN_JSON_PASTE39, \
  2429. NLOHMANN_JSON_PASTE38, \
  2430. NLOHMANN_JSON_PASTE37, \
  2431. NLOHMANN_JSON_PASTE36, \
  2432. NLOHMANN_JSON_PASTE35, \
  2433. NLOHMANN_JSON_PASTE34, \
  2434. NLOHMANN_JSON_PASTE33, \
  2435. NLOHMANN_JSON_PASTE32, \
  2436. NLOHMANN_JSON_PASTE31, \
  2437. NLOHMANN_JSON_PASTE30, \
  2438. NLOHMANN_JSON_PASTE29, \
  2439. NLOHMANN_JSON_PASTE28, \
  2440. NLOHMANN_JSON_PASTE27, \
  2441. NLOHMANN_JSON_PASTE26, \
  2442. NLOHMANN_JSON_PASTE25, \
  2443. NLOHMANN_JSON_PASTE24, \
  2444. NLOHMANN_JSON_PASTE23, \
  2445. NLOHMANN_JSON_PASTE22, \
  2446. NLOHMANN_JSON_PASTE21, \
  2447. NLOHMANN_JSON_PASTE20, \
  2448. NLOHMANN_JSON_PASTE19, \
  2449. NLOHMANN_JSON_PASTE18, \
  2450. NLOHMANN_JSON_PASTE17, \
  2451. NLOHMANN_JSON_PASTE16, \
  2452. NLOHMANN_JSON_PASTE15, \
  2453. NLOHMANN_JSON_PASTE14, \
  2454. NLOHMANN_JSON_PASTE13, \
  2455. NLOHMANN_JSON_PASTE12, \
  2456. NLOHMANN_JSON_PASTE11, \
  2457. NLOHMANN_JSON_PASTE10, \
  2458. NLOHMANN_JSON_PASTE9, \
  2459. NLOHMANN_JSON_PASTE8, \
  2460. NLOHMANN_JSON_PASTE7, \
  2461. NLOHMANN_JSON_PASTE6, \
  2462. NLOHMANN_JSON_PASTE5, \
  2463. NLOHMANN_JSON_PASTE4, \
  2464. NLOHMANN_JSON_PASTE3, \
  2465. NLOHMANN_JSON_PASTE2, \
  2466. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2467. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2468. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2469. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2470. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2471. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2472. #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)
  2473. #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)
  2474. #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)
  2475. #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)
  2476. #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)
  2477. #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)
  2478. #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)
  2479. #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)
  2480. #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)
  2481. #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)
  2482. #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)
  2483. #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)
  2484. #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)
  2485. #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)
  2486. #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)
  2487. #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)
  2488. #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)
  2489. #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)
  2490. #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)
  2491. #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)
  2492. #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)
  2493. #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)
  2494. #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)
  2495. #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)
  2496. #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)
  2497. #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)
  2498. #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)
  2499. #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)
  2500. #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)
  2501. #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)
  2502. #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)
  2503. #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)
  2504. #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)
  2505. #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)
  2506. #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)
  2507. #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)
  2508. #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)
  2509. #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)
  2510. #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)
  2511. #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)
  2512. #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)
  2513. #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)
  2514. #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)
  2515. #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)
  2516. #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)
  2517. #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)
  2518. #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)
  2519. #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)
  2520. #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)
  2521. #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)
  2522. #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)
  2523. #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)
  2524. #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)
  2525. #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)
  2526. #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)
  2527. #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)
  2528. #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)
  2529. #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)
  2530. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2531. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2532. #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;
  2533. /*!
  2534. @brief macro
  2535. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2536. @since version 3.9.0
  2537. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
  2538. */
  2539. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2540. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2541. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2542. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2543. friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2544. /*!
  2545. @brief macro
  2546. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT
  2547. @since version 3.11.0
  2548. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
  2549. */
  2550. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2551. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2552. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2553. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2554. 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__)) }
  2555. /*!
  2556. @brief macro
  2557. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE
  2558. @since version 3.11.3
  2559. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
  2560. */
  2561. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2562. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2563. friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2564. /*!
  2565. @brief macro
  2566. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2567. @since version 3.9.0
  2568. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
  2569. */
  2570. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2571. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2572. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2573. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2574. void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2575. /*!
  2576. @brief macro
  2577. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT
  2578. @since version 3.11.0
  2579. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
  2580. */
  2581. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2582. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2583. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2584. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2585. 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__)) }
  2586. /*!
  2587. @brief macro
  2588. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
  2589. @since version 3.11.3
  2590. @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
  2591. */
  2592. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2593. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2594. void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2595. /*!
  2596. @brief macro
  2597. @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE
  2598. @since version 3.12.0
  2599. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2600. */
  2601. #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Type, BaseType, ...) \
  2602. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2603. 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__)) } \
  2604. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2605. 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__)) }
  2606. /*!
  2607. @brief macro
  2608. @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT
  2609. @since version 3.12.0
  2610. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2611. */
  2612. #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
  2613. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2614. 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__)) } \
  2615. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2616. 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__)) }
  2617. /*!
  2618. @brief macro
  2619. @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE
  2620. @since version 3.12.0
  2621. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2622. */
  2623. #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
  2624. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2625. 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__)) }
  2626. /*!
  2627. @brief macro
  2628. @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE
  2629. @since version 3.12.0
  2630. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2631. */
  2632. #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(Type, BaseType, ...) \
  2633. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2634. 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__)) } \
  2635. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2636. 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__)) }
  2637. /*!
  2638. @brief macro
  2639. @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT
  2640. @since version 3.12.0
  2641. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2642. */
  2643. #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
  2644. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2645. 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__)) } \
  2646. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2647. 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__)) }
  2648. /*!
  2649. @brief macro
  2650. @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
  2651. @since version 3.12.0
  2652. @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
  2653. */
  2654. #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
  2655. template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
  2656. 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__)) }
  2657. // inspired from https://stackoverflow.com/a/26745591
  2658. // allows calling any std function as if (e.g., with begin):
  2659. // using std::begin; begin(x);
  2660. //
  2661. // it allows using the detected idiom to retrieve the return type
  2662. // of such an expression
  2663. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2664. namespace detail { \
  2665. using std::std_name; \
  2666. \
  2667. template<typename... T> \
  2668. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2669. } \
  2670. \
  2671. namespace detail2 { \
  2672. struct std_name##_tag \
  2673. { \
  2674. }; \
  2675. \
  2676. template<typename... T> \
  2677. std_name##_tag std_name(T&&...); \
  2678. \
  2679. template<typename... T> \
  2680. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2681. \
  2682. template<typename... T> \
  2683. struct would_call_std_##std_name \
  2684. { \
  2685. static constexpr auto const value = ::nlohmann::detail:: \
  2686. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2687. }; \
  2688. } /* namespace detail2 */ \
  2689. \
  2690. template<typename... T> \
  2691. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2692. { \
  2693. }
  2694. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2695. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2696. #endif
  2697. #if JSON_USE_IMPLICIT_CONVERSIONS
  2698. #define JSON_EXPLICIT
  2699. #else
  2700. #define JSON_EXPLICIT explicit
  2701. #endif
  2702. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2703. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2704. #endif
  2705. #ifndef JSON_USE_GLOBAL_UDLS
  2706. #define JSON_USE_GLOBAL_UDLS 1
  2707. #endif
  2708. #if JSON_HAS_THREE_WAY_COMPARISON
  2709. #include <compare> // partial_ordering
  2710. #endif
  2711. NLOHMANN_JSON_NAMESPACE_BEGIN
  2712. namespace detail
  2713. {
  2714. ///////////////////////////
  2715. // JSON type enumeration //
  2716. ///////////////////////////
  2717. /*!
  2718. @brief the JSON type enumeration
  2719. This enumeration collects the different JSON types. It is internally used to
  2720. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2721. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2722. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2723. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2724. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2725. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2726. @ref basic_json::is_structured() rely on it.
  2727. @note There are three enumeration entries (number_integer, number_unsigned, and
  2728. number_float), because the library distinguishes these three types for numbers:
  2729. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2730. @ref basic_json::number_integer_t is used for signed integers, and
  2731. @ref basic_json::number_float_t is used for floating-point numbers or to
  2732. approximate integers which do not fit in the limits of their respective type.
  2733. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2734. value with the default value for a given type
  2735. @since version 1.0.0
  2736. */
  2737. enum class value_t : std::uint8_t
  2738. {
  2739. null, ///< null value
  2740. object, ///< object (unordered set of name/value pairs)
  2741. array, ///< array (ordered collection of values)
  2742. string, ///< string value
  2743. boolean, ///< boolean value
  2744. number_integer, ///< number value (signed integer)
  2745. number_unsigned, ///< number value (unsigned integer)
  2746. number_float, ///< number value (floating-point)
  2747. binary, ///< binary array (ordered collection of bytes)
  2748. discarded ///< discarded by the parser callback function
  2749. };
  2750. /*!
  2751. @brief comparison operator for JSON types
  2752. Returns an ordering that is similar to Python:
  2753. - order: null < boolean < number < object < array < string < binary
  2754. - furthermore, each type is not smaller than itself
  2755. - discarded values are not comparable
  2756. - binary is represented as a b"" string in python and directly comparable to a
  2757. string; however, making a binary array directly comparable with a string would
  2758. be surprising behavior in a JSON file.
  2759. @since version 1.0.0
  2760. */
  2761. #if JSON_HAS_THREE_WAY_COMPARISON
  2762. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2763. #else
  2764. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2765. #endif
  2766. {
  2767. static constexpr std::array<std::uint8_t, 9> order = {{
  2768. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2769. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2770. 6 /* binary */
  2771. }
  2772. };
  2773. const auto l_index = static_cast<std::size_t>(lhs);
  2774. const auto r_index = static_cast<std::size_t>(rhs);
  2775. #if JSON_HAS_THREE_WAY_COMPARISON
  2776. if (l_index < order.size() && r_index < order.size())
  2777. {
  2778. return order[l_index] <=> order[r_index]; // *NOPAD*
  2779. }
  2780. return std::partial_ordering::unordered;
  2781. #else
  2782. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2783. #endif
  2784. }
  2785. // GCC selects the built-in operator< over an operator rewritten from
  2786. // a user-defined spaceship operator
  2787. // Clang, MSVC, and ICC select the rewritten candidate
  2788. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2789. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2790. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2791. {
  2792. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2793. }
  2794. #endif
  2795. } // namespace detail
  2796. NLOHMANN_JSON_NAMESPACE_END
  2797. // #include <nlohmann/detail/string_escape.hpp>
  2798. // __ _____ _____ _____
  2799. // __| | __| | | | JSON for Modern C++
  2800. // | | |__ | | | | | | version 3.12.0
  2801. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2802. //
  2803. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  2804. // SPDX-License-Identifier: MIT
  2805. // #include <nlohmann/detail/abi_macros.hpp>
  2806. NLOHMANN_JSON_NAMESPACE_BEGIN
  2807. namespace detail
  2808. {
  2809. /*!
  2810. @brief replace all occurrences of a substring by another string
  2811. @param[in,out] s the string to manipulate; changed so that all
  2812. occurrences of @a f are replaced with @a t
  2813. @param[in] f the substring to replace with @a t
  2814. @param[in] t the string to replace @a f
  2815. @pre The search string @a f must not be empty. **This precondition is
  2816. enforced with an assertion.**
  2817. @since version 2.0.0
  2818. */
  2819. template<typename StringType>
  2820. inline void replace_substring(StringType& s, const StringType& f,
  2821. const StringType& t)
  2822. {
  2823. JSON_ASSERT(!f.empty());
  2824. for (auto pos = s.find(f); // find the first occurrence of f
  2825. pos != StringType::npos; // make sure f was found
  2826. s.replace(pos, f.size(), t), // replace with t, and
  2827. pos = s.find(f, pos + t.size())) // find the next occurrence of f
  2828. {}
  2829. }
  2830. /*!
  2831. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2832. * @param[in] s string to escape
  2833. * @return escaped string
  2834. *
  2835. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2836. */
  2837. template<typename StringType>
  2838. inline StringType escape(StringType s)
  2839. {
  2840. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2841. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2842. return s;
  2843. }
  2844. /*!
  2845. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2846. * @param[in] s string to unescape
  2847. * @return unescaped string
  2848. *
  2849. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2850. */
  2851. template<typename StringType>
  2852. inline void unescape(StringType& s)
  2853. {
  2854. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2855. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2856. }
  2857. } // namespace detail
  2858. NLOHMANN_JSON_NAMESPACE_END
  2859. // #include <nlohmann/detail/input/position_t.hpp>
  2860. // __ _____ _____ _____
  2861. // __| | __| | | | JSON for Modern C++
  2862. // | | |__ | | | | | | version 3.12.0
  2863. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2864. //
  2865. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  2866. // SPDX-License-Identifier: MIT
  2867. #include <cstddef> // size_t
  2868. // #include <nlohmann/detail/abi_macros.hpp>
  2869. NLOHMANN_JSON_NAMESPACE_BEGIN
  2870. namespace detail
  2871. {
  2872. /// struct to capture the start position of the current token
  2873. struct position_t
  2874. {
  2875. /// the total number of characters read
  2876. std::size_t chars_read_total = 0;
  2877. /// the number of characters read in the current line
  2878. std::size_t chars_read_current_line = 0;
  2879. /// the number of lines read
  2880. std::size_t lines_read = 0;
  2881. /// conversion to size_t to preserve SAX interface
  2882. constexpr operator size_t() const
  2883. {
  2884. return chars_read_total;
  2885. }
  2886. };
  2887. } // namespace detail
  2888. NLOHMANN_JSON_NAMESPACE_END
  2889. // #include <nlohmann/detail/macro_scope.hpp>
  2890. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2891. // __ _____ _____ _____
  2892. // __| | __| | | | JSON for Modern C++
  2893. // | | |__ | | | | | | version 3.12.0
  2894. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2895. //
  2896. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  2897. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2898. // SPDX-License-Identifier: MIT
  2899. #include <array> // array
  2900. #include <cstddef> // size_t
  2901. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2902. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2903. // #include <nlohmann/detail/macro_scope.hpp>
  2904. NLOHMANN_JSON_NAMESPACE_BEGIN
  2905. namespace detail
  2906. {
  2907. template<typename T>
  2908. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2909. #ifdef JSON_HAS_CPP_14
  2910. // the following utilities are natively available in C++14
  2911. using std::enable_if_t;
  2912. using std::index_sequence;
  2913. using std::make_index_sequence;
  2914. using std::index_sequence_for;
  2915. #else
  2916. // alias templates to reduce boilerplate
  2917. template<bool B, typename T = void>
  2918. using enable_if_t = typename std::enable_if<B, T>::type;
  2919. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2920. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2921. //// START OF CODE FROM GOOGLE ABSEIL
  2922. // integer_sequence
  2923. //
  2924. // Class template representing a compile-time integer sequence. An instantiation
  2925. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2926. // type through its template arguments (which is a common need when
  2927. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2928. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2929. //
  2930. // Example:
  2931. //
  2932. // template< class T, T... Ints >
  2933. // void user_function(integer_sequence<T, Ints...>);
  2934. //
  2935. // int main()
  2936. // {
  2937. // // user_function's `T` will be deduced to `int` and `Ints...`
  2938. // // will be deduced to `0, 1, 2, 3, 4`.
  2939. // user_function(make_integer_sequence<int, 5>());
  2940. // }
  2941. template <typename T, T... Ints>
  2942. struct integer_sequence
  2943. {
  2944. using value_type = T;
  2945. static constexpr std::size_t size() noexcept
  2946. {
  2947. return sizeof...(Ints);
  2948. }
  2949. };
  2950. // index_sequence
  2951. //
  2952. // A helper template for an `integer_sequence` of `size_t`,
  2953. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2954. // `std::index_sequence`.
  2955. template <size_t... Ints>
  2956. using index_sequence = integer_sequence<size_t, Ints...>;
  2957. namespace utility_internal
  2958. {
  2959. template <typename Seq, size_t SeqSize, size_t Rem>
  2960. struct Extend;
  2961. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2962. template <typename T, T... Ints, size_t SeqSize>
  2963. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2964. {
  2965. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2966. };
  2967. template <typename T, T... Ints, size_t SeqSize>
  2968. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2969. {
  2970. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2971. };
  2972. // Recursion helper for 'make_integer_sequence<T, N>'.
  2973. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2974. template <typename T, size_t N>
  2975. struct Gen
  2976. {
  2977. using type =
  2978. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2979. };
  2980. template <typename T>
  2981. struct Gen<T, 0>
  2982. {
  2983. using type = integer_sequence<T>;
  2984. };
  2985. } // namespace utility_internal
  2986. // Compile-time sequences of integers
  2987. // make_integer_sequence
  2988. //
  2989. // This template alias is equivalent to
  2990. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2991. // replacement for C++14's `std::make_integer_sequence`.
  2992. template <typename T, T N>
  2993. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2994. // make_index_sequence
  2995. //
  2996. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2997. // and is designed to be a drop-in replacement for C++14's
  2998. // `std::make_index_sequence`.
  2999. template <size_t N>
  3000. using make_index_sequence = make_integer_sequence<size_t, N>;
  3001. // index_sequence_for
  3002. //
  3003. // Converts a typename pack into an index sequence of the same length, and
  3004. // is designed to be a drop-in replacement for C++14's
  3005. // `std::index_sequence_for()`
  3006. template <typename... Ts>
  3007. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  3008. //// END OF CODE FROM GOOGLE ABSEIL
  3009. #endif
  3010. // dispatch utility (taken from ranges-v3)
  3011. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  3012. template<> struct priority_tag<0> {};
  3013. // taken from ranges-v3
  3014. template<typename T>
  3015. struct static_const
  3016. {
  3017. static JSON_INLINE_VARIABLE constexpr T value{};
  3018. };
  3019. #ifndef JSON_HAS_CPP_17
  3020. template<typename T>
  3021. constexpr T static_const<T>::value;
  3022. #endif
  3023. template<typename T, typename... Args>
  3024. constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  3025. {
  3026. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  3027. }
  3028. } // namespace detail
  3029. NLOHMANN_JSON_NAMESPACE_END
  3030. // #include <nlohmann/detail/meta/type_traits.hpp>
  3031. // __ _____ _____ _____
  3032. // __| | __| | | | JSON for Modern C++
  3033. // | | |__ | | | | | | version 3.12.0
  3034. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3035. //
  3036. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  3037. // SPDX-License-Identifier: MIT
  3038. #include <limits> // numeric_limits
  3039. #include <string> // char_traits
  3040. #include <tuple> // tuple
  3041. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  3042. #include <utility> // declval
  3043. #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
  3044. #include <cstddef> // byte
  3045. #endif
  3046. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  3047. // __ _____ _____ _____
  3048. // __| | __| | | | JSON for Modern C++
  3049. // | | |__ | | | | | | version 3.12.0
  3050. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3051. //
  3052. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  3053. // SPDX-License-Identifier: MIT
  3054. #include <iterator> // random_access_iterator_tag
  3055. // #include <nlohmann/detail/abi_macros.hpp>
  3056. // #include <nlohmann/detail/meta/void_t.hpp>
  3057. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3058. NLOHMANN_JSON_NAMESPACE_BEGIN
  3059. namespace detail
  3060. {
  3061. template<typename It, typename = void>
  3062. struct iterator_types {};
  3063. template<typename It>
  3064. struct iterator_types <
  3065. It,
  3066. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  3067. typename It::reference, typename It::iterator_category >>
  3068. {
  3069. using difference_type = typename It::difference_type;
  3070. using value_type = typename It::value_type;
  3071. using pointer = typename It::pointer;
  3072. using reference = typename It::reference;
  3073. using iterator_category = typename It::iterator_category;
  3074. };
  3075. // This is required as some compilers implement std::iterator_traits in a way that
  3076. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  3077. template<typename T, typename = void>
  3078. struct iterator_traits
  3079. {
  3080. };
  3081. template<typename T>
  3082. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  3083. : iterator_types<T>
  3084. {
  3085. };
  3086. template<typename T>
  3087. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  3088. {
  3089. using iterator_category = std::random_access_iterator_tag;
  3090. using value_type = T;
  3091. using difference_type = ptrdiff_t;
  3092. using pointer = T*;
  3093. using reference = T&;
  3094. };
  3095. } // namespace detail
  3096. NLOHMANN_JSON_NAMESPACE_END
  3097. // #include <nlohmann/detail/macro_scope.hpp>
  3098. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  3099. // __ _____ _____ _____
  3100. // __| | __| | | | JSON for Modern C++
  3101. // | | |__ | | | | | | version 3.12.0
  3102. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3103. //
  3104. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  3105. // SPDX-License-Identifier: MIT
  3106. // #include <nlohmann/detail/macro_scope.hpp>
  3107. NLOHMANN_JSON_NAMESPACE_BEGIN
  3108. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  3109. NLOHMANN_JSON_NAMESPACE_END
  3110. // #include <nlohmann/detail/meta/call_std/end.hpp>
  3111. // __ _____ _____ _____
  3112. // __| | __| | | | JSON for Modern C++
  3113. // | | |__ | | | | | | version 3.12.0
  3114. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3115. //
  3116. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  3117. // SPDX-License-Identifier: MIT
  3118. // #include <nlohmann/detail/macro_scope.hpp>
  3119. NLOHMANN_JSON_NAMESPACE_BEGIN
  3120. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  3121. NLOHMANN_JSON_NAMESPACE_END
  3122. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3123. // #include <nlohmann/detail/meta/detected.hpp>
  3124. // #include <nlohmann/json_fwd.hpp>
  3125. // __ _____ _____ _____
  3126. // __| | __| | | | JSON for Modern C++
  3127. // | | |__ | | | | | | version 3.12.0
  3128. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3129. //
  3130. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  3131. // SPDX-License-Identifier: MIT
  3132. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3133. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3134. #include <cstdint> // int64_t, uint64_t
  3135. #include <map> // map
  3136. #include <memory> // allocator
  3137. #include <string> // string
  3138. #include <vector> // vector
  3139. // #include <nlohmann/detail/abi_macros.hpp>
  3140. /*!
  3141. @brief namespace for Niels Lohmann
  3142. @see https://github.com/nlohmann
  3143. @since version 1.0.0
  3144. */
  3145. NLOHMANN_JSON_NAMESPACE_BEGIN
  3146. /*!
  3147. @brief default JSONSerializer template argument
  3148. This serializer ignores the template arguments and uses ADL
  3149. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3150. for serialization.
  3151. */
  3152. template<typename T = void, typename SFINAE = void>
  3153. struct adl_serializer;
  3154. /// a class to store JSON values
  3155. /// @sa https://json.nlohmann.me/api/basic_json/
  3156. template<template<typename U, typename V, typename... Args> class ObjectType =
  3157. std::map,
  3158. template<typename U, typename... Args> class ArrayType = std::vector,
  3159. class StringType = std::string, class BooleanType = bool,
  3160. class NumberIntegerType = std::int64_t,
  3161. class NumberUnsignedType = std::uint64_t,
  3162. class NumberFloatType = double,
  3163. template<typename U> class AllocatorType = std::allocator,
  3164. template<typename T, typename SFINAE = void> class JSONSerializer =
  3165. adl_serializer,
  3166. class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
  3167. class CustomBaseClass = void>
  3168. class basic_json;
  3169. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3170. /// @sa https://json.nlohmann.me/api/json_pointer/
  3171. template<typename RefStringType>
  3172. class json_pointer;
  3173. /*!
  3174. @brief default specialization
  3175. @sa https://json.nlohmann.me/api/json/
  3176. */
  3177. using json = basic_json<>;
  3178. /// @brief a minimal map-like container that preserves insertion order
  3179. /// @sa https://json.nlohmann.me/api/ordered_map/
  3180. template<class Key, class T, class IgnoredLess, class Allocator>
  3181. struct ordered_map;
  3182. /// @brief specialization that maintains the insertion order of object keys
  3183. /// @sa https://json.nlohmann.me/api/ordered_json/
  3184. using ordered_json = basic_json<nlohmann::ordered_map>;
  3185. NLOHMANN_JSON_NAMESPACE_END
  3186. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3187. NLOHMANN_JSON_NAMESPACE_BEGIN
  3188. /*!
  3189. @brief detail namespace with internal helper functions
  3190. This namespace collects functions that should not be exposed,
  3191. implementations of some @ref basic_json methods, and meta-programming helpers.
  3192. @since version 2.1.0
  3193. */
  3194. namespace detail
  3195. {
  3196. /////////////
  3197. // helpers //
  3198. /////////////
  3199. // Note to maintainers:
  3200. //
  3201. // Every trait in this file expects a non-CV-qualified type.
  3202. // The only exceptions are in the 'aliases for detected' section
  3203. // (i.e., those of the form: decltype(T::member_function(std::declval<T>())))
  3204. //
  3205. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3206. // (e.g., to_json(BasicJsonType&, const T&))
  3207. template<typename> struct is_basic_json : std::false_type {};
  3208. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3209. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3210. // used by exceptions create() member functions
  3211. // true_type for the pointer to possibly cv-qualified basic_json or std::nullptr_t
  3212. // false_type otherwise
  3213. template<typename BasicJsonContext>
  3214. struct is_basic_json_context :
  3215. std::integral_constant < bool,
  3216. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3217. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3218. {};
  3219. //////////////////////
  3220. // json_ref helpers //
  3221. //////////////////////
  3222. template<typename>
  3223. class json_ref;
  3224. template<typename>
  3225. struct is_json_ref : std::false_type {};
  3226. template<typename T>
  3227. struct is_json_ref<json_ref<T>> : std::true_type {};
  3228. //////////////////////////
  3229. // aliases for detected //
  3230. //////////////////////////
  3231. template<typename T>
  3232. using mapped_type_t = typename T::mapped_type;
  3233. template<typename T>
  3234. using key_type_t = typename T::key_type;
  3235. template<typename T>
  3236. using value_type_t = typename T::value_type;
  3237. template<typename T>
  3238. using difference_type_t = typename T::difference_type;
  3239. template<typename T>
  3240. using pointer_t = typename T::pointer;
  3241. template<typename T>
  3242. using reference_t = typename T::reference;
  3243. template<typename T>
  3244. using iterator_category_t = typename T::iterator_category;
  3245. template<typename T, typename... Args>
  3246. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3247. template<typename T, typename... Args>
  3248. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3249. template<typename T, typename U>
  3250. using get_template_function = decltype(std::declval<T>().template get<U>());
  3251. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3252. template<typename BasicJsonType, typename T, typename = void>
  3253. struct has_from_json : std::false_type {};
  3254. // trait checking if j.get<T> is valid
  3255. // use this trait instead of std::is_constructible or std::is_convertible,
  3256. // both rely on, or make use of implicit conversions, and thus fail when T
  3257. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3258. template <typename BasicJsonType, typename T>
  3259. struct is_getable
  3260. {
  3261. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3262. };
  3263. template<typename BasicJsonType, typename T>
  3264. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3265. {
  3266. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3267. static constexpr bool value =
  3268. is_detected_exact<void, from_json_function, serializer,
  3269. const BasicJsonType&, T&>::value;
  3270. };
  3271. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3272. // this overload is used for non-default-constructible user-defined-types
  3273. template<typename BasicJsonType, typename T, typename = void>
  3274. struct has_non_default_from_json : std::false_type {};
  3275. template<typename BasicJsonType, typename T>
  3276. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3277. {
  3278. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3279. static constexpr bool value =
  3280. is_detected_exact<T, from_json_function, serializer,
  3281. const BasicJsonType&>::value;
  3282. };
  3283. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3284. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3285. template<typename BasicJsonType, typename T, typename = void>
  3286. struct has_to_json : std::false_type {};
  3287. template<typename BasicJsonType, typename T>
  3288. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3289. {
  3290. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3291. static constexpr bool value =
  3292. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3293. T>::value;
  3294. };
  3295. template<typename T>
  3296. using detect_key_compare = typename T::key_compare;
  3297. template<typename T>
  3298. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3299. // obtains the actual object key comparator
  3300. template<typename BasicJsonType>
  3301. struct actual_object_comparator
  3302. {
  3303. using object_t = typename BasicJsonType::object_t;
  3304. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3305. using type = typename std::conditional < has_key_compare<object_t>::value,
  3306. typename object_t::key_compare, object_comparator_t>::type;
  3307. };
  3308. template<typename BasicJsonType>
  3309. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3310. /////////////////
  3311. // char_traits //
  3312. /////////////////
  3313. // Primary template of char_traits calls std char_traits
  3314. template<typename T>
  3315. struct char_traits : std::char_traits<T>
  3316. {};
  3317. // Explicitly define char traits for unsigned char since it is not standard
  3318. template<>
  3319. struct char_traits<unsigned char> : std::char_traits<char>
  3320. {
  3321. using char_type = unsigned char;
  3322. using int_type = uint64_t;
  3323. // Redefine to_int_type function
  3324. static int_type to_int_type(char_type c) noexcept
  3325. {
  3326. return static_cast<int_type>(c);
  3327. }
  3328. static char_type to_char_type(int_type i) noexcept
  3329. {
  3330. return static_cast<char_type>(i);
  3331. }
  3332. static constexpr int_type eof() noexcept
  3333. {
  3334. return static_cast<int_type>(std::char_traits<char>::eof());
  3335. }
  3336. };
  3337. // Explicitly define char traits for signed char since it is not standard
  3338. template<>
  3339. struct char_traits<signed char> : std::char_traits<char>
  3340. {
  3341. using char_type = signed char;
  3342. using int_type = uint64_t;
  3343. // Redefine to_int_type function
  3344. static int_type to_int_type(char_type c) noexcept
  3345. {
  3346. return static_cast<int_type>(c);
  3347. }
  3348. static char_type to_char_type(int_type i) noexcept
  3349. {
  3350. return static_cast<char_type>(i);
  3351. }
  3352. static constexpr int_type eof() noexcept
  3353. {
  3354. return static_cast<int_type>(std::char_traits<char>::eof());
  3355. }
  3356. };
  3357. #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
  3358. template<>
  3359. struct char_traits<std::byte> : std::char_traits<char>
  3360. {
  3361. using char_type = std::byte;
  3362. using int_type = uint64_t;
  3363. static int_type to_int_type(char_type c) noexcept
  3364. {
  3365. return static_cast<int_type>(std::to_integer<unsigned char>(c));
  3366. }
  3367. static char_type to_char_type(int_type i) noexcept
  3368. {
  3369. return std::byte(static_cast<unsigned char>(i));
  3370. }
  3371. static constexpr int_type eof() noexcept
  3372. {
  3373. return static_cast<int_type>(std::char_traits<char>::eof());
  3374. }
  3375. };
  3376. #endif
  3377. ///////////////////
  3378. // is_ functions //
  3379. ///////////////////
  3380. // https://en.cppreference.com/w/cpp/types/conjunction
  3381. template<class...> struct conjunction : std::true_type { };
  3382. template<class B> struct conjunction<B> : B { };
  3383. template<class B, class... Bn>
  3384. struct conjunction<B, Bn...>
  3385. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3386. // https://en.cppreference.com/w/cpp/types/negation
  3387. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3388. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3389. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3390. // This causes compile errors in e.g., Clang 3.5 or GCC 4.9.
  3391. template <typename T>
  3392. struct is_default_constructible : std::is_default_constructible<T> {};
  3393. template <typename T1, typename T2>
  3394. struct is_default_constructible<std::pair<T1, T2>>
  3395. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3396. template <typename T1, typename T2>
  3397. struct is_default_constructible<const std::pair<T1, T2>>
  3398. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3399. template <typename... Ts>
  3400. struct is_default_constructible<std::tuple<Ts...>>
  3401. : conjunction<is_default_constructible<Ts>...> {};
  3402. template <typename... Ts>
  3403. struct is_default_constructible<const std::tuple<Ts...>>
  3404. : conjunction<is_default_constructible<Ts>...> {};
  3405. template <typename T, typename... Args>
  3406. struct is_constructible : std::is_constructible<T, Args...> {};
  3407. template <typename T1, typename T2>
  3408. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3409. template <typename T1, typename T2>
  3410. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3411. template <typename... Ts>
  3412. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3413. template <typename... Ts>
  3414. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3415. template<typename T, typename = void>
  3416. struct is_iterator_traits : std::false_type {};
  3417. template<typename T>
  3418. struct is_iterator_traits<iterator_traits<T>>
  3419. {
  3420. private:
  3421. using traits = iterator_traits<T>;
  3422. public:
  3423. static constexpr auto value =
  3424. is_detected<value_type_t, traits>::value &&
  3425. is_detected<difference_type_t, traits>::value &&
  3426. is_detected<pointer_t, traits>::value &&
  3427. is_detected<iterator_category_t, traits>::value &&
  3428. is_detected<reference_t, traits>::value;
  3429. };
  3430. template<typename T>
  3431. struct is_range
  3432. {
  3433. private:
  3434. using t_ref = typename std::add_lvalue_reference<T>::type;
  3435. using iterator = detected_t<result_of_begin, t_ref>;
  3436. using sentinel = detected_t<result_of_end, t_ref>;
  3437. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3438. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3439. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3440. static constexpr auto is_iterator_begin =
  3441. is_iterator_traits<iterator_traits<iterator>>::value;
  3442. public:
  3443. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3444. };
  3445. template<typename R>
  3446. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3447. template<typename T>
  3448. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3449. // The following implementation of is_complete_type is taken from
  3450. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3451. // and is written by Xiang Fan who agreed to use it in this library.
  3452. template<typename T, typename = void>
  3453. struct is_complete_type : std::false_type {};
  3454. template<typename T>
  3455. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3456. template<typename BasicJsonType, typename CompatibleObjectType,
  3457. typename = void>
  3458. struct is_compatible_object_type_impl : std::false_type {};
  3459. template<typename BasicJsonType, typename CompatibleObjectType>
  3460. struct is_compatible_object_type_impl <
  3461. BasicJsonType, CompatibleObjectType,
  3462. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3463. is_detected<key_type_t, CompatibleObjectType>::value >>
  3464. {
  3465. using object_t = typename BasicJsonType::object_t;
  3466. // macOS's is_constructible does not play well with nonesuch...
  3467. static constexpr bool value =
  3468. is_constructible<typename object_t::key_type,
  3469. typename CompatibleObjectType::key_type>::value &&
  3470. is_constructible<typename object_t::mapped_type,
  3471. typename CompatibleObjectType::mapped_type>::value;
  3472. };
  3473. template<typename BasicJsonType, typename CompatibleObjectType>
  3474. struct is_compatible_object_type
  3475. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3476. template<typename BasicJsonType, typename ConstructibleObjectType,
  3477. typename = void>
  3478. struct is_constructible_object_type_impl : std::false_type {};
  3479. template<typename BasicJsonType, typename ConstructibleObjectType>
  3480. struct is_constructible_object_type_impl <
  3481. BasicJsonType, ConstructibleObjectType,
  3482. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3483. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3484. {
  3485. using object_t = typename BasicJsonType::object_t;
  3486. static constexpr bool value =
  3487. (is_default_constructible<ConstructibleObjectType>::value &&
  3488. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3489. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3490. (is_constructible<typename ConstructibleObjectType::key_type,
  3491. typename object_t::key_type>::value &&
  3492. std::is_same <
  3493. typename object_t::mapped_type,
  3494. typename ConstructibleObjectType::mapped_type >::value)) ||
  3495. (has_from_json<BasicJsonType,
  3496. typename ConstructibleObjectType::mapped_type>::value ||
  3497. has_non_default_from_json <
  3498. BasicJsonType,
  3499. typename ConstructibleObjectType::mapped_type >::value);
  3500. };
  3501. template<typename BasicJsonType, typename ConstructibleObjectType>
  3502. struct is_constructible_object_type
  3503. : is_constructible_object_type_impl<BasicJsonType,
  3504. ConstructibleObjectType> {};
  3505. template<typename BasicJsonType, typename CompatibleStringType>
  3506. struct is_compatible_string_type
  3507. {
  3508. static constexpr auto value =
  3509. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3510. };
  3511. template<typename BasicJsonType, typename ConstructibleStringType>
  3512. struct is_constructible_string_type
  3513. {
  3514. // launder type through decltype() to fix compilation failure on ICPC
  3515. #ifdef __INTEL_COMPILER
  3516. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3517. #else
  3518. using laundered_type = ConstructibleStringType;
  3519. #endif
  3520. static constexpr auto value =
  3521. conjunction <
  3522. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3523. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3524. value_type_t, laundered_type >>::value;
  3525. };
  3526. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3527. struct is_compatible_array_type_impl : std::false_type {};
  3528. template<typename BasicJsonType, typename CompatibleArrayType>
  3529. struct is_compatible_array_type_impl <
  3530. BasicJsonType, CompatibleArrayType,
  3531. enable_if_t <
  3532. is_detected<iterator_t, CompatibleArrayType>::value&&
  3533. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3534. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3535. // c.f. https://github.com/nlohmann/json/pull/3073
  3536. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3537. {
  3538. static constexpr bool value =
  3539. is_constructible<BasicJsonType,
  3540. range_value_t<CompatibleArrayType>>::value;
  3541. };
  3542. template<typename BasicJsonType, typename CompatibleArrayType>
  3543. struct is_compatible_array_type
  3544. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3545. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3546. struct is_constructible_array_type_impl : std::false_type {};
  3547. template<typename BasicJsonType, typename ConstructibleArrayType>
  3548. struct is_constructible_array_type_impl <
  3549. BasicJsonType, ConstructibleArrayType,
  3550. enable_if_t<std::is_same<ConstructibleArrayType,
  3551. typename BasicJsonType::value_type>::value >>
  3552. : std::true_type {};
  3553. template<typename BasicJsonType, typename ConstructibleArrayType>
  3554. struct is_constructible_array_type_impl <
  3555. BasicJsonType, ConstructibleArrayType,
  3556. enable_if_t < !std::is_same<ConstructibleArrayType,
  3557. typename BasicJsonType::value_type>::value&&
  3558. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3559. is_default_constructible<ConstructibleArrayType>::value&&
  3560. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3561. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3562. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3563. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3564. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3565. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3566. // c.f. https://github.com/nlohmann/json/pull/3073
  3567. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3568. is_complete_type <
  3569. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3570. {
  3571. using value_type = range_value_t<ConstructibleArrayType>;
  3572. static constexpr bool value =
  3573. std::is_same<value_type,
  3574. typename BasicJsonType::array_t::value_type>::value ||
  3575. has_from_json<BasicJsonType,
  3576. value_type>::value ||
  3577. has_non_default_from_json <
  3578. BasicJsonType,
  3579. value_type >::value;
  3580. };
  3581. template<typename BasicJsonType, typename ConstructibleArrayType>
  3582. struct is_constructible_array_type
  3583. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3584. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3585. typename = void>
  3586. struct is_compatible_integer_type_impl : std::false_type {};
  3587. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3588. struct is_compatible_integer_type_impl <
  3589. RealIntegerType, CompatibleNumberIntegerType,
  3590. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3591. std::is_integral<CompatibleNumberIntegerType>::value&&
  3592. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3593. {
  3594. // is there an assert somewhere on overflows?
  3595. using RealLimits = std::numeric_limits<RealIntegerType>;
  3596. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3597. static constexpr auto value =
  3598. is_constructible<RealIntegerType,
  3599. CompatibleNumberIntegerType>::value &&
  3600. CompatibleLimits::is_integer &&
  3601. RealLimits::is_signed == CompatibleLimits::is_signed;
  3602. };
  3603. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3604. struct is_compatible_integer_type
  3605. : is_compatible_integer_type_impl<RealIntegerType,
  3606. CompatibleNumberIntegerType> {};
  3607. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3608. struct is_compatible_type_impl: std::false_type {};
  3609. template<typename BasicJsonType, typename CompatibleType>
  3610. struct is_compatible_type_impl <
  3611. BasicJsonType, CompatibleType,
  3612. enable_if_t<is_complete_type<CompatibleType>::value >>
  3613. {
  3614. static constexpr bool value =
  3615. has_to_json<BasicJsonType, CompatibleType>::value;
  3616. };
  3617. template<typename BasicJsonType, typename CompatibleType>
  3618. struct is_compatible_type
  3619. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3620. template<typename BasicJsonType, typename CompatibleReferenceType>
  3621. struct is_compatible_reference_type_impl
  3622. {
  3623. using JsonType = uncvref_t<BasicJsonType>;
  3624. using CVType = typename std::remove_reference<CompatibleReferenceType>::type;
  3625. using Type = typename std::remove_cv<CVType>::type;
  3626. constexpr static bool value = std::is_reference<CompatibleReferenceType>::value &&
  3627. (!std::is_const<typename std::remove_reference<BasicJsonType>::type>::value || std::is_const<CVType>::value) &&
  3628. (std::is_same<typename JsonType::boolean_t, Type>::value ||
  3629. std::is_same<typename JsonType::number_float_t, Type>::value ||
  3630. std::is_same<typename JsonType::number_integer_t, Type>::value ||
  3631. std::is_same<typename JsonType::number_unsigned_t, Type>::value ||
  3632. std::is_same<typename JsonType::string_t, Type>::value ||
  3633. std::is_same<typename JsonType::binary_t, Type>::value ||
  3634. std::is_same<typename JsonType::object_t, Type>::value ||
  3635. std::is_same<typename JsonType::array_t, Type>::value);
  3636. };
  3637. template<typename BasicJsonType, typename CompatibleReferenceType>
  3638. struct is_compatible_reference_type
  3639. : is_compatible_reference_type_impl<BasicJsonType, CompatibleReferenceType> {};
  3640. template<typename T1, typename T2>
  3641. struct is_constructible_tuple : std::false_type {};
  3642. template<typename T1, typename... Args>
  3643. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3644. template<typename BasicJsonType, typename T>
  3645. struct is_json_iterator_of : std::false_type {};
  3646. template<typename BasicJsonType>
  3647. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3648. template<typename BasicJsonType>
  3649. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3650. {};
  3651. // checks if a given type T is a template specialization of Primary
  3652. template<template <typename...> class Primary, typename T>
  3653. struct is_specialization_of : std::false_type {};
  3654. template<template <typename...> class Primary, typename... Args>
  3655. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3656. template<typename T>
  3657. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3658. // checks if B is a json_pointer<A>
  3659. template <typename A, typename B>
  3660. struct is_json_pointer_of : std::false_type {};
  3661. template <typename A>
  3662. struct is_json_pointer_of<A, ::nlohmann::json_pointer<A>> : std::true_type {};
  3663. template <typename A>
  3664. struct is_json_pointer_of<A, ::nlohmann::json_pointer<A>&> : std::true_type {};
  3665. // checks if A and B are comparable using Compare functor
  3666. template<typename Compare, typename A, typename B, typename = void>
  3667. struct is_comparable : std::false_type {};
  3668. // We exclude json_pointer here, because the checks using Compare(A, B) will
  3669. // use json_pointer::operator string_t() which triggers a deprecation warning
  3670. // for GCC. See https://github.com/nlohmann/json/issues/4621. The call to
  3671. // is_json_pointer_of can be removed once the deprecated function has been
  3672. // removed.
  3673. template<typename Compare, typename A, typename B>
  3674. struct is_comparable < Compare, A, B, enable_if_t < !is_json_pointer_of<A, B>::value
  3675. && std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>()))>::value
  3676. && std::is_constructible <decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))>::value
  3677. >> : std::true_type {};
  3678. template<typename T>
  3679. using detect_is_transparent = typename T::is_transparent;
  3680. // type trait to check if KeyType can be used as an object key (without a BasicJsonType)
  3681. // see is_usable_as_basic_json_key_type below
  3682. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3683. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3684. using is_usable_as_key_type = typename std::conditional <
  3685. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3686. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3687. ObjectKeyType>::value)
  3688. && (!RequireTransparentComparator
  3689. || is_detected <detect_is_transparent, Comparator>::value)
  3690. && !is_json_pointer<KeyType>::value,
  3691. std::true_type,
  3692. std::false_type >::type;
  3693. // type trait to check if KeyType can be used as an object key
  3694. // true if:
  3695. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3696. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3697. // - the comparator is transparent or RequireTransparentComparator is false
  3698. // - KeyType is not a JSON iterator or json_pointer
  3699. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3700. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3701. using is_usable_as_basic_json_key_type = typename std::conditional <
  3702. (is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3703. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3704. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3705. && !is_json_iterator_of<BasicJsonType, KeyType>::value)
  3706. #ifdef JSON_HAS_CPP_17
  3707. || std::is_convertible<KeyType, std::string_view>::value
  3708. #endif
  3709. , std::true_type,
  3710. std::false_type >::type;
  3711. template<typename ObjectType, typename KeyType>
  3712. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3713. // type trait to check if object_t has an erase() member functions accepting KeyType
  3714. template<typename BasicJsonType, typename KeyType>
  3715. using has_erase_with_key_type = typename std::conditional <
  3716. is_detected <
  3717. detect_erase_with_key_type,
  3718. typename BasicJsonType::object_t, KeyType >::value,
  3719. std::true_type,
  3720. std::false_type >::type;
  3721. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3722. // ordered_map inherits capacity() from std::vector)
  3723. template <typename T>
  3724. struct is_ordered_map
  3725. {
  3726. using one = char;
  3727. struct two
  3728. {
  3729. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3730. };
  3731. template <typename C> static one test( decltype(&C::capacity) ) ;
  3732. template <typename C> static two test(...);
  3733. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,cppcoreguidelines-use-enum-class)
  3734. };
  3735. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3736. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3737. T conditional_static_cast(U value)
  3738. {
  3739. return static_cast<T>(value);
  3740. }
  3741. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3742. T conditional_static_cast(U value)
  3743. {
  3744. return value;
  3745. }
  3746. template<typename... Types>
  3747. using all_integral = conjunction<std::is_integral<Types>...>;
  3748. template<typename... Types>
  3749. using all_signed = conjunction<std::is_signed<Types>...>;
  3750. template<typename... Types>
  3751. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3752. // there's a disjunction trait in another PR; replace when merged
  3753. template<typename... Types>
  3754. using same_sign = std::integral_constant < bool,
  3755. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3756. template<typename OfType, typename T>
  3757. using never_out_of_range = std::integral_constant < bool,
  3758. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3759. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3760. template<typename OfType, typename T,
  3761. bool OfTypeSigned = std::is_signed<OfType>::value,
  3762. bool TSigned = std::is_signed<T>::value>
  3763. struct value_in_range_of_impl2;
  3764. template<typename OfType, typename T>
  3765. struct value_in_range_of_impl2<OfType, T, false, false>
  3766. {
  3767. static constexpr bool test(T val)
  3768. {
  3769. using CommonType = typename std::common_type<OfType, T>::type;
  3770. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3771. }
  3772. };
  3773. template<typename OfType, typename T>
  3774. struct value_in_range_of_impl2<OfType, T, true, false>
  3775. {
  3776. static constexpr bool test(T val)
  3777. {
  3778. using CommonType = typename std::common_type<OfType, T>::type;
  3779. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3780. }
  3781. };
  3782. template<typename OfType, typename T>
  3783. struct value_in_range_of_impl2<OfType, T, false, true>
  3784. {
  3785. static constexpr bool test(T val)
  3786. {
  3787. using CommonType = typename std::common_type<OfType, T>::type;
  3788. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3789. }
  3790. };
  3791. template<typename OfType, typename T>
  3792. struct value_in_range_of_impl2<OfType, T, true, true>
  3793. {
  3794. static constexpr bool test(T val)
  3795. {
  3796. using CommonType = typename std::common_type<OfType, T>::type;
  3797. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3798. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3799. }
  3800. };
  3801. template<typename OfType, typename T,
  3802. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3803. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3804. struct value_in_range_of_impl1;
  3805. template<typename OfType, typename T>
  3806. struct value_in_range_of_impl1<OfType, T, false>
  3807. {
  3808. static constexpr bool test(T val)
  3809. {
  3810. return value_in_range_of_impl2<OfType, T>::test(val);
  3811. }
  3812. };
  3813. template<typename OfType, typename T>
  3814. struct value_in_range_of_impl1<OfType, T, true>
  3815. {
  3816. static constexpr bool test(T /*val*/)
  3817. {
  3818. return true;
  3819. }
  3820. };
  3821. template<typename OfType, typename T>
  3822. constexpr bool value_in_range_of(T val)
  3823. {
  3824. return value_in_range_of_impl1<OfType, T>::test(val);
  3825. }
  3826. template<bool Value>
  3827. using bool_constant = std::integral_constant<bool, Value>;
  3828. ///////////////////////////////////////////////////////////////////////////////
  3829. // is_c_string
  3830. ///////////////////////////////////////////////////////////////////////////////
  3831. namespace impl
  3832. {
  3833. template<typename T>
  3834. constexpr bool is_c_string()
  3835. {
  3836. using TUnExt = typename std::remove_extent<T>::type;
  3837. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3838. using TUnPtr = typename std::remove_pointer<T>::type;
  3839. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3840. return
  3841. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3842. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3843. }
  3844. } // namespace impl
  3845. // checks whether T is a [cv] char */[cv] char[] C string
  3846. template<typename T>
  3847. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3848. template<typename T>
  3849. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3850. ///////////////////////////////////////////////////////////////////////////////
  3851. // is_transparent
  3852. ///////////////////////////////////////////////////////////////////////////////
  3853. namespace impl
  3854. {
  3855. template<typename T>
  3856. constexpr bool is_transparent()
  3857. {
  3858. return is_detected<detect_is_transparent, T>::value;
  3859. }
  3860. } // namespace impl
  3861. // checks whether T has a member named is_transparent
  3862. template<typename T>
  3863. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3864. ///////////////////////////////////////////////////////////////////////////////
  3865. } // namespace detail
  3866. NLOHMANN_JSON_NAMESPACE_END
  3867. // #include <nlohmann/detail/string_concat.hpp>
  3868. // __ _____ _____ _____
  3869. // __| | __| | | | JSON for Modern C++
  3870. // | | |__ | | | | | | version 3.12.0
  3871. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3872. //
  3873. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  3874. // SPDX-License-Identifier: MIT
  3875. #include <cstring> // strlen
  3876. #include <string> // string
  3877. #include <utility> // forward
  3878. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3879. // #include <nlohmann/detail/meta/detected.hpp>
  3880. NLOHMANN_JSON_NAMESPACE_BEGIN
  3881. namespace detail
  3882. {
  3883. inline std::size_t concat_length()
  3884. {
  3885. return 0;
  3886. }
  3887. template<typename... Args>
  3888. inline std::size_t concat_length(const char* cstr, const Args& ... rest);
  3889. template<typename StringType, typename... Args>
  3890. inline std::size_t concat_length(const StringType& str, const Args& ... rest);
  3891. template<typename... Args>
  3892. inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
  3893. {
  3894. return 1 + concat_length(rest...);
  3895. }
  3896. template<typename... Args>
  3897. inline std::size_t concat_length(const char* cstr, const Args& ... rest)
  3898. {
  3899. // cppcheck-suppress ignoredReturnValue
  3900. return ::strlen(cstr) + concat_length(rest...);
  3901. }
  3902. template<typename StringType, typename... Args>
  3903. inline std::size_t concat_length(const StringType& str, const Args& ... rest)
  3904. {
  3905. return str.size() + concat_length(rest...);
  3906. }
  3907. template<typename OutStringType>
  3908. inline void concat_into(OutStringType& /*out*/)
  3909. {}
  3910. template<typename StringType, typename Arg>
  3911. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3912. template<typename StringType, typename Arg>
  3913. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3914. template<typename StringType, typename Arg>
  3915. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3916. template<typename StringType, typename Arg>
  3917. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3918. template<typename StringType, typename Arg>
  3919. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3920. template<typename StringType, typename Arg>
  3921. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3922. template<typename StringType, typename Arg>
  3923. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3924. template<typename StringType, typename Arg>
  3925. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3926. template < typename OutStringType, typename Arg, typename... Args,
  3927. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3928. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3929. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3930. template < typename OutStringType, typename Arg, typename... Args,
  3931. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3932. && !detect_string_can_append_op<OutStringType, Arg>::value
  3933. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3934. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3935. template < typename OutStringType, typename Arg, typename... Args,
  3936. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3937. && !detect_string_can_append_op<OutStringType, Arg>::value
  3938. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3939. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3940. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3941. template<typename OutStringType, typename Arg, typename... Args,
  3942. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3943. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3944. {
  3945. out.append(std::forward<Arg>(arg));
  3946. concat_into(out, std::forward<Args>(rest)...);
  3947. }
  3948. template < typename OutStringType, typename Arg, typename... Args,
  3949. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3950. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3951. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3952. {
  3953. out += std::forward<Arg>(arg);
  3954. concat_into(out, std::forward<Args>(rest)...);
  3955. }
  3956. template < typename OutStringType, typename Arg, typename... Args,
  3957. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3958. && !detect_string_can_append_op<OutStringType, Arg>::value
  3959. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3960. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3961. {
  3962. out.append(arg.begin(), arg.end());
  3963. concat_into(out, std::forward<Args>(rest)...);
  3964. }
  3965. template < typename OutStringType, typename Arg, typename... Args,
  3966. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3967. && !detect_string_can_append_op<OutStringType, Arg>::value
  3968. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3969. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3970. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3971. {
  3972. out.append(arg.data(), arg.size());
  3973. concat_into(out, std::forward<Args>(rest)...);
  3974. }
  3975. template<typename OutStringType = std::string, typename... Args>
  3976. inline OutStringType concat(Args && ... args)
  3977. {
  3978. OutStringType str;
  3979. str.reserve(concat_length(args...));
  3980. concat_into(str, std::forward<Args>(args)...);
  3981. return str;
  3982. }
  3983. } // namespace detail
  3984. NLOHMANN_JSON_NAMESPACE_END
  3985. // With -Wweak-vtables, Clang will complain about the exception classes as they
  3986. // have no out-of-line virtual method definitions and their vtable will be
  3987. // emitted in every translation unit. This issue cannot be fixed with a
  3988. // header-only library as there is no implementation file to move these
  3989. // functions to. As a result, we suppress this warning here to avoid client
  3990. // code stumbling over this. See https://github.com/nlohmann/json/issues/4087
  3991. // for a discussion.
  3992. #if defined(__clang__)
  3993. #pragma clang diagnostic push
  3994. #pragma clang diagnostic ignored "-Wweak-vtables"
  3995. #endif
  3996. NLOHMANN_JSON_NAMESPACE_BEGIN
  3997. namespace detail
  3998. {
  3999. ////////////////
  4000. // exceptions //
  4001. ////////////////
  4002. /// @brief general exception of the @ref basic_json class
  4003. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  4004. class exception : public std::exception
  4005. {
  4006. public:
  4007. /// returns the explanatory string
  4008. const char* what() const noexcept override
  4009. {
  4010. return m.what();
  4011. }
  4012. /// the id of the exception
  4013. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  4014. protected:
  4015. JSON_HEDLEY_NON_NULL(3)
  4016. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  4017. static std::string name(const std::string& ename, int id_)
  4018. {
  4019. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  4020. }
  4021. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  4022. {
  4023. return "";
  4024. }
  4025. template<typename BasicJsonType>
  4026. static std::string diagnostics(const BasicJsonType* leaf_element)
  4027. {
  4028. #if JSON_DIAGNOSTICS
  4029. std::vector<std::string> tokens;
  4030. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  4031. {
  4032. switch (current->m_parent->type())
  4033. {
  4034. case value_t::array:
  4035. {
  4036. for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
  4037. {
  4038. if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
  4039. {
  4040. tokens.emplace_back(std::to_string(i));
  4041. break;
  4042. }
  4043. }
  4044. break;
  4045. }
  4046. case value_t::object:
  4047. {
  4048. for (const auto& element : *current->m_parent->m_data.m_value.object)
  4049. {
  4050. if (&element.second == current)
  4051. {
  4052. tokens.emplace_back(element.first.c_str());
  4053. break;
  4054. }
  4055. }
  4056. break;
  4057. }
  4058. case value_t::null: // LCOV_EXCL_LINE
  4059. case value_t::string: // LCOV_EXCL_LINE
  4060. case value_t::boolean: // LCOV_EXCL_LINE
  4061. case value_t::number_integer: // LCOV_EXCL_LINE
  4062. case value_t::number_unsigned: // LCOV_EXCL_LINE
  4063. case value_t::number_float: // LCOV_EXCL_LINE
  4064. case value_t::binary: // LCOV_EXCL_LINE
  4065. case value_t::discarded: // LCOV_EXCL_LINE
  4066. default: // LCOV_EXCL_LINE
  4067. break; // LCOV_EXCL_LINE
  4068. }
  4069. }
  4070. if (tokens.empty())
  4071. {
  4072. return "";
  4073. }
  4074. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  4075. [](const std::string & a, const std::string & b)
  4076. {
  4077. return concat(a, '/', detail::escape(b));
  4078. });
  4079. return concat('(', str, ") ", get_byte_positions(leaf_element));
  4080. #else
  4081. return get_byte_positions(leaf_element);
  4082. #endif
  4083. }
  4084. private:
  4085. /// an exception object as storage for error messages
  4086. std::runtime_error m;
  4087. #if JSON_DIAGNOSTIC_POSITIONS
  4088. template<typename BasicJsonType>
  4089. static std::string get_byte_positions(const BasicJsonType* leaf_element)
  4090. {
  4091. if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
  4092. {
  4093. return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
  4094. }
  4095. return "";
  4096. }
  4097. #else
  4098. template<typename BasicJsonType>
  4099. static std::string get_byte_positions(const BasicJsonType* leaf_element)
  4100. {
  4101. static_cast<void>(leaf_element);
  4102. return "";
  4103. }
  4104. #endif
  4105. };
  4106. /// @brief exception indicating a parse error
  4107. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  4108. class parse_error : public exception
  4109. {
  4110. public:
  4111. /*!
  4112. @brief create a parse error exception
  4113. @param[in] id_ the id of the exception
  4114. @param[in] pos the position where the error occurred (or with
  4115. chars_read_total=0 if the position cannot be
  4116. determined)
  4117. @param[in] what_arg the explanatory string
  4118. @return parse_error object
  4119. */
  4120. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4121. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  4122. {
  4123. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  4124. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  4125. return {id_, pos.chars_read_total, w.c_str()};
  4126. }
  4127. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4128. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  4129. {
  4130. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  4131. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  4132. ": ", exception::diagnostics(context), what_arg);
  4133. return {id_, byte_, w.c_str()};
  4134. }
  4135. /*!
  4136. @brief byte index of the parse error
  4137. The byte index of the last read character in the input file.
  4138. @note For an input with n bytes, 1 is the index of the first character and
  4139. n+1 is the index of the terminating null byte or the end of file.
  4140. This also holds true when reading a byte vector (CBOR or MessagePack).
  4141. */
  4142. const std::size_t byte;
  4143. private:
  4144. parse_error(int id_, std::size_t byte_, const char* what_arg)
  4145. : exception(id_, what_arg), byte(byte_) {}
  4146. static std::string position_string(const position_t& pos)
  4147. {
  4148. return concat(" at line ", std::to_string(pos.lines_read + 1),
  4149. ", column ", std::to_string(pos.chars_read_current_line));
  4150. }
  4151. };
  4152. /// @brief exception indicating errors with iterators
  4153. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  4154. class invalid_iterator : public exception
  4155. {
  4156. public:
  4157. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4158. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  4159. {
  4160. const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  4161. return {id_, w.c_str()};
  4162. }
  4163. private:
  4164. JSON_HEDLEY_NON_NULL(3)
  4165. invalid_iterator(int id_, const char* what_arg)
  4166. : exception(id_, what_arg) {}
  4167. };
  4168. /// @brief exception indicating executing a member function with a wrong type
  4169. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  4170. class type_error : public exception
  4171. {
  4172. public:
  4173. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4174. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  4175. {
  4176. const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  4177. return {id_, w.c_str()};
  4178. }
  4179. private:
  4180. JSON_HEDLEY_NON_NULL(3)
  4181. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4182. };
  4183. /// @brief exception indicating access out of the defined range
  4184. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  4185. class out_of_range : public exception
  4186. {
  4187. public:
  4188. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4189. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  4190. {
  4191. const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  4192. return {id_, w.c_str()};
  4193. }
  4194. private:
  4195. JSON_HEDLEY_NON_NULL(3)
  4196. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4197. };
  4198. /// @brief exception indicating other library errors
  4199. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  4200. class other_error : public exception
  4201. {
  4202. public:
  4203. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  4204. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  4205. {
  4206. const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  4207. return {id_, w.c_str()};
  4208. }
  4209. private:
  4210. JSON_HEDLEY_NON_NULL(3)
  4211. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4212. };
  4213. } // namespace detail
  4214. NLOHMANN_JSON_NAMESPACE_END
  4215. #if defined(__clang__)
  4216. #pragma clang diagnostic pop
  4217. #endif
  4218. // #include <nlohmann/detail/macro_scope.hpp>
  4219. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4220. // #include <nlohmann/detail/meta/identity_tag.hpp>
  4221. // __ _____ _____ _____
  4222. // __| | __| | | | JSON for Modern C++
  4223. // | | |__ | | | | | | version 3.12.0
  4224. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4225. //
  4226. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  4227. // SPDX-License-Identifier: MIT
  4228. // #include <nlohmann/detail/abi_macros.hpp>
  4229. NLOHMANN_JSON_NAMESPACE_BEGIN
  4230. namespace detail
  4231. {
  4232. // dispatching helper struct
  4233. template <class T> struct identity_tag {};
  4234. } // namespace detail
  4235. NLOHMANN_JSON_NAMESPACE_END
  4236. // #include <nlohmann/detail/meta/std_fs.hpp>
  4237. // __ _____ _____ _____
  4238. // __| | __| | | | JSON for Modern C++
  4239. // | | |__ | | | | | | version 3.12.0
  4240. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4241. //
  4242. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  4243. // SPDX-License-Identifier: MIT
  4244. // #include <nlohmann/detail/macro_scope.hpp>
  4245. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4246. #include <experimental/filesystem>
  4247. NLOHMANN_JSON_NAMESPACE_BEGIN
  4248. namespace detail
  4249. {
  4250. namespace std_fs = std::experimental::filesystem;
  4251. } // namespace detail
  4252. NLOHMANN_JSON_NAMESPACE_END
  4253. #elif JSON_HAS_FILESYSTEM
  4254. #include <filesystem> // NOLINT(build/c++17)
  4255. NLOHMANN_JSON_NAMESPACE_BEGIN
  4256. namespace detail
  4257. {
  4258. namespace std_fs = std::filesystem;
  4259. } // namespace detail
  4260. NLOHMANN_JSON_NAMESPACE_END
  4261. #endif
  4262. // #include <nlohmann/detail/meta/type_traits.hpp>
  4263. // #include <nlohmann/detail/meta/logic.hpp>
  4264. // #include <nlohmann/detail/macro_scope.hpp>
  4265. NLOHMANN_JSON_NAMESPACE_BEGIN
  4266. namespace detail
  4267. {
  4268. #ifdef JSON_HAS_CPP_17
  4269. template<bool... Booleans>
  4270. struct cxpr_or_impl : std::integral_constant < bool, (Booleans || ...) > {};
  4271. template<bool... Booleans>
  4272. struct cxpr_and_impl : std::integral_constant < bool, (Booleans &&...) > {};
  4273. #else
  4274. template<bool... Booleans>
  4275. struct cxpr_or_impl : std::false_type {};
  4276. template<bool... Booleans>
  4277. struct cxpr_or_impl<true, Booleans...> : std::true_type {};
  4278. template<bool... Booleans>
  4279. struct cxpr_or_impl<false, Booleans...> : cxpr_or_impl<Booleans...> {};
  4280. template<bool... Booleans>
  4281. struct cxpr_and_impl : std::true_type {};
  4282. template<bool... Booleans>
  4283. struct cxpr_and_impl<true, Booleans...> : cxpr_and_impl<Booleans...> {};
  4284. template<bool... Booleans>
  4285. struct cxpr_and_impl<false, Booleans...> : std::false_type {};
  4286. #endif
  4287. template<class Boolean>
  4288. struct cxpr_not : std::integral_constant < bool, !Boolean::value > {};
  4289. template<class... Booleans>
  4290. struct cxpr_or : cxpr_or_impl<Booleans::value...> {};
  4291. template<bool... Booleans>
  4292. struct cxpr_or_c : cxpr_or_impl<Booleans...> {};
  4293. template<class... Booleans>
  4294. struct cxpr_and : cxpr_and_impl<Booleans::value...> {};
  4295. template<bool... Booleans>
  4296. struct cxpr_and_c : cxpr_and_impl<Booleans...> {};
  4297. } // namespace detail
  4298. NLOHMANN_JSON_NAMESPACE_END
  4299. // #include <nlohmann/detail/string_concat.hpp>
  4300. // #include <nlohmann/detail/value_t.hpp>
  4301. // include after macro_scope.hpp
  4302. #ifdef JSON_HAS_CPP_17
  4303. #include <optional> // optional
  4304. #endif
  4305. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4306. #include <string_view> // u8string_view
  4307. #endif
  4308. NLOHMANN_JSON_NAMESPACE_BEGIN
  4309. namespace detail
  4310. {
  4311. template<typename BasicJsonType>
  4312. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  4313. {
  4314. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  4315. {
  4316. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  4317. }
  4318. n = nullptr;
  4319. }
  4320. #ifdef JSON_HAS_CPP_17
  4321. template < typename BasicJsonType, typename T,
  4322. typename std::enable_if < !nlohmann::detail::is_basic_json<T>::value, int >::type = 0 >
  4323. void from_json(const BasicJsonType& j, std::optional<T>& opt)
  4324. {
  4325. if (j.is_null())
  4326. {
  4327. opt = std::nullopt;
  4328. }
  4329. else
  4330. {
  4331. opt.emplace(j.template get<T>());
  4332. }
  4333. }
  4334. #endif // JSON_HAS_CPP_17
  4335. // overloads for basic_json template parameters
  4336. template < typename BasicJsonType, typename ArithmeticType,
  4337. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4338. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4339. int > = 0 >
  4340. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4341. {
  4342. switch (static_cast<value_t>(j))
  4343. {
  4344. case value_t::number_unsigned:
  4345. {
  4346. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4347. break;
  4348. }
  4349. case value_t::number_integer:
  4350. {
  4351. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4352. break;
  4353. }
  4354. case value_t::number_float:
  4355. {
  4356. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4357. break;
  4358. }
  4359. case value_t::null:
  4360. case value_t::object:
  4361. case value_t::array:
  4362. case value_t::string:
  4363. case value_t::boolean:
  4364. case value_t::binary:
  4365. case value_t::discarded:
  4366. default:
  4367. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4368. }
  4369. }
  4370. template<typename BasicJsonType>
  4371. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4372. {
  4373. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4374. {
  4375. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4376. }
  4377. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4378. }
  4379. template<typename BasicJsonType>
  4380. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4381. {
  4382. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4383. {
  4384. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4385. }
  4386. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4387. }
  4388. template <
  4389. typename BasicJsonType, typename StringType,
  4390. enable_if_t <
  4391. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4392. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4393. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4394. && !is_json_ref<StringType>::value, int > = 0 >
  4395. inline void from_json(const BasicJsonType& j, StringType& s)
  4396. {
  4397. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4398. {
  4399. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4400. }
  4401. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4402. }
  4403. template<typename BasicJsonType>
  4404. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4405. {
  4406. get_arithmetic_value(j, val);
  4407. }
  4408. template<typename BasicJsonType>
  4409. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4410. {
  4411. get_arithmetic_value(j, val);
  4412. }
  4413. template<typename BasicJsonType>
  4414. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4415. {
  4416. get_arithmetic_value(j, val);
  4417. }
  4418. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4419. template<typename BasicJsonType, typename EnumType,
  4420. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4421. inline void from_json(const BasicJsonType& j, EnumType& e)
  4422. {
  4423. typename std::underlying_type<EnumType>::type val;
  4424. get_arithmetic_value(j, val);
  4425. e = static_cast<EnumType>(val);
  4426. }
  4427. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4428. // forward_list doesn't have an insert method
  4429. template<typename BasicJsonType, typename T, typename Allocator,
  4430. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4431. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4432. {
  4433. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4434. {
  4435. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4436. }
  4437. l.clear();
  4438. std::transform(j.rbegin(), j.rend(),
  4439. std::front_inserter(l), [](const BasicJsonType & i)
  4440. {
  4441. return i.template get<T>();
  4442. });
  4443. }
  4444. // valarray doesn't have an insert method
  4445. template<typename BasicJsonType, typename T,
  4446. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4447. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4448. {
  4449. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4450. {
  4451. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4452. }
  4453. l.resize(j.size());
  4454. std::transform(j.begin(), j.end(), std::begin(l),
  4455. [](const BasicJsonType & elem)
  4456. {
  4457. return elem.template get<T>();
  4458. });
  4459. }
  4460. template<typename BasicJsonType, typename T, std::size_t N>
  4461. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4462. -> decltype(j.template get<T>(), void())
  4463. {
  4464. for (std::size_t i = 0; i < N; ++i)
  4465. {
  4466. arr[i] = j.at(i).template get<T>();
  4467. }
  4468. }
  4469. template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
  4470. auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4471. -> decltype(j.template get<T>(), void())
  4472. {
  4473. for (std::size_t i1 = 0; i1 < N1; ++i1)
  4474. {
  4475. for (std::size_t i2 = 0; i2 < N2; ++i2)
  4476. {
  4477. arr[i1][i2] = j.at(i1).at(i2).template get<T>();
  4478. }
  4479. }
  4480. }
  4481. template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
  4482. auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4483. -> decltype(j.template get<T>(), void())
  4484. {
  4485. for (std::size_t i1 = 0; i1 < N1; ++i1)
  4486. {
  4487. for (std::size_t i2 = 0; i2 < N2; ++i2)
  4488. {
  4489. for (std::size_t i3 = 0; i3 < N3; ++i3)
  4490. {
  4491. arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
  4492. }
  4493. }
  4494. }
  4495. }
  4496. template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
  4497. 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)
  4498. -> decltype(j.template get<T>(), void())
  4499. {
  4500. for (std::size_t i1 = 0; i1 < N1; ++i1)
  4501. {
  4502. for (std::size_t i2 = 0; i2 < N2; ++i2)
  4503. {
  4504. for (std::size_t i3 = 0; i3 < N3; ++i3)
  4505. {
  4506. for (std::size_t i4 = 0; i4 < N4; ++i4)
  4507. {
  4508. arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
  4509. }
  4510. }
  4511. }
  4512. }
  4513. }
  4514. template<typename BasicJsonType>
  4515. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4516. {
  4517. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4518. }
  4519. template<typename BasicJsonType, typename T, std::size_t N>
  4520. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4521. priority_tag<2> /*unused*/)
  4522. -> decltype(j.template get<T>(), void())
  4523. {
  4524. for (std::size_t i = 0; i < N; ++i)
  4525. {
  4526. arr[i] = j.at(i).template get<T>();
  4527. }
  4528. }
  4529. template<typename BasicJsonType, typename ConstructibleArrayType,
  4530. enable_if_t<
  4531. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4532. int> = 0>
  4533. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4534. -> decltype(
  4535. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4536. j.template get<typename ConstructibleArrayType::value_type>(),
  4537. void())
  4538. {
  4539. using std::end;
  4540. ConstructibleArrayType ret;
  4541. ret.reserve(j.size());
  4542. std::transform(j.begin(), j.end(),
  4543. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4544. {
  4545. // get<BasicJsonType>() returns *this, this won't call a from_json
  4546. // method when value_type is BasicJsonType
  4547. return i.template get<typename ConstructibleArrayType::value_type>();
  4548. });
  4549. arr = std::move(ret);
  4550. }
  4551. template<typename BasicJsonType, typename ConstructibleArrayType,
  4552. enable_if_t<
  4553. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4554. int> = 0>
  4555. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4556. priority_tag<0> /*unused*/)
  4557. {
  4558. using std::end;
  4559. ConstructibleArrayType ret;
  4560. std::transform(
  4561. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4562. [](const BasicJsonType & i)
  4563. {
  4564. // get<BasicJsonType>() returns *this, this won't call a from_json
  4565. // method when value_type is BasicJsonType
  4566. return i.template get<typename ConstructibleArrayType::value_type>();
  4567. });
  4568. arr = std::move(ret);
  4569. }
  4570. template < typename BasicJsonType, typename ConstructibleArrayType,
  4571. enable_if_t <
  4572. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4573. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4574. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4575. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4576. !is_basic_json<ConstructibleArrayType>::value,
  4577. int > = 0 >
  4578. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4579. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4580. j.template get<typename ConstructibleArrayType::value_type>(),
  4581. void())
  4582. {
  4583. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4584. {
  4585. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4586. }
  4587. from_json_array_impl(j, arr, priority_tag<3> {});
  4588. }
  4589. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4590. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4591. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4592. {
  4593. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4594. }
  4595. template < typename BasicJsonType, typename T, std::size_t N >
  4596. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4597. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4598. {
  4599. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4600. {
  4601. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4602. }
  4603. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4604. }
  4605. template<typename BasicJsonType>
  4606. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4607. {
  4608. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4609. {
  4610. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4611. }
  4612. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4613. }
  4614. template<typename BasicJsonType, typename ConstructibleObjectType,
  4615. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4616. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4617. {
  4618. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4619. {
  4620. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4621. }
  4622. ConstructibleObjectType ret;
  4623. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4624. using value_type = typename ConstructibleObjectType::value_type;
  4625. std::transform(
  4626. inner_object->begin(), inner_object->end(),
  4627. std::inserter(ret, ret.begin()),
  4628. [](typename BasicJsonType::object_t::value_type const & p)
  4629. {
  4630. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4631. });
  4632. obj = std::move(ret);
  4633. }
  4634. // overload for arithmetic types, not chosen for basic_json template arguments
  4635. // (BooleanType, etc.); note: Is it really necessary to provide explicit
  4636. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4637. // an arithmetic type?
  4638. template < typename BasicJsonType, typename ArithmeticType,
  4639. enable_if_t <
  4640. std::is_arithmetic<ArithmeticType>::value&&
  4641. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4642. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4643. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4644. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4645. int > = 0 >
  4646. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4647. {
  4648. switch (static_cast<value_t>(j))
  4649. {
  4650. case value_t::number_unsigned:
  4651. {
  4652. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4653. break;
  4654. }
  4655. case value_t::number_integer:
  4656. {
  4657. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4658. break;
  4659. }
  4660. case value_t::number_float:
  4661. {
  4662. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4663. break;
  4664. }
  4665. case value_t::boolean:
  4666. {
  4667. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4668. break;
  4669. }
  4670. case value_t::null:
  4671. case value_t::object:
  4672. case value_t::array:
  4673. case value_t::string:
  4674. case value_t::binary:
  4675. case value_t::discarded:
  4676. default:
  4677. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4678. }
  4679. }
  4680. template<typename BasicJsonType, typename Type>
  4681. detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType&& j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<0> /*unused*/)
  4682. {
  4683. return std::forward<BasicJsonType>(j).template get<detail::uncvref_t<Type>>();
  4684. }
  4685. template<typename BasicJsonType, typename Type,
  4686. detail::enable_if_t<detail::is_compatible_reference_type<BasicJsonType, Type>::value, int> = 0>
  4687. Type from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<1> /*unused*/)
  4688. {
  4689. return std::forward<BasicJsonType>(j).template get_ref<Type>();
  4690. }
  4691. template<typename BasicJsonType, typename Type,
  4692. detail::enable_if_t<std::is_arithmetic<uncvref_t<Type>>::value, int> = 0>
  4693. detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<2> /*unused*/)
  4694. {
  4695. return std::forward<BasicJsonType>(j).template get<detail::uncvref_t<Type>>();
  4696. }
  4697. template<std::size_t PTagValue, typename BasicJsonType, typename... Types>
  4698. using tuple_type = std::tuple < decltype(from_json_tuple_get_impl(std::declval<BasicJsonType>(), detail::identity_tag<Types> {}, detail::priority_tag<PTagValue> {}))... >;
  4699. template<std::size_t PTagValue, typename... Args, typename BasicJsonType, std::size_t... Idx>
  4700. tuple_type<PTagValue, BasicJsonType, Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4701. {
  4702. return tuple_type<PTagValue, BasicJsonType, Args...>(from_json_tuple_get_impl(std::forward<BasicJsonType>(j).at(Idx), detail::identity_tag<Args> {}, detail::priority_tag<PTagValue> {})...);
  4703. }
  4704. template<std::size_t PTagValue, typename BasicJsonType>
  4705. std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/)
  4706. {
  4707. return {};
  4708. }
  4709. template < typename BasicJsonType, class A1, class A2 >
  4710. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4711. {
  4712. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4713. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4714. }
  4715. template<typename BasicJsonType, typename A1, typename A2>
  4716. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4717. {
  4718. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4719. }
  4720. template<typename BasicJsonType, typename... Args>
  4721. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4722. {
  4723. static_assert(cxpr_and<cxpr_or<cxpr_not<std::is_reference<Args>>, is_compatible_reference_type<BasicJsonType, Args>>...>::value,
  4724. "Can not return a tuple containing references to types not contained in a Json, try Json::get_to()");
  4725. return from_json_tuple_impl_base<1, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4726. }
  4727. template<typename BasicJsonType, typename... Args>
  4728. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4729. {
  4730. t = from_json_tuple_impl_base<2, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4731. }
  4732. template<typename BasicJsonType, typename TupleRelated>
  4733. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4734. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4735. {
  4736. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4737. {
  4738. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4739. }
  4740. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4741. }
  4742. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4743. typename = enable_if_t < !std::is_constructible <
  4744. typename BasicJsonType::string_t, Key >::value >>
  4745. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  4746. {
  4747. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4748. {
  4749. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4750. }
  4751. m.clear();
  4752. for (const auto& p : j)
  4753. {
  4754. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4755. {
  4756. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4757. }
  4758. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4759. }
  4760. }
  4761. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4762. typename = enable_if_t < !std::is_constructible <
  4763. typename BasicJsonType::string_t, Key >::value >>
  4764. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4765. {
  4766. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4767. {
  4768. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4769. }
  4770. m.clear();
  4771. for (const auto& p : j)
  4772. {
  4773. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4774. {
  4775. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4776. }
  4777. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4778. }
  4779. }
  4780. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4781. template<typename BasicJsonType>
  4782. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4783. {
  4784. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4785. {
  4786. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4787. }
  4788. const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4789. // Checking for C++20 standard or later can be insufficient in case the
  4790. // library support for char8_t is either incomplete or was disabled
  4791. // altogether. Use the __cpp_lib_char8_t feature test instead.
  4792. #if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
  4793. p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
  4794. #else
  4795. p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
  4796. #endif
  4797. }
  4798. #endif
  4799. struct from_json_fn
  4800. {
  4801. template<typename BasicJsonType, typename T>
  4802. auto operator()(const BasicJsonType& j, T&& val) const
  4803. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4804. -> decltype(from_json(j, std::forward<T>(val)))
  4805. {
  4806. return from_json(j, std::forward<T>(val));
  4807. }
  4808. };
  4809. } // namespace detail
  4810. #ifndef JSON_HAS_CPP_17
  4811. /// namespace to hold default `from_json` function
  4812. /// to see why this is required:
  4813. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4814. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4815. {
  4816. #endif
  4817. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4818. detail::static_const<detail::from_json_fn>::value;
  4819. #ifndef JSON_HAS_CPP_17
  4820. } // namespace
  4821. #endif
  4822. NLOHMANN_JSON_NAMESPACE_END
  4823. // #include <nlohmann/detail/conversions/to_json.hpp>
  4824. // __ _____ _____ _____
  4825. // __| | __| | | | JSON for Modern C++
  4826. // | | |__ | | | | | | version 3.12.0
  4827. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4828. //
  4829. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  4830. // SPDX-License-Identifier: MIT
  4831. // #include <nlohmann/detail/macro_scope.hpp>
  4832. // JSON_HAS_CPP_17
  4833. #ifdef JSON_HAS_CPP_17
  4834. #include <optional> // optional
  4835. #endif
  4836. #include <algorithm> // copy
  4837. #include <iterator> // begin, end
  4838. #include <memory> // allocator_traits
  4839. #include <string> // basic_string, char_traits
  4840. #include <tuple> // tuple, get
  4841. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4842. #include <utility> // move, forward, declval, pair
  4843. #include <valarray> // valarray
  4844. #include <vector> // vector
  4845. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4846. // __ _____ _____ _____
  4847. // __| | __| | | | JSON for Modern C++
  4848. // | | |__ | | | | | | version 3.12.0
  4849. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4850. //
  4851. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  4852. // SPDX-License-Identifier: MIT
  4853. #include <cstddef> // size_t
  4854. #include <iterator> // forward_iterator_tag
  4855. #include <tuple> // tuple_size, get, tuple_element
  4856. #include <utility> // move
  4857. #if JSON_HAS_RANGES
  4858. #include <ranges> // enable_borrowed_range
  4859. #endif
  4860. // #include <nlohmann/detail/abi_macros.hpp>
  4861. // #include <nlohmann/detail/meta/type_traits.hpp>
  4862. // #include <nlohmann/detail/string_utils.hpp>
  4863. // __ _____ _____ _____
  4864. // __| | __| | | | JSON for Modern C++
  4865. // | | |__ | | | | | | version 3.12.0
  4866. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4867. //
  4868. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  4869. // SPDX-License-Identifier: MIT
  4870. #include <cstddef> // size_t
  4871. #include <string> // string, to_string
  4872. // #include <nlohmann/detail/abi_macros.hpp>
  4873. NLOHMANN_JSON_NAMESPACE_BEGIN
  4874. namespace detail
  4875. {
  4876. template<typename StringType>
  4877. void int_to_string(StringType& target, std::size_t value)
  4878. {
  4879. // For ADL
  4880. using std::to_string;
  4881. target = to_string(value);
  4882. }
  4883. template<typename StringType>
  4884. StringType to_string(std::size_t value)
  4885. {
  4886. StringType result;
  4887. int_to_string(result, value);
  4888. return result;
  4889. }
  4890. } // namespace detail
  4891. NLOHMANN_JSON_NAMESPACE_END
  4892. // #include <nlohmann/detail/value_t.hpp>
  4893. NLOHMANN_JSON_NAMESPACE_BEGIN
  4894. namespace detail
  4895. {
  4896. template<typename IteratorType> class iteration_proxy_value
  4897. {
  4898. public:
  4899. using difference_type = std::ptrdiff_t;
  4900. using value_type = iteration_proxy_value;
  4901. using pointer = value_type *;
  4902. using reference = value_type &;
  4903. using iterator_category = std::forward_iterator_tag;
  4904. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4905. private:
  4906. /// the iterator
  4907. IteratorType anchor{};
  4908. /// an index for arrays (used to create key names)
  4909. std::size_t array_index = 0;
  4910. /// last stringified array index
  4911. mutable std::size_t array_index_last = 0;
  4912. /// a string representation of the array index
  4913. mutable string_type array_index_str = "0";
  4914. /// an empty string (to return a reference for primitive values)
  4915. string_type empty_str{};
  4916. public:
  4917. explicit iteration_proxy_value() = default;
  4918. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4919. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4920. && std::is_nothrow_default_constructible<string_type>::value)
  4921. : anchor(std::move(it))
  4922. , array_index(array_index_)
  4923. {}
  4924. iteration_proxy_value(iteration_proxy_value const&) = default;
  4925. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4926. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4927. iteration_proxy_value(iteration_proxy_value&&)
  4928. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4929. && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4930. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4931. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4932. && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4933. ~iteration_proxy_value() = default;
  4934. /// dereference operator (needed for range-based for)
  4935. const iteration_proxy_value& operator*() const
  4936. {
  4937. return *this;
  4938. }
  4939. /// increment operator (needed for range-based for)
  4940. iteration_proxy_value& operator++()
  4941. {
  4942. ++anchor;
  4943. ++array_index;
  4944. return *this;
  4945. }
  4946. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4947. {
  4948. auto tmp = iteration_proxy_value(anchor, array_index);
  4949. ++anchor;
  4950. ++array_index;
  4951. return tmp;
  4952. }
  4953. /// equality operator (needed for InputIterator)
  4954. bool operator==(const iteration_proxy_value& o) const
  4955. {
  4956. return anchor == o.anchor;
  4957. }
  4958. /// inequality operator (needed for range-based for)
  4959. bool operator!=(const iteration_proxy_value& o) const
  4960. {
  4961. return anchor != o.anchor;
  4962. }
  4963. /// return key of the iterator
  4964. const string_type& key() const
  4965. {
  4966. JSON_ASSERT(anchor.m_object != nullptr);
  4967. switch (anchor.m_object->type())
  4968. {
  4969. // use integer array index as key
  4970. case value_t::array:
  4971. {
  4972. if (array_index != array_index_last)
  4973. {
  4974. int_to_string( array_index_str, array_index );
  4975. array_index_last = array_index;
  4976. }
  4977. return array_index_str;
  4978. }
  4979. // use key from the object
  4980. case value_t::object:
  4981. return anchor.key();
  4982. // use an empty key for all primitive types
  4983. case value_t::null:
  4984. case value_t::string:
  4985. case value_t::boolean:
  4986. case value_t::number_integer:
  4987. case value_t::number_unsigned:
  4988. case value_t::number_float:
  4989. case value_t::binary:
  4990. case value_t::discarded:
  4991. default:
  4992. return empty_str;
  4993. }
  4994. }
  4995. /// return value of the iterator
  4996. typename IteratorType::reference value() const
  4997. {
  4998. return anchor.value();
  4999. }
  5000. };
  5001. /// proxy class for the items() function
  5002. template<typename IteratorType> class iteration_proxy
  5003. {
  5004. private:
  5005. /// the container to iterate
  5006. typename IteratorType::pointer container = nullptr;
  5007. public:
  5008. explicit iteration_proxy() = default;
  5009. /// construct iteration proxy from a container
  5010. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  5011. : container(&cont) {}
  5012. iteration_proxy(iteration_proxy const&) = default;
  5013. iteration_proxy& operator=(iteration_proxy const&) = default;
  5014. iteration_proxy(iteration_proxy&&) noexcept = default;
  5015. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  5016. ~iteration_proxy() = default;
  5017. /// return iterator begin (needed for range-based for)
  5018. iteration_proxy_value<IteratorType> begin() const noexcept
  5019. {
  5020. return iteration_proxy_value<IteratorType>(container->begin());
  5021. }
  5022. /// return iterator end (needed for range-based for)
  5023. iteration_proxy_value<IteratorType> end() const noexcept
  5024. {
  5025. return iteration_proxy_value<IteratorType>(container->end());
  5026. }
  5027. };
  5028. // Structured Bindings Support
  5029. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  5030. // And see https://github.com/nlohmann/json/pull/1391
  5031. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  5032. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  5033. {
  5034. return i.key();
  5035. }
  5036. // Structured Bindings Support
  5037. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  5038. // And see https://github.com/nlohmann/json/pull/1391
  5039. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  5040. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  5041. {
  5042. return i.value();
  5043. }
  5044. } // namespace detail
  5045. NLOHMANN_JSON_NAMESPACE_END
  5046. // The Addition to the STD Namespace is required to add
  5047. // Structured Bindings Support to the iteration_proxy_value class
  5048. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  5049. // And see https://github.com/nlohmann/json/pull/1391
  5050. namespace std
  5051. {
  5052. #if defined(__clang__)
  5053. // Fix: https://github.com/nlohmann/json/issues/1401
  5054. #pragma clang diagnostic push
  5055. #pragma clang diagnostic ignored "-Wmismatched-tags"
  5056. #endif
  5057. template<typename IteratorType>
  5058. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  5059. : public std::integral_constant<std::size_t, 2> {};
  5060. template<std::size_t N, typename IteratorType>
  5061. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  5062. {
  5063. public:
  5064. using type = decltype(
  5065. get<N>(std::declval <
  5066. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  5067. };
  5068. #if defined(__clang__)
  5069. #pragma clang diagnostic pop
  5070. #endif
  5071. } // namespace std
  5072. #if JSON_HAS_RANGES
  5073. template <typename IteratorType>
  5074. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  5075. #endif
  5076. // #include <nlohmann/detail/meta/cpp_future.hpp>
  5077. // #include <nlohmann/detail/meta/std_fs.hpp>
  5078. // #include <nlohmann/detail/meta/type_traits.hpp>
  5079. // #include <nlohmann/detail/value_t.hpp>
  5080. NLOHMANN_JSON_NAMESPACE_BEGIN
  5081. namespace detail
  5082. {
  5083. //////////////////
  5084. // constructors //
  5085. //////////////////
  5086. /*
  5087. * Note all external_constructor<>::construct functions need to call
  5088. * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
  5089. * allocated value (e.g., a string). See bug issue
  5090. * https://github.com/nlohmann/json/issues/2865 for more information.
  5091. */
  5092. template<value_t> struct external_constructor;
  5093. template<>
  5094. struct external_constructor<value_t::boolean>
  5095. {
  5096. template<typename BasicJsonType>
  5097. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  5098. {
  5099. j.m_data.m_value.destroy(j.m_data.m_type);
  5100. j.m_data.m_type = value_t::boolean;
  5101. j.m_data.m_value = b;
  5102. j.assert_invariant();
  5103. }
  5104. };
  5105. template<>
  5106. struct external_constructor<value_t::string>
  5107. {
  5108. template<typename BasicJsonType>
  5109. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  5110. {
  5111. j.m_data.m_value.destroy(j.m_data.m_type);
  5112. j.m_data.m_type = value_t::string;
  5113. j.m_data.m_value = s;
  5114. j.assert_invariant();
  5115. }
  5116. template<typename BasicJsonType>
  5117. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  5118. {
  5119. j.m_data.m_value.destroy(j.m_data.m_type);
  5120. j.m_data.m_type = value_t::string;
  5121. j.m_data.m_value = std::move(s);
  5122. j.assert_invariant();
  5123. }
  5124. template < typename BasicJsonType, typename CompatibleStringType,
  5125. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  5126. int > = 0 >
  5127. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  5128. {
  5129. j.m_data.m_value.destroy(j.m_data.m_type);
  5130. j.m_data.m_type = value_t::string;
  5131. j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  5132. j.assert_invariant();
  5133. }
  5134. };
  5135. template<>
  5136. struct external_constructor<value_t::binary>
  5137. {
  5138. template<typename BasicJsonType>
  5139. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  5140. {
  5141. j.m_data.m_value.destroy(j.m_data.m_type);
  5142. j.m_data.m_type = value_t::binary;
  5143. j.m_data.m_value = typename BasicJsonType::binary_t(b);
  5144. j.assert_invariant();
  5145. }
  5146. template<typename BasicJsonType>
  5147. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  5148. {
  5149. j.m_data.m_value.destroy(j.m_data.m_type);
  5150. j.m_data.m_type = value_t::binary;
  5151. j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
  5152. j.assert_invariant();
  5153. }
  5154. };
  5155. template<>
  5156. struct external_constructor<value_t::number_float>
  5157. {
  5158. template<typename BasicJsonType>
  5159. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  5160. {
  5161. j.m_data.m_value.destroy(j.m_data.m_type);
  5162. j.m_data.m_type = value_t::number_float;
  5163. j.m_data.m_value = val;
  5164. j.assert_invariant();
  5165. }
  5166. };
  5167. template<>
  5168. struct external_constructor<value_t::number_unsigned>
  5169. {
  5170. template<typename BasicJsonType>
  5171. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  5172. {
  5173. j.m_data.m_value.destroy(j.m_data.m_type);
  5174. j.m_data.m_type = value_t::number_unsigned;
  5175. j.m_data.m_value = val;
  5176. j.assert_invariant();
  5177. }
  5178. };
  5179. template<>
  5180. struct external_constructor<value_t::number_integer>
  5181. {
  5182. template<typename BasicJsonType>
  5183. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  5184. {
  5185. j.m_data.m_value.destroy(j.m_data.m_type);
  5186. j.m_data.m_type = value_t::number_integer;
  5187. j.m_data.m_value = val;
  5188. j.assert_invariant();
  5189. }
  5190. };
  5191. template<>
  5192. struct external_constructor<value_t::array>
  5193. {
  5194. template<typename BasicJsonType>
  5195. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  5196. {
  5197. j.m_data.m_value.destroy(j.m_data.m_type);
  5198. j.m_data.m_type = value_t::array;
  5199. j.m_data.m_value = arr;
  5200. j.set_parents();
  5201. j.assert_invariant();
  5202. }
  5203. template<typename BasicJsonType>
  5204. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  5205. {
  5206. j.m_data.m_value.destroy(j.m_data.m_type);
  5207. j.m_data.m_type = value_t::array;
  5208. j.m_data.m_value = std::move(arr);
  5209. j.set_parents();
  5210. j.assert_invariant();
  5211. }
  5212. template < typename BasicJsonType, typename CompatibleArrayType,
  5213. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  5214. int > = 0 >
  5215. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  5216. {
  5217. using std::begin;
  5218. using std::end;
  5219. j.m_data.m_value.destroy(j.m_data.m_type);
  5220. j.m_data.m_type = value_t::array;
  5221. j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  5222. j.set_parents();
  5223. j.assert_invariant();
  5224. }
  5225. template<typename BasicJsonType>
  5226. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  5227. {
  5228. j.m_data.m_value.destroy(j.m_data.m_type);
  5229. j.m_data.m_type = value_t::array;
  5230. j.m_data.m_value = value_t::array;
  5231. j.m_data.m_value.array->reserve(arr.size());
  5232. for (const bool x : arr)
  5233. {
  5234. j.m_data.m_value.array->push_back(x);
  5235. j.set_parent(j.m_data.m_value.array->back());
  5236. }
  5237. j.assert_invariant();
  5238. }
  5239. template<typename BasicJsonType, typename T,
  5240. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  5241. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  5242. {
  5243. j.m_data.m_value.destroy(j.m_data.m_type);
  5244. j.m_data.m_type = value_t::array;
  5245. j.m_data.m_value = value_t::array;
  5246. j.m_data.m_value.array->resize(arr.size());
  5247. if (arr.size() > 0)
  5248. {
  5249. std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
  5250. }
  5251. j.set_parents();
  5252. j.assert_invariant();
  5253. }
  5254. };
  5255. template<>
  5256. struct external_constructor<value_t::object>
  5257. {
  5258. template<typename BasicJsonType>
  5259. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  5260. {
  5261. j.m_data.m_value.destroy(j.m_data.m_type);
  5262. j.m_data.m_type = value_t::object;
  5263. j.m_data.m_value = obj;
  5264. j.set_parents();
  5265. j.assert_invariant();
  5266. }
  5267. template<typename BasicJsonType>
  5268. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  5269. {
  5270. j.m_data.m_value.destroy(j.m_data.m_type);
  5271. j.m_data.m_type = value_t::object;
  5272. j.m_data.m_value = std::move(obj);
  5273. j.set_parents();
  5274. j.assert_invariant();
  5275. }
  5276. template < typename BasicJsonType, typename CompatibleObjectType,
  5277. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  5278. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  5279. {
  5280. using std::begin;
  5281. using std::end;
  5282. j.m_data.m_value.destroy(j.m_data.m_type);
  5283. j.m_data.m_type = value_t::object;
  5284. j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  5285. j.set_parents();
  5286. j.assert_invariant();
  5287. }
  5288. };
  5289. /////////////
  5290. // to_json //
  5291. /////////////
  5292. #ifdef JSON_HAS_CPP_17
  5293. template<typename BasicJsonType, typename T,
  5294. enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
  5295. void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
  5296. {
  5297. if (opt.has_value())
  5298. {
  5299. j = *opt;
  5300. }
  5301. else
  5302. {
  5303. j = nullptr;
  5304. }
  5305. }
  5306. #endif
  5307. template<typename BasicJsonType, typename T,
  5308. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  5309. inline void to_json(BasicJsonType& j, T b) noexcept
  5310. {
  5311. external_constructor<value_t::boolean>::construct(j, b);
  5312. }
  5313. template < typename BasicJsonType, typename BoolRef,
  5314. enable_if_t <
  5315. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  5316. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  5317. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  5318. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  5319. typename BasicJsonType::boolean_t >::value))
  5320. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  5321. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  5322. {
  5323. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  5324. }
  5325. template<typename BasicJsonType, typename CompatibleString,
  5326. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  5327. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  5328. {
  5329. external_constructor<value_t::string>::construct(j, s);
  5330. }
  5331. template<typename BasicJsonType>
  5332. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  5333. {
  5334. external_constructor<value_t::string>::construct(j, std::move(s));
  5335. }
  5336. template<typename BasicJsonType, typename FloatType,
  5337. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  5338. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  5339. {
  5340. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  5341. }
  5342. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  5343. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  5344. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  5345. {
  5346. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  5347. }
  5348. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  5349. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  5350. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  5351. {
  5352. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  5353. }
  5354. #if !JSON_DISABLE_ENUM_SERIALIZATION
  5355. template<typename BasicJsonType, typename EnumType,
  5356. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  5357. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  5358. {
  5359. using underlying_type = typename std::underlying_type<EnumType>::type;
  5360. static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
  5361. external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
  5362. }
  5363. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  5364. template<typename BasicJsonType>
  5365. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  5366. {
  5367. external_constructor<value_t::array>::construct(j, e);
  5368. }
  5369. template < typename BasicJsonType, typename CompatibleArrayType,
  5370. enable_if_t < is_compatible_array_type<BasicJsonType,
  5371. CompatibleArrayType>::value&&
  5372. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  5373. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  5374. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  5375. !is_basic_json<CompatibleArrayType>::value,
  5376. int > = 0 >
  5377. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  5378. {
  5379. external_constructor<value_t::array>::construct(j, arr);
  5380. }
  5381. template<typename BasicJsonType>
  5382. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  5383. {
  5384. external_constructor<value_t::binary>::construct(j, bin);
  5385. }
  5386. template<typename BasicJsonType, typename T,
  5387. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  5388. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  5389. {
  5390. external_constructor<value_t::array>::construct(j, std::move(arr));
  5391. }
  5392. template<typename BasicJsonType>
  5393. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  5394. {
  5395. external_constructor<value_t::array>::construct(j, std::move(arr));
  5396. }
  5397. template < typename BasicJsonType, typename CompatibleObjectType,
  5398. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  5399. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  5400. {
  5401. external_constructor<value_t::object>::construct(j, obj);
  5402. }
  5403. template<typename BasicJsonType>
  5404. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  5405. {
  5406. external_constructor<value_t::object>::construct(j, std::move(obj));
  5407. }
  5408. template <
  5409. typename BasicJsonType, typename T, std::size_t N,
  5410. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  5411. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5412. int > = 0 >
  5413. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5414. {
  5415. external_constructor<value_t::array>::construct(j, arr);
  5416. }
  5417. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  5418. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  5419. {
  5420. j = { p.first, p.second };
  5421. }
  5422. // for https://github.com/nlohmann/json/pull/1134
  5423. template<typename BasicJsonType, typename T,
  5424. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  5425. inline void to_json(BasicJsonType& j, const T& b)
  5426. {
  5427. j = { {b.key(), b.value()} };
  5428. }
  5429. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  5430. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  5431. {
  5432. j = { std::get<Idx>(t)... };
  5433. }
  5434. template<typename BasicJsonType, typename Tuple>
  5435. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
  5436. {
  5437. using array_t = typename BasicJsonType::array_t;
  5438. j = array_t();
  5439. }
  5440. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  5441. inline void to_json(BasicJsonType& j, const T& t)
  5442. {
  5443. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  5444. }
  5445. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  5446. #if defined(__cpp_lib_char8_t)
  5447. template<typename BasicJsonType, typename Tr, typename Allocator>
  5448. inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s)
  5449. {
  5450. using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
  5451. j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator());
  5452. }
  5453. #endif
  5454. template<typename BasicJsonType>
  5455. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  5456. {
  5457. // Returns either a std::string or a std::u8string depending whether library
  5458. // support for char8_t is enabled.
  5459. j = p.u8string();
  5460. }
  5461. #endif
  5462. struct to_json_fn
  5463. {
  5464. template<typename BasicJsonType, typename T>
  5465. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  5466. -> decltype(to_json(j, std::forward<T>(val)), void())
  5467. {
  5468. return to_json(j, std::forward<T>(val));
  5469. }
  5470. };
  5471. } // namespace detail
  5472. #ifndef JSON_HAS_CPP_17
  5473. /// namespace to hold default `to_json` function
  5474. /// to see why this is required:
  5475. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5476. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5477. {
  5478. #endif
  5479. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5480. detail::static_const<detail::to_json_fn>::value;
  5481. #ifndef JSON_HAS_CPP_17
  5482. } // namespace
  5483. #endif
  5484. NLOHMANN_JSON_NAMESPACE_END
  5485. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5486. NLOHMANN_JSON_NAMESPACE_BEGIN
  5487. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5488. template<typename ValueType, typename>
  5489. struct adl_serializer
  5490. {
  5491. /// @brief convert a JSON value to any value type
  5492. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5493. template<typename BasicJsonType, typename TargetType = ValueType>
  5494. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5495. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5496. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5497. {
  5498. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5499. }
  5500. /// @brief convert a JSON value to any value type
  5501. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5502. template<typename BasicJsonType, typename TargetType = ValueType>
  5503. static auto from_json(BasicJsonType && j) noexcept(
  5504. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5505. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5506. {
  5507. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5508. }
  5509. /// @brief convert any value type to a JSON value
  5510. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5511. template<typename BasicJsonType, typename TargetType = ValueType>
  5512. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5513. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5514. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5515. {
  5516. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5517. }
  5518. };
  5519. NLOHMANN_JSON_NAMESPACE_END
  5520. // #include <nlohmann/byte_container_with_subtype.hpp>
  5521. // __ _____ _____ _____
  5522. // __| | __| | | | JSON for Modern C++
  5523. // | | |__ | | | | | | version 3.12.0
  5524. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5525. //
  5526. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  5527. // SPDX-License-Identifier: MIT
  5528. #include <cstdint> // uint8_t, uint64_t
  5529. #include <tuple> // tie
  5530. #include <utility> // move
  5531. // #include <nlohmann/detail/abi_macros.hpp>
  5532. NLOHMANN_JSON_NAMESPACE_BEGIN
  5533. /// @brief an internal type for a backed binary type
  5534. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5535. template<typename BinaryType>
  5536. class byte_container_with_subtype : public BinaryType
  5537. {
  5538. public:
  5539. using container_type = BinaryType;
  5540. using subtype_type = std::uint64_t;
  5541. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5542. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5543. : container_type()
  5544. {}
  5545. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5546. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5547. : container_type(b)
  5548. {}
  5549. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5550. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5551. : container_type(std::move(b))
  5552. {}
  5553. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5554. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5555. : container_type(b)
  5556. , m_subtype(subtype_)
  5557. , m_has_subtype(true)
  5558. {}
  5559. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5560. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5561. : container_type(std::move(b))
  5562. , m_subtype(subtype_)
  5563. , m_has_subtype(true)
  5564. {}
  5565. bool operator==(const byte_container_with_subtype& rhs) const
  5566. {
  5567. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5568. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5569. }
  5570. bool operator!=(const byte_container_with_subtype& rhs) const
  5571. {
  5572. return !(rhs == *this);
  5573. }
  5574. /// @brief sets the binary subtype
  5575. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5576. void set_subtype(subtype_type subtype_) noexcept
  5577. {
  5578. m_subtype = subtype_;
  5579. m_has_subtype = true;
  5580. }
  5581. /// @brief return the binary subtype
  5582. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5583. constexpr subtype_type subtype() const noexcept
  5584. {
  5585. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5586. }
  5587. /// @brief return whether the value has a subtype
  5588. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5589. constexpr bool has_subtype() const noexcept
  5590. {
  5591. return m_has_subtype;
  5592. }
  5593. /// @brief clears the binary subtype
  5594. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5595. void clear_subtype() noexcept
  5596. {
  5597. m_subtype = 0;
  5598. m_has_subtype = false;
  5599. }
  5600. private:
  5601. subtype_type m_subtype = 0;
  5602. bool m_has_subtype = false;
  5603. };
  5604. NLOHMANN_JSON_NAMESPACE_END
  5605. // #include <nlohmann/detail/conversions/from_json.hpp>
  5606. // #include <nlohmann/detail/conversions/to_json.hpp>
  5607. // #include <nlohmann/detail/exceptions.hpp>
  5608. // #include <nlohmann/detail/hash.hpp>
  5609. // __ _____ _____ _____
  5610. // __| | __| | | | JSON for Modern C++
  5611. // | | |__ | | | | | | version 3.12.0
  5612. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5613. //
  5614. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  5615. // SPDX-License-Identifier: MIT
  5616. #include <cstdint> // uint8_t
  5617. #include <cstddef> // size_t
  5618. #include <functional> // hash
  5619. // #include <nlohmann/detail/abi_macros.hpp>
  5620. // #include <nlohmann/detail/value_t.hpp>
  5621. NLOHMANN_JSON_NAMESPACE_BEGIN
  5622. namespace detail
  5623. {
  5624. // boost::hash_combine
  5625. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5626. {
  5627. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5628. return seed;
  5629. }
  5630. /*!
  5631. @brief hash a JSON value
  5632. The hash function tries to rely on std::hash where possible. Furthermore, the
  5633. type of the JSON value is taken into account to have different hash values for
  5634. null, 0, 0U, and false, etc.
  5635. @tparam BasicJsonType basic_json specialization
  5636. @param j JSON value to hash
  5637. @return hash value of j
  5638. */
  5639. template<typename BasicJsonType>
  5640. std::size_t hash(const BasicJsonType& j)
  5641. {
  5642. using string_t = typename BasicJsonType::string_t;
  5643. using number_integer_t = typename BasicJsonType::number_integer_t;
  5644. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5645. using number_float_t = typename BasicJsonType::number_float_t;
  5646. const auto type = static_cast<std::size_t>(j.type());
  5647. switch (j.type())
  5648. {
  5649. case BasicJsonType::value_t::null:
  5650. case BasicJsonType::value_t::discarded:
  5651. {
  5652. return combine(type, 0);
  5653. }
  5654. case BasicJsonType::value_t::object:
  5655. {
  5656. auto seed = combine(type, j.size());
  5657. for (const auto& element : j.items())
  5658. {
  5659. const auto h = std::hash<string_t> {}(element.key());
  5660. seed = combine(seed, h);
  5661. seed = combine(seed, hash(element.value()));
  5662. }
  5663. return seed;
  5664. }
  5665. case BasicJsonType::value_t::array:
  5666. {
  5667. auto seed = combine(type, j.size());
  5668. for (const auto& element : j)
  5669. {
  5670. seed = combine(seed, hash(element));
  5671. }
  5672. return seed;
  5673. }
  5674. case BasicJsonType::value_t::string:
  5675. {
  5676. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5677. return combine(type, h);
  5678. }
  5679. case BasicJsonType::value_t::boolean:
  5680. {
  5681. const auto h = std::hash<bool> {}(j.template get<bool>());
  5682. return combine(type, h);
  5683. }
  5684. case BasicJsonType::value_t::number_integer:
  5685. {
  5686. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5687. return combine(type, h);
  5688. }
  5689. case BasicJsonType::value_t::number_unsigned:
  5690. {
  5691. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5692. return combine(type, h);
  5693. }
  5694. case BasicJsonType::value_t::number_float:
  5695. {
  5696. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5697. return combine(type, h);
  5698. }
  5699. case BasicJsonType::value_t::binary:
  5700. {
  5701. auto seed = combine(type, j.get_binary().size());
  5702. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5703. seed = combine(seed, h);
  5704. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5705. for (const auto byte : j.get_binary())
  5706. {
  5707. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5708. }
  5709. return seed;
  5710. }
  5711. default: // LCOV_EXCL_LINE
  5712. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5713. return 0; // LCOV_EXCL_LINE
  5714. }
  5715. }
  5716. } // namespace detail
  5717. NLOHMANN_JSON_NAMESPACE_END
  5718. // #include <nlohmann/detail/input/binary_reader.hpp>
  5719. // __ _____ _____ _____
  5720. // __| | __| | | | JSON for Modern C++
  5721. // | | |__ | | | | | | version 3.12.0
  5722. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5723. //
  5724. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  5725. // SPDX-License-Identifier: MIT
  5726. #include <algorithm> // generate_n
  5727. #include <array> // array
  5728. #include <cmath> // ldexp
  5729. #include <cstddef> // size_t
  5730. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5731. #include <cstdio> // snprintf
  5732. #include <cstring> // memcpy
  5733. #include <iterator> // back_inserter
  5734. #include <limits> // numeric_limits
  5735. #include <string> // char_traits, string
  5736. #include <utility> // make_pair, move
  5737. #include <vector> // vector
  5738. #ifdef __cpp_lib_byteswap
  5739. #include <bit> //byteswap
  5740. #endif
  5741. // #include <nlohmann/detail/exceptions.hpp>
  5742. // #include <nlohmann/detail/input/input_adapters.hpp>
  5743. // __ _____ _____ _____
  5744. // __| | __| | | | JSON for Modern C++
  5745. // | | |__ | | | | | | version 3.12.0
  5746. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5747. //
  5748. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  5749. // SPDX-License-Identifier: MIT
  5750. #include <array> // array
  5751. #include <cstddef> // size_t
  5752. #include <cstring> // strlen
  5753. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5754. #include <memory> // shared_ptr, make_shared, addressof
  5755. #include <numeric> // accumulate
  5756. #include <streambuf> // streambuf
  5757. #include <string> // string, char_traits
  5758. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5759. #include <utility> // pair, declval
  5760. #ifndef JSON_NO_IO
  5761. #include <cstdio> // FILE *
  5762. #include <istream> // istream
  5763. #endif // JSON_NO_IO
  5764. // #include <nlohmann/detail/exceptions.hpp>
  5765. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5766. // #include <nlohmann/detail/macro_scope.hpp>
  5767. // #include <nlohmann/detail/meta/type_traits.hpp>
  5768. NLOHMANN_JSON_NAMESPACE_BEGIN
  5769. namespace detail
  5770. {
  5771. /// the supported input formats
  5772. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5773. ////////////////////
  5774. // input adapters //
  5775. ////////////////////
  5776. #ifndef JSON_NO_IO
  5777. /*!
  5778. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5779. buffer. This adapter is a very low level adapter.
  5780. */
  5781. class file_input_adapter
  5782. {
  5783. public:
  5784. using char_type = char;
  5785. JSON_HEDLEY_NON_NULL(2)
  5786. explicit file_input_adapter(std::FILE* f) noexcept
  5787. : m_file(f)
  5788. {
  5789. JSON_ASSERT(m_file != nullptr);
  5790. }
  5791. // make class move-only
  5792. file_input_adapter(const file_input_adapter&) = delete;
  5793. file_input_adapter(file_input_adapter&&) noexcept = default;
  5794. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5795. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5796. ~file_input_adapter() = default;
  5797. std::char_traits<char>::int_type get_character() noexcept
  5798. {
  5799. return std::fgetc(m_file);
  5800. }
  5801. // returns the number of characters successfully read
  5802. template<class T>
  5803. std::size_t get_elements(T* dest, std::size_t count = 1)
  5804. {
  5805. return fread(dest, 1, sizeof(T) * count, m_file);
  5806. }
  5807. private:
  5808. /// the file pointer to read from
  5809. std::FILE* m_file;
  5810. };
  5811. /*!
  5812. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5813. beginning of input. Does not support changing the underlying std::streambuf
  5814. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5815. subsequent use of standard std::istream operations to process any input
  5816. characters following those used in parsing the JSON input. Clears the
  5817. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5818. subsequent call for input from the std::istream.
  5819. */
  5820. class input_stream_adapter
  5821. {
  5822. public:
  5823. using char_type = char;
  5824. ~input_stream_adapter()
  5825. {
  5826. // clear stream flags; we use underlying streambuf I/O, do not
  5827. // maintain ifstream flags, except eof
  5828. if (is != nullptr)
  5829. {
  5830. is->clear(is->rdstate() & std::ios::eofbit);
  5831. }
  5832. }
  5833. explicit input_stream_adapter(std::istream& i)
  5834. : is(&i), sb(i.rdbuf())
  5835. {}
  5836. // deleted because of pointer members
  5837. input_stream_adapter(const input_stream_adapter&) = delete;
  5838. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5839. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5840. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5841. : is(rhs.is), sb(rhs.sb)
  5842. {
  5843. rhs.is = nullptr;
  5844. rhs.sb = nullptr;
  5845. }
  5846. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5847. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5848. // end up as the same value, e.g., 0xFFFFFFFF.
  5849. std::char_traits<char>::int_type get_character()
  5850. {
  5851. auto res = sb->sbumpc();
  5852. // set eof manually, as we don't use the istream interface.
  5853. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5854. {
  5855. is->clear(is->rdstate() | std::ios::eofbit);
  5856. }
  5857. return res;
  5858. }
  5859. template<class T>
  5860. std::size_t get_elements(T* dest, std::size_t count = 1)
  5861. {
  5862. auto res = static_cast<std::size_t>(sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T))));
  5863. if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
  5864. {
  5865. is->clear(is->rdstate() | std::ios::eofbit);
  5866. }
  5867. return res;
  5868. }
  5869. private:
  5870. /// the associated input stream
  5871. std::istream* is = nullptr;
  5872. std::streambuf* sb = nullptr;
  5873. };
  5874. #endif // JSON_NO_IO
  5875. // General-purpose iterator-based adapter. It might not be as fast as
  5876. // theoretically possible for some containers, but it is extremely versatile.
  5877. template<typename IteratorType>
  5878. class iterator_input_adapter
  5879. {
  5880. public:
  5881. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5882. iterator_input_adapter(IteratorType first, IteratorType last)
  5883. : current(std::move(first)), end(std::move(last))
  5884. {}
  5885. typename char_traits<char_type>::int_type get_character()
  5886. {
  5887. if (JSON_HEDLEY_LIKELY(current != end))
  5888. {
  5889. auto result = char_traits<char_type>::to_int_type(*current);
  5890. std::advance(current, 1);
  5891. return result;
  5892. }
  5893. return char_traits<char_type>::eof();
  5894. }
  5895. // for general iterators, we cannot really do something better than falling back to processing the range one-by-one
  5896. template<class T>
  5897. std::size_t get_elements(T* dest, std::size_t count = 1)
  5898. {
  5899. auto* ptr = reinterpret_cast<char*>(dest);
  5900. for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
  5901. {
  5902. if (JSON_HEDLEY_LIKELY(current != end))
  5903. {
  5904. ptr[read_index] = static_cast<char>(*current);
  5905. std::advance(current, 1);
  5906. }
  5907. else
  5908. {
  5909. return read_index;
  5910. }
  5911. }
  5912. return count * sizeof(T);
  5913. }
  5914. private:
  5915. IteratorType current;
  5916. IteratorType end;
  5917. template<typename BaseInputAdapter, size_t T>
  5918. friend struct wide_string_input_helper;
  5919. bool empty() const
  5920. {
  5921. return current == end;
  5922. }
  5923. };
  5924. template<typename BaseInputAdapter, size_t T>
  5925. struct wide_string_input_helper;
  5926. template<typename BaseInputAdapter>
  5927. struct wide_string_input_helper<BaseInputAdapter, 4>
  5928. {
  5929. // UTF-32
  5930. static void fill_buffer(BaseInputAdapter& input,
  5931. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5932. size_t& utf8_bytes_index,
  5933. size_t& utf8_bytes_filled)
  5934. {
  5935. utf8_bytes_index = 0;
  5936. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5937. {
  5938. utf8_bytes[0] = std::char_traits<char>::eof();
  5939. utf8_bytes_filled = 1;
  5940. }
  5941. else
  5942. {
  5943. // get the current character
  5944. const auto wc = input.get_character();
  5945. // UTF-32 to UTF-8 encoding
  5946. if (wc < 0x80)
  5947. {
  5948. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5949. utf8_bytes_filled = 1;
  5950. }
  5951. else if (wc <= 0x7FF)
  5952. {
  5953. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5954. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5955. utf8_bytes_filled = 2;
  5956. }
  5957. else if (wc <= 0xFFFF)
  5958. {
  5959. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5960. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5961. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5962. utf8_bytes_filled = 3;
  5963. }
  5964. else if (wc <= 0x10FFFF)
  5965. {
  5966. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5967. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5968. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5969. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5970. utf8_bytes_filled = 4;
  5971. }
  5972. else
  5973. {
  5974. // unknown character
  5975. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5976. utf8_bytes_filled = 1;
  5977. }
  5978. }
  5979. }
  5980. };
  5981. template<typename BaseInputAdapter>
  5982. struct wide_string_input_helper<BaseInputAdapter, 2>
  5983. {
  5984. // UTF-16
  5985. static void fill_buffer(BaseInputAdapter& input,
  5986. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5987. size_t& utf8_bytes_index,
  5988. size_t& utf8_bytes_filled)
  5989. {
  5990. utf8_bytes_index = 0;
  5991. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5992. {
  5993. utf8_bytes[0] = std::char_traits<char>::eof();
  5994. utf8_bytes_filled = 1;
  5995. }
  5996. else
  5997. {
  5998. // get the current character
  5999. const auto wc = input.get_character();
  6000. // UTF-16 to UTF-8 encoding
  6001. if (wc < 0x80)
  6002. {
  6003. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  6004. utf8_bytes_filled = 1;
  6005. }
  6006. else if (wc <= 0x7FF)
  6007. {
  6008. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  6009. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  6010. utf8_bytes_filled = 2;
  6011. }
  6012. else if (0xD800 > wc || wc >= 0xE000)
  6013. {
  6014. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  6015. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  6016. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  6017. utf8_bytes_filled = 3;
  6018. }
  6019. else
  6020. {
  6021. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  6022. {
  6023. const auto wc2 = static_cast<unsigned int>(input.get_character());
  6024. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  6025. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  6026. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  6027. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  6028. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  6029. utf8_bytes_filled = 4;
  6030. }
  6031. else
  6032. {
  6033. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  6034. utf8_bytes_filled = 1;
  6035. }
  6036. }
  6037. }
  6038. }
  6039. };
  6040. // Wraps another input adapter to convert wide character types into individual bytes.
  6041. template<typename BaseInputAdapter, typename WideCharType>
  6042. class wide_string_input_adapter
  6043. {
  6044. public:
  6045. using char_type = char;
  6046. wide_string_input_adapter(BaseInputAdapter base)
  6047. : base_adapter(base) {}
  6048. typename std::char_traits<char>::int_type get_character() noexcept
  6049. {
  6050. // check if the buffer needs to be filled
  6051. if (utf8_bytes_index == utf8_bytes_filled)
  6052. {
  6053. fill_buffer<sizeof(WideCharType)>();
  6054. JSON_ASSERT(utf8_bytes_filled > 0);
  6055. JSON_ASSERT(utf8_bytes_index == 0);
  6056. }
  6057. // use buffer
  6058. JSON_ASSERT(utf8_bytes_filled > 0);
  6059. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  6060. return utf8_bytes[utf8_bytes_index++];
  6061. }
  6062. // parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
  6063. template<class T>
  6064. std::size_t get_elements(T* /*dest*/, std::size_t /*count*/ = 1)
  6065. {
  6066. JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
  6067. }
  6068. private:
  6069. BaseInputAdapter base_adapter;
  6070. template<size_t T>
  6071. void fill_buffer()
  6072. {
  6073. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  6074. }
  6075. /// a buffer for UTF-8 bytes
  6076. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  6077. /// index to the utf8_codes array for the next valid byte
  6078. std::size_t utf8_bytes_index = 0;
  6079. /// number of valid bytes in the utf8_codes array
  6080. std::size_t utf8_bytes_filled = 0;
  6081. };
  6082. template<typename IteratorType, typename Enable = void>
  6083. struct iterator_input_adapter_factory
  6084. {
  6085. using iterator_type = IteratorType;
  6086. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  6087. using adapter_type = iterator_input_adapter<iterator_type>;
  6088. static adapter_type create(IteratorType first, IteratorType last)
  6089. {
  6090. return adapter_type(std::move(first), std::move(last));
  6091. }
  6092. };
  6093. template<typename T>
  6094. struct is_iterator_of_multibyte
  6095. {
  6096. using value_type = typename std::iterator_traits<T>::value_type;
  6097. enum // NOLINT(cppcoreguidelines-use-enum-class)
  6098. {
  6099. value = sizeof(value_type) > 1
  6100. };
  6101. };
  6102. template<typename IteratorType>
  6103. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  6104. {
  6105. using iterator_type = IteratorType;
  6106. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  6107. using base_adapter_type = iterator_input_adapter<iterator_type>;
  6108. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  6109. static adapter_type create(IteratorType first, IteratorType last)
  6110. {
  6111. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  6112. }
  6113. };
  6114. // General purpose iterator-based input
  6115. template<typename IteratorType>
  6116. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  6117. {
  6118. using factory_type = iterator_input_adapter_factory<IteratorType>;
  6119. return factory_type::create(first, last);
  6120. }
  6121. // Convenience shorthand from container to iterator
  6122. // Enables ADL on begin(container) and end(container)
  6123. // Encloses the using declarations in namespace for not to leak them to outside scope
  6124. namespace container_input_adapter_factory_impl
  6125. {
  6126. using std::begin;
  6127. using std::end;
  6128. template<typename ContainerType, typename Enable = void>
  6129. struct container_input_adapter_factory {};
  6130. template<typename ContainerType>
  6131. struct container_input_adapter_factory< ContainerType,
  6132. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  6133. {
  6134. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  6135. static adapter_type create(const ContainerType& container)
  6136. {
  6137. return input_adapter(begin(container), end(container));
  6138. }
  6139. };
  6140. } // namespace container_input_adapter_factory_impl
  6141. template<typename ContainerType>
  6142. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  6143. {
  6144. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  6145. }
  6146. // specialization for std::string
  6147. using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
  6148. #ifndef JSON_NO_IO
  6149. // Special cases with fast paths
  6150. inline file_input_adapter input_adapter(std::FILE* file)
  6151. {
  6152. if (file == nullptr)
  6153. {
  6154. 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));
  6155. }
  6156. return file_input_adapter(file);
  6157. }
  6158. inline input_stream_adapter input_adapter(std::istream& stream)
  6159. {
  6160. return input_stream_adapter(stream);
  6161. }
  6162. inline input_stream_adapter input_adapter(std::istream&& stream)
  6163. {
  6164. return input_stream_adapter(stream);
  6165. }
  6166. #endif // JSON_NO_IO
  6167. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  6168. // Null-delimited strings, and the like.
  6169. template < typename CharT,
  6170. typename std::enable_if <
  6171. std::is_pointer<CharT>::value&&
  6172. !std::is_array<CharT>::value&&
  6173. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  6174. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  6175. int >::type = 0 >
  6176. contiguous_bytes_input_adapter input_adapter(CharT b)
  6177. {
  6178. if (b == nullptr)
  6179. {
  6180. 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));
  6181. }
  6182. auto length = std::strlen(reinterpret_cast<const char*>(b));
  6183. const auto* ptr = reinterpret_cast<const char*>(b);
  6184. return input_adapter(ptr, ptr + length); // cppcheck-suppress[nullPointerArithmeticRedundantCheck]
  6185. }
  6186. template<typename T, std::size_t N>
  6187. 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)
  6188. {
  6189. return input_adapter(array, array + N);
  6190. }
  6191. // This class only handles inputs of input_buffer_adapter type.
  6192. // It's required so that expressions like {ptr, len} can be implicitly cast
  6193. // to the correct adapter.
  6194. class span_input_adapter
  6195. {
  6196. public:
  6197. template < typename CharT,
  6198. typename std::enable_if <
  6199. std::is_pointer<CharT>::value&&
  6200. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  6201. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  6202. int >::type = 0 >
  6203. span_input_adapter(CharT b, std::size_t l)
  6204. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  6205. template<class IteratorType,
  6206. typename std::enable_if<
  6207. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  6208. int>::type = 0>
  6209. span_input_adapter(IteratorType first, IteratorType last)
  6210. : ia(input_adapter(first, last)) {}
  6211. contiguous_bytes_input_adapter&& get()
  6212. {
  6213. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  6214. }
  6215. private:
  6216. contiguous_bytes_input_adapter ia;
  6217. };
  6218. } // namespace detail
  6219. NLOHMANN_JSON_NAMESPACE_END
  6220. // #include <nlohmann/detail/input/json_sax.hpp>
  6221. // __ _____ _____ _____
  6222. // __| | __| | | | JSON for Modern C++
  6223. // | | |__ | | | | | | version 3.12.0
  6224. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6225. //
  6226. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  6227. // SPDX-License-Identifier: MIT
  6228. #include <cstddef>
  6229. #include <string> // string
  6230. #include <type_traits> // enable_if_t
  6231. #include <utility> // move
  6232. #include <vector> // vector
  6233. // #include <nlohmann/detail/exceptions.hpp>
  6234. // #include <nlohmann/detail/input/lexer.hpp>
  6235. // __ _____ _____ _____
  6236. // __| | __| | | | JSON for Modern C++
  6237. // | | |__ | | | | | | version 3.12.0
  6238. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6239. //
  6240. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  6241. // SPDX-License-Identifier: MIT
  6242. #include <array> // array
  6243. #include <clocale> // localeconv
  6244. #include <cstddef> // size_t
  6245. #include <cstdio> // snprintf
  6246. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6247. #include <initializer_list> // initializer_list
  6248. #include <string> // char_traits, string
  6249. #include <utility> // move
  6250. #include <vector> // vector
  6251. // #include <nlohmann/detail/input/input_adapters.hpp>
  6252. // #include <nlohmann/detail/input/position_t.hpp>
  6253. // #include <nlohmann/detail/macro_scope.hpp>
  6254. // #include <nlohmann/detail/meta/type_traits.hpp>
  6255. NLOHMANN_JSON_NAMESPACE_BEGIN
  6256. namespace detail
  6257. {
  6258. ///////////
  6259. // lexer //
  6260. ///////////
  6261. template<typename BasicJsonType>
  6262. class lexer_base
  6263. {
  6264. public:
  6265. /// token types for the parser
  6266. enum class token_type
  6267. {
  6268. uninitialized, ///< indicating the scanner is uninitialized
  6269. literal_true, ///< the `true` literal
  6270. literal_false, ///< the `false` literal
  6271. literal_null, ///< the `null` literal
  6272. value_string, ///< a string -- use get_string() for actual value
  6273. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6274. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6275. value_float, ///< an floating point number -- use get_number_float() for actual value
  6276. begin_array, ///< the character for array begin `[`
  6277. begin_object, ///< the character for object begin `{`
  6278. end_array, ///< the character for array end `]`
  6279. end_object, ///< the character for object end `}`
  6280. name_separator, ///< the name separator `:`
  6281. value_separator, ///< the value separator `,`
  6282. parse_error, ///< indicating a parse error
  6283. end_of_input, ///< indicating the end of the input buffer
  6284. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6285. };
  6286. /// return name of values of type token_type (only used for errors)
  6287. JSON_HEDLEY_RETURNS_NON_NULL
  6288. JSON_HEDLEY_CONST
  6289. static const char* token_type_name(const token_type t) noexcept
  6290. {
  6291. switch (t)
  6292. {
  6293. case token_type::uninitialized:
  6294. return "<uninitialized>";
  6295. case token_type::literal_true:
  6296. return "true literal";
  6297. case token_type::literal_false:
  6298. return "false literal";
  6299. case token_type::literal_null:
  6300. return "null literal";
  6301. case token_type::value_string:
  6302. return "string literal";
  6303. case token_type::value_unsigned:
  6304. case token_type::value_integer:
  6305. case token_type::value_float:
  6306. return "number literal";
  6307. case token_type::begin_array:
  6308. return "'['";
  6309. case token_type::begin_object:
  6310. return "'{'";
  6311. case token_type::end_array:
  6312. return "']'";
  6313. case token_type::end_object:
  6314. return "'}'";
  6315. case token_type::name_separator:
  6316. return "':'";
  6317. case token_type::value_separator:
  6318. return "','";
  6319. case token_type::parse_error:
  6320. return "<parse error>";
  6321. case token_type::end_of_input:
  6322. return "end of input";
  6323. case token_type::literal_or_value:
  6324. return "'[', '{', or a literal";
  6325. // LCOV_EXCL_START
  6326. default: // catch non-enum values
  6327. return "unknown token";
  6328. // LCOV_EXCL_STOP
  6329. }
  6330. }
  6331. };
  6332. /*!
  6333. @brief lexical analysis
  6334. This class organizes the lexical analysis during JSON deserialization.
  6335. */
  6336. template<typename BasicJsonType, typename InputAdapterType>
  6337. class lexer : public lexer_base<BasicJsonType>
  6338. {
  6339. using number_integer_t = typename BasicJsonType::number_integer_t;
  6340. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6341. using number_float_t = typename BasicJsonType::number_float_t;
  6342. using string_t = typename BasicJsonType::string_t;
  6343. using char_type = typename InputAdapterType::char_type;
  6344. using char_int_type = typename char_traits<char_type>::int_type;
  6345. public:
  6346. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6347. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6348. : ia(std::move(adapter))
  6349. , ignore_comments(ignore_comments_)
  6350. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6351. {}
  6352. // deleted because of pointer members
  6353. lexer(const lexer&) = delete;
  6354. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6355. lexer& operator=(lexer&) = delete;
  6356. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6357. ~lexer() = default;
  6358. private:
  6359. /////////////////////
  6360. // locales
  6361. /////////////////////
  6362. /// return the locale-dependent decimal point
  6363. JSON_HEDLEY_PURE
  6364. static char get_decimal_point() noexcept
  6365. {
  6366. const auto* loc = localeconv();
  6367. JSON_ASSERT(loc != nullptr);
  6368. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6369. }
  6370. /////////////////////
  6371. // scan functions
  6372. /////////////////////
  6373. /*!
  6374. @brief get codepoint from 4 hex characters following `\u`
  6375. For input "\u c1 c2 c3 c4" the codepoint is:
  6376. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6377. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6378. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6379. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6380. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6381. between the ASCII value of the character and the desired integer value.
  6382. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6383. non-hex character)
  6384. */
  6385. int get_codepoint()
  6386. {
  6387. // this function only makes sense after reading `\u`
  6388. JSON_ASSERT(current == 'u');
  6389. int codepoint = 0;
  6390. const auto factors = { 12u, 8u, 4u, 0u };
  6391. for (const auto factor : factors)
  6392. {
  6393. get();
  6394. if (current >= '0' && current <= '9')
  6395. {
  6396. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6397. }
  6398. else if (current >= 'A' && current <= 'F')
  6399. {
  6400. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6401. }
  6402. else if (current >= 'a' && current <= 'f')
  6403. {
  6404. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6405. }
  6406. else
  6407. {
  6408. return -1;
  6409. }
  6410. }
  6411. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6412. return codepoint;
  6413. }
  6414. /*!
  6415. @brief check if the next byte(s) are inside a given range
  6416. Adds the current byte and, for each passed range, reads a new byte and
  6417. checks if it is inside the range. If a violation was detected, set up an
  6418. error message and return false. Otherwise, return true.
  6419. @param[in] ranges list of integers; interpreted as list of pairs of
  6420. inclusive lower and upper bound, respectively
  6421. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6422. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6423. @return true if and only if no range violation was detected
  6424. */
  6425. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6426. {
  6427. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6428. add(current);
  6429. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6430. {
  6431. get();
  6432. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
  6433. {
  6434. add(current);
  6435. }
  6436. else
  6437. {
  6438. error_message = "invalid string: ill-formed UTF-8 byte";
  6439. return false;
  6440. }
  6441. }
  6442. return true;
  6443. }
  6444. /*!
  6445. @brief scan a string literal
  6446. This function scans a string according to Sect. 7 of RFC 8259. While
  6447. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6448. function returns successfully, token_buffer is *not* null-terminated (as it
  6449. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6450. string.
  6451. @return token_type::value_string if string could be successfully scanned,
  6452. token_type::parse_error otherwise
  6453. @note In case of errors, variable error_message contains a textual
  6454. description.
  6455. */
  6456. token_type scan_string()
  6457. {
  6458. // reset token_buffer (ignore opening quote)
  6459. reset();
  6460. // we entered the function by reading an open quote
  6461. JSON_ASSERT(current == '\"');
  6462. while (true)
  6463. {
  6464. // get the next character
  6465. switch (get())
  6466. {
  6467. // end of file while parsing the string
  6468. case char_traits<char_type>::eof():
  6469. {
  6470. error_message = "invalid string: missing closing quote";
  6471. return token_type::parse_error;
  6472. }
  6473. // closing quote
  6474. case '\"':
  6475. {
  6476. return token_type::value_string;
  6477. }
  6478. // escapes
  6479. case '\\':
  6480. {
  6481. switch (get())
  6482. {
  6483. // quotation mark
  6484. case '\"':
  6485. add('\"');
  6486. break;
  6487. // reverse solidus
  6488. case '\\':
  6489. add('\\');
  6490. break;
  6491. // solidus
  6492. case '/':
  6493. add('/');
  6494. break;
  6495. // backspace
  6496. case 'b':
  6497. add('\b');
  6498. break;
  6499. // form feed
  6500. case 'f':
  6501. add('\f');
  6502. break;
  6503. // line feed
  6504. case 'n':
  6505. add('\n');
  6506. break;
  6507. // carriage return
  6508. case 'r':
  6509. add('\r');
  6510. break;
  6511. // tab
  6512. case 't':
  6513. add('\t');
  6514. break;
  6515. // unicode escapes
  6516. case 'u':
  6517. {
  6518. const int codepoint1 = get_codepoint();
  6519. int codepoint = codepoint1; // start with codepoint1
  6520. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6521. {
  6522. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6523. return token_type::parse_error;
  6524. }
  6525. // check if code point is a high surrogate
  6526. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6527. {
  6528. // expect next \uxxxx entry
  6529. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6530. {
  6531. const int codepoint2 = get_codepoint();
  6532. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6533. {
  6534. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6535. return token_type::parse_error;
  6536. }
  6537. // check if codepoint2 is a low surrogate
  6538. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6539. {
  6540. // overwrite codepoint
  6541. codepoint = static_cast<int>(
  6542. // high surrogate occupies the most significant 22 bits
  6543. (static_cast<unsigned int>(codepoint1) << 10u)
  6544. // low surrogate occupies the least significant 15 bits
  6545. + static_cast<unsigned int>(codepoint2)
  6546. // there is still the 0xD800, 0xDC00, and 0x10000 noise
  6547. // in the result, so we have to subtract with:
  6548. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6549. - 0x35FDC00u);
  6550. }
  6551. else
  6552. {
  6553. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6554. return token_type::parse_error;
  6555. }
  6556. }
  6557. else
  6558. {
  6559. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6560. return token_type::parse_error;
  6561. }
  6562. }
  6563. else
  6564. {
  6565. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6566. {
  6567. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6568. return token_type::parse_error;
  6569. }
  6570. }
  6571. // the result of the above calculation yields a proper codepoint
  6572. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6573. // translate codepoint into bytes
  6574. if (codepoint < 0x80)
  6575. {
  6576. // 1-byte characters: 0xxxxxxx (ASCII)
  6577. add(static_cast<char_int_type>(codepoint));
  6578. }
  6579. else if (codepoint <= 0x7FF)
  6580. {
  6581. // 2-byte characters: 110xxxxx 10xxxxxx
  6582. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6583. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6584. }
  6585. else if (codepoint <= 0xFFFF)
  6586. {
  6587. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6588. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6589. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6590. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6591. }
  6592. else
  6593. {
  6594. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6595. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6596. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6597. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6598. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6599. }
  6600. break;
  6601. }
  6602. // other characters after escape
  6603. default:
  6604. error_message = "invalid string: forbidden character after backslash";
  6605. return token_type::parse_error;
  6606. }
  6607. break;
  6608. }
  6609. // invalid control characters
  6610. case 0x00:
  6611. {
  6612. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6613. return token_type::parse_error;
  6614. }
  6615. case 0x01:
  6616. {
  6617. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6618. return token_type::parse_error;
  6619. }
  6620. case 0x02:
  6621. {
  6622. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6623. return token_type::parse_error;
  6624. }
  6625. case 0x03:
  6626. {
  6627. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6628. return token_type::parse_error;
  6629. }
  6630. case 0x04:
  6631. {
  6632. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6633. return token_type::parse_error;
  6634. }
  6635. case 0x05:
  6636. {
  6637. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6638. return token_type::parse_error;
  6639. }
  6640. case 0x06:
  6641. {
  6642. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6643. return token_type::parse_error;
  6644. }
  6645. case 0x07:
  6646. {
  6647. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6648. return token_type::parse_error;
  6649. }
  6650. case 0x08:
  6651. {
  6652. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6653. return token_type::parse_error;
  6654. }
  6655. case 0x09:
  6656. {
  6657. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6658. return token_type::parse_error;
  6659. }
  6660. case 0x0A:
  6661. {
  6662. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6663. return token_type::parse_error;
  6664. }
  6665. case 0x0B:
  6666. {
  6667. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6668. return token_type::parse_error;
  6669. }
  6670. case 0x0C:
  6671. {
  6672. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6673. return token_type::parse_error;
  6674. }
  6675. case 0x0D:
  6676. {
  6677. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6678. return token_type::parse_error;
  6679. }
  6680. case 0x0E:
  6681. {
  6682. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6683. return token_type::parse_error;
  6684. }
  6685. case 0x0F:
  6686. {
  6687. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6688. return token_type::parse_error;
  6689. }
  6690. case 0x10:
  6691. {
  6692. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6693. return token_type::parse_error;
  6694. }
  6695. case 0x11:
  6696. {
  6697. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6698. return token_type::parse_error;
  6699. }
  6700. case 0x12:
  6701. {
  6702. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6703. return token_type::parse_error;
  6704. }
  6705. case 0x13:
  6706. {
  6707. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6708. return token_type::parse_error;
  6709. }
  6710. case 0x14:
  6711. {
  6712. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6713. return token_type::parse_error;
  6714. }
  6715. case 0x15:
  6716. {
  6717. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6718. return token_type::parse_error;
  6719. }
  6720. case 0x16:
  6721. {
  6722. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6723. return token_type::parse_error;
  6724. }
  6725. case 0x17:
  6726. {
  6727. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6728. return token_type::parse_error;
  6729. }
  6730. case 0x18:
  6731. {
  6732. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6733. return token_type::parse_error;
  6734. }
  6735. case 0x19:
  6736. {
  6737. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6738. return token_type::parse_error;
  6739. }
  6740. case 0x1A:
  6741. {
  6742. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6743. return token_type::parse_error;
  6744. }
  6745. case 0x1B:
  6746. {
  6747. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6748. return token_type::parse_error;
  6749. }
  6750. case 0x1C:
  6751. {
  6752. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6753. return token_type::parse_error;
  6754. }
  6755. case 0x1D:
  6756. {
  6757. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6758. return token_type::parse_error;
  6759. }
  6760. case 0x1E:
  6761. {
  6762. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6763. return token_type::parse_error;
  6764. }
  6765. case 0x1F:
  6766. {
  6767. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6768. return token_type::parse_error;
  6769. }
  6770. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6771. case 0x20:
  6772. case 0x21:
  6773. case 0x23:
  6774. case 0x24:
  6775. case 0x25:
  6776. case 0x26:
  6777. case 0x27:
  6778. case 0x28:
  6779. case 0x29:
  6780. case 0x2A:
  6781. case 0x2B:
  6782. case 0x2C:
  6783. case 0x2D:
  6784. case 0x2E:
  6785. case 0x2F:
  6786. case 0x30:
  6787. case 0x31:
  6788. case 0x32:
  6789. case 0x33:
  6790. case 0x34:
  6791. case 0x35:
  6792. case 0x36:
  6793. case 0x37:
  6794. case 0x38:
  6795. case 0x39:
  6796. case 0x3A:
  6797. case 0x3B:
  6798. case 0x3C:
  6799. case 0x3D:
  6800. case 0x3E:
  6801. case 0x3F:
  6802. case 0x40:
  6803. case 0x41:
  6804. case 0x42:
  6805. case 0x43:
  6806. case 0x44:
  6807. case 0x45:
  6808. case 0x46:
  6809. case 0x47:
  6810. case 0x48:
  6811. case 0x49:
  6812. case 0x4A:
  6813. case 0x4B:
  6814. case 0x4C:
  6815. case 0x4D:
  6816. case 0x4E:
  6817. case 0x4F:
  6818. case 0x50:
  6819. case 0x51:
  6820. case 0x52:
  6821. case 0x53:
  6822. case 0x54:
  6823. case 0x55:
  6824. case 0x56:
  6825. case 0x57:
  6826. case 0x58:
  6827. case 0x59:
  6828. case 0x5A:
  6829. case 0x5B:
  6830. case 0x5D:
  6831. case 0x5E:
  6832. case 0x5F:
  6833. case 0x60:
  6834. case 0x61:
  6835. case 0x62:
  6836. case 0x63:
  6837. case 0x64:
  6838. case 0x65:
  6839. case 0x66:
  6840. case 0x67:
  6841. case 0x68:
  6842. case 0x69:
  6843. case 0x6A:
  6844. case 0x6B:
  6845. case 0x6C:
  6846. case 0x6D:
  6847. case 0x6E:
  6848. case 0x6F:
  6849. case 0x70:
  6850. case 0x71:
  6851. case 0x72:
  6852. case 0x73:
  6853. case 0x74:
  6854. case 0x75:
  6855. case 0x76:
  6856. case 0x77:
  6857. case 0x78:
  6858. case 0x79:
  6859. case 0x7A:
  6860. case 0x7B:
  6861. case 0x7C:
  6862. case 0x7D:
  6863. case 0x7E:
  6864. case 0x7F:
  6865. {
  6866. add(current);
  6867. break;
  6868. }
  6869. // U+0080..U+07FF: bytes C2..DF 80..BF
  6870. case 0xC2:
  6871. case 0xC3:
  6872. case 0xC4:
  6873. case 0xC5:
  6874. case 0xC6:
  6875. case 0xC7:
  6876. case 0xC8:
  6877. case 0xC9:
  6878. case 0xCA:
  6879. case 0xCB:
  6880. case 0xCC:
  6881. case 0xCD:
  6882. case 0xCE:
  6883. case 0xCF:
  6884. case 0xD0:
  6885. case 0xD1:
  6886. case 0xD2:
  6887. case 0xD3:
  6888. case 0xD4:
  6889. case 0xD5:
  6890. case 0xD6:
  6891. case 0xD7:
  6892. case 0xD8:
  6893. case 0xD9:
  6894. case 0xDA:
  6895. case 0xDB:
  6896. case 0xDC:
  6897. case 0xDD:
  6898. case 0xDE:
  6899. case 0xDF:
  6900. {
  6901. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6902. {
  6903. return token_type::parse_error;
  6904. }
  6905. break;
  6906. }
  6907. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6908. case 0xE0:
  6909. {
  6910. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6911. {
  6912. return token_type::parse_error;
  6913. }
  6914. break;
  6915. }
  6916. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6917. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6918. case 0xE1:
  6919. case 0xE2:
  6920. case 0xE3:
  6921. case 0xE4:
  6922. case 0xE5:
  6923. case 0xE6:
  6924. case 0xE7:
  6925. case 0xE8:
  6926. case 0xE9:
  6927. case 0xEA:
  6928. case 0xEB:
  6929. case 0xEC:
  6930. case 0xEE:
  6931. case 0xEF:
  6932. {
  6933. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  6934. {
  6935. return token_type::parse_error;
  6936. }
  6937. break;
  6938. }
  6939. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  6940. case 0xED:
  6941. {
  6942. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  6943. {
  6944. return token_type::parse_error;
  6945. }
  6946. break;
  6947. }
  6948. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  6949. case 0xF0:
  6950. {
  6951. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6952. {
  6953. return token_type::parse_error;
  6954. }
  6955. break;
  6956. }
  6957. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  6958. case 0xF1:
  6959. case 0xF2:
  6960. case 0xF3:
  6961. {
  6962. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6963. {
  6964. return token_type::parse_error;
  6965. }
  6966. break;
  6967. }
  6968. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  6969. case 0xF4:
  6970. {
  6971. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  6972. {
  6973. return token_type::parse_error;
  6974. }
  6975. break;
  6976. }
  6977. // the remaining bytes (80..C1 and F5..FF) are ill-formed
  6978. default:
  6979. {
  6980. error_message = "invalid string: ill-formed UTF-8 byte";
  6981. return token_type::parse_error;
  6982. }
  6983. }
  6984. }
  6985. }
  6986. /*!
  6987. * @brief scan a comment
  6988. * @return whether comment could be scanned successfully
  6989. */
  6990. bool scan_comment()
  6991. {
  6992. switch (get())
  6993. {
  6994. // single-line comments skip input until a newline or EOF is read
  6995. case '/':
  6996. {
  6997. while (true)
  6998. {
  6999. switch (get())
  7000. {
  7001. case '\n':
  7002. case '\r':
  7003. case char_traits<char_type>::eof():
  7004. case '\0':
  7005. return true;
  7006. default:
  7007. break;
  7008. }
  7009. }
  7010. JSON_HEDLEY_UNREACHABLE();
  7011. }
  7012. // multi-line comments skip input until */ is read
  7013. case '*':
  7014. {
  7015. while (true)
  7016. {
  7017. switch (get())
  7018. {
  7019. case char_traits<char_type>::eof():
  7020. case '\0':
  7021. {
  7022. error_message = "invalid comment; missing closing '*/'";
  7023. return false;
  7024. }
  7025. case '*':
  7026. {
  7027. switch (get())
  7028. {
  7029. case '/':
  7030. return true;
  7031. default:
  7032. {
  7033. unget();
  7034. continue;
  7035. }
  7036. }
  7037. }
  7038. default:
  7039. continue;
  7040. }
  7041. }
  7042. JSON_HEDLEY_UNREACHABLE();
  7043. }
  7044. // unexpected character after reading '/'
  7045. default:
  7046. {
  7047. error_message = "invalid comment; expecting '/' or '*' after '/'";
  7048. return false;
  7049. }
  7050. }
  7051. }
  7052. JSON_HEDLEY_NON_NULL(2)
  7053. static void strtof(float& f, const char* str, char** endptr) noexcept
  7054. {
  7055. f = std::strtof(str, endptr);
  7056. }
  7057. JSON_HEDLEY_NON_NULL(2)
  7058. static void strtof(double& f, const char* str, char** endptr) noexcept
  7059. {
  7060. f = std::strtod(str, endptr);
  7061. }
  7062. JSON_HEDLEY_NON_NULL(2)
  7063. static void strtof(long double& f, const char* str, char** endptr) noexcept
  7064. {
  7065. f = std::strtold(str, endptr);
  7066. }
  7067. /*!
  7068. @brief scan a number literal
  7069. This function scans a string according to Sect. 6 of RFC 8259.
  7070. The function is realized with a deterministic finite state machine derived
  7071. from the grammar described in RFC 8259. Starting in state "init", the
  7072. input is read and used to determined the next state. Only state "done"
  7073. accepts the number. State "error" is a trap state to model errors. In the
  7074. table below, "anything" means any character but the ones listed before.
  7075. state | 0 | 1-9 | e E | + | - | . | anything
  7076. ---------|----------|----------|----------|---------|---------|----------|-----------
  7077. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  7078. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  7079. zero | done | done | exponent | done | done | decimal1 | done
  7080. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  7081. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  7082. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  7083. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  7084. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  7085. any2 | any2 | any2 | done | done | done | done | done
  7086. The state machine is realized with one label per state (prefixed with
  7087. "scan_number_") and `goto` statements between them. The state machine
  7088. contains cycles, but any cycle can be left when EOF is read. Therefore,
  7089. the function is guaranteed to terminate.
  7090. During scanning, the read bytes are stored in token_buffer. This string is
  7091. then converted to a signed integer, an unsigned integer, or a
  7092. floating-point number.
  7093. @return token_type::value_unsigned, token_type::value_integer, or
  7094. token_type::value_float if number could be successfully scanned,
  7095. token_type::parse_error otherwise
  7096. @note The scanner is independent of the current locale. Internally, the
  7097. locale's decimal point is used instead of `.` to work with the
  7098. locale-dependent converters.
  7099. */
  7100. 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.
  7101. {
  7102. // reset token_buffer to store the number's bytes
  7103. reset();
  7104. // the type of the parsed number; initially set to unsigned; will be
  7105. // changed if minus sign, decimal point, or exponent is read
  7106. token_type number_type = token_type::value_unsigned;
  7107. // state (init): we just found out we need to scan a number
  7108. switch (current)
  7109. {
  7110. case '-':
  7111. {
  7112. add(current);
  7113. goto scan_number_minus;
  7114. }
  7115. case '0':
  7116. {
  7117. add(current);
  7118. goto scan_number_zero;
  7119. }
  7120. case '1':
  7121. case '2':
  7122. case '3':
  7123. case '4':
  7124. case '5':
  7125. case '6':
  7126. case '7':
  7127. case '8':
  7128. case '9':
  7129. {
  7130. add(current);
  7131. goto scan_number_any1;
  7132. }
  7133. // all other characters are rejected outside scan_number()
  7134. default: // LCOV_EXCL_LINE
  7135. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7136. }
  7137. scan_number_minus:
  7138. // state: we just parsed a leading minus sign
  7139. number_type = token_type::value_integer;
  7140. switch (get())
  7141. {
  7142. case '0':
  7143. {
  7144. add(current);
  7145. goto scan_number_zero;
  7146. }
  7147. case '1':
  7148. case '2':
  7149. case '3':
  7150. case '4':
  7151. case '5':
  7152. case '6':
  7153. case '7':
  7154. case '8':
  7155. case '9':
  7156. {
  7157. add(current);
  7158. goto scan_number_any1;
  7159. }
  7160. default:
  7161. {
  7162. error_message = "invalid number; expected digit after '-'";
  7163. return token_type::parse_error;
  7164. }
  7165. }
  7166. scan_number_zero:
  7167. // state: we just parse a zero (maybe with a leading minus sign)
  7168. switch (get())
  7169. {
  7170. case '.':
  7171. {
  7172. add(decimal_point_char);
  7173. decimal_point_position = token_buffer.size() - 1;
  7174. goto scan_number_decimal1;
  7175. }
  7176. case 'e':
  7177. case 'E':
  7178. {
  7179. add(current);
  7180. goto scan_number_exponent;
  7181. }
  7182. default:
  7183. goto scan_number_done;
  7184. }
  7185. scan_number_any1:
  7186. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7187. switch (get())
  7188. {
  7189. case '0':
  7190. case '1':
  7191. case '2':
  7192. case '3':
  7193. case '4':
  7194. case '5':
  7195. case '6':
  7196. case '7':
  7197. case '8':
  7198. case '9':
  7199. {
  7200. add(current);
  7201. goto scan_number_any1;
  7202. }
  7203. case '.':
  7204. {
  7205. add(decimal_point_char);
  7206. decimal_point_position = token_buffer.size() - 1;
  7207. goto scan_number_decimal1;
  7208. }
  7209. case 'e':
  7210. case 'E':
  7211. {
  7212. add(current);
  7213. goto scan_number_exponent;
  7214. }
  7215. default:
  7216. goto scan_number_done;
  7217. }
  7218. scan_number_decimal1:
  7219. // state: we just parsed a decimal point
  7220. number_type = token_type::value_float;
  7221. switch (get())
  7222. {
  7223. case '0':
  7224. case '1':
  7225. case '2':
  7226. case '3':
  7227. case '4':
  7228. case '5':
  7229. case '6':
  7230. case '7':
  7231. case '8':
  7232. case '9':
  7233. {
  7234. add(current);
  7235. goto scan_number_decimal2;
  7236. }
  7237. default:
  7238. {
  7239. error_message = "invalid number; expected digit after '.'";
  7240. return token_type::parse_error;
  7241. }
  7242. }
  7243. scan_number_decimal2:
  7244. // we just parsed at least one number after a decimal point
  7245. switch (get())
  7246. {
  7247. case '0':
  7248. case '1':
  7249. case '2':
  7250. case '3':
  7251. case '4':
  7252. case '5':
  7253. case '6':
  7254. case '7':
  7255. case '8':
  7256. case '9':
  7257. {
  7258. add(current);
  7259. goto scan_number_decimal2;
  7260. }
  7261. case 'e':
  7262. case 'E':
  7263. {
  7264. add(current);
  7265. goto scan_number_exponent;
  7266. }
  7267. default:
  7268. goto scan_number_done;
  7269. }
  7270. scan_number_exponent:
  7271. // we just parsed an exponent
  7272. number_type = token_type::value_float;
  7273. switch (get())
  7274. {
  7275. case '+':
  7276. case '-':
  7277. {
  7278. add(current);
  7279. goto scan_number_sign;
  7280. }
  7281. case '0':
  7282. case '1':
  7283. case '2':
  7284. case '3':
  7285. case '4':
  7286. case '5':
  7287. case '6':
  7288. case '7':
  7289. case '8':
  7290. case '9':
  7291. {
  7292. add(current);
  7293. goto scan_number_any2;
  7294. }
  7295. default:
  7296. {
  7297. error_message =
  7298. "invalid number; expected '+', '-', or digit after exponent";
  7299. return token_type::parse_error;
  7300. }
  7301. }
  7302. scan_number_sign:
  7303. // we just parsed an exponent sign
  7304. switch (get())
  7305. {
  7306. case '0':
  7307. case '1':
  7308. case '2':
  7309. case '3':
  7310. case '4':
  7311. case '5':
  7312. case '6':
  7313. case '7':
  7314. case '8':
  7315. case '9':
  7316. {
  7317. add(current);
  7318. goto scan_number_any2;
  7319. }
  7320. default:
  7321. {
  7322. error_message = "invalid number; expected digit after exponent sign";
  7323. return token_type::parse_error;
  7324. }
  7325. }
  7326. scan_number_any2:
  7327. // we just parsed a number after the exponent or exponent sign
  7328. switch (get())
  7329. {
  7330. case '0':
  7331. case '1':
  7332. case '2':
  7333. case '3':
  7334. case '4':
  7335. case '5':
  7336. case '6':
  7337. case '7':
  7338. case '8':
  7339. case '9':
  7340. {
  7341. add(current);
  7342. goto scan_number_any2;
  7343. }
  7344. default:
  7345. goto scan_number_done;
  7346. }
  7347. scan_number_done:
  7348. // unget the character after the number (we only read it to know that
  7349. // we are done scanning a number)
  7350. unget();
  7351. char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7352. errno = 0;
  7353. // try to parse integers first and fall back to floats
  7354. if (number_type == token_type::value_unsigned)
  7355. {
  7356. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7357. // we checked the number format before
  7358. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7359. if (errno != ERANGE)
  7360. {
  7361. value_unsigned = static_cast<number_unsigned_t>(x);
  7362. if (value_unsigned == x)
  7363. {
  7364. return token_type::value_unsigned;
  7365. }
  7366. }
  7367. }
  7368. else if (number_type == token_type::value_integer)
  7369. {
  7370. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7371. // we checked the number format before
  7372. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7373. if (errno != ERANGE)
  7374. {
  7375. value_integer = static_cast<number_integer_t>(x);
  7376. if (value_integer == x)
  7377. {
  7378. return token_type::value_integer;
  7379. }
  7380. }
  7381. }
  7382. // this code is reached if we parse a floating-point number or if an
  7383. // integer conversion above failed
  7384. strtof(value_float, token_buffer.data(), &endptr);
  7385. // we checked the number format before
  7386. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7387. return token_type::value_float;
  7388. }
  7389. /*!
  7390. @param[in] literal_text the literal text to expect
  7391. @param[in] length the length of the passed literal text
  7392. @param[in] return_type the token type to return on success
  7393. */
  7394. JSON_HEDLEY_NON_NULL(2)
  7395. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7396. token_type return_type)
  7397. {
  7398. JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7399. for (std::size_t i = 1; i < length; ++i)
  7400. {
  7401. if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7402. {
  7403. error_message = "invalid literal";
  7404. return token_type::parse_error;
  7405. }
  7406. }
  7407. return return_type;
  7408. }
  7409. /////////////////////
  7410. // input management
  7411. /////////////////////
  7412. /// reset token_buffer; current character is beginning of token
  7413. void reset() noexcept
  7414. {
  7415. token_buffer.clear();
  7416. token_string.clear();
  7417. decimal_point_position = std::string::npos;
  7418. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7419. }
  7420. /*
  7421. @brief get next character from the input
  7422. This function provides the interface to the used input adapter. It does
  7423. not throw in case the input reached EOF, but returns a
  7424. `char_traits<char>::eof()` in that case. Stores the scanned characters
  7425. for use in error messages.
  7426. @return character read from the input
  7427. */
  7428. char_int_type get()
  7429. {
  7430. ++position.chars_read_total;
  7431. ++position.chars_read_current_line;
  7432. if (next_unget)
  7433. {
  7434. // only reset the next_unget variable and work with current
  7435. next_unget = false;
  7436. }
  7437. else
  7438. {
  7439. current = ia.get_character();
  7440. }
  7441. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7442. {
  7443. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7444. }
  7445. if (current == '\n')
  7446. {
  7447. ++position.lines_read;
  7448. position.chars_read_current_line = 0;
  7449. }
  7450. return current;
  7451. }
  7452. /*!
  7453. @brief unget current character (read it again on next get)
  7454. We implement unget by setting variable next_unget to true. The input is not
  7455. changed - we just simulate ungetting by modifying chars_read_total,
  7456. chars_read_current_line, and token_string. The next call to get() will
  7457. behave as if the unget character is read again.
  7458. */
  7459. void unget()
  7460. {
  7461. next_unget = true;
  7462. --position.chars_read_total;
  7463. // in case we "unget" a newline, we have to also decrement the lines_read
  7464. if (position.chars_read_current_line == 0)
  7465. {
  7466. if (position.lines_read > 0)
  7467. {
  7468. --position.lines_read;
  7469. }
  7470. }
  7471. else
  7472. {
  7473. --position.chars_read_current_line;
  7474. }
  7475. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7476. {
  7477. JSON_ASSERT(!token_string.empty());
  7478. token_string.pop_back();
  7479. }
  7480. }
  7481. /// add a character to token_buffer
  7482. void add(char_int_type c)
  7483. {
  7484. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7485. }
  7486. public:
  7487. /////////////////////
  7488. // value getters
  7489. /////////////////////
  7490. /// return integer value
  7491. constexpr number_integer_t get_number_integer() const noexcept
  7492. {
  7493. return value_integer;
  7494. }
  7495. /// return unsigned integer value
  7496. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7497. {
  7498. return value_unsigned;
  7499. }
  7500. /// return floating-point value
  7501. constexpr number_float_t get_number_float() const noexcept
  7502. {
  7503. return value_float;
  7504. }
  7505. /// return current string value (implicitly resets the token; useful only once)
  7506. string_t& get_string()
  7507. {
  7508. // translate decimal points from locale back to '.' (#4084)
  7509. if (decimal_point_char != '.' && decimal_point_position != std::string::npos)
  7510. {
  7511. token_buffer[decimal_point_position] = '.';
  7512. }
  7513. return token_buffer;
  7514. }
  7515. /////////////////////
  7516. // diagnostics
  7517. /////////////////////
  7518. /// return position of last read token
  7519. constexpr position_t get_position() const noexcept
  7520. {
  7521. return position;
  7522. }
  7523. /// return the last read token (for errors only). Will never contain EOF
  7524. /// (an arbitrary value that is not a valid char value, often -1), because
  7525. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7526. std::string get_token_string() const
  7527. {
  7528. // escape control characters
  7529. std::string result;
  7530. for (const auto c : token_string)
  7531. {
  7532. if (static_cast<unsigned char>(c) <= '\x1F')
  7533. {
  7534. // escape control characters
  7535. std::array<char, 9> cs{{}};
  7536. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7537. result += cs.data();
  7538. }
  7539. else
  7540. {
  7541. // add character as is
  7542. result.push_back(static_cast<std::string::value_type>(c));
  7543. }
  7544. }
  7545. return result;
  7546. }
  7547. /// return syntax error message
  7548. JSON_HEDLEY_RETURNS_NON_NULL
  7549. constexpr const char* get_error_message() const noexcept
  7550. {
  7551. return error_message;
  7552. }
  7553. /////////////////////
  7554. // actual scanner
  7555. /////////////////////
  7556. /*!
  7557. @brief skip the UTF-8 byte order mark
  7558. @return true iff there is no BOM or the correct BOM has been skipped
  7559. */
  7560. bool skip_bom()
  7561. {
  7562. if (get() == 0xEF)
  7563. {
  7564. // check if we completely parse the BOM
  7565. return get() == 0xBB && get() == 0xBF;
  7566. }
  7567. // the first character is not the beginning of the BOM; unget it to
  7568. // process is later
  7569. unget();
  7570. return true;
  7571. }
  7572. void skip_whitespace()
  7573. {
  7574. do
  7575. {
  7576. get();
  7577. }
  7578. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7579. }
  7580. token_type scan()
  7581. {
  7582. // initially, skip the BOM
  7583. if (position.chars_read_total == 0 && !skip_bom())
  7584. {
  7585. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7586. return token_type::parse_error;
  7587. }
  7588. // read the next character and ignore whitespace
  7589. skip_whitespace();
  7590. // ignore comments
  7591. while (ignore_comments && current == '/')
  7592. {
  7593. if (!scan_comment())
  7594. {
  7595. return token_type::parse_error;
  7596. }
  7597. // skip following whitespace
  7598. skip_whitespace();
  7599. }
  7600. switch (current)
  7601. {
  7602. // structural characters
  7603. case '[':
  7604. return token_type::begin_array;
  7605. case ']':
  7606. return token_type::end_array;
  7607. case '{':
  7608. return token_type::begin_object;
  7609. case '}':
  7610. return token_type::end_object;
  7611. case ':':
  7612. return token_type::name_separator;
  7613. case ',':
  7614. return token_type::value_separator;
  7615. // literals
  7616. case 't':
  7617. {
  7618. 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')}};
  7619. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7620. }
  7621. case 'f':
  7622. {
  7623. 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')}};
  7624. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7625. }
  7626. case 'n':
  7627. {
  7628. 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')}};
  7629. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7630. }
  7631. // string
  7632. case '\"':
  7633. return scan_string();
  7634. // number
  7635. case '-':
  7636. case '0':
  7637. case '1':
  7638. case '2':
  7639. case '3':
  7640. case '4':
  7641. case '5':
  7642. case '6':
  7643. case '7':
  7644. case '8':
  7645. case '9':
  7646. return scan_number();
  7647. // end of input (the null byte is needed when parsing from
  7648. // string literals)
  7649. case '\0':
  7650. case char_traits<char_type>::eof():
  7651. return token_type::end_of_input;
  7652. // error
  7653. default:
  7654. error_message = "invalid literal";
  7655. return token_type::parse_error;
  7656. }
  7657. }
  7658. private:
  7659. /// input adapter
  7660. InputAdapterType ia;
  7661. /// whether comments should be ignored (true) or signaled as errors (false)
  7662. const bool ignore_comments = false;
  7663. /// the current character
  7664. char_int_type current = char_traits<char_type>::eof();
  7665. /// whether the next get() call should just return current
  7666. bool next_unget = false;
  7667. /// the start position of the current token
  7668. position_t position {};
  7669. /// raw input token string (for error messages)
  7670. std::vector<char_type> token_string {};
  7671. /// buffer for variable-length tokens (numbers, strings)
  7672. string_t token_buffer {};
  7673. /// a description of occurred lexer errors
  7674. const char* error_message = "";
  7675. // number values
  7676. number_integer_t value_integer = 0;
  7677. number_unsigned_t value_unsigned = 0;
  7678. number_float_t value_float = 0;
  7679. /// the decimal point
  7680. const char_int_type decimal_point_char = '.';
  7681. /// the position of the decimal point in the input
  7682. std::size_t decimal_point_position = std::string::npos;
  7683. };
  7684. } // namespace detail
  7685. NLOHMANN_JSON_NAMESPACE_END
  7686. // #include <nlohmann/detail/macro_scope.hpp>
  7687. // #include <nlohmann/detail/string_concat.hpp>
  7688. NLOHMANN_JSON_NAMESPACE_BEGIN
  7689. /*!
  7690. @brief SAX interface
  7691. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  7692. Each function is called in different situations while the input is parsed. The
  7693. boolean return value informs the parser whether to continue processing the
  7694. input.
  7695. */
  7696. template<typename BasicJsonType>
  7697. struct json_sax
  7698. {
  7699. using number_integer_t = typename BasicJsonType::number_integer_t;
  7700. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7701. using number_float_t = typename BasicJsonType::number_float_t;
  7702. using string_t = typename BasicJsonType::string_t;
  7703. using binary_t = typename BasicJsonType::binary_t;
  7704. /*!
  7705. @brief a null value was read
  7706. @return whether parsing should proceed
  7707. */
  7708. virtual bool null() = 0;
  7709. /*!
  7710. @brief a boolean value was read
  7711. @param[in] val boolean value
  7712. @return whether parsing should proceed
  7713. */
  7714. virtual bool boolean(bool val) = 0;
  7715. /*!
  7716. @brief an integer number was read
  7717. @param[in] val integer value
  7718. @return whether parsing should proceed
  7719. */
  7720. virtual bool number_integer(number_integer_t val) = 0;
  7721. /*!
  7722. @brief an unsigned integer number was read
  7723. @param[in] val unsigned integer value
  7724. @return whether parsing should proceed
  7725. */
  7726. virtual bool number_unsigned(number_unsigned_t val) = 0;
  7727. /*!
  7728. @brief a floating-point number was read
  7729. @param[in] val floating-point value
  7730. @param[in] s raw token value
  7731. @return whether parsing should proceed
  7732. */
  7733. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  7734. /*!
  7735. @brief a string value was read
  7736. @param[in] val string value
  7737. @return whether parsing should proceed
  7738. @note It is safe to move the passed string value.
  7739. */
  7740. virtual bool string(string_t& val) = 0;
  7741. /*!
  7742. @brief a binary value was read
  7743. @param[in] val binary value
  7744. @return whether parsing should proceed
  7745. @note It is safe to move the passed binary value.
  7746. */
  7747. virtual bool binary(binary_t& val) = 0;
  7748. /*!
  7749. @brief the beginning of an object was read
  7750. @param[in] elements number of object elements or -1 if unknown
  7751. @return whether parsing should proceed
  7752. @note binary formats may report the number of elements
  7753. */
  7754. virtual bool start_object(std::size_t elements) = 0;
  7755. /*!
  7756. @brief an object key was read
  7757. @param[in] val object key
  7758. @return whether parsing should proceed
  7759. @note It is safe to move the passed string.
  7760. */
  7761. virtual bool key(string_t& val) = 0;
  7762. /*!
  7763. @brief the end of an object was read
  7764. @return whether parsing should proceed
  7765. */
  7766. virtual bool end_object() = 0;
  7767. /*!
  7768. @brief the beginning of an array was read
  7769. @param[in] elements number of array elements or -1 if unknown
  7770. @return whether parsing should proceed
  7771. @note binary formats may report the number of elements
  7772. */
  7773. virtual bool start_array(std::size_t elements) = 0;
  7774. /*!
  7775. @brief the end of an array was read
  7776. @return whether parsing should proceed
  7777. */
  7778. virtual bool end_array() = 0;
  7779. /*!
  7780. @brief a parse error occurred
  7781. @param[in] position the position in the input where the error occurs
  7782. @param[in] last_token the last read token
  7783. @param[in] ex an exception object describing the error
  7784. @return whether parsing should proceed (must return false)
  7785. */
  7786. virtual bool parse_error(std::size_t position,
  7787. const std::string& last_token,
  7788. const detail::exception& ex) = 0;
  7789. json_sax() = default;
  7790. json_sax(const json_sax&) = default;
  7791. json_sax(json_sax&&) noexcept = default;
  7792. json_sax& operator=(const json_sax&) = default;
  7793. json_sax& operator=(json_sax&&) noexcept = default;
  7794. virtual ~json_sax() = default;
  7795. };
  7796. namespace detail
  7797. {
  7798. constexpr std::size_t unknown_size()
  7799. {
  7800. return (std::numeric_limits<std::size_t>::max)();
  7801. }
  7802. /*!
  7803. @brief SAX implementation to create a JSON value from SAX events
  7804. This class implements the @ref json_sax interface and processes the SAX events
  7805. to create a JSON value which makes it basically a DOM parser. The structure or
  7806. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  7807. a pointer to the respective array or object for each recursion depth.
  7808. After successful parsing, the value that is passed by reference to the
  7809. constructor contains the parsed value.
  7810. @tparam BasicJsonType the JSON type
  7811. */
  7812. template<typename BasicJsonType, typename InputAdapterType>
  7813. class json_sax_dom_parser
  7814. {
  7815. public:
  7816. using number_integer_t = typename BasicJsonType::number_integer_t;
  7817. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7818. using number_float_t = typename BasicJsonType::number_float_t;
  7819. using string_t = typename BasicJsonType::string_t;
  7820. using binary_t = typename BasicJsonType::binary_t;
  7821. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  7822. /*!
  7823. @param[in,out] r reference to a JSON value that is manipulated while
  7824. parsing
  7825. @param[in] allow_exceptions_ whether parse errors yield exceptions
  7826. */
  7827. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr)
  7828. : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
  7829. {}
  7830. // make class move-only
  7831. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  7832. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7833. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  7834. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7835. ~json_sax_dom_parser() = default;
  7836. bool null()
  7837. {
  7838. handle_value(nullptr);
  7839. return true;
  7840. }
  7841. bool boolean(bool val)
  7842. {
  7843. handle_value(val);
  7844. return true;
  7845. }
  7846. bool number_integer(number_integer_t val)
  7847. {
  7848. handle_value(val);
  7849. return true;
  7850. }
  7851. bool number_unsigned(number_unsigned_t val)
  7852. {
  7853. handle_value(val);
  7854. return true;
  7855. }
  7856. bool number_float(number_float_t val, const string_t& /*unused*/)
  7857. {
  7858. handle_value(val);
  7859. return true;
  7860. }
  7861. bool string(string_t& val)
  7862. {
  7863. handle_value(val);
  7864. return true;
  7865. }
  7866. bool binary(binary_t& val)
  7867. {
  7868. handle_value(std::move(val));
  7869. return true;
  7870. }
  7871. bool start_object(std::size_t len)
  7872. {
  7873. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  7874. #if JSON_DIAGNOSTIC_POSITIONS
  7875. // Manually set the start position of the object here.
  7876. // Ensure this is after the call to handle_value to ensure correct start position.
  7877. if (m_lexer_ref)
  7878. {
  7879. // Lexer has read the first character of the object, so
  7880. // subtract 1 from the position to get the correct start position.
  7881. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  7882. }
  7883. #endif
  7884. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  7885. {
  7886. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  7887. }
  7888. return true;
  7889. }
  7890. bool key(string_t& val)
  7891. {
  7892. JSON_ASSERT(!ref_stack.empty());
  7893. JSON_ASSERT(ref_stack.back()->is_object());
  7894. // add null at the given key and store the reference for later
  7895. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
  7896. return true;
  7897. }
  7898. bool end_object()
  7899. {
  7900. JSON_ASSERT(!ref_stack.empty());
  7901. JSON_ASSERT(ref_stack.back()->is_object());
  7902. #if JSON_DIAGNOSTIC_POSITIONS
  7903. if (m_lexer_ref)
  7904. {
  7905. // Lexer's position is past the closing brace, so set that as the end position.
  7906. ref_stack.back()->end_position = m_lexer_ref->get_position();
  7907. }
  7908. #endif
  7909. ref_stack.back()->set_parents();
  7910. ref_stack.pop_back();
  7911. return true;
  7912. }
  7913. bool start_array(std::size_t len)
  7914. {
  7915. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  7916. #if JSON_DIAGNOSTIC_POSITIONS
  7917. // Manually set the start position of the array here.
  7918. // Ensure this is after the call to handle_value to ensure correct start position.
  7919. if (m_lexer_ref)
  7920. {
  7921. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  7922. }
  7923. #endif
  7924. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  7925. {
  7926. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  7927. }
  7928. return true;
  7929. }
  7930. bool end_array()
  7931. {
  7932. JSON_ASSERT(!ref_stack.empty());
  7933. JSON_ASSERT(ref_stack.back()->is_array());
  7934. #if JSON_DIAGNOSTIC_POSITIONS
  7935. if (m_lexer_ref)
  7936. {
  7937. // Lexer's position is past the closing bracket, so set that as the end position.
  7938. ref_stack.back()->end_position = m_lexer_ref->get_position();
  7939. }
  7940. #endif
  7941. ref_stack.back()->set_parents();
  7942. ref_stack.pop_back();
  7943. return true;
  7944. }
  7945. template<class Exception>
  7946. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  7947. const Exception& ex)
  7948. {
  7949. errored = true;
  7950. static_cast<void>(ex);
  7951. if (allow_exceptions)
  7952. {
  7953. JSON_THROW(ex);
  7954. }
  7955. return false;
  7956. }
  7957. constexpr bool is_errored() const
  7958. {
  7959. return errored;
  7960. }
  7961. private:
  7962. #if JSON_DIAGNOSTIC_POSITIONS
  7963. void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
  7964. {
  7965. if (m_lexer_ref)
  7966. {
  7967. // Lexer has read past the current field value, so set the end position to the current position.
  7968. // The start position will be set below based on the length of the string representation
  7969. // of the value.
  7970. v.end_position = m_lexer_ref->get_position();
  7971. switch (v.type())
  7972. {
  7973. case value_t::boolean:
  7974. {
  7975. // 4 and 5 are the string length of "true" and "false"
  7976. v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
  7977. break;
  7978. }
  7979. case value_t::null:
  7980. {
  7981. // 4 is the string length of "null"
  7982. v.start_position = v.end_position - 4;
  7983. break;
  7984. }
  7985. case value_t::string:
  7986. {
  7987. // include the length of the quotes, which is 2
  7988. v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
  7989. break;
  7990. }
  7991. // As we handle the start and end positions for values created during parsing,
  7992. // we do not expect the following value type to be called. Regardless, set the positions
  7993. // in case this is created manually or through a different constructor. Exclude from lcov
  7994. // since the exact condition of this switch is esoteric.
  7995. // LCOV_EXCL_START
  7996. case value_t::discarded:
  7997. {
  7998. v.end_position = std::string::npos;
  7999. v.start_position = v.end_position;
  8000. break;
  8001. }
  8002. // LCOV_EXCL_STOP
  8003. case value_t::binary:
  8004. case value_t::number_integer:
  8005. case value_t::number_unsigned:
  8006. case value_t::number_float:
  8007. {
  8008. v.start_position = v.end_position - m_lexer_ref->get_string().size();
  8009. break;
  8010. }
  8011. case value_t::object:
  8012. case value_t::array:
  8013. {
  8014. // object and array are handled in start_object() and start_array() handlers
  8015. // skip setting the values here.
  8016. break;
  8017. }
  8018. default: // LCOV_EXCL_LINE
  8019. // Handle all possible types discretely, default handler should never be reached.
  8020. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
  8021. }
  8022. }
  8023. }
  8024. #endif
  8025. /*!
  8026. @invariant If the ref stack is empty, then the passed value will be the new
  8027. root.
  8028. @invariant If the ref stack contains a value, then it is an array or an
  8029. object to which we can add elements
  8030. */
  8031. template<typename Value>
  8032. JSON_HEDLEY_RETURNS_NON_NULL
  8033. BasicJsonType* handle_value(Value&& v)
  8034. {
  8035. if (ref_stack.empty())
  8036. {
  8037. root = BasicJsonType(std::forward<Value>(v));
  8038. #if JSON_DIAGNOSTIC_POSITIONS
  8039. handle_diagnostic_positions_for_json_value(root);
  8040. #endif
  8041. return &root;
  8042. }
  8043. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  8044. if (ref_stack.back()->is_array())
  8045. {
  8046. ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
  8047. #if JSON_DIAGNOSTIC_POSITIONS
  8048. handle_diagnostic_positions_for_json_value(ref_stack.back()->m_data.m_value.array->back());
  8049. #endif
  8050. return &(ref_stack.back()->m_data.m_value.array->back());
  8051. }
  8052. JSON_ASSERT(ref_stack.back()->is_object());
  8053. JSON_ASSERT(object_element);
  8054. *object_element = BasicJsonType(std::forward<Value>(v));
  8055. #if JSON_DIAGNOSTIC_POSITIONS
  8056. handle_diagnostic_positions_for_json_value(*object_element);
  8057. #endif
  8058. return object_element;
  8059. }
  8060. /// the parsed JSON value
  8061. BasicJsonType& root;
  8062. /// stack to model hierarchy of values
  8063. std::vector<BasicJsonType*> ref_stack {};
  8064. /// helper to hold the reference for the next object element
  8065. BasicJsonType* object_element = nullptr;
  8066. /// whether a syntax error occurred
  8067. bool errored = false;
  8068. /// whether to throw exceptions in case of errors
  8069. const bool allow_exceptions = true;
  8070. /// the lexer reference to obtain the current position
  8071. lexer_t* m_lexer_ref = nullptr;
  8072. };
  8073. template<typename BasicJsonType, typename InputAdapterType>
  8074. class json_sax_dom_callback_parser
  8075. {
  8076. public:
  8077. using number_integer_t = typename BasicJsonType::number_integer_t;
  8078. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8079. using number_float_t = typename BasicJsonType::number_float_t;
  8080. using string_t = typename BasicJsonType::string_t;
  8081. using binary_t = typename BasicJsonType::binary_t;
  8082. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  8083. using parse_event_t = typename BasicJsonType::parse_event_t;
  8084. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  8085. json_sax_dom_callback_parser(BasicJsonType& r,
  8086. parser_callback_t cb,
  8087. const bool allow_exceptions_ = true,
  8088. lexer_t* lexer_ = nullptr)
  8089. : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
  8090. {
  8091. keep_stack.push_back(true);
  8092. }
  8093. // make class move-only
  8094. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  8095. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  8096. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  8097. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  8098. ~json_sax_dom_callback_parser() = default;
  8099. bool null()
  8100. {
  8101. handle_value(nullptr);
  8102. return true;
  8103. }
  8104. bool boolean(bool val)
  8105. {
  8106. handle_value(val);
  8107. return true;
  8108. }
  8109. bool number_integer(number_integer_t val)
  8110. {
  8111. handle_value(val);
  8112. return true;
  8113. }
  8114. bool number_unsigned(number_unsigned_t val)
  8115. {
  8116. handle_value(val);
  8117. return true;
  8118. }
  8119. bool number_float(number_float_t val, const string_t& /*unused*/)
  8120. {
  8121. handle_value(val);
  8122. return true;
  8123. }
  8124. bool string(string_t& val)
  8125. {
  8126. handle_value(val);
  8127. return true;
  8128. }
  8129. bool binary(binary_t& val)
  8130. {
  8131. handle_value(std::move(val));
  8132. return true;
  8133. }
  8134. bool start_object(std::size_t len)
  8135. {
  8136. // check callback for object start
  8137. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  8138. keep_stack.push_back(keep);
  8139. auto val = handle_value(BasicJsonType::value_t::object, true);
  8140. ref_stack.push_back(val.second);
  8141. if (ref_stack.back())
  8142. {
  8143. #if JSON_DIAGNOSTIC_POSITIONS
  8144. // Manually set the start position of the object here.
  8145. // Ensure this is after the call to handle_value to ensure correct start position.
  8146. if (m_lexer_ref)
  8147. {
  8148. // Lexer has read the first character of the object, so
  8149. // subtract 1 from the position to get the correct start position.
  8150. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  8151. }
  8152. #endif
  8153. // check object limit
  8154. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  8155. {
  8156. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  8157. }
  8158. }
  8159. return true;
  8160. }
  8161. bool key(string_t& val)
  8162. {
  8163. BasicJsonType k = BasicJsonType(val);
  8164. // check callback for the key
  8165. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  8166. key_keep_stack.push_back(keep);
  8167. // add discarded value at the given key and store the reference for later
  8168. if (keep && ref_stack.back())
  8169. {
  8170. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
  8171. }
  8172. return true;
  8173. }
  8174. bool end_object()
  8175. {
  8176. if (ref_stack.back())
  8177. {
  8178. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  8179. {
  8180. // discard object
  8181. *ref_stack.back() = discarded;
  8182. #if JSON_DIAGNOSTIC_POSITIONS
  8183. // Set start/end positions for discarded object.
  8184. handle_diagnostic_positions_for_json_value(*ref_stack.back());
  8185. #endif
  8186. }
  8187. else
  8188. {
  8189. #if JSON_DIAGNOSTIC_POSITIONS
  8190. if (m_lexer_ref)
  8191. {
  8192. // Lexer's position is past the closing brace, so set that as the end position.
  8193. ref_stack.back()->end_position = m_lexer_ref->get_position();
  8194. }
  8195. #endif
  8196. ref_stack.back()->set_parents();
  8197. }
  8198. }
  8199. JSON_ASSERT(!ref_stack.empty());
  8200. JSON_ASSERT(!keep_stack.empty());
  8201. ref_stack.pop_back();
  8202. keep_stack.pop_back();
  8203. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  8204. {
  8205. // remove discarded value
  8206. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  8207. {
  8208. if (it->is_discarded())
  8209. {
  8210. ref_stack.back()->erase(it);
  8211. break;
  8212. }
  8213. }
  8214. }
  8215. return true;
  8216. }
  8217. bool start_array(std::size_t len)
  8218. {
  8219. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  8220. keep_stack.push_back(keep);
  8221. auto val = handle_value(BasicJsonType::value_t::array, true);
  8222. ref_stack.push_back(val.second);
  8223. if (ref_stack.back())
  8224. {
  8225. #if JSON_DIAGNOSTIC_POSITIONS
  8226. // Manually set the start position of the array here.
  8227. // Ensure this is after the call to handle_value to ensure correct start position.
  8228. if (m_lexer_ref)
  8229. {
  8230. // Lexer has read the first character of the array, so
  8231. // subtract 1 from the position to get the correct start position.
  8232. ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
  8233. }
  8234. #endif
  8235. // check array limit
  8236. if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
  8237. {
  8238. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  8239. }
  8240. }
  8241. return true;
  8242. }
  8243. bool end_array()
  8244. {
  8245. bool keep = true;
  8246. if (ref_stack.back())
  8247. {
  8248. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  8249. if (keep)
  8250. {
  8251. #if JSON_DIAGNOSTIC_POSITIONS
  8252. if (m_lexer_ref)
  8253. {
  8254. // Lexer's position is past the closing bracket, so set that as the end position.
  8255. ref_stack.back()->end_position = m_lexer_ref->get_position();
  8256. }
  8257. #endif
  8258. ref_stack.back()->set_parents();
  8259. }
  8260. else
  8261. {
  8262. // discard array
  8263. *ref_stack.back() = discarded;
  8264. #if JSON_DIAGNOSTIC_POSITIONS
  8265. // Set start/end positions for discarded array.
  8266. handle_diagnostic_positions_for_json_value(*ref_stack.back());
  8267. #endif
  8268. }
  8269. }
  8270. JSON_ASSERT(!ref_stack.empty());
  8271. JSON_ASSERT(!keep_stack.empty());
  8272. ref_stack.pop_back();
  8273. keep_stack.pop_back();
  8274. // remove discarded value
  8275. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  8276. {
  8277. ref_stack.back()->m_data.m_value.array->pop_back();
  8278. }
  8279. return true;
  8280. }
  8281. template<class Exception>
  8282. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  8283. const Exception& ex)
  8284. {
  8285. errored = true;
  8286. static_cast<void>(ex);
  8287. if (allow_exceptions)
  8288. {
  8289. JSON_THROW(ex);
  8290. }
  8291. return false;
  8292. }
  8293. constexpr bool is_errored() const
  8294. {
  8295. return errored;
  8296. }
  8297. private:
  8298. #if JSON_DIAGNOSTIC_POSITIONS
  8299. void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
  8300. {
  8301. if (m_lexer_ref)
  8302. {
  8303. // Lexer has read past the current field value, so set the end position to the current position.
  8304. // The start position will be set below based on the length of the string representation
  8305. // of the value.
  8306. v.end_position = m_lexer_ref->get_position();
  8307. switch (v.type())
  8308. {
  8309. case value_t::boolean:
  8310. {
  8311. // 4 and 5 are the string length of "true" and "false"
  8312. v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
  8313. break;
  8314. }
  8315. case value_t::null:
  8316. {
  8317. // 4 is the string length of "null"
  8318. v.start_position = v.end_position - 4;
  8319. break;
  8320. }
  8321. case value_t::string:
  8322. {
  8323. // include the length of the quotes, which is 2
  8324. v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
  8325. break;
  8326. }
  8327. case value_t::discarded:
  8328. {
  8329. v.end_position = std::string::npos;
  8330. v.start_position = v.end_position;
  8331. break;
  8332. }
  8333. case value_t::binary:
  8334. case value_t::number_integer:
  8335. case value_t::number_unsigned:
  8336. case value_t::number_float:
  8337. {
  8338. v.start_position = v.end_position - m_lexer_ref->get_string().size();
  8339. break;
  8340. }
  8341. case value_t::object:
  8342. case value_t::array:
  8343. {
  8344. // object and array are handled in start_object() and start_array() handlers
  8345. // skip setting the values here.
  8346. break;
  8347. }
  8348. default: // LCOV_EXCL_LINE
  8349. // Handle all possible types discretely, default handler should never be reached.
  8350. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
  8351. }
  8352. }
  8353. }
  8354. #endif
  8355. /*!
  8356. @param[in] v value to add to the JSON value we build during parsing
  8357. @param[in] skip_callback whether we should skip calling the callback
  8358. function; this is required after start_array() and
  8359. start_object() SAX events, because otherwise we would call the
  8360. callback function with an empty array or object, respectively.
  8361. @invariant If the ref stack is empty, then the passed value will be the new
  8362. root.
  8363. @invariant If the ref stack contains a value, then it is an array or an
  8364. object to which we can add elements
  8365. @return pair of boolean (whether value should be kept) and pointer (to the
  8366. passed value in the ref_stack hierarchy; nullptr if not kept)
  8367. */
  8368. template<typename Value>
  8369. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  8370. {
  8371. JSON_ASSERT(!keep_stack.empty());
  8372. // do not handle this value if we know it would be added to a discarded
  8373. // container
  8374. if (!keep_stack.back())
  8375. {
  8376. return {false, nullptr};
  8377. }
  8378. // create value
  8379. auto value = BasicJsonType(std::forward<Value>(v));
  8380. #if JSON_DIAGNOSTIC_POSITIONS
  8381. handle_diagnostic_positions_for_json_value(value);
  8382. #endif
  8383. // check callback
  8384. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  8385. // do not handle this value if we just learnt it shall be discarded
  8386. if (!keep)
  8387. {
  8388. return {false, nullptr};
  8389. }
  8390. if (ref_stack.empty())
  8391. {
  8392. root = std::move(value);
  8393. return {true, & root};
  8394. }
  8395. // skip this value if we already decided to skip the parent
  8396. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  8397. if (!ref_stack.back())
  8398. {
  8399. return {false, nullptr};
  8400. }
  8401. // we now only expect arrays and objects
  8402. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  8403. // array
  8404. if (ref_stack.back()->is_array())
  8405. {
  8406. ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
  8407. return {true, & (ref_stack.back()->m_data.m_value.array->back())};
  8408. }
  8409. // object
  8410. JSON_ASSERT(ref_stack.back()->is_object());
  8411. // check if we should store an element for the current key
  8412. JSON_ASSERT(!key_keep_stack.empty());
  8413. const bool store_element = key_keep_stack.back();
  8414. key_keep_stack.pop_back();
  8415. if (!store_element)
  8416. {
  8417. return {false, nullptr};
  8418. }
  8419. JSON_ASSERT(object_element);
  8420. *object_element = std::move(value);
  8421. return {true, object_element};
  8422. }
  8423. /// the parsed JSON value
  8424. BasicJsonType& root;
  8425. /// stack to model hierarchy of values
  8426. std::vector<BasicJsonType*> ref_stack {};
  8427. /// stack to manage which values to keep
  8428. std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
  8429. /// stack to manage which object keys to keep
  8430. std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
  8431. /// helper to hold the reference for the next object element
  8432. BasicJsonType* object_element = nullptr;
  8433. /// whether a syntax error occurred
  8434. bool errored = false;
  8435. /// callback function
  8436. const parser_callback_t callback = nullptr;
  8437. /// whether to throw exceptions in case of errors
  8438. const bool allow_exceptions = true;
  8439. /// a discarded value for the callback
  8440. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  8441. /// the lexer reference to obtain the current position
  8442. lexer_t* m_lexer_ref = nullptr;
  8443. };
  8444. template<typename BasicJsonType>
  8445. class json_sax_acceptor
  8446. {
  8447. public:
  8448. using number_integer_t = typename BasicJsonType::number_integer_t;
  8449. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8450. using number_float_t = typename BasicJsonType::number_float_t;
  8451. using string_t = typename BasicJsonType::string_t;
  8452. using binary_t = typename BasicJsonType::binary_t;
  8453. bool null()
  8454. {
  8455. return true;
  8456. }
  8457. bool boolean(bool /*unused*/)
  8458. {
  8459. return true;
  8460. }
  8461. bool number_integer(number_integer_t /*unused*/)
  8462. {
  8463. return true;
  8464. }
  8465. bool number_unsigned(number_unsigned_t /*unused*/)
  8466. {
  8467. return true;
  8468. }
  8469. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  8470. {
  8471. return true;
  8472. }
  8473. bool string(string_t& /*unused*/)
  8474. {
  8475. return true;
  8476. }
  8477. bool binary(binary_t& /*unused*/)
  8478. {
  8479. return true;
  8480. }
  8481. bool start_object(std::size_t /*unused*/ = detail::unknown_size())
  8482. {
  8483. return true;
  8484. }
  8485. bool key(string_t& /*unused*/)
  8486. {
  8487. return true;
  8488. }
  8489. bool end_object()
  8490. {
  8491. return true;
  8492. }
  8493. bool start_array(std::size_t /*unused*/ = detail::unknown_size())
  8494. {
  8495. return true;
  8496. }
  8497. bool end_array()
  8498. {
  8499. return true;
  8500. }
  8501. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  8502. {
  8503. return false;
  8504. }
  8505. };
  8506. } // namespace detail
  8507. NLOHMANN_JSON_NAMESPACE_END
  8508. // #include <nlohmann/detail/input/lexer.hpp>
  8509. // #include <nlohmann/detail/macro_scope.hpp>
  8510. // #include <nlohmann/detail/meta/is_sax.hpp>
  8511. // __ _____ _____ _____
  8512. // __| | __| | | | JSON for Modern C++
  8513. // | | |__ | | | | | | version 3.12.0
  8514. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  8515. //
  8516. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  8517. // SPDX-License-Identifier: MIT
  8518. #include <cstdint> // size_t
  8519. #include <utility> // declval
  8520. #include <string> // string
  8521. // #include <nlohmann/detail/abi_macros.hpp>
  8522. // #include <nlohmann/detail/meta/detected.hpp>
  8523. // #include <nlohmann/detail/meta/type_traits.hpp>
  8524. NLOHMANN_JSON_NAMESPACE_BEGIN
  8525. namespace detail
  8526. {
  8527. template<typename T>
  8528. using null_function_t = decltype(std::declval<T&>().null());
  8529. template<typename T>
  8530. using boolean_function_t =
  8531. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  8532. template<typename T, typename Integer>
  8533. using number_integer_function_t =
  8534. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  8535. template<typename T, typename Unsigned>
  8536. using number_unsigned_function_t =
  8537. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  8538. template<typename T, typename Float, typename String>
  8539. using number_float_function_t = decltype(std::declval<T&>().number_float(
  8540. std::declval<Float>(), std::declval<const String&>()));
  8541. template<typename T, typename String>
  8542. using string_function_t =
  8543. decltype(std::declval<T&>().string(std::declval<String&>()));
  8544. template<typename T, typename Binary>
  8545. using binary_function_t =
  8546. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  8547. template<typename T>
  8548. using start_object_function_t =
  8549. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  8550. template<typename T, typename String>
  8551. using key_function_t =
  8552. decltype(std::declval<T&>().key(std::declval<String&>()));
  8553. template<typename T>
  8554. using end_object_function_t = decltype(std::declval<T&>().end_object());
  8555. template<typename T>
  8556. using start_array_function_t =
  8557. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  8558. template<typename T>
  8559. using end_array_function_t = decltype(std::declval<T&>().end_array());
  8560. template<typename T, typename Exception>
  8561. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  8562. std::declval<std::size_t>(), std::declval<const std::string&>(),
  8563. std::declval<const Exception&>()));
  8564. template<typename SAX, typename BasicJsonType>
  8565. struct is_sax
  8566. {
  8567. private:
  8568. static_assert(is_basic_json<BasicJsonType>::value,
  8569. "BasicJsonType must be of type basic_json<...>");
  8570. using number_integer_t = typename BasicJsonType::number_integer_t;
  8571. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8572. using number_float_t = typename BasicJsonType::number_float_t;
  8573. using string_t = typename BasicJsonType::string_t;
  8574. using binary_t = typename BasicJsonType::binary_t;
  8575. using exception_t = typename BasicJsonType::exception;
  8576. public:
  8577. static constexpr bool value =
  8578. is_detected_exact<bool, null_function_t, SAX>::value &&
  8579. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  8580. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  8581. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  8582. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  8583. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  8584. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  8585. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  8586. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  8587. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  8588. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  8589. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  8590. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  8591. };
  8592. template<typename SAX, typename BasicJsonType>
  8593. struct is_sax_static_asserts
  8594. {
  8595. private:
  8596. static_assert(is_basic_json<BasicJsonType>::value,
  8597. "BasicJsonType must be of type basic_json<...>");
  8598. using number_integer_t = typename BasicJsonType::number_integer_t;
  8599. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8600. using number_float_t = typename BasicJsonType::number_float_t;
  8601. using string_t = typename BasicJsonType::string_t;
  8602. using binary_t = typename BasicJsonType::binary_t;
  8603. using exception_t = typename BasicJsonType::exception;
  8604. public:
  8605. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  8606. "Missing/invalid function: bool null()");
  8607. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  8608. "Missing/invalid function: bool boolean(bool)");
  8609. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  8610. "Missing/invalid function: bool boolean(bool)");
  8611. static_assert(
  8612. is_detected_exact<bool, number_integer_function_t, SAX,
  8613. number_integer_t>::value,
  8614. "Missing/invalid function: bool number_integer(number_integer_t)");
  8615. static_assert(
  8616. is_detected_exact<bool, number_unsigned_function_t, SAX,
  8617. number_unsigned_t>::value,
  8618. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  8619. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  8620. number_float_t, string_t>::value,
  8621. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  8622. static_assert(
  8623. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  8624. "Missing/invalid function: bool string(string_t&)");
  8625. static_assert(
  8626. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  8627. "Missing/invalid function: bool binary(binary_t&)");
  8628. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  8629. "Missing/invalid function: bool start_object(std::size_t)");
  8630. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  8631. "Missing/invalid function: bool key(string_t&)");
  8632. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  8633. "Missing/invalid function: bool end_object()");
  8634. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  8635. "Missing/invalid function: bool start_array(std::size_t)");
  8636. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  8637. "Missing/invalid function: bool end_array()");
  8638. static_assert(
  8639. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  8640. "Missing/invalid function: bool parse_error(std::size_t, const "
  8641. "std::string&, const exception&)");
  8642. };
  8643. } // namespace detail
  8644. NLOHMANN_JSON_NAMESPACE_END
  8645. // #include <nlohmann/detail/meta/type_traits.hpp>
  8646. // #include <nlohmann/detail/string_concat.hpp>
  8647. // #include <nlohmann/detail/value_t.hpp>
  8648. NLOHMANN_JSON_NAMESPACE_BEGIN
  8649. namespace detail
  8650. {
  8651. /// how to treat CBOR tags
  8652. enum class cbor_tag_handler_t
  8653. {
  8654. error, ///< throw a parse_error exception in case of a tag
  8655. ignore, ///< ignore tags
  8656. store ///< store tags as binary type
  8657. };
  8658. /*!
  8659. @brief determine system byte order
  8660. @return true if and only if system's byte order is little endian
  8661. @note from https://stackoverflow.com/a/1001328/266378
  8662. */
  8663. inline bool little_endianness(int num = 1) noexcept
  8664. {
  8665. return *reinterpret_cast<char*>(&num) == 1;
  8666. }
  8667. ///////////////////
  8668. // binary reader //
  8669. ///////////////////
  8670. /*!
  8671. @brief deserialization of CBOR, MessagePack, and UBJSON values
  8672. */
  8673. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
  8674. class binary_reader
  8675. {
  8676. using number_integer_t = typename BasicJsonType::number_integer_t;
  8677. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  8678. using number_float_t = typename BasicJsonType::number_float_t;
  8679. using string_t = typename BasicJsonType::string_t;
  8680. using binary_t = typename BasicJsonType::binary_t;
  8681. using json_sax_t = SAX;
  8682. using char_type = typename InputAdapterType::char_type;
  8683. using char_int_type = typename char_traits<char_type>::int_type;
  8684. public:
  8685. /*!
  8686. @brief create a binary reader
  8687. @param[in] adapter input adapter to read from
  8688. */
  8689. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  8690. {
  8691. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  8692. }
  8693. // make class move-only
  8694. binary_reader(const binary_reader&) = delete;
  8695. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  8696. binary_reader& operator=(const binary_reader&) = delete;
  8697. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  8698. ~binary_reader() = default;
  8699. /*!
  8700. @param[in] format the binary format to parse
  8701. @param[in] sax_ a SAX event processor
  8702. @param[in] strict whether to expect the input to be consumed completed
  8703. @param[in] tag_handler how to treat CBOR tags
  8704. @return whether parsing was successful
  8705. */
  8706. JSON_HEDLEY_NON_NULL(3)
  8707. bool sax_parse(const input_format_t format,
  8708. json_sax_t* sax_,
  8709. const bool strict = true,
  8710. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  8711. {
  8712. sax = sax_;
  8713. bool result = false;
  8714. switch (format)
  8715. {
  8716. case input_format_t::bson:
  8717. result = parse_bson_internal();
  8718. break;
  8719. case input_format_t::cbor:
  8720. result = parse_cbor_internal(true, tag_handler);
  8721. break;
  8722. case input_format_t::msgpack:
  8723. result = parse_msgpack_internal();
  8724. break;
  8725. case input_format_t::ubjson:
  8726. case input_format_t::bjdata:
  8727. result = parse_ubjson_internal();
  8728. break;
  8729. case input_format_t::json: // LCOV_EXCL_LINE
  8730. default: // LCOV_EXCL_LINE
  8731. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8732. }
  8733. // strict mode: next byte must be EOF
  8734. if (result && strict)
  8735. {
  8736. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  8737. {
  8738. get_ignore_noop();
  8739. }
  8740. else
  8741. {
  8742. get();
  8743. }
  8744. if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
  8745. {
  8746. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  8747. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  8748. }
  8749. }
  8750. return result;
  8751. }
  8752. private:
  8753. //////////
  8754. // BSON //
  8755. //////////
  8756. /*!
  8757. @brief Reads in a BSON-object and passes it to the SAX-parser.
  8758. @return whether a valid BSON-value was passed to the SAX parser
  8759. */
  8760. bool parse_bson_internal()
  8761. {
  8762. std::int32_t document_size{};
  8763. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8764. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
  8765. {
  8766. return false;
  8767. }
  8768. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8769. {
  8770. return false;
  8771. }
  8772. return sax->end_object();
  8773. }
  8774. /*!
  8775. @brief Parses a C-style string from the BSON input.
  8776. @param[in,out] result A reference to the string variable where the read
  8777. string is to be stored.
  8778. @return `true` if the \x00-byte indicating the end of the string was
  8779. encountered before the EOF; false` indicates an unexpected EOF.
  8780. */
  8781. bool get_bson_cstr(string_t& result)
  8782. {
  8783. auto out = std::back_inserter(result);
  8784. while (true)
  8785. {
  8786. get();
  8787. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8788. {
  8789. return false;
  8790. }
  8791. if (current == 0x00)
  8792. {
  8793. return true;
  8794. }
  8795. *out++ = static_cast<typename string_t::value_type>(current);
  8796. }
  8797. }
  8798. /*!
  8799. @brief Parses a zero-terminated string of length @a len from the BSON
  8800. input.
  8801. @param[in] len The length (including the zero-byte at the end) of the
  8802. string to be read.
  8803. @param[in,out] result A reference to the string variable where the read
  8804. string is to be stored.
  8805. @tparam NumberType The type of the length @a len
  8806. @pre len >= 1
  8807. @return `true` if the string was successfully parsed
  8808. */
  8809. template<typename NumberType>
  8810. bool get_bson_string(const NumberType len, string_t& result)
  8811. {
  8812. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8813. {
  8814. auto last_token = get_token_string();
  8815. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8816. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8817. }
  8818. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
  8819. }
  8820. /*!
  8821. @brief Parses a byte array input of length @a len from the BSON input.
  8822. @param[in] len The length of the byte array to be read.
  8823. @param[in,out] result A reference to the binary variable where the read
  8824. array is to be stored.
  8825. @tparam NumberType The type of the length @a len
  8826. @pre len >= 0
  8827. @return `true` if the byte array was successfully parsed
  8828. */
  8829. template<typename NumberType>
  8830. bool get_bson_binary(const NumberType len, binary_t& result)
  8831. {
  8832. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8833. {
  8834. auto last_token = get_token_string();
  8835. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8836. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8837. }
  8838. // All BSON binary values have a subtype
  8839. std::uint8_t subtype{};
  8840. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8841. result.set_subtype(subtype);
  8842. return get_binary(input_format_t::bson, len, result);
  8843. }
  8844. /*!
  8845. @brief Read a BSON document element of the given @a element_type.
  8846. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8847. @param[in] element_type_parse_position The position in the input stream,
  8848. where the `element_type` was read.
  8849. @warning Not all BSON element types are supported yet. An unsupported
  8850. @a element_type will give rise to a parse_error.114:
  8851. Unsupported BSON record type 0x...
  8852. @return whether a valid BSON-object/array was passed to the SAX parser
  8853. */
  8854. bool parse_bson_element_internal(const char_int_type element_type,
  8855. const std::size_t element_type_parse_position)
  8856. {
  8857. switch (element_type)
  8858. {
  8859. case 0x01: // double
  8860. {
  8861. double number{};
  8862. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8863. }
  8864. case 0x02: // string
  8865. {
  8866. std::int32_t len{};
  8867. string_t value;
  8868. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8869. }
  8870. case 0x03: // object
  8871. {
  8872. return parse_bson_internal();
  8873. }
  8874. case 0x04: // array
  8875. {
  8876. return parse_bson_array();
  8877. }
  8878. case 0x05: // binary
  8879. {
  8880. std::int32_t len{};
  8881. binary_t value;
  8882. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8883. }
  8884. case 0x08: // boolean
  8885. {
  8886. return sax->boolean(get() != 0);
  8887. }
  8888. case 0x0A: // null
  8889. {
  8890. return sax->null();
  8891. }
  8892. case 0x10: // int32
  8893. {
  8894. std::int32_t value{};
  8895. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8896. }
  8897. case 0x12: // int64
  8898. {
  8899. std::int64_t value{};
  8900. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8901. }
  8902. case 0x11: // uint64
  8903. {
  8904. std::uint64_t value{};
  8905. return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
  8906. }
  8907. default: // anything else is not supported (yet)
  8908. {
  8909. std::array<char, 3> cr{{}};
  8910. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8911. const std::string cr_str{cr.data()};
  8912. return sax->parse_error(element_type_parse_position, cr_str,
  8913. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8914. }
  8915. }
  8916. }
  8917. /*!
  8918. @brief Read a BSON element list (as specified in the BSON-spec)
  8919. The same binary layout is used for objects and arrays, hence it must be
  8920. indicated with the argument @a is_array which one is expected
  8921. (true --> array, false --> object).
  8922. @param[in] is_array Determines if the element list being read is to be
  8923. treated as an object (@a is_array == false), or as an
  8924. array (@a is_array == true).
  8925. @return whether a valid BSON-object/array was passed to the SAX parser
  8926. */
  8927. bool parse_bson_element_list(const bool is_array)
  8928. {
  8929. string_t key;
  8930. while (auto element_type = get())
  8931. {
  8932. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8933. {
  8934. return false;
  8935. }
  8936. const std::size_t element_type_parse_position = chars_read;
  8937. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8938. {
  8939. return false;
  8940. }
  8941. if (!is_array && !sax->key(key))
  8942. {
  8943. return false;
  8944. }
  8945. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8946. {
  8947. return false;
  8948. }
  8949. // get_bson_cstr only appends
  8950. key.clear();
  8951. }
  8952. return true;
  8953. }
  8954. /*!
  8955. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8956. @return whether a valid BSON-array was passed to the SAX parser
  8957. */
  8958. bool parse_bson_array()
  8959. {
  8960. std::int32_t document_size{};
  8961. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8962. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
  8963. {
  8964. return false;
  8965. }
  8966. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8967. {
  8968. return false;
  8969. }
  8970. return sax->end_array();
  8971. }
  8972. //////////
  8973. // CBOR //
  8974. //////////
  8975. /*!
  8976. @param[in] get_char whether a new character should be retrieved from the
  8977. input (true) or whether the last read character should
  8978. be considered instead (false)
  8979. @param[in] tag_handler how CBOR tags should be treated
  8980. @return whether a valid CBOR value was passed to the SAX parser
  8981. */
  8982. template<typename NumberType>
  8983. bool get_cbor_negative_integer()
  8984. {
  8985. NumberType number{};
  8986. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::cbor, number)))
  8987. {
  8988. return false;
  8989. }
  8990. const auto max_val = static_cast<NumberType>((std::numeric_limits<number_integer_t>::max)());
  8991. if (number > max_val)
  8992. {
  8993. return sax->parse_error(chars_read, get_token_string(),
  8994. parse_error::create(112, chars_read,
  8995. exception_message(input_format_t::cbor, "negative integer overflow", "value"), nullptr));
  8996. }
  8997. return sax->number_integer(static_cast<number_integer_t>(-1) - static_cast<number_integer_t>(number));
  8998. }
  8999. bool parse_cbor_internal(const bool get_char,
  9000. const cbor_tag_handler_t tag_handler)
  9001. {
  9002. switch (get_char ? get() : current)
  9003. {
  9004. // EOF
  9005. case char_traits<char_type>::eof():
  9006. return unexpect_eof(input_format_t::cbor, "value");
  9007. // Integer 0x00..0x17 (0..23)
  9008. case 0x00:
  9009. case 0x01:
  9010. case 0x02:
  9011. case 0x03:
  9012. case 0x04:
  9013. case 0x05:
  9014. case 0x06:
  9015. case 0x07:
  9016. case 0x08:
  9017. case 0x09:
  9018. case 0x0A:
  9019. case 0x0B:
  9020. case 0x0C:
  9021. case 0x0D:
  9022. case 0x0E:
  9023. case 0x0F:
  9024. case 0x10:
  9025. case 0x11:
  9026. case 0x12:
  9027. case 0x13:
  9028. case 0x14:
  9029. case 0x15:
  9030. case 0x16:
  9031. case 0x17:
  9032. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9033. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  9034. {
  9035. std::uint8_t number{};
  9036. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  9037. }
  9038. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  9039. {
  9040. std::uint16_t number{};
  9041. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  9042. }
  9043. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  9044. {
  9045. std::uint32_t number{};
  9046. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  9047. }
  9048. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  9049. {
  9050. std::uint64_t number{};
  9051. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  9052. }
  9053. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  9054. case 0x20:
  9055. case 0x21:
  9056. case 0x22:
  9057. case 0x23:
  9058. case 0x24:
  9059. case 0x25:
  9060. case 0x26:
  9061. case 0x27:
  9062. case 0x28:
  9063. case 0x29:
  9064. case 0x2A:
  9065. case 0x2B:
  9066. case 0x2C:
  9067. case 0x2D:
  9068. case 0x2E:
  9069. case 0x2F:
  9070. case 0x30:
  9071. case 0x31:
  9072. case 0x32:
  9073. case 0x33:
  9074. case 0x34:
  9075. case 0x35:
  9076. case 0x36:
  9077. case 0x37:
  9078. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  9079. case 0x38: // Negative integer (one-byte uint8_t follows)
  9080. return get_cbor_negative_integer<std::uint8_t>();
  9081. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  9082. return get_cbor_negative_integer<std::uint16_t>();
  9083. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  9084. return get_cbor_negative_integer<std::uint32_t>();
  9085. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  9086. return get_cbor_negative_integer<std::uint64_t>();
  9087. // Binary data (0x00..0x17 bytes follow)
  9088. case 0x40:
  9089. case 0x41:
  9090. case 0x42:
  9091. case 0x43:
  9092. case 0x44:
  9093. case 0x45:
  9094. case 0x46:
  9095. case 0x47:
  9096. case 0x48:
  9097. case 0x49:
  9098. case 0x4A:
  9099. case 0x4B:
  9100. case 0x4C:
  9101. case 0x4D:
  9102. case 0x4E:
  9103. case 0x4F:
  9104. case 0x50:
  9105. case 0x51:
  9106. case 0x52:
  9107. case 0x53:
  9108. case 0x54:
  9109. case 0x55:
  9110. case 0x56:
  9111. case 0x57:
  9112. case 0x58: // Binary data (one-byte uint8_t for n follows)
  9113. case 0x59: // Binary data (two-byte uint16_t for n follow)
  9114. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  9115. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  9116. case 0x5F: // Binary data (indefinite length)
  9117. {
  9118. binary_t b;
  9119. return get_cbor_binary(b) && sax->binary(b);
  9120. }
  9121. // UTF-8 string (0x00..0x17 bytes follow)
  9122. case 0x60:
  9123. case 0x61:
  9124. case 0x62:
  9125. case 0x63:
  9126. case 0x64:
  9127. case 0x65:
  9128. case 0x66:
  9129. case 0x67:
  9130. case 0x68:
  9131. case 0x69:
  9132. case 0x6A:
  9133. case 0x6B:
  9134. case 0x6C:
  9135. case 0x6D:
  9136. case 0x6E:
  9137. case 0x6F:
  9138. case 0x70:
  9139. case 0x71:
  9140. case 0x72:
  9141. case 0x73:
  9142. case 0x74:
  9143. case 0x75:
  9144. case 0x76:
  9145. case 0x77:
  9146. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  9147. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  9148. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  9149. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  9150. case 0x7F: // UTF-8 string (indefinite length)
  9151. {
  9152. string_t s;
  9153. return get_cbor_string(s) && sax->string(s);
  9154. }
  9155. // array (0x00..0x17 data items follow)
  9156. case 0x80:
  9157. case 0x81:
  9158. case 0x82:
  9159. case 0x83:
  9160. case 0x84:
  9161. case 0x85:
  9162. case 0x86:
  9163. case 0x87:
  9164. case 0x88:
  9165. case 0x89:
  9166. case 0x8A:
  9167. case 0x8B:
  9168. case 0x8C:
  9169. case 0x8D:
  9170. case 0x8E:
  9171. case 0x8F:
  9172. case 0x90:
  9173. case 0x91:
  9174. case 0x92:
  9175. case 0x93:
  9176. case 0x94:
  9177. case 0x95:
  9178. case 0x96:
  9179. case 0x97:
  9180. return get_cbor_array(
  9181. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  9182. case 0x98: // array (one-byte uint8_t for n follows)
  9183. {
  9184. std::uint8_t len{};
  9185. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  9186. }
  9187. case 0x99: // array (two-byte uint16_t for n follow)
  9188. {
  9189. std::uint16_t len{};
  9190. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  9191. }
  9192. case 0x9A: // array (four-byte uint32_t for n follow)
  9193. {
  9194. std::uint32_t len{};
  9195. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  9196. }
  9197. case 0x9B: // array (eight-byte uint64_t for n follow)
  9198. {
  9199. std::uint64_t len{};
  9200. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  9201. }
  9202. case 0x9F: // array (indefinite length)
  9203. return get_cbor_array(detail::unknown_size(), tag_handler);
  9204. // map (0x00..0x17 pairs of data items follow)
  9205. case 0xA0:
  9206. case 0xA1:
  9207. case 0xA2:
  9208. case 0xA3:
  9209. case 0xA4:
  9210. case 0xA5:
  9211. case 0xA6:
  9212. case 0xA7:
  9213. case 0xA8:
  9214. case 0xA9:
  9215. case 0xAA:
  9216. case 0xAB:
  9217. case 0xAC:
  9218. case 0xAD:
  9219. case 0xAE:
  9220. case 0xAF:
  9221. case 0xB0:
  9222. case 0xB1:
  9223. case 0xB2:
  9224. case 0xB3:
  9225. case 0xB4:
  9226. case 0xB5:
  9227. case 0xB6:
  9228. case 0xB7:
  9229. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  9230. case 0xB8: // map (one-byte uint8_t for n follows)
  9231. {
  9232. std::uint8_t len{};
  9233. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  9234. }
  9235. case 0xB9: // map (two-byte uint16_t for n follow)
  9236. {
  9237. std::uint16_t len{};
  9238. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  9239. }
  9240. case 0xBA: // map (four-byte uint32_t for n follow)
  9241. {
  9242. std::uint32_t len{};
  9243. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  9244. }
  9245. case 0xBB: // map (eight-byte uint64_t for n follow)
  9246. {
  9247. std::uint64_t len{};
  9248. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  9249. }
  9250. case 0xBF: // map (indefinite length)
  9251. return get_cbor_object(detail::unknown_size(), tag_handler);
  9252. case 0xC6: // tagged item
  9253. case 0xC7:
  9254. case 0xC8:
  9255. case 0xC9:
  9256. case 0xCA:
  9257. case 0xCB:
  9258. case 0xCC:
  9259. case 0xCD:
  9260. case 0xCE:
  9261. case 0xCF:
  9262. case 0xD0:
  9263. case 0xD1:
  9264. case 0xD2:
  9265. case 0xD3:
  9266. case 0xD4:
  9267. case 0xD8: // tagged item (1 byte follows)
  9268. case 0xD9: // tagged item (2 bytes follow)
  9269. case 0xDA: // tagged item (4 bytes follow)
  9270. case 0xDB: // tagged item (8 bytes follow)
  9271. {
  9272. switch (tag_handler)
  9273. {
  9274. case cbor_tag_handler_t::error:
  9275. {
  9276. auto last_token = get_token_string();
  9277. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9278. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9279. }
  9280. case cbor_tag_handler_t::ignore:
  9281. {
  9282. // ignore binary subtype
  9283. switch (current)
  9284. {
  9285. case 0xD8:
  9286. {
  9287. std::uint8_t subtype_to_ignore{};
  9288. get_number(input_format_t::cbor, subtype_to_ignore);
  9289. break;
  9290. }
  9291. case 0xD9:
  9292. {
  9293. std::uint16_t subtype_to_ignore{};
  9294. get_number(input_format_t::cbor, subtype_to_ignore);
  9295. break;
  9296. }
  9297. case 0xDA:
  9298. {
  9299. std::uint32_t subtype_to_ignore{};
  9300. get_number(input_format_t::cbor, subtype_to_ignore);
  9301. break;
  9302. }
  9303. case 0xDB:
  9304. {
  9305. std::uint64_t subtype_to_ignore{};
  9306. get_number(input_format_t::cbor, subtype_to_ignore);
  9307. break;
  9308. }
  9309. default:
  9310. break;
  9311. }
  9312. return parse_cbor_internal(true, tag_handler);
  9313. }
  9314. case cbor_tag_handler_t::store:
  9315. {
  9316. binary_t b;
  9317. // use binary subtype and store in a binary container
  9318. switch (current)
  9319. {
  9320. case 0xD8:
  9321. {
  9322. std::uint8_t subtype{};
  9323. get_number(input_format_t::cbor, subtype);
  9324. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9325. break;
  9326. }
  9327. case 0xD9:
  9328. {
  9329. std::uint16_t subtype{};
  9330. get_number(input_format_t::cbor, subtype);
  9331. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9332. break;
  9333. }
  9334. case 0xDA:
  9335. {
  9336. std::uint32_t subtype{};
  9337. get_number(input_format_t::cbor, subtype);
  9338. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9339. break;
  9340. }
  9341. case 0xDB:
  9342. {
  9343. std::uint64_t subtype{};
  9344. get_number(input_format_t::cbor, subtype);
  9345. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  9346. break;
  9347. }
  9348. default:
  9349. return parse_cbor_internal(true, tag_handler);
  9350. }
  9351. get();
  9352. return get_cbor_binary(b) && sax->binary(b);
  9353. }
  9354. default: // LCOV_EXCL_LINE
  9355. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  9356. return false; // LCOV_EXCL_LINE
  9357. }
  9358. }
  9359. case 0xF4: // false
  9360. return sax->boolean(false);
  9361. case 0xF5: // true
  9362. return sax->boolean(true);
  9363. case 0xF6: // null
  9364. return sax->null();
  9365. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  9366. {
  9367. const auto byte1_raw = get();
  9368. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  9369. {
  9370. return false;
  9371. }
  9372. const auto byte2_raw = get();
  9373. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  9374. {
  9375. return false;
  9376. }
  9377. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  9378. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  9379. // Code from RFC 7049, Appendix D, Figure 3:
  9380. // As half-precision floating-point numbers were only added
  9381. // to IEEE 754 in 2008, today's programming platforms often
  9382. // still only have limited support for them. It is very
  9383. // easy to include at least decoding support for them even
  9384. // without such support. An example of a small decoder for
  9385. // half-precision floating-point numbers in the C language
  9386. // is shown in Fig. 3.
  9387. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  9388. const double val = [&half]
  9389. {
  9390. const int exp = (half >> 10u) & 0x1Fu;
  9391. const unsigned int mant = half & 0x3FFu;
  9392. JSON_ASSERT(0 <= exp&& exp <= 32);
  9393. JSON_ASSERT(mant <= 1024);
  9394. switch (exp)
  9395. {
  9396. case 0:
  9397. return std::ldexp(mant, -24);
  9398. case 31:
  9399. return (mant == 0)
  9400. ? std::numeric_limits<double>::infinity()
  9401. : std::numeric_limits<double>::quiet_NaN();
  9402. default:
  9403. return std::ldexp(mant + 1024, exp - 25);
  9404. }
  9405. }();
  9406. return sax->number_float((half & 0x8000u) != 0
  9407. ? static_cast<number_float_t>(-val)
  9408. : static_cast<number_float_t>(val), "");
  9409. }
  9410. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  9411. {
  9412. float number{};
  9413. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9414. }
  9415. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  9416. {
  9417. double number{};
  9418. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9419. }
  9420. default: // anything else (0xFF is handled inside the other types)
  9421. {
  9422. auto last_token = get_token_string();
  9423. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9424. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9425. }
  9426. }
  9427. }
  9428. /*!
  9429. @brief reads a CBOR string
  9430. This function first reads starting bytes to determine the expected
  9431. string length and then copies this number of bytes into a string.
  9432. Additionally, CBOR's strings with indefinite lengths are supported.
  9433. @param[out] result created string
  9434. @return whether string creation completed
  9435. */
  9436. bool get_cbor_string(string_t& result)
  9437. {
  9438. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  9439. {
  9440. return false;
  9441. }
  9442. switch (current)
  9443. {
  9444. // UTF-8 string (0x00..0x17 bytes follow)
  9445. case 0x60:
  9446. case 0x61:
  9447. case 0x62:
  9448. case 0x63:
  9449. case 0x64:
  9450. case 0x65:
  9451. case 0x66:
  9452. case 0x67:
  9453. case 0x68:
  9454. case 0x69:
  9455. case 0x6A:
  9456. case 0x6B:
  9457. case 0x6C:
  9458. case 0x6D:
  9459. case 0x6E:
  9460. case 0x6F:
  9461. case 0x70:
  9462. case 0x71:
  9463. case 0x72:
  9464. case 0x73:
  9465. case 0x74:
  9466. case 0x75:
  9467. case 0x76:
  9468. case 0x77:
  9469. {
  9470. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  9471. }
  9472. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  9473. {
  9474. std::uint8_t len{};
  9475. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9476. }
  9477. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  9478. {
  9479. std::uint16_t len{};
  9480. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9481. }
  9482. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  9483. {
  9484. std::uint32_t len{};
  9485. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9486. }
  9487. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  9488. {
  9489. std::uint64_t len{};
  9490. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  9491. }
  9492. case 0x7F: // UTF-8 string (indefinite length)
  9493. {
  9494. while (get() != 0xFF)
  9495. {
  9496. string_t chunk;
  9497. if (!get_cbor_string(chunk))
  9498. {
  9499. return false;
  9500. }
  9501. result.append(chunk);
  9502. }
  9503. return true;
  9504. }
  9505. default:
  9506. {
  9507. auto last_token = get_token_string();
  9508. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9509. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  9510. }
  9511. }
  9512. }
  9513. /*!
  9514. @brief reads a CBOR byte array
  9515. This function first reads starting bytes to determine the expected
  9516. byte array length and then copies this number of bytes into the byte array.
  9517. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  9518. @param[out] result created byte array
  9519. @return whether byte array creation completed
  9520. */
  9521. bool get_cbor_binary(binary_t& result)
  9522. {
  9523. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  9524. {
  9525. return false;
  9526. }
  9527. switch (current)
  9528. {
  9529. // Binary data (0x00..0x17 bytes follow)
  9530. case 0x40:
  9531. case 0x41:
  9532. case 0x42:
  9533. case 0x43:
  9534. case 0x44:
  9535. case 0x45:
  9536. case 0x46:
  9537. case 0x47:
  9538. case 0x48:
  9539. case 0x49:
  9540. case 0x4A:
  9541. case 0x4B:
  9542. case 0x4C:
  9543. case 0x4D:
  9544. case 0x4E:
  9545. case 0x4F:
  9546. case 0x50:
  9547. case 0x51:
  9548. case 0x52:
  9549. case 0x53:
  9550. case 0x54:
  9551. case 0x55:
  9552. case 0x56:
  9553. case 0x57:
  9554. {
  9555. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  9556. }
  9557. case 0x58: // Binary data (one-byte uint8_t for n follows)
  9558. {
  9559. std::uint8_t len{};
  9560. return get_number(input_format_t::cbor, len) &&
  9561. get_binary(input_format_t::cbor, len, result);
  9562. }
  9563. case 0x59: // Binary data (two-byte uint16_t for n follow)
  9564. {
  9565. std::uint16_t len{};
  9566. return get_number(input_format_t::cbor, len) &&
  9567. get_binary(input_format_t::cbor, len, result);
  9568. }
  9569. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  9570. {
  9571. std::uint32_t len{};
  9572. return get_number(input_format_t::cbor, len) &&
  9573. get_binary(input_format_t::cbor, len, result);
  9574. }
  9575. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  9576. {
  9577. std::uint64_t len{};
  9578. return get_number(input_format_t::cbor, len) &&
  9579. get_binary(input_format_t::cbor, len, result);
  9580. }
  9581. case 0x5F: // Binary data (indefinite length)
  9582. {
  9583. while (get() != 0xFF)
  9584. {
  9585. binary_t chunk;
  9586. if (!get_cbor_binary(chunk))
  9587. {
  9588. return false;
  9589. }
  9590. result.insert(result.end(), chunk.begin(), chunk.end());
  9591. }
  9592. return true;
  9593. }
  9594. default:
  9595. {
  9596. auto last_token = get_token_string();
  9597. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9598. 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));
  9599. }
  9600. }
  9601. }
  9602. /*!
  9603. @param[in] len the length of the array or detail::unknown_size() for an
  9604. array of indefinite size
  9605. @param[in] tag_handler how CBOR tags should be treated
  9606. @return whether array creation completed
  9607. */
  9608. bool get_cbor_array(const std::size_t len,
  9609. const cbor_tag_handler_t tag_handler)
  9610. {
  9611. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9612. {
  9613. return false;
  9614. }
  9615. if (len != detail::unknown_size())
  9616. {
  9617. for (std::size_t i = 0; i < len; ++i)
  9618. {
  9619. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  9620. {
  9621. return false;
  9622. }
  9623. }
  9624. }
  9625. else
  9626. {
  9627. while (get() != 0xFF)
  9628. {
  9629. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  9630. {
  9631. return false;
  9632. }
  9633. }
  9634. }
  9635. return sax->end_array();
  9636. }
  9637. /*!
  9638. @param[in] len the length of the object or detail::unknown_size() for an
  9639. object of indefinite size
  9640. @param[in] tag_handler how CBOR tags should be treated
  9641. @return whether object creation completed
  9642. */
  9643. bool get_cbor_object(const std::size_t len,
  9644. const cbor_tag_handler_t tag_handler)
  9645. {
  9646. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9647. {
  9648. return false;
  9649. }
  9650. if (len != 0)
  9651. {
  9652. string_t key;
  9653. if (len != detail::unknown_size())
  9654. {
  9655. for (std::size_t i = 0; i < len; ++i)
  9656. {
  9657. get();
  9658. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  9659. {
  9660. return false;
  9661. }
  9662. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  9663. {
  9664. return false;
  9665. }
  9666. key.clear();
  9667. }
  9668. }
  9669. else
  9670. {
  9671. while (get() != 0xFF)
  9672. {
  9673. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  9674. {
  9675. return false;
  9676. }
  9677. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  9678. {
  9679. return false;
  9680. }
  9681. key.clear();
  9682. }
  9683. }
  9684. }
  9685. return sax->end_object();
  9686. }
  9687. /////////////
  9688. // MsgPack //
  9689. /////////////
  9690. /*!
  9691. @return whether a valid MessagePack value was passed to the SAX parser
  9692. */
  9693. bool parse_msgpack_internal()
  9694. {
  9695. switch (get())
  9696. {
  9697. // EOF
  9698. case char_traits<char_type>::eof():
  9699. return unexpect_eof(input_format_t::msgpack, "value");
  9700. // positive fixint
  9701. case 0x00:
  9702. case 0x01:
  9703. case 0x02:
  9704. case 0x03:
  9705. case 0x04:
  9706. case 0x05:
  9707. case 0x06:
  9708. case 0x07:
  9709. case 0x08:
  9710. case 0x09:
  9711. case 0x0A:
  9712. case 0x0B:
  9713. case 0x0C:
  9714. case 0x0D:
  9715. case 0x0E:
  9716. case 0x0F:
  9717. case 0x10:
  9718. case 0x11:
  9719. case 0x12:
  9720. case 0x13:
  9721. case 0x14:
  9722. case 0x15:
  9723. case 0x16:
  9724. case 0x17:
  9725. case 0x18:
  9726. case 0x19:
  9727. case 0x1A:
  9728. case 0x1B:
  9729. case 0x1C:
  9730. case 0x1D:
  9731. case 0x1E:
  9732. case 0x1F:
  9733. case 0x20:
  9734. case 0x21:
  9735. case 0x22:
  9736. case 0x23:
  9737. case 0x24:
  9738. case 0x25:
  9739. case 0x26:
  9740. case 0x27:
  9741. case 0x28:
  9742. case 0x29:
  9743. case 0x2A:
  9744. case 0x2B:
  9745. case 0x2C:
  9746. case 0x2D:
  9747. case 0x2E:
  9748. case 0x2F:
  9749. case 0x30:
  9750. case 0x31:
  9751. case 0x32:
  9752. case 0x33:
  9753. case 0x34:
  9754. case 0x35:
  9755. case 0x36:
  9756. case 0x37:
  9757. case 0x38:
  9758. case 0x39:
  9759. case 0x3A:
  9760. case 0x3B:
  9761. case 0x3C:
  9762. case 0x3D:
  9763. case 0x3E:
  9764. case 0x3F:
  9765. case 0x40:
  9766. case 0x41:
  9767. case 0x42:
  9768. case 0x43:
  9769. case 0x44:
  9770. case 0x45:
  9771. case 0x46:
  9772. case 0x47:
  9773. case 0x48:
  9774. case 0x49:
  9775. case 0x4A:
  9776. case 0x4B:
  9777. case 0x4C:
  9778. case 0x4D:
  9779. case 0x4E:
  9780. case 0x4F:
  9781. case 0x50:
  9782. case 0x51:
  9783. case 0x52:
  9784. case 0x53:
  9785. case 0x54:
  9786. case 0x55:
  9787. case 0x56:
  9788. case 0x57:
  9789. case 0x58:
  9790. case 0x59:
  9791. case 0x5A:
  9792. case 0x5B:
  9793. case 0x5C:
  9794. case 0x5D:
  9795. case 0x5E:
  9796. case 0x5F:
  9797. case 0x60:
  9798. case 0x61:
  9799. case 0x62:
  9800. case 0x63:
  9801. case 0x64:
  9802. case 0x65:
  9803. case 0x66:
  9804. case 0x67:
  9805. case 0x68:
  9806. case 0x69:
  9807. case 0x6A:
  9808. case 0x6B:
  9809. case 0x6C:
  9810. case 0x6D:
  9811. case 0x6E:
  9812. case 0x6F:
  9813. case 0x70:
  9814. case 0x71:
  9815. case 0x72:
  9816. case 0x73:
  9817. case 0x74:
  9818. case 0x75:
  9819. case 0x76:
  9820. case 0x77:
  9821. case 0x78:
  9822. case 0x79:
  9823. case 0x7A:
  9824. case 0x7B:
  9825. case 0x7C:
  9826. case 0x7D:
  9827. case 0x7E:
  9828. case 0x7F:
  9829. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9830. // fixmap
  9831. case 0x80:
  9832. case 0x81:
  9833. case 0x82:
  9834. case 0x83:
  9835. case 0x84:
  9836. case 0x85:
  9837. case 0x86:
  9838. case 0x87:
  9839. case 0x88:
  9840. case 0x89:
  9841. case 0x8A:
  9842. case 0x8B:
  9843. case 0x8C:
  9844. case 0x8D:
  9845. case 0x8E:
  9846. case 0x8F:
  9847. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9848. // fixarray
  9849. case 0x90:
  9850. case 0x91:
  9851. case 0x92:
  9852. case 0x93:
  9853. case 0x94:
  9854. case 0x95:
  9855. case 0x96:
  9856. case 0x97:
  9857. case 0x98:
  9858. case 0x99:
  9859. case 0x9A:
  9860. case 0x9B:
  9861. case 0x9C:
  9862. case 0x9D:
  9863. case 0x9E:
  9864. case 0x9F:
  9865. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9866. // fixstr
  9867. case 0xA0:
  9868. case 0xA1:
  9869. case 0xA2:
  9870. case 0xA3:
  9871. case 0xA4:
  9872. case 0xA5:
  9873. case 0xA6:
  9874. case 0xA7:
  9875. case 0xA8:
  9876. case 0xA9:
  9877. case 0xAA:
  9878. case 0xAB:
  9879. case 0xAC:
  9880. case 0xAD:
  9881. case 0xAE:
  9882. case 0xAF:
  9883. case 0xB0:
  9884. case 0xB1:
  9885. case 0xB2:
  9886. case 0xB3:
  9887. case 0xB4:
  9888. case 0xB5:
  9889. case 0xB6:
  9890. case 0xB7:
  9891. case 0xB8:
  9892. case 0xB9:
  9893. case 0xBA:
  9894. case 0xBB:
  9895. case 0xBC:
  9896. case 0xBD:
  9897. case 0xBE:
  9898. case 0xBF:
  9899. case 0xD9: // str 8
  9900. case 0xDA: // str 16
  9901. case 0xDB: // str 32
  9902. {
  9903. string_t s;
  9904. return get_msgpack_string(s) && sax->string(s);
  9905. }
  9906. case 0xC0: // nil
  9907. return sax->null();
  9908. case 0xC2: // false
  9909. return sax->boolean(false);
  9910. case 0xC3: // true
  9911. return sax->boolean(true);
  9912. case 0xC4: // bin 8
  9913. case 0xC5: // bin 16
  9914. case 0xC6: // bin 32
  9915. case 0xC7: // ext 8
  9916. case 0xC8: // ext 16
  9917. case 0xC9: // ext 32
  9918. case 0xD4: // fixext 1
  9919. case 0xD5: // fixext 2
  9920. case 0xD6: // fixext 4
  9921. case 0xD7: // fixext 8
  9922. case 0xD8: // fixext 16
  9923. {
  9924. binary_t b;
  9925. return get_msgpack_binary(b) && sax->binary(b);
  9926. }
  9927. case 0xCA: // float 32
  9928. {
  9929. float number{};
  9930. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9931. }
  9932. case 0xCB: // float 64
  9933. {
  9934. double number{};
  9935. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9936. }
  9937. case 0xCC: // uint 8
  9938. {
  9939. std::uint8_t number{};
  9940. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9941. }
  9942. case 0xCD: // uint 16
  9943. {
  9944. std::uint16_t number{};
  9945. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9946. }
  9947. case 0xCE: // uint 32
  9948. {
  9949. std::uint32_t number{};
  9950. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9951. }
  9952. case 0xCF: // uint 64
  9953. {
  9954. std::uint64_t number{};
  9955. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9956. }
  9957. case 0xD0: // int 8
  9958. {
  9959. std::int8_t number{};
  9960. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9961. }
  9962. case 0xD1: // int 16
  9963. {
  9964. std::int16_t number{};
  9965. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9966. }
  9967. case 0xD2: // int 32
  9968. {
  9969. std::int32_t number{};
  9970. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9971. }
  9972. case 0xD3: // int 64
  9973. {
  9974. std::int64_t number{};
  9975. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9976. }
  9977. case 0xDC: // array 16
  9978. {
  9979. std::uint16_t len{};
  9980. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9981. }
  9982. case 0xDD: // array 32
  9983. {
  9984. std::uint32_t len{};
  9985. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9986. }
  9987. case 0xDE: // map 16
  9988. {
  9989. std::uint16_t len{};
  9990. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9991. }
  9992. case 0xDF: // map 32
  9993. {
  9994. std::uint32_t len{};
  9995. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9996. }
  9997. // negative fixint
  9998. case 0xE0:
  9999. case 0xE1:
  10000. case 0xE2:
  10001. case 0xE3:
  10002. case 0xE4:
  10003. case 0xE5:
  10004. case 0xE6:
  10005. case 0xE7:
  10006. case 0xE8:
  10007. case 0xE9:
  10008. case 0xEA:
  10009. case 0xEB:
  10010. case 0xEC:
  10011. case 0xED:
  10012. case 0xEE:
  10013. case 0xEF:
  10014. case 0xF0:
  10015. case 0xF1:
  10016. case 0xF2:
  10017. case 0xF3:
  10018. case 0xF4:
  10019. case 0xF5:
  10020. case 0xF6:
  10021. case 0xF7:
  10022. case 0xF8:
  10023. case 0xF9:
  10024. case 0xFA:
  10025. case 0xFB:
  10026. case 0xFC:
  10027. case 0xFD:
  10028. case 0xFE:
  10029. case 0xFF:
  10030. return sax->number_integer(static_cast<std::int8_t>(current));
  10031. default: // anything else
  10032. {
  10033. auto last_token = get_token_string();
  10034. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10035. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  10036. }
  10037. }
  10038. }
  10039. /*!
  10040. @brief reads a MessagePack string
  10041. This function first reads starting bytes to determine the expected
  10042. string length and then copies this number of bytes into a string.
  10043. @param[out] result created string
  10044. @return whether string creation completed
  10045. */
  10046. bool get_msgpack_string(string_t& result)
  10047. {
  10048. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  10049. {
  10050. return false;
  10051. }
  10052. switch (current)
  10053. {
  10054. // fixstr
  10055. case 0xA0:
  10056. case 0xA1:
  10057. case 0xA2:
  10058. case 0xA3:
  10059. case 0xA4:
  10060. case 0xA5:
  10061. case 0xA6:
  10062. case 0xA7:
  10063. case 0xA8:
  10064. case 0xA9:
  10065. case 0xAA:
  10066. case 0xAB:
  10067. case 0xAC:
  10068. case 0xAD:
  10069. case 0xAE:
  10070. case 0xAF:
  10071. case 0xB0:
  10072. case 0xB1:
  10073. case 0xB2:
  10074. case 0xB3:
  10075. case 0xB4:
  10076. case 0xB5:
  10077. case 0xB6:
  10078. case 0xB7:
  10079. case 0xB8:
  10080. case 0xB9:
  10081. case 0xBA:
  10082. case 0xBB:
  10083. case 0xBC:
  10084. case 0xBD:
  10085. case 0xBE:
  10086. case 0xBF:
  10087. {
  10088. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  10089. }
  10090. case 0xD9: // str 8
  10091. {
  10092. std::uint8_t len{};
  10093. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  10094. }
  10095. case 0xDA: // str 16
  10096. {
  10097. std::uint16_t len{};
  10098. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  10099. }
  10100. case 0xDB: // str 32
  10101. {
  10102. std::uint32_t len{};
  10103. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  10104. }
  10105. default:
  10106. {
  10107. auto last_token = get_token_string();
  10108. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10109. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  10110. }
  10111. }
  10112. }
  10113. /*!
  10114. @brief reads a MessagePack byte array
  10115. This function first reads starting bytes to determine the expected
  10116. byte array length and then copies this number of bytes into a byte array.
  10117. @param[out] result created byte array
  10118. @return whether byte array creation completed
  10119. */
  10120. bool get_msgpack_binary(binary_t& result)
  10121. {
  10122. // helper function to set the subtype
  10123. auto assign_and_return_true = [&result](std::int8_t subtype)
  10124. {
  10125. result.set_subtype(static_cast<std::uint8_t>(subtype));
  10126. return true;
  10127. };
  10128. switch (current)
  10129. {
  10130. case 0xC4: // bin 8
  10131. {
  10132. std::uint8_t len{};
  10133. return get_number(input_format_t::msgpack, len) &&
  10134. get_binary(input_format_t::msgpack, len, result);
  10135. }
  10136. case 0xC5: // bin 16
  10137. {
  10138. std::uint16_t len{};
  10139. return get_number(input_format_t::msgpack, len) &&
  10140. get_binary(input_format_t::msgpack, len, result);
  10141. }
  10142. case 0xC6: // bin 32
  10143. {
  10144. std::uint32_t len{};
  10145. return get_number(input_format_t::msgpack, len) &&
  10146. get_binary(input_format_t::msgpack, len, result);
  10147. }
  10148. case 0xC7: // ext 8
  10149. {
  10150. std::uint8_t len{};
  10151. std::int8_t subtype{};
  10152. return get_number(input_format_t::msgpack, len) &&
  10153. get_number(input_format_t::msgpack, subtype) &&
  10154. get_binary(input_format_t::msgpack, len, result) &&
  10155. assign_and_return_true(subtype);
  10156. }
  10157. case 0xC8: // ext 16
  10158. {
  10159. std::uint16_t len{};
  10160. std::int8_t subtype{};
  10161. return get_number(input_format_t::msgpack, len) &&
  10162. get_number(input_format_t::msgpack, subtype) &&
  10163. get_binary(input_format_t::msgpack, len, result) &&
  10164. assign_and_return_true(subtype);
  10165. }
  10166. case 0xC9: // ext 32
  10167. {
  10168. std::uint32_t len{};
  10169. std::int8_t subtype{};
  10170. return get_number(input_format_t::msgpack, len) &&
  10171. get_number(input_format_t::msgpack, subtype) &&
  10172. get_binary(input_format_t::msgpack, len, result) &&
  10173. assign_and_return_true(subtype);
  10174. }
  10175. case 0xD4: // fixext 1
  10176. {
  10177. std::int8_t subtype{};
  10178. return get_number(input_format_t::msgpack, subtype) &&
  10179. get_binary(input_format_t::msgpack, 1, result) &&
  10180. assign_and_return_true(subtype);
  10181. }
  10182. case 0xD5: // fixext 2
  10183. {
  10184. std::int8_t subtype{};
  10185. return get_number(input_format_t::msgpack, subtype) &&
  10186. get_binary(input_format_t::msgpack, 2, result) &&
  10187. assign_and_return_true(subtype);
  10188. }
  10189. case 0xD6: // fixext 4
  10190. {
  10191. std::int8_t subtype{};
  10192. return get_number(input_format_t::msgpack, subtype) &&
  10193. get_binary(input_format_t::msgpack, 4, result) &&
  10194. assign_and_return_true(subtype);
  10195. }
  10196. case 0xD7: // fixext 8
  10197. {
  10198. std::int8_t subtype{};
  10199. return get_number(input_format_t::msgpack, subtype) &&
  10200. get_binary(input_format_t::msgpack, 8, result) &&
  10201. assign_and_return_true(subtype);
  10202. }
  10203. case 0xD8: // fixext 16
  10204. {
  10205. std::int8_t subtype{};
  10206. return get_number(input_format_t::msgpack, subtype) &&
  10207. get_binary(input_format_t::msgpack, 16, result) &&
  10208. assign_and_return_true(subtype);
  10209. }
  10210. default: // LCOV_EXCL_LINE
  10211. return false; // LCOV_EXCL_LINE
  10212. }
  10213. }
  10214. /*!
  10215. @param[in] len the length of the array
  10216. @return whether array creation completed
  10217. */
  10218. bool get_msgpack_array(const std::size_t len)
  10219. {
  10220. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  10221. {
  10222. return false;
  10223. }
  10224. for (std::size_t i = 0; i < len; ++i)
  10225. {
  10226. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  10227. {
  10228. return false;
  10229. }
  10230. }
  10231. return sax->end_array();
  10232. }
  10233. /*!
  10234. @param[in] len the length of the object
  10235. @return whether object creation completed
  10236. */
  10237. bool get_msgpack_object(const std::size_t len)
  10238. {
  10239. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  10240. {
  10241. return false;
  10242. }
  10243. string_t key;
  10244. for (std::size_t i = 0; i < len; ++i)
  10245. {
  10246. get();
  10247. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  10248. {
  10249. return false;
  10250. }
  10251. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  10252. {
  10253. return false;
  10254. }
  10255. key.clear();
  10256. }
  10257. return sax->end_object();
  10258. }
  10259. ////////////
  10260. // UBJSON //
  10261. ////////////
  10262. /*!
  10263. @param[in] get_char whether a new character should be retrieved from the
  10264. input (true, default) or whether the last read
  10265. character should be considered instead
  10266. @return whether a valid UBJSON value was passed to the SAX parser
  10267. */
  10268. bool parse_ubjson_internal(const bool get_char = true)
  10269. {
  10270. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  10271. }
  10272. /*!
  10273. @brief reads a UBJSON string
  10274. This function is either called after reading the 'S' byte explicitly
  10275. indicating a string, or in case of an object key where the 'S' byte can be
  10276. left out.
  10277. @param[out] result created string
  10278. @param[in] get_char whether a new character should be retrieved from the
  10279. input (true, default) or whether the last read
  10280. character should be considered instead
  10281. @return whether string creation completed
  10282. */
  10283. bool get_ubjson_string(string_t& result, const bool get_char = true)
  10284. {
  10285. if (get_char)
  10286. {
  10287. get(); // TODO(niels): may we ignore N here?
  10288. }
  10289. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  10290. {
  10291. return false;
  10292. }
  10293. switch (current)
  10294. {
  10295. case 'U':
  10296. {
  10297. std::uint8_t len{};
  10298. return get_number(input_format, len) && get_string(input_format, len, result);
  10299. }
  10300. case 'i':
  10301. {
  10302. std::int8_t len{};
  10303. return get_number(input_format, len) && get_string(input_format, len, result);
  10304. }
  10305. case 'I':
  10306. {
  10307. std::int16_t len{};
  10308. return get_number(input_format, len) && get_string(input_format, len, result);
  10309. }
  10310. case 'l':
  10311. {
  10312. std::int32_t len{};
  10313. return get_number(input_format, len) && get_string(input_format, len, result);
  10314. }
  10315. case 'L':
  10316. {
  10317. std::int64_t len{};
  10318. return get_number(input_format, len) && get_string(input_format, len, result);
  10319. }
  10320. case 'u':
  10321. {
  10322. if (input_format != input_format_t::bjdata)
  10323. {
  10324. break;
  10325. }
  10326. std::uint16_t len{};
  10327. return get_number(input_format, len) && get_string(input_format, len, result);
  10328. }
  10329. case 'm':
  10330. {
  10331. if (input_format != input_format_t::bjdata)
  10332. {
  10333. break;
  10334. }
  10335. std::uint32_t len{};
  10336. return get_number(input_format, len) && get_string(input_format, len, result);
  10337. }
  10338. case 'M':
  10339. {
  10340. if (input_format != input_format_t::bjdata)
  10341. {
  10342. break;
  10343. }
  10344. std::uint64_t len{};
  10345. return get_number(input_format, len) && get_string(input_format, len, result);
  10346. }
  10347. default:
  10348. break;
  10349. }
  10350. auto last_token = get_token_string();
  10351. std::string message;
  10352. if (input_format != input_format_t::bjdata)
  10353. {
  10354. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  10355. }
  10356. else
  10357. {
  10358. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  10359. }
  10360. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  10361. }
  10362. /*!
  10363. @param[out] dim an integer vector storing the ND array dimensions
  10364. @return whether reading ND array size vector is successful
  10365. */
  10366. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  10367. {
  10368. std::pair<std::size_t, char_int_type> size_and_type;
  10369. size_t dimlen = 0;
  10370. bool no_ndarray = true;
  10371. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  10372. {
  10373. return false;
  10374. }
  10375. if (size_and_type.first != npos)
  10376. {
  10377. if (size_and_type.second != 0)
  10378. {
  10379. if (size_and_type.second != 'N')
  10380. {
  10381. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10382. {
  10383. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  10384. {
  10385. return false;
  10386. }
  10387. dim.push_back(dimlen);
  10388. }
  10389. }
  10390. }
  10391. else
  10392. {
  10393. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10394. {
  10395. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  10396. {
  10397. return false;
  10398. }
  10399. dim.push_back(dimlen);
  10400. }
  10401. }
  10402. }
  10403. else
  10404. {
  10405. while (current != ']')
  10406. {
  10407. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  10408. {
  10409. return false;
  10410. }
  10411. dim.push_back(dimlen);
  10412. get_ignore_noop();
  10413. }
  10414. }
  10415. return true;
  10416. }
  10417. /*!
  10418. @param[out] result determined size
  10419. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  10420. or ndarray dimension is not allowed; `false` means ndarray
  10421. is allowed; for output, `true` means an ndarray is found;
  10422. is_ndarray can only return `true` when its initial value
  10423. is `false`
  10424. @param[in] prefix type marker if already read, otherwise set to 0
  10425. @return whether size determination completed
  10426. */
  10427. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  10428. {
  10429. if (prefix == 0)
  10430. {
  10431. prefix = get_ignore_noop();
  10432. }
  10433. switch (prefix)
  10434. {
  10435. case 'U':
  10436. {
  10437. std::uint8_t number{};
  10438. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10439. {
  10440. return false;
  10441. }
  10442. result = static_cast<std::size_t>(number);
  10443. return true;
  10444. }
  10445. case 'i':
  10446. {
  10447. std::int8_t number{};
  10448. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10449. {
  10450. return false;
  10451. }
  10452. if (number < 0)
  10453. {
  10454. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10455. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10456. }
  10457. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  10458. return true;
  10459. }
  10460. case 'I':
  10461. {
  10462. std::int16_t number{};
  10463. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10464. {
  10465. return false;
  10466. }
  10467. if (number < 0)
  10468. {
  10469. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10470. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10471. }
  10472. result = static_cast<std::size_t>(number);
  10473. return true;
  10474. }
  10475. case 'l':
  10476. {
  10477. std::int32_t number{};
  10478. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10479. {
  10480. return false;
  10481. }
  10482. if (number < 0)
  10483. {
  10484. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10485. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10486. }
  10487. result = static_cast<std::size_t>(number);
  10488. return true;
  10489. }
  10490. case 'L':
  10491. {
  10492. std::int64_t number{};
  10493. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10494. {
  10495. return false;
  10496. }
  10497. if (number < 0)
  10498. {
  10499. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  10500. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  10501. }
  10502. if (!value_in_range_of<std::size_t>(number))
  10503. {
  10504. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  10505. exception_message(input_format, "integer value overflow", "size"), nullptr));
  10506. }
  10507. result = static_cast<std::size_t>(number);
  10508. return true;
  10509. }
  10510. case 'u':
  10511. {
  10512. if (input_format != input_format_t::bjdata)
  10513. {
  10514. break;
  10515. }
  10516. std::uint16_t number{};
  10517. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10518. {
  10519. return false;
  10520. }
  10521. result = static_cast<std::size_t>(number);
  10522. return true;
  10523. }
  10524. case 'm':
  10525. {
  10526. if (input_format != input_format_t::bjdata)
  10527. {
  10528. break;
  10529. }
  10530. std::uint32_t number{};
  10531. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10532. {
  10533. return false;
  10534. }
  10535. result = conditional_static_cast<std::size_t>(number);
  10536. return true;
  10537. }
  10538. case 'M':
  10539. {
  10540. if (input_format != input_format_t::bjdata)
  10541. {
  10542. break;
  10543. }
  10544. std::uint64_t number{};
  10545. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  10546. {
  10547. return false;
  10548. }
  10549. if (!value_in_range_of<std::size_t>(number))
  10550. {
  10551. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  10552. exception_message(input_format, "integer value overflow", "size"), nullptr));
  10553. }
  10554. result = detail::conditional_static_cast<std::size_t>(number);
  10555. return true;
  10556. }
  10557. case '[':
  10558. {
  10559. if (input_format != input_format_t::bjdata)
  10560. {
  10561. break;
  10562. }
  10563. if (is_ndarray) // ndarray dimensional vector can only contain integers and cannot embed another array
  10564. {
  10565. 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));
  10566. }
  10567. std::vector<size_t> dim;
  10568. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  10569. {
  10570. return false;
  10571. }
  10572. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  10573. {
  10574. result = dim.at(dim.size() - 1);
  10575. return true;
  10576. }
  10577. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  10578. {
  10579. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  10580. {
  10581. if ( i == 0 )
  10582. {
  10583. result = 0;
  10584. return true;
  10585. }
  10586. }
  10587. string_t key = "_ArraySize_";
  10588. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  10589. {
  10590. return false;
  10591. }
  10592. result = 1;
  10593. for (auto i : dim)
  10594. {
  10595. // Pre-multiplication overflow check: if i > 0 and result > SIZE_MAX/i, then result*i would overflow.
  10596. // This check must happen before multiplication since overflow detection after the fact is unreliable
  10597. // as modular arithmetic can produce any value, not just 0 or SIZE_MAX.
  10598. if (JSON_HEDLEY_UNLIKELY(i > 0 && result > (std::numeric_limits<std::size_t>::max)() / i))
  10599. {
  10600. 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));
  10601. }
  10602. result *= i;
  10603. // Additional post-multiplication check to catch any edge cases the pre-check might miss
  10604. if (result == 0 || result == npos)
  10605. {
  10606. 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));
  10607. }
  10608. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  10609. {
  10610. return false;
  10611. }
  10612. }
  10613. is_ndarray = true;
  10614. return sax->end_array();
  10615. }
  10616. result = 0;
  10617. return true;
  10618. }
  10619. default:
  10620. break;
  10621. }
  10622. auto last_token = get_token_string();
  10623. std::string message;
  10624. if (input_format != input_format_t::bjdata)
  10625. {
  10626. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  10627. }
  10628. else
  10629. {
  10630. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  10631. }
  10632. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  10633. }
  10634. /*!
  10635. @brief determine the type and size for a container
  10636. In the optimized UBJSON format, a type and a size can be provided to allow
  10637. for a more compact representation.
  10638. @param[out] result pair of the size and the type
  10639. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  10640. @return whether pair creation completed
  10641. */
  10642. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  10643. {
  10644. result.first = npos; // size
  10645. result.second = 0; // type
  10646. bool is_ndarray = false;
  10647. get_ignore_noop();
  10648. if (current == '$')
  10649. {
  10650. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  10651. if (input_format == input_format_t::bjdata
  10652. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  10653. {
  10654. auto last_token = get_token_string();
  10655. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10656. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  10657. }
  10658. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  10659. {
  10660. return false;
  10661. }
  10662. get_ignore_noop();
  10663. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  10664. {
  10665. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  10666. {
  10667. return false;
  10668. }
  10669. auto last_token = get_token_string();
  10670. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10671. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  10672. }
  10673. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  10674. if (input_format == input_format_t::bjdata && is_ndarray)
  10675. {
  10676. if (inside_ndarray)
  10677. {
  10678. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  10679. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  10680. }
  10681. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  10682. }
  10683. return is_error;
  10684. }
  10685. if (current == '#')
  10686. {
  10687. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  10688. if (input_format == input_format_t::bjdata && is_ndarray)
  10689. {
  10690. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  10691. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  10692. }
  10693. return is_error;
  10694. }
  10695. return true;
  10696. }
  10697. /*!
  10698. @param prefix the previously read or set type prefix
  10699. @return whether value creation completed
  10700. */
  10701. bool get_ubjson_value(const char_int_type prefix)
  10702. {
  10703. switch (prefix)
  10704. {
  10705. case char_traits<char_type>::eof(): // EOF
  10706. return unexpect_eof(input_format, "value");
  10707. case 'T': // true
  10708. return sax->boolean(true);
  10709. case 'F': // false
  10710. return sax->boolean(false);
  10711. case 'Z': // null
  10712. return sax->null();
  10713. case 'B': // byte
  10714. {
  10715. if (input_format != input_format_t::bjdata)
  10716. {
  10717. break;
  10718. }
  10719. std::uint8_t number{};
  10720. return get_number(input_format, number) && sax->number_unsigned(number);
  10721. }
  10722. case 'U':
  10723. {
  10724. std::uint8_t number{};
  10725. return get_number(input_format, number) && sax->number_unsigned(number);
  10726. }
  10727. case 'i':
  10728. {
  10729. std::int8_t number{};
  10730. return get_number(input_format, number) && sax->number_integer(number);
  10731. }
  10732. case 'I':
  10733. {
  10734. std::int16_t number{};
  10735. return get_number(input_format, number) && sax->number_integer(number);
  10736. }
  10737. case 'l':
  10738. {
  10739. std::int32_t number{};
  10740. return get_number(input_format, number) && sax->number_integer(number);
  10741. }
  10742. case 'L':
  10743. {
  10744. std::int64_t number{};
  10745. return get_number(input_format, number) && sax->number_integer(number);
  10746. }
  10747. case 'u':
  10748. {
  10749. if (input_format != input_format_t::bjdata)
  10750. {
  10751. break;
  10752. }
  10753. std::uint16_t number{};
  10754. return get_number(input_format, number) && sax->number_unsigned(number);
  10755. }
  10756. case 'm':
  10757. {
  10758. if (input_format != input_format_t::bjdata)
  10759. {
  10760. break;
  10761. }
  10762. std::uint32_t number{};
  10763. return get_number(input_format, number) && sax->number_unsigned(number);
  10764. }
  10765. case 'M':
  10766. {
  10767. if (input_format != input_format_t::bjdata)
  10768. {
  10769. break;
  10770. }
  10771. std::uint64_t number{};
  10772. return get_number(input_format, number) && sax->number_unsigned(number);
  10773. }
  10774. case 'h':
  10775. {
  10776. if (input_format != input_format_t::bjdata)
  10777. {
  10778. break;
  10779. }
  10780. const auto byte1_raw = get();
  10781. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10782. {
  10783. return false;
  10784. }
  10785. const auto byte2_raw = get();
  10786. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10787. {
  10788. return false;
  10789. }
  10790. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  10791. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10792. // Code from RFC 7049, Appendix D, Figure 3:
  10793. // As half-precision floating-point numbers were only added
  10794. // to IEEE 754 in 2008, today's programming platforms often
  10795. // still only have limited support for them. It is very
  10796. // easy to include at least decoding support for them even
  10797. // without such support. An example of a small decoder for
  10798. // half-precision floating-point numbers in the C language
  10799. // is shown in Fig. 3.
  10800. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10801. const double val = [&half]
  10802. {
  10803. const int exp = (half >> 10u) & 0x1Fu;
  10804. const unsigned int mant = half & 0x3FFu;
  10805. JSON_ASSERT(0 <= exp&& exp <= 32);
  10806. JSON_ASSERT(mant <= 1024);
  10807. switch (exp)
  10808. {
  10809. case 0:
  10810. return std::ldexp(mant, -24);
  10811. case 31:
  10812. return (mant == 0)
  10813. ? std::numeric_limits<double>::infinity()
  10814. : std::numeric_limits<double>::quiet_NaN();
  10815. default:
  10816. return std::ldexp(mant + 1024, exp - 25);
  10817. }
  10818. }();
  10819. return sax->number_float((half & 0x8000u) != 0
  10820. ? static_cast<number_float_t>(-val)
  10821. : static_cast<number_float_t>(val), "");
  10822. }
  10823. case 'd':
  10824. {
  10825. float number{};
  10826. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10827. }
  10828. case 'D':
  10829. {
  10830. double number{};
  10831. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10832. }
  10833. case 'H':
  10834. {
  10835. return get_ubjson_high_precision_number();
  10836. }
  10837. case 'C': // char
  10838. {
  10839. get();
  10840. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10841. {
  10842. return false;
  10843. }
  10844. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10845. {
  10846. auto last_token = get_token_string();
  10847. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10848. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10849. }
  10850. string_t s(1, static_cast<typename string_t::value_type>(current));
  10851. return sax->string(s);
  10852. }
  10853. case 'S': // string
  10854. {
  10855. string_t s;
  10856. return get_ubjson_string(s) && sax->string(s);
  10857. }
  10858. case '[': // array
  10859. return get_ubjson_array();
  10860. case '{': // object
  10861. return get_ubjson_object();
  10862. default: // anything else
  10863. break;
  10864. }
  10865. auto last_token = get_token_string();
  10866. 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));
  10867. }
  10868. /*!
  10869. @return whether array creation completed
  10870. */
  10871. bool get_ubjson_array()
  10872. {
  10873. std::pair<std::size_t, char_int_type> size_and_type;
  10874. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10875. {
  10876. return false;
  10877. }
  10878. // 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):
  10879. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10880. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10881. {
  10882. 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
  10883. 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)
  10884. {
  10885. return p.first < t;
  10886. });
  10887. string_t key = "_ArrayType_";
  10888. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10889. {
  10890. auto last_token = get_token_string();
  10891. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10892. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10893. }
  10894. string_t type = it->second; // sax->string() takes a reference
  10895. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10896. {
  10897. return false;
  10898. }
  10899. if (size_and_type.second == 'C' || size_and_type.second == 'B')
  10900. {
  10901. size_and_type.second = 'U';
  10902. }
  10903. key = "_ArrayData_";
  10904. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10905. {
  10906. return false;
  10907. }
  10908. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10909. {
  10910. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10911. {
  10912. return false;
  10913. }
  10914. }
  10915. return (sax->end_array() && sax->end_object());
  10916. }
  10917. // If BJData type marker is 'B' decode as binary
  10918. if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
  10919. {
  10920. binary_t result;
  10921. return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
  10922. }
  10923. if (size_and_type.first != npos)
  10924. {
  10925. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10926. {
  10927. return false;
  10928. }
  10929. if (size_and_type.second != 0)
  10930. {
  10931. if (size_and_type.second != 'N')
  10932. {
  10933. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10934. {
  10935. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10936. {
  10937. return false;
  10938. }
  10939. }
  10940. }
  10941. }
  10942. else
  10943. {
  10944. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10945. {
  10946. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10947. {
  10948. return false;
  10949. }
  10950. }
  10951. }
  10952. }
  10953. else
  10954. {
  10955. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
  10956. {
  10957. return false;
  10958. }
  10959. while (current != ']')
  10960. {
  10961. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10962. {
  10963. return false;
  10964. }
  10965. get_ignore_noop();
  10966. }
  10967. }
  10968. return sax->end_array();
  10969. }
  10970. /*!
  10971. @return whether object creation completed
  10972. */
  10973. bool get_ubjson_object()
  10974. {
  10975. std::pair<std::size_t, char_int_type> size_and_type;
  10976. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10977. {
  10978. return false;
  10979. }
  10980. // do not accept ND-array size in objects in BJData
  10981. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10982. {
  10983. auto last_token = get_token_string();
  10984. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10985. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10986. }
  10987. string_t key;
  10988. if (size_and_type.first != npos)
  10989. {
  10990. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10991. {
  10992. return false;
  10993. }
  10994. if (size_and_type.second != 0)
  10995. {
  10996. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10997. {
  10998. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10999. {
  11000. return false;
  11001. }
  11002. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  11003. {
  11004. return false;
  11005. }
  11006. key.clear();
  11007. }
  11008. }
  11009. else
  11010. {
  11011. for (std::size_t i = 0; i < size_and_type.first; ++i)
  11012. {
  11013. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  11014. {
  11015. return false;
  11016. }
  11017. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  11018. {
  11019. return false;
  11020. }
  11021. key.clear();
  11022. }
  11023. }
  11024. }
  11025. else
  11026. {
  11027. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
  11028. {
  11029. return false;
  11030. }
  11031. while (current != '}')
  11032. {
  11033. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  11034. {
  11035. return false;
  11036. }
  11037. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  11038. {
  11039. return false;
  11040. }
  11041. get_ignore_noop();
  11042. key.clear();
  11043. }
  11044. }
  11045. return sax->end_object();
  11046. }
  11047. // Note, no reader for UBJSON binary types is implemented because they do
  11048. // not exist
  11049. bool get_ubjson_high_precision_number()
  11050. {
  11051. // get the size of the following number string
  11052. std::size_t size{};
  11053. bool no_ndarray = true;
  11054. auto res = get_ubjson_size_value(size, no_ndarray);
  11055. if (JSON_HEDLEY_UNLIKELY(!res))
  11056. {
  11057. return res;
  11058. }
  11059. // get number string
  11060. std::vector<char> number_vector;
  11061. for (std::size_t i = 0; i < size; ++i)
  11062. {
  11063. get();
  11064. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  11065. {
  11066. return false;
  11067. }
  11068. number_vector.push_back(static_cast<char>(current));
  11069. }
  11070. // parse number string
  11071. using ia_type = decltype(detail::input_adapter(number_vector));
  11072. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  11073. const auto result_number = number_lexer.scan();
  11074. const auto number_string = number_lexer.get_token_string();
  11075. const auto result_remainder = number_lexer.scan();
  11076. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  11077. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  11078. {
  11079. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  11080. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  11081. }
  11082. switch (result_number)
  11083. {
  11084. case token_type::value_integer:
  11085. return sax->number_integer(number_lexer.get_number_integer());
  11086. case token_type::value_unsigned:
  11087. return sax->number_unsigned(number_lexer.get_number_unsigned());
  11088. case token_type::value_float:
  11089. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  11090. case token_type::uninitialized:
  11091. case token_type::literal_true:
  11092. case token_type::literal_false:
  11093. case token_type::literal_null:
  11094. case token_type::value_string:
  11095. case token_type::begin_array:
  11096. case token_type::begin_object:
  11097. case token_type::end_array:
  11098. case token_type::end_object:
  11099. case token_type::name_separator:
  11100. case token_type::value_separator:
  11101. case token_type::parse_error:
  11102. case token_type::end_of_input:
  11103. case token_type::literal_or_value:
  11104. default:
  11105. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  11106. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  11107. }
  11108. }
  11109. ///////////////////////
  11110. // Utility functions //
  11111. ///////////////////////
  11112. /*!
  11113. @brief get next character from the input
  11114. This function provides the interface to the used input adapter. It does
  11115. not throw in case the input reached EOF, but returns a -'ve valued
  11116. `char_traits<char_type>::eof()` in that case.
  11117. @return character read from the input
  11118. */
  11119. char_int_type get()
  11120. {
  11121. ++chars_read;
  11122. return current = ia.get_character();
  11123. }
  11124. /*!
  11125. @brief get_to read into a primitive type
  11126. This function provides the interface to the used input adapter. It does
  11127. not throw in case the input reached EOF, but returns false instead
  11128. @return bool, whether the read was successful
  11129. */
  11130. template<class T>
  11131. bool get_to(T& dest, const input_format_t format, const char* context)
  11132. {
  11133. auto new_chars_read = ia.get_elements(&dest);
  11134. chars_read += new_chars_read;
  11135. if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
  11136. {
  11137. // in case of failure, advance position by 1 to report the failing location
  11138. ++chars_read;
  11139. sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  11140. return false;
  11141. }
  11142. return true;
  11143. }
  11144. /*!
  11145. @return character read from the input after ignoring all 'N' entries
  11146. */
  11147. char_int_type get_ignore_noop()
  11148. {
  11149. do
  11150. {
  11151. get();
  11152. }
  11153. while (current == 'N');
  11154. return current;
  11155. }
  11156. template<class NumberType>
  11157. static void byte_swap(NumberType& number)
  11158. {
  11159. constexpr std::size_t sz = sizeof(number);
  11160. #ifdef __cpp_lib_byteswap
  11161. if constexpr (sz == 1)
  11162. {
  11163. return;
  11164. }
  11165. else if constexpr(std::is_integral_v<NumberType>)
  11166. {
  11167. number = std::byteswap(number);
  11168. return;
  11169. }
  11170. else
  11171. {
  11172. #endif
  11173. auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
  11174. for (std::size_t i = 0; i < sz / 2; ++i)
  11175. {
  11176. std::swap(ptr[i], ptr[sz - i - 1]);
  11177. }
  11178. #ifdef __cpp_lib_byteswap
  11179. }
  11180. #endif
  11181. }
  11182. /*
  11183. @brief read a number from the input
  11184. @tparam NumberType the type of the number
  11185. @param[in] format the current format (for diagnostics)
  11186. @param[out] result number of type @a NumberType
  11187. @return whether conversion completed
  11188. @note This function needs to respect the system's endianness, because
  11189. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  11190. (big endian) and therefore need reordering on little endian systems.
  11191. On the other hand, BSON and BJData use little endian and should reorder
  11192. on big endian systems.
  11193. */
  11194. template<typename NumberType, bool InputIsLittleEndian = false>
  11195. bool get_number(const input_format_t format, NumberType& result)
  11196. {
  11197. // read in the original format
  11198. if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
  11199. {
  11200. return false;
  11201. }
  11202. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  11203. {
  11204. byte_swap(result);
  11205. }
  11206. return true;
  11207. }
  11208. /*!
  11209. @brief create a string by reading characters from the input
  11210. @tparam NumberType the type of the number
  11211. @param[in] format the current format (for diagnostics)
  11212. @param[in] len number of characters to read
  11213. @param[out] result string created by reading @a len bytes
  11214. @return whether string creation completed
  11215. @note We can not reserve @a len bytes for the result, because @a len
  11216. may be too large. Usually, @ref unexpect_eof() detects the end of
  11217. the input before we run out of string memory.
  11218. */
  11219. template<typename NumberType>
  11220. bool get_string(const input_format_t format,
  11221. const NumberType len,
  11222. string_t& result)
  11223. {
  11224. bool success = true;
  11225. for (NumberType i = 0; i < len; i++)
  11226. {
  11227. get();
  11228. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  11229. {
  11230. success = false;
  11231. break;
  11232. }
  11233. result.push_back(static_cast<typename string_t::value_type>(current));
  11234. }
  11235. return success;
  11236. }
  11237. /*!
  11238. @brief create a byte array by reading bytes from the input
  11239. @tparam NumberType the type of the number
  11240. @param[in] format the current format (for diagnostics)
  11241. @param[in] len number of bytes to read
  11242. @param[out] result byte array created by reading @a len bytes
  11243. @return whether byte array creation completed
  11244. @note We can not reserve @a len bytes for the result, because @a len
  11245. may be too large. Usually, @ref unexpect_eof() detects the end of
  11246. the input before we run out of memory.
  11247. */
  11248. template<typename NumberType>
  11249. bool get_binary(const input_format_t format,
  11250. const NumberType len,
  11251. binary_t& result)
  11252. {
  11253. bool success = true;
  11254. for (NumberType i = 0; i < len; i++)
  11255. {
  11256. get();
  11257. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  11258. {
  11259. success = false;
  11260. break;
  11261. }
  11262. result.push_back(static_cast<typename binary_t::value_type>(current));
  11263. }
  11264. return success;
  11265. }
  11266. /*!
  11267. @param[in] format the current format (for diagnostics)
  11268. @param[in] context further context information (for diagnostics)
  11269. @return whether the last read character is not EOF
  11270. */
  11271. JSON_HEDLEY_NON_NULL(3)
  11272. bool unexpect_eof(const input_format_t format, const char* context) const
  11273. {
  11274. if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
  11275. {
  11276. return sax->parse_error(chars_read, "<end of file>",
  11277. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  11278. }
  11279. return true;
  11280. }
  11281. /*!
  11282. @return a string representation of the last read byte
  11283. */
  11284. std::string get_token_string() const
  11285. {
  11286. std::array<char, 3> cr{{}};
  11287. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  11288. return std::string{cr.data()};
  11289. }
  11290. /*!
  11291. @param[in] format the current format
  11292. @param[in] detail a detailed error message
  11293. @param[in] context further context information
  11294. @return a message string to use in the parse_error exceptions
  11295. */
  11296. std::string exception_message(const input_format_t format,
  11297. const std::string& detail,
  11298. const std::string& context) const
  11299. {
  11300. std::string error_msg = "syntax error while parsing ";
  11301. switch (format)
  11302. {
  11303. case input_format_t::cbor:
  11304. error_msg += "CBOR";
  11305. break;
  11306. case input_format_t::msgpack:
  11307. error_msg += "MessagePack";
  11308. break;
  11309. case input_format_t::ubjson:
  11310. error_msg += "UBJSON";
  11311. break;
  11312. case input_format_t::bson:
  11313. error_msg += "BSON";
  11314. break;
  11315. case input_format_t::bjdata:
  11316. error_msg += "BJData";
  11317. break;
  11318. case input_format_t::json: // LCOV_EXCL_LINE
  11319. default: // LCOV_EXCL_LINE
  11320. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  11321. }
  11322. return concat(error_msg, ' ', context, ": ", detail);
  11323. }
  11324. private:
  11325. static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
  11326. /// input adapter
  11327. InputAdapterType ia;
  11328. /// the current character
  11329. char_int_type current = char_traits<char_type>::eof();
  11330. /// the number of characters read
  11331. std::size_t chars_read = 0;
  11332. /// whether we can assume little endianness
  11333. const bool is_little_endian = little_endianness();
  11334. /// input format
  11335. const input_format_t input_format = input_format_t::json;
  11336. /// the SAX parser
  11337. json_sax_t* sax = nullptr;
  11338. // excluded markers in bjdata optimized type
  11339. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  11340. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  11341. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  11342. make_array<bjd_type>( \
  11343. bjd_type{'B', "byte"}, \
  11344. bjd_type{'C', "char"}, \
  11345. bjd_type{'D', "double"}, \
  11346. bjd_type{'I', "int16"}, \
  11347. bjd_type{'L', "int64"}, \
  11348. bjd_type{'M', "uint64"}, \
  11349. bjd_type{'U', "uint8"}, \
  11350. bjd_type{'d', "single"}, \
  11351. bjd_type{'i', "int8"}, \
  11352. bjd_type{'l', "int32"}, \
  11353. bjd_type{'m', "uint32"}, \
  11354. bjd_type{'u', "uint16"})
  11355. JSON_PRIVATE_UNLESS_TESTED:
  11356. // lookup tables
  11357. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  11358. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  11359. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  11360. using bjd_type = std::pair<char_int_type, string_t>;
  11361. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  11362. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  11363. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  11364. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  11365. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  11366. };
  11367. #ifndef JSON_HAS_CPP_17
  11368. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  11369. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  11370. #endif
  11371. } // namespace detail
  11372. NLOHMANN_JSON_NAMESPACE_END
  11373. // #include <nlohmann/detail/input/input_adapters.hpp>
  11374. // #include <nlohmann/detail/input/lexer.hpp>
  11375. // #include <nlohmann/detail/input/parser.hpp>
  11376. // __ _____ _____ _____
  11377. // __| | __| | | | JSON for Modern C++
  11378. // | | |__ | | | | | | version 3.12.0
  11379. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11380. //
  11381. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  11382. // SPDX-License-Identifier: MIT
  11383. #include <cmath> // isfinite
  11384. #include <cstdint> // uint8_t
  11385. #include <functional> // function
  11386. #include <string> // string
  11387. #include <utility> // move
  11388. #include <vector> // vector
  11389. // #include <nlohmann/detail/exceptions.hpp>
  11390. // #include <nlohmann/detail/input/input_adapters.hpp>
  11391. // #include <nlohmann/detail/input/json_sax.hpp>
  11392. // #include <nlohmann/detail/input/lexer.hpp>
  11393. // #include <nlohmann/detail/macro_scope.hpp>
  11394. // #include <nlohmann/detail/meta/is_sax.hpp>
  11395. // #include <nlohmann/detail/string_concat.hpp>
  11396. // #include <nlohmann/detail/value_t.hpp>
  11397. NLOHMANN_JSON_NAMESPACE_BEGIN
  11398. namespace detail
  11399. {
  11400. ////////////
  11401. // parser //
  11402. ////////////
  11403. enum class parse_event_t : std::uint8_t
  11404. {
  11405. /// the parser read `{` and started to process a JSON object
  11406. object_start,
  11407. /// the parser read `}` and finished processing a JSON object
  11408. object_end,
  11409. /// the parser read `[` and started to process a JSON array
  11410. array_start,
  11411. /// the parser read `]` and finished processing a JSON array
  11412. array_end,
  11413. /// the parser read a key of a value in an object
  11414. key,
  11415. /// the parser finished reading a JSON value
  11416. value
  11417. };
  11418. template<typename BasicJsonType>
  11419. using parser_callback_t =
  11420. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  11421. /*!
  11422. @brief syntax analysis
  11423. This class implements a recursive descent parser.
  11424. */
  11425. template<typename BasicJsonType, typename InputAdapterType>
  11426. class parser
  11427. {
  11428. using number_integer_t = typename BasicJsonType::number_integer_t;
  11429. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  11430. using number_float_t = typename BasicJsonType::number_float_t;
  11431. using string_t = typename BasicJsonType::string_t;
  11432. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  11433. using token_type = typename lexer_t::token_type;
  11434. public:
  11435. /// a parser reading from an input adapter
  11436. explicit parser(InputAdapterType&& adapter,
  11437. parser_callback_t<BasicJsonType> cb = nullptr,
  11438. const bool allow_exceptions_ = true,
  11439. const bool ignore_comments = false,
  11440. const bool ignore_trailing_commas_ = false)
  11441. : callback(std::move(cb))
  11442. , m_lexer(std::move(adapter), ignore_comments)
  11443. , allow_exceptions(allow_exceptions_)
  11444. , ignore_trailing_commas(ignore_trailing_commas_)
  11445. {
  11446. // read first token
  11447. get_token();
  11448. }
  11449. /*!
  11450. @brief public parser interface
  11451. @param[in] strict whether to expect the last token to be EOF
  11452. @param[in,out] result parsed JSON value
  11453. @throw parse_error.101 in case of an unexpected token
  11454. @throw parse_error.102 if to_unicode fails or surrogate error
  11455. @throw parse_error.103 if to_unicode fails
  11456. */
  11457. void parse(const bool strict, BasicJsonType& result)
  11458. {
  11459. if (callback)
  11460. {
  11461. json_sax_dom_callback_parser<BasicJsonType, InputAdapterType> sdp(result, callback, allow_exceptions, &m_lexer);
  11462. sax_parse_internal(&sdp);
  11463. // in strict mode, input must be completely read
  11464. if (strict && (get_token() != token_type::end_of_input))
  11465. {
  11466. sdp.parse_error(m_lexer.get_position(),
  11467. m_lexer.get_token_string(),
  11468. parse_error::create(101, m_lexer.get_position(),
  11469. exception_message(token_type::end_of_input, "value"), nullptr));
  11470. }
  11471. // in case of an error, return a discarded value
  11472. if (sdp.is_errored())
  11473. {
  11474. result = value_t::discarded;
  11475. return;
  11476. }
  11477. // set top-level value to null if it was discarded by the callback
  11478. // function
  11479. if (result.is_discarded())
  11480. {
  11481. result = nullptr;
  11482. }
  11483. }
  11484. else
  11485. {
  11486. json_sax_dom_parser<BasicJsonType, InputAdapterType> sdp(result, allow_exceptions, &m_lexer);
  11487. sax_parse_internal(&sdp);
  11488. // in strict mode, input must be completely read
  11489. if (strict && (get_token() != token_type::end_of_input))
  11490. {
  11491. sdp.parse_error(m_lexer.get_position(),
  11492. m_lexer.get_token_string(),
  11493. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  11494. }
  11495. // in case of an error, return a discarded value
  11496. if (sdp.is_errored())
  11497. {
  11498. result = value_t::discarded;
  11499. return;
  11500. }
  11501. }
  11502. result.assert_invariant();
  11503. }
  11504. /*!
  11505. @brief public accept interface
  11506. @param[in] strict whether to expect the last token to be EOF
  11507. @return whether the input is a proper JSON text
  11508. */
  11509. bool accept(const bool strict = true)
  11510. {
  11511. json_sax_acceptor<BasicJsonType> sax_acceptor;
  11512. return sax_parse(&sax_acceptor, strict);
  11513. }
  11514. template<typename SAX>
  11515. JSON_HEDLEY_NON_NULL(2)
  11516. bool sax_parse(SAX* sax, const bool strict = true)
  11517. {
  11518. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  11519. const bool result = sax_parse_internal(sax);
  11520. // strict mode: next byte must be EOF
  11521. if (result && strict && (get_token() != token_type::end_of_input))
  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(), exception_message(token_type::end_of_input, "value"), nullptr));
  11526. }
  11527. return result;
  11528. }
  11529. private:
  11530. template<typename SAX>
  11531. JSON_HEDLEY_NON_NULL(2)
  11532. bool sax_parse_internal(SAX* sax)
  11533. {
  11534. // stack to remember the hierarchy of structured values we are parsing
  11535. // true = array; false = object
  11536. std::vector<bool> states;
  11537. // value to avoid a goto (see comment where set to true)
  11538. bool skip_to_state_evaluation = false;
  11539. while (true)
  11540. {
  11541. if (!skip_to_state_evaluation)
  11542. {
  11543. // invariant: get_token() was called before each iteration
  11544. switch (last_token)
  11545. {
  11546. case token_type::begin_object:
  11547. {
  11548. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
  11549. {
  11550. return false;
  11551. }
  11552. // closing } -> we are done
  11553. if (get_token() == token_type::end_object)
  11554. {
  11555. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  11556. {
  11557. return false;
  11558. }
  11559. break;
  11560. }
  11561. // parse key
  11562. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  11563. {
  11564. return sax->parse_error(m_lexer.get_position(),
  11565. m_lexer.get_token_string(),
  11566. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  11567. }
  11568. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  11569. {
  11570. return false;
  11571. }
  11572. // parse separator (:)
  11573. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  11574. {
  11575. return sax->parse_error(m_lexer.get_position(),
  11576. m_lexer.get_token_string(),
  11577. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  11578. }
  11579. // remember we are now inside an object
  11580. states.push_back(false);
  11581. // parse values
  11582. get_token();
  11583. continue;
  11584. }
  11585. case token_type::begin_array:
  11586. {
  11587. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
  11588. {
  11589. return false;
  11590. }
  11591. // closing ] -> we are done
  11592. if (get_token() == token_type::end_array)
  11593. {
  11594. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  11595. {
  11596. return false;
  11597. }
  11598. break;
  11599. }
  11600. // remember we are now inside an array
  11601. states.push_back(true);
  11602. // parse values (no need to call get_token)
  11603. continue;
  11604. }
  11605. case token_type::value_float:
  11606. {
  11607. const auto res = m_lexer.get_number_float();
  11608. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  11609. {
  11610. return sax->parse_error(m_lexer.get_position(),
  11611. m_lexer.get_token_string(),
  11612. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  11613. }
  11614. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  11615. {
  11616. return false;
  11617. }
  11618. break;
  11619. }
  11620. case token_type::literal_false:
  11621. {
  11622. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  11623. {
  11624. return false;
  11625. }
  11626. break;
  11627. }
  11628. case token_type::literal_null:
  11629. {
  11630. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  11631. {
  11632. return false;
  11633. }
  11634. break;
  11635. }
  11636. case token_type::literal_true:
  11637. {
  11638. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  11639. {
  11640. return false;
  11641. }
  11642. break;
  11643. }
  11644. case token_type::value_integer:
  11645. {
  11646. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  11647. {
  11648. return false;
  11649. }
  11650. break;
  11651. }
  11652. case token_type::value_string:
  11653. {
  11654. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  11655. {
  11656. return false;
  11657. }
  11658. break;
  11659. }
  11660. case token_type::value_unsigned:
  11661. {
  11662. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  11663. {
  11664. return false;
  11665. }
  11666. break;
  11667. }
  11668. case token_type::parse_error:
  11669. {
  11670. // using "uninitialized" to avoid an "expected" message
  11671. return sax->parse_error(m_lexer.get_position(),
  11672. m_lexer.get_token_string(),
  11673. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  11674. }
  11675. case token_type::end_of_input:
  11676. {
  11677. if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
  11678. {
  11679. return sax->parse_error(m_lexer.get_position(),
  11680. m_lexer.get_token_string(),
  11681. parse_error::create(101, m_lexer.get_position(),
  11682. "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
  11683. }
  11684. return sax->parse_error(m_lexer.get_position(),
  11685. m_lexer.get_token_string(),
  11686. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  11687. }
  11688. case token_type::uninitialized:
  11689. case token_type::end_array:
  11690. case token_type::end_object:
  11691. case token_type::name_separator:
  11692. case token_type::value_separator:
  11693. case token_type::literal_or_value:
  11694. default: // the last token was unexpected
  11695. {
  11696. return sax->parse_error(m_lexer.get_position(),
  11697. m_lexer.get_token_string(),
  11698. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  11699. }
  11700. }
  11701. }
  11702. else
  11703. {
  11704. skip_to_state_evaluation = false;
  11705. }
  11706. // we reached this line after we successfully parsed a value
  11707. if (states.empty())
  11708. {
  11709. // empty stack: we reached the end of the hierarchy: done
  11710. return true;
  11711. }
  11712. if (states.back()) // array
  11713. {
  11714. // comma -> next value
  11715. // or end of array (ignore_trailing_commas = true)
  11716. if (get_token() == token_type::value_separator)
  11717. {
  11718. // parse a new value
  11719. get_token();
  11720. // if ignore_trailing_commas and last_token is ], we can continue to "closing ]"
  11721. if (!(ignore_trailing_commas && last_token == token_type::end_array))
  11722. {
  11723. continue;
  11724. }
  11725. }
  11726. // closing ]
  11727. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  11728. {
  11729. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  11730. {
  11731. return false;
  11732. }
  11733. // We are done with this array. Before we can parse a
  11734. // new value, we need to evaluate the new state first.
  11735. // By setting skip_to_state_evaluation to false, we
  11736. // are effectively jumping to the beginning of this if.
  11737. JSON_ASSERT(!states.empty());
  11738. states.pop_back();
  11739. skip_to_state_evaluation = true;
  11740. continue;
  11741. }
  11742. return sax->parse_error(m_lexer.get_position(),
  11743. m_lexer.get_token_string(),
  11744. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  11745. }
  11746. // states.back() is false -> object
  11747. // comma -> next value
  11748. // or end of object (ignore_trailing_commas = true)
  11749. if (get_token() == token_type::value_separator)
  11750. {
  11751. get_token();
  11752. // if ignore_trailing_commas and last_token is }, we can continue to "closing }"
  11753. if (!(ignore_trailing_commas && last_token == token_type::end_object))
  11754. {
  11755. // parse key
  11756. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  11757. {
  11758. return sax->parse_error(m_lexer.get_position(),
  11759. m_lexer.get_token_string(),
  11760. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  11761. }
  11762. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  11763. {
  11764. return false;
  11765. }
  11766. // parse separator (:)
  11767. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  11768. {
  11769. return sax->parse_error(m_lexer.get_position(),
  11770. m_lexer.get_token_string(),
  11771. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  11772. }
  11773. // parse values
  11774. get_token();
  11775. continue;
  11776. }
  11777. }
  11778. // closing }
  11779. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  11780. {
  11781. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  11782. {
  11783. return false;
  11784. }
  11785. // We are done with this object. Before we can parse a
  11786. // new value, we need to evaluate the new state first.
  11787. // By setting skip_to_state_evaluation to false, we
  11788. // are effectively jumping to the beginning of this if.
  11789. JSON_ASSERT(!states.empty());
  11790. states.pop_back();
  11791. skip_to_state_evaluation = true;
  11792. continue;
  11793. }
  11794. return sax->parse_error(m_lexer.get_position(),
  11795. m_lexer.get_token_string(),
  11796. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  11797. }
  11798. }
  11799. /// get next token from lexer
  11800. token_type get_token()
  11801. {
  11802. return last_token = m_lexer.scan();
  11803. }
  11804. std::string exception_message(const token_type expected, const std::string& context)
  11805. {
  11806. std::string error_msg = "syntax error ";
  11807. if (!context.empty())
  11808. {
  11809. error_msg += concat("while parsing ", context, ' ');
  11810. }
  11811. error_msg += "- ";
  11812. if (last_token == token_type::parse_error)
  11813. {
  11814. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  11815. m_lexer.get_token_string(), '\'');
  11816. }
  11817. else
  11818. {
  11819. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  11820. }
  11821. if (expected != token_type::uninitialized)
  11822. {
  11823. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  11824. }
  11825. return error_msg;
  11826. }
  11827. private:
  11828. /// callback function
  11829. const parser_callback_t<BasicJsonType> callback = nullptr;
  11830. /// the type of the last read token
  11831. token_type last_token = token_type::uninitialized;
  11832. /// the lexer
  11833. lexer_t m_lexer;
  11834. /// whether to throw exceptions in case of errors
  11835. const bool allow_exceptions = true;
  11836. /// whether trailing commas in objects and arrays should be ignored (true) or signaled as errors (false)
  11837. const bool ignore_trailing_commas = false;
  11838. };
  11839. } // namespace detail
  11840. NLOHMANN_JSON_NAMESPACE_END
  11841. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11842. // __ _____ _____ _____
  11843. // __| | __| | | | JSON for Modern C++
  11844. // | | |__ | | | | | | version 3.12.0
  11845. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11846. //
  11847. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  11848. // SPDX-License-Identifier: MIT
  11849. // #include <nlohmann/detail/abi_macros.hpp>
  11850. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11851. // __ _____ _____ _____
  11852. // __| | __| | | | JSON for Modern C++
  11853. // | | |__ | | | | | | version 3.12.0
  11854. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11855. //
  11856. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  11857. // SPDX-License-Identifier: MIT
  11858. #include <cstddef> // ptrdiff_t
  11859. #include <limits> // numeric_limits
  11860. // #include <nlohmann/detail/macro_scope.hpp>
  11861. NLOHMANN_JSON_NAMESPACE_BEGIN
  11862. namespace detail
  11863. {
  11864. /*
  11865. @brief an iterator for primitive JSON types
  11866. This class models an iterator for primitive JSON types (boolean, number,
  11867. string). Its only purpose is to allow the iterator/const_iterator classes
  11868. to "iterate" over primitive values. Internally, the iterator is modeled by
  11869. a `difference_type` variable. Value begin_value (`0`) models the begin and
  11870. end_value (`1`) models past the end.
  11871. */
  11872. class primitive_iterator_t
  11873. {
  11874. private:
  11875. using difference_type = std::ptrdiff_t;
  11876. static constexpr difference_type begin_value = 0;
  11877. static constexpr difference_type end_value = begin_value + 1;
  11878. JSON_PRIVATE_UNLESS_TESTED:
  11879. /// iterator as signed integer type
  11880. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11881. public:
  11882. constexpr difference_type get_value() const noexcept
  11883. {
  11884. return m_it;
  11885. }
  11886. /// set iterator to a defined beginning
  11887. void set_begin() noexcept
  11888. {
  11889. m_it = begin_value;
  11890. }
  11891. /// set iterator to a defined past the end
  11892. void set_end() noexcept
  11893. {
  11894. m_it = end_value;
  11895. }
  11896. /// return whether the iterator can be dereferenced
  11897. constexpr bool is_begin() const noexcept
  11898. {
  11899. return m_it == begin_value;
  11900. }
  11901. /// return whether the iterator is at end
  11902. constexpr bool is_end() const noexcept
  11903. {
  11904. return m_it == end_value;
  11905. }
  11906. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11907. {
  11908. return lhs.m_it == rhs.m_it;
  11909. }
  11910. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11911. {
  11912. return lhs.m_it < rhs.m_it;
  11913. }
  11914. primitive_iterator_t operator+(difference_type n) noexcept
  11915. {
  11916. auto result = *this;
  11917. result += n;
  11918. return result;
  11919. }
  11920. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11921. {
  11922. return lhs.m_it - rhs.m_it;
  11923. }
  11924. primitive_iterator_t& operator++() noexcept
  11925. {
  11926. ++m_it;
  11927. return *this;
  11928. }
  11929. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11930. {
  11931. auto result = *this;
  11932. ++m_it;
  11933. return result;
  11934. }
  11935. primitive_iterator_t& operator--() noexcept
  11936. {
  11937. --m_it;
  11938. return *this;
  11939. }
  11940. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11941. {
  11942. auto result = *this;
  11943. --m_it;
  11944. return result;
  11945. }
  11946. primitive_iterator_t& operator+=(difference_type n) noexcept
  11947. {
  11948. m_it += n;
  11949. return *this;
  11950. }
  11951. primitive_iterator_t& operator-=(difference_type n) noexcept
  11952. {
  11953. m_it -= n;
  11954. return *this;
  11955. }
  11956. };
  11957. } // namespace detail
  11958. NLOHMANN_JSON_NAMESPACE_END
  11959. NLOHMANN_JSON_NAMESPACE_BEGIN
  11960. namespace detail
  11961. {
  11962. /*!
  11963. @brief an iterator value
  11964. @note This structure could easily be a union, but MSVC currently does not allow
  11965. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11966. */
  11967. template<typename BasicJsonType> struct internal_iterator
  11968. {
  11969. /// iterator for JSON objects
  11970. typename BasicJsonType::object_t::iterator object_iterator {};
  11971. /// iterator for JSON arrays
  11972. typename BasicJsonType::array_t::iterator array_iterator {};
  11973. /// generic iterator for all other types
  11974. primitive_iterator_t primitive_iterator {};
  11975. };
  11976. } // namespace detail
  11977. NLOHMANN_JSON_NAMESPACE_END
  11978. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11979. // __ _____ _____ _____
  11980. // __| | __| | | | JSON for Modern C++
  11981. // | | |__ | | | | | | version 3.12.0
  11982. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11983. //
  11984. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  11985. // SPDX-License-Identifier: MIT
  11986. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11987. #include <type_traits> // conditional, is_const, remove_const
  11988. // #include <nlohmann/detail/exceptions.hpp>
  11989. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11990. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11991. // #include <nlohmann/detail/macro_scope.hpp>
  11992. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11993. // #include <nlohmann/detail/meta/type_traits.hpp>
  11994. // #include <nlohmann/detail/value_t.hpp>
  11995. NLOHMANN_JSON_NAMESPACE_BEGIN
  11996. namespace detail
  11997. {
  11998. // forward declare to be able to friend it later on
  11999. template<typename IteratorType> class iteration_proxy;
  12000. template<typename IteratorType> class iteration_proxy_value;
  12001. /*!
  12002. @brief a template for a bidirectional iterator for the @ref basic_json class
  12003. This class implements a both iterators (iterator and const_iterator) for the
  12004. @ref basic_json class.
  12005. @note An iterator is called *initialized* when a pointer to a JSON value has
  12006. been set (e.g., by a constructor or a copy assignment). If the iterator is
  12007. default-constructed, it is *uninitialized* and most methods are undefined.
  12008. **The library uses assertions to detect calls on uninitialized iterators.**
  12009. @requirement The class satisfies the following concept requirements:
  12010. -
  12011. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  12012. The iterator that can be moved can be moved in both directions (i.e.
  12013. incremented and decremented).
  12014. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  12015. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  12016. */
  12017. template<typename BasicJsonType>
  12018. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  12019. {
  12020. /// the iterator with BasicJsonType of different const-ness
  12021. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  12022. /// allow basic_json to access private members
  12023. friend other_iter_impl;
  12024. friend BasicJsonType;
  12025. friend iteration_proxy<iter_impl>;
  12026. friend iteration_proxy_value<iter_impl>;
  12027. using object_t = typename BasicJsonType::object_t;
  12028. using array_t = typename BasicJsonType::array_t;
  12029. // make sure BasicJsonType is basic_json or const basic_json
  12030. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  12031. "iter_impl only accepts (const) basic_json");
  12032. // superficial check for the LegacyBidirectionalIterator named requirement
  12033. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  12034. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  12035. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  12036. public:
  12037. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  12038. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  12039. /// A user-defined iterator should provide publicly accessible typedefs named
  12040. /// iterator_category, value_type, difference_type, pointer, and reference.
  12041. /// Note that value_type is required to be non-const, even for constant iterators.
  12042. using iterator_category = std::bidirectional_iterator_tag;
  12043. /// the type of the values when the iterator is dereferenced
  12044. using value_type = typename BasicJsonType::value_type;
  12045. /// a type to represent differences between iterators
  12046. using difference_type = typename BasicJsonType::difference_type;
  12047. /// defines a pointer to the type iterated over (value_type)
  12048. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  12049. typename BasicJsonType::const_pointer,
  12050. typename BasicJsonType::pointer>::type;
  12051. /// defines a reference to the type iterated over (value_type)
  12052. using reference =
  12053. typename std::conditional<std::is_const<BasicJsonType>::value,
  12054. typename BasicJsonType::const_reference,
  12055. typename BasicJsonType::reference>::type;
  12056. iter_impl() = default;
  12057. ~iter_impl() = default;
  12058. iter_impl(iter_impl&&) noexcept = default;
  12059. iter_impl& operator=(iter_impl&&) noexcept = default;
  12060. /*!
  12061. @brief constructor for a given JSON instance
  12062. @param[in] object pointer to a JSON object for this iterator
  12063. @pre object != nullptr
  12064. @post The iterator is initialized; i.e. `m_object != nullptr`.
  12065. */
  12066. explicit iter_impl(pointer object) noexcept : m_object(object)
  12067. {
  12068. JSON_ASSERT(m_object != nullptr);
  12069. switch (m_object->m_data.m_type)
  12070. {
  12071. case value_t::object:
  12072. {
  12073. m_it.object_iterator = typename object_t::iterator();
  12074. break;
  12075. }
  12076. case value_t::array:
  12077. {
  12078. m_it.array_iterator = typename array_t::iterator();
  12079. break;
  12080. }
  12081. case value_t::null:
  12082. case value_t::string:
  12083. case value_t::boolean:
  12084. case value_t::number_integer:
  12085. case value_t::number_unsigned:
  12086. case value_t::number_float:
  12087. case value_t::binary:
  12088. case value_t::discarded:
  12089. default:
  12090. {
  12091. m_it.primitive_iterator = primitive_iterator_t();
  12092. break;
  12093. }
  12094. }
  12095. }
  12096. /*!
  12097. @note The conventional copy constructor and copy assignment are implicitly
  12098. defined. Combined with the following converting constructor and
  12099. assignment, they support: (1) copy from iterator to iterator, (2)
  12100. copy from const iterator to const iterator, and (3) conversion from
  12101. iterator to const iterator. However conversion from const iterator
  12102. to iterator is not defined.
  12103. */
  12104. /*!
  12105. @brief const copy constructor
  12106. @param[in] other const iterator to copy from
  12107. @note This copy constructor had to be defined explicitly to circumvent a bug
  12108. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  12109. information refer to: https://github.com/nlohmann/json/issues/1608
  12110. */
  12111. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  12112. : m_object(other.m_object), m_it(other.m_it)
  12113. {}
  12114. /*!
  12115. @brief converting assignment
  12116. @param[in] other const iterator to copy from
  12117. @return const/non-const iterator
  12118. @note It is not checked whether @a other is initialized.
  12119. */
  12120. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  12121. {
  12122. if (&other != this)
  12123. {
  12124. m_object = other.m_object;
  12125. m_it = other.m_it;
  12126. }
  12127. return *this;
  12128. }
  12129. /*!
  12130. @brief converting constructor
  12131. @param[in] other non-const iterator to copy from
  12132. @note It is not checked whether @a other is initialized.
  12133. */
  12134. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  12135. : m_object(other.m_object), m_it(other.m_it)
  12136. {}
  12137. /*!
  12138. @brief converting assignment
  12139. @param[in] other non-const iterator to copy from
  12140. @return const/non-const iterator
  12141. @note It is not checked whether @a other is initialized.
  12142. */
  12143. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  12144. {
  12145. m_object = other.m_object;
  12146. m_it = other.m_it;
  12147. return *this;
  12148. }
  12149. JSON_PRIVATE_UNLESS_TESTED:
  12150. /*!
  12151. @brief set the iterator to the first value
  12152. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12153. */
  12154. void set_begin() noexcept
  12155. {
  12156. JSON_ASSERT(m_object != nullptr);
  12157. switch (m_object->m_data.m_type)
  12158. {
  12159. case value_t::object:
  12160. {
  12161. m_it.object_iterator = m_object->m_data.m_value.object->begin();
  12162. break;
  12163. }
  12164. case value_t::array:
  12165. {
  12166. m_it.array_iterator = m_object->m_data.m_value.array->begin();
  12167. break;
  12168. }
  12169. case value_t::null:
  12170. {
  12171. // set to end so begin()==end() is true: null is empty
  12172. m_it.primitive_iterator.set_end();
  12173. break;
  12174. }
  12175. case value_t::string:
  12176. case value_t::boolean:
  12177. case value_t::number_integer:
  12178. case value_t::number_unsigned:
  12179. case value_t::number_float:
  12180. case value_t::binary:
  12181. case value_t::discarded:
  12182. default:
  12183. {
  12184. m_it.primitive_iterator.set_begin();
  12185. break;
  12186. }
  12187. }
  12188. }
  12189. /*!
  12190. @brief set the iterator past the last value
  12191. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12192. */
  12193. void set_end() noexcept
  12194. {
  12195. JSON_ASSERT(m_object != nullptr);
  12196. switch (m_object->m_data.m_type)
  12197. {
  12198. case value_t::object:
  12199. {
  12200. m_it.object_iterator = m_object->m_data.m_value.object->end();
  12201. break;
  12202. }
  12203. case value_t::array:
  12204. {
  12205. m_it.array_iterator = m_object->m_data.m_value.array->end();
  12206. break;
  12207. }
  12208. case value_t::null:
  12209. case value_t::string:
  12210. case value_t::boolean:
  12211. case value_t::number_integer:
  12212. case value_t::number_unsigned:
  12213. case value_t::number_float:
  12214. case value_t::binary:
  12215. case value_t::discarded:
  12216. default:
  12217. {
  12218. m_it.primitive_iterator.set_end();
  12219. break;
  12220. }
  12221. }
  12222. }
  12223. public:
  12224. /*!
  12225. @brief return a reference to the value pointed to by the iterator
  12226. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12227. */
  12228. reference operator*() const
  12229. {
  12230. JSON_ASSERT(m_object != nullptr);
  12231. switch (m_object->m_data.m_type)
  12232. {
  12233. case value_t::object:
  12234. {
  12235. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  12236. return m_it.object_iterator->second;
  12237. }
  12238. case value_t::array:
  12239. {
  12240. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  12241. return *m_it.array_iterator;
  12242. }
  12243. case value_t::null:
  12244. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12245. case value_t::string:
  12246. case value_t::boolean:
  12247. case value_t::number_integer:
  12248. case value_t::number_unsigned:
  12249. case value_t::number_float:
  12250. case value_t::binary:
  12251. case value_t::discarded:
  12252. default:
  12253. {
  12254. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  12255. {
  12256. return *m_object;
  12257. }
  12258. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12259. }
  12260. }
  12261. }
  12262. /*!
  12263. @brief dereference the iterator
  12264. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12265. */
  12266. pointer operator->() const
  12267. {
  12268. JSON_ASSERT(m_object != nullptr);
  12269. switch (m_object->m_data.m_type)
  12270. {
  12271. case value_t::object:
  12272. {
  12273. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  12274. return &(m_it.object_iterator->second);
  12275. }
  12276. case value_t::array:
  12277. {
  12278. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  12279. return &*m_it.array_iterator;
  12280. }
  12281. case value_t::null:
  12282. case value_t::string:
  12283. case value_t::boolean:
  12284. case value_t::number_integer:
  12285. case value_t::number_unsigned:
  12286. case value_t::number_float:
  12287. case value_t::binary:
  12288. case value_t::discarded:
  12289. default:
  12290. {
  12291. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  12292. {
  12293. return m_object;
  12294. }
  12295. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12296. }
  12297. }
  12298. }
  12299. /*!
  12300. @brief post-increment (it++)
  12301. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12302. */
  12303. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  12304. {
  12305. auto result = *this;
  12306. ++(*this);
  12307. return result;
  12308. }
  12309. /*!
  12310. @brief pre-increment (++it)
  12311. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12312. */
  12313. iter_impl& operator++()
  12314. {
  12315. JSON_ASSERT(m_object != nullptr);
  12316. switch (m_object->m_data.m_type)
  12317. {
  12318. case value_t::object:
  12319. {
  12320. std::advance(m_it.object_iterator, 1);
  12321. break;
  12322. }
  12323. case value_t::array:
  12324. {
  12325. std::advance(m_it.array_iterator, 1);
  12326. break;
  12327. }
  12328. case value_t::null:
  12329. case value_t::string:
  12330. case value_t::boolean:
  12331. case value_t::number_integer:
  12332. case value_t::number_unsigned:
  12333. case value_t::number_float:
  12334. case value_t::binary:
  12335. case value_t::discarded:
  12336. default:
  12337. {
  12338. ++m_it.primitive_iterator;
  12339. break;
  12340. }
  12341. }
  12342. return *this;
  12343. }
  12344. /*!
  12345. @brief post-decrement (it--)
  12346. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12347. */
  12348. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  12349. {
  12350. auto result = *this;
  12351. --(*this);
  12352. return result;
  12353. }
  12354. /*!
  12355. @brief pre-decrement (--it)
  12356. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12357. */
  12358. iter_impl& operator--()
  12359. {
  12360. JSON_ASSERT(m_object != nullptr);
  12361. switch (m_object->m_data.m_type)
  12362. {
  12363. case value_t::object:
  12364. {
  12365. std::advance(m_it.object_iterator, -1);
  12366. break;
  12367. }
  12368. case value_t::array:
  12369. {
  12370. std::advance(m_it.array_iterator, -1);
  12371. break;
  12372. }
  12373. case value_t::null:
  12374. case value_t::string:
  12375. case value_t::boolean:
  12376. case value_t::number_integer:
  12377. case value_t::number_unsigned:
  12378. case value_t::number_float:
  12379. case value_t::binary:
  12380. case value_t::discarded:
  12381. default:
  12382. {
  12383. --m_it.primitive_iterator;
  12384. break;
  12385. }
  12386. }
  12387. return *this;
  12388. }
  12389. /*!
  12390. @brief comparison: equal
  12391. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12392. */
  12393. 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 >
  12394. bool operator==(const IterImpl& other) const
  12395. {
  12396. // if objects are not the same, the comparison is undefined
  12397. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  12398. {
  12399. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  12400. }
  12401. // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
  12402. if (m_object == nullptr)
  12403. {
  12404. return true;
  12405. }
  12406. switch (m_object->m_data.m_type)
  12407. {
  12408. case value_t::object:
  12409. return (m_it.object_iterator == other.m_it.object_iterator);
  12410. case value_t::array:
  12411. return (m_it.array_iterator == other.m_it.array_iterator);
  12412. case value_t::null:
  12413. case value_t::string:
  12414. case value_t::boolean:
  12415. case value_t::number_integer:
  12416. case value_t::number_unsigned:
  12417. case value_t::number_float:
  12418. case value_t::binary:
  12419. case value_t::discarded:
  12420. default:
  12421. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  12422. }
  12423. }
  12424. /*!
  12425. @brief comparison: not equal
  12426. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12427. */
  12428. 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 >
  12429. bool operator!=(const IterImpl& other) const
  12430. {
  12431. return !operator==(other);
  12432. }
  12433. /*!
  12434. @brief comparison: smaller
  12435. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12436. */
  12437. bool operator<(const iter_impl& other) const
  12438. {
  12439. // if objects are not the same, the comparison is undefined
  12440. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  12441. {
  12442. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  12443. }
  12444. // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
  12445. if (m_object == nullptr)
  12446. {
  12447. // the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
  12448. return false;
  12449. }
  12450. switch (m_object->m_data.m_type)
  12451. {
  12452. case value_t::object:
  12453. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  12454. case value_t::array:
  12455. return (m_it.array_iterator < other.m_it.array_iterator);
  12456. case value_t::null:
  12457. case value_t::string:
  12458. case value_t::boolean:
  12459. case value_t::number_integer:
  12460. case value_t::number_unsigned:
  12461. case value_t::number_float:
  12462. case value_t::binary:
  12463. case value_t::discarded:
  12464. default:
  12465. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  12466. }
  12467. }
  12468. /*!
  12469. @brief comparison: less than or equal
  12470. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12471. */
  12472. bool operator<=(const iter_impl& other) const
  12473. {
  12474. return !other.operator < (*this);
  12475. }
  12476. /*!
  12477. @brief comparison: greater than
  12478. @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
  12479. */
  12480. bool operator>(const iter_impl& other) const
  12481. {
  12482. return !operator<=(other);
  12483. }
  12484. /*!
  12485. @brief comparison: greater than or equal
  12486. @pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
  12487. */
  12488. bool operator>=(const iter_impl& other) const
  12489. {
  12490. return !operator<(other);
  12491. }
  12492. /*!
  12493. @brief add to iterator
  12494. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12495. */
  12496. iter_impl& operator+=(difference_type i)
  12497. {
  12498. JSON_ASSERT(m_object != nullptr);
  12499. switch (m_object->m_data.m_type)
  12500. {
  12501. case value_t::object:
  12502. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  12503. case value_t::array:
  12504. {
  12505. std::advance(m_it.array_iterator, i);
  12506. break;
  12507. }
  12508. case value_t::null:
  12509. case value_t::string:
  12510. case value_t::boolean:
  12511. case value_t::number_integer:
  12512. case value_t::number_unsigned:
  12513. case value_t::number_float:
  12514. case value_t::binary:
  12515. case value_t::discarded:
  12516. default:
  12517. {
  12518. m_it.primitive_iterator += i;
  12519. break;
  12520. }
  12521. }
  12522. return *this;
  12523. }
  12524. /*!
  12525. @brief subtract from iterator
  12526. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12527. */
  12528. iter_impl& operator-=(difference_type i)
  12529. {
  12530. return operator+=(-i);
  12531. }
  12532. /*!
  12533. @brief add to iterator
  12534. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12535. */
  12536. iter_impl operator+(difference_type i) const
  12537. {
  12538. auto result = *this;
  12539. result += i;
  12540. return result;
  12541. }
  12542. /*!
  12543. @brief addition of distance and iterator
  12544. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12545. */
  12546. friend iter_impl operator+(difference_type i, const iter_impl& it)
  12547. {
  12548. auto result = it;
  12549. result += i;
  12550. return result;
  12551. }
  12552. /*!
  12553. @brief subtract from iterator
  12554. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12555. */
  12556. iter_impl operator-(difference_type i) const
  12557. {
  12558. auto result = *this;
  12559. result -= i;
  12560. return result;
  12561. }
  12562. /*!
  12563. @brief return difference
  12564. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12565. */
  12566. difference_type operator-(const iter_impl& other) const
  12567. {
  12568. JSON_ASSERT(m_object != nullptr);
  12569. switch (m_object->m_data.m_type)
  12570. {
  12571. case value_t::object:
  12572. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  12573. case value_t::array:
  12574. return m_it.array_iterator - other.m_it.array_iterator;
  12575. case value_t::null:
  12576. case value_t::string:
  12577. case value_t::boolean:
  12578. case value_t::number_integer:
  12579. case value_t::number_unsigned:
  12580. case value_t::number_float:
  12581. case value_t::binary:
  12582. case value_t::discarded:
  12583. default:
  12584. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  12585. }
  12586. }
  12587. /*!
  12588. @brief access to successor
  12589. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12590. */
  12591. reference operator[](difference_type n) const
  12592. {
  12593. JSON_ASSERT(m_object != nullptr);
  12594. switch (m_object->m_data.m_type)
  12595. {
  12596. case value_t::object:
  12597. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  12598. case value_t::array:
  12599. return *std::next(m_it.array_iterator, n);
  12600. case value_t::null:
  12601. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12602. case value_t::string:
  12603. case value_t::boolean:
  12604. case value_t::number_integer:
  12605. case value_t::number_unsigned:
  12606. case value_t::number_float:
  12607. case value_t::binary:
  12608. case value_t::discarded:
  12609. default:
  12610. {
  12611. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  12612. {
  12613. return *m_object;
  12614. }
  12615. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  12616. }
  12617. }
  12618. }
  12619. /*!
  12620. @brief return the key of an object iterator
  12621. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12622. */
  12623. const typename object_t::key_type& key() const
  12624. {
  12625. JSON_ASSERT(m_object != nullptr);
  12626. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  12627. {
  12628. return m_it.object_iterator->first;
  12629. }
  12630. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  12631. }
  12632. /*!
  12633. @brief return the value of an iterator
  12634. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  12635. */
  12636. reference value() const
  12637. {
  12638. return operator*();
  12639. }
  12640. JSON_PRIVATE_UNLESS_TESTED:
  12641. /// associated JSON instance
  12642. pointer m_object = nullptr;
  12643. /// the actual iterator of the associated instance
  12644. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  12645. };
  12646. } // namespace detail
  12647. NLOHMANN_JSON_NAMESPACE_END
  12648. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  12649. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  12650. // __ _____ _____ _____
  12651. // __| | __| | | | JSON for Modern C++
  12652. // | | |__ | | | | | | version 3.12.0
  12653. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12654. //
  12655. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  12656. // SPDX-License-Identifier: MIT
  12657. #include <cstddef> // ptrdiff_t
  12658. #include <iterator> // reverse_iterator
  12659. #include <utility> // declval
  12660. // #include <nlohmann/detail/abi_macros.hpp>
  12661. NLOHMANN_JSON_NAMESPACE_BEGIN
  12662. namespace detail
  12663. {
  12664. //////////////////////
  12665. // reverse_iterator //
  12666. //////////////////////
  12667. /*!
  12668. @brief a template for a reverse iterator class
  12669. @tparam Base the base iterator type to reverse. Valid types are @ref
  12670. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  12671. create @ref const_reverse_iterator).
  12672. @requirement The class satisfies the following concept requirements:
  12673. -
  12674. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  12675. The iterator that can be moved can be moved in both directions (i.e.
  12676. incremented and decremented).
  12677. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  12678. It is possible to write to the pointed-to element (only if @a Base is
  12679. @ref iterator).
  12680. @since version 1.0.0
  12681. */
  12682. template<typename Base>
  12683. class json_reverse_iterator : public std::reverse_iterator<Base>
  12684. {
  12685. public:
  12686. using difference_type = std::ptrdiff_t;
  12687. /// shortcut to the reverse iterator adapter
  12688. using base_iterator = std::reverse_iterator<Base>;
  12689. /// the reference type for the pointed-to element
  12690. using reference = typename Base::reference;
  12691. /// create reverse iterator from iterator
  12692. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  12693. : base_iterator(it) {}
  12694. /// create reverse iterator from base class
  12695. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  12696. /// post-increment (it++)
  12697. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  12698. {
  12699. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  12700. }
  12701. /// pre-increment (++it)
  12702. json_reverse_iterator& operator++()
  12703. {
  12704. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  12705. }
  12706. /// post-decrement (it--)
  12707. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  12708. {
  12709. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  12710. }
  12711. /// pre-decrement (--it)
  12712. json_reverse_iterator& operator--()
  12713. {
  12714. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  12715. }
  12716. /// add to iterator
  12717. json_reverse_iterator& operator+=(difference_type i)
  12718. {
  12719. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  12720. }
  12721. /// add to iterator
  12722. json_reverse_iterator operator+(difference_type i) const
  12723. {
  12724. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  12725. }
  12726. /// subtract from iterator
  12727. json_reverse_iterator operator-(difference_type i) const
  12728. {
  12729. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  12730. }
  12731. /// return difference
  12732. difference_type operator-(const json_reverse_iterator& other) const
  12733. {
  12734. return base_iterator(*this) - base_iterator(other);
  12735. }
  12736. /// access to successor
  12737. reference operator[](difference_type n) const
  12738. {
  12739. return *(this->operator+(n));
  12740. }
  12741. /// return the key of an object iterator
  12742. auto key() const -> decltype(std::declval<Base>().key())
  12743. {
  12744. auto it = --this->base();
  12745. return it.key();
  12746. }
  12747. /// return the value of an iterator
  12748. reference value() const
  12749. {
  12750. auto it = --this->base();
  12751. return it.operator * ();
  12752. }
  12753. };
  12754. } // namespace detail
  12755. NLOHMANN_JSON_NAMESPACE_END
  12756. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  12757. // #include <nlohmann/detail/json_custom_base_class.hpp>
  12758. // __ _____ _____ _____
  12759. // __| | __| | | | JSON for Modern C++
  12760. // | | |__ | | | | | | version 3.12.0
  12761. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12762. //
  12763. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  12764. // SPDX-License-Identifier: MIT
  12765. #include <type_traits> // conditional, is_same
  12766. // #include <nlohmann/detail/abi_macros.hpp>
  12767. NLOHMANN_JSON_NAMESPACE_BEGIN
  12768. namespace detail
  12769. {
  12770. /*!
  12771. @brief Default base class of the @ref basic_json class.
  12772. So that the correct implementations of the copy / move ctors / assign operators
  12773. of @ref basic_json do not require complex case distinctions
  12774. (no base class / custom base class used as customization point),
  12775. @ref basic_json always has a base class.
  12776. By default, this class is used because it is empty and thus has no effect
  12777. on the behavior of @ref basic_json.
  12778. */
  12779. struct json_default_base {};
  12780. template<class T>
  12781. using json_base_class = typename std::conditional <
  12782. std::is_same<T, void>::value,
  12783. json_default_base,
  12784. T
  12785. >::type;
  12786. } // namespace detail
  12787. NLOHMANN_JSON_NAMESPACE_END
  12788. // #include <nlohmann/detail/json_pointer.hpp>
  12789. // __ _____ _____ _____
  12790. // __| | __| | | | JSON for Modern C++
  12791. // | | |__ | | | | | | version 3.12.0
  12792. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12793. //
  12794. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  12795. // SPDX-License-Identifier: MIT
  12796. #include <algorithm> // all_of
  12797. #include <cctype> // isdigit
  12798. #include <cerrno> // errno, ERANGE
  12799. #include <cstdlib> // strtoull
  12800. #ifndef JSON_NO_IO
  12801. #include <iosfwd> // ostream
  12802. #endif // JSON_NO_IO
  12803. #include <limits> // max
  12804. #include <numeric> // accumulate
  12805. #include <string> // string
  12806. #include <utility> // move
  12807. #include <vector> // vector
  12808. // #include <nlohmann/detail/exceptions.hpp>
  12809. // #include <nlohmann/detail/macro_scope.hpp>
  12810. // #include <nlohmann/detail/string_concat.hpp>
  12811. // #include <nlohmann/detail/string_escape.hpp>
  12812. // #include <nlohmann/detail/value_t.hpp>
  12813. NLOHMANN_JSON_NAMESPACE_BEGIN
  12814. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  12815. /// @sa https://json.nlohmann.me/api/json_pointer/
  12816. template<typename RefStringType>
  12817. class json_pointer
  12818. {
  12819. // allow basic_json to access private members
  12820. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12821. friend class basic_json;
  12822. template<typename>
  12823. friend class json_pointer;
  12824. template<typename T>
  12825. struct string_t_helper
  12826. {
  12827. using type = T;
  12828. };
  12829. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12830. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  12831. {
  12832. using type = StringType;
  12833. };
  12834. public:
  12835. // for backwards compatibility accept BasicJsonType
  12836. using string_t = typename string_t_helper<RefStringType>::type;
  12837. /// @brief create JSON pointer
  12838. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  12839. explicit json_pointer(const string_t& s = "")
  12840. : reference_tokens(split(s))
  12841. {}
  12842. /// @brief return a string representation of the JSON pointer
  12843. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  12844. string_t to_string() const
  12845. {
  12846. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  12847. string_t{},
  12848. [](const string_t& a, const string_t& b)
  12849. {
  12850. return detail::concat(a, '/', detail::escape(b));
  12851. });
  12852. }
  12853. /// @brief return a string representation of the JSON pointer
  12854. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  12855. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  12856. operator string_t() const
  12857. {
  12858. return to_string();
  12859. }
  12860. #ifndef JSON_NO_IO
  12861. /// @brief write string representation of the JSON pointer to stream
  12862. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  12863. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  12864. {
  12865. o << ptr.to_string();
  12866. return o;
  12867. }
  12868. #endif
  12869. /// @brief append another JSON pointer at the end of this JSON pointer
  12870. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12871. json_pointer& operator/=(const json_pointer& ptr)
  12872. {
  12873. reference_tokens.insert(reference_tokens.end(),
  12874. ptr.reference_tokens.begin(),
  12875. ptr.reference_tokens.end());
  12876. return *this;
  12877. }
  12878. /// @brief append an unescaped reference token at the end of this JSON pointer
  12879. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12880. json_pointer& operator/=(string_t token)
  12881. {
  12882. push_back(std::move(token));
  12883. return *this;
  12884. }
  12885. /// @brief append an array index at the end of this JSON pointer
  12886. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12887. json_pointer& operator/=(std::size_t array_idx)
  12888. {
  12889. return *this /= std::to_string(array_idx);
  12890. }
  12891. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  12892. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12893. friend json_pointer operator/(const json_pointer& lhs,
  12894. const json_pointer& rhs)
  12895. {
  12896. return json_pointer(lhs) /= rhs;
  12897. }
  12898. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  12899. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12900. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12901. {
  12902. return json_pointer(lhs) /= std::move(token);
  12903. }
  12904. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12905. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12906. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12907. {
  12908. return json_pointer(lhs) /= array_idx;
  12909. }
  12910. /// @brief returns the parent of this JSON pointer
  12911. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12912. json_pointer parent_pointer() const
  12913. {
  12914. if (empty())
  12915. {
  12916. return *this;
  12917. }
  12918. json_pointer res = *this;
  12919. res.pop_back();
  12920. return res;
  12921. }
  12922. /// @brief remove last reference token
  12923. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12924. void pop_back()
  12925. {
  12926. if (JSON_HEDLEY_UNLIKELY(empty()))
  12927. {
  12928. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12929. }
  12930. reference_tokens.pop_back();
  12931. }
  12932. /// @brief return last reference token
  12933. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12934. const string_t& back() const
  12935. {
  12936. if (JSON_HEDLEY_UNLIKELY(empty()))
  12937. {
  12938. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12939. }
  12940. return reference_tokens.back();
  12941. }
  12942. /// @brief append an unescaped token at the end of the reference pointer
  12943. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12944. void push_back(const string_t& token)
  12945. {
  12946. reference_tokens.push_back(token);
  12947. }
  12948. /// @brief append an unescaped token at the end of the reference pointer
  12949. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12950. void push_back(string_t&& token)
  12951. {
  12952. reference_tokens.push_back(std::move(token));
  12953. }
  12954. /// @brief return whether pointer points to the root document
  12955. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12956. bool empty() const noexcept
  12957. {
  12958. return reference_tokens.empty();
  12959. }
  12960. private:
  12961. /*!
  12962. @param[in] s reference token to be converted into an array index
  12963. @return integer representation of @a s
  12964. @throw parse_error.106 if an array index begins with '0'
  12965. @throw parse_error.109 if an array index begins not with a digit
  12966. @throw out_of_range.404 if string @a s could not be converted to an integer
  12967. @throw out_of_range.410 if an array index exceeds size_type
  12968. */
  12969. template<typename BasicJsonType>
  12970. static typename BasicJsonType::size_type array_index(const string_t& s)
  12971. {
  12972. using size_type = typename BasicJsonType::size_type;
  12973. // error condition (cf. RFC 6901, Sect. 4)
  12974. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12975. {
  12976. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12977. }
  12978. // error condition (cf. RFC 6901, Sect. 4)
  12979. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12980. {
  12981. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12982. }
  12983. const char* p = s.c_str();
  12984. char* p_end = nullptr; // NOLINT(misc-const-correctness)
  12985. errno = 0; // strtoull doesn't reset errno
  12986. const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12987. if (p == p_end // invalid input or empty string
  12988. || errno == ERANGE // out of range
  12989. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12990. {
  12991. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12992. }
  12993. // only triggered on special platforms (like 32bit), see also
  12994. // https://github.com/nlohmann/json/pull/2203
  12995. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12996. {
  12997. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12998. }
  12999. return static_cast<size_type>(res);
  13000. }
  13001. JSON_PRIVATE_UNLESS_TESTED:
  13002. json_pointer top() const
  13003. {
  13004. if (JSON_HEDLEY_UNLIKELY(empty()))
  13005. {
  13006. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  13007. }
  13008. json_pointer result = *this;
  13009. result.reference_tokens = {reference_tokens[0]};
  13010. return result;
  13011. }
  13012. private:
  13013. /*!
  13014. @brief create and return a reference to the pointed to value
  13015. @complexity Linear in the number of reference tokens.
  13016. @throw parse_error.109 if array index is not a number
  13017. @throw type_error.313 if value cannot be unflattened
  13018. */
  13019. template<typename BasicJsonType>
  13020. BasicJsonType& get_and_create(BasicJsonType& j) const
  13021. {
  13022. auto* result = &j;
  13023. // in case no reference tokens exist, return a reference to the JSON value
  13024. // j which will be overwritten by a primitive value
  13025. for (const auto& reference_token : reference_tokens)
  13026. {
  13027. switch (result->type())
  13028. {
  13029. case detail::value_t::null:
  13030. {
  13031. if (reference_token == "0")
  13032. {
  13033. // start a new array if the reference token is 0
  13034. result = &result->operator[](0);
  13035. }
  13036. else
  13037. {
  13038. // start a new object otherwise
  13039. result = &result->operator[](reference_token);
  13040. }
  13041. break;
  13042. }
  13043. case detail::value_t::object:
  13044. {
  13045. // create an entry in the object
  13046. result = &result->operator[](reference_token);
  13047. break;
  13048. }
  13049. case detail::value_t::array:
  13050. {
  13051. // create an entry in the array
  13052. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  13053. break;
  13054. }
  13055. /*
  13056. The following code is only reached if there exists a reference
  13057. token _and_ the current value is primitive. In this case, we have
  13058. an error situation, because primitive values may only occur as
  13059. a single value; that is, with an empty list of reference tokens.
  13060. */
  13061. case detail::value_t::string:
  13062. case detail::value_t::boolean:
  13063. case detail::value_t::number_integer:
  13064. case detail::value_t::number_unsigned:
  13065. case detail::value_t::number_float:
  13066. case detail::value_t::binary:
  13067. case detail::value_t::discarded:
  13068. default:
  13069. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  13070. }
  13071. }
  13072. return *result;
  13073. }
  13074. /*!
  13075. @brief return a reference to the pointed to value
  13076. @note This version does not throw if a value is not present, but tries to
  13077. create nested values instead. For instance, calling this function
  13078. with pointer `"/this/that"` on a null value is equivalent to calling
  13079. `operator[]("this").operator[]("that")` on that value, effectively
  13080. changing the null value to an object.
  13081. @param[in] ptr a JSON value
  13082. @return reference to the JSON value pointed to by the JSON pointer
  13083. @complexity Linear in the length of the JSON pointer.
  13084. @throw parse_error.106 if an array index begins with '0'
  13085. @throw parse_error.109 if an array index was not a number
  13086. @throw out_of_range.404 if the JSON pointer can not be resolved
  13087. */
  13088. template<typename BasicJsonType>
  13089. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  13090. {
  13091. for (const auto& reference_token : reference_tokens)
  13092. {
  13093. // convert null values to arrays or objects before continuing
  13094. if (ptr->is_null())
  13095. {
  13096. // check if the reference token is a number
  13097. const bool nums =
  13098. std::all_of(reference_token.begin(), reference_token.end(),
  13099. [](const unsigned char x)
  13100. {
  13101. return std::isdigit(x);
  13102. });
  13103. // change value to an array for numbers or "-" or to object otherwise
  13104. *ptr = (nums || reference_token == "-")
  13105. ? detail::value_t::array
  13106. : detail::value_t::object;
  13107. }
  13108. switch (ptr->type())
  13109. {
  13110. case detail::value_t::object:
  13111. {
  13112. // use unchecked object access
  13113. ptr = &ptr->operator[](reference_token);
  13114. break;
  13115. }
  13116. case detail::value_t::array:
  13117. {
  13118. if (reference_token == "-")
  13119. {
  13120. // explicitly treat "-" as index beyond the end
  13121. ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
  13122. }
  13123. else
  13124. {
  13125. // convert array index to number; unchecked access
  13126. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  13127. }
  13128. break;
  13129. }
  13130. case detail::value_t::null:
  13131. case detail::value_t::string:
  13132. case detail::value_t::boolean:
  13133. case detail::value_t::number_integer:
  13134. case detail::value_t::number_unsigned:
  13135. case detail::value_t::number_float:
  13136. case detail::value_t::binary:
  13137. case detail::value_t::discarded:
  13138. default:
  13139. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13140. }
  13141. }
  13142. return *ptr;
  13143. }
  13144. /*!
  13145. @throw parse_error.106 if an array index begins with '0'
  13146. @throw parse_error.109 if an array index was not a number
  13147. @throw out_of_range.402 if the array index '-' is used
  13148. @throw out_of_range.404 if the JSON pointer can not be resolved
  13149. */
  13150. template<typename BasicJsonType>
  13151. BasicJsonType& get_checked(BasicJsonType* ptr) const
  13152. {
  13153. for (const auto& reference_token : reference_tokens)
  13154. {
  13155. switch (ptr->type())
  13156. {
  13157. case detail::value_t::object:
  13158. {
  13159. // note: at performs range check
  13160. ptr = &ptr->at(reference_token);
  13161. break;
  13162. }
  13163. case detail::value_t::array:
  13164. {
  13165. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13166. {
  13167. // "-" always fails the range check
  13168. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  13169. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  13170. ") is out of range"), ptr));
  13171. }
  13172. // note: at performs range check
  13173. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  13174. break;
  13175. }
  13176. case detail::value_t::null:
  13177. case detail::value_t::string:
  13178. case detail::value_t::boolean:
  13179. case detail::value_t::number_integer:
  13180. case detail::value_t::number_unsigned:
  13181. case detail::value_t::number_float:
  13182. case detail::value_t::binary:
  13183. case detail::value_t::discarded:
  13184. default:
  13185. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13186. }
  13187. }
  13188. return *ptr;
  13189. }
  13190. /*!
  13191. @brief return a const reference to the pointed to value
  13192. @param[in] ptr a JSON value
  13193. @return const reference to the JSON value pointed to by the JSON
  13194. pointer
  13195. @throw parse_error.106 if an array index begins with '0'
  13196. @throw parse_error.109 if an array index was not a number
  13197. @throw out_of_range.402 if the array index '-' is used
  13198. @throw out_of_range.404 if the JSON pointer can not be resolved
  13199. */
  13200. template<typename BasicJsonType>
  13201. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  13202. {
  13203. for (const auto& reference_token : reference_tokens)
  13204. {
  13205. switch (ptr->type())
  13206. {
  13207. case detail::value_t::object:
  13208. {
  13209. // use unchecked object access
  13210. ptr = &ptr->operator[](reference_token);
  13211. break;
  13212. }
  13213. case detail::value_t::array:
  13214. {
  13215. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13216. {
  13217. // "-" cannot be used for const access
  13218. 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));
  13219. }
  13220. // use unchecked array access
  13221. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  13222. break;
  13223. }
  13224. case detail::value_t::null:
  13225. case detail::value_t::string:
  13226. case detail::value_t::boolean:
  13227. case detail::value_t::number_integer:
  13228. case detail::value_t::number_unsigned:
  13229. case detail::value_t::number_float:
  13230. case detail::value_t::binary:
  13231. case detail::value_t::discarded:
  13232. default:
  13233. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13234. }
  13235. }
  13236. return *ptr;
  13237. }
  13238. /*!
  13239. @throw parse_error.106 if an array index begins with '0'
  13240. @throw parse_error.109 if an array index was not a number
  13241. @throw out_of_range.402 if the array index '-' is used
  13242. @throw out_of_range.404 if the JSON pointer can not be resolved
  13243. */
  13244. template<typename BasicJsonType>
  13245. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  13246. {
  13247. for (const auto& reference_token : reference_tokens)
  13248. {
  13249. switch (ptr->type())
  13250. {
  13251. case detail::value_t::object:
  13252. {
  13253. // note: at performs range check
  13254. ptr = &ptr->at(reference_token);
  13255. break;
  13256. }
  13257. case detail::value_t::array:
  13258. {
  13259. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13260. {
  13261. // "-" always fails the range check
  13262. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  13263. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  13264. ") is out of range"), ptr));
  13265. }
  13266. // note: at performs range check
  13267. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  13268. break;
  13269. }
  13270. case detail::value_t::null:
  13271. case detail::value_t::string:
  13272. case detail::value_t::boolean:
  13273. case detail::value_t::number_integer:
  13274. case detail::value_t::number_unsigned:
  13275. case detail::value_t::number_float:
  13276. case detail::value_t::binary:
  13277. case detail::value_t::discarded:
  13278. default:
  13279. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  13280. }
  13281. }
  13282. return *ptr;
  13283. }
  13284. /*!
  13285. @throw parse_error.106 if an array index begins with '0'
  13286. @throw parse_error.109 if an array index was not a number
  13287. */
  13288. template<typename BasicJsonType>
  13289. bool contains(const BasicJsonType* ptr) const
  13290. {
  13291. for (const auto& reference_token : reference_tokens)
  13292. {
  13293. switch (ptr->type())
  13294. {
  13295. case detail::value_t::object:
  13296. {
  13297. if (!ptr->contains(reference_token))
  13298. {
  13299. // we did not find the key in the object
  13300. return false;
  13301. }
  13302. ptr = &ptr->operator[](reference_token);
  13303. break;
  13304. }
  13305. case detail::value_t::array:
  13306. {
  13307. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  13308. {
  13309. // "-" always fails the range check
  13310. return false;
  13311. }
  13312. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  13313. {
  13314. // invalid char
  13315. return false;
  13316. }
  13317. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  13318. {
  13319. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  13320. {
  13321. // the first char should be between '1' and '9'
  13322. return false;
  13323. }
  13324. for (std::size_t i = 1; i < reference_token.size(); i++)
  13325. {
  13326. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  13327. {
  13328. // other char should be between '0' and '9'
  13329. return false;
  13330. }
  13331. }
  13332. }
  13333. const auto idx = array_index<BasicJsonType>(reference_token);
  13334. if (idx >= ptr->size())
  13335. {
  13336. // index out of range
  13337. return false;
  13338. }
  13339. ptr = &ptr->operator[](idx);
  13340. break;
  13341. }
  13342. case detail::value_t::null:
  13343. case detail::value_t::string:
  13344. case detail::value_t::boolean:
  13345. case detail::value_t::number_integer:
  13346. case detail::value_t::number_unsigned:
  13347. case detail::value_t::number_float:
  13348. case detail::value_t::binary:
  13349. case detail::value_t::discarded:
  13350. default:
  13351. {
  13352. // we do not expect primitive values if there is still a
  13353. // reference token to process
  13354. return false;
  13355. }
  13356. }
  13357. }
  13358. // no reference token left means we found a primitive value
  13359. return true;
  13360. }
  13361. /*!
  13362. @brief split the string input to reference tokens
  13363. @note This function is only called by the json_pointer constructor.
  13364. All exceptions below are documented there.
  13365. @throw parse_error.107 if the pointer is not empty or begins with '/'
  13366. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  13367. */
  13368. static std::vector<string_t> split(const string_t& reference_string)
  13369. {
  13370. std::vector<string_t> result;
  13371. // special case: empty reference string -> no reference tokens
  13372. if (reference_string.empty())
  13373. {
  13374. return result;
  13375. }
  13376. // check if a nonempty reference string begins with slash
  13377. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  13378. {
  13379. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  13380. }
  13381. // extract the reference tokens:
  13382. // - slash: position of the last read slash (or end of string)
  13383. // - start: position after the previous slash
  13384. for (
  13385. // search for the first slash after the first character
  13386. std::size_t slash = reference_string.find_first_of('/', 1),
  13387. // set the beginning of the first reference token
  13388. start = 1;
  13389. // we can stop if start == 0 (if slash == string_t::npos)
  13390. start != 0;
  13391. // set the beginning of the next reference token
  13392. // (will eventually be 0 if slash == string_t::npos)
  13393. start = (slash == string_t::npos) ? 0 : slash + 1,
  13394. // find next slash
  13395. slash = reference_string.find_first_of('/', start))
  13396. {
  13397. // use the text between the beginning of the reference token
  13398. // (start) and the last slash (slash).
  13399. auto reference_token = reference_string.substr(start, slash - start);
  13400. // check reference tokens are properly escaped
  13401. for (std::size_t pos = reference_token.find_first_of('~');
  13402. pos != string_t::npos;
  13403. pos = reference_token.find_first_of('~', pos + 1))
  13404. {
  13405. JSON_ASSERT(reference_token[pos] == '~');
  13406. // ~ must be followed by 0 or 1
  13407. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  13408. (reference_token[pos + 1] != '0' &&
  13409. reference_token[pos + 1] != '1')))
  13410. {
  13411. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  13412. }
  13413. }
  13414. // finally, store the reference token
  13415. detail::unescape(reference_token);
  13416. result.push_back(reference_token);
  13417. }
  13418. return result;
  13419. }
  13420. private:
  13421. /*!
  13422. @param[in] reference_string the reference string to the current value
  13423. @param[in] value the value to consider
  13424. @param[in,out] result the result object to insert values to
  13425. @note Empty objects or arrays are flattened to `null`.
  13426. */
  13427. template<typename BasicJsonType>
  13428. static void flatten(const string_t& reference_string,
  13429. const BasicJsonType& value,
  13430. BasicJsonType& result)
  13431. {
  13432. switch (value.type())
  13433. {
  13434. case detail::value_t::array:
  13435. {
  13436. if (value.m_data.m_value.array->empty())
  13437. {
  13438. // flatten empty array as null
  13439. result[reference_string] = nullptr;
  13440. }
  13441. else
  13442. {
  13443. // iterate array and use index as a reference string
  13444. for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
  13445. {
  13446. flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
  13447. value.m_data.m_value.array->operator[](i), result);
  13448. }
  13449. }
  13450. break;
  13451. }
  13452. case detail::value_t::object:
  13453. {
  13454. if (value.m_data.m_value.object->empty())
  13455. {
  13456. // flatten empty object as null
  13457. result[reference_string] = nullptr;
  13458. }
  13459. else
  13460. {
  13461. // iterate object and use keys as reference string
  13462. for (const auto& element : *value.m_data.m_value.object)
  13463. {
  13464. flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
  13465. }
  13466. }
  13467. break;
  13468. }
  13469. case detail::value_t::null:
  13470. case detail::value_t::string:
  13471. case detail::value_t::boolean:
  13472. case detail::value_t::number_integer:
  13473. case detail::value_t::number_unsigned:
  13474. case detail::value_t::number_float:
  13475. case detail::value_t::binary:
  13476. case detail::value_t::discarded:
  13477. default:
  13478. {
  13479. // add a primitive value with its reference string
  13480. result[reference_string] = value;
  13481. break;
  13482. }
  13483. }
  13484. }
  13485. /*!
  13486. @param[in] value flattened JSON
  13487. @return unflattened JSON
  13488. @throw parse_error.109 if array index is not a number
  13489. @throw type_error.314 if value is not an object
  13490. @throw type_error.315 if object values are not primitive
  13491. @throw type_error.313 if value cannot be unflattened
  13492. */
  13493. template<typename BasicJsonType>
  13494. static BasicJsonType
  13495. unflatten(const BasicJsonType& value)
  13496. {
  13497. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  13498. {
  13499. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  13500. }
  13501. BasicJsonType result;
  13502. // iterate the JSON object values
  13503. for (const auto& element : *value.m_data.m_value.object)
  13504. {
  13505. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  13506. {
  13507. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  13508. }
  13509. // Assign the value to the reference pointed to by JSON pointer. Note
  13510. // that if the JSON pointer is "" (i.e., points to the whole value),
  13511. // function get_and_create returns a reference to the result itself.
  13512. // An assignment will then create a primitive value.
  13513. json_pointer(element.first).get_and_create(result) = element.second;
  13514. }
  13515. return result;
  13516. }
  13517. // can't use the conversion operator because of ambiguity
  13518. json_pointer<string_t> convert() const&
  13519. {
  13520. json_pointer<string_t> result;
  13521. result.reference_tokens = reference_tokens;
  13522. return result;
  13523. }
  13524. json_pointer<string_t> convert()&&
  13525. {
  13526. json_pointer<string_t> result;
  13527. result.reference_tokens = std::move(reference_tokens);
  13528. return result;
  13529. }
  13530. public:
  13531. #if JSON_HAS_THREE_WAY_COMPARISON
  13532. /// @brief compares two JSON pointers for equality
  13533. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13534. template<typename RefStringTypeRhs>
  13535. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  13536. {
  13537. return reference_tokens == rhs.reference_tokens;
  13538. }
  13539. /// @brief compares JSON pointer and string for equality
  13540. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13541. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  13542. bool operator==(const string_t& rhs) const
  13543. {
  13544. return *this == json_pointer(rhs);
  13545. }
  13546. /// @brief 3-way compares two JSON pointers
  13547. template<typename RefStringTypeRhs>
  13548. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  13549. {
  13550. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  13551. }
  13552. #else
  13553. /// @brief compares two JSON pointers for equality
  13554. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13555. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13556. // NOLINTNEXTLINE(readability-redundant-declaration)
  13557. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13558. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  13559. /// @brief compares JSON pointer and string for equality
  13560. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13561. template<typename RefStringTypeLhs, typename StringType>
  13562. // NOLINTNEXTLINE(readability-redundant-declaration)
  13563. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13564. const StringType& rhs);
  13565. /// @brief compares string and JSON pointer for equality
  13566. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  13567. template<typename RefStringTypeRhs, typename StringType>
  13568. // NOLINTNEXTLINE(readability-redundant-declaration)
  13569. friend bool operator==(const StringType& lhs,
  13570. const json_pointer<RefStringTypeRhs>& rhs);
  13571. /// @brief compares two JSON pointers for inequality
  13572. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  13573. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13574. // NOLINTNEXTLINE(readability-redundant-declaration)
  13575. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13576. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  13577. /// @brief compares JSON pointer and string for inequality
  13578. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  13579. template<typename RefStringTypeLhs, typename StringType>
  13580. // NOLINTNEXTLINE(readability-redundant-declaration)
  13581. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13582. const StringType& rhs);
  13583. /// @brief compares string and JSON pointer for inequality
  13584. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  13585. template<typename RefStringTypeRhs, typename StringType>
  13586. // NOLINTNEXTLINE(readability-redundant-declaration)
  13587. friend bool operator!=(const StringType& lhs,
  13588. const json_pointer<RefStringTypeRhs>& rhs);
  13589. /// @brief compares two JSON pointer for less-than
  13590. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13591. // NOLINTNEXTLINE(readability-redundant-declaration)
  13592. friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  13593. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  13594. #endif
  13595. private:
  13596. /// the reference tokens
  13597. std::vector<string_t> reference_tokens;
  13598. };
  13599. #if !JSON_HAS_THREE_WAY_COMPARISON
  13600. // functions cannot be defined inside the class due to ODR violations
  13601. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13602. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13603. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  13604. {
  13605. return lhs.reference_tokens == rhs.reference_tokens;
  13606. }
  13607. template<typename RefStringTypeLhs,
  13608. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  13609. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  13610. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  13611. const StringType& rhs)
  13612. {
  13613. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  13614. }
  13615. template<typename RefStringTypeRhs,
  13616. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  13617. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  13618. inline bool operator==(const StringType& lhs,
  13619. const json_pointer<RefStringTypeRhs>& rhs)
  13620. {
  13621. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  13622. }
  13623. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13624. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13625. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  13626. {
  13627. return !(lhs == rhs);
  13628. }
  13629. template<typename RefStringTypeLhs,
  13630. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  13631. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  13632. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  13633. const StringType& rhs)
  13634. {
  13635. return !(lhs == rhs);
  13636. }
  13637. template<typename RefStringTypeRhs,
  13638. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  13639. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  13640. inline bool operator!=(const StringType& lhs,
  13641. const json_pointer<RefStringTypeRhs>& rhs)
  13642. {
  13643. return !(lhs == rhs);
  13644. }
  13645. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  13646. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  13647. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  13648. {
  13649. return lhs.reference_tokens < rhs.reference_tokens;
  13650. }
  13651. #endif
  13652. NLOHMANN_JSON_NAMESPACE_END
  13653. // #include <nlohmann/detail/json_ref.hpp>
  13654. // __ _____ _____ _____
  13655. // __| | __| | | | JSON for Modern C++
  13656. // | | |__ | | | | | | version 3.12.0
  13657. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  13658. //
  13659. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  13660. // SPDX-License-Identifier: MIT
  13661. #include <initializer_list>
  13662. #include <utility>
  13663. // #include <nlohmann/detail/abi_macros.hpp>
  13664. // #include <nlohmann/detail/meta/type_traits.hpp>
  13665. NLOHMANN_JSON_NAMESPACE_BEGIN
  13666. namespace detail
  13667. {
  13668. template<typename BasicJsonType>
  13669. class json_ref
  13670. {
  13671. public:
  13672. using value_type = BasicJsonType;
  13673. json_ref(value_type&& value)
  13674. : owned_value(std::move(value))
  13675. {}
  13676. json_ref(const value_type& value)
  13677. : value_ref(&value)
  13678. {}
  13679. json_ref(std::initializer_list<json_ref> init)
  13680. : owned_value(init)
  13681. {}
  13682. template <
  13683. class... Args,
  13684. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  13685. json_ref(Args && ... args)
  13686. : owned_value(std::forward<Args>(args)...)
  13687. {}
  13688. // class should be movable only
  13689. json_ref(json_ref&&) noexcept = default;
  13690. json_ref(const json_ref&) = delete;
  13691. json_ref& operator=(const json_ref&) = delete;
  13692. json_ref& operator=(json_ref&&) = delete;
  13693. ~json_ref() = default;
  13694. value_type moved_or_copied() const
  13695. {
  13696. if (value_ref == nullptr)
  13697. {
  13698. return std::move(owned_value);
  13699. }
  13700. return *value_ref;
  13701. }
  13702. value_type const& operator*() const
  13703. {
  13704. return value_ref ? *value_ref : owned_value;
  13705. }
  13706. value_type const* operator->() const
  13707. {
  13708. return &** this;
  13709. }
  13710. private:
  13711. mutable value_type owned_value = nullptr;
  13712. value_type const* value_ref = nullptr;
  13713. };
  13714. } // namespace detail
  13715. NLOHMANN_JSON_NAMESPACE_END
  13716. // #include <nlohmann/detail/macro_scope.hpp>
  13717. // #include <nlohmann/detail/string_concat.hpp>
  13718. // #include <nlohmann/detail/string_escape.hpp>
  13719. // #include <nlohmann/detail/string_utils.hpp>
  13720. // #include <nlohmann/detail/meta/cpp_future.hpp>
  13721. // #include <nlohmann/detail/meta/type_traits.hpp>
  13722. // #include <nlohmann/detail/output/binary_writer.hpp>
  13723. // __ _____ _____ _____
  13724. // __| | __| | | | JSON for Modern C++
  13725. // | | |__ | | | | | | version 3.12.0
  13726. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  13727. //
  13728. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  13729. // SPDX-License-Identifier: MIT
  13730. #include <algorithm> // reverse
  13731. #include <array> // array
  13732. #include <map> // map
  13733. #include <cmath> // isnan, isinf
  13734. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  13735. #include <cstring> // memcpy
  13736. #include <limits> // numeric_limits
  13737. #include <string> // string
  13738. #include <utility> // move
  13739. #include <vector> // vector
  13740. // #include <nlohmann/detail/input/binary_reader.hpp>
  13741. // #include <nlohmann/detail/macro_scope.hpp>
  13742. // #include <nlohmann/detail/output/output_adapters.hpp>
  13743. // __ _____ _____ _____
  13744. // __| | __| | | | JSON for Modern C++
  13745. // | | |__ | | | | | | version 3.12.0
  13746. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  13747. //
  13748. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  13749. // SPDX-License-Identifier: MIT
  13750. #include <algorithm> // copy
  13751. #include <cstddef> // size_t
  13752. #include <iterator> // back_inserter
  13753. #include <memory> // shared_ptr, make_shared
  13754. #include <string> // basic_string
  13755. #include <vector> // vector
  13756. #ifndef JSON_NO_IO
  13757. #include <ios> // streamsize
  13758. #include <ostream> // basic_ostream
  13759. #endif // JSON_NO_IO
  13760. // #include <nlohmann/detail/macro_scope.hpp>
  13761. NLOHMANN_JSON_NAMESPACE_BEGIN
  13762. namespace detail
  13763. {
  13764. /// abstract output adapter interface
  13765. template<typename CharType> struct output_adapter_protocol
  13766. {
  13767. virtual void write_character(CharType c) = 0;
  13768. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  13769. virtual ~output_adapter_protocol() = default;
  13770. output_adapter_protocol() = default;
  13771. output_adapter_protocol(const output_adapter_protocol&) = default;
  13772. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  13773. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  13774. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  13775. };
  13776. /// a type to simplify interfaces
  13777. template<typename CharType>
  13778. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  13779. /// output adapter for byte vectors
  13780. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  13781. class output_vector_adapter : public output_adapter_protocol<CharType>
  13782. {
  13783. public:
  13784. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  13785. : v(vec)
  13786. {}
  13787. void write_character(CharType c) override
  13788. {
  13789. v.push_back(c);
  13790. }
  13791. JSON_HEDLEY_NON_NULL(2)
  13792. void write_characters(const CharType* s, std::size_t length) override
  13793. {
  13794. v.insert(v.end(), s, s + length);
  13795. }
  13796. private:
  13797. std::vector<CharType, AllocatorType>& v;
  13798. };
  13799. #ifndef JSON_NO_IO
  13800. /// output adapter for output streams
  13801. template<typename CharType>
  13802. class output_stream_adapter : public output_adapter_protocol<CharType>
  13803. {
  13804. public:
  13805. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  13806. : stream(s)
  13807. {}
  13808. void write_character(CharType c) override
  13809. {
  13810. stream.put(c);
  13811. }
  13812. JSON_HEDLEY_NON_NULL(2)
  13813. void write_characters(const CharType* s, std::size_t length) override
  13814. {
  13815. stream.write(s, static_cast<std::streamsize>(length));
  13816. }
  13817. private:
  13818. std::basic_ostream<CharType>& stream;
  13819. };
  13820. #endif // JSON_NO_IO
  13821. /// output adapter for basic_string
  13822. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13823. class output_string_adapter : public output_adapter_protocol<CharType>
  13824. {
  13825. public:
  13826. explicit output_string_adapter(StringType& s) noexcept
  13827. : str(s)
  13828. {}
  13829. void write_character(CharType c) override
  13830. {
  13831. str.push_back(c);
  13832. }
  13833. JSON_HEDLEY_NON_NULL(2)
  13834. void write_characters(const CharType* s, std::size_t length) override
  13835. {
  13836. str.append(s, length);
  13837. }
  13838. private:
  13839. StringType& str;
  13840. };
  13841. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13842. class output_adapter
  13843. {
  13844. public:
  13845. template<typename AllocatorType = std::allocator<CharType>>
  13846. output_adapter(std::vector<CharType, AllocatorType>& vec)
  13847. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  13848. #ifndef JSON_NO_IO
  13849. output_adapter(std::basic_ostream<CharType>& s)
  13850. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  13851. #endif // JSON_NO_IO
  13852. output_adapter(StringType& s)
  13853. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  13854. operator output_adapter_t<CharType>()
  13855. {
  13856. return oa;
  13857. }
  13858. private:
  13859. output_adapter_t<CharType> oa = nullptr;
  13860. };
  13861. } // namespace detail
  13862. NLOHMANN_JSON_NAMESPACE_END
  13863. // #include <nlohmann/detail/string_concat.hpp>
  13864. NLOHMANN_JSON_NAMESPACE_BEGIN
  13865. namespace detail
  13866. {
  13867. /// how to encode BJData
  13868. enum class bjdata_version_t
  13869. {
  13870. draft2,
  13871. draft3,
  13872. };
  13873. ///////////////////
  13874. // binary writer //
  13875. ///////////////////
  13876. /*!
  13877. @brief serialization to CBOR and MessagePack values
  13878. */
  13879. template<typename BasicJsonType, typename CharType>
  13880. class binary_writer
  13881. {
  13882. using string_t = typename BasicJsonType::string_t;
  13883. using binary_t = typename BasicJsonType::binary_t;
  13884. using number_float_t = typename BasicJsonType::number_float_t;
  13885. public:
  13886. /*!
  13887. @brief create a binary writer
  13888. @param[in] adapter output adapter to write to
  13889. */
  13890. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  13891. {
  13892. JSON_ASSERT(oa);
  13893. }
  13894. /*!
  13895. @param[in] j JSON value to serialize
  13896. @pre j.type() == value_t::object
  13897. */
  13898. void write_bson(const BasicJsonType& j)
  13899. {
  13900. switch (j.type())
  13901. {
  13902. case value_t::object:
  13903. {
  13904. write_bson_object(*j.m_data.m_value.object);
  13905. break;
  13906. }
  13907. case value_t::null:
  13908. case value_t::array:
  13909. case value_t::string:
  13910. case value_t::boolean:
  13911. case value_t::number_integer:
  13912. case value_t::number_unsigned:
  13913. case value_t::number_float:
  13914. case value_t::binary:
  13915. case value_t::discarded:
  13916. default:
  13917. {
  13918. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13919. }
  13920. }
  13921. }
  13922. /*!
  13923. @param[in] j JSON value to serialize
  13924. */
  13925. void write_cbor(const BasicJsonType& j)
  13926. {
  13927. switch (j.type())
  13928. {
  13929. case value_t::null:
  13930. {
  13931. oa->write_character(to_char_type(0xF6));
  13932. break;
  13933. }
  13934. case value_t::boolean:
  13935. {
  13936. oa->write_character(j.m_data.m_value.boolean
  13937. ? to_char_type(0xF5)
  13938. : to_char_type(0xF4));
  13939. break;
  13940. }
  13941. case value_t::number_integer:
  13942. {
  13943. if (j.m_data.m_value.number_integer >= 0)
  13944. {
  13945. // CBOR does not differentiate between positive signed
  13946. // integers and unsigned integers. Therefore, we used the
  13947. // code from the value_t::number_unsigned case here.
  13948. if (j.m_data.m_value.number_integer <= 0x17)
  13949. {
  13950. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13951. }
  13952. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13953. {
  13954. oa->write_character(to_char_type(0x18));
  13955. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13956. }
  13957. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13958. {
  13959. oa->write_character(to_char_type(0x19));
  13960. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13961. }
  13962. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13963. {
  13964. oa->write_character(to_char_type(0x1A));
  13965. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13966. }
  13967. else
  13968. {
  13969. oa->write_character(to_char_type(0x1B));
  13970. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13971. }
  13972. }
  13973. else
  13974. {
  13975. // The conversions below encode the sign in the first
  13976. // byte, and the value is converted to a positive number.
  13977. const auto positive_number = -1 - j.m_data.m_value.number_integer;
  13978. if (j.m_data.m_value.number_integer >= -24)
  13979. {
  13980. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13981. }
  13982. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13983. {
  13984. oa->write_character(to_char_type(0x38));
  13985. write_number(static_cast<std::uint8_t>(positive_number));
  13986. }
  13987. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13988. {
  13989. oa->write_character(to_char_type(0x39));
  13990. write_number(static_cast<std::uint16_t>(positive_number));
  13991. }
  13992. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13993. {
  13994. oa->write_character(to_char_type(0x3A));
  13995. write_number(static_cast<std::uint32_t>(positive_number));
  13996. }
  13997. else
  13998. {
  13999. oa->write_character(to_char_type(0x3B));
  14000. write_number(static_cast<std::uint64_t>(positive_number));
  14001. }
  14002. }
  14003. break;
  14004. }
  14005. case value_t::number_unsigned:
  14006. {
  14007. if (j.m_data.m_value.number_unsigned <= 0x17)
  14008. {
  14009. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  14010. }
  14011. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  14012. {
  14013. oa->write_character(to_char_type(0x18));
  14014. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  14015. }
  14016. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  14017. {
  14018. oa->write_character(to_char_type(0x19));
  14019. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
  14020. }
  14021. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  14022. {
  14023. oa->write_character(to_char_type(0x1A));
  14024. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
  14025. }
  14026. else
  14027. {
  14028. oa->write_character(to_char_type(0x1B));
  14029. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
  14030. }
  14031. break;
  14032. }
  14033. case value_t::number_float:
  14034. {
  14035. if (std::isnan(j.m_data.m_value.number_float))
  14036. {
  14037. // NaN is 0xf97e00 in CBOR
  14038. oa->write_character(to_char_type(0xF9));
  14039. oa->write_character(to_char_type(0x7E));
  14040. oa->write_character(to_char_type(0x00));
  14041. }
  14042. else if (std::isinf(j.m_data.m_value.number_float))
  14043. {
  14044. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  14045. oa->write_character(to_char_type(0xf9));
  14046. oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  14047. oa->write_character(to_char_type(0x00));
  14048. }
  14049. else
  14050. {
  14051. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
  14052. }
  14053. break;
  14054. }
  14055. case value_t::string:
  14056. {
  14057. // step 1: write control byte and the string length
  14058. const auto N = j.m_data.m_value.string->size();
  14059. if (N <= 0x17)
  14060. {
  14061. write_number(static_cast<std::uint8_t>(0x60 + N));
  14062. }
  14063. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14064. {
  14065. oa->write_character(to_char_type(0x78));
  14066. write_number(static_cast<std::uint8_t>(N));
  14067. }
  14068. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14069. {
  14070. oa->write_character(to_char_type(0x79));
  14071. write_number(static_cast<std::uint16_t>(N));
  14072. }
  14073. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14074. {
  14075. oa->write_character(to_char_type(0x7A));
  14076. write_number(static_cast<std::uint32_t>(N));
  14077. }
  14078. // LCOV_EXCL_START
  14079. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  14080. {
  14081. oa->write_character(to_char_type(0x7B));
  14082. write_number(static_cast<std::uint64_t>(N));
  14083. }
  14084. // LCOV_EXCL_STOP
  14085. // step 2: write the string
  14086. oa->write_characters(
  14087. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  14088. j.m_data.m_value.string->size());
  14089. break;
  14090. }
  14091. case value_t::array:
  14092. {
  14093. // step 1: write control byte and the array size
  14094. const auto N = j.m_data.m_value.array->size();
  14095. if (N <= 0x17)
  14096. {
  14097. write_number(static_cast<std::uint8_t>(0x80 + N));
  14098. }
  14099. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14100. {
  14101. oa->write_character(to_char_type(0x98));
  14102. write_number(static_cast<std::uint8_t>(N));
  14103. }
  14104. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14105. {
  14106. oa->write_character(to_char_type(0x99));
  14107. write_number(static_cast<std::uint16_t>(N));
  14108. }
  14109. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14110. {
  14111. oa->write_character(to_char_type(0x9A));
  14112. write_number(static_cast<std::uint32_t>(N));
  14113. }
  14114. // LCOV_EXCL_START
  14115. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  14116. {
  14117. oa->write_character(to_char_type(0x9B));
  14118. write_number(static_cast<std::uint64_t>(N));
  14119. }
  14120. // LCOV_EXCL_STOP
  14121. // step 2: write each element
  14122. for (const auto& el : *j.m_data.m_value.array)
  14123. {
  14124. write_cbor(el);
  14125. }
  14126. break;
  14127. }
  14128. case value_t::binary:
  14129. {
  14130. if (j.m_data.m_value.binary->has_subtype())
  14131. {
  14132. if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  14133. {
  14134. write_number(static_cast<std::uint8_t>(0xd8));
  14135. write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
  14136. }
  14137. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  14138. {
  14139. write_number(static_cast<std::uint8_t>(0xd9));
  14140. write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
  14141. }
  14142. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  14143. {
  14144. write_number(static_cast<std::uint8_t>(0xda));
  14145. write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
  14146. }
  14147. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  14148. {
  14149. write_number(static_cast<std::uint8_t>(0xdb));
  14150. write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
  14151. }
  14152. }
  14153. // step 1: write control byte and the binary array size
  14154. const auto N = j.m_data.m_value.binary->size();
  14155. if (N <= 0x17)
  14156. {
  14157. write_number(static_cast<std::uint8_t>(0x40 + N));
  14158. }
  14159. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14160. {
  14161. oa->write_character(to_char_type(0x58));
  14162. write_number(static_cast<std::uint8_t>(N));
  14163. }
  14164. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14165. {
  14166. oa->write_character(to_char_type(0x59));
  14167. write_number(static_cast<std::uint16_t>(N));
  14168. }
  14169. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14170. {
  14171. oa->write_character(to_char_type(0x5A));
  14172. write_number(static_cast<std::uint32_t>(N));
  14173. }
  14174. // LCOV_EXCL_START
  14175. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  14176. {
  14177. oa->write_character(to_char_type(0x5B));
  14178. write_number(static_cast<std::uint64_t>(N));
  14179. }
  14180. // LCOV_EXCL_STOP
  14181. // step 2: write each element
  14182. oa->write_characters(
  14183. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  14184. N);
  14185. break;
  14186. }
  14187. case value_t::object:
  14188. {
  14189. // step 1: write control byte and the object size
  14190. const auto N = j.m_data.m_value.object->size();
  14191. if (N <= 0x17)
  14192. {
  14193. write_number(static_cast<std::uint8_t>(0xA0 + N));
  14194. }
  14195. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14196. {
  14197. oa->write_character(to_char_type(0xB8));
  14198. write_number(static_cast<std::uint8_t>(N));
  14199. }
  14200. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14201. {
  14202. oa->write_character(to_char_type(0xB9));
  14203. write_number(static_cast<std::uint16_t>(N));
  14204. }
  14205. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14206. {
  14207. oa->write_character(to_char_type(0xBA));
  14208. write_number(static_cast<std::uint32_t>(N));
  14209. }
  14210. // LCOV_EXCL_START
  14211. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  14212. {
  14213. oa->write_character(to_char_type(0xBB));
  14214. write_number(static_cast<std::uint64_t>(N));
  14215. }
  14216. // LCOV_EXCL_STOP
  14217. // step 2: write each element
  14218. for (const auto& el : *j.m_data.m_value.object)
  14219. {
  14220. write_cbor(el.first);
  14221. write_cbor(el.second);
  14222. }
  14223. break;
  14224. }
  14225. case value_t::discarded:
  14226. default:
  14227. break;
  14228. }
  14229. }
  14230. /*!
  14231. @param[in] j JSON value to serialize
  14232. */
  14233. void write_msgpack(const BasicJsonType& j)
  14234. {
  14235. switch (j.type())
  14236. {
  14237. case value_t::null: // nil
  14238. {
  14239. oa->write_character(to_char_type(0xC0));
  14240. break;
  14241. }
  14242. case value_t::boolean: // true and false
  14243. {
  14244. oa->write_character(j.m_data.m_value.boolean
  14245. ? to_char_type(0xC3)
  14246. : to_char_type(0xC2));
  14247. break;
  14248. }
  14249. case value_t::number_integer:
  14250. {
  14251. if (j.m_data.m_value.number_integer >= 0)
  14252. {
  14253. // MessagePack does not differentiate between positive
  14254. // signed integers and unsigned integers. Therefore, we used
  14255. // the code from the value_t::number_unsigned case here.
  14256. if (j.m_data.m_value.number_unsigned < 128)
  14257. {
  14258. // positive fixnum
  14259. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14260. }
  14261. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  14262. {
  14263. // uint 8
  14264. oa->write_character(to_char_type(0xCC));
  14265. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14266. }
  14267. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  14268. {
  14269. // uint 16
  14270. oa->write_character(to_char_type(0xCD));
  14271. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  14272. }
  14273. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  14274. {
  14275. // uint 32
  14276. oa->write_character(to_char_type(0xCE));
  14277. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  14278. }
  14279. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14280. {
  14281. // uint 64
  14282. oa->write_character(to_char_type(0xCF));
  14283. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  14284. }
  14285. }
  14286. else
  14287. {
  14288. if (j.m_data.m_value.number_integer >= -32)
  14289. {
  14290. // negative fixnum
  14291. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  14292. }
  14293. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  14294. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  14295. {
  14296. // int 8
  14297. oa->write_character(to_char_type(0xD0));
  14298. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  14299. }
  14300. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  14301. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  14302. {
  14303. // int 16
  14304. oa->write_character(to_char_type(0xD1));
  14305. write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
  14306. }
  14307. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  14308. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  14309. {
  14310. // int 32
  14311. oa->write_character(to_char_type(0xD2));
  14312. write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
  14313. }
  14314. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  14315. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  14316. {
  14317. // int 64
  14318. oa->write_character(to_char_type(0xD3));
  14319. write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
  14320. }
  14321. }
  14322. break;
  14323. }
  14324. case value_t::number_unsigned:
  14325. {
  14326. if (j.m_data.m_value.number_unsigned < 128)
  14327. {
  14328. // positive fixnum
  14329. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14330. }
  14331. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  14332. {
  14333. // uint 8
  14334. oa->write_character(to_char_type(0xCC));
  14335. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  14336. }
  14337. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  14338. {
  14339. // uint 16
  14340. oa->write_character(to_char_type(0xCD));
  14341. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  14342. }
  14343. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  14344. {
  14345. // uint 32
  14346. oa->write_character(to_char_type(0xCE));
  14347. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  14348. }
  14349. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14350. {
  14351. // uint 64
  14352. oa->write_character(to_char_type(0xCF));
  14353. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  14354. }
  14355. break;
  14356. }
  14357. case value_t::number_float:
  14358. {
  14359. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
  14360. break;
  14361. }
  14362. case value_t::string:
  14363. {
  14364. // step 1: write control byte and the string length
  14365. const auto N = j.m_data.m_value.string->size();
  14366. if (N <= 31)
  14367. {
  14368. // fixstr
  14369. write_number(static_cast<std::uint8_t>(0xA0 | N));
  14370. }
  14371. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14372. {
  14373. // str 8
  14374. oa->write_character(to_char_type(0xD9));
  14375. write_number(static_cast<std::uint8_t>(N));
  14376. }
  14377. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14378. {
  14379. // str 16
  14380. oa->write_character(to_char_type(0xDA));
  14381. write_number(static_cast<std::uint16_t>(N));
  14382. }
  14383. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14384. {
  14385. // str 32
  14386. oa->write_character(to_char_type(0xDB));
  14387. write_number(static_cast<std::uint32_t>(N));
  14388. }
  14389. // step 2: write the string
  14390. oa->write_characters(
  14391. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  14392. j.m_data.m_value.string->size());
  14393. break;
  14394. }
  14395. case value_t::array:
  14396. {
  14397. // step 1: write control byte and the array size
  14398. const auto N = j.m_data.m_value.array->size();
  14399. if (N <= 15)
  14400. {
  14401. // fixarray
  14402. write_number(static_cast<std::uint8_t>(0x90 | N));
  14403. }
  14404. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14405. {
  14406. // array 16
  14407. oa->write_character(to_char_type(0xDC));
  14408. write_number(static_cast<std::uint16_t>(N));
  14409. }
  14410. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14411. {
  14412. // array 32
  14413. oa->write_character(to_char_type(0xDD));
  14414. write_number(static_cast<std::uint32_t>(N));
  14415. }
  14416. // step 2: write each element
  14417. for (const auto& el : *j.m_data.m_value.array)
  14418. {
  14419. write_msgpack(el);
  14420. }
  14421. break;
  14422. }
  14423. case value_t::binary:
  14424. {
  14425. // step 0: determine if the binary type has a set subtype to
  14426. // determine whether to use the ext or fixext types
  14427. const bool use_ext = j.m_data.m_value.binary->has_subtype();
  14428. // step 1: write control byte and the byte string length
  14429. const auto N = j.m_data.m_value.binary->size();
  14430. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  14431. {
  14432. std::uint8_t output_type{};
  14433. bool fixed = true;
  14434. if (use_ext)
  14435. {
  14436. switch (N)
  14437. {
  14438. case 1:
  14439. output_type = 0xD4; // fixext 1
  14440. break;
  14441. case 2:
  14442. output_type = 0xD5; // fixext 2
  14443. break;
  14444. case 4:
  14445. output_type = 0xD6; // fixext 4
  14446. break;
  14447. case 8:
  14448. output_type = 0xD7; // fixext 8
  14449. break;
  14450. case 16:
  14451. output_type = 0xD8; // fixext 16
  14452. break;
  14453. default:
  14454. output_type = 0xC7; // ext 8
  14455. fixed = false;
  14456. break;
  14457. }
  14458. }
  14459. else
  14460. {
  14461. output_type = 0xC4; // bin 8
  14462. fixed = false;
  14463. }
  14464. oa->write_character(to_char_type(output_type));
  14465. if (!fixed)
  14466. {
  14467. write_number(static_cast<std::uint8_t>(N));
  14468. }
  14469. }
  14470. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14471. {
  14472. const std::uint8_t output_type = use_ext
  14473. ? 0xC8 // ext 16
  14474. : 0xC5; // bin 16
  14475. oa->write_character(to_char_type(output_type));
  14476. write_number(static_cast<std::uint16_t>(N));
  14477. }
  14478. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14479. {
  14480. const std::uint8_t output_type = use_ext
  14481. ? 0xC9 // ext 32
  14482. : 0xC6; // bin 32
  14483. oa->write_character(to_char_type(output_type));
  14484. write_number(static_cast<std::uint32_t>(N));
  14485. }
  14486. // step 1.5: if this is an ext type, write the subtype
  14487. if (use_ext)
  14488. {
  14489. write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
  14490. }
  14491. // step 2: write the byte string
  14492. oa->write_characters(
  14493. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  14494. N);
  14495. break;
  14496. }
  14497. case value_t::object:
  14498. {
  14499. // step 1: write control byte and the object size
  14500. const auto N = j.m_data.m_value.object->size();
  14501. if (N <= 15)
  14502. {
  14503. // fixmap
  14504. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  14505. }
  14506. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  14507. {
  14508. // map 16
  14509. oa->write_character(to_char_type(0xDE));
  14510. write_number(static_cast<std::uint16_t>(N));
  14511. }
  14512. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  14513. {
  14514. // map 32
  14515. oa->write_character(to_char_type(0xDF));
  14516. write_number(static_cast<std::uint32_t>(N));
  14517. }
  14518. // step 2: write each element
  14519. for (const auto& el : *j.m_data.m_value.object)
  14520. {
  14521. write_msgpack(el.first);
  14522. write_msgpack(el.second);
  14523. }
  14524. break;
  14525. }
  14526. case value_t::discarded:
  14527. default:
  14528. break;
  14529. }
  14530. }
  14531. /*!
  14532. @param[in] j JSON value to serialize
  14533. @param[in] use_count whether to use '#' prefixes (optimized format)
  14534. @param[in] use_type whether to use '$' prefixes (optimized format)
  14535. @param[in] add_prefix whether prefixes need to be used for this value
  14536. @param[in] use_bjdata whether write in BJData format, default is false
  14537. @param[in] bjdata_version which BJData version to use, default is draft2
  14538. */
  14539. void write_ubjson(const BasicJsonType& j, const bool use_count,
  14540. const bool use_type, const bool add_prefix = true,
  14541. const bool use_bjdata = false, const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
  14542. {
  14543. const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
  14544. switch (j.type())
  14545. {
  14546. case value_t::null:
  14547. {
  14548. if (add_prefix)
  14549. {
  14550. oa->write_character(to_char_type('Z'));
  14551. }
  14552. break;
  14553. }
  14554. case value_t::boolean:
  14555. {
  14556. if (add_prefix)
  14557. {
  14558. oa->write_character(j.m_data.m_value.boolean
  14559. ? to_char_type('T')
  14560. : to_char_type('F'));
  14561. }
  14562. break;
  14563. }
  14564. case value_t::number_integer:
  14565. {
  14566. write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
  14567. break;
  14568. }
  14569. case value_t::number_unsigned:
  14570. {
  14571. write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
  14572. break;
  14573. }
  14574. case value_t::number_float:
  14575. {
  14576. write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
  14577. break;
  14578. }
  14579. case value_t::string:
  14580. {
  14581. if (add_prefix)
  14582. {
  14583. oa->write_character(to_char_type('S'));
  14584. }
  14585. write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
  14586. oa->write_characters(
  14587. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  14588. j.m_data.m_value.string->size());
  14589. break;
  14590. }
  14591. case value_t::array:
  14592. {
  14593. if (add_prefix)
  14594. {
  14595. oa->write_character(to_char_type('['));
  14596. }
  14597. bool prefix_required = true;
  14598. if (use_type && !j.m_data.m_value.array->empty())
  14599. {
  14600. JSON_ASSERT(use_count);
  14601. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  14602. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  14603. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  14604. {
  14605. return ubjson_prefix(v, use_bjdata) == first_prefix;
  14606. });
  14607. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  14608. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  14609. {
  14610. prefix_required = false;
  14611. oa->write_character(to_char_type('$'));
  14612. oa->write_character(first_prefix);
  14613. }
  14614. }
  14615. if (use_count)
  14616. {
  14617. oa->write_character(to_char_type('#'));
  14618. write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
  14619. }
  14620. for (const auto& el : *j.m_data.m_value.array)
  14621. {
  14622. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
  14623. }
  14624. if (!use_count)
  14625. {
  14626. oa->write_character(to_char_type(']'));
  14627. }
  14628. break;
  14629. }
  14630. case value_t::binary:
  14631. {
  14632. if (add_prefix)
  14633. {
  14634. oa->write_character(to_char_type('['));
  14635. }
  14636. if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
  14637. {
  14638. JSON_ASSERT(use_count);
  14639. oa->write_character(to_char_type('$'));
  14640. oa->write_character(bjdata_draft3 ? 'B' : 'U');
  14641. }
  14642. if (use_count)
  14643. {
  14644. oa->write_character(to_char_type('#'));
  14645. write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
  14646. }
  14647. if (use_type)
  14648. {
  14649. oa->write_characters(
  14650. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  14651. j.m_data.m_value.binary->size());
  14652. }
  14653. else
  14654. {
  14655. for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
  14656. {
  14657. oa->write_character(to_char_type(bjdata_draft3 ? 'B' : 'U'));
  14658. oa->write_character(j.m_data.m_value.binary->data()[i]);
  14659. }
  14660. }
  14661. if (!use_count)
  14662. {
  14663. oa->write_character(to_char_type(']'));
  14664. }
  14665. break;
  14666. }
  14667. case value_t::object:
  14668. {
  14669. 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())
  14670. {
  14671. 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)
  14672. {
  14673. break;
  14674. }
  14675. }
  14676. if (add_prefix)
  14677. {
  14678. oa->write_character(to_char_type('{'));
  14679. }
  14680. bool prefix_required = true;
  14681. if (use_type && !j.m_data.m_value.object->empty())
  14682. {
  14683. JSON_ASSERT(use_count);
  14684. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  14685. const bool same_prefix = std::all_of(j.begin(), j.end(),
  14686. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  14687. {
  14688. return ubjson_prefix(v, use_bjdata) == first_prefix;
  14689. });
  14690. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  14691. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  14692. {
  14693. prefix_required = false;
  14694. oa->write_character(to_char_type('$'));
  14695. oa->write_character(first_prefix);
  14696. }
  14697. }
  14698. if (use_count)
  14699. {
  14700. oa->write_character(to_char_type('#'));
  14701. write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
  14702. }
  14703. for (const auto& el : *j.m_data.m_value.object)
  14704. {
  14705. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  14706. oa->write_characters(
  14707. reinterpret_cast<const CharType*>(el.first.c_str()),
  14708. el.first.size());
  14709. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
  14710. }
  14711. if (!use_count)
  14712. {
  14713. oa->write_character(to_char_type('}'));
  14714. }
  14715. break;
  14716. }
  14717. case value_t::discarded:
  14718. default:
  14719. break;
  14720. }
  14721. }
  14722. private:
  14723. //////////
  14724. // BSON //
  14725. //////////
  14726. /*!
  14727. @return The size of a BSON document entry header, including the id marker
  14728. and the entry name size (and its null-terminator).
  14729. */
  14730. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  14731. {
  14732. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  14733. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  14734. {
  14735. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  14736. }
  14737. static_cast<void>(j);
  14738. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  14739. }
  14740. /*!
  14741. @brief Writes the given @a element_type and @a name to the output adapter
  14742. */
  14743. void write_bson_entry_header(const string_t& name,
  14744. const std::uint8_t element_type)
  14745. {
  14746. oa->write_character(to_char_type(element_type)); // boolean
  14747. oa->write_characters(
  14748. reinterpret_cast<const CharType*>(name.c_str()),
  14749. name.size() + 1u);
  14750. }
  14751. /*!
  14752. @brief Writes a BSON element with key @a name and boolean value @a value
  14753. */
  14754. void write_bson_boolean(const string_t& name,
  14755. const bool value)
  14756. {
  14757. write_bson_entry_header(name, 0x08);
  14758. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  14759. }
  14760. /*!
  14761. @brief Writes a BSON element with key @a name and double value @a value
  14762. */
  14763. void write_bson_double(const string_t& name,
  14764. const double value)
  14765. {
  14766. write_bson_entry_header(name, 0x01);
  14767. write_number<double>(value, true);
  14768. }
  14769. /*!
  14770. @return The size of the BSON-encoded string in @a value
  14771. */
  14772. static std::size_t calc_bson_string_size(const string_t& value)
  14773. {
  14774. return sizeof(std::int32_t) + value.size() + 1ul;
  14775. }
  14776. /*!
  14777. @brief Writes a BSON element with key @a name and string value @a value
  14778. */
  14779. void write_bson_string(const string_t& name,
  14780. const string_t& value)
  14781. {
  14782. write_bson_entry_header(name, 0x02);
  14783. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  14784. oa->write_characters(
  14785. reinterpret_cast<const CharType*>(value.c_str()),
  14786. value.size() + 1);
  14787. }
  14788. /*!
  14789. @brief Writes a BSON element with key @a name and null value
  14790. */
  14791. void write_bson_null(const string_t& name)
  14792. {
  14793. write_bson_entry_header(name, 0x0A);
  14794. }
  14795. /*!
  14796. @return The size of the BSON-encoded integer @a value
  14797. */
  14798. static std::size_t calc_bson_integer_size(const std::int64_t value)
  14799. {
  14800. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  14801. ? sizeof(std::int32_t)
  14802. : sizeof(std::int64_t);
  14803. }
  14804. /*!
  14805. @brief Writes a BSON element with key @a name and integer @a value
  14806. */
  14807. void write_bson_integer(const string_t& name,
  14808. const std::int64_t value)
  14809. {
  14810. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  14811. {
  14812. write_bson_entry_header(name, 0x10); // int32
  14813. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  14814. }
  14815. else
  14816. {
  14817. write_bson_entry_header(name, 0x12); // int64
  14818. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  14819. }
  14820. }
  14821. /*!
  14822. @return The size of the BSON-encoded unsigned integer in @a j
  14823. */
  14824. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  14825. {
  14826. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14827. ? sizeof(std::int32_t)
  14828. : sizeof(std::int64_t);
  14829. }
  14830. /*!
  14831. @brief Writes a BSON element with key @a name and unsigned @a value
  14832. */
  14833. void write_bson_unsigned(const string_t& name,
  14834. const BasicJsonType& j)
  14835. {
  14836. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14837. {
  14838. write_bson_entry_header(name, 0x10 /* int32 */);
  14839. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
  14840. }
  14841. else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14842. {
  14843. write_bson_entry_header(name, 0x12 /* int64 */);
  14844. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
  14845. }
  14846. else
  14847. {
  14848. write_bson_entry_header(name, 0x11 /* uint64 */);
  14849. write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true);
  14850. }
  14851. }
  14852. /*!
  14853. @brief Writes a BSON element with key @a name and object @a value
  14854. */
  14855. void write_bson_object_entry(const string_t& name,
  14856. const typename BasicJsonType::object_t& value)
  14857. {
  14858. write_bson_entry_header(name, 0x03); // object
  14859. write_bson_object(value);
  14860. }
  14861. /*!
  14862. @return The size of the BSON-encoded array @a value
  14863. */
  14864. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  14865. {
  14866. std::size_t array_index = 0ul;
  14867. 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)
  14868. {
  14869. return result + calc_bson_element_size(std::to_string(array_index++), el);
  14870. });
  14871. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  14872. }
  14873. /*!
  14874. @return The size of the BSON-encoded binary array @a value
  14875. */
  14876. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  14877. {
  14878. return sizeof(std::int32_t) + value.size() + 1ul;
  14879. }
  14880. /*!
  14881. @brief Writes a BSON element with key @a name and array @a value
  14882. */
  14883. void write_bson_array(const string_t& name,
  14884. const typename BasicJsonType::array_t& value)
  14885. {
  14886. write_bson_entry_header(name, 0x04); // array
  14887. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  14888. std::size_t array_index = 0ul;
  14889. for (const auto& el : value)
  14890. {
  14891. write_bson_element(std::to_string(array_index++), el);
  14892. }
  14893. oa->write_character(to_char_type(0x00));
  14894. }
  14895. /*!
  14896. @brief Writes a BSON element with key @a name and binary value @a value
  14897. */
  14898. void write_bson_binary(const string_t& name,
  14899. const binary_t& value)
  14900. {
  14901. write_bson_entry_header(name, 0x05);
  14902. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  14903. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  14904. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  14905. }
  14906. /*!
  14907. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  14908. @return The calculated size for the BSON document entry for @a j with the given @a name.
  14909. */
  14910. static std::size_t calc_bson_element_size(const string_t& name,
  14911. const BasicJsonType& j)
  14912. {
  14913. const auto header_size = calc_bson_entry_header_size(name, j);
  14914. switch (j.type())
  14915. {
  14916. case value_t::object:
  14917. return header_size + calc_bson_object_size(*j.m_data.m_value.object);
  14918. case value_t::array:
  14919. return header_size + calc_bson_array_size(*j.m_data.m_value.array);
  14920. case value_t::binary:
  14921. return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
  14922. case value_t::boolean:
  14923. return header_size + 1ul;
  14924. case value_t::number_float:
  14925. return header_size + 8ul;
  14926. case value_t::number_integer:
  14927. return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
  14928. case value_t::number_unsigned:
  14929. return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
  14930. case value_t::string:
  14931. return header_size + calc_bson_string_size(*j.m_data.m_value.string);
  14932. case value_t::null:
  14933. return header_size + 0ul;
  14934. // LCOV_EXCL_START
  14935. case value_t::discarded:
  14936. default:
  14937. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14938. return 0ul;
  14939. // LCOV_EXCL_STOP
  14940. }
  14941. }
  14942. /*!
  14943. @brief Serializes the JSON value @a j to BSON and associates it with the
  14944. key @a name.
  14945. @param name The name to associate with the JSON entity @a j within the
  14946. current BSON document
  14947. */
  14948. void write_bson_element(const string_t& name,
  14949. const BasicJsonType& j)
  14950. {
  14951. switch (j.type())
  14952. {
  14953. case value_t::object:
  14954. return write_bson_object_entry(name, *j.m_data.m_value.object);
  14955. case value_t::array:
  14956. return write_bson_array(name, *j.m_data.m_value.array);
  14957. case value_t::binary:
  14958. return write_bson_binary(name, *j.m_data.m_value.binary);
  14959. case value_t::boolean:
  14960. return write_bson_boolean(name, j.m_data.m_value.boolean);
  14961. case value_t::number_float:
  14962. return write_bson_double(name, j.m_data.m_value.number_float);
  14963. case value_t::number_integer:
  14964. return write_bson_integer(name, j.m_data.m_value.number_integer);
  14965. case value_t::number_unsigned:
  14966. return write_bson_unsigned(name, j);
  14967. case value_t::string:
  14968. return write_bson_string(name, *j.m_data.m_value.string);
  14969. case value_t::null:
  14970. return write_bson_null(name);
  14971. // LCOV_EXCL_START
  14972. case value_t::discarded:
  14973. default:
  14974. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14975. return;
  14976. // LCOV_EXCL_STOP
  14977. }
  14978. }
  14979. /*!
  14980. @brief Calculates the size of the BSON serialization of the given
  14981. JSON-object @a j.
  14982. @param[in] value JSON value to serialize
  14983. @pre value.type() == value_t::object
  14984. */
  14985. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14986. {
  14987. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14988. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14989. {
  14990. return result += calc_bson_element_size(el.first, el.second);
  14991. });
  14992. return sizeof(std::int32_t) + document_size + 1ul;
  14993. }
  14994. /*!
  14995. @param[in] value JSON value to serialize
  14996. @pre value.type() == value_t::object
  14997. */
  14998. void write_bson_object(const typename BasicJsonType::object_t& value)
  14999. {
  15000. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  15001. for (const auto& el : value)
  15002. {
  15003. write_bson_element(el.first, el.second);
  15004. }
  15005. oa->write_character(to_char_type(0x00));
  15006. }
  15007. //////////
  15008. // CBOR //
  15009. //////////
  15010. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  15011. {
  15012. return to_char_type(0xFA); // Single-Precision Float
  15013. }
  15014. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  15015. {
  15016. return to_char_type(0xFB); // Double-Precision Float
  15017. }
  15018. /////////////
  15019. // MsgPack //
  15020. /////////////
  15021. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  15022. {
  15023. return to_char_type(0xCA); // float 32
  15024. }
  15025. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  15026. {
  15027. return to_char_type(0xCB); // float 64
  15028. }
  15029. ////////////
  15030. // UBJSON //
  15031. ////////////
  15032. // UBJSON: write number (floating point)
  15033. template<typename NumberType, typename std::enable_if<
  15034. std::is_floating_point<NumberType>::value, int>::type = 0>
  15035. void write_number_with_ubjson_prefix(const NumberType n,
  15036. const bool add_prefix,
  15037. const bool use_bjdata)
  15038. {
  15039. if (add_prefix)
  15040. {
  15041. oa->write_character(get_ubjson_float_prefix(n));
  15042. }
  15043. write_number(n, use_bjdata);
  15044. }
  15045. // UBJSON: write number (unsigned integer)
  15046. template<typename NumberType, typename std::enable_if<
  15047. std::is_unsigned<NumberType>::value, int>::type = 0>
  15048. void write_number_with_ubjson_prefix(const NumberType n,
  15049. const bool add_prefix,
  15050. const bool use_bjdata)
  15051. {
  15052. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  15053. {
  15054. if (add_prefix)
  15055. {
  15056. oa->write_character(to_char_type('i')); // int8
  15057. }
  15058. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  15059. }
  15060. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  15061. {
  15062. if (add_prefix)
  15063. {
  15064. oa->write_character(to_char_type('U')); // uint8
  15065. }
  15066. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  15067. }
  15068. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  15069. {
  15070. if (add_prefix)
  15071. {
  15072. oa->write_character(to_char_type('I')); // int16
  15073. }
  15074. write_number(static_cast<std::int16_t>(n), use_bjdata);
  15075. }
  15076. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  15077. {
  15078. if (add_prefix)
  15079. {
  15080. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  15081. }
  15082. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  15083. }
  15084. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  15085. {
  15086. if (add_prefix)
  15087. {
  15088. oa->write_character(to_char_type('l')); // int32
  15089. }
  15090. write_number(static_cast<std::int32_t>(n), use_bjdata);
  15091. }
  15092. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  15093. {
  15094. if (add_prefix)
  15095. {
  15096. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  15097. }
  15098. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  15099. }
  15100. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  15101. {
  15102. if (add_prefix)
  15103. {
  15104. oa->write_character(to_char_type('L')); // int64
  15105. }
  15106. write_number(static_cast<std::int64_t>(n), use_bjdata);
  15107. }
  15108. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  15109. {
  15110. if (add_prefix)
  15111. {
  15112. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  15113. }
  15114. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  15115. }
  15116. else
  15117. {
  15118. if (add_prefix)
  15119. {
  15120. oa->write_character(to_char_type('H')); // high-precision number
  15121. }
  15122. const auto number = BasicJsonType(n).dump();
  15123. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  15124. for (std::size_t i = 0; i < number.size(); ++i)
  15125. {
  15126. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  15127. }
  15128. }
  15129. }
  15130. // UBJSON: write number (signed integer)
  15131. template < typename NumberType, typename std::enable_if <
  15132. std::is_signed<NumberType>::value&&
  15133. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  15134. void write_number_with_ubjson_prefix(const NumberType n,
  15135. const bool add_prefix,
  15136. const bool use_bjdata)
  15137. {
  15138. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  15139. {
  15140. if (add_prefix)
  15141. {
  15142. oa->write_character(to_char_type('i')); // int8
  15143. }
  15144. write_number(static_cast<std::int8_t>(n), use_bjdata);
  15145. }
  15146. 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)()))
  15147. {
  15148. if (add_prefix)
  15149. {
  15150. oa->write_character(to_char_type('U')); // uint8
  15151. }
  15152. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  15153. }
  15154. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  15155. {
  15156. if (add_prefix)
  15157. {
  15158. oa->write_character(to_char_type('I')); // int16
  15159. }
  15160. write_number(static_cast<std::int16_t>(n), use_bjdata);
  15161. }
  15162. 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)())))
  15163. {
  15164. if (add_prefix)
  15165. {
  15166. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  15167. }
  15168. write_number(static_cast<uint16_t>(n), use_bjdata);
  15169. }
  15170. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  15171. {
  15172. if (add_prefix)
  15173. {
  15174. oa->write_character(to_char_type('l')); // int32
  15175. }
  15176. write_number(static_cast<std::int32_t>(n), use_bjdata);
  15177. }
  15178. 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)())))
  15179. {
  15180. if (add_prefix)
  15181. {
  15182. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  15183. }
  15184. write_number(static_cast<uint32_t>(n), use_bjdata);
  15185. }
  15186. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  15187. {
  15188. if (add_prefix)
  15189. {
  15190. oa->write_character(to_char_type('L')); // int64
  15191. }
  15192. write_number(static_cast<std::int64_t>(n), use_bjdata);
  15193. }
  15194. // LCOV_EXCL_START
  15195. else
  15196. {
  15197. if (add_prefix)
  15198. {
  15199. oa->write_character(to_char_type('H')); // high-precision number
  15200. }
  15201. const auto number = BasicJsonType(n).dump();
  15202. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  15203. for (std::size_t i = 0; i < number.size(); ++i)
  15204. {
  15205. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  15206. }
  15207. }
  15208. // LCOV_EXCL_STOP
  15209. }
  15210. /*!
  15211. @brief determine the type prefix of container values
  15212. */
  15213. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  15214. {
  15215. switch (j.type())
  15216. {
  15217. case value_t::null:
  15218. return 'Z';
  15219. case value_t::boolean:
  15220. return j.m_data.m_value.boolean ? 'T' : 'F';
  15221. case value_t::number_integer:
  15222. {
  15223. 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)())
  15224. {
  15225. return 'i';
  15226. }
  15227. 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)())
  15228. {
  15229. return 'U';
  15230. }
  15231. 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)())
  15232. {
  15233. return 'I';
  15234. }
  15235. 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)()))
  15236. {
  15237. return 'u';
  15238. }
  15239. 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)())
  15240. {
  15241. return 'l';
  15242. }
  15243. 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)()))
  15244. {
  15245. return 'm';
  15246. }
  15247. 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)())
  15248. {
  15249. return 'L';
  15250. }
  15251. // anything else is treated as a high-precision number
  15252. return 'H'; // LCOV_EXCL_LINE
  15253. }
  15254. case value_t::number_unsigned:
  15255. {
  15256. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  15257. {
  15258. return 'i';
  15259. }
  15260. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  15261. {
  15262. return 'U';
  15263. }
  15264. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  15265. {
  15266. return 'I';
  15267. }
  15268. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  15269. {
  15270. return 'u';
  15271. }
  15272. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  15273. {
  15274. return 'l';
  15275. }
  15276. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  15277. {
  15278. return 'm';
  15279. }
  15280. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  15281. {
  15282. return 'L';
  15283. }
  15284. if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  15285. {
  15286. return 'M';
  15287. }
  15288. // anything else is treated as a high-precision number
  15289. return 'H'; // LCOV_EXCL_LINE
  15290. }
  15291. case value_t::number_float:
  15292. return get_ubjson_float_prefix(j.m_data.m_value.number_float);
  15293. case value_t::string:
  15294. return 'S';
  15295. case value_t::array: // fallthrough
  15296. case value_t::binary:
  15297. return '[';
  15298. case value_t::object:
  15299. return '{';
  15300. case value_t::discarded:
  15301. default: // discarded values
  15302. return 'N';
  15303. }
  15304. }
  15305. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  15306. {
  15307. return 'd'; // float 32
  15308. }
  15309. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  15310. {
  15311. return 'D'; // float 64
  15312. }
  15313. /*!
  15314. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  15315. */
  15316. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type, const bjdata_version_t bjdata_version)
  15317. {
  15318. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  15319. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'},
  15320. {"char", 'C'}, {"byte", 'B'}
  15321. };
  15322. string_t key = "_ArrayType_";
  15323. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  15324. if (it == bjdtype.end())
  15325. {
  15326. return true;
  15327. }
  15328. CharType dtype = it->second;
  15329. key = "_ArraySize_";
  15330. std::size_t len = (value.at(key).empty() ? 0 : 1);
  15331. for (const auto& el : value.at(key))
  15332. {
  15333. len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
  15334. }
  15335. key = "_ArrayData_";
  15336. if (value.at(key).size() != len)
  15337. {
  15338. return true;
  15339. }
  15340. oa->write_character('[');
  15341. oa->write_character('$');
  15342. oa->write_character(dtype);
  15343. oa->write_character('#');
  15344. key = "_ArraySize_";
  15345. write_ubjson(value.at(key), use_count, use_type, true, true, bjdata_version);
  15346. key = "_ArrayData_";
  15347. if (dtype == 'U' || dtype == 'C' || dtype == 'B')
  15348. {
  15349. for (const auto& el : value.at(key))
  15350. {
  15351. write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
  15352. }
  15353. }
  15354. else if (dtype == 'i')
  15355. {
  15356. for (const auto& el : value.at(key))
  15357. {
  15358. write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
  15359. }
  15360. }
  15361. else if (dtype == 'u')
  15362. {
  15363. for (const auto& el : value.at(key))
  15364. {
  15365. write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
  15366. }
  15367. }
  15368. else if (dtype == 'I')
  15369. {
  15370. for (const auto& el : value.at(key))
  15371. {
  15372. write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
  15373. }
  15374. }
  15375. else if (dtype == 'm')
  15376. {
  15377. for (const auto& el : value.at(key))
  15378. {
  15379. write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
  15380. }
  15381. }
  15382. else if (dtype == 'l')
  15383. {
  15384. for (const auto& el : value.at(key))
  15385. {
  15386. write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
  15387. }
  15388. }
  15389. else if (dtype == 'M')
  15390. {
  15391. for (const auto& el : value.at(key))
  15392. {
  15393. write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
  15394. }
  15395. }
  15396. else if (dtype == 'L')
  15397. {
  15398. for (const auto& el : value.at(key))
  15399. {
  15400. write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
  15401. }
  15402. }
  15403. else if (dtype == 'd')
  15404. {
  15405. for (const auto& el : value.at(key))
  15406. {
  15407. write_number(static_cast<float>(el.m_data.m_value.number_float), true);
  15408. }
  15409. }
  15410. else if (dtype == 'D')
  15411. {
  15412. for (const auto& el : value.at(key))
  15413. {
  15414. write_number(static_cast<double>(el.m_data.m_value.number_float), true);
  15415. }
  15416. }
  15417. return false;
  15418. }
  15419. ///////////////////////
  15420. // Utility functions //
  15421. ///////////////////////
  15422. /*
  15423. @brief write a number to output input
  15424. @param[in] n number of type @a NumberType
  15425. @param[in] OutputIsLittleEndian Set to true if output data is
  15426. required to be little endian
  15427. @tparam NumberType the type of the number
  15428. @note This function needs to respect the system's endianness, because bytes
  15429. in CBOR, MessagePack, and UBJSON are stored in network order (big
  15430. endian) and therefore need reordering on little endian systems.
  15431. On the other hand, BSON and BJData use little endian and should reorder
  15432. on big endian systems.
  15433. */
  15434. template<typename NumberType>
  15435. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  15436. {
  15437. // step 1: write the number to an array of length NumberType
  15438. std::array<CharType, sizeof(NumberType)> vec{};
  15439. std::memcpy(vec.data(), &n, sizeof(NumberType));
  15440. // step 2: write the array to output (with possible reordering)
  15441. if (is_little_endian != OutputIsLittleEndian)
  15442. {
  15443. // reverse byte order prior to conversion if necessary
  15444. std::reverse(vec.begin(), vec.end());
  15445. }
  15446. oa->write_characters(vec.data(), sizeof(NumberType));
  15447. }
  15448. void write_compact_float(const number_float_t n, detail::input_format_t format)
  15449. {
  15450. #ifdef __GNUC__
  15451. #pragma GCC diagnostic push
  15452. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15453. #endif
  15454. if (!std::isfinite(n) || ((static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  15455. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  15456. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))))
  15457. {
  15458. oa->write_character(format == detail::input_format_t::cbor
  15459. ? get_cbor_float_prefix(static_cast<float>(n))
  15460. : get_msgpack_float_prefix(static_cast<float>(n)));
  15461. write_number(static_cast<float>(n));
  15462. }
  15463. else
  15464. {
  15465. oa->write_character(format == detail::input_format_t::cbor
  15466. ? get_cbor_float_prefix(n)
  15467. : get_msgpack_float_prefix(n));
  15468. write_number(n);
  15469. }
  15470. #ifdef __GNUC__
  15471. #pragma GCC diagnostic pop
  15472. #endif
  15473. }
  15474. public:
  15475. // The following to_char_type functions are implement the conversion
  15476. // between uint8_t and CharType. In case CharType is not unsigned,
  15477. // such a conversion is required to allow values greater than 128.
  15478. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  15479. template < typename C = CharType,
  15480. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  15481. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  15482. {
  15483. return *reinterpret_cast<char*>(&x);
  15484. }
  15485. template < typename C = CharType,
  15486. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  15487. static CharType to_char_type(std::uint8_t x) noexcept
  15488. {
  15489. // The std::is_trivial trait is deprecated in C++26. The replacement is to use
  15490. // std::is_trivially_copyable and std::is_trivially_default_constructible.
  15491. // However, some older library implementations support std::is_trivial
  15492. // but not all the std::is_trivially_* traits.
  15493. // Since detecting full support across all libraries is difficult,
  15494. // we use std::is_trivial unless we are using a standard where it has been deprecated.
  15495. // For more details, see: https://github.com/nlohmann/json/pull/4775#issuecomment-2884361627
  15496. #ifdef JSON_HAS_CPP_26
  15497. static_assert(std::is_trivially_copyable<CharType>::value, "CharType must be trivially copyable");
  15498. static_assert(std::is_trivially_default_constructible<CharType>::value, "CharType must be trivially default constructible");
  15499. #else
  15500. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  15501. #endif
  15502. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  15503. CharType result;
  15504. std::memcpy(&result, &x, sizeof(x));
  15505. return result;
  15506. }
  15507. template<typename C = CharType,
  15508. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  15509. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  15510. {
  15511. return x;
  15512. }
  15513. template < typename InputCharType, typename C = CharType,
  15514. enable_if_t <
  15515. std::is_signed<C>::value &&
  15516. std::is_signed<char>::value &&
  15517. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  15518. > * = nullptr >
  15519. static constexpr CharType to_char_type(InputCharType x) noexcept
  15520. {
  15521. return x;
  15522. }
  15523. private:
  15524. /// whether we can assume little endianness
  15525. const bool is_little_endian = little_endianness();
  15526. /// the output
  15527. output_adapter_t<CharType> oa = nullptr;
  15528. };
  15529. } // namespace detail
  15530. NLOHMANN_JSON_NAMESPACE_END
  15531. // #include <nlohmann/detail/output/output_adapters.hpp>
  15532. // #include <nlohmann/detail/output/serializer.hpp>
  15533. // __ _____ _____ _____
  15534. // __| | __| | | | JSON for Modern C++
  15535. // | | |__ | | | | | | version 3.12.0
  15536. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  15537. //
  15538. // SPDX-FileCopyrightText: 2008, 2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  15539. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  15540. // SPDX-License-Identifier: MIT
  15541. #include <algorithm> // reverse, remove, fill, find, none_of
  15542. #include <array> // array
  15543. #include <clocale> // localeconv, lconv
  15544. #include <cmath> // labs, isfinite, isnan, signbit
  15545. #include <cstddef> // size_t, ptrdiff_t
  15546. #include <cstdint> // uint8_t
  15547. #include <cstdio> // snprintf
  15548. #include <limits> // numeric_limits
  15549. #include <string> // string, char_traits
  15550. #include <iomanip> // setfill, setw
  15551. #include <type_traits> // is_same
  15552. #include <utility> // move
  15553. // #include <nlohmann/detail/conversions/to_chars.hpp>
  15554. // __ _____ _____ _____
  15555. // __| | __| | | | JSON for Modern C++
  15556. // | | |__ | | | | | | version 3.12.0
  15557. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  15558. //
  15559. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  15560. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  15561. // SPDX-License-Identifier: MIT
  15562. #include <array> // array
  15563. #include <cmath> // signbit, isfinite
  15564. #include <cstdint> // intN_t, uintN_t
  15565. #include <cstring> // memcpy, memmove
  15566. #include <limits> // numeric_limits
  15567. #include <type_traits> // conditional
  15568. // #include <nlohmann/detail/macro_scope.hpp>
  15569. NLOHMANN_JSON_NAMESPACE_BEGIN
  15570. namespace detail
  15571. {
  15572. /*!
  15573. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  15574. conversion.
  15575. This implementation is a slightly modified version of the reference
  15576. implementation which may be obtained from
  15577. http://florian.loitsch.com/publications (bench.tar.gz).
  15578. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  15579. For a detailed description of the algorithm see:
  15580. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  15581. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  15582. Language Design and Implementation, PLDI 2010
  15583. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  15584. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  15585. Design and Implementation, PLDI 1996
  15586. */
  15587. namespace dtoa_impl
  15588. {
  15589. template<typename Target, typename Source>
  15590. Target reinterpret_bits(const Source source)
  15591. {
  15592. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  15593. Target target;
  15594. std::memcpy(&target, &source, sizeof(Source));
  15595. return target;
  15596. }
  15597. struct diyfp // f * 2^e
  15598. {
  15599. static constexpr int kPrecision = 64; // = q
  15600. std::uint64_t f = 0;
  15601. int e = 0;
  15602. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  15603. /*!
  15604. @brief returns x - y
  15605. @pre x.e == y.e and x.f >= y.f
  15606. */
  15607. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  15608. {
  15609. JSON_ASSERT(x.e == y.e);
  15610. JSON_ASSERT(x.f >= y.f);
  15611. return {x.f - y.f, x.e};
  15612. }
  15613. /*!
  15614. @brief returns x * y
  15615. @note The result is rounded. (Only the upper q bits are returned.)
  15616. */
  15617. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  15618. {
  15619. static_assert(kPrecision == 64, "internal error");
  15620. // Computes:
  15621. // f = round((x.f * y.f) / 2^q)
  15622. // e = x.e + y.e + q
  15623. // Emulate the 64-bit * 64-bit multiplication:
  15624. //
  15625. // p = u * v
  15626. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  15627. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  15628. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  15629. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  15630. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  15631. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  15632. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  15633. //
  15634. // (Since Q might be larger than 2^32 - 1)
  15635. //
  15636. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  15637. //
  15638. // (Q_hi + H does not overflow a 64-bit int)
  15639. //
  15640. // = p_lo + 2^64 p_hi
  15641. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  15642. const std::uint64_t u_hi = x.f >> 32u;
  15643. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  15644. const std::uint64_t v_hi = y.f >> 32u;
  15645. const std::uint64_t p0 = u_lo * v_lo;
  15646. const std::uint64_t p1 = u_lo * v_hi;
  15647. const std::uint64_t p2 = u_hi * v_lo;
  15648. const std::uint64_t p3 = u_hi * v_hi;
  15649. const std::uint64_t p0_hi = p0 >> 32u;
  15650. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  15651. const std::uint64_t p1_hi = p1 >> 32u;
  15652. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  15653. const std::uint64_t p2_hi = p2 >> 32u;
  15654. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  15655. // The full product might now be computed as
  15656. //
  15657. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  15658. // p_lo = p0_lo + (Q << 32)
  15659. //
  15660. // But in this particular case here, the full p_lo is not required.
  15661. // Effectively, we only need to add the highest bit in p_lo to p_hi (and
  15662. // Q_hi + 1 does not overflow).
  15663. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  15664. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  15665. return {h, x.e + y.e + 64};
  15666. }
  15667. /*!
  15668. @brief normalize x such that the significand is >= 2^(q-1)
  15669. @pre x.f != 0
  15670. */
  15671. static diyfp normalize(diyfp x) noexcept
  15672. {
  15673. JSON_ASSERT(x.f != 0);
  15674. while ((x.f >> 63u) == 0)
  15675. {
  15676. x.f <<= 1u;
  15677. x.e--;
  15678. }
  15679. return x;
  15680. }
  15681. /*!
  15682. @brief normalize x such that the result has the exponent E
  15683. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  15684. */
  15685. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  15686. {
  15687. const int delta = x.e - target_exponent;
  15688. JSON_ASSERT(delta >= 0);
  15689. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  15690. return {x.f << delta, target_exponent};
  15691. }
  15692. };
  15693. struct boundaries
  15694. {
  15695. diyfp w;
  15696. diyfp minus;
  15697. diyfp plus;
  15698. };
  15699. /*!
  15700. Compute the (normalized) diyfp representing the input number 'value' and its
  15701. boundaries.
  15702. @pre value must be finite and positive
  15703. */
  15704. template<typename FloatType>
  15705. boundaries compute_boundaries(FloatType value)
  15706. {
  15707. JSON_ASSERT(std::isfinite(value));
  15708. JSON_ASSERT(value > 0);
  15709. // Convert the IEEE representation into a diyfp.
  15710. //
  15711. // If v is denormal:
  15712. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  15713. // If v is normalized:
  15714. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  15715. static_assert(std::numeric_limits<FloatType>::is_iec559,
  15716. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  15717. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  15718. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  15719. constexpr int kMinExp = 1 - kBias;
  15720. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  15721. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  15722. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  15723. const std::uint64_t E = bits >> (kPrecision - 1);
  15724. const std::uint64_t F = bits & (kHiddenBit - 1);
  15725. const bool is_denormal = E == 0;
  15726. const diyfp v = is_denormal
  15727. ? diyfp(F, kMinExp)
  15728. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  15729. // Compute the boundaries m- and m+ of the floating-point value
  15730. // v = f * 2^e.
  15731. //
  15732. // Determine v- and v+, the floating-point predecessor and successor of v,
  15733. // respectively.
  15734. //
  15735. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  15736. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  15737. //
  15738. // v+ = v + 2^e
  15739. //
  15740. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  15741. // between m- and m+ round to v, regardless of how the input rounding
  15742. // algorithm breaks ties.
  15743. //
  15744. // ---+-------------+-------------+-------------+-------------+--- (A)
  15745. // v- m- v m+ v+
  15746. //
  15747. // -----------------+------+------+-------------+-------------+--- (B)
  15748. // v- m- v m+ v+
  15749. const bool lower_boundary_is_closer = F == 0 && E > 1;
  15750. const diyfp m_plus = diyfp((2 * v.f) + 1, v.e - 1);
  15751. const diyfp m_minus = lower_boundary_is_closer
  15752. ? diyfp((4 * v.f) - 1, v.e - 2) // (B)
  15753. : diyfp((2 * v.f) - 1, v.e - 1); // (A)
  15754. // Determine the normalized w+ = m+.
  15755. const diyfp w_plus = diyfp::normalize(m_plus);
  15756. // Determine w- = m- such that e_(w-) = e_(w+).
  15757. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  15758. return {diyfp::normalize(v), w_minus, w_plus};
  15759. }
  15760. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  15761. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  15762. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  15763. //
  15764. // alpha <= e = e_c + e_w + q <= gamma
  15765. //
  15766. // or
  15767. //
  15768. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  15769. // <= f_c * f_w * 2^gamma
  15770. //
  15771. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  15772. //
  15773. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  15774. //
  15775. // or
  15776. //
  15777. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  15778. //
  15779. // The choice of (alpha,gamma) determines the size of the table and the form of
  15780. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  15781. // in practice:
  15782. //
  15783. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  15784. // processed independently: An integral part p1, and a fractional part p2:
  15785. //
  15786. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  15787. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  15788. // = p1 + p2 * 2^e
  15789. //
  15790. // The conversion of p1 into decimal form requires a series of divisions and
  15791. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  15792. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  15793. // achieved by choosing
  15794. //
  15795. // -e >= 32 or e <= -32 := gamma
  15796. //
  15797. // In order to convert the fractional part
  15798. //
  15799. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15800. //
  15801. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  15802. // d[-i] are extracted in order:
  15803. //
  15804. // (10 * p2) div 2^-e = d[-1]
  15805. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  15806. //
  15807. // The multiplication by 10 must not overflow. It is sufficient to choose
  15808. //
  15809. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  15810. //
  15811. // Since p2 = f mod 2^-e < 2^-e,
  15812. //
  15813. // -e <= 60 or e >= -60 := alpha
  15814. constexpr int kAlpha = -60;
  15815. constexpr int kGamma = -32;
  15816. struct cached_power // c = f * 2^e ~= 10^k
  15817. {
  15818. std::uint64_t f;
  15819. int e;
  15820. int k;
  15821. };
  15822. /*!
  15823. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  15824. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  15825. satisfies (Definition 3.2 from [1])
  15826. alpha <= e_c + e + q <= gamma.
  15827. */
  15828. inline cached_power get_cached_power_for_binary_exponent(int e)
  15829. {
  15830. // Now
  15831. //
  15832. // alpha <= e_c + e + q <= gamma (1)
  15833. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  15834. //
  15835. // and since the c's are normalized, 2^(q-1) <= f_c,
  15836. //
  15837. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  15838. // ==> 2^(alpha - e - 1) <= c
  15839. //
  15840. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  15841. //
  15842. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  15843. // = ceil( (alpha - e - 1) * log_10(2) )
  15844. //
  15845. // From the paper:
  15846. // "In theory the result of the procedure could be wrong since c is rounded,
  15847. // and the computation itself is approximated [...]. In practice, however,
  15848. // this simple function is sufficient."
  15849. //
  15850. // For IEEE double precision floating-point numbers converted into
  15851. // normalized diyfp's w = f * 2^e, with q = 64,
  15852. //
  15853. // e >= -1022 (min IEEE exponent)
  15854. // -52 (p - 1)
  15855. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  15856. // -11 (normalize the diyfp)
  15857. // = -1137
  15858. //
  15859. // and
  15860. //
  15861. // e <= +1023 (max IEEE exponent)
  15862. // -52 (p - 1)
  15863. // -11 (normalize the diyfp)
  15864. // = 960
  15865. //
  15866. // This binary exponent range [-1137,960] results in a decimal exponent
  15867. // range [-307,324]. One does not need to store a cached power for each
  15868. // k in this range. For each such k it suffices to find a cached power
  15869. // such that the exponent of the product lies in [alpha,gamma].
  15870. // This implies that the difference of the decimal exponents of adjacent
  15871. // table entries must be less than or equal to
  15872. //
  15873. // floor( (gamma - alpha) * log_10(2) ) = 8.
  15874. //
  15875. // (A smaller distance gamma-alpha would require a larger table.)
  15876. // NB:
  15877. // Actually, this function returns c, such that -60 <= e_c + e + 64 <= -34.
  15878. constexpr int kCachedPowersMinDecExp = -300;
  15879. constexpr int kCachedPowersDecStep = 8;
  15880. static constexpr std::array<cached_power, 79> kCachedPowers =
  15881. {
  15882. {
  15883. { 0xAB70FE17C79AC6CA, -1060, -300 },
  15884. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  15885. { 0xBE5691EF416BD60C, -1007, -284 },
  15886. { 0x8DD01FAD907FFC3C, -980, -276 },
  15887. { 0xD3515C2831559A83, -954, -268 },
  15888. { 0x9D71AC8FADA6C9B5, -927, -260 },
  15889. { 0xEA9C227723EE8BCB, -901, -252 },
  15890. { 0xAECC49914078536D, -874, -244 },
  15891. { 0x823C12795DB6CE57, -847, -236 },
  15892. { 0xC21094364DFB5637, -821, -228 },
  15893. { 0x9096EA6F3848984F, -794, -220 },
  15894. { 0xD77485CB25823AC7, -768, -212 },
  15895. { 0xA086CFCD97BF97F4, -741, -204 },
  15896. { 0xEF340A98172AACE5, -715, -196 },
  15897. { 0xB23867FB2A35B28E, -688, -188 },
  15898. { 0x84C8D4DFD2C63F3B, -661, -180 },
  15899. { 0xC5DD44271AD3CDBA, -635, -172 },
  15900. { 0x936B9FCEBB25C996, -608, -164 },
  15901. { 0xDBAC6C247D62A584, -582, -156 },
  15902. { 0xA3AB66580D5FDAF6, -555, -148 },
  15903. { 0xF3E2F893DEC3F126, -529, -140 },
  15904. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  15905. { 0x87625F056C7C4A8B, -475, -124 },
  15906. { 0xC9BCFF6034C13053, -449, -116 },
  15907. { 0x964E858C91BA2655, -422, -108 },
  15908. { 0xDFF9772470297EBD, -396, -100 },
  15909. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  15910. { 0xF8A95FCF88747D94, -343, -84 },
  15911. { 0xB94470938FA89BCF, -316, -76 },
  15912. { 0x8A08F0F8BF0F156B, -289, -68 },
  15913. { 0xCDB02555653131B6, -263, -60 },
  15914. { 0x993FE2C6D07B7FAC, -236, -52 },
  15915. { 0xE45C10C42A2B3B06, -210, -44 },
  15916. { 0xAA242499697392D3, -183, -36 },
  15917. { 0xFD87B5F28300CA0E, -157, -28 },
  15918. { 0xBCE5086492111AEB, -130, -20 },
  15919. { 0x8CBCCC096F5088CC, -103, -12 },
  15920. { 0xD1B71758E219652C, -77, -4 },
  15921. { 0x9C40000000000000, -50, 4 },
  15922. { 0xE8D4A51000000000, -24, 12 },
  15923. { 0xAD78EBC5AC620000, 3, 20 },
  15924. { 0x813F3978F8940984, 30, 28 },
  15925. { 0xC097CE7BC90715B3, 56, 36 },
  15926. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15927. { 0xD5D238A4ABE98068, 109, 52 },
  15928. { 0x9F4F2726179A2245, 136, 60 },
  15929. { 0xED63A231D4C4FB27, 162, 68 },
  15930. { 0xB0DE65388CC8ADA8, 189, 76 },
  15931. { 0x83C7088E1AAB65DB, 216, 84 },
  15932. { 0xC45D1DF942711D9A, 242, 92 },
  15933. { 0x924D692CA61BE758, 269, 100 },
  15934. { 0xDA01EE641A708DEA, 295, 108 },
  15935. { 0xA26DA3999AEF774A, 322, 116 },
  15936. { 0xF209787BB47D6B85, 348, 124 },
  15937. { 0xB454E4A179DD1877, 375, 132 },
  15938. { 0x865B86925B9BC5C2, 402, 140 },
  15939. { 0xC83553C5C8965D3D, 428, 148 },
  15940. { 0x952AB45CFA97A0B3, 455, 156 },
  15941. { 0xDE469FBD99A05FE3, 481, 164 },
  15942. { 0xA59BC234DB398C25, 508, 172 },
  15943. { 0xF6C69A72A3989F5C, 534, 180 },
  15944. { 0xB7DCBF5354E9BECE, 561, 188 },
  15945. { 0x88FCF317F22241E2, 588, 196 },
  15946. { 0xCC20CE9BD35C78A5, 614, 204 },
  15947. { 0x98165AF37B2153DF, 641, 212 },
  15948. { 0xE2A0B5DC971F303A, 667, 220 },
  15949. { 0xA8D9D1535CE3B396, 694, 228 },
  15950. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15951. { 0xBB764C4CA7A44410, 747, 244 },
  15952. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15953. { 0xD01FEF10A657842C, 800, 260 },
  15954. { 0x9B10A4E5E9913129, 827, 268 },
  15955. { 0xE7109BFBA19C0C9D, 853, 276 },
  15956. { 0xAC2820D9623BF429, 880, 284 },
  15957. { 0x80444B5E7AA7CF85, 907, 292 },
  15958. { 0xBF21E44003ACDD2D, 933, 300 },
  15959. { 0x8E679C2F5E44FF8F, 960, 308 },
  15960. { 0xD433179D9C8CB841, 986, 316 },
  15961. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15962. }
  15963. };
  15964. // This computation gives exactly the same results for k as
  15965. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15966. // for |e| <= 1500, but doesn't require floating-point operations.
  15967. // NB: log_10(2) ~= 78913 / 2^18
  15968. JSON_ASSERT(e >= -1500);
  15969. JSON_ASSERT(e <= 1500);
  15970. const int f = kAlpha - e - 1;
  15971. const int k = ((f * 78913) / (1 << 18)) + static_cast<int>(f > 0);
  15972. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15973. JSON_ASSERT(index >= 0);
  15974. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15975. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15976. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15977. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15978. return cached;
  15979. }
  15980. /*!
  15981. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15982. For n == 0, returns 1 and sets pow10 := 1.
  15983. */
  15984. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15985. {
  15986. // LCOV_EXCL_START
  15987. if (n >= 1000000000)
  15988. {
  15989. pow10 = 1000000000;
  15990. return 10;
  15991. }
  15992. // LCOV_EXCL_STOP
  15993. if (n >= 100000000)
  15994. {
  15995. pow10 = 100000000;
  15996. return 9;
  15997. }
  15998. if (n >= 10000000)
  15999. {
  16000. pow10 = 10000000;
  16001. return 8;
  16002. }
  16003. if (n >= 1000000)
  16004. {
  16005. pow10 = 1000000;
  16006. return 7;
  16007. }
  16008. if (n >= 100000)
  16009. {
  16010. pow10 = 100000;
  16011. return 6;
  16012. }
  16013. if (n >= 10000)
  16014. {
  16015. pow10 = 10000;
  16016. return 5;
  16017. }
  16018. if (n >= 1000)
  16019. {
  16020. pow10 = 1000;
  16021. return 4;
  16022. }
  16023. if (n >= 100)
  16024. {
  16025. pow10 = 100;
  16026. return 3;
  16027. }
  16028. if (n >= 10)
  16029. {
  16030. pow10 = 10;
  16031. return 2;
  16032. }
  16033. pow10 = 1;
  16034. return 1;
  16035. }
  16036. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  16037. std::uint64_t rest, std::uint64_t ten_k)
  16038. {
  16039. JSON_ASSERT(len >= 1);
  16040. JSON_ASSERT(dist <= delta);
  16041. JSON_ASSERT(rest <= delta);
  16042. JSON_ASSERT(ten_k > 0);
  16043. // <--------------------------- delta ---->
  16044. // <---- dist --------->
  16045. // --------------[------------------+-------------------]--------------
  16046. // M- w M+
  16047. //
  16048. // ten_k
  16049. // <------>
  16050. // <---- rest ---->
  16051. // --------------[------------------+----+--------------]--------------
  16052. // w V
  16053. // = buf * 10^k
  16054. //
  16055. // ten_k represents a unit-in-the-last-place in the decimal representation
  16056. // stored in buf.
  16057. // Decrement buf by ten_k while this takes buf closer to w.
  16058. // The tests are written in this order to avoid overflow in unsigned
  16059. // integer arithmetic.
  16060. while (rest < dist
  16061. && delta - rest >= ten_k
  16062. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  16063. {
  16064. JSON_ASSERT(buf[len - 1] != '0');
  16065. buf[len - 1]--;
  16066. rest += ten_k;
  16067. }
  16068. }
  16069. /*!
  16070. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  16071. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  16072. */
  16073. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  16074. diyfp M_minus, diyfp w, diyfp M_plus)
  16075. {
  16076. static_assert(kAlpha >= -60, "internal error");
  16077. static_assert(kGamma <= -32, "internal error");
  16078. // Generates the digits (and the exponent) of a decimal floating-point
  16079. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  16080. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  16081. //
  16082. // <--------------------------- delta ---->
  16083. // <---- dist --------->
  16084. // --------------[------------------+-------------------]--------------
  16085. // M- w M+
  16086. //
  16087. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  16088. // V is in [M-,M+].
  16089. JSON_ASSERT(M_plus.e >= kAlpha);
  16090. JSON_ASSERT(M_plus.e <= kGamma);
  16091. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  16092. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  16093. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  16094. //
  16095. // M+ = f * 2^e
  16096. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  16097. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  16098. // = p1 + p2 * 2^e
  16099. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  16100. 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.)
  16101. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  16102. // 1)
  16103. //
  16104. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  16105. JSON_ASSERT(p1 > 0);
  16106. std::uint32_t pow10{};
  16107. const int k = find_largest_pow10(p1, pow10);
  16108. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  16109. //
  16110. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  16111. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  16112. //
  16113. // M+ = p1 + p2 * 2^e
  16114. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  16115. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  16116. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  16117. //
  16118. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  16119. //
  16120. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  16121. //
  16122. // but stop as soon as
  16123. //
  16124. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  16125. int n = k;
  16126. while (n > 0)
  16127. {
  16128. // Invariants:
  16129. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  16130. // pow10 = 10^(n-1) <= p1 < 10^n
  16131. //
  16132. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  16133. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  16134. //
  16135. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  16136. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  16137. //
  16138. JSON_ASSERT(d <= 9);
  16139. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  16140. //
  16141. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  16142. //
  16143. p1 = r;
  16144. n--;
  16145. //
  16146. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  16147. // pow10 = 10^n
  16148. //
  16149. // Now check if enough digits have been generated.
  16150. // Compute
  16151. //
  16152. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  16153. //
  16154. // Note:
  16155. // Since rest and delta share the same exponent e, it suffices to
  16156. // compare the significands.
  16157. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  16158. if (rest <= delta)
  16159. {
  16160. // V = buffer * 10^n, with M- <= V <= M+.
  16161. decimal_exponent += n;
  16162. // We may now just stop. But instead, it looks as if the buffer
  16163. // could be decremented to bring V closer to w.
  16164. //
  16165. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  16166. // The rounding procedure works with diyfp's with an implicit
  16167. // exponent of e.
  16168. //
  16169. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  16170. //
  16171. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  16172. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  16173. return;
  16174. }
  16175. pow10 /= 10;
  16176. //
  16177. // pow10 = 10^(n-1) <= p1 < 10^n
  16178. // Invariants restored.
  16179. }
  16180. // 2)
  16181. //
  16182. // The digits of the integral part have been generated:
  16183. //
  16184. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  16185. // = buffer + p2 * 2^e
  16186. //
  16187. // Now generate the digits of the fractional part p2 * 2^e.
  16188. //
  16189. // Note:
  16190. // No decimal point is generated: the exponent is adjusted instead.
  16191. //
  16192. // p2 actually represents the fraction
  16193. //
  16194. // p2 * 2^e
  16195. // = p2 / 2^-e
  16196. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  16197. //
  16198. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  16199. //
  16200. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  16201. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  16202. //
  16203. // using
  16204. //
  16205. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  16206. // = ( d) * 2^-e + ( r)
  16207. //
  16208. // or
  16209. // 10^m * p2 * 2^e = d + r * 2^e
  16210. //
  16211. // i.e.
  16212. //
  16213. // M+ = buffer + p2 * 2^e
  16214. // = buffer + 10^-m * (d + r * 2^e)
  16215. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  16216. //
  16217. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  16218. JSON_ASSERT(p2 > delta);
  16219. int m = 0;
  16220. for (;;)
  16221. {
  16222. // Invariant:
  16223. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  16224. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  16225. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  16226. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  16227. //
  16228. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  16229. p2 *= 10;
  16230. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  16231. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  16232. //
  16233. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  16234. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  16235. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  16236. //
  16237. JSON_ASSERT(d <= 9);
  16238. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  16239. //
  16240. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  16241. //
  16242. p2 = r;
  16243. m++;
  16244. //
  16245. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  16246. // Invariant restored.
  16247. // Check if enough digits have been generated.
  16248. //
  16249. // 10^-m * p2 * 2^e <= delta * 2^e
  16250. // p2 * 2^e <= 10^m * delta * 2^e
  16251. // p2 <= 10^m * delta
  16252. delta *= 10;
  16253. dist *= 10;
  16254. if (p2 <= delta)
  16255. {
  16256. break;
  16257. }
  16258. }
  16259. // V = buffer * 10^-m, with M- <= V <= M+.
  16260. decimal_exponent -= m;
  16261. // 1 ulp in the decimal representation is now 10^-m.
  16262. // Since delta and dist are now scaled by 10^m, we need to do the
  16263. // same with ulp in order to keep the units in sync.
  16264. //
  16265. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  16266. //
  16267. const std::uint64_t ten_m = one.f;
  16268. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  16269. // By construction this algorithm generates the shortest possible decimal
  16270. // number (Loitsch, Theorem 6.2) which rounds back to w.
  16271. // For an input number of precision p, at least
  16272. //
  16273. // N = 1 + ceil(p * log_10(2))
  16274. //
  16275. // decimal digits are sufficient to identify all binary floating-point
  16276. // numbers (Matula, "In-and-Out conversions").
  16277. // This implies that the algorithm does not produce more than N decimal
  16278. // digits.
  16279. //
  16280. // N = 17 for p = 53 (IEEE double precision)
  16281. // N = 9 for p = 24 (IEEE single precision)
  16282. }
  16283. /*!
  16284. v = buf * 10^decimal_exponent
  16285. len is the length of the buffer (number of decimal digits)
  16286. The buffer must be large enough, i.e. >= max_digits10.
  16287. */
  16288. JSON_HEDLEY_NON_NULL(1)
  16289. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  16290. diyfp m_minus, diyfp v, diyfp m_plus)
  16291. {
  16292. JSON_ASSERT(m_plus.e == m_minus.e);
  16293. JSON_ASSERT(m_plus.e == v.e);
  16294. // --------(-----------------------+-----------------------)-------- (A)
  16295. // m- v m+
  16296. //
  16297. // --------------------(-----------+-----------------------)-------- (B)
  16298. // m- v m+
  16299. //
  16300. // First scale v (and m- and m+) such that the exponent is in the range
  16301. // [alpha, gamma].
  16302. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  16303. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  16304. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  16305. const diyfp w = diyfp::mul(v, c_minus_k);
  16306. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  16307. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  16308. // ----(---+---)---------------(---+---)---------------(---+---)----
  16309. // w- w w+
  16310. // = c*m- = c*v = c*m+
  16311. //
  16312. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  16313. // w+ are now off by a small amount.
  16314. // In fact:
  16315. //
  16316. // w - v * 10^k < 1 ulp
  16317. //
  16318. // To account for this inaccuracy, add resp. subtract 1 ulp.
  16319. //
  16320. // --------+---[---------------(---+---)---------------]---+--------
  16321. // w- M- w M+ w+
  16322. //
  16323. // Now any number in [M-, M+] (bounds included) will round to w when input,
  16324. // regardless of how the input rounding algorithm breaks ties.
  16325. //
  16326. // And digit_gen generates the shortest possible such number in [M-, M+].
  16327. // Note that this does not mean that Grisu2 always generates the shortest
  16328. // possible number in the interval (m-, m+).
  16329. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  16330. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  16331. decimal_exponent = -cached.k; // = -(-k) = k
  16332. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  16333. }
  16334. /*!
  16335. v = buf * 10^decimal_exponent
  16336. len is the length of the buffer (number of decimal digits)
  16337. The buffer must be large enough, i.e. >= max_digits10.
  16338. */
  16339. template<typename FloatType>
  16340. JSON_HEDLEY_NON_NULL(1)
  16341. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  16342. {
  16343. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  16344. "internal error: not enough precision");
  16345. JSON_ASSERT(std::isfinite(value));
  16346. JSON_ASSERT(value > 0);
  16347. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  16348. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  16349. // decimal representations are not exactly "short".
  16350. //
  16351. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  16352. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  16353. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  16354. // does.
  16355. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  16356. // representation using the corresponding std::from_chars function recovers value exactly". That
  16357. // indicates that single precision floating-point numbers should be recovered using
  16358. // 'std::strtof'.
  16359. //
  16360. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  16361. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  16362. // value is off by 1 ulp.
  16363. #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
  16364. const boundaries w = compute_boundaries(static_cast<double>(value));
  16365. #else
  16366. const boundaries w = compute_boundaries(value);
  16367. #endif
  16368. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  16369. }
  16370. /*!
  16371. @brief appends a decimal representation of e to buf
  16372. @return a pointer to the element following the exponent.
  16373. @pre -1000 < e < 1000
  16374. */
  16375. JSON_HEDLEY_NON_NULL(1)
  16376. JSON_HEDLEY_RETURNS_NON_NULL
  16377. inline char* append_exponent(char* buf, int e)
  16378. {
  16379. JSON_ASSERT(e > -1000);
  16380. JSON_ASSERT(e < 1000);
  16381. if (e < 0)
  16382. {
  16383. e = -e;
  16384. *buf++ = '-';
  16385. }
  16386. else
  16387. {
  16388. *buf++ = '+';
  16389. }
  16390. auto k = static_cast<std::uint32_t>(e);
  16391. if (k < 10)
  16392. {
  16393. // Always print at least two digits in the exponent.
  16394. // This is for compatibility with printf("%g").
  16395. *buf++ = '0';
  16396. *buf++ = static_cast<char>('0' + k);
  16397. }
  16398. else if (k < 100)
  16399. {
  16400. *buf++ = static_cast<char>('0' + (k / 10));
  16401. k %= 10;
  16402. *buf++ = static_cast<char>('0' + k);
  16403. }
  16404. else
  16405. {
  16406. *buf++ = static_cast<char>('0' + (k / 100));
  16407. k %= 100;
  16408. *buf++ = static_cast<char>('0' + (k / 10));
  16409. k %= 10;
  16410. *buf++ = static_cast<char>('0' + k);
  16411. }
  16412. return buf;
  16413. }
  16414. /*!
  16415. @brief prettify v = buf * 10^decimal_exponent
  16416. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  16417. notation. Otherwise it will be printed in exponential notation.
  16418. @pre min_exp < 0
  16419. @pre max_exp > 0
  16420. */
  16421. JSON_HEDLEY_NON_NULL(1)
  16422. JSON_HEDLEY_RETURNS_NON_NULL
  16423. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  16424. int min_exp, int max_exp)
  16425. {
  16426. JSON_ASSERT(min_exp < 0);
  16427. JSON_ASSERT(max_exp > 0);
  16428. const int k = len;
  16429. const int n = len + decimal_exponent;
  16430. // v = buf * 10^(n-k)
  16431. // k is the length of the buffer (number of decimal digits)
  16432. // n is the position of the decimal point relative to the start of the buffer.
  16433. if (k <= n && n <= max_exp)
  16434. {
  16435. // digits[000]
  16436. // len <= max_exp + 2
  16437. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  16438. // Make it look like a floating-point number (#362, #378)
  16439. buf[n + 0] = '.';
  16440. buf[n + 1] = '0';
  16441. return buf + (static_cast<size_t>(n) + 2);
  16442. }
  16443. if (0 < n && n <= max_exp)
  16444. {
  16445. // dig.its
  16446. // len <= max_digits10 + 1
  16447. JSON_ASSERT(k > n);
  16448. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  16449. buf[n] = '.';
  16450. return buf + (static_cast<size_t>(k) + 1U);
  16451. }
  16452. if (min_exp < n && n <= 0)
  16453. {
  16454. // 0.[000]digits
  16455. // len <= 2 + (-min_exp - 1) + max_digits10
  16456. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  16457. buf[0] = '0';
  16458. buf[1] = '.';
  16459. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  16460. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  16461. }
  16462. if (k == 1)
  16463. {
  16464. // dE+123
  16465. // len <= 1 + 5
  16466. buf += 1;
  16467. }
  16468. else
  16469. {
  16470. // d.igitsE+123
  16471. // len <= max_digits10 + 1 + 5
  16472. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  16473. buf[1] = '.';
  16474. buf += 1 + static_cast<size_t>(k);
  16475. }
  16476. *buf++ = 'e';
  16477. return append_exponent(buf, n - 1);
  16478. }
  16479. } // namespace dtoa_impl
  16480. /*!
  16481. @brief generates a decimal representation of the floating-point number value in [first, last).
  16482. The format of the resulting decimal representation is similar to printf's %g
  16483. format. Returns an iterator pointing past-the-end of the decimal representation.
  16484. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  16485. @note The buffer must be large enough.
  16486. @note The result is NOT null-terminated.
  16487. */
  16488. template<typename FloatType>
  16489. JSON_HEDLEY_NON_NULL(1, 2)
  16490. JSON_HEDLEY_RETURNS_NON_NULL
  16491. char* to_chars(char* first, const char* last, FloatType value)
  16492. {
  16493. static_cast<void>(last); // maybe unused - fix warning
  16494. JSON_ASSERT(std::isfinite(value));
  16495. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  16496. if (std::signbit(value))
  16497. {
  16498. value = -value;
  16499. *first++ = '-';
  16500. }
  16501. #ifdef __GNUC__
  16502. #pragma GCC diagnostic push
  16503. #pragma GCC diagnostic ignored "-Wfloat-equal"
  16504. #endif
  16505. if (value == 0) // +-0
  16506. {
  16507. *first++ = '0';
  16508. // Make it look like a floating-point number (#362, #378)
  16509. *first++ = '.';
  16510. *first++ = '0';
  16511. return first;
  16512. }
  16513. #ifdef __GNUC__
  16514. #pragma GCC diagnostic pop
  16515. #endif
  16516. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  16517. // Compute v = buffer * 10^decimal_exponent.
  16518. // The decimal digits are stored in the buffer, which needs to be interpreted
  16519. // as an unsigned decimal integer.
  16520. // len is the length of the buffer, i.e., the number of decimal digits.
  16521. int len = 0;
  16522. int decimal_exponent = 0;
  16523. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  16524. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  16525. // Format the buffer like printf("%.*g", prec, value)
  16526. constexpr int kMinExp = -4;
  16527. // Use digits10 here to increase compatibility with version 2.
  16528. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  16529. JSON_ASSERT(last - first >= kMaxExp + 2);
  16530. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  16531. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  16532. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  16533. }
  16534. } // namespace detail
  16535. NLOHMANN_JSON_NAMESPACE_END
  16536. // #include <nlohmann/detail/exceptions.hpp>
  16537. // #include <nlohmann/detail/macro_scope.hpp>
  16538. // #include <nlohmann/detail/meta/cpp_future.hpp>
  16539. // #include <nlohmann/detail/output/binary_writer.hpp>
  16540. // #include <nlohmann/detail/output/output_adapters.hpp>
  16541. // #include <nlohmann/detail/string_concat.hpp>
  16542. // #include <nlohmann/detail/value_t.hpp>
  16543. NLOHMANN_JSON_NAMESPACE_BEGIN
  16544. namespace detail
  16545. {
  16546. ///////////////////
  16547. // serialization //
  16548. ///////////////////
  16549. /// how to treat decoding errors
  16550. enum class error_handler_t
  16551. {
  16552. strict, ///< throw a type_error exception in case of invalid UTF-8
  16553. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  16554. ignore ///< ignore invalid UTF-8 sequences
  16555. };
  16556. template<typename BasicJsonType>
  16557. class serializer
  16558. {
  16559. using string_t = typename BasicJsonType::string_t;
  16560. using number_float_t = typename BasicJsonType::number_float_t;
  16561. using number_integer_t = typename BasicJsonType::number_integer_t;
  16562. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  16563. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  16564. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  16565. static constexpr std::uint8_t UTF8_REJECT = 1;
  16566. public:
  16567. /*!
  16568. @param[in] s output stream to serialize to
  16569. @param[in] ichar indentation character to use
  16570. @param[in] error_handler_ how to react on decoding errors
  16571. */
  16572. serializer(output_adapter_t<char> s, const char ichar,
  16573. error_handler_t error_handler_ = error_handler_t::strict)
  16574. : o(std::move(s))
  16575. , loc(std::localeconv())
  16576. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  16577. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  16578. , indent_char(ichar)
  16579. , indent_string(512, indent_char)
  16580. , error_handler(error_handler_)
  16581. {}
  16582. // deleted because of pointer members
  16583. serializer(const serializer&) = delete;
  16584. serializer& operator=(const serializer&) = delete;
  16585. serializer(serializer&&) = delete;
  16586. serializer& operator=(serializer&&) = delete;
  16587. ~serializer() = default;
  16588. /*!
  16589. @brief internal implementation of the serialization function
  16590. This function is called by the public member function dump and organizes
  16591. the serialization internally. The indentation level is propagated as
  16592. additional parameter. In case of arrays and objects, the function is
  16593. called recursively.
  16594. - strings and object keys are escaped using `escape_string()`
  16595. - integer numbers are converted implicitly via `operator<<`
  16596. - floating-point numbers are converted to a string using `"%g"` format
  16597. - binary values are serialized as objects containing the subtype and the
  16598. byte array
  16599. @param[in] val value to serialize
  16600. @param[in] pretty_print whether the output shall be pretty-printed
  16601. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  16602. in the output are escaped with `\uXXXX` sequences, and the result consists
  16603. of ASCII characters only.
  16604. @param[in] indent_step the indent level
  16605. @param[in] current_indent the current indent level (only used internally)
  16606. */
  16607. void dump(const BasicJsonType& val,
  16608. const bool pretty_print,
  16609. const bool ensure_ascii,
  16610. const unsigned int indent_step,
  16611. const unsigned int current_indent = 0)
  16612. {
  16613. switch (val.m_data.m_type)
  16614. {
  16615. case value_t::object:
  16616. {
  16617. if (val.m_data.m_value.object->empty())
  16618. {
  16619. o->write_characters("{}", 2);
  16620. return;
  16621. }
  16622. if (pretty_print)
  16623. {
  16624. o->write_characters("{\n", 2);
  16625. // variable to hold indentation for recursive calls
  16626. const auto new_indent = current_indent + indent_step;
  16627. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  16628. {
  16629. indent_string.resize(indent_string.size() * 2, ' ');
  16630. }
  16631. // first n-1 elements
  16632. auto i = val.m_data.m_value.object->cbegin();
  16633. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  16634. {
  16635. o->write_characters(indent_string.c_str(), new_indent);
  16636. o->write_character('\"');
  16637. dump_escaped(i->first, ensure_ascii);
  16638. o->write_characters("\": ", 3);
  16639. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  16640. o->write_characters(",\n", 2);
  16641. }
  16642. // last element
  16643. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  16644. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  16645. o->write_characters(indent_string.c_str(), new_indent);
  16646. o->write_character('\"');
  16647. dump_escaped(i->first, ensure_ascii);
  16648. o->write_characters("\": ", 3);
  16649. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  16650. o->write_character('\n');
  16651. o->write_characters(indent_string.c_str(), current_indent);
  16652. o->write_character('}');
  16653. }
  16654. else
  16655. {
  16656. o->write_character('{');
  16657. // first n-1 elements
  16658. auto i = val.m_data.m_value.object->cbegin();
  16659. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  16660. {
  16661. o->write_character('\"');
  16662. dump_escaped(i->first, ensure_ascii);
  16663. o->write_characters("\":", 2);
  16664. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  16665. o->write_character(',');
  16666. }
  16667. // last element
  16668. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  16669. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  16670. o->write_character('\"');
  16671. dump_escaped(i->first, ensure_ascii);
  16672. o->write_characters("\":", 2);
  16673. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  16674. o->write_character('}');
  16675. }
  16676. return;
  16677. }
  16678. case value_t::array:
  16679. {
  16680. if (val.m_data.m_value.array->empty())
  16681. {
  16682. o->write_characters("[]", 2);
  16683. return;
  16684. }
  16685. if (pretty_print)
  16686. {
  16687. o->write_characters("[\n", 2);
  16688. // variable to hold indentation for recursive calls
  16689. const auto new_indent = current_indent + indent_step;
  16690. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  16691. {
  16692. indent_string.resize(indent_string.size() * 2, ' ');
  16693. }
  16694. // first n-1 elements
  16695. for (auto i = val.m_data.m_value.array->cbegin();
  16696. i != val.m_data.m_value.array->cend() - 1; ++i)
  16697. {
  16698. o->write_characters(indent_string.c_str(), new_indent);
  16699. dump(*i, true, ensure_ascii, indent_step, new_indent);
  16700. o->write_characters(",\n", 2);
  16701. }
  16702. // last element
  16703. JSON_ASSERT(!val.m_data.m_value.array->empty());
  16704. o->write_characters(indent_string.c_str(), new_indent);
  16705. dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  16706. o->write_character('\n');
  16707. o->write_characters(indent_string.c_str(), current_indent);
  16708. o->write_character(']');
  16709. }
  16710. else
  16711. {
  16712. o->write_character('[');
  16713. // first n-1 elements
  16714. for (auto i = val.m_data.m_value.array->cbegin();
  16715. i != val.m_data.m_value.array->cend() - 1; ++i)
  16716. {
  16717. dump(*i, false, ensure_ascii, indent_step, current_indent);
  16718. o->write_character(',');
  16719. }
  16720. // last element
  16721. JSON_ASSERT(!val.m_data.m_value.array->empty());
  16722. dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  16723. o->write_character(']');
  16724. }
  16725. return;
  16726. }
  16727. case value_t::string:
  16728. {
  16729. o->write_character('\"');
  16730. dump_escaped(*val.m_data.m_value.string, ensure_ascii);
  16731. o->write_character('\"');
  16732. return;
  16733. }
  16734. case value_t::binary:
  16735. {
  16736. if (pretty_print)
  16737. {
  16738. o->write_characters("{\n", 2);
  16739. // variable to hold indentation for recursive calls
  16740. const auto new_indent = current_indent + indent_step;
  16741. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  16742. {
  16743. indent_string.resize(indent_string.size() * 2, ' ');
  16744. }
  16745. o->write_characters(indent_string.c_str(), new_indent);
  16746. o->write_characters("\"bytes\": [", 10);
  16747. if (!val.m_data.m_value.binary->empty())
  16748. {
  16749. for (auto i = val.m_data.m_value.binary->cbegin();
  16750. i != val.m_data.m_value.binary->cend() - 1; ++i)
  16751. {
  16752. dump_integer(*i);
  16753. o->write_characters(", ", 2);
  16754. }
  16755. dump_integer(val.m_data.m_value.binary->back());
  16756. }
  16757. o->write_characters("],\n", 3);
  16758. o->write_characters(indent_string.c_str(), new_indent);
  16759. o->write_characters("\"subtype\": ", 11);
  16760. if (val.m_data.m_value.binary->has_subtype())
  16761. {
  16762. dump_integer(val.m_data.m_value.binary->subtype());
  16763. }
  16764. else
  16765. {
  16766. o->write_characters("null", 4);
  16767. }
  16768. o->write_character('\n');
  16769. o->write_characters(indent_string.c_str(), current_indent);
  16770. o->write_character('}');
  16771. }
  16772. else
  16773. {
  16774. o->write_characters("{\"bytes\":[", 10);
  16775. if (!val.m_data.m_value.binary->empty())
  16776. {
  16777. for (auto i = val.m_data.m_value.binary->cbegin();
  16778. i != val.m_data.m_value.binary->cend() - 1; ++i)
  16779. {
  16780. dump_integer(*i);
  16781. o->write_character(',');
  16782. }
  16783. dump_integer(val.m_data.m_value.binary->back());
  16784. }
  16785. o->write_characters("],\"subtype\":", 12);
  16786. if (val.m_data.m_value.binary->has_subtype())
  16787. {
  16788. dump_integer(val.m_data.m_value.binary->subtype());
  16789. o->write_character('}');
  16790. }
  16791. else
  16792. {
  16793. o->write_characters("null}", 5);
  16794. }
  16795. }
  16796. return;
  16797. }
  16798. case value_t::boolean:
  16799. {
  16800. if (val.m_data.m_value.boolean)
  16801. {
  16802. o->write_characters("true", 4);
  16803. }
  16804. else
  16805. {
  16806. o->write_characters("false", 5);
  16807. }
  16808. return;
  16809. }
  16810. case value_t::number_integer:
  16811. {
  16812. dump_integer(val.m_data.m_value.number_integer);
  16813. return;
  16814. }
  16815. case value_t::number_unsigned:
  16816. {
  16817. dump_integer(val.m_data.m_value.number_unsigned);
  16818. return;
  16819. }
  16820. case value_t::number_float:
  16821. {
  16822. dump_float(val.m_data.m_value.number_float);
  16823. return;
  16824. }
  16825. case value_t::discarded:
  16826. {
  16827. o->write_characters("<discarded>", 11);
  16828. return;
  16829. }
  16830. case value_t::null:
  16831. {
  16832. o->write_characters("null", 4);
  16833. return;
  16834. }
  16835. default: // LCOV_EXCL_LINE
  16836. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16837. }
  16838. }
  16839. JSON_PRIVATE_UNLESS_TESTED:
  16840. /*!
  16841. @brief dump escaped string
  16842. Escape a string by replacing certain special characters by a sequence of an
  16843. escape character (backslash) and another character and other control
  16844. characters by a sequence of "\u" followed by a four-digit hex
  16845. representation. The escaped string is written to output stream @a o.
  16846. @param[in] s the string to escape
  16847. @param[in] ensure_ascii whether to escape non-ASCII characters with
  16848. \uXXXX sequences
  16849. @complexity Linear in the length of string @a s.
  16850. */
  16851. void dump_escaped(const string_t& s, const bool ensure_ascii)
  16852. {
  16853. std::uint32_t codepoint{};
  16854. std::uint8_t state = UTF8_ACCEPT;
  16855. std::size_t bytes = 0; // number of bytes written to string_buffer
  16856. // number of bytes written at the point of the last valid byte
  16857. std::size_t bytes_after_last_accept = 0;
  16858. std::size_t undumped_chars = 0;
  16859. for (std::size_t i = 0; i < s.size(); ++i)
  16860. {
  16861. const auto byte = static_cast<std::uint8_t>(s[i]);
  16862. switch (decode(state, codepoint, byte))
  16863. {
  16864. case UTF8_ACCEPT: // decode found a new code point
  16865. {
  16866. switch (codepoint)
  16867. {
  16868. case 0x08: // backspace
  16869. {
  16870. string_buffer[bytes++] = '\\';
  16871. string_buffer[bytes++] = 'b';
  16872. break;
  16873. }
  16874. case 0x09: // horizontal tab
  16875. {
  16876. string_buffer[bytes++] = '\\';
  16877. string_buffer[bytes++] = 't';
  16878. break;
  16879. }
  16880. case 0x0A: // newline
  16881. {
  16882. string_buffer[bytes++] = '\\';
  16883. string_buffer[bytes++] = 'n';
  16884. break;
  16885. }
  16886. case 0x0C: // formfeed
  16887. {
  16888. string_buffer[bytes++] = '\\';
  16889. string_buffer[bytes++] = 'f';
  16890. break;
  16891. }
  16892. case 0x0D: // carriage return
  16893. {
  16894. string_buffer[bytes++] = '\\';
  16895. string_buffer[bytes++] = 'r';
  16896. break;
  16897. }
  16898. case 0x22: // quotation mark
  16899. {
  16900. string_buffer[bytes++] = '\\';
  16901. string_buffer[bytes++] = '\"';
  16902. break;
  16903. }
  16904. case 0x5C: // reverse solidus
  16905. {
  16906. string_buffer[bytes++] = '\\';
  16907. string_buffer[bytes++] = '\\';
  16908. break;
  16909. }
  16910. default:
  16911. {
  16912. // escape control characters (0x00..0x1F) or, if
  16913. // ensure_ascii parameter is used, non-ASCII characters
  16914. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  16915. {
  16916. if (codepoint <= 0xFFFF)
  16917. {
  16918. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16919. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  16920. static_cast<std::uint16_t>(codepoint)));
  16921. bytes += 6;
  16922. }
  16923. else
  16924. {
  16925. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16926. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16927. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16928. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16929. bytes += 12;
  16930. }
  16931. }
  16932. else
  16933. {
  16934. // copy byte to buffer (all previous bytes
  16935. // been copied have in default case above)
  16936. string_buffer[bytes++] = s[i];
  16937. }
  16938. break;
  16939. }
  16940. }
  16941. // write buffer and reset index; there must be 13 bytes
  16942. // left, as this is the maximal number of bytes to be
  16943. // written ("\uxxxx\uxxxx\0") for one code point
  16944. if (string_buffer.size() - bytes < 13)
  16945. {
  16946. o->write_characters(string_buffer.data(), bytes);
  16947. bytes = 0;
  16948. }
  16949. // remember the byte position of this accept
  16950. bytes_after_last_accept = bytes;
  16951. undumped_chars = 0;
  16952. break;
  16953. }
  16954. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16955. {
  16956. switch (error_handler)
  16957. {
  16958. case error_handler_t::strict:
  16959. {
  16960. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16961. }
  16962. case error_handler_t::ignore:
  16963. case error_handler_t::replace:
  16964. {
  16965. // in case we saw this character the first time, we
  16966. // would like to read it again, because the byte
  16967. // may be OK for itself, but just not OK for the
  16968. // previous sequence
  16969. if (undumped_chars > 0)
  16970. {
  16971. --i;
  16972. }
  16973. // reset length buffer to the last accepted index;
  16974. // thus removing/ignoring the invalid characters
  16975. bytes = bytes_after_last_accept;
  16976. if (error_handler == error_handler_t::replace)
  16977. {
  16978. // add a replacement character
  16979. if (ensure_ascii)
  16980. {
  16981. string_buffer[bytes++] = '\\';
  16982. string_buffer[bytes++] = 'u';
  16983. string_buffer[bytes++] = 'f';
  16984. string_buffer[bytes++] = 'f';
  16985. string_buffer[bytes++] = 'f';
  16986. string_buffer[bytes++] = 'd';
  16987. }
  16988. else
  16989. {
  16990. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16991. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16992. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16993. }
  16994. // write buffer and reset index; there must be 13 bytes
  16995. // left, as this is the maximal number of bytes to be
  16996. // written ("\uxxxx\uxxxx\0") for one code point
  16997. if (string_buffer.size() - bytes < 13)
  16998. {
  16999. o->write_characters(string_buffer.data(), bytes);
  17000. bytes = 0;
  17001. }
  17002. bytes_after_last_accept = bytes;
  17003. }
  17004. undumped_chars = 0;
  17005. // continue processing the string
  17006. state = UTF8_ACCEPT;
  17007. break;
  17008. }
  17009. default: // LCOV_EXCL_LINE
  17010. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17011. }
  17012. break;
  17013. }
  17014. default: // decode found yet incomplete multibyte code point
  17015. {
  17016. if (!ensure_ascii)
  17017. {
  17018. // code point will not be escaped - copy byte to buffer
  17019. string_buffer[bytes++] = s[i];
  17020. }
  17021. ++undumped_chars;
  17022. break;
  17023. }
  17024. }
  17025. }
  17026. // we finished processing the string
  17027. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  17028. {
  17029. // write buffer
  17030. if (bytes > 0)
  17031. {
  17032. o->write_characters(string_buffer.data(), bytes);
  17033. }
  17034. }
  17035. else
  17036. {
  17037. // we finish reading, but do not accept: string was incomplete
  17038. switch (error_handler)
  17039. {
  17040. case error_handler_t::strict:
  17041. {
  17042. 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));
  17043. }
  17044. case error_handler_t::ignore:
  17045. {
  17046. // write all accepted bytes
  17047. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  17048. break;
  17049. }
  17050. case error_handler_t::replace:
  17051. {
  17052. // write all accepted bytes
  17053. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  17054. // add a replacement character
  17055. if (ensure_ascii)
  17056. {
  17057. o->write_characters("\\ufffd", 6);
  17058. }
  17059. else
  17060. {
  17061. o->write_characters("\xEF\xBF\xBD", 3);
  17062. }
  17063. break;
  17064. }
  17065. default: // LCOV_EXCL_LINE
  17066. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17067. }
  17068. }
  17069. }
  17070. private:
  17071. /*!
  17072. @brief count digits
  17073. Count the number of decimal (base 10) digits for an input unsigned integer.
  17074. @param[in] x unsigned integer number to count its digits
  17075. @return number of decimal digits
  17076. */
  17077. unsigned int count_digits(number_unsigned_t x) noexcept
  17078. {
  17079. unsigned int n_digits = 1;
  17080. for (;;)
  17081. {
  17082. if (x < 10)
  17083. {
  17084. return n_digits;
  17085. }
  17086. if (x < 100)
  17087. {
  17088. return n_digits + 1;
  17089. }
  17090. if (x < 1000)
  17091. {
  17092. return n_digits + 2;
  17093. }
  17094. if (x < 10000)
  17095. {
  17096. return n_digits + 3;
  17097. }
  17098. x = x / 10000u;
  17099. n_digits += 4;
  17100. }
  17101. }
  17102. /*!
  17103. * @brief convert a byte to a uppercase hex representation
  17104. * @param[in] byte byte to represent
  17105. * @return representation ("00".."FF")
  17106. */
  17107. static std::string hex_bytes(std::uint8_t byte)
  17108. {
  17109. std::string result = "FF";
  17110. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  17111. result[0] = nibble_to_hex[byte / 16];
  17112. result[1] = nibble_to_hex[byte % 16];
  17113. return result;
  17114. }
  17115. // templates to avoid warnings about useless casts
  17116. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  17117. bool is_negative_number(NumberType x)
  17118. {
  17119. return x < 0;
  17120. }
  17121. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  17122. bool is_negative_number(NumberType /*unused*/)
  17123. {
  17124. return false;
  17125. }
  17126. /*!
  17127. @brief dump an integer
  17128. Dump a given integer to output stream @a o. Works internally with
  17129. @a number_buffer.
  17130. @param[in] x integer number (signed or unsigned) to dump
  17131. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  17132. */
  17133. template < typename NumberType, detail::enable_if_t <
  17134. std::is_integral<NumberType>::value ||
  17135. std::is_same<NumberType, number_unsigned_t>::value ||
  17136. std::is_same<NumberType, number_integer_t>::value ||
  17137. std::is_same<NumberType, binary_char_t>::value,
  17138. int > = 0 >
  17139. void dump_integer(NumberType x)
  17140. {
  17141. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  17142. {
  17143. {
  17144. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  17145. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  17146. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  17147. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  17148. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  17149. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  17150. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  17151. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  17152. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  17153. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  17154. }
  17155. };
  17156. // special case for "0"
  17157. if (x == 0)
  17158. {
  17159. o->write_character('0');
  17160. return;
  17161. }
  17162. // use a pointer to fill the buffer
  17163. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  17164. number_unsigned_t abs_value;
  17165. unsigned int n_chars{};
  17166. if (is_negative_number(x))
  17167. {
  17168. *buffer_ptr = '-';
  17169. abs_value = remove_sign(static_cast<number_integer_t>(x));
  17170. // account one more byte for the minus sign
  17171. n_chars = 1 + count_digits(abs_value);
  17172. }
  17173. else
  17174. {
  17175. abs_value = static_cast<number_unsigned_t>(x);
  17176. n_chars = count_digits(abs_value);
  17177. }
  17178. // spare 1 byte for '\0'
  17179. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  17180. // jump to the end to generate the string from backward,
  17181. // so we later avoid reversing the result
  17182. buffer_ptr += static_cast<typename decltype(number_buffer)::difference_type>(n_chars);
  17183. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  17184. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  17185. while (abs_value >= 100)
  17186. {
  17187. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  17188. abs_value /= 100;
  17189. *(--buffer_ptr) = digits_to_99[digits_index][1];
  17190. *(--buffer_ptr) = digits_to_99[digits_index][0];
  17191. }
  17192. if (abs_value >= 10)
  17193. {
  17194. const auto digits_index = static_cast<unsigned>(abs_value);
  17195. *(--buffer_ptr) = digits_to_99[digits_index][1];
  17196. *(--buffer_ptr) = digits_to_99[digits_index][0];
  17197. }
  17198. else
  17199. {
  17200. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  17201. }
  17202. o->write_characters(number_buffer.data(), n_chars);
  17203. }
  17204. /*!
  17205. @brief dump a floating-point number
  17206. Dump a given floating-point number to output stream @a o. Works internally
  17207. with @a number_buffer.
  17208. @param[in] x floating-point number to dump
  17209. */
  17210. void dump_float(number_float_t x)
  17211. {
  17212. // NaN / inf
  17213. if (!std::isfinite(x))
  17214. {
  17215. o->write_characters("null", 4);
  17216. return;
  17217. }
  17218. // If number_float_t is an IEEE-754 single or double precision number,
  17219. // use the Grisu2 algorithm to produce short numbers which are
  17220. // guaranteed to round-trip, using strtof and strtod, resp.
  17221. //
  17222. // NB: The test below works if <long double> == <double>.
  17223. static constexpr bool is_ieee_single_or_double
  17224. = (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) ||
  17225. (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);
  17226. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  17227. }
  17228. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  17229. {
  17230. auto* begin = number_buffer.data();
  17231. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  17232. o->write_characters(begin, static_cast<size_t>(end - begin));
  17233. }
  17234. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  17235. {
  17236. // get the number of digits for a float -> text -> float round-trip
  17237. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  17238. // the actual conversion
  17239. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  17240. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  17241. // negative value indicates an error
  17242. JSON_ASSERT(len > 0);
  17243. // check if the buffer was large enough
  17244. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  17245. // erase thousands separators
  17246. if (thousands_sep != '\0')
  17247. {
  17248. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  17249. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  17250. std::fill(end, number_buffer.end(), '\0');
  17251. JSON_ASSERT((end - number_buffer.begin()) <= len);
  17252. len = (end - number_buffer.begin());
  17253. }
  17254. // convert decimal point to '.'
  17255. if (decimal_point != '\0' && decimal_point != '.')
  17256. {
  17257. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  17258. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  17259. if (dec_pos != number_buffer.end())
  17260. {
  17261. *dec_pos = '.';
  17262. }
  17263. }
  17264. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  17265. // determine if we need to append ".0"
  17266. const bool value_is_int_like =
  17267. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  17268. [](char c)
  17269. {
  17270. return c == '.' || c == 'e';
  17271. });
  17272. if (value_is_int_like)
  17273. {
  17274. o->write_characters(".0", 2);
  17275. }
  17276. }
  17277. /*!
  17278. @brief check whether a string is UTF-8 encoded
  17279. The function checks each byte of a string whether it is UTF-8 encoded. The
  17280. result of the check is stored in the @a state parameter. The function must
  17281. be called initially with state 0 (accept). State 1 means the string must
  17282. be rejected, because the current byte is not allowed. If the string is
  17283. completely processed, but the state is non-zero, the string ended
  17284. prematurely; that is, the last byte indicated more bytes should have
  17285. followed.
  17286. @param[in,out] state the state of the decoding
  17287. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  17288. @param[in] byte next byte to decode
  17289. @return new state
  17290. @note The function has been edited: a std::array is used.
  17291. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  17292. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  17293. */
  17294. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  17295. {
  17296. static const std::array<std::uint8_t, 400> utf8d =
  17297. {
  17298. {
  17299. 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
  17300. 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
  17301. 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
  17302. 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
  17303. 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
  17304. 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
  17305. 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
  17306. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  17307. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  17308. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  17309. 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
  17310. 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
  17311. 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
  17312. 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
  17313. }
  17314. };
  17315. JSON_ASSERT(static_cast<std::size_t>(byte) < utf8d.size());
  17316. const std::uint8_t type = utf8d[byte];
  17317. codep = (state != UTF8_ACCEPT)
  17318. ? (byte & 0x3fu) | (codep << 6u)
  17319. : (0xFFu >> type) & (byte);
  17320. const std::size_t index = 256u + (static_cast<size_t>(state) * 16u) + static_cast<size_t>(type);
  17321. JSON_ASSERT(index < utf8d.size());
  17322. state = utf8d[index];
  17323. return state;
  17324. }
  17325. /*
  17326. * Overload to make the compiler happy while it is instantiating
  17327. * dump_integer for number_unsigned_t.
  17328. * Must never be called.
  17329. */
  17330. number_unsigned_t remove_sign(number_unsigned_t x)
  17331. {
  17332. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17333. return x; // LCOV_EXCL_LINE
  17334. }
  17335. /*
  17336. * Helper function for dump_integer
  17337. *
  17338. * This function takes a negative signed integer and returns its absolute
  17339. * value as an unsigned integer. The plus/minus shuffling is necessary as we
  17340. * cannot directly remove the sign of an arbitrary signed integer as the
  17341. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  17342. * #1708 for details.
  17343. */
  17344. number_unsigned_t remove_sign(number_integer_t x) noexcept
  17345. {
  17346. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  17347. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  17348. }
  17349. private:
  17350. /// the output of the serializer
  17351. output_adapter_t<char> o = nullptr;
  17352. /// a (hopefully) large enough character buffer
  17353. std::array<char, 64> number_buffer{{}};
  17354. /// the locale
  17355. const std::lconv* loc = nullptr;
  17356. /// the locale's thousand separator character
  17357. const char thousands_sep = '\0';
  17358. /// the locale's decimal point character
  17359. const char decimal_point = '\0';
  17360. /// string buffer
  17361. std::array<char, 512> string_buffer{{}};
  17362. /// the indentation character
  17363. const char indent_char;
  17364. /// the indentation string
  17365. string_t indent_string;
  17366. /// error_handler how to react on decoding errors
  17367. const error_handler_t error_handler;
  17368. };
  17369. } // namespace detail
  17370. NLOHMANN_JSON_NAMESPACE_END
  17371. // #include <nlohmann/detail/value_t.hpp>
  17372. // #include <nlohmann/json_fwd.hpp>
  17373. // #include <nlohmann/ordered_map.hpp>
  17374. // __ _____ _____ _____
  17375. // __| | __| | | | JSON for Modern C++
  17376. // | | |__ | | | | | | version 3.12.0
  17377. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  17378. //
  17379. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  17380. // SPDX-License-Identifier: MIT
  17381. #include <functional> // equal_to, less
  17382. #include <initializer_list> // initializer_list
  17383. #include <iterator> // input_iterator_tag, iterator_traits
  17384. #include <memory> // allocator
  17385. #include <stdexcept> // for out_of_range
  17386. #include <type_traits> // enable_if, is_convertible
  17387. #include <utility> // pair
  17388. #include <vector> // vector
  17389. // #include <nlohmann/detail/macro_scope.hpp>
  17390. // #include <nlohmann/detail/meta/type_traits.hpp>
  17391. NLOHMANN_JSON_NAMESPACE_BEGIN
  17392. /// ordered_map: a minimal map-like container that preserves insertion order
  17393. /// for use within nlohmann::basic_json<ordered_map>
  17394. template <class Key, class T, class IgnoredLess = std::less<Key>,
  17395. class Allocator = std::allocator<std::pair<const Key, T>>>
  17396. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  17397. {
  17398. using key_type = Key;
  17399. using mapped_type = T;
  17400. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  17401. using iterator = typename Container::iterator;
  17402. using const_iterator = typename Container::const_iterator;
  17403. using size_type = typename Container::size_type;
  17404. using value_type = typename Container::value_type;
  17405. #ifdef JSON_HAS_CPP_14
  17406. using key_compare = std::equal_to<>;
  17407. #else
  17408. using key_compare = std::equal_to<Key>;
  17409. #endif
  17410. // Explicit constructors instead of `using Container::Container`
  17411. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  17412. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  17413. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  17414. template <class It>
  17415. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  17416. : Container{first, last, alloc} {}
  17417. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  17418. : Container{init, alloc} {}
  17419. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  17420. {
  17421. for (auto it = this->begin(); it != this->end(); ++it)
  17422. {
  17423. if (m_compare(it->first, key))
  17424. {
  17425. return {it, false};
  17426. }
  17427. }
  17428. Container::emplace_back(key, std::forward<T>(t));
  17429. return {std::prev(this->end()), true};
  17430. }
  17431. template<class KeyType, detail::enable_if_t<
  17432. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17433. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  17434. {
  17435. for (auto it = this->begin(); it != this->end(); ++it)
  17436. {
  17437. if (m_compare(it->first, key))
  17438. {
  17439. return {it, false};
  17440. }
  17441. }
  17442. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  17443. return {std::prev(this->end()), true};
  17444. }
  17445. T& operator[](const key_type& key)
  17446. {
  17447. return emplace(key, T{}).first->second;
  17448. }
  17449. template<class KeyType, detail::enable_if_t<
  17450. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17451. T & operator[](KeyType && key)
  17452. {
  17453. return emplace(std::forward<KeyType>(key), T{}).first->second;
  17454. }
  17455. const T& operator[](const key_type& key) const
  17456. {
  17457. return at(key);
  17458. }
  17459. template<class KeyType, detail::enable_if_t<
  17460. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17461. const T & operator[](KeyType && key) const
  17462. {
  17463. return at(std::forward<KeyType>(key));
  17464. }
  17465. T& at(const key_type& key)
  17466. {
  17467. for (auto it = this->begin(); it != this->end(); ++it)
  17468. {
  17469. if (m_compare(it->first, key))
  17470. {
  17471. return it->second;
  17472. }
  17473. }
  17474. JSON_THROW(std::out_of_range("key not found"));
  17475. }
  17476. template<class KeyType, detail::enable_if_t<
  17477. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17478. T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  17479. {
  17480. for (auto it = this->begin(); it != this->end(); ++it)
  17481. {
  17482. if (m_compare(it->first, key))
  17483. {
  17484. return it->second;
  17485. }
  17486. }
  17487. JSON_THROW(std::out_of_range("key not found"));
  17488. }
  17489. const T& at(const key_type& key) const
  17490. {
  17491. for (auto it = this->begin(); it != this->end(); ++it)
  17492. {
  17493. if (m_compare(it->first, key))
  17494. {
  17495. return it->second;
  17496. }
  17497. }
  17498. JSON_THROW(std::out_of_range("key not found"));
  17499. }
  17500. template<class KeyType, detail::enable_if_t<
  17501. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17502. const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  17503. {
  17504. for (auto it = this->begin(); it != this->end(); ++it)
  17505. {
  17506. if (m_compare(it->first, key))
  17507. {
  17508. return it->second;
  17509. }
  17510. }
  17511. JSON_THROW(std::out_of_range("key not found"));
  17512. }
  17513. size_type erase(const key_type& key)
  17514. {
  17515. for (auto it = this->begin(); it != this->end(); ++it)
  17516. {
  17517. if (m_compare(it->first, key))
  17518. {
  17519. // Since we cannot move const Keys, re-construct them in place
  17520. for (auto next = it; ++next != this->end(); ++it)
  17521. {
  17522. it->~value_type(); // Destroy but keep allocation
  17523. new (&*it) value_type{std::move(*next)};
  17524. }
  17525. Container::pop_back();
  17526. return 1;
  17527. }
  17528. }
  17529. return 0;
  17530. }
  17531. template<class KeyType, detail::enable_if_t<
  17532. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17533. size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  17534. {
  17535. for (auto it = this->begin(); it != this->end(); ++it)
  17536. {
  17537. if (m_compare(it->first, key))
  17538. {
  17539. // Since we cannot move const Keys, re-construct them in place
  17540. for (auto next = it; ++next != this->end(); ++it)
  17541. {
  17542. it->~value_type(); // Destroy but keep allocation
  17543. new (&*it) value_type{std::move(*next)};
  17544. }
  17545. Container::pop_back();
  17546. return 1;
  17547. }
  17548. }
  17549. return 0;
  17550. }
  17551. iterator erase(iterator pos)
  17552. {
  17553. return erase(pos, std::next(pos));
  17554. }
  17555. iterator erase(iterator first, iterator last)
  17556. {
  17557. if (first == last)
  17558. {
  17559. return first;
  17560. }
  17561. const auto elements_affected = std::distance(first, last);
  17562. const auto offset = std::distance(Container::begin(), first);
  17563. // This is the start situation. We need to delete elements_affected
  17564. // elements (3 in this example: e, f, g), and need to return an
  17565. // iterator past the last deleted element (h in this example).
  17566. // Note that offset is the distance from the start of the vector
  17567. // to first. We will need this later.
  17568. // [ a, b, c, d, e, f, g, h, i, j ]
  17569. // ^ ^
  17570. // first last
  17571. // Since we cannot move const Keys, we re-construct them in place.
  17572. // We start at first and re-construct (viz. copy) the elements from
  17573. // the back of the vector. Example for the first iteration:
  17574. // ,--------.
  17575. // v | destroy e and re-construct with h
  17576. // [ a, b, c, d, e, f, g, h, i, j ]
  17577. // ^ ^
  17578. // it it + elements_affected
  17579. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  17580. {
  17581. it->~value_type(); // destroy but keep allocation
  17582. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  17583. }
  17584. // [ a, b, c, d, h, i, j, h, i, j ]
  17585. // ^ ^
  17586. // first last
  17587. // remove the unneeded elements at the end of the vector
  17588. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  17589. // [ a, b, c, d, h, i, j ]
  17590. // ^ ^
  17591. // first last
  17592. // first is now pointing past the last deleted element, but we cannot
  17593. // use this iterator, because it may have been invalidated by the
  17594. // resize call. Instead, we can return begin() + offset.
  17595. return Container::begin() + offset;
  17596. }
  17597. size_type count(const key_type& key) const
  17598. {
  17599. for (auto it = this->begin(); it != this->end(); ++it)
  17600. {
  17601. if (m_compare(it->first, key))
  17602. {
  17603. return 1;
  17604. }
  17605. }
  17606. return 0;
  17607. }
  17608. template<class KeyType, detail::enable_if_t<
  17609. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17610. size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  17611. {
  17612. for (auto it = this->begin(); it != this->end(); ++it)
  17613. {
  17614. if (m_compare(it->first, key))
  17615. {
  17616. return 1;
  17617. }
  17618. }
  17619. return 0;
  17620. }
  17621. iterator find(const key_type& key)
  17622. {
  17623. for (auto it = this->begin(); it != this->end(); ++it)
  17624. {
  17625. if (m_compare(it->first, key))
  17626. {
  17627. return it;
  17628. }
  17629. }
  17630. return Container::end();
  17631. }
  17632. template<class KeyType, detail::enable_if_t<
  17633. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  17634. iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  17635. {
  17636. for (auto it = this->begin(); it != this->end(); ++it)
  17637. {
  17638. if (m_compare(it->first, key))
  17639. {
  17640. return it;
  17641. }
  17642. }
  17643. return Container::end();
  17644. }
  17645. const_iterator find(const key_type& key) const
  17646. {
  17647. for (auto it = this->begin(); it != this->end(); ++it)
  17648. {
  17649. if (m_compare(it->first, key))
  17650. {
  17651. return it;
  17652. }
  17653. }
  17654. return Container::end();
  17655. }
  17656. std::pair<iterator, bool> insert( value_type&& value )
  17657. {
  17658. return emplace(value.first, std::move(value.second));
  17659. }
  17660. std::pair<iterator, bool> insert( const value_type& value )
  17661. {
  17662. for (auto it = this->begin(); it != this->end(); ++it)
  17663. {
  17664. if (m_compare(it->first, value.first))
  17665. {
  17666. return {it, false};
  17667. }
  17668. }
  17669. Container::push_back(value);
  17670. return {--this->end(), true};
  17671. }
  17672. template<typename InputIt>
  17673. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  17674. std::input_iterator_tag>::value>::type;
  17675. template<typename InputIt, typename = require_input_iter<InputIt>>
  17676. void insert(InputIt first, InputIt last)
  17677. {
  17678. for (auto it = first; it != last; ++it)
  17679. {
  17680. insert(*it);
  17681. }
  17682. }
  17683. private:
  17684. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  17685. };
  17686. NLOHMANN_JSON_NAMESPACE_END
  17687. #if defined(JSON_HAS_CPP_17)
  17688. #if JSON_HAS_STATIC_RTTI
  17689. #include <any>
  17690. #endif
  17691. #include <string_view>
  17692. #endif
  17693. /*!
  17694. @brief namespace for Niels Lohmann
  17695. @see https://github.com/nlohmann
  17696. @since version 1.0.0
  17697. */
  17698. NLOHMANN_JSON_NAMESPACE_BEGIN
  17699. /*!
  17700. @brief a class to store JSON values
  17701. @internal
  17702. @invariant The member variables @a m_value and @a m_type have the following
  17703. relationship:
  17704. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  17705. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  17706. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  17707. The invariants are checked by member function assert_invariant().
  17708. @note ObjectType trick from https://stackoverflow.com/a/9860911
  17709. @endinternal
  17710. @since version 1.0.0
  17711. @nosubgrouping
  17712. */
  17713. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  17714. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  17715. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  17716. {
  17717. private:
  17718. template<detail::value_t> friend struct detail::external_constructor;
  17719. template<typename>
  17720. friend class ::nlohmann::json_pointer;
  17721. // can be restored when json_pointer backwards compatibility is removed
  17722. // friend ::nlohmann::json_pointer<StringType>;
  17723. template<typename BasicJsonType, typename InputType>
  17724. friend class ::nlohmann::detail::parser;
  17725. friend ::nlohmann::detail::serializer<basic_json>;
  17726. template<typename BasicJsonType>
  17727. friend class ::nlohmann::detail::iter_impl;
  17728. template<typename BasicJsonType, typename CharType>
  17729. friend class ::nlohmann::detail::binary_writer;
  17730. template<typename BasicJsonType, typename InputType, typename SAX>
  17731. friend class ::nlohmann::detail::binary_reader;
  17732. template<typename BasicJsonType, typename InputAdapterType>
  17733. friend class ::nlohmann::detail::json_sax_dom_parser;
  17734. template<typename BasicJsonType, typename InputAdapterType>
  17735. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  17736. friend class ::nlohmann::detail::exception;
  17737. /// workaround type for MSVC
  17738. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  17739. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  17740. JSON_PRIVATE_UNLESS_TESTED:
  17741. // convenience aliases for types residing in namespace detail;
  17742. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  17743. template<typename InputAdapterType>
  17744. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  17745. InputAdapterType adapter,
  17746. detail::parser_callback_t<basic_json>cb = nullptr,
  17747. const bool allow_exceptions = true,
  17748. const bool ignore_comments = false,
  17749. const bool ignore_trailing_commas = false
  17750. )
  17751. {
  17752. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  17753. std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas);
  17754. }
  17755. private:
  17756. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  17757. template<typename BasicJsonType>
  17758. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  17759. template<typename BasicJsonType>
  17760. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  17761. template<typename Iterator>
  17762. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  17763. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  17764. template<typename CharType>
  17765. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  17766. template<typename InputType>
  17767. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  17768. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  17769. JSON_PRIVATE_UNLESS_TESTED:
  17770. using serializer = ::nlohmann::detail::serializer<basic_json>;
  17771. public:
  17772. using value_t = detail::value_t;
  17773. /// JSON Pointer, see @ref nlohmann::json_pointer
  17774. using json_pointer = ::nlohmann::json_pointer<StringType>;
  17775. template<typename T, typename SFINAE>
  17776. using json_serializer = JSONSerializer<T, SFINAE>;
  17777. /// how to treat decoding errors
  17778. using error_handler_t = detail::error_handler_t;
  17779. /// how to treat CBOR tags
  17780. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  17781. /// how to encode BJData
  17782. using bjdata_version_t = detail::bjdata_version_t;
  17783. /// helper type for initializer lists of basic_json values
  17784. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  17785. using input_format_t = detail::input_format_t;
  17786. /// SAX interface type, see @ref nlohmann::json_sax
  17787. using json_sax_t = json_sax<basic_json>;
  17788. ////////////////
  17789. // exceptions //
  17790. ////////////////
  17791. /// @name exceptions
  17792. /// Classes to implement user-defined exceptions.
  17793. /// @{
  17794. using exception = detail::exception;
  17795. using parse_error = detail::parse_error;
  17796. using invalid_iterator = detail::invalid_iterator;
  17797. using type_error = detail::type_error;
  17798. using out_of_range = detail::out_of_range;
  17799. using other_error = detail::other_error;
  17800. /// @}
  17801. /////////////////////
  17802. // container types //
  17803. /////////////////////
  17804. /// @name container types
  17805. /// The canonic container types to use @ref basic_json like any other STL
  17806. /// container.
  17807. /// @{
  17808. /// the type of elements in a basic_json container
  17809. using value_type = basic_json;
  17810. /// the type of an element reference
  17811. using reference = value_type&;
  17812. /// the type of an element const reference
  17813. using const_reference = const value_type&;
  17814. /// a type to represent differences between iterators
  17815. using difference_type = std::ptrdiff_t;
  17816. /// a type to represent container sizes
  17817. using size_type = std::size_t;
  17818. /// the allocator type
  17819. using allocator_type = AllocatorType<basic_json>;
  17820. /// the type of an element pointer
  17821. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  17822. /// the type of an element const pointer
  17823. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  17824. /// an iterator for a basic_json container
  17825. using iterator = iter_impl<basic_json>;
  17826. /// a const iterator for a basic_json container
  17827. using const_iterator = iter_impl<const basic_json>;
  17828. /// a reverse iterator for a basic_json container
  17829. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  17830. /// a const reverse iterator for a basic_json container
  17831. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  17832. /// @}
  17833. /// @brief returns the allocator associated with the container
  17834. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  17835. static allocator_type get_allocator()
  17836. {
  17837. return allocator_type();
  17838. }
  17839. /// @brief returns version information on the library
  17840. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  17841. JSON_HEDLEY_WARN_UNUSED_RESULT
  17842. static basic_json meta()
  17843. {
  17844. basic_json result;
  17845. result["copyright"] = "(C) 2013-2026 Niels Lohmann";
  17846. result["name"] = "JSON for Modern C++";
  17847. result["url"] = "https://github.com/nlohmann/json";
  17848. result["version"]["string"] =
  17849. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  17850. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  17851. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  17852. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  17853. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  17854. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  17855. #ifdef _WIN32
  17856. result["platform"] = "win32";
  17857. #elif defined __linux__
  17858. result["platform"] = "linux";
  17859. #elif defined __APPLE__
  17860. result["platform"] = "apple";
  17861. #elif defined __unix__
  17862. result["platform"] = "unix";
  17863. #else
  17864. result["platform"] = "unknown";
  17865. #endif
  17866. #if defined(__ICC) || defined(__INTEL_COMPILER)
  17867. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  17868. #elif defined(__clang__)
  17869. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  17870. #elif defined(__GNUC__) || defined(__GNUG__)
  17871. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  17872. std::to_string(__GNUC__), '.',
  17873. std::to_string(__GNUC_MINOR__), '.',
  17874. std::to_string(__GNUC_PATCHLEVEL__))
  17875. }
  17876. };
  17877. #elif defined(__HP_cc) || defined(__HP_aCC)
  17878. result["compiler"] = "hp"
  17879. #elif defined(__IBMCPP__)
  17880. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  17881. #elif defined(_MSC_VER)
  17882. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  17883. #elif defined(__PGI)
  17884. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  17885. #elif defined(__SUNPRO_CC)
  17886. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  17887. #else
  17888. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  17889. #endif
  17890. #if defined(_MSVC_LANG)
  17891. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  17892. #elif defined(__cplusplus)
  17893. result["compiler"]["c++"] = std::to_string(__cplusplus);
  17894. #else
  17895. result["compiler"]["c++"] = "unknown";
  17896. #endif
  17897. return result;
  17898. }
  17899. ///////////////////////////
  17900. // JSON value data types //
  17901. ///////////////////////////
  17902. /// @name JSON value data types
  17903. /// The data types to store a JSON value. These types are derived from
  17904. /// the template arguments passed to class @ref basic_json.
  17905. /// @{
  17906. /// @brief default object key comparator type
  17907. /// The actual object key comparator type (@ref object_comparator_t) may be
  17908. /// different.
  17909. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  17910. #if defined(JSON_HAS_CPP_14)
  17911. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  17912. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  17913. using default_object_comparator_t = std::less<>;
  17914. #else
  17915. using default_object_comparator_t = std::less<StringType>;
  17916. #endif
  17917. /// @brief a type for an object
  17918. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  17919. using object_t = ObjectType<StringType,
  17920. basic_json,
  17921. default_object_comparator_t,
  17922. AllocatorType<std::pair<const StringType,
  17923. basic_json>>>;
  17924. /// @brief a type for an array
  17925. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  17926. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  17927. /// @brief a type for a string
  17928. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  17929. using string_t = StringType;
  17930. /// @brief a type for a boolean
  17931. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17932. using boolean_t = BooleanType;
  17933. /// @brief a type for a number (integer)
  17934. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17935. using number_integer_t = NumberIntegerType;
  17936. /// @brief a type for a number (unsigned)
  17937. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17938. using number_unsigned_t = NumberUnsignedType;
  17939. /// @brief a type for a number (floating-point)
  17940. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17941. using number_float_t = NumberFloatType;
  17942. /// @brief a type for a packed binary type
  17943. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17944. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17945. /// @brief object key comparator type
  17946. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17947. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17948. /// @}
  17949. private:
  17950. /// helper for exception-safe object creation
  17951. template<typename T, typename... Args>
  17952. JSON_HEDLEY_RETURNS_NON_NULL
  17953. static T* create(Args&& ... args)
  17954. {
  17955. AllocatorType<T> alloc;
  17956. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17957. auto deleter = [&](T * obj)
  17958. {
  17959. AllocatorTraits::deallocate(alloc, obj, 1);
  17960. };
  17961. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17962. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17963. JSON_ASSERT(obj != nullptr);
  17964. return obj.release();
  17965. }
  17966. ////////////////////////
  17967. // JSON value storage //
  17968. ////////////////////////
  17969. JSON_PRIVATE_UNLESS_TESTED:
  17970. /*!
  17971. @brief a JSON value
  17972. The actual storage for a JSON value of the @ref basic_json class. This
  17973. union combines the different storage types for the JSON value types
  17974. defined in @ref value_t.
  17975. JSON type | value_t type | used type
  17976. --------- | --------------- | ------------------------
  17977. object | object | pointer to @ref object_t
  17978. array | array | pointer to @ref array_t
  17979. string | string | pointer to @ref string_t
  17980. boolean | boolean | @ref boolean_t
  17981. number | number_integer | @ref number_integer_t
  17982. number | number_unsigned | @ref number_unsigned_t
  17983. number | number_float | @ref number_float_t
  17984. binary | binary | pointer to @ref binary_t
  17985. null | null | *no value is stored*
  17986. @note Variable-length types (objects, arrays, and strings) are stored as
  17987. pointers. The size of the union should not exceed 64 bits if the default
  17988. value types are used.
  17989. @since version 1.0.0
  17990. */
  17991. union json_value
  17992. {
  17993. /// object (stored with pointer to save storage)
  17994. object_t* object;
  17995. /// array (stored with pointer to save storage)
  17996. array_t* array;
  17997. /// string (stored with pointer to save storage)
  17998. string_t* string;
  17999. /// binary (stored with pointer to save storage)
  18000. binary_t* binary;
  18001. /// boolean
  18002. boolean_t boolean;
  18003. /// number (integer)
  18004. number_integer_t number_integer;
  18005. /// number (unsigned integer)
  18006. number_unsigned_t number_unsigned;
  18007. /// number (floating-point)
  18008. number_float_t number_float;
  18009. /// default constructor (for null values)
  18010. json_value() = default;
  18011. /// constructor for booleans
  18012. json_value(boolean_t v) noexcept : boolean(v) {}
  18013. /// constructor for numbers (integer)
  18014. json_value(number_integer_t v) noexcept : number_integer(v) {}
  18015. /// constructor for numbers (unsigned)
  18016. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  18017. /// constructor for numbers (floating-point)
  18018. json_value(number_float_t v) noexcept : number_float(v) {}
  18019. /// constructor for empty values of a given type
  18020. json_value(value_t t)
  18021. {
  18022. switch (t)
  18023. {
  18024. case value_t::object:
  18025. {
  18026. object = create<object_t>();
  18027. break;
  18028. }
  18029. case value_t::array:
  18030. {
  18031. array = create<array_t>();
  18032. break;
  18033. }
  18034. case value_t::string:
  18035. {
  18036. string = create<string_t>("");
  18037. break;
  18038. }
  18039. case value_t::binary:
  18040. {
  18041. binary = create<binary_t>();
  18042. break;
  18043. }
  18044. case value_t::boolean:
  18045. {
  18046. boolean = static_cast<boolean_t>(false);
  18047. break;
  18048. }
  18049. case value_t::number_integer:
  18050. {
  18051. number_integer = static_cast<number_integer_t>(0);
  18052. break;
  18053. }
  18054. case value_t::number_unsigned:
  18055. {
  18056. number_unsigned = static_cast<number_unsigned_t>(0);
  18057. break;
  18058. }
  18059. case value_t::number_float:
  18060. {
  18061. number_float = static_cast<number_float_t>(0.0);
  18062. break;
  18063. }
  18064. case value_t::null:
  18065. {
  18066. object = nullptr; // silence warning, see #821
  18067. break;
  18068. }
  18069. case value_t::discarded:
  18070. default:
  18071. {
  18072. object = nullptr; // silence warning, see #821
  18073. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  18074. {
  18075. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.12.0", nullptr)); // LCOV_EXCL_LINE
  18076. }
  18077. break;
  18078. }
  18079. }
  18080. }
  18081. /// constructor for strings
  18082. json_value(const string_t& value) : string(create<string_t>(value)) {}
  18083. /// constructor for rvalue strings
  18084. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  18085. /// constructor for objects
  18086. json_value(const object_t& value) : object(create<object_t>(value)) {}
  18087. /// constructor for rvalue objects
  18088. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  18089. /// constructor for arrays
  18090. json_value(const array_t& value) : array(create<array_t>(value)) {}
  18091. /// constructor for rvalue arrays
  18092. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  18093. /// constructor for binary arrays
  18094. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  18095. /// constructor for rvalue binary arrays
  18096. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  18097. /// constructor for binary arrays (internal type)
  18098. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  18099. /// constructor for rvalue binary arrays (internal type)
  18100. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  18101. void destroy(value_t t)
  18102. {
  18103. if (
  18104. (t == value_t::object && object == nullptr) ||
  18105. (t == value_t::array && array == nullptr) ||
  18106. (t == value_t::string && string == nullptr) ||
  18107. (t == value_t::binary && binary == nullptr)
  18108. )
  18109. {
  18110. // not initialized (e.g., due to exception in the ctor)
  18111. return;
  18112. }
  18113. if (t == value_t::array || t == value_t::object)
  18114. {
  18115. // flatten the current json_value to a heap-allocated stack
  18116. std::vector<basic_json> stack;
  18117. // move the top-level items to stack
  18118. if (t == value_t::array)
  18119. {
  18120. stack.reserve(array->size());
  18121. std::move(array->begin(), array->end(), std::back_inserter(stack));
  18122. }
  18123. else
  18124. {
  18125. stack.reserve(object->size());
  18126. for (auto&& it : *object)
  18127. {
  18128. stack.push_back(std::move(it.second));
  18129. }
  18130. }
  18131. while (!stack.empty())
  18132. {
  18133. // move the last item to a local variable to be processed
  18134. basic_json current_item(std::move(stack.back()));
  18135. stack.pop_back();
  18136. // if current_item is array/object, move
  18137. // its children to the stack to be processed later
  18138. if (current_item.is_array())
  18139. {
  18140. std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
  18141. current_item.m_data.m_value.array->clear();
  18142. }
  18143. else if (current_item.is_object())
  18144. {
  18145. for (auto&& it : *current_item.m_data.m_value.object)
  18146. {
  18147. stack.push_back(std::move(it.second));
  18148. }
  18149. current_item.m_data.m_value.object->clear();
  18150. }
  18151. // it's now safe that current_item gets destructed
  18152. // since it doesn't have any children
  18153. }
  18154. }
  18155. switch (t)
  18156. {
  18157. case value_t::object:
  18158. {
  18159. AllocatorType<object_t> alloc;
  18160. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  18161. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  18162. break;
  18163. }
  18164. case value_t::array:
  18165. {
  18166. AllocatorType<array_t> alloc;
  18167. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  18168. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  18169. break;
  18170. }
  18171. case value_t::string:
  18172. {
  18173. AllocatorType<string_t> alloc;
  18174. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  18175. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  18176. break;
  18177. }
  18178. case value_t::binary:
  18179. {
  18180. AllocatorType<binary_t> alloc;
  18181. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  18182. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  18183. break;
  18184. }
  18185. case value_t::null:
  18186. case value_t::boolean:
  18187. case value_t::number_integer:
  18188. case value_t::number_unsigned:
  18189. case value_t::number_float:
  18190. case value_t::discarded:
  18191. default:
  18192. {
  18193. break;
  18194. }
  18195. }
  18196. }
  18197. };
  18198. private:
  18199. /*!
  18200. @brief checks the class invariants
  18201. This function asserts the class invariants. It needs to be called at the
  18202. end of every constructor to make sure that created objects respect the
  18203. invariant. Furthermore, it has to be called each time the type of a JSON
  18204. value is changed, because the invariant expresses a relationship between
  18205. @a m_type and @a m_value.
  18206. Furthermore, the parent relation is checked for arrays and objects: If
  18207. @a check_parents true and the value is an array or object, then the
  18208. container's elements must have the current value as parent.
  18209. @param[in] check_parents whether the parent relation should be checked.
  18210. The value is true by default and should only be set to false
  18211. during destruction of objects when the invariant does not
  18212. need to hold.
  18213. */
  18214. void assert_invariant(bool check_parents = true) const noexcept
  18215. {
  18216. JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
  18217. JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
  18218. JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
  18219. JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
  18220. #if JSON_DIAGNOSTICS
  18221. JSON_TRY
  18222. {
  18223. // cppcheck-suppress assertWithSideEffect
  18224. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  18225. {
  18226. return j.m_parent == this;
  18227. }));
  18228. }
  18229. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  18230. #endif
  18231. static_cast<void>(check_parents);
  18232. }
  18233. void set_parents()
  18234. {
  18235. #if JSON_DIAGNOSTICS
  18236. switch (m_data.m_type)
  18237. {
  18238. case value_t::array:
  18239. {
  18240. for (auto& element : *m_data.m_value.array)
  18241. {
  18242. element.m_parent = this;
  18243. }
  18244. break;
  18245. }
  18246. case value_t::object:
  18247. {
  18248. for (auto& element : *m_data.m_value.object)
  18249. {
  18250. element.second.m_parent = this;
  18251. }
  18252. break;
  18253. }
  18254. case value_t::null:
  18255. case value_t::string:
  18256. case value_t::boolean:
  18257. case value_t::number_integer:
  18258. case value_t::number_unsigned:
  18259. case value_t::number_float:
  18260. case value_t::binary:
  18261. case value_t::discarded:
  18262. default:
  18263. break;
  18264. }
  18265. #endif
  18266. }
  18267. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  18268. {
  18269. #if JSON_DIAGNOSTICS
  18270. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  18271. {
  18272. (it + i)->m_parent = this;
  18273. }
  18274. #else
  18275. static_cast<void>(count_set_parents);
  18276. #endif
  18277. return it;
  18278. }
  18279. reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
  18280. {
  18281. #if JSON_DIAGNOSTICS
  18282. if (old_capacity != detail::unknown_size())
  18283. {
  18284. // see https://github.com/nlohmann/json/issues/2838
  18285. JSON_ASSERT(type() == value_t::array);
  18286. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  18287. {
  18288. // capacity has changed: update all parents
  18289. set_parents();
  18290. return j;
  18291. }
  18292. }
  18293. // ordered_json uses a vector internally, so pointers could have
  18294. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  18295. #ifdef JSON_HEDLEY_MSVC_VERSION
  18296. #pragma warning(push )
  18297. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  18298. #endif
  18299. if (detail::is_ordered_map<object_t>::value)
  18300. {
  18301. set_parents();
  18302. return j;
  18303. }
  18304. #ifdef JSON_HEDLEY_MSVC_VERSION
  18305. #pragma warning( pop )
  18306. #endif
  18307. j.m_parent = this;
  18308. #else
  18309. static_cast<void>(j);
  18310. static_cast<void>(old_capacity);
  18311. #endif
  18312. return j;
  18313. }
  18314. public:
  18315. //////////////////////////
  18316. // JSON parser callback //
  18317. //////////////////////////
  18318. /// @brief parser event types
  18319. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  18320. using parse_event_t = detail::parse_event_t;
  18321. /// @brief per-element parser callback type
  18322. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  18323. using parser_callback_t = detail::parser_callback_t<basic_json>;
  18324. //////////////////
  18325. // constructors //
  18326. //////////////////
  18327. /// @name constructors and destructors
  18328. /// Constructors of class @ref basic_json, copy/move constructor, copy
  18329. /// assignment, static functions creating objects, and the destructor.
  18330. /// @{
  18331. /// @brief create an empty value with a given type
  18332. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18333. basic_json(const value_t v)
  18334. : m_data(v)
  18335. {
  18336. assert_invariant();
  18337. }
  18338. /// @brief create a null object
  18339. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18340. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  18341. : basic_json(value_t::null)
  18342. {
  18343. assert_invariant();
  18344. }
  18345. /// @brief create a JSON value from compatible types
  18346. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18347. template < typename CompatibleType,
  18348. typename U = detail::uncvref_t<CompatibleType>,
  18349. detail::enable_if_t <
  18350. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  18351. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  18352. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  18353. std::forward<CompatibleType>(val))))
  18354. {
  18355. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  18356. set_parents();
  18357. assert_invariant();
  18358. }
  18359. /// @brief create a JSON value from an existing one
  18360. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18361. template < typename BasicJsonType,
  18362. detail::enable_if_t <
  18363. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  18364. basic_json(const BasicJsonType& val)
  18365. #if JSON_DIAGNOSTIC_POSITIONS
  18366. : start_position(val.start_pos()),
  18367. end_position(val.end_pos())
  18368. #endif
  18369. {
  18370. using other_boolean_t = typename BasicJsonType::boolean_t;
  18371. using other_number_float_t = typename BasicJsonType::number_float_t;
  18372. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  18373. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  18374. using other_string_t = typename BasicJsonType::string_t;
  18375. using other_object_t = typename BasicJsonType::object_t;
  18376. using other_array_t = typename BasicJsonType::array_t;
  18377. using other_binary_t = typename BasicJsonType::binary_t;
  18378. switch (val.type())
  18379. {
  18380. case value_t::boolean:
  18381. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  18382. break;
  18383. case value_t::number_float:
  18384. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  18385. break;
  18386. case value_t::number_integer:
  18387. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  18388. break;
  18389. case value_t::number_unsigned:
  18390. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  18391. break;
  18392. case value_t::string:
  18393. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  18394. break;
  18395. case value_t::object:
  18396. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  18397. break;
  18398. case value_t::array:
  18399. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  18400. break;
  18401. case value_t::binary:
  18402. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  18403. break;
  18404. case value_t::null:
  18405. *this = nullptr;
  18406. break;
  18407. case value_t::discarded:
  18408. m_data.m_type = value_t::discarded;
  18409. break;
  18410. default: // LCOV_EXCL_LINE
  18411. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  18412. }
  18413. JSON_ASSERT(m_data.m_type == val.type());
  18414. set_parents();
  18415. assert_invariant();
  18416. }
  18417. /// @brief create a container (array or object) from an initializer list
  18418. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18419. basic_json(initializer_list_t init,
  18420. bool type_deduction = true,
  18421. value_t manual_type = value_t::array)
  18422. {
  18423. // check if each element is an array with two elements whose first
  18424. // element is a string
  18425. bool is_an_object = std::all_of(init.begin(), init.end(),
  18426. [](const detail::json_ref<basic_json>& element_ref)
  18427. {
  18428. // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
  18429. // (many string types can be constructed from 0 via its null-pointer guise, so we get a
  18430. // broken call to op[key_type], the wrong semantics, and a 4804 warning on Windows)
  18431. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
  18432. });
  18433. // adjust type if type deduction is not wanted
  18434. if (!type_deduction)
  18435. {
  18436. // if an array is wanted, do not create an object though possible
  18437. if (manual_type == value_t::array)
  18438. {
  18439. is_an_object = false;
  18440. }
  18441. // if an object is wanted but impossible, throw an exception
  18442. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  18443. {
  18444. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  18445. }
  18446. }
  18447. if (is_an_object)
  18448. {
  18449. // the initializer list is a list of pairs -> create an object
  18450. m_data.m_type = value_t::object;
  18451. m_data.m_value = value_t::object;
  18452. for (auto& element_ref : init)
  18453. {
  18454. auto element = element_ref.moved_or_copied();
  18455. m_data.m_value.object->emplace(
  18456. std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
  18457. std::move((*element.m_data.m_value.array)[1]));
  18458. }
  18459. }
  18460. else
  18461. {
  18462. // the initializer list describes an array -> create an array
  18463. m_data.m_type = value_t::array;
  18464. m_data.m_value.array = create<array_t>(init.begin(), init.end());
  18465. }
  18466. set_parents();
  18467. assert_invariant();
  18468. }
  18469. /// @brief explicitly create a binary array (without subtype)
  18470. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18471. JSON_HEDLEY_WARN_UNUSED_RESULT
  18472. static basic_json binary(const typename binary_t::container_type& init)
  18473. {
  18474. auto res = basic_json();
  18475. res.m_data.m_type = value_t::binary;
  18476. res.m_data.m_value = init;
  18477. return res;
  18478. }
  18479. /// @brief explicitly create a binary array (with subtype)
  18480. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18481. JSON_HEDLEY_WARN_UNUSED_RESULT
  18482. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  18483. {
  18484. auto res = basic_json();
  18485. res.m_data.m_type = value_t::binary;
  18486. res.m_data.m_value = binary_t(init, subtype);
  18487. return res;
  18488. }
  18489. /// @brief explicitly create a binary array
  18490. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18491. JSON_HEDLEY_WARN_UNUSED_RESULT
  18492. static basic_json binary(typename binary_t::container_type&& init)
  18493. {
  18494. auto res = basic_json();
  18495. res.m_data.m_type = value_t::binary;
  18496. res.m_data.m_value = std::move(init);
  18497. return res;
  18498. }
  18499. /// @brief explicitly create a binary array (with subtype)
  18500. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  18501. JSON_HEDLEY_WARN_UNUSED_RESULT
  18502. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  18503. {
  18504. auto res = basic_json();
  18505. res.m_data.m_type = value_t::binary;
  18506. res.m_data.m_value = binary_t(std::move(init), subtype);
  18507. return res;
  18508. }
  18509. /// @brief explicitly create an array from an initializer list
  18510. /// @sa https://json.nlohmann.me/api/basic_json/array/
  18511. JSON_HEDLEY_WARN_UNUSED_RESULT
  18512. static basic_json array(initializer_list_t init = {})
  18513. {
  18514. return basic_json(init, false, value_t::array);
  18515. }
  18516. /// @brief explicitly create an object from an initializer list
  18517. /// @sa https://json.nlohmann.me/api/basic_json/object/
  18518. JSON_HEDLEY_WARN_UNUSED_RESULT
  18519. static basic_json object(initializer_list_t init = {})
  18520. {
  18521. return basic_json(init, false, value_t::object);
  18522. }
  18523. /// @brief construct an array with count copies of given value
  18524. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18525. basic_json(size_type cnt, const basic_json& val):
  18526. m_data{cnt, val}
  18527. {
  18528. set_parents();
  18529. assert_invariant();
  18530. }
  18531. /// @brief construct a JSON container given an iterator range
  18532. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18533. template < class InputIT, typename std::enable_if <
  18534. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  18535. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  18536. basic_json(InputIT first, InputIT last) // NOLINT(performance-unnecessary-value-param)
  18537. {
  18538. JSON_ASSERT(first.m_object != nullptr);
  18539. JSON_ASSERT(last.m_object != nullptr);
  18540. // make sure the iterator fits the current value
  18541. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  18542. {
  18543. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  18544. }
  18545. // copy type from the first iterator
  18546. m_data.m_type = first.m_object->m_data.m_type;
  18547. // check if the iterator range is complete for primitive values
  18548. switch (m_data.m_type)
  18549. {
  18550. case value_t::boolean:
  18551. case value_t::number_float:
  18552. case value_t::number_integer:
  18553. case value_t::number_unsigned:
  18554. case value_t::string:
  18555. {
  18556. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  18557. || !last.m_it.primitive_iterator.is_end()))
  18558. {
  18559. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  18560. }
  18561. break;
  18562. }
  18563. case value_t::null:
  18564. case value_t::object:
  18565. case value_t::array:
  18566. case value_t::binary:
  18567. case value_t::discarded:
  18568. default:
  18569. break;
  18570. }
  18571. switch (m_data.m_type)
  18572. {
  18573. case value_t::number_integer:
  18574. {
  18575. m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
  18576. break;
  18577. }
  18578. case value_t::number_unsigned:
  18579. {
  18580. m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
  18581. break;
  18582. }
  18583. case value_t::number_float:
  18584. {
  18585. m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
  18586. break;
  18587. }
  18588. case value_t::boolean:
  18589. {
  18590. m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
  18591. break;
  18592. }
  18593. case value_t::string:
  18594. {
  18595. m_data.m_value = *first.m_object->m_data.m_value.string;
  18596. break;
  18597. }
  18598. case value_t::object:
  18599. {
  18600. m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
  18601. last.m_it.object_iterator);
  18602. break;
  18603. }
  18604. case value_t::array:
  18605. {
  18606. m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
  18607. last.m_it.array_iterator);
  18608. break;
  18609. }
  18610. case value_t::binary:
  18611. {
  18612. m_data.m_value = *first.m_object->m_data.m_value.binary;
  18613. break;
  18614. }
  18615. case value_t::null:
  18616. case value_t::discarded:
  18617. default:
  18618. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  18619. }
  18620. set_parents();
  18621. assert_invariant();
  18622. }
  18623. ///////////////////////////////////////
  18624. // other constructors and destructor //
  18625. ///////////////////////////////////////
  18626. template<typename JsonRef,
  18627. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  18628. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  18629. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  18630. /// @brief copy constructor
  18631. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18632. basic_json(const basic_json& other)
  18633. : json_base_class_t(other)
  18634. #if JSON_DIAGNOSTIC_POSITIONS
  18635. , start_position(other.start_position)
  18636. , end_position(other.end_position)
  18637. #endif
  18638. {
  18639. m_data.m_type = other.m_data.m_type;
  18640. // check of passed value is valid
  18641. other.assert_invariant();
  18642. switch (m_data.m_type)
  18643. {
  18644. case value_t::object:
  18645. {
  18646. m_data.m_value = *other.m_data.m_value.object;
  18647. break;
  18648. }
  18649. case value_t::array:
  18650. {
  18651. m_data.m_value = *other.m_data.m_value.array;
  18652. break;
  18653. }
  18654. case value_t::string:
  18655. {
  18656. m_data.m_value = *other.m_data.m_value.string;
  18657. break;
  18658. }
  18659. case value_t::boolean:
  18660. {
  18661. m_data.m_value = other.m_data.m_value.boolean;
  18662. break;
  18663. }
  18664. case value_t::number_integer:
  18665. {
  18666. m_data.m_value = other.m_data.m_value.number_integer;
  18667. break;
  18668. }
  18669. case value_t::number_unsigned:
  18670. {
  18671. m_data.m_value = other.m_data.m_value.number_unsigned;
  18672. break;
  18673. }
  18674. case value_t::number_float:
  18675. {
  18676. m_data.m_value = other.m_data.m_value.number_float;
  18677. break;
  18678. }
  18679. case value_t::binary:
  18680. {
  18681. m_data.m_value = *other.m_data.m_value.binary;
  18682. break;
  18683. }
  18684. case value_t::null:
  18685. case value_t::discarded:
  18686. default:
  18687. break;
  18688. }
  18689. set_parents();
  18690. assert_invariant();
  18691. }
  18692. /// @brief move constructor
  18693. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  18694. basic_json(basic_json&& other) noexcept
  18695. : json_base_class_t(std::forward<json_base_class_t>(other)),
  18696. m_data(std::move(other.m_data)) // cppcheck-suppress[accessForwarded] TODO check
  18697. #if JSON_DIAGNOSTIC_POSITIONS
  18698. , start_position(other.start_position) // cppcheck-suppress[accessForwarded] TODO check
  18699. , end_position(other.end_position) // cppcheck-suppress[accessForwarded] TODO check
  18700. #endif
  18701. {
  18702. // check that the passed value is valid
  18703. other.assert_invariant(false); // cppcheck-suppress[accessForwarded]
  18704. // invalidate payload
  18705. other.m_data.m_type = value_t::null;
  18706. other.m_data.m_value = {};
  18707. #if JSON_DIAGNOSTIC_POSITIONS
  18708. other.start_position = std::string::npos;
  18709. other.end_position = std::string::npos;
  18710. #endif
  18711. set_parents();
  18712. assert_invariant();
  18713. }
  18714. /// @brief copy assignment
  18715. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  18716. basic_json& operator=(basic_json other) noexcept ( // NOLINT(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
  18717. std::is_nothrow_move_constructible<value_t>::value&&
  18718. std::is_nothrow_move_assignable<value_t>::value&&
  18719. std::is_nothrow_move_constructible<json_value>::value&&
  18720. std::is_nothrow_move_assignable<json_value>::value&&
  18721. std::is_nothrow_move_assignable<json_base_class_t>::value
  18722. )
  18723. {
  18724. // check that the passed value is valid
  18725. other.assert_invariant();
  18726. using std::swap;
  18727. swap(m_data.m_type, other.m_data.m_type);
  18728. swap(m_data.m_value, other.m_data.m_value);
  18729. #if JSON_DIAGNOSTIC_POSITIONS
  18730. swap(start_position, other.start_position);
  18731. swap(end_position, other.end_position);
  18732. #endif
  18733. json_base_class_t::operator=(std::move(other));
  18734. set_parents();
  18735. assert_invariant();
  18736. return *this;
  18737. }
  18738. /// @brief destructor
  18739. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  18740. ~basic_json() noexcept
  18741. {
  18742. assert_invariant(false);
  18743. }
  18744. /// @}
  18745. public:
  18746. ///////////////////////
  18747. // object inspection //
  18748. ///////////////////////
  18749. /// @name object inspection
  18750. /// Functions to inspect the type of a JSON value.
  18751. /// @{
  18752. /// @brief serialization
  18753. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  18754. string_t dump(const int indent = -1,
  18755. const char indent_char = ' ',
  18756. const bool ensure_ascii = false,
  18757. const error_handler_t error_handler = error_handler_t::strict) const
  18758. {
  18759. string_t result;
  18760. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  18761. if (indent >= 0)
  18762. {
  18763. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  18764. }
  18765. else
  18766. {
  18767. s.dump(*this, false, ensure_ascii, 0);
  18768. }
  18769. return result;
  18770. }
  18771. /// @brief return the type of the JSON value (explicit)
  18772. /// @sa https://json.nlohmann.me/api/basic_json/type/
  18773. constexpr value_t type() const noexcept
  18774. {
  18775. return m_data.m_type;
  18776. }
  18777. /// @brief return whether type is primitive
  18778. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  18779. constexpr bool is_primitive() const noexcept
  18780. {
  18781. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  18782. }
  18783. /// @brief return whether type is structured
  18784. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  18785. constexpr bool is_structured() const noexcept
  18786. {
  18787. return is_array() || is_object();
  18788. }
  18789. /// @brief return whether value is null
  18790. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  18791. constexpr bool is_null() const noexcept
  18792. {
  18793. return m_data.m_type == value_t::null;
  18794. }
  18795. /// @brief return whether value is a boolean
  18796. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  18797. constexpr bool is_boolean() const noexcept
  18798. {
  18799. return m_data.m_type == value_t::boolean;
  18800. }
  18801. /// @brief return whether value is a number
  18802. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  18803. constexpr bool is_number() const noexcept
  18804. {
  18805. return is_number_integer() || is_number_float();
  18806. }
  18807. /// @brief return whether value is an integer number
  18808. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  18809. constexpr bool is_number_integer() const noexcept
  18810. {
  18811. return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
  18812. }
  18813. /// @brief return whether value is an unsigned integer number
  18814. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  18815. constexpr bool is_number_unsigned() const noexcept
  18816. {
  18817. return m_data.m_type == value_t::number_unsigned;
  18818. }
  18819. /// @brief return whether value is a floating-point number
  18820. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  18821. constexpr bool is_number_float() const noexcept
  18822. {
  18823. return m_data.m_type == value_t::number_float;
  18824. }
  18825. /// @brief return whether value is an object
  18826. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  18827. constexpr bool is_object() const noexcept
  18828. {
  18829. return m_data.m_type == value_t::object;
  18830. }
  18831. /// @brief return whether value is an array
  18832. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  18833. constexpr bool is_array() const noexcept
  18834. {
  18835. return m_data.m_type == value_t::array;
  18836. }
  18837. /// @brief return whether value is a string
  18838. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  18839. constexpr bool is_string() const noexcept
  18840. {
  18841. return m_data.m_type == value_t::string;
  18842. }
  18843. /// @brief return whether value is a binary array
  18844. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  18845. constexpr bool is_binary() const noexcept
  18846. {
  18847. return m_data.m_type == value_t::binary;
  18848. }
  18849. /// @brief return whether value is discarded
  18850. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  18851. constexpr bool is_discarded() const noexcept
  18852. {
  18853. return m_data.m_type == value_t::discarded;
  18854. }
  18855. /// @brief return the type of the JSON value (implicit)
  18856. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  18857. constexpr operator value_t() const noexcept
  18858. {
  18859. return m_data.m_type;
  18860. }
  18861. /// @}
  18862. private:
  18863. //////////////////
  18864. // value access //
  18865. //////////////////
  18866. /// get a boolean (explicit)
  18867. boolean_t get_impl(boolean_t* /*unused*/) const
  18868. {
  18869. if (JSON_HEDLEY_LIKELY(is_boolean()))
  18870. {
  18871. return m_data.m_value.boolean;
  18872. }
  18873. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  18874. }
  18875. /// get a pointer to the value (object)
  18876. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  18877. {
  18878. return is_object() ? m_data.m_value.object : nullptr;
  18879. }
  18880. /// get a pointer to the value (object)
  18881. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  18882. {
  18883. return is_object() ? m_data.m_value.object : nullptr;
  18884. }
  18885. /// get a pointer to the value (array)
  18886. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  18887. {
  18888. return is_array() ? m_data.m_value.array : nullptr;
  18889. }
  18890. /// get a pointer to the value (array)
  18891. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  18892. {
  18893. return is_array() ? m_data.m_value.array : nullptr;
  18894. }
  18895. /// get a pointer to the value (string)
  18896. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  18897. {
  18898. return is_string() ? m_data.m_value.string : nullptr;
  18899. }
  18900. /// get a pointer to the value (string)
  18901. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  18902. {
  18903. return is_string() ? m_data.m_value.string : nullptr;
  18904. }
  18905. /// get a pointer to the value (boolean)
  18906. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  18907. {
  18908. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18909. }
  18910. /// get a pointer to the value (boolean)
  18911. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  18912. {
  18913. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18914. }
  18915. /// get a pointer to the value (integer number)
  18916. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  18917. {
  18918. return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
  18919. }
  18920. /// get a pointer to the value (integer number)
  18921. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  18922. {
  18923. return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
  18924. }
  18925. /// get a pointer to the value (unsigned number)
  18926. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  18927. {
  18928. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18929. }
  18930. /// get a pointer to the value (unsigned number)
  18931. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  18932. {
  18933. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18934. }
  18935. /// get a pointer to the value (floating-point number)
  18936. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  18937. {
  18938. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18939. }
  18940. /// get a pointer to the value (floating-point number)
  18941. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  18942. {
  18943. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18944. }
  18945. /// get a pointer to the value (binary)
  18946. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  18947. {
  18948. return is_binary() ? m_data.m_value.binary : nullptr;
  18949. }
  18950. /// get a pointer to the value (binary)
  18951. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  18952. {
  18953. return is_binary() ? m_data.m_value.binary : nullptr;
  18954. }
  18955. /*!
  18956. @brief helper function to implement get_ref()
  18957. This function helps to implement get_ref() without code duplication for
  18958. const and non-const overloads
  18959. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  18960. @throw type_error.303 if ReferenceType does not match underlying value
  18961. type of the current JSON
  18962. */
  18963. template<typename ReferenceType, typename ThisType>
  18964. static ReferenceType get_ref_impl(ThisType& obj)
  18965. {
  18966. // delegate the call to get_ptr<>()
  18967. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18968. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18969. {
  18970. return *ptr;
  18971. }
  18972. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18973. }
  18974. public:
  18975. /// @name value access
  18976. /// Direct access to the stored value of a JSON value.
  18977. /// @{
  18978. /// @brief get a pointer value (implicit)
  18979. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18980. template<typename PointerType, typename std::enable_if<
  18981. std::is_pointer<PointerType>::value, int>::type = 0>
  18982. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18983. {
  18984. // delegate the call to get_impl_ptr<>()
  18985. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18986. }
  18987. /// @brief get a pointer value (implicit)
  18988. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18989. template < typename PointerType, typename std::enable_if <
  18990. std::is_pointer<PointerType>::value&&
  18991. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18992. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18993. {
  18994. // delegate the call to get_impl_ptr<>() const
  18995. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18996. }
  18997. private:
  18998. /*!
  18999. @brief get a value (explicit)
  19000. Explicit type conversion between the JSON value and a compatible value
  19001. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  19002. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  19003. The value is converted by calling the @ref json_serializer<ValueType>
  19004. `from_json()` method.
  19005. The function is equivalent to executing
  19006. @code {.cpp}
  19007. ValueType ret;
  19008. JSONSerializer<ValueType>::from_json(*this, ret);
  19009. return ret;
  19010. @endcode
  19011. This overloads is chosen if:
  19012. - @a ValueType is not @ref basic_json,
  19013. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  19014. `void from_json(const basic_json&, ValueType&)`, and
  19015. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  19016. the form `ValueType from_json(const basic_json&)`
  19017. @tparam ValueType the returned value type
  19018. @return copy of the JSON value, converted to @a ValueType
  19019. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  19020. @liveexample{The example below shows several conversions from JSON values
  19021. to other types. There a few things to note: (1) Floating-point numbers can
  19022. be converted to integers\, (2) A JSON array can be converted to a standard
  19023. `std::vector<short>`\, (3) A JSON object can be converted to C++
  19024. associative containers such as `std::unordered_map<std::string\,
  19025. json>`.,get__ValueType_const}
  19026. @since version 2.1.0
  19027. */
  19028. template < typename ValueType,
  19029. detail::enable_if_t <
  19030. detail::is_default_constructible<ValueType>::value&&
  19031. detail::has_from_json<basic_json_t, ValueType>::value,
  19032. int > = 0 >
  19033. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  19034. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  19035. {
  19036. auto ret = ValueType();
  19037. JSONSerializer<ValueType>::from_json(*this, ret);
  19038. return ret;
  19039. }
  19040. /*!
  19041. @brief get a value (explicit); special case
  19042. Explicit type conversion between the JSON value and a compatible value
  19043. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  19044. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  19045. The value is converted by calling the @ref json_serializer<ValueType>
  19046. `from_json()` method.
  19047. The function is equivalent to executing
  19048. @code {.cpp}
  19049. return JSONSerializer<ValueType>::from_json(*this);
  19050. @endcode
  19051. This overloads is chosen if:
  19052. - @a ValueType is not @ref basic_json and
  19053. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  19054. `ValueType from_json(const basic_json&)`
  19055. @note If @ref json_serializer<ValueType> has both overloads of
  19056. `from_json()`, this one is chosen.
  19057. @tparam ValueType the returned value type
  19058. @return copy of the JSON value, converted to @a ValueType
  19059. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  19060. @since version 2.1.0
  19061. */
  19062. template < typename ValueType,
  19063. detail::enable_if_t <
  19064. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  19065. int > = 0 >
  19066. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  19067. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  19068. {
  19069. return JSONSerializer<ValueType>::from_json(*this);
  19070. }
  19071. /*!
  19072. @brief get special-case overload
  19073. This overloads converts the current @ref basic_json in a different
  19074. @ref basic_json type
  19075. @tparam BasicJsonType == @ref basic_json
  19076. @return a copy of *this, converted into @a BasicJsonType
  19077. @complexity Depending on the implementation of the called `from_json()`
  19078. method.
  19079. @since version 3.2.0
  19080. */
  19081. template < typename BasicJsonType,
  19082. detail::enable_if_t <
  19083. detail::is_basic_json<BasicJsonType>::value,
  19084. int > = 0 >
  19085. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  19086. {
  19087. return *this;
  19088. }
  19089. /*!
  19090. @brief get special-case overload
  19091. This overloads avoids a lot of template boilerplate, it can be seen as the
  19092. identity method
  19093. @tparam BasicJsonType == @ref basic_json
  19094. @return a copy of *this
  19095. @complexity Constant.
  19096. @since version 2.1.0
  19097. */
  19098. template<typename BasicJsonType,
  19099. detail::enable_if_t<
  19100. std::is_same<BasicJsonType, basic_json_t>::value,
  19101. int> = 0>
  19102. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  19103. {
  19104. return *this;
  19105. }
  19106. /*!
  19107. @brief get a pointer value (explicit)
  19108. @copydoc get()
  19109. */
  19110. template<typename PointerType,
  19111. detail::enable_if_t<
  19112. std::is_pointer<PointerType>::value,
  19113. int> = 0>
  19114. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  19115. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  19116. {
  19117. // delegate the call to get_ptr
  19118. return get_ptr<PointerType>();
  19119. }
  19120. public:
  19121. /*!
  19122. @brief get a (pointer) value (explicit)
  19123. Performs explicit type conversion between the JSON value and a compatible value if required.
  19124. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  19125. No copies are made.
  19126. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  19127. from the current @ref basic_json.
  19128. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  19129. method.
  19130. @tparam ValueTypeCV the provided value type
  19131. @tparam ValueType the returned value type
  19132. @return copy of the JSON value, converted to @tparam ValueType if necessary
  19133. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  19134. @since version 2.1.0
  19135. */
  19136. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  19137. #if defined(JSON_HAS_CPP_14)
  19138. constexpr
  19139. #endif
  19140. auto get() const noexcept(
  19141. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  19142. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  19143. {
  19144. // we cannot static_assert on ValueTypeCV being non-const, because
  19145. // there is support for get<const basic_json_t>(), which is why we
  19146. // still need the uncvref
  19147. static_assert(!std::is_reference<ValueTypeCV>::value,
  19148. "get() cannot be used with reference types, you might want to use get_ref()");
  19149. return get_impl<ValueType>(detail::priority_tag<4> {});
  19150. }
  19151. /*!
  19152. @brief get a pointer value (explicit)
  19153. Explicit pointer access to the internally stored JSON value. No copies are
  19154. made.
  19155. @warning The pointer becomes invalid if the underlying JSON object
  19156. changes.
  19157. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  19158. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  19159. @ref number_unsigned_t, or @ref number_float_t.
  19160. @return pointer to the internally stored JSON value if the requested
  19161. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  19162. @complexity Constant.
  19163. @liveexample{The example below shows how pointers to internal values of a
  19164. JSON value can be requested. Note that no type conversions are made and a
  19165. `nullptr` is returned if the value and the requested pointer type does not
  19166. match.,get__PointerType}
  19167. @sa see @ref get_ptr() for explicit pointer-member access
  19168. @since version 1.0.0
  19169. */
  19170. template<typename PointerType, typename std::enable_if<
  19171. std::is_pointer<PointerType>::value, int>::type = 0>
  19172. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  19173. {
  19174. // delegate the call to get_ptr
  19175. return get_ptr<PointerType>();
  19176. }
  19177. /// @brief get a value (explicit)
  19178. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  19179. template < typename ValueType,
  19180. detail::enable_if_t <
  19181. !detail::is_basic_json<ValueType>::value&&
  19182. detail::has_from_json<basic_json_t, ValueType>::value,
  19183. int > = 0 >
  19184. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  19185. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  19186. {
  19187. JSONSerializer<ValueType>::from_json(*this, v);
  19188. return v;
  19189. }
  19190. // specialization to allow calling get_to with a basic_json value
  19191. // see https://github.com/nlohmann/json/issues/2175
  19192. template<typename ValueType,
  19193. detail::enable_if_t <
  19194. detail::is_basic_json<ValueType>::value,
  19195. int> = 0>
  19196. ValueType & get_to(ValueType& v) const
  19197. {
  19198. v = *this;
  19199. return v;
  19200. }
  19201. template <
  19202. typename T, std::size_t N,
  19203. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  19204. detail::enable_if_t <
  19205. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  19206. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  19207. noexcept(noexcept(JSONSerializer<Array>::from_json(
  19208. std::declval<const basic_json_t&>(), v)))
  19209. {
  19210. JSONSerializer<Array>::from_json(*this, v);
  19211. return v;
  19212. }
  19213. /// @brief get a reference value (implicit)
  19214. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  19215. template<typename ReferenceType, typename std::enable_if<
  19216. std::is_reference<ReferenceType>::value, int>::type = 0>
  19217. ReferenceType get_ref()
  19218. {
  19219. // delegate call to get_ref_impl
  19220. return get_ref_impl<ReferenceType>(*this);
  19221. }
  19222. /// @brief get a reference value (implicit)
  19223. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  19224. template < typename ReferenceType, typename std::enable_if <
  19225. std::is_reference<ReferenceType>::value&&
  19226. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  19227. ReferenceType get_ref() const
  19228. {
  19229. // delegate call to get_ref_impl
  19230. return get_ref_impl<ReferenceType>(*this);
  19231. }
  19232. /*!
  19233. @brief get a value (implicit)
  19234. Implicit type conversion between the JSON value and a compatible value.
  19235. The call is realized by calling @ref get() const.
  19236. @tparam ValueType non-pointer type compatible to the JSON value, for
  19237. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  19238. `std::vector` types for JSON arrays. The character type of @ref string_t
  19239. as well as an initializer list of this type is excluded to avoid
  19240. ambiguities as these types implicitly convert to `std::string`.
  19241. @return copy of the JSON value, converted to type @a ValueType
  19242. @throw type_error.302 in case passed type @a ValueType is incompatible
  19243. to the JSON value type (e.g., the JSON value is of type boolean, but a
  19244. string is requested); see example below
  19245. @complexity Linear in the size of the JSON value.
  19246. @liveexample{The example below shows several conversions from JSON values
  19247. to other types. There a few things to note: (1) Floating-point numbers can
  19248. be converted to integers\, (2) A JSON array can be converted to a standard
  19249. `std::vector<short>`\, (3) A JSON object can be converted to C++
  19250. associative containers such as `std::unordered_map<std::string\,
  19251. json>`.,operator__ValueType}
  19252. @since version 1.0.0
  19253. */
  19254. template < typename ValueType, typename std::enable_if <
  19255. detail::conjunction <
  19256. detail::negation<std::is_pointer<ValueType>>,
  19257. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  19258. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  19259. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  19260. detail::negation<detail::is_basic_json<ValueType>>,
  19261. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  19262. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  19263. detail::negation<std::is_same<ValueType, std::string_view>>,
  19264. #endif
  19265. #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
  19266. detail::negation<std::is_same<ValueType, std::any>>,
  19267. #endif
  19268. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  19269. >::value, int >::type = 0 >
  19270. JSON_EXPLICIT operator ValueType() const
  19271. {
  19272. // delegate the call to get<>() const
  19273. return get<ValueType>();
  19274. }
  19275. /// @brief get a binary value
  19276. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  19277. binary_t& get_binary()
  19278. {
  19279. if (!is_binary())
  19280. {
  19281. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  19282. }
  19283. return *get_ptr<binary_t*>();
  19284. }
  19285. /// @brief get a binary value
  19286. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  19287. const binary_t& get_binary() const
  19288. {
  19289. if (!is_binary())
  19290. {
  19291. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  19292. }
  19293. return *get_ptr<const binary_t*>();
  19294. }
  19295. /// @}
  19296. ////////////////////
  19297. // element access //
  19298. ////////////////////
  19299. /// @name element access
  19300. /// Access to the JSON value.
  19301. /// @{
  19302. /// @brief access specified array element with bounds checking
  19303. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19304. reference at(size_type idx)
  19305. {
  19306. // at only works for arrays
  19307. if (JSON_HEDLEY_LIKELY(is_array()))
  19308. {
  19309. JSON_TRY
  19310. {
  19311. return set_parent(m_data.m_value.array->at(idx));
  19312. }
  19313. JSON_CATCH (std::out_of_range&)
  19314. {
  19315. // create a better exception explanation
  19316. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19317. } // cppcheck-suppress[missingReturn]
  19318. }
  19319. else
  19320. {
  19321. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19322. }
  19323. }
  19324. /// @brief access specified array element with bounds checking
  19325. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19326. const_reference at(size_type idx) const
  19327. {
  19328. // at only works for arrays
  19329. if (JSON_HEDLEY_LIKELY(is_array()))
  19330. {
  19331. JSON_TRY
  19332. {
  19333. return m_data.m_value.array->at(idx);
  19334. }
  19335. JSON_CATCH (std::out_of_range&)
  19336. {
  19337. // create a better exception explanation
  19338. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19339. } // cppcheck-suppress[missingReturn]
  19340. }
  19341. else
  19342. {
  19343. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19344. }
  19345. }
  19346. /// @brief access specified object element with bounds checking
  19347. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19348. reference at(const typename object_t::key_type& key)
  19349. {
  19350. // at only works for objects
  19351. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19352. {
  19353. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19354. }
  19355. auto it = m_data.m_value.object->find(key);
  19356. if (it == m_data.m_value.object->end())
  19357. {
  19358. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  19359. }
  19360. return set_parent(it->second);
  19361. }
  19362. /// @brief access specified object element with bounds checking
  19363. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19364. template<class KeyType, detail::enable_if_t<
  19365. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19366. reference at(KeyType && key)
  19367. {
  19368. // at only works for objects
  19369. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19370. {
  19371. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19372. }
  19373. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19374. if (it == m_data.m_value.object->end())
  19375. {
  19376. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  19377. }
  19378. return set_parent(it->second);
  19379. }
  19380. /// @brief access specified object element with bounds checking
  19381. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19382. const_reference at(const typename object_t::key_type& key) const
  19383. {
  19384. // at only works for objects
  19385. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19386. {
  19387. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19388. }
  19389. auto it = m_data.m_value.object->find(key);
  19390. if (it == m_data.m_value.object->end())
  19391. {
  19392. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  19393. }
  19394. return it->second;
  19395. }
  19396. /// @brief access specified object element with bounds checking
  19397. /// @sa https://json.nlohmann.me/api/basic_json/at/
  19398. template<class KeyType, detail::enable_if_t<
  19399. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19400. const_reference at(KeyType && key) const
  19401. {
  19402. // at only works for objects
  19403. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19404. {
  19405. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  19406. }
  19407. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19408. if (it == m_data.m_value.object->end())
  19409. {
  19410. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  19411. }
  19412. return it->second;
  19413. }
  19414. /// @brief access specified array element
  19415. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19416. reference operator[](size_type idx)
  19417. {
  19418. // implicitly convert a null value to an empty array
  19419. if (is_null())
  19420. {
  19421. m_data.m_type = value_t::array;
  19422. m_data.m_value.array = create<array_t>();
  19423. assert_invariant();
  19424. }
  19425. // operator[] only works for arrays
  19426. if (JSON_HEDLEY_LIKELY(is_array()))
  19427. {
  19428. // fill up the array with null values if given idx is outside the range
  19429. if (idx >= m_data.m_value.array->size())
  19430. {
  19431. #if JSON_DIAGNOSTICS
  19432. // remember array size & capacity before resizing
  19433. const auto old_size = m_data.m_value.array->size();
  19434. const auto old_capacity = m_data.m_value.array->capacity();
  19435. #endif
  19436. m_data.m_value.array->resize(idx + 1);
  19437. #if JSON_DIAGNOSTICS
  19438. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  19439. {
  19440. // capacity has changed: update all parents
  19441. set_parents();
  19442. }
  19443. else
  19444. {
  19445. // set parent for values added above
  19446. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  19447. }
  19448. #endif
  19449. assert_invariant();
  19450. }
  19451. return m_data.m_value.array->operator[](idx);
  19452. }
  19453. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  19454. }
  19455. /// @brief access specified array element
  19456. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19457. const_reference operator[](size_type idx) const
  19458. {
  19459. // const operator[] only works for arrays
  19460. if (JSON_HEDLEY_LIKELY(is_array()))
  19461. {
  19462. return m_data.m_value.array->operator[](idx);
  19463. }
  19464. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  19465. }
  19466. /// @brief access specified object element
  19467. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19468. reference operator[](typename object_t::key_type key) // NOLINT(performance-unnecessary-value-param)
  19469. {
  19470. // implicitly convert a null value to an empty object
  19471. if (is_null())
  19472. {
  19473. m_data.m_type = value_t::object;
  19474. m_data.m_value.object = create<object_t>();
  19475. assert_invariant();
  19476. }
  19477. // operator[] only works for objects
  19478. if (JSON_HEDLEY_LIKELY(is_object()))
  19479. {
  19480. auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
  19481. return set_parent(result.first->second);
  19482. }
  19483. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19484. }
  19485. /// @brief access specified object element
  19486. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19487. const_reference operator[](const typename object_t::key_type& key) const
  19488. {
  19489. // const operator[] only works for objects
  19490. if (JSON_HEDLEY_LIKELY(is_object()))
  19491. {
  19492. auto it = m_data.m_value.object->find(key);
  19493. JSON_ASSERT(it != m_data.m_value.object->end());
  19494. return it->second;
  19495. }
  19496. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19497. }
  19498. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  19499. // (they seemingly cannot be constrained to resolve the ambiguity)
  19500. template<typename T>
  19501. reference operator[](T* key)
  19502. {
  19503. return operator[](typename object_t::key_type(key));
  19504. }
  19505. template<typename T>
  19506. const_reference operator[](T* key) const
  19507. {
  19508. return operator[](typename object_t::key_type(key));
  19509. }
  19510. /// @brief access specified object element
  19511. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19512. template<class KeyType, detail::enable_if_t<
  19513. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19514. reference operator[](KeyType && key)
  19515. {
  19516. // implicitly convert a null value to an empty object
  19517. if (is_null())
  19518. {
  19519. m_data.m_type = value_t::object;
  19520. m_data.m_value.object = create<object_t>();
  19521. assert_invariant();
  19522. }
  19523. // operator[] only works for objects
  19524. if (JSON_HEDLEY_LIKELY(is_object()))
  19525. {
  19526. auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  19527. return set_parent(result.first->second);
  19528. }
  19529. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19530. }
  19531. /// @brief access specified object element
  19532. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  19533. template<class KeyType, detail::enable_if_t<
  19534. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19535. const_reference operator[](KeyType && key) const
  19536. {
  19537. // const operator[] only works for objects
  19538. if (JSON_HEDLEY_LIKELY(is_object()))
  19539. {
  19540. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19541. JSON_ASSERT(it != m_data.m_value.object->end());
  19542. return it->second;
  19543. }
  19544. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  19545. }
  19546. private:
  19547. template<typename KeyType>
  19548. using is_comparable_with_object_key = detail::is_comparable <
  19549. object_comparator_t, const typename object_t::key_type&, KeyType >;
  19550. template<typename ValueType>
  19551. using value_return_type = std::conditional <
  19552. detail::is_c_string_uncvref<ValueType>::value,
  19553. string_t, typename std::decay<ValueType>::type >;
  19554. public:
  19555. /// @brief access specified object element with default value
  19556. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19557. template < class ValueType, detail::enable_if_t <
  19558. !detail::is_transparent<object_comparator_t>::value
  19559. && detail::is_getable<basic_json_t, ValueType>::value
  19560. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19561. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  19562. {
  19563. // value only works for objects
  19564. if (JSON_HEDLEY_LIKELY(is_object()))
  19565. {
  19566. // If 'key' is found, return its value. Otherwise, return `default_value'.
  19567. const auto it = find(key);
  19568. if (it != end())
  19569. {
  19570. return it->template get<ValueType>();
  19571. }
  19572. return default_value;
  19573. }
  19574. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19575. }
  19576. /// @brief access specified object element with default value
  19577. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19578. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  19579. detail::enable_if_t <
  19580. !detail::is_transparent<object_comparator_t>::value
  19581. && detail::is_getable<basic_json_t, ReturnType>::value
  19582. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19583. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  19584. {
  19585. // value only works for objects
  19586. if (JSON_HEDLEY_LIKELY(is_object()))
  19587. {
  19588. // If 'key' is found, return its value. Otherwise, return `default_value'.
  19589. const auto it = find(key);
  19590. if (it != end())
  19591. {
  19592. return it->template get<ReturnType>();
  19593. }
  19594. return std::forward<ValueType>(default_value);
  19595. }
  19596. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19597. }
  19598. /// @brief access specified object element with default value
  19599. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19600. template < class ValueType, class KeyType, detail::enable_if_t <
  19601. detail::is_transparent<object_comparator_t>::value
  19602. && !detail::is_json_pointer<KeyType>::value
  19603. && is_comparable_with_object_key<KeyType>::value
  19604. && detail::is_getable<basic_json_t, ValueType>::value
  19605. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19606. ValueType value(KeyType && key, const ValueType& default_value) const
  19607. {
  19608. // value only works for objects
  19609. if (JSON_HEDLEY_LIKELY(is_object()))
  19610. {
  19611. // If 'key' is found, return its value. Otherwise, return `default_value'.
  19612. const auto it = find(std::forward<KeyType>(key));
  19613. if (it != end())
  19614. {
  19615. return it->template get<ValueType>();
  19616. }
  19617. return default_value;
  19618. }
  19619. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19620. }
  19621. /// @brief access specified object element via JSON Pointer with default value
  19622. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19623. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  19624. detail::enable_if_t <
  19625. detail::is_transparent<object_comparator_t>::value
  19626. && !detail::is_json_pointer<KeyType>::value
  19627. && is_comparable_with_object_key<KeyType>::value
  19628. && detail::is_getable<basic_json_t, ReturnType>::value
  19629. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19630. ReturnType value(KeyType && key, ValueType && default_value) const
  19631. {
  19632. // value only works for objects
  19633. if (JSON_HEDLEY_LIKELY(is_object()))
  19634. {
  19635. // If 'key' is found, return its value. Otherwise, return `default_value'.
  19636. const auto it = find(std::forward<KeyType>(key));
  19637. if (it != end())
  19638. {
  19639. return it->template get<ReturnType>();
  19640. }
  19641. return std::forward<ValueType>(default_value);
  19642. }
  19643. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19644. }
  19645. /// @brief access specified object element via JSON Pointer with default value
  19646. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19647. template < class ValueType, detail::enable_if_t <
  19648. detail::is_getable<basic_json_t, ValueType>::value
  19649. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19650. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  19651. {
  19652. // value only works for objects
  19653. if (JSON_HEDLEY_LIKELY(is_object()))
  19654. {
  19655. // If the pointer resolves to a value, return it. Otherwise, return
  19656. // 'default_value'.
  19657. JSON_TRY
  19658. {
  19659. return ptr.get_checked(this).template get<ValueType>();
  19660. }
  19661. JSON_INTERNAL_CATCH (out_of_range&)
  19662. {
  19663. return default_value;
  19664. }
  19665. }
  19666. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19667. }
  19668. /// @brief access specified object element via JSON Pointer with default value
  19669. /// @sa https://json.nlohmann.me/api/basic_json/value/
  19670. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  19671. detail::enable_if_t <
  19672. detail::is_getable<basic_json_t, ReturnType>::value
  19673. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19674. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  19675. {
  19676. // value only works for objects
  19677. if (JSON_HEDLEY_LIKELY(is_object()))
  19678. {
  19679. // If the pointer resolves to a value, return it. Otherwise, return
  19680. // 'default_value'.
  19681. JSON_TRY
  19682. {
  19683. return ptr.get_checked(this).template get<ReturnType>();
  19684. }
  19685. JSON_INTERNAL_CATCH (out_of_range&)
  19686. {
  19687. return std::forward<ValueType>(default_value);
  19688. }
  19689. }
  19690. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  19691. }
  19692. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  19693. detail::is_basic_json<BasicJsonType>::value
  19694. && detail::is_getable<basic_json_t, ValueType>::value
  19695. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19696. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19697. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  19698. {
  19699. return value(ptr.convert(), default_value);
  19700. }
  19701. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  19702. detail::enable_if_t <
  19703. detail::is_basic_json<BasicJsonType>::value
  19704. && detail::is_getable<basic_json_t, ReturnType>::value
  19705. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  19706. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19707. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  19708. {
  19709. return value(ptr.convert(), std::forward<ValueType>(default_value));
  19710. }
  19711. /// @brief access the first element
  19712. /// @sa https://json.nlohmann.me/api/basic_json/front/
  19713. reference front()
  19714. {
  19715. return *begin();
  19716. }
  19717. /// @brief access the first element
  19718. /// @sa https://json.nlohmann.me/api/basic_json/front/
  19719. const_reference front() const
  19720. {
  19721. return *cbegin();
  19722. }
  19723. /// @brief access the last element
  19724. /// @sa https://json.nlohmann.me/api/basic_json/back/
  19725. reference back()
  19726. {
  19727. auto tmp = end();
  19728. --tmp;
  19729. return *tmp;
  19730. }
  19731. /// @brief access the last element
  19732. /// @sa https://json.nlohmann.me/api/basic_json/back/
  19733. const_reference back() const
  19734. {
  19735. auto tmp = cend();
  19736. --tmp;
  19737. return *tmp;
  19738. }
  19739. /// @brief remove element given an iterator
  19740. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19741. template < class IteratorType, detail::enable_if_t <
  19742. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  19743. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  19744. IteratorType erase(IteratorType pos) // NOLINT(performance-unnecessary-value-param)
  19745. {
  19746. // make sure the iterator fits the current value
  19747. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  19748. {
  19749. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19750. }
  19751. IteratorType result = end();
  19752. switch (m_data.m_type)
  19753. {
  19754. case value_t::boolean:
  19755. case value_t::number_float:
  19756. case value_t::number_integer:
  19757. case value_t::number_unsigned:
  19758. case value_t::string:
  19759. case value_t::binary:
  19760. {
  19761. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  19762. {
  19763. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  19764. }
  19765. if (is_string())
  19766. {
  19767. AllocatorType<string_t> alloc;
  19768. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  19769. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  19770. m_data.m_value.string = nullptr;
  19771. }
  19772. else if (is_binary())
  19773. {
  19774. AllocatorType<binary_t> alloc;
  19775. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  19776. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  19777. m_data.m_value.binary = nullptr;
  19778. }
  19779. m_data.m_type = value_t::null;
  19780. assert_invariant();
  19781. break;
  19782. }
  19783. case value_t::object:
  19784. {
  19785. result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
  19786. break;
  19787. }
  19788. case value_t::array:
  19789. {
  19790. result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
  19791. break;
  19792. }
  19793. case value_t::null:
  19794. case value_t::discarded:
  19795. default:
  19796. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19797. }
  19798. return result;
  19799. }
  19800. /// @brief remove elements given an iterator range
  19801. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19802. template < class IteratorType, detail::enable_if_t <
  19803. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  19804. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  19805. IteratorType erase(IteratorType first, IteratorType last) // NOLINT(performance-unnecessary-value-param)
  19806. {
  19807. // make sure the iterator fits the current value
  19808. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  19809. {
  19810. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  19811. }
  19812. IteratorType result = end();
  19813. switch (m_data.m_type)
  19814. {
  19815. case value_t::boolean:
  19816. case value_t::number_float:
  19817. case value_t::number_integer:
  19818. case value_t::number_unsigned:
  19819. case value_t::string:
  19820. case value_t::binary:
  19821. {
  19822. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  19823. || !last.m_it.primitive_iterator.is_end()))
  19824. {
  19825. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  19826. }
  19827. if (is_string())
  19828. {
  19829. AllocatorType<string_t> alloc;
  19830. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  19831. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  19832. m_data.m_value.string = nullptr;
  19833. }
  19834. else if (is_binary())
  19835. {
  19836. AllocatorType<binary_t> alloc;
  19837. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  19838. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  19839. m_data.m_value.binary = nullptr;
  19840. }
  19841. m_data.m_type = value_t::null;
  19842. assert_invariant();
  19843. break;
  19844. }
  19845. case value_t::object:
  19846. {
  19847. result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
  19848. last.m_it.object_iterator);
  19849. break;
  19850. }
  19851. case value_t::array:
  19852. {
  19853. result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
  19854. last.m_it.array_iterator);
  19855. break;
  19856. }
  19857. case value_t::null:
  19858. case value_t::discarded:
  19859. default:
  19860. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19861. }
  19862. return result;
  19863. }
  19864. private:
  19865. template < typename KeyType, detail::enable_if_t <
  19866. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19867. size_type erase_internal(KeyType && key)
  19868. {
  19869. // this erase only works for objects
  19870. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19871. {
  19872. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19873. }
  19874. return m_data.m_value.object->erase(std::forward<KeyType>(key));
  19875. }
  19876. template < typename KeyType, detail::enable_if_t <
  19877. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19878. size_type erase_internal(KeyType && key)
  19879. {
  19880. // this erase only works for objects
  19881. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19882. {
  19883. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19884. }
  19885. const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19886. if (it != m_data.m_value.object->end())
  19887. {
  19888. m_data.m_value.object->erase(it);
  19889. return 1;
  19890. }
  19891. return 0;
  19892. }
  19893. public:
  19894. /// @brief remove element from a JSON object given a key
  19895. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19896. size_type erase(const typename object_t::key_type& key)
  19897. {
  19898. // the indirection via erase_internal() is added to avoid making this
  19899. // function a template and thus de-rank it during overload resolution
  19900. return erase_internal(key);
  19901. }
  19902. /// @brief remove element from a JSON object given a key
  19903. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19904. template<class KeyType, detail::enable_if_t<
  19905. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19906. size_type erase(KeyType && key)
  19907. {
  19908. return erase_internal(std::forward<KeyType>(key));
  19909. }
  19910. /// @brief remove element from a JSON array given an index
  19911. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19912. void erase(const size_type idx)
  19913. {
  19914. // this erase only works for arrays
  19915. if (JSON_HEDLEY_LIKELY(is_array()))
  19916. {
  19917. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  19918. {
  19919. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19920. }
  19921. m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
  19922. }
  19923. else
  19924. {
  19925. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19926. }
  19927. }
  19928. /// @}
  19929. ////////////
  19930. // lookup //
  19931. ////////////
  19932. /// @name lookup
  19933. /// @{
  19934. /// @brief find an element in a JSON object
  19935. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19936. iterator find(const typename object_t::key_type& key)
  19937. {
  19938. auto result = end();
  19939. if (is_object())
  19940. {
  19941. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19942. }
  19943. return result;
  19944. }
  19945. /// @brief find an element in a JSON object
  19946. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19947. const_iterator find(const typename object_t::key_type& key) const
  19948. {
  19949. auto result = cend();
  19950. if (is_object())
  19951. {
  19952. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19953. }
  19954. return result;
  19955. }
  19956. /// @brief find an element in a JSON object
  19957. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19958. template<class KeyType, detail::enable_if_t<
  19959. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19960. iterator find(KeyType && key)
  19961. {
  19962. auto result = end();
  19963. if (is_object())
  19964. {
  19965. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19966. }
  19967. return result;
  19968. }
  19969. /// @brief find an element in a JSON object
  19970. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19971. template<class KeyType, detail::enable_if_t<
  19972. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19973. const_iterator find(KeyType && key) const
  19974. {
  19975. auto result = cend();
  19976. if (is_object())
  19977. {
  19978. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19979. }
  19980. return result;
  19981. }
  19982. /// @brief returns the number of occurrences of a key in a JSON object
  19983. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19984. size_type count(const typename object_t::key_type& key) const
  19985. {
  19986. // return 0 for all nonobject types
  19987. return is_object() ? m_data.m_value.object->count(key) : 0;
  19988. }
  19989. /// @brief returns the number of occurrences of a key in a JSON object
  19990. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19991. template<class KeyType, detail::enable_if_t<
  19992. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19993. size_type count(KeyType && key) const
  19994. {
  19995. // return 0 for all nonobject types
  19996. return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
  19997. }
  19998. /// @brief check the existence of an element in a JSON object
  19999. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  20000. bool contains(const typename object_t::key_type& key) const
  20001. {
  20002. return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
  20003. }
  20004. /// @brief check the existence of an element in a JSON object
  20005. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  20006. template<class KeyType, detail::enable_if_t<
  20007. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  20008. bool contains(KeyType && key) const
  20009. {
  20010. return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
  20011. }
  20012. /// @brief check the existence of an element in a JSON object given a JSON pointer
  20013. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  20014. bool contains(const json_pointer& ptr) const
  20015. {
  20016. return ptr.contains(this);
  20017. }
  20018. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20019. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20020. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20021. {
  20022. return ptr.contains(this);
  20023. }
  20024. /// @}
  20025. ///////////////
  20026. // iterators //
  20027. ///////////////
  20028. /// @name iterators
  20029. /// @{
  20030. /// @brief returns an iterator to the first element
  20031. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  20032. iterator begin() noexcept
  20033. {
  20034. iterator result(this);
  20035. result.set_begin();
  20036. return result;
  20037. }
  20038. /// @brief returns an iterator to the first element
  20039. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  20040. const_iterator begin() const noexcept
  20041. {
  20042. return cbegin();
  20043. }
  20044. /// @brief returns a const iterator to the first element
  20045. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  20046. const_iterator cbegin() const noexcept
  20047. {
  20048. const_iterator result(this);
  20049. result.set_begin();
  20050. return result;
  20051. }
  20052. /// @brief returns an iterator to one past the last element
  20053. /// @sa https://json.nlohmann.me/api/basic_json/end/
  20054. iterator end() noexcept
  20055. {
  20056. iterator result(this);
  20057. result.set_end();
  20058. return result;
  20059. }
  20060. /// @brief returns an iterator to one past the last element
  20061. /// @sa https://json.nlohmann.me/api/basic_json/end/
  20062. const_iterator end() const noexcept
  20063. {
  20064. return cend();
  20065. }
  20066. /// @brief returns an iterator to one past the last element
  20067. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  20068. const_iterator cend() const noexcept
  20069. {
  20070. const_iterator result(this);
  20071. result.set_end();
  20072. return result;
  20073. }
  20074. /// @brief returns an iterator to the reverse-beginning
  20075. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  20076. reverse_iterator rbegin() noexcept
  20077. {
  20078. return reverse_iterator(end());
  20079. }
  20080. /// @brief returns an iterator to the reverse-beginning
  20081. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  20082. const_reverse_iterator rbegin() const noexcept
  20083. {
  20084. return crbegin();
  20085. }
  20086. /// @brief returns an iterator to the reverse-end
  20087. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  20088. reverse_iterator rend() noexcept
  20089. {
  20090. return reverse_iterator(begin());
  20091. }
  20092. /// @brief returns an iterator to the reverse-end
  20093. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  20094. const_reverse_iterator rend() const noexcept
  20095. {
  20096. return crend();
  20097. }
  20098. /// @brief returns a const reverse iterator to the last element
  20099. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  20100. const_reverse_iterator crbegin() const noexcept
  20101. {
  20102. return const_reverse_iterator(cend());
  20103. }
  20104. /// @brief returns a const reverse iterator to one before the first
  20105. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  20106. const_reverse_iterator crend() const noexcept
  20107. {
  20108. return const_reverse_iterator(cbegin());
  20109. }
  20110. public:
  20111. /// @brief wrapper to access iterator member functions in range-based for
  20112. /// @sa https://json.nlohmann.me/api/basic_json/items/
  20113. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  20114. /// version 4.0.0 of the library. Please use @ref items() instead;
  20115. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  20116. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  20117. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  20118. {
  20119. return ref.items();
  20120. }
  20121. /// @brief wrapper to access iterator member functions in range-based for
  20122. /// @sa https://json.nlohmann.me/api/basic_json/items/
  20123. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  20124. /// version 4.0.0 of the library. Please use @ref items() instead;
  20125. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  20126. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  20127. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  20128. {
  20129. return ref.items();
  20130. }
  20131. /// @brief helper to access iterator member functions in range-based for
  20132. /// @sa https://json.nlohmann.me/api/basic_json/items/
  20133. iteration_proxy<iterator> items() noexcept
  20134. {
  20135. return iteration_proxy<iterator>(*this);
  20136. }
  20137. /// @brief helper to access iterator member functions in range-based for
  20138. /// @sa https://json.nlohmann.me/api/basic_json/items/
  20139. iteration_proxy<const_iterator> items() const noexcept
  20140. {
  20141. return iteration_proxy<const_iterator>(*this);
  20142. }
  20143. /// @}
  20144. //////////////
  20145. // capacity //
  20146. //////////////
  20147. /// @name capacity
  20148. /// @{
  20149. /// @brief checks whether the container is empty.
  20150. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  20151. bool empty() const noexcept
  20152. {
  20153. switch (m_data.m_type)
  20154. {
  20155. case value_t::null:
  20156. {
  20157. // null values are empty
  20158. return true;
  20159. }
  20160. case value_t::array:
  20161. {
  20162. // delegate call to array_t::empty()
  20163. return m_data.m_value.array->empty();
  20164. }
  20165. case value_t::object:
  20166. {
  20167. // delegate call to object_t::empty()
  20168. return m_data.m_value.object->empty();
  20169. }
  20170. case value_t::string:
  20171. case value_t::boolean:
  20172. case value_t::number_integer:
  20173. case value_t::number_unsigned:
  20174. case value_t::number_float:
  20175. case value_t::binary:
  20176. case value_t::discarded:
  20177. default:
  20178. {
  20179. // all other types are nonempty
  20180. return false;
  20181. }
  20182. }
  20183. }
  20184. /// @brief returns the number of elements
  20185. /// @sa https://json.nlohmann.me/api/basic_json/size/
  20186. size_type size() const noexcept
  20187. {
  20188. switch (m_data.m_type)
  20189. {
  20190. case value_t::null:
  20191. {
  20192. // null values are empty
  20193. return 0;
  20194. }
  20195. case value_t::array:
  20196. {
  20197. // delegate call to array_t::size()
  20198. return m_data.m_value.array->size();
  20199. }
  20200. case value_t::object:
  20201. {
  20202. // delegate call to object_t::size()
  20203. return m_data.m_value.object->size();
  20204. }
  20205. case value_t::string:
  20206. case value_t::boolean:
  20207. case value_t::number_integer:
  20208. case value_t::number_unsigned:
  20209. case value_t::number_float:
  20210. case value_t::binary:
  20211. case value_t::discarded:
  20212. default:
  20213. {
  20214. // all other types have size 1
  20215. return 1;
  20216. }
  20217. }
  20218. }
  20219. /// @brief returns the maximum possible number of elements
  20220. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  20221. size_type max_size() const noexcept
  20222. {
  20223. switch (m_data.m_type)
  20224. {
  20225. case value_t::array:
  20226. {
  20227. // delegate call to array_t::max_size()
  20228. return m_data.m_value.array->max_size();
  20229. }
  20230. case value_t::object:
  20231. {
  20232. // delegate call to object_t::max_size()
  20233. return m_data.m_value.object->max_size();
  20234. }
  20235. case value_t::null:
  20236. case value_t::string:
  20237. case value_t::boolean:
  20238. case value_t::number_integer:
  20239. case value_t::number_unsigned:
  20240. case value_t::number_float:
  20241. case value_t::binary:
  20242. case value_t::discarded:
  20243. default:
  20244. {
  20245. // all other types have max_size() == size()
  20246. return size();
  20247. }
  20248. }
  20249. }
  20250. /// @}
  20251. ///////////////
  20252. // modifiers //
  20253. ///////////////
  20254. /// @name modifiers
  20255. /// @{
  20256. /// @brief clears the contents
  20257. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  20258. void clear() noexcept
  20259. {
  20260. switch (m_data.m_type)
  20261. {
  20262. case value_t::number_integer:
  20263. {
  20264. m_data.m_value.number_integer = 0;
  20265. break;
  20266. }
  20267. case value_t::number_unsigned:
  20268. {
  20269. m_data.m_value.number_unsigned = 0;
  20270. break;
  20271. }
  20272. case value_t::number_float:
  20273. {
  20274. m_data.m_value.number_float = 0.0;
  20275. break;
  20276. }
  20277. case value_t::boolean:
  20278. {
  20279. m_data.m_value.boolean = false;
  20280. break;
  20281. }
  20282. case value_t::string:
  20283. {
  20284. m_data.m_value.string->clear();
  20285. break;
  20286. }
  20287. case value_t::binary:
  20288. {
  20289. m_data.m_value.binary->clear();
  20290. break;
  20291. }
  20292. case value_t::array:
  20293. {
  20294. m_data.m_value.array->clear();
  20295. break;
  20296. }
  20297. case value_t::object:
  20298. {
  20299. m_data.m_value.object->clear();
  20300. break;
  20301. }
  20302. case value_t::null:
  20303. case value_t::discarded:
  20304. default:
  20305. break;
  20306. }
  20307. }
  20308. /// @brief add an object to an array
  20309. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20310. void push_back(basic_json&& val)
  20311. {
  20312. // push_back only works for null objects or arrays
  20313. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  20314. {
  20315. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  20316. }
  20317. // transform a null object into an array
  20318. if (is_null())
  20319. {
  20320. m_data.m_type = value_t::array;
  20321. m_data.m_value = value_t::array;
  20322. assert_invariant();
  20323. }
  20324. // add the element to the array (move semantics)
  20325. const auto old_capacity = m_data.m_value.array->capacity();
  20326. m_data.m_value.array->push_back(std::move(val));
  20327. set_parent(m_data.m_value.array->back(), old_capacity);
  20328. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  20329. }
  20330. /// @brief add an object to an array
  20331. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20332. reference operator+=(basic_json&& val)
  20333. {
  20334. push_back(std::move(val));
  20335. return *this;
  20336. }
  20337. /// @brief add an object to an array
  20338. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20339. void push_back(const basic_json& val)
  20340. {
  20341. // push_back only works for null objects or arrays
  20342. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  20343. {
  20344. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  20345. }
  20346. // transform a null object into an array
  20347. if (is_null())
  20348. {
  20349. m_data.m_type = value_t::array;
  20350. m_data.m_value = value_t::array;
  20351. assert_invariant();
  20352. }
  20353. // add the element to the array
  20354. const auto old_capacity = m_data.m_value.array->capacity();
  20355. m_data.m_value.array->push_back(val);
  20356. set_parent(m_data.m_value.array->back(), old_capacity);
  20357. }
  20358. /// @brief add an object to an array
  20359. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20360. reference operator+=(const basic_json& val)
  20361. {
  20362. push_back(val);
  20363. return *this;
  20364. }
  20365. /// @brief add an object to an object
  20366. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20367. void push_back(const typename object_t::value_type& val)
  20368. {
  20369. // push_back only works for null objects or objects
  20370. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  20371. {
  20372. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  20373. }
  20374. // transform a null object into an object
  20375. if (is_null())
  20376. {
  20377. m_data.m_type = value_t::object;
  20378. m_data.m_value = value_t::object;
  20379. assert_invariant();
  20380. }
  20381. // add the element to the object
  20382. auto res = m_data.m_value.object->insert(val);
  20383. set_parent(res.first->second);
  20384. }
  20385. /// @brief add an object to an object
  20386. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20387. reference operator+=(const typename object_t::value_type& val)
  20388. {
  20389. push_back(val);
  20390. return *this;
  20391. }
  20392. /// @brief add an object to an object
  20393. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  20394. void push_back(initializer_list_t init)
  20395. {
  20396. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  20397. {
  20398. basic_json&& key = init.begin()->moved_or_copied();
  20399. push_back(typename object_t::value_type(
  20400. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  20401. }
  20402. else
  20403. {
  20404. push_back(basic_json(init));
  20405. }
  20406. }
  20407. /// @brief add an object to an object
  20408. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  20409. reference operator+=(initializer_list_t init)
  20410. {
  20411. push_back(init);
  20412. return *this;
  20413. }
  20414. /// @brief add an object to an array
  20415. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  20416. template<class... Args>
  20417. reference emplace_back(Args&& ... args)
  20418. {
  20419. // emplace_back only works for null objects or arrays
  20420. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  20421. {
  20422. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  20423. }
  20424. // transform a null object into an array
  20425. if (is_null())
  20426. {
  20427. m_data.m_type = value_t::array;
  20428. m_data.m_value = value_t::array;
  20429. assert_invariant();
  20430. }
  20431. // add the element to the array (perfect forwarding)
  20432. const auto old_capacity = m_data.m_value.array->capacity();
  20433. m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
  20434. return set_parent(m_data.m_value.array->back(), old_capacity);
  20435. }
  20436. /// @brief add an object to an object if key does not exist
  20437. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  20438. template<class... Args>
  20439. std::pair<iterator, bool> emplace(Args&& ... args)
  20440. {
  20441. // emplace only works for null objects or arrays
  20442. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  20443. {
  20444. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  20445. }
  20446. // transform a null object into an object
  20447. if (is_null())
  20448. {
  20449. m_data.m_type = value_t::object;
  20450. m_data.m_value = value_t::object;
  20451. assert_invariant();
  20452. }
  20453. // add the element to the array (perfect forwarding)
  20454. auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
  20455. set_parent(res.first->second);
  20456. // create a result iterator and set iterator to the result of emplace
  20457. auto it = begin();
  20458. it.m_it.object_iterator = res.first;
  20459. // return pair of iterator and boolean
  20460. return {it, res.second};
  20461. }
  20462. /// Helper for insertion of an iterator
  20463. /// @note: This uses std::distance to support GCC 4.8,
  20464. /// see https://github.com/nlohmann/json/pull/1257
  20465. template<typename... Args>
  20466. iterator insert_iterator(const_iterator pos, Args&& ... args) // NOLINT(performance-unnecessary-value-param)
  20467. {
  20468. iterator result(this);
  20469. JSON_ASSERT(m_data.m_value.array != nullptr);
  20470. auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
  20471. m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  20472. result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
  20473. // This could have been written as:
  20474. // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  20475. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  20476. set_parents();
  20477. return result;
  20478. }
  20479. /// @brief inserts element into array
  20480. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20481. iterator insert(const_iterator pos, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
  20482. {
  20483. // insert only works for arrays
  20484. if (JSON_HEDLEY_LIKELY(is_array()))
  20485. {
  20486. // check if iterator pos fits to this JSON value
  20487. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20488. {
  20489. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20490. }
  20491. // insert to array and return iterator
  20492. return insert_iterator(pos, val);
  20493. }
  20494. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20495. }
  20496. /// @brief inserts element into array
  20497. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20498. iterator insert(const_iterator pos, basic_json&& val) // NOLINT(performance-unnecessary-value-param)
  20499. {
  20500. return insert(pos, val);
  20501. }
  20502. /// @brief inserts copies of element into array
  20503. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20504. iterator insert(const_iterator pos, size_type cnt, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
  20505. {
  20506. // insert only works for arrays
  20507. if (JSON_HEDLEY_LIKELY(is_array()))
  20508. {
  20509. // check if iterator pos fits to this JSON value
  20510. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20511. {
  20512. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20513. }
  20514. // insert to array and return iterator
  20515. return insert_iterator(pos, cnt, val);
  20516. }
  20517. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20518. }
  20519. /// @brief inserts range of elements into array
  20520. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20521. iterator insert(const_iterator pos, const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
  20522. {
  20523. // insert only works for arrays
  20524. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  20525. {
  20526. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20527. }
  20528. // check if iterator pos fits to this JSON value
  20529. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20530. {
  20531. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20532. }
  20533. // check if range iterators belong to the same JSON object
  20534. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  20535. {
  20536. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  20537. }
  20538. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  20539. {
  20540. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  20541. }
  20542. // insert to array and return iterator
  20543. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  20544. }
  20545. /// @brief inserts elements from initializer list into array
  20546. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20547. iterator insert(const_iterator pos, initializer_list_t ilist) // NOLINT(performance-unnecessary-value-param)
  20548. {
  20549. // insert only works for arrays
  20550. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  20551. {
  20552. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20553. }
  20554. // check if iterator pos fits to this JSON value
  20555. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  20556. {
  20557. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  20558. }
  20559. // insert to array and return iterator
  20560. return insert_iterator(pos, ilist.begin(), ilist.end());
  20561. }
  20562. /// @brief inserts range of elements into object
  20563. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  20564. void insert(const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
  20565. {
  20566. // insert only works for objects
  20567. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  20568. {
  20569. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  20570. }
  20571. // check if range iterators belong to the same JSON object
  20572. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  20573. {
  20574. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  20575. }
  20576. // passed iterators must belong to objects
  20577. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  20578. {
  20579. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  20580. }
  20581. m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  20582. set_parents();
  20583. }
  20584. /// @brief updates a JSON object from another object, overwriting existing keys
  20585. /// @sa https://json.nlohmann.me/api/basic_json/update/
  20586. void update(const_reference j, bool merge_objects = false)
  20587. {
  20588. update(j.begin(), j.end(), merge_objects);
  20589. }
  20590. /// @brief updates a JSON object from another object, overwriting existing keys
  20591. /// @sa https://json.nlohmann.me/api/basic_json/update/
  20592. void update(const_iterator first, const_iterator last, bool merge_objects = false) // NOLINT(performance-unnecessary-value-param)
  20593. {
  20594. // implicitly convert a null value to an empty object
  20595. if (is_null())
  20596. {
  20597. m_data.m_type = value_t::object;
  20598. m_data.m_value.object = create<object_t>();
  20599. assert_invariant();
  20600. }
  20601. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  20602. {
  20603. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  20604. }
  20605. // check if range iterators belong to the same JSON object
  20606. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  20607. {
  20608. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  20609. }
  20610. // passed iterators must belong to objects
  20611. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  20612. {
  20613. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  20614. }
  20615. for (auto it = first; it != last; ++it)
  20616. {
  20617. if (merge_objects && it.value().is_object())
  20618. {
  20619. auto it2 = m_data.m_value.object->find(it.key());
  20620. if (it2 != m_data.m_value.object->end())
  20621. {
  20622. it2->second.update(it.value(), true);
  20623. continue;
  20624. }
  20625. }
  20626. m_data.m_value.object->operator[](it.key()) = it.value();
  20627. #if JSON_DIAGNOSTICS
  20628. m_data.m_value.object->operator[](it.key()).m_parent = this;
  20629. #endif
  20630. }
  20631. }
  20632. /// @brief exchanges the values
  20633. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20634. void swap(reference other) noexcept (
  20635. std::is_nothrow_move_constructible<value_t>::value&&
  20636. std::is_nothrow_move_assignable<value_t>::value&&
  20637. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20638. std::is_nothrow_move_assignable<json_value>::value
  20639. )
  20640. {
  20641. std::swap(m_data.m_type, other.m_data.m_type);
  20642. std::swap(m_data.m_value, other.m_data.m_value);
  20643. set_parents();
  20644. other.set_parents();
  20645. assert_invariant();
  20646. }
  20647. /// @brief exchanges the values
  20648. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20649. friend void swap(reference left, reference right) noexcept (
  20650. std::is_nothrow_move_constructible<value_t>::value&&
  20651. std::is_nothrow_move_assignable<value_t>::value&&
  20652. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20653. std::is_nothrow_move_assignable<json_value>::value
  20654. )
  20655. {
  20656. left.swap(right);
  20657. }
  20658. /// @brief exchanges the values
  20659. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20660. void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20661. {
  20662. // swap only works for arrays
  20663. if (JSON_HEDLEY_LIKELY(is_array()))
  20664. {
  20665. using std::swap;
  20666. swap(*(m_data.m_value.array), other);
  20667. }
  20668. else
  20669. {
  20670. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  20671. }
  20672. }
  20673. /// @brief exchanges the values
  20674. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20675. void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20676. {
  20677. // swap only works for objects
  20678. if (JSON_HEDLEY_LIKELY(is_object()))
  20679. {
  20680. using std::swap;
  20681. swap(*(m_data.m_value.object), other);
  20682. }
  20683. else
  20684. {
  20685. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  20686. }
  20687. }
  20688. /// @brief exchanges the values
  20689. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20690. void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20691. {
  20692. // swap only works for strings
  20693. if (JSON_HEDLEY_LIKELY(is_string()))
  20694. {
  20695. using std::swap;
  20696. swap(*(m_data.m_value.string), other);
  20697. }
  20698. else
  20699. {
  20700. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  20701. }
  20702. }
  20703. /// @brief exchanges the values
  20704. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20705. void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  20706. {
  20707. // swap only works for strings
  20708. if (JSON_HEDLEY_LIKELY(is_binary()))
  20709. {
  20710. using std::swap;
  20711. swap(*(m_data.m_value.binary), other);
  20712. }
  20713. else
  20714. {
  20715. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  20716. }
  20717. }
  20718. /// @brief exchanges the values
  20719. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  20720. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  20721. {
  20722. // swap only works for strings
  20723. if (JSON_HEDLEY_LIKELY(is_binary()))
  20724. {
  20725. using std::swap;
  20726. swap(*(m_data.m_value.binary), other);
  20727. }
  20728. else
  20729. {
  20730. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  20731. }
  20732. }
  20733. /// @}
  20734. //////////////////////////////////////////
  20735. // lexicographical comparison operators //
  20736. //////////////////////////////////////////
  20737. /// @name lexicographical comparison operators
  20738. /// @{
  20739. // note parentheses around operands are necessary; see
  20740. // https://github.com/nlohmann/json/issues/1530
  20741. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  20742. const auto lhs_type = lhs.type(); \
  20743. const auto rhs_type = rhs.type(); \
  20744. \
  20745. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  20746. { \
  20747. switch (lhs_type) \
  20748. { \
  20749. case value_t::array: \
  20750. return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
  20751. \
  20752. case value_t::object: \
  20753. return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
  20754. \
  20755. case value_t::null: \
  20756. return (null_result); \
  20757. \
  20758. case value_t::string: \
  20759. return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
  20760. \
  20761. case value_t::boolean: \
  20762. return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
  20763. \
  20764. case value_t::number_integer: \
  20765. return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
  20766. \
  20767. case value_t::number_unsigned: \
  20768. return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
  20769. \
  20770. case value_t::number_float: \
  20771. return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
  20772. \
  20773. case value_t::binary: \
  20774. return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
  20775. \
  20776. case value_t::discarded: \
  20777. default: \
  20778. return (unordered_result); \
  20779. } \
  20780. } \
  20781. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  20782. { \
  20783. return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
  20784. } \
  20785. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  20786. { \
  20787. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
  20788. } \
  20789. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  20790. { \
  20791. return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
  20792. } \
  20793. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  20794. { \
  20795. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
  20796. } \
  20797. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  20798. { \
  20799. return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
  20800. } \
  20801. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  20802. { \
  20803. return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
  20804. } \
  20805. else if(compares_unordered(lhs, rhs))\
  20806. {\
  20807. return (unordered_result);\
  20808. }\
  20809. \
  20810. return (default_result);
  20811. JSON_PRIVATE_UNLESS_TESTED:
  20812. // returns true if:
  20813. // - any operand is NaN and the other operand is of number type
  20814. // - any operand is discarded
  20815. // in legacy mode, discarded values are considered ordered if
  20816. // an operation is computed as an odd number of inverses of others
  20817. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  20818. {
  20819. if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
  20820. || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
  20821. {
  20822. return true;
  20823. }
  20824. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20825. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  20826. #else
  20827. static_cast<void>(inverse);
  20828. return lhs.is_discarded() || rhs.is_discarded();
  20829. #endif
  20830. }
  20831. private:
  20832. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  20833. {
  20834. return compares_unordered(*this, rhs, inverse);
  20835. }
  20836. public:
  20837. #if JSON_HAS_THREE_WAY_COMPARISON
  20838. /// @brief comparison: equal
  20839. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20840. bool operator==(const_reference rhs) const noexcept
  20841. {
  20842. #ifdef __GNUC__
  20843. #pragma GCC diagnostic push
  20844. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20845. #endif
  20846. const_reference lhs = *this;
  20847. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20848. #ifdef __GNUC__
  20849. #pragma GCC diagnostic pop
  20850. #endif
  20851. }
  20852. /// @brief comparison: equal
  20853. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20854. template<typename ScalarType>
  20855. requires std::is_scalar_v<ScalarType>
  20856. bool operator==(ScalarType rhs) const noexcept
  20857. {
  20858. return *this == basic_json(rhs);
  20859. }
  20860. /// @brief comparison: not equal
  20861. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20862. bool operator!=(const_reference rhs) const noexcept
  20863. {
  20864. if (compares_unordered(rhs, true))
  20865. {
  20866. return false;
  20867. }
  20868. return !operator==(rhs);
  20869. }
  20870. /// @brief comparison: 3-way
  20871. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20872. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  20873. {
  20874. const_reference lhs = *this;
  20875. // default_result is used if we cannot compare values. In that case,
  20876. // we compare types.
  20877. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  20878. std::partial_ordering::equivalent,
  20879. std::partial_ordering::unordered,
  20880. lhs_type <=> rhs_type) // *NOPAD*
  20881. }
  20882. /// @brief comparison: 3-way
  20883. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20884. template<typename ScalarType>
  20885. requires std::is_scalar_v<ScalarType>
  20886. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  20887. {
  20888. return *this <=> basic_json(rhs); // *NOPAD*
  20889. }
  20890. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20891. // all operators that are computed as an odd number of inverses of others
  20892. // need to be overloaded to emulate the legacy comparison behavior
  20893. /// @brief comparison: less than or equal
  20894. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20895. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20896. bool operator<=(const_reference rhs) const noexcept
  20897. {
  20898. if (compares_unordered(rhs, true))
  20899. {
  20900. return false;
  20901. }
  20902. return !(rhs < *this);
  20903. }
  20904. /// @brief comparison: less than or equal
  20905. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20906. template<typename ScalarType>
  20907. requires std::is_scalar_v<ScalarType>
  20908. bool operator<=(ScalarType rhs) const noexcept
  20909. {
  20910. return *this <= basic_json(rhs);
  20911. }
  20912. /// @brief comparison: greater than or equal
  20913. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20914. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20915. bool operator>=(const_reference rhs) const noexcept
  20916. {
  20917. if (compares_unordered(rhs, true))
  20918. {
  20919. return false;
  20920. }
  20921. return !(*this < rhs);
  20922. }
  20923. /// @brief comparison: greater than or equal
  20924. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20925. template<typename ScalarType>
  20926. requires std::is_scalar_v<ScalarType>
  20927. bool operator>=(ScalarType rhs) const noexcept
  20928. {
  20929. return *this >= basic_json(rhs);
  20930. }
  20931. #endif
  20932. #else
  20933. /// @brief comparison: equal
  20934. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20935. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20936. {
  20937. #ifdef __GNUC__
  20938. #pragma GCC diagnostic push
  20939. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20940. #endif
  20941. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20942. #ifdef __GNUC__
  20943. #pragma GCC diagnostic pop
  20944. #endif
  20945. }
  20946. /// @brief comparison: equal
  20947. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20948. template<typename ScalarType, typename std::enable_if<
  20949. std::is_scalar<ScalarType>::value, int>::type = 0>
  20950. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20951. {
  20952. return lhs == basic_json(rhs);
  20953. }
  20954. /// @brief comparison: equal
  20955. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20956. template<typename ScalarType, typename std::enable_if<
  20957. std::is_scalar<ScalarType>::value, int>::type = 0>
  20958. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20959. {
  20960. return basic_json(lhs) == rhs;
  20961. }
  20962. /// @brief comparison: not equal
  20963. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20964. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20965. {
  20966. if (compares_unordered(lhs, rhs, true))
  20967. {
  20968. return false;
  20969. }
  20970. return !(lhs == rhs);
  20971. }
  20972. /// @brief comparison: not equal
  20973. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20974. template<typename ScalarType, typename std::enable_if<
  20975. std::is_scalar<ScalarType>::value, int>::type = 0>
  20976. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20977. {
  20978. return lhs != basic_json(rhs);
  20979. }
  20980. /// @brief comparison: not equal
  20981. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20982. template<typename ScalarType, typename std::enable_if<
  20983. std::is_scalar<ScalarType>::value, int>::type = 0>
  20984. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20985. {
  20986. return basic_json(lhs) != rhs;
  20987. }
  20988. /// @brief comparison: less than
  20989. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20990. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20991. {
  20992. // default_result is used if we cannot compare values. In that case,
  20993. // we compare types. Note we have to call the operator explicitly,
  20994. // because MSVC has problems otherwise.
  20995. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20996. }
  20997. /// @brief comparison: less than
  20998. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20999. template<typename ScalarType, typename std::enable_if<
  21000. std::is_scalar<ScalarType>::value, int>::type = 0>
  21001. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  21002. {
  21003. return lhs < basic_json(rhs);
  21004. }
  21005. /// @brief comparison: less than
  21006. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  21007. template<typename ScalarType, typename std::enable_if<
  21008. std::is_scalar<ScalarType>::value, int>::type = 0>
  21009. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  21010. {
  21011. return basic_json(lhs) < rhs;
  21012. }
  21013. /// @brief comparison: less than or equal
  21014. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  21015. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  21016. {
  21017. if (compares_unordered(lhs, rhs, true))
  21018. {
  21019. return false;
  21020. }
  21021. return !(rhs < lhs);
  21022. }
  21023. /// @brief comparison: less than or equal
  21024. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  21025. template<typename ScalarType, typename std::enable_if<
  21026. std::is_scalar<ScalarType>::value, int>::type = 0>
  21027. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  21028. {
  21029. return lhs <= basic_json(rhs);
  21030. }
  21031. /// @brief comparison: less than or equal
  21032. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  21033. template<typename ScalarType, typename std::enable_if<
  21034. std::is_scalar<ScalarType>::value, int>::type = 0>
  21035. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  21036. {
  21037. return basic_json(lhs) <= rhs;
  21038. }
  21039. /// @brief comparison: greater than
  21040. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  21041. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  21042. {
  21043. // double inverse
  21044. if (compares_unordered(lhs, rhs))
  21045. {
  21046. return false;
  21047. }
  21048. return !(lhs <= rhs);
  21049. }
  21050. /// @brief comparison: greater than
  21051. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  21052. template<typename ScalarType, typename std::enable_if<
  21053. std::is_scalar<ScalarType>::value, int>::type = 0>
  21054. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  21055. {
  21056. return lhs > basic_json(rhs);
  21057. }
  21058. /// @brief comparison: greater than
  21059. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  21060. template<typename ScalarType, typename std::enable_if<
  21061. std::is_scalar<ScalarType>::value, int>::type = 0>
  21062. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  21063. {
  21064. return basic_json(lhs) > rhs;
  21065. }
  21066. /// @brief comparison: greater than or equal
  21067. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  21068. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  21069. {
  21070. if (compares_unordered(lhs, rhs, true))
  21071. {
  21072. return false;
  21073. }
  21074. return !(lhs < rhs);
  21075. }
  21076. /// @brief comparison: greater than or equal
  21077. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  21078. template<typename ScalarType, typename std::enable_if<
  21079. std::is_scalar<ScalarType>::value, int>::type = 0>
  21080. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  21081. {
  21082. return lhs >= basic_json(rhs);
  21083. }
  21084. /// @brief comparison: greater than or equal
  21085. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  21086. template<typename ScalarType, typename std::enable_if<
  21087. std::is_scalar<ScalarType>::value, int>::type = 0>
  21088. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  21089. {
  21090. return basic_json(lhs) >= rhs;
  21091. }
  21092. #endif
  21093. #undef JSON_IMPLEMENT_OPERATOR
  21094. /// @}
  21095. ///////////////////
  21096. // serialization //
  21097. ///////////////////
  21098. /// @name serialization
  21099. /// @{
  21100. #ifndef JSON_NO_IO
  21101. /// @brief serialize to stream
  21102. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  21103. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  21104. {
  21105. // read width member and use it as the indentation parameter if nonzero
  21106. const bool pretty_print = o.width() > 0;
  21107. const auto indentation = pretty_print ? o.width() : 0;
  21108. // reset width to 0 for subsequent calls to this stream
  21109. o.width(0);
  21110. // do the actual serialization
  21111. serializer s(detail::output_adapter<char>(o), o.fill());
  21112. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  21113. return o;
  21114. }
  21115. /// @brief serialize to stream
  21116. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  21117. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  21118. /// version 4.0.0 of the library. Please use
  21119. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  21120. /// replace calls like `j >> o;` with `o << j;`.
  21121. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  21122. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  21123. {
  21124. return o << j;
  21125. }
  21126. #endif // JSON_NO_IO
  21127. /// @}
  21128. /////////////////////
  21129. // deserialization //
  21130. /////////////////////
  21131. /// @name deserialization
  21132. /// @{
  21133. /// @brief deserialize from a compatible input
  21134. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  21135. template<typename InputType>
  21136. JSON_HEDLEY_WARN_UNUSED_RESULT
  21137. static basic_json parse(InputType&& i,
  21138. parser_callback_t cb = nullptr,
  21139. const bool allow_exceptions = true,
  21140. const bool ignore_comments = false,
  21141. const bool ignore_trailing_commas = false)
  21142. {
  21143. basic_json result;
  21144. parser(detail::input_adapter(std::forward<InputType>(i)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas).parse(true, result); // cppcheck-suppress[accessMoved,accessForwarded]
  21145. return result;
  21146. }
  21147. /// @brief deserialize from a pair of character iterators
  21148. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  21149. template<typename IteratorType>
  21150. JSON_HEDLEY_WARN_UNUSED_RESULT
  21151. static basic_json parse(IteratorType first,
  21152. IteratorType last,
  21153. parser_callback_t cb = nullptr,
  21154. const bool allow_exceptions = true,
  21155. const bool ignore_comments = false,
  21156. const bool ignore_trailing_commas = false)
  21157. {
  21158. basic_json result;
  21159. parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas).parse(true, result); // cppcheck-suppress[accessMoved]
  21160. return result;
  21161. }
  21162. JSON_HEDLEY_WARN_UNUSED_RESULT
  21163. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  21164. static basic_json parse(detail::span_input_adapter&& i,
  21165. parser_callback_t cb = nullptr,
  21166. const bool allow_exceptions = true,
  21167. const bool ignore_comments = false,
  21168. const bool ignore_trailing_commas = false)
  21169. {
  21170. basic_json result;
  21171. parser(i.get(), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas).parse(true, result); // cppcheck-suppress[accessMoved]
  21172. return result;
  21173. }
  21174. /// @brief check if the input is valid JSON
  21175. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  21176. template<typename InputType>
  21177. static bool accept(InputType&& i,
  21178. const bool ignore_comments = false,
  21179. const bool ignore_trailing_commas = false)
  21180. {
  21181. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
  21182. }
  21183. /// @brief check if the input is valid JSON
  21184. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  21185. template<typename IteratorType>
  21186. static bool accept(IteratorType first, IteratorType last,
  21187. const bool ignore_comments = false,
  21188. const bool ignore_trailing_commas = false)
  21189. {
  21190. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
  21191. }
  21192. JSON_HEDLEY_WARN_UNUSED_RESULT
  21193. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  21194. static bool accept(detail::span_input_adapter&& i,
  21195. const bool ignore_comments = false,
  21196. const bool ignore_trailing_commas = false)
  21197. {
  21198. return parser(i.get(), nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
  21199. }
  21200. /// @brief generate SAX events
  21201. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  21202. template <typename InputType, typename SAX>
  21203. JSON_HEDLEY_NON_NULL(2)
  21204. static bool sax_parse(InputType&& i, SAX* sax,
  21205. input_format_t format = input_format_t::json,
  21206. const bool strict = true,
  21207. const bool ignore_comments = false,
  21208. const bool ignore_trailing_commas = false)
  21209. {
  21210. #if defined(__clang__)
  21211. #pragma clang diagnostic push
  21212. #pragma clang diagnostic ignored "-Wtautological-pointer-compare"
  21213. #elif defined(__GNUC__)
  21214. #pragma GCC diagnostic push
  21215. #pragma GCC diagnostic ignored "-Wnonnull-compare"
  21216. #endif
  21217. if (sax == nullptr)
  21218. {
  21219. JSON_THROW(other_error::create(502, "SAX handler must not be null", nullptr));
  21220. }
  21221. #if defined(__clang__)
  21222. #pragma clang diagnostic pop
  21223. #elif defined(__GNUC__)
  21224. #pragma GCC diagnostic pop
  21225. #endif
  21226. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21227. return format == input_format_t::json
  21228. ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict)
  21229. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  21230. }
  21231. /// @brief generate SAX events
  21232. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  21233. template<class IteratorType, class SAX>
  21234. JSON_HEDLEY_NON_NULL(3)
  21235. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  21236. input_format_t format = input_format_t::json,
  21237. const bool strict = true,
  21238. const bool ignore_comments = false,
  21239. const bool ignore_trailing_commas = false)
  21240. {
  21241. #if defined(__clang__)
  21242. #pragma clang diagnostic push
  21243. #pragma clang diagnostic ignored "-Wtautological-pointer-compare"
  21244. #elif defined(__GNUC__)
  21245. #pragma GCC diagnostic push
  21246. #pragma GCC diagnostic ignored "-Wnonnull-compare"
  21247. #endif
  21248. if (sax == nullptr)
  21249. {
  21250. JSON_THROW(other_error::create(502, "SAX handler must not be null", nullptr));
  21251. }
  21252. #if defined(__clang__)
  21253. #pragma clang diagnostic pop
  21254. #elif defined(__GNUC__)
  21255. #pragma GCC diagnostic pop
  21256. #endif
  21257. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21258. return format == input_format_t::json
  21259. ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict)
  21260. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  21261. }
  21262. /// @brief generate SAX events
  21263. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  21264. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  21265. /// version 4.0.0 of the library. Please use
  21266. /// sax_parse(ptr, ptr + len) instead.
  21267. template <typename SAX>
  21268. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  21269. JSON_HEDLEY_NON_NULL(2)
  21270. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  21271. input_format_t format = input_format_t::json,
  21272. const bool strict = true,
  21273. const bool ignore_comments = false,
  21274. const bool ignore_trailing_commas = false)
  21275. {
  21276. #if defined(__clang__)
  21277. #pragma clang diagnostic push
  21278. #pragma clang diagnostic ignored "-Wtautological-pointer-compare"
  21279. #elif defined(__GNUC__)
  21280. #pragma GCC diagnostic push
  21281. #pragma GCC diagnostic ignored "-Wnonnull-compare"
  21282. #endif
  21283. if (sax == nullptr)
  21284. {
  21285. JSON_THROW(other_error::create(502, "SAX handler must not be null", nullptr));
  21286. }
  21287. #if defined(__clang__)
  21288. #pragma clang diagnostic pop
  21289. #elif defined(__GNUC__)
  21290. #pragma GCC diagnostic pop
  21291. #endif
  21292. auto ia = i.get();
  21293. return format == input_format_t::json
  21294. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21295. ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict)
  21296. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21297. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  21298. }
  21299. #ifndef JSON_NO_IO
  21300. /// @brief deserialize from stream
  21301. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  21302. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  21303. /// version 4.0.0 of the library. Please use
  21304. /// operator>>(std::istream&, basic_json&) instead; that is,
  21305. /// replace calls like `j << i;` with `i >> j;`.
  21306. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  21307. friend std::istream& operator<<(basic_json& j, std::istream& i)
  21308. {
  21309. return operator>>(i, j);
  21310. }
  21311. /// @brief deserialize from stream
  21312. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  21313. friend std::istream& operator>>(std::istream& i, basic_json& j)
  21314. {
  21315. parser(detail::input_adapter(i)).parse(false, j);
  21316. return i;
  21317. }
  21318. #endif // JSON_NO_IO
  21319. /// @}
  21320. ///////////////////////////
  21321. // convenience functions //
  21322. ///////////////////////////
  21323. /// @brief return the type as string
  21324. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  21325. JSON_HEDLEY_RETURNS_NON_NULL
  21326. const char* type_name() const noexcept
  21327. {
  21328. switch (m_data.m_type)
  21329. {
  21330. case value_t::null:
  21331. return "null";
  21332. case value_t::object:
  21333. return "object";
  21334. case value_t::array:
  21335. return "array";
  21336. case value_t::string:
  21337. return "string";
  21338. case value_t::boolean:
  21339. return "boolean";
  21340. case value_t::binary:
  21341. return "binary";
  21342. case value_t::discarded:
  21343. return "discarded";
  21344. case value_t::number_integer:
  21345. case value_t::number_unsigned:
  21346. case value_t::number_float:
  21347. return "number";
  21348. default:
  21349. return "invalid";
  21350. }
  21351. }
  21352. JSON_PRIVATE_UNLESS_TESTED:
  21353. //////////////////////
  21354. // member variables //
  21355. //////////////////////
  21356. struct data
  21357. {
  21358. /// the type of the current element
  21359. value_t m_type = value_t::null;
  21360. /// the value of the current element
  21361. json_value m_value = {};
  21362. data(const value_t v)
  21363. : m_type(v), m_value(v)
  21364. {
  21365. }
  21366. data(size_type cnt, const basic_json& val)
  21367. : m_type(value_t::array)
  21368. {
  21369. m_value.array = create<array_t>(cnt, val);
  21370. }
  21371. data() noexcept = default;
  21372. data(data&&) noexcept = default;
  21373. data(const data&) noexcept = delete;
  21374. data& operator=(data&&) noexcept = delete;
  21375. data& operator=(const data&) noexcept = delete;
  21376. ~data() noexcept
  21377. {
  21378. m_value.destroy(m_type);
  21379. }
  21380. };
  21381. data m_data = {};
  21382. #if JSON_DIAGNOSTICS
  21383. /// a pointer to a parent value (for debugging purposes)
  21384. basic_json* m_parent = nullptr;
  21385. #endif
  21386. #if JSON_DIAGNOSTIC_POSITIONS
  21387. /// the start position of the value
  21388. std::size_t start_position = std::string::npos;
  21389. /// the end position of the value
  21390. std::size_t end_position = std::string::npos;
  21391. public:
  21392. constexpr std::size_t start_pos() const noexcept
  21393. {
  21394. return start_position;
  21395. }
  21396. constexpr std::size_t end_pos() const noexcept
  21397. {
  21398. return end_position;
  21399. }
  21400. #endif
  21401. //////////////////////////////////////////
  21402. // binary serialization/deserialization //
  21403. //////////////////////////////////////////
  21404. /// @name binary serialization/deserialization support
  21405. /// @{
  21406. public:
  21407. /// @brief create a CBOR serialization of a given JSON value
  21408. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  21409. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  21410. {
  21411. std::vector<std::uint8_t> result;
  21412. to_cbor(j, result);
  21413. return result;
  21414. }
  21415. /// @brief create a CBOR serialization of a given JSON value
  21416. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  21417. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21418. {
  21419. binary_writer<std::uint8_t>(o).write_cbor(j);
  21420. }
  21421. /// @brief create a CBOR serialization of a given JSON value
  21422. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  21423. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  21424. {
  21425. binary_writer<char>(o).write_cbor(j);
  21426. }
  21427. /// @brief create a MessagePack serialization of a given JSON value
  21428. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  21429. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  21430. {
  21431. std::vector<std::uint8_t> result;
  21432. to_msgpack(j, result);
  21433. return result;
  21434. }
  21435. /// @brief create a MessagePack serialization of a given JSON value
  21436. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  21437. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21438. {
  21439. binary_writer<std::uint8_t>(o).write_msgpack(j);
  21440. }
  21441. /// @brief create a MessagePack serialization of a given JSON value
  21442. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  21443. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  21444. {
  21445. binary_writer<char>(o).write_msgpack(j);
  21446. }
  21447. /// @brief create a UBJSON serialization of a given JSON value
  21448. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  21449. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  21450. const bool use_size = false,
  21451. const bool use_type = false)
  21452. {
  21453. std::vector<std::uint8_t> result;
  21454. to_ubjson(j, result, use_size, use_type);
  21455. return result;
  21456. }
  21457. /// @brief create a UBJSON serialization of a given JSON value
  21458. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  21459. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  21460. const bool use_size = false, const bool use_type = false)
  21461. {
  21462. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  21463. }
  21464. /// @brief create a UBJSON serialization of a given JSON value
  21465. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  21466. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  21467. const bool use_size = false, const bool use_type = false)
  21468. {
  21469. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  21470. }
  21471. /// @brief create a BJData serialization of a given JSON value
  21472. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  21473. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  21474. const bool use_size = false,
  21475. const bool use_type = false,
  21476. const bjdata_version_t version = bjdata_version_t::draft2)
  21477. {
  21478. std::vector<std::uint8_t> result;
  21479. to_bjdata(j, result, use_size, use_type, version);
  21480. return result;
  21481. }
  21482. /// @brief create a BJData serialization of a given JSON value
  21483. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  21484. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  21485. const bool use_size = false, const bool use_type = false,
  21486. const bjdata_version_t version = bjdata_version_t::draft2)
  21487. {
  21488. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true, version);
  21489. }
  21490. /// @brief create a BJData serialization of a given JSON value
  21491. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  21492. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  21493. const bool use_size = false, const bool use_type = false,
  21494. const bjdata_version_t version = bjdata_version_t::draft2)
  21495. {
  21496. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true, version);
  21497. }
  21498. /// @brief create a BSON serialization of a given JSON value
  21499. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  21500. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  21501. {
  21502. std::vector<std::uint8_t> result;
  21503. to_bson(j, result);
  21504. return result;
  21505. }
  21506. /// @brief create a BSON serialization of a given JSON value
  21507. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  21508. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21509. {
  21510. binary_writer<std::uint8_t>(o).write_bson(j);
  21511. }
  21512. /// @brief create a BSON serialization of a given JSON value
  21513. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  21514. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  21515. {
  21516. binary_writer<char>(o).write_bson(j);
  21517. }
  21518. /// @brief create a JSON value from an input in CBOR format
  21519. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  21520. template<typename InputType>
  21521. JSON_HEDLEY_WARN_UNUSED_RESULT
  21522. static basic_json from_cbor(InputType&& i,
  21523. const bool strict = true,
  21524. const bool allow_exceptions = true,
  21525. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21526. {
  21527. basic_json result;
  21528. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21529. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21530. 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]
  21531. return res ? result : basic_json(value_t::discarded);
  21532. }
  21533. /// @brief create a JSON value from an input in CBOR format
  21534. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  21535. template<typename IteratorType>
  21536. JSON_HEDLEY_WARN_UNUSED_RESULT
  21537. static basic_json from_cbor(IteratorType first, IteratorType last,
  21538. const bool strict = true,
  21539. const bool allow_exceptions = true,
  21540. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21541. {
  21542. basic_json result;
  21543. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21544. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21545. 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]
  21546. return res ? result : basic_json(value_t::discarded);
  21547. }
  21548. template<typename T>
  21549. JSON_HEDLEY_WARN_UNUSED_RESULT
  21550. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  21551. static basic_json from_cbor(const T* ptr, std::size_t len,
  21552. const bool strict = true,
  21553. const bool allow_exceptions = true,
  21554. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21555. {
  21556. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  21557. }
  21558. JSON_HEDLEY_WARN_UNUSED_RESULT
  21559. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  21560. static basic_json from_cbor(detail::span_input_adapter&& i,
  21561. const bool strict = true,
  21562. const bool allow_exceptions = true,
  21563. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21564. {
  21565. basic_json result;
  21566. auto ia = i.get();
  21567. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21568. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21569. 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]
  21570. return res ? result : basic_json(value_t::discarded);
  21571. }
  21572. /// @brief create a JSON value from an input in MessagePack format
  21573. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  21574. template<typename InputType>
  21575. JSON_HEDLEY_WARN_UNUSED_RESULT
  21576. static basic_json from_msgpack(InputType&& i,
  21577. const bool strict = true,
  21578. const bool allow_exceptions = true)
  21579. {
  21580. basic_json result;
  21581. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21582. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21583. 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]
  21584. return res ? result : basic_json(value_t::discarded);
  21585. }
  21586. /// @brief create a JSON value from an input in MessagePack format
  21587. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  21588. template<typename IteratorType>
  21589. JSON_HEDLEY_WARN_UNUSED_RESULT
  21590. static basic_json from_msgpack(IteratorType first, IteratorType last,
  21591. const bool strict = true,
  21592. const bool allow_exceptions = true)
  21593. {
  21594. basic_json result;
  21595. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21596. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21597. 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]
  21598. return res ? result : basic_json(value_t::discarded);
  21599. }
  21600. template<typename T>
  21601. JSON_HEDLEY_WARN_UNUSED_RESULT
  21602. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  21603. static basic_json from_msgpack(const T* ptr, std::size_t len,
  21604. const bool strict = true,
  21605. const bool allow_exceptions = true)
  21606. {
  21607. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  21608. }
  21609. JSON_HEDLEY_WARN_UNUSED_RESULT
  21610. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  21611. static basic_json from_msgpack(detail::span_input_adapter&& i,
  21612. const bool strict = true,
  21613. const bool allow_exceptions = true)
  21614. {
  21615. basic_json result;
  21616. auto ia = i.get();
  21617. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21618. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21619. 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]
  21620. return res ? result : basic_json(value_t::discarded);
  21621. }
  21622. /// @brief create a JSON value from an input in UBJSON format
  21623. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  21624. template<typename InputType>
  21625. JSON_HEDLEY_WARN_UNUSED_RESULT
  21626. static basic_json from_ubjson(InputType&& i,
  21627. const bool strict = true,
  21628. const bool allow_exceptions = true)
  21629. {
  21630. basic_json result;
  21631. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21632. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21633. 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]
  21634. return res ? result : basic_json(value_t::discarded);
  21635. }
  21636. /// @brief create a JSON value from an input in UBJSON format
  21637. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  21638. template<typename IteratorType>
  21639. JSON_HEDLEY_WARN_UNUSED_RESULT
  21640. static basic_json from_ubjson(IteratorType first, IteratorType last,
  21641. const bool strict = true,
  21642. const bool allow_exceptions = true)
  21643. {
  21644. basic_json result;
  21645. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21646. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21647. 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]
  21648. return res ? result : basic_json(value_t::discarded);
  21649. }
  21650. template<typename T>
  21651. JSON_HEDLEY_WARN_UNUSED_RESULT
  21652. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  21653. static basic_json from_ubjson(const T* ptr, std::size_t len,
  21654. const bool strict = true,
  21655. const bool allow_exceptions = true)
  21656. {
  21657. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  21658. }
  21659. JSON_HEDLEY_WARN_UNUSED_RESULT
  21660. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  21661. static basic_json from_ubjson(detail::span_input_adapter&& i,
  21662. const bool strict = true,
  21663. const bool allow_exceptions = true)
  21664. {
  21665. basic_json result;
  21666. auto ia = i.get();
  21667. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21668. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21669. 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]
  21670. return res ? result : basic_json(value_t::discarded);
  21671. }
  21672. /// @brief create a JSON value from an input in BJData format
  21673. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  21674. template<typename InputType>
  21675. JSON_HEDLEY_WARN_UNUSED_RESULT
  21676. static basic_json from_bjdata(InputType&& i,
  21677. const bool strict = true,
  21678. const bool allow_exceptions = true)
  21679. {
  21680. basic_json result;
  21681. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21682. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21683. 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]
  21684. return res ? result : basic_json(value_t::discarded);
  21685. }
  21686. /// @brief create a JSON value from an input in BJData format
  21687. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  21688. template<typename IteratorType>
  21689. JSON_HEDLEY_WARN_UNUSED_RESULT
  21690. static basic_json from_bjdata(IteratorType first, IteratorType last,
  21691. const bool strict = true,
  21692. const bool allow_exceptions = true)
  21693. {
  21694. basic_json result;
  21695. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21696. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21697. 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]
  21698. return res ? result : basic_json(value_t::discarded);
  21699. }
  21700. /// @brief create a JSON value from an input in BSON format
  21701. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  21702. template<typename InputType>
  21703. JSON_HEDLEY_WARN_UNUSED_RESULT
  21704. static basic_json from_bson(InputType&& i,
  21705. const bool strict = true,
  21706. const bool allow_exceptions = true)
  21707. {
  21708. basic_json result;
  21709. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21710. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21711. 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]
  21712. return res ? result : basic_json(value_t::discarded);
  21713. }
  21714. /// @brief create a JSON value from an input in BSON format
  21715. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  21716. template<typename IteratorType>
  21717. JSON_HEDLEY_WARN_UNUSED_RESULT
  21718. static basic_json from_bson(IteratorType first, IteratorType last,
  21719. const bool strict = true,
  21720. const bool allow_exceptions = true)
  21721. {
  21722. basic_json result;
  21723. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21724. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21725. 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]
  21726. return res ? result : basic_json(value_t::discarded);
  21727. }
  21728. template<typename T>
  21729. JSON_HEDLEY_WARN_UNUSED_RESULT
  21730. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  21731. static basic_json from_bson(const T* ptr, std::size_t len,
  21732. const bool strict = true,
  21733. const bool allow_exceptions = true)
  21734. {
  21735. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  21736. }
  21737. JSON_HEDLEY_WARN_UNUSED_RESULT
  21738. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  21739. static basic_json from_bson(detail::span_input_adapter&& i,
  21740. const bool strict = true,
  21741. const bool allow_exceptions = true)
  21742. {
  21743. basic_json result;
  21744. auto ia = i.get();
  21745. detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
  21746. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21747. 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]
  21748. return res ? result : basic_json(value_t::discarded);
  21749. }
  21750. /// @}
  21751. //////////////////////////
  21752. // JSON Pointer support //
  21753. //////////////////////////
  21754. /// @name JSON Pointer functions
  21755. /// @{
  21756. /// @brief access specified element via JSON Pointer
  21757. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  21758. reference operator[](const json_pointer& ptr)
  21759. {
  21760. return ptr.get_unchecked(this);
  21761. }
  21762. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21763. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21764. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  21765. {
  21766. return ptr.get_unchecked(this);
  21767. }
  21768. /// @brief access specified element via JSON Pointer
  21769. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  21770. const_reference operator[](const json_pointer& ptr) const
  21771. {
  21772. return ptr.get_unchecked(this);
  21773. }
  21774. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21775. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21776. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  21777. {
  21778. return ptr.get_unchecked(this);
  21779. }
  21780. /// @brief access specified element via JSON Pointer
  21781. /// @sa https://json.nlohmann.me/api/basic_json/at/
  21782. reference at(const json_pointer& ptr)
  21783. {
  21784. return ptr.get_checked(this);
  21785. }
  21786. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21787. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21788. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  21789. {
  21790. return ptr.get_checked(this);
  21791. }
  21792. /// @brief access specified element via JSON Pointer
  21793. /// @sa https://json.nlohmann.me/api/basic_json/at/
  21794. const_reference at(const json_pointer& ptr) const
  21795. {
  21796. return ptr.get_checked(this);
  21797. }
  21798. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  21799. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  21800. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  21801. {
  21802. return ptr.get_checked(this);
  21803. }
  21804. /// @brief return flattened JSON value
  21805. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  21806. basic_json flatten() const
  21807. {
  21808. basic_json result(value_t::object);
  21809. json_pointer::flatten("", *this, result);
  21810. return result;
  21811. }
  21812. /// @brief unflatten a previously flattened JSON value
  21813. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  21814. basic_json unflatten() const
  21815. {
  21816. return json_pointer::unflatten(*this);
  21817. }
  21818. /// @}
  21819. //////////////////////////
  21820. // JSON Patch functions //
  21821. //////////////////////////
  21822. /// @name JSON Patch functions
  21823. /// @{
  21824. /// @brief applies a JSON patch in-place without copying the object
  21825. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21826. void patch_inplace(const basic_json& json_patch)
  21827. {
  21828. basic_json& result = *this;
  21829. // the valid JSON Patch operations
  21830. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  21831. const auto get_op = [](const string_t& op)
  21832. {
  21833. if (op == "add")
  21834. {
  21835. return patch_operations::add;
  21836. }
  21837. if (op == "remove")
  21838. {
  21839. return patch_operations::remove;
  21840. }
  21841. if (op == "replace")
  21842. {
  21843. return patch_operations::replace;
  21844. }
  21845. if (op == "move")
  21846. {
  21847. return patch_operations::move;
  21848. }
  21849. if (op == "copy")
  21850. {
  21851. return patch_operations::copy;
  21852. }
  21853. if (op == "test")
  21854. {
  21855. return patch_operations::test;
  21856. }
  21857. return patch_operations::invalid;
  21858. };
  21859. // wrapper for "add" operation; add value at ptr
  21860. const auto operation_add = [&result](json_pointer & ptr, const basic_json & val)
  21861. {
  21862. // adding to the root of the target document means replacing it
  21863. if (ptr.empty())
  21864. {
  21865. result = val;
  21866. return;
  21867. }
  21868. // make sure the top element of the pointer exists
  21869. json_pointer const top_pointer = ptr.top();
  21870. if (top_pointer != ptr)
  21871. {
  21872. result.at(top_pointer);
  21873. }
  21874. // get reference to the parent of the JSON pointer ptr
  21875. const auto last_path = ptr.back();
  21876. ptr.pop_back();
  21877. // parent must exist when performing patch add per RFC6902 specs
  21878. basic_json& parent = result.at(ptr);
  21879. switch (parent.m_data.m_type)
  21880. {
  21881. case value_t::null:
  21882. case value_t::object:
  21883. {
  21884. // use operator[] to add value
  21885. parent[last_path] = val;
  21886. break;
  21887. }
  21888. case value_t::array:
  21889. {
  21890. if (last_path == "-")
  21891. {
  21892. // special case: append to back
  21893. parent.push_back(val);
  21894. }
  21895. else
  21896. {
  21897. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  21898. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  21899. {
  21900. // avoid undefined behavior
  21901. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  21902. }
  21903. // default case: insert add offset
  21904. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  21905. }
  21906. break;
  21907. }
  21908. // if there exists a parent, it cannot be primitive
  21909. case value_t::string: // LCOV_EXCL_LINE
  21910. case value_t::boolean: // LCOV_EXCL_LINE
  21911. case value_t::number_integer: // LCOV_EXCL_LINE
  21912. case value_t::number_unsigned: // LCOV_EXCL_LINE
  21913. case value_t::number_float: // LCOV_EXCL_LINE
  21914. case value_t::binary: // LCOV_EXCL_LINE
  21915. case value_t::discarded: // LCOV_EXCL_LINE
  21916. default: // LCOV_EXCL_LINE
  21917. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  21918. }
  21919. };
  21920. // wrapper for "remove" operation; remove value at ptr
  21921. const auto operation_remove = [this, & result](json_pointer & ptr)
  21922. {
  21923. // get reference to the parent of the JSON pointer ptr
  21924. const auto last_path = ptr.back();
  21925. ptr.pop_back();
  21926. basic_json& parent = result.at(ptr);
  21927. // remove child
  21928. if (parent.is_object())
  21929. {
  21930. // perform range check
  21931. auto it = parent.find(last_path);
  21932. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  21933. {
  21934. parent.erase(it);
  21935. }
  21936. else
  21937. {
  21938. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  21939. }
  21940. }
  21941. else if (parent.is_array())
  21942. {
  21943. // note erase performs range check
  21944. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  21945. }
  21946. };
  21947. // type check: top level value must be an array
  21948. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  21949. {
  21950. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  21951. }
  21952. // iterate and apply the operations
  21953. for (const auto& val : json_patch)
  21954. {
  21955. // wrapper to get a value for an operation
  21956. const auto get_value = [&val](const string_t& op,
  21957. const string_t& member,
  21958. bool string_type) -> basic_json &
  21959. {
  21960. // find value
  21961. auto it = val.m_data.m_value.object->find(member);
  21962. // context-sensitive error message
  21963. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
  21964. // check if the desired value is present
  21965. if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
  21966. {
  21967. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21968. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  21969. }
  21970. // check if the result is of type string
  21971. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  21972. {
  21973. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21974. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  21975. }
  21976. // no error: return value
  21977. return it->second;
  21978. };
  21979. // type check: every element of the array must be an object
  21980. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  21981. {
  21982. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  21983. }
  21984. // collect mandatory members
  21985. const auto op = get_value("op", "op", true).template get<string_t>();
  21986. const auto path = get_value(op, "path", true).template get<string_t>();
  21987. json_pointer ptr(path);
  21988. switch (get_op(op))
  21989. {
  21990. case patch_operations::add:
  21991. {
  21992. operation_add(ptr, get_value("add", "value", false));
  21993. break;
  21994. }
  21995. case patch_operations::remove:
  21996. {
  21997. operation_remove(ptr);
  21998. break;
  21999. }
  22000. case patch_operations::replace:
  22001. {
  22002. // the "path" location must exist - use at()
  22003. result.at(ptr) = get_value("replace", "value", false);
  22004. break;
  22005. }
  22006. case patch_operations::move:
  22007. {
  22008. const auto from_path = get_value("move", "from", true).template get<string_t>();
  22009. json_pointer from_ptr(from_path);
  22010. // the "from" location must exist - use at()
  22011. basic_json const v = result.at(from_ptr);
  22012. // The move operation is functionally identical to a
  22013. // "remove" operation on the "from" location, followed
  22014. // immediately by an "add" operation at the target
  22015. // location with the value that was just removed.
  22016. operation_remove(from_ptr);
  22017. operation_add(ptr, v);
  22018. break;
  22019. }
  22020. case patch_operations::copy:
  22021. {
  22022. const auto from_path = get_value("copy", "from", true).template get<string_t>();
  22023. const json_pointer from_ptr(from_path);
  22024. // the "from" location must exist - use at()
  22025. basic_json const v = result.at(from_ptr);
  22026. // The copy is functionally identical to an "add"
  22027. // operation at the target location using the value
  22028. // specified in the "from" member.
  22029. operation_add(ptr, v);
  22030. break;
  22031. }
  22032. case patch_operations::test:
  22033. {
  22034. bool success = false;
  22035. JSON_TRY
  22036. {
  22037. // check if "value" matches the one at "path"
  22038. // the "path" location must exist - use at()
  22039. success = (result.at(ptr) == get_value("test", "value", false));
  22040. }
  22041. JSON_INTERNAL_CATCH (out_of_range&)
  22042. {
  22043. // ignore out of range errors: success remains false
  22044. }
  22045. // throw an exception if the test fails
  22046. if (JSON_HEDLEY_UNLIKELY(!success))
  22047. {
  22048. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  22049. }
  22050. break;
  22051. }
  22052. case patch_operations::invalid:
  22053. default:
  22054. {
  22055. // op must be "add", "remove", "replace", "move", "copy", or
  22056. // "test"
  22057. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  22058. }
  22059. }
  22060. }
  22061. }
  22062. /// @brief applies a JSON patch to a copy of the current object
  22063. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  22064. basic_json patch(const basic_json& json_patch) const
  22065. {
  22066. basic_json result = *this;
  22067. result.patch_inplace(json_patch);
  22068. return result;
  22069. }
  22070. /// @brief creates a diff as a JSON patch
  22071. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  22072. JSON_HEDLEY_WARN_UNUSED_RESULT
  22073. static basic_json diff(const basic_json& source, const basic_json& target,
  22074. const string_t& path = "")
  22075. {
  22076. // the patch
  22077. basic_json result(value_t::array);
  22078. // if the values are the same, return an empty patch
  22079. if (source == target)
  22080. {
  22081. return result;
  22082. }
  22083. if (source.type() != target.type())
  22084. {
  22085. // different types: replace value
  22086. result.push_back(
  22087. {
  22088. {"op", "replace"}, {"path", path}, {"value", target}
  22089. });
  22090. return result;
  22091. }
  22092. switch (source.type())
  22093. {
  22094. case value_t::array:
  22095. {
  22096. // first pass: traverse common elements
  22097. std::size_t i = 0;
  22098. while (i < source.size() && i < target.size())
  22099. {
  22100. // recursive call to compare array values at index i
  22101. auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
  22102. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  22103. ++i;
  22104. }
  22105. // We now reached the end of at least one array
  22106. // in a second pass, traverse the remaining elements
  22107. // remove my remaining elements
  22108. const auto end_index = static_cast<difference_type>(result.size());
  22109. while (i < source.size())
  22110. {
  22111. // add operations in reverse order to avoid invalid
  22112. // indices
  22113. result.insert(result.begin() + end_index, object(
  22114. {
  22115. {"op", "remove"},
  22116. {"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(i))}
  22117. }));
  22118. ++i;
  22119. }
  22120. // add other remaining elements
  22121. while (i < target.size())
  22122. {
  22123. result.push_back(
  22124. {
  22125. {"op", "add"},
  22126. {"path", detail::concat<string_t>(path, "/-")},
  22127. {"value", target[i]}
  22128. });
  22129. ++i;
  22130. }
  22131. break;
  22132. }
  22133. case value_t::object:
  22134. {
  22135. // first pass: traverse this object's elements
  22136. for (auto it = source.cbegin(); it != source.cend(); ++it)
  22137. {
  22138. // escape the key name to be used in a JSON patch
  22139. const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
  22140. if (target.find(it.key()) != target.end())
  22141. {
  22142. // recursive call to compare object values at key it
  22143. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  22144. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  22145. }
  22146. else
  22147. {
  22148. // found a key that is not in o -> remove it
  22149. result.push_back(object(
  22150. {
  22151. {"op", "remove"}, {"path", path_key}
  22152. }));
  22153. }
  22154. }
  22155. // second pass: traverse other object's elements
  22156. for (auto it = target.cbegin(); it != target.cend(); ++it)
  22157. {
  22158. if (source.find(it.key()) == source.end())
  22159. {
  22160. // found a key that is not in this -> add it
  22161. const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
  22162. result.push_back(
  22163. {
  22164. {"op", "add"}, {"path", path_key},
  22165. {"value", it.value()}
  22166. });
  22167. }
  22168. }
  22169. break;
  22170. }
  22171. case value_t::null:
  22172. case value_t::string:
  22173. case value_t::boolean:
  22174. case value_t::number_integer:
  22175. case value_t::number_unsigned:
  22176. case value_t::number_float:
  22177. case value_t::binary:
  22178. case value_t::discarded:
  22179. default:
  22180. {
  22181. // both primitive types: replace value
  22182. result.push_back(
  22183. {
  22184. {"op", "replace"}, {"path", path}, {"value", target}
  22185. });
  22186. break;
  22187. }
  22188. }
  22189. return result;
  22190. }
  22191. /// @}
  22192. ////////////////////////////////
  22193. // JSON Merge Patch functions //
  22194. ////////////////////////////////
  22195. /// @name JSON Merge Patch functions
  22196. /// @{
  22197. /// @brief applies a JSON Merge Patch
  22198. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  22199. void merge_patch(const basic_json& apply_patch)
  22200. {
  22201. if (apply_patch.is_object())
  22202. {
  22203. if (!is_object())
  22204. {
  22205. *this = object();
  22206. }
  22207. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  22208. {
  22209. if (it.value().is_null())
  22210. {
  22211. erase(it.key());
  22212. }
  22213. else
  22214. {
  22215. operator[](it.key()).merge_patch(it.value());
  22216. }
  22217. }
  22218. }
  22219. else
  22220. {
  22221. *this = apply_patch;
  22222. }
  22223. }
  22224. /// @}
  22225. };
  22226. /// @brief user-defined to_string function for JSON values
  22227. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  22228. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22229. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  22230. {
  22231. return j.dump();
  22232. }
  22233. inline namespace literals
  22234. {
  22235. inline namespace json_literals
  22236. {
  22237. /// @brief user-defined string literal for JSON values
  22238. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  22239. JSON_HEDLEY_NON_NULL(1)
  22240. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  22241. inline nlohmann::json operator""_json(const char* s, std::size_t n)
  22242. #else
  22243. // GCC 4.8 requires a space between "" and suffix
  22244. inline nlohmann::json operator"" _json(const char* s, std::size_t n)
  22245. #endif
  22246. {
  22247. return nlohmann::json::parse(s, s + n);
  22248. }
  22249. #if defined(__cpp_char8_t)
  22250. JSON_HEDLEY_NON_NULL(1)
  22251. inline nlohmann::json operator""_json(const char8_t* s, std::size_t n)
  22252. {
  22253. return nlohmann::json::parse(reinterpret_cast<const char*>(s),
  22254. reinterpret_cast<const char*>(s) + n);
  22255. }
  22256. #endif
  22257. /// @brief user-defined string literal for JSON pointer
  22258. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  22259. JSON_HEDLEY_NON_NULL(1)
  22260. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  22261. inline nlohmann::json::json_pointer operator""_json_pointer(const char* s, std::size_t n)
  22262. #else
  22263. // GCC 4.8 requires a space between "" and suffix
  22264. inline nlohmann::json::json_pointer operator"" _json_pointer(const char* s, std::size_t n)
  22265. #endif
  22266. {
  22267. return nlohmann::json::json_pointer(std::string(s, n));
  22268. }
  22269. #if defined(__cpp_char8_t)
  22270. inline nlohmann::json::json_pointer operator""_json_pointer(const char8_t* s, std::size_t n)
  22271. {
  22272. return nlohmann::json::json_pointer(std::string(reinterpret_cast<const char*>(s), n));
  22273. }
  22274. #endif
  22275. } // namespace json_literals
  22276. } // namespace literals
  22277. NLOHMANN_JSON_NAMESPACE_END
  22278. ///////////////////////
  22279. // nonmember support //
  22280. ///////////////////////
  22281. namespace std // NOLINT(cert-dcl58-cpp)
  22282. {
  22283. /// @brief hash value for JSON objects
  22284. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  22285. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22286. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  22287. {
  22288. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  22289. {
  22290. return nlohmann::detail::hash(j);
  22291. }
  22292. };
  22293. // specialization for std::less<value_t>
  22294. template<>
  22295. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  22296. {
  22297. /*!
  22298. @brief compare two value_t enum values
  22299. @since version 3.0.0
  22300. */
  22301. bool operator()(::nlohmann::detail::value_t lhs,
  22302. ::nlohmann::detail::value_t rhs) const noexcept
  22303. {
  22304. #if JSON_HAS_THREE_WAY_COMPARISON
  22305. return std::is_lt(lhs <=> rhs); // *NOPAD*
  22306. #else
  22307. return ::nlohmann::detail::operator<(lhs, rhs);
  22308. #endif
  22309. }
  22310. };
  22311. // C++20 prohibit function specialization in the std namespace.
  22312. #ifndef JSON_HAS_CPP_20
  22313. /// @brief exchanges the values of two JSON objects
  22314. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  22315. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22316. 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)
  22317. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  22318. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  22319. {
  22320. j1.swap(j2);
  22321. }
  22322. #endif
  22323. } // namespace std
  22324. #if JSON_USE_GLOBAL_UDLS
  22325. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  22326. using nlohmann::literals::json_literals::operator""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22327. using nlohmann::literals::json_literals::operator""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22328. #else
  22329. // GCC 4.8 requires a space between "" and suffix
  22330. using nlohmann::literals::json_literals::operator"" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22331. using nlohmann::literals::json_literals::operator"" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  22332. #endif
  22333. #endif
  22334. // #include <nlohmann/detail/macro_unscope.hpp>
  22335. // __ _____ _____ _____
  22336. // __| | __| | | | JSON for Modern C++
  22337. // | | |__ | | | | | | version 3.12.0
  22338. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  22339. //
  22340. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  22341. // SPDX-License-Identifier: MIT
  22342. // restore clang diagnostic settings
  22343. #if defined(__clang__)
  22344. #pragma clang diagnostic pop
  22345. #endif
  22346. // clean up
  22347. #undef JSON_ASSERT
  22348. #undef JSON_INTERNAL_CATCH
  22349. #undef JSON_THROW
  22350. #undef JSON_PRIVATE_UNLESS_TESTED
  22351. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22352. #undef NLOHMANN_BASIC_JSON_TPL
  22353. #undef JSON_EXPLICIT
  22354. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  22355. #undef JSON_INLINE_VARIABLE
  22356. #undef JSON_NO_UNIQUE_ADDRESS
  22357. #undef JSON_DISABLE_ENUM_SERIALIZATION
  22358. #undef JSON_USE_GLOBAL_UDLS
  22359. #ifndef JSON_TEST_KEEP_MACROS
  22360. #undef JSON_CATCH
  22361. #undef JSON_TRY
  22362. #undef JSON_HAS_CPP_11
  22363. #undef JSON_HAS_CPP_14
  22364. #undef JSON_HAS_CPP_17
  22365. #undef JSON_HAS_CPP_20
  22366. #undef JSON_HAS_CPP_23
  22367. #undef JSON_HAS_CPP_26
  22368. #undef JSON_HAS_FILESYSTEM
  22369. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  22370. #undef JSON_HAS_THREE_WAY_COMPARISON
  22371. #undef JSON_HAS_RANGES
  22372. #undef JSON_HAS_STATIC_RTTI
  22373. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  22374. #endif
  22375. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  22376. // __ _____ _____ _____
  22377. // __| | __| | | | JSON for Modern C++
  22378. // | | |__ | | | | | | version 3.12.0
  22379. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  22380. //
  22381. // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
  22382. // SPDX-License-Identifier: MIT
  22383. #undef JSON_HEDLEY_ALWAYS_INLINE
  22384. #undef JSON_HEDLEY_ARM_VERSION
  22385. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  22386. #undef JSON_HEDLEY_ARRAY_PARAM
  22387. #undef JSON_HEDLEY_ASSUME
  22388. #undef JSON_HEDLEY_BEGIN_C_DECLS
  22389. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  22390. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  22391. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  22392. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  22393. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  22394. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  22395. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  22396. #undef JSON_HEDLEY_COMPCERT_VERSION
  22397. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  22398. #undef JSON_HEDLEY_CONCAT
  22399. #undef JSON_HEDLEY_CONCAT3
  22400. #undef JSON_HEDLEY_CONCAT3_EX
  22401. #undef JSON_HEDLEY_CONCAT_EX
  22402. #undef JSON_HEDLEY_CONST
  22403. #undef JSON_HEDLEY_CONSTEXPR
  22404. #undef JSON_HEDLEY_CONST_CAST
  22405. #undef JSON_HEDLEY_CPP_CAST
  22406. #undef JSON_HEDLEY_CRAY_VERSION
  22407. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  22408. #undef JSON_HEDLEY_C_DECL
  22409. #undef JSON_HEDLEY_DEPRECATED
  22410. #undef JSON_HEDLEY_DEPRECATED_FOR
  22411. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  22412. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  22413. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  22414. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  22415. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  22416. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  22417. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  22418. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  22419. #undef JSON_HEDLEY_DMC_VERSION
  22420. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  22421. #undef JSON_HEDLEY_EMPTY_BASES
  22422. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  22423. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  22424. #undef JSON_HEDLEY_END_C_DECLS
  22425. #undef JSON_HEDLEY_FLAGS
  22426. #undef JSON_HEDLEY_FLAGS_CAST
  22427. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  22428. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  22429. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  22430. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  22431. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  22432. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  22433. #undef JSON_HEDLEY_GCC_HAS_WARNING
  22434. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  22435. #undef JSON_HEDLEY_GCC_VERSION
  22436. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  22437. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  22438. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  22439. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  22440. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  22441. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  22442. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  22443. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  22444. #undef JSON_HEDLEY_GNUC_VERSION
  22445. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  22446. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  22447. #undef JSON_HEDLEY_HAS_BUILTIN
  22448. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  22449. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  22450. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  22451. #undef JSON_HEDLEY_HAS_EXTENSION
  22452. #undef JSON_HEDLEY_HAS_FEATURE
  22453. #undef JSON_HEDLEY_HAS_WARNING
  22454. #undef JSON_HEDLEY_IAR_VERSION
  22455. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  22456. #undef JSON_HEDLEY_IBM_VERSION
  22457. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  22458. #undef JSON_HEDLEY_IMPORT
  22459. #undef JSON_HEDLEY_INLINE
  22460. #undef JSON_HEDLEY_INTEL_CL_VERSION
  22461. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  22462. #undef JSON_HEDLEY_INTEL_VERSION
  22463. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  22464. #undef JSON_HEDLEY_IS_CONSTANT
  22465. #undef JSON_HEDLEY_IS_CONSTEXPR_
  22466. #undef JSON_HEDLEY_LIKELY
  22467. #undef JSON_HEDLEY_MALLOC
  22468. #undef JSON_HEDLEY_MCST_LCC_VERSION
  22469. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  22470. #undef JSON_HEDLEY_MESSAGE
  22471. #undef JSON_HEDLEY_MSVC_VERSION
  22472. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  22473. #undef JSON_HEDLEY_NEVER_INLINE
  22474. #undef JSON_HEDLEY_NON_NULL
  22475. #undef JSON_HEDLEY_NO_ESCAPE
  22476. #undef JSON_HEDLEY_NO_RETURN
  22477. #undef JSON_HEDLEY_NO_THROW
  22478. #undef JSON_HEDLEY_NULL
  22479. #undef JSON_HEDLEY_PELLES_VERSION
  22480. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  22481. #undef JSON_HEDLEY_PGI_VERSION
  22482. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  22483. #undef JSON_HEDLEY_PREDICT
  22484. #undef JSON_HEDLEY_PRINTF_FORMAT
  22485. #undef JSON_HEDLEY_PRIVATE
  22486. #undef JSON_HEDLEY_PUBLIC
  22487. #undef JSON_HEDLEY_PURE
  22488. #undef JSON_HEDLEY_REINTERPRET_CAST
  22489. #undef JSON_HEDLEY_REQUIRE
  22490. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  22491. #undef JSON_HEDLEY_REQUIRE_MSG
  22492. #undef JSON_HEDLEY_RESTRICT
  22493. #undef JSON_HEDLEY_RETURNS_NON_NULL
  22494. #undef JSON_HEDLEY_SENTINEL
  22495. #undef JSON_HEDLEY_STATIC_ASSERT
  22496. #undef JSON_HEDLEY_STATIC_CAST
  22497. #undef JSON_HEDLEY_STRINGIFY
  22498. #undef JSON_HEDLEY_STRINGIFY_EX
  22499. #undef JSON_HEDLEY_SUNPRO_VERSION
  22500. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  22501. #undef JSON_HEDLEY_TINYC_VERSION
  22502. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  22503. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  22504. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  22505. #undef JSON_HEDLEY_TI_CL2000_VERSION
  22506. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  22507. #undef JSON_HEDLEY_TI_CL430_VERSION
  22508. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  22509. #undef JSON_HEDLEY_TI_CL6X_VERSION
  22510. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  22511. #undef JSON_HEDLEY_TI_CL7X_VERSION
  22512. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  22513. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  22514. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  22515. #undef JSON_HEDLEY_TI_VERSION
  22516. #undef JSON_HEDLEY_TI_VERSION_CHECK
  22517. #undef JSON_HEDLEY_UNAVAILABLE
  22518. #undef JSON_HEDLEY_UNLIKELY
  22519. #undef JSON_HEDLEY_UNPREDICTABLE
  22520. #undef JSON_HEDLEY_UNREACHABLE
  22521. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  22522. #undef JSON_HEDLEY_VERSION
  22523. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  22524. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  22525. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  22526. #undef JSON_HEDLEY_VERSION_ENCODE
  22527. #undef JSON_HEDLEY_WARNING
  22528. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  22529. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  22530. #undef JSON_HEDLEY_FALL_THROUGH
  22531. #endif // INCLUDE_NLOHMANN_JSON_HPP_