json.hpp 899 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 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.11.3
  34. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  35. //
  36. // SPDX-FileCopyrightText: 2013-2023 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.11.3
  43. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  44. //
  45. // SPDX-FileCopyrightText: 2013-2023 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 != 11 || NLOHMANN_JSON_VERSION_PATCH != 3
  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 11 // NOLINT(modernize-macro-to-enum)
  57. #define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum)
  58. #ifndef JSON_DIAGNOSTICS
  59. #define JSON_DIAGNOSTICS 0
  60. #endif
  61. #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  62. #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
  63. #endif
  64. #if JSON_DIAGNOSTICS
  65. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
  66. #else
  67. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
  68. #endif
  69. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  70. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
  71. #else
  72. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
  73. #endif
  74. #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
  75. #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
  76. #endif
  77. // Construct the namespace ABI tags component
  78. #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
  79. #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
  80. NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
  81. #define NLOHMANN_JSON_ABI_TAGS \
  82. NLOHMANN_JSON_ABI_TAGS_CONCAT( \
  83. NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
  84. NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
  85. // Construct the namespace version component
  86. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
  87. _v ## major ## _ ## minor ## _ ## patch
  88. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
  89. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
  90. #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
  91. #define NLOHMANN_JSON_NAMESPACE_VERSION
  92. #else
  93. #define NLOHMANN_JSON_NAMESPACE_VERSION \
  94. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
  95. NLOHMANN_JSON_VERSION_MINOR, \
  96. NLOHMANN_JSON_VERSION_PATCH)
  97. #endif
  98. // Combine namespace components
  99. #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
  100. #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
  101. NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
  102. #ifndef NLOHMANN_JSON_NAMESPACE
  103. #define NLOHMANN_JSON_NAMESPACE \
  104. nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
  105. NLOHMANN_JSON_ABI_TAGS, \
  106. NLOHMANN_JSON_NAMESPACE_VERSION)
  107. #endif
  108. #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
  109. #define NLOHMANN_JSON_NAMESPACE_BEGIN \
  110. namespace nlohmann \
  111. { \
  112. inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
  113. NLOHMANN_JSON_ABI_TAGS, \
  114. NLOHMANN_JSON_NAMESPACE_VERSION) \
  115. {
  116. #endif
  117. #ifndef NLOHMANN_JSON_NAMESPACE_END
  118. #define NLOHMANN_JSON_NAMESPACE_END \
  119. } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
  120. } // namespace nlohmann
  121. #endif
  122. // #include <nlohmann/detail/conversions/from_json.hpp>
  123. // __ _____ _____ _____
  124. // __| | __| | | | JSON for Modern C++
  125. // | | |__ | | | | | | version 3.11.3
  126. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  127. //
  128. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  129. // SPDX-License-Identifier: MIT
  130. #include <algorithm> // transform
  131. #include <array> // array
  132. #include <forward_list> // forward_list
  133. #include <iterator> // inserter, front_inserter, end
  134. #include <map> // map
  135. #include <string> // string
  136. #include <tuple> // tuple, make_tuple
  137. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  138. #include <unordered_map> // unordered_map
  139. #include <utility> // pair, declval
  140. #include <valarray> // valarray
  141. // #include <nlohmann/detail/exceptions.hpp>
  142. // __ _____ _____ _____
  143. // __| | __| | | | JSON for Modern C++
  144. // | | |__ | | | | | | version 3.11.3
  145. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  146. //
  147. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  148. // SPDX-License-Identifier: MIT
  149. #include <cstddef> // nullptr_t
  150. #include <exception> // exception
  151. #if JSON_DIAGNOSTICS
  152. #include <numeric> // accumulate
  153. #endif
  154. #include <stdexcept> // runtime_error
  155. #include <string> // to_string
  156. #include <vector> // vector
  157. // #include <nlohmann/detail/value_t.hpp>
  158. // __ _____ _____ _____
  159. // __| | __| | | | JSON for Modern C++
  160. // | | |__ | | | | | | version 3.11.3
  161. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  162. //
  163. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  164. // SPDX-License-Identifier: MIT
  165. #include <array> // array
  166. #include <cstddef> // size_t
  167. #include <cstdint> // uint8_t
  168. #include <string> // string
  169. // #include <nlohmann/detail/macro_scope.hpp>
  170. // __ _____ _____ _____
  171. // __| | __| | | | JSON for Modern C++
  172. // | | |__ | | | | | | version 3.11.3
  173. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  174. //
  175. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  176. // SPDX-License-Identifier: MIT
  177. #include <utility> // declval, pair
  178. // #include <nlohmann/detail/meta/detected.hpp>
  179. // __ _____ _____ _____
  180. // __| | __| | | | JSON for Modern C++
  181. // | | |__ | | | | | | version 3.11.3
  182. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  183. //
  184. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  185. // SPDX-License-Identifier: MIT
  186. #include <type_traits>
  187. // #include <nlohmann/detail/meta/void_t.hpp>
  188. // __ _____ _____ _____
  189. // __| | __| | | | JSON for Modern C++
  190. // | | |__ | | | | | | version 3.11.3
  191. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  192. //
  193. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  194. // SPDX-License-Identifier: MIT
  195. // #include <nlohmann/detail/abi_macros.hpp>
  196. NLOHMANN_JSON_NAMESPACE_BEGIN
  197. namespace detail
  198. {
  199. template<typename ...Ts> struct make_void
  200. {
  201. using type = void;
  202. };
  203. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  204. } // namespace detail
  205. NLOHMANN_JSON_NAMESPACE_END
  206. NLOHMANN_JSON_NAMESPACE_BEGIN
  207. namespace detail
  208. {
  209. // https://en.cppreference.com/w/cpp/experimental/is_detected
  210. struct nonesuch
  211. {
  212. nonesuch() = delete;
  213. ~nonesuch() = delete;
  214. nonesuch(nonesuch const&) = delete;
  215. nonesuch(nonesuch const&&) = delete;
  216. void operator=(nonesuch const&) = delete;
  217. void operator=(nonesuch&&) = delete;
  218. };
  219. template<class Default,
  220. class AlwaysVoid,
  221. template<class...> class Op,
  222. class... Args>
  223. struct detector
  224. {
  225. using value_t = std::false_type;
  226. using type = Default;
  227. };
  228. template<class Default, template<class...> class Op, class... Args>
  229. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  230. {
  231. using value_t = std::true_type;
  232. using type = Op<Args...>;
  233. };
  234. template<template<class...> class Op, class... Args>
  235. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  236. template<template<class...> class Op, class... Args>
  237. struct is_detected_lazy : is_detected<Op, Args...> { };
  238. template<template<class...> class Op, class... Args>
  239. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  240. template<class Default, template<class...> class Op, class... Args>
  241. using detected_or = detector<Default, void, Op, Args...>;
  242. template<class Default, template<class...> class Op, class... Args>
  243. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  244. template<class Expected, template<class...> class Op, class... Args>
  245. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  246. template<class To, template<class...> class Op, class... Args>
  247. using is_detected_convertible =
  248. std::is_convertible<detected_t<Op, Args...>, To>;
  249. } // namespace detail
  250. NLOHMANN_JSON_NAMESPACE_END
  251. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  252. // __ _____ _____ _____
  253. // __| | __| | | | JSON for Modern C++
  254. // | | |__ | | | | | | version 3.11.3
  255. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  256. //
  257. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  258. // SPDX-FileCopyrightText: 2016-2021 Evan Nemerson <evan@nemerson.com>
  259. // SPDX-License-Identifier: MIT
  260. /* Hedley - https://nemequ.github.io/hedley
  261. * Created by Evan Nemerson <evan@nemerson.com>
  262. */
  263. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  264. #if defined(JSON_HEDLEY_VERSION)
  265. #undef JSON_HEDLEY_VERSION
  266. #endif
  267. #define JSON_HEDLEY_VERSION 15
  268. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  269. #undef JSON_HEDLEY_STRINGIFY_EX
  270. #endif
  271. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  272. #if defined(JSON_HEDLEY_STRINGIFY)
  273. #undef JSON_HEDLEY_STRINGIFY
  274. #endif
  275. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  276. #if defined(JSON_HEDLEY_CONCAT_EX)
  277. #undef JSON_HEDLEY_CONCAT_EX
  278. #endif
  279. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  280. #if defined(JSON_HEDLEY_CONCAT)
  281. #undef JSON_HEDLEY_CONCAT
  282. #endif
  283. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  284. #if defined(JSON_HEDLEY_CONCAT3_EX)
  285. #undef JSON_HEDLEY_CONCAT3_EX
  286. #endif
  287. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  288. #if defined(JSON_HEDLEY_CONCAT3)
  289. #undef JSON_HEDLEY_CONCAT3
  290. #endif
  291. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  292. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  293. #undef JSON_HEDLEY_VERSION_ENCODE
  294. #endif
  295. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  296. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  297. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  298. #endif
  299. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  300. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  301. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  302. #endif
  303. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  304. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  305. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  306. #endif
  307. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  308. #if defined(JSON_HEDLEY_GNUC_VERSION)
  309. #undef JSON_HEDLEY_GNUC_VERSION
  310. #endif
  311. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  312. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  313. #elif defined(__GNUC__)
  314. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  315. #endif
  316. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  317. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  318. #endif
  319. #if defined(JSON_HEDLEY_GNUC_VERSION)
  320. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  321. #else
  322. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  323. #endif
  324. #if defined(JSON_HEDLEY_MSVC_VERSION)
  325. #undef JSON_HEDLEY_MSVC_VERSION
  326. #endif
  327. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  328. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  329. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  330. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  331. #elif defined(_MSC_VER) && !defined(__ICL)
  332. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  333. #endif
  334. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  335. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  336. #endif
  337. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  338. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  339. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  340. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  341. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  342. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  343. #else
  344. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  345. #endif
  346. #if defined(JSON_HEDLEY_INTEL_VERSION)
  347. #undef JSON_HEDLEY_INTEL_VERSION
  348. #endif
  349. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  350. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  351. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  352. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  353. #endif
  354. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  355. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  356. #endif
  357. #if defined(JSON_HEDLEY_INTEL_VERSION)
  358. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  359. #else
  360. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  361. #endif
  362. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  363. #undef JSON_HEDLEY_INTEL_CL_VERSION
  364. #endif
  365. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  366. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  367. #endif
  368. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  369. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  370. #endif
  371. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  372. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  373. #else
  374. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  375. #endif
  376. #if defined(JSON_HEDLEY_PGI_VERSION)
  377. #undef JSON_HEDLEY_PGI_VERSION
  378. #endif
  379. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  380. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  381. #endif
  382. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  383. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  384. #endif
  385. #if defined(JSON_HEDLEY_PGI_VERSION)
  386. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  387. #else
  388. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  389. #endif
  390. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  391. #undef JSON_HEDLEY_SUNPRO_VERSION
  392. #endif
  393. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  394. #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)
  395. #elif defined(__SUNPRO_C)
  396. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  397. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  398. #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)
  399. #elif defined(__SUNPRO_CC)
  400. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  401. #endif
  402. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  403. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  404. #endif
  405. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  406. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  407. #else
  408. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  409. #endif
  410. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  411. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  412. #endif
  413. #if defined(__EMSCRIPTEN__)
  414. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  415. #endif
  416. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  417. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  418. #endif
  419. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  420. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  421. #else
  422. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  423. #endif
  424. #if defined(JSON_HEDLEY_ARM_VERSION)
  425. #undef JSON_HEDLEY_ARM_VERSION
  426. #endif
  427. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  428. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  429. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  430. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  431. #endif
  432. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  433. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  434. #endif
  435. #if defined(JSON_HEDLEY_ARM_VERSION)
  436. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  437. #else
  438. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  439. #endif
  440. #if defined(JSON_HEDLEY_IBM_VERSION)
  441. #undef JSON_HEDLEY_IBM_VERSION
  442. #endif
  443. #if defined(__ibmxl__)
  444. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  445. #elif defined(__xlC__) && defined(__xlC_ver__)
  446. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  447. #elif defined(__xlC__)
  448. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  449. #endif
  450. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  451. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  452. #endif
  453. #if defined(JSON_HEDLEY_IBM_VERSION)
  454. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  455. #else
  456. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  457. #endif
  458. #if defined(JSON_HEDLEY_TI_VERSION)
  459. #undef JSON_HEDLEY_TI_VERSION
  460. #endif
  461. #if \
  462. defined(__TI_COMPILER_VERSION__) && \
  463. ( \
  464. defined(__TMS470__) || defined(__TI_ARM__) || \
  465. defined(__MSP430__) || \
  466. defined(__TMS320C2000__) \
  467. )
  468. #if (__TI_COMPILER_VERSION__ >= 16000000)
  469. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  470. #endif
  471. #endif
  472. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  473. #undef JSON_HEDLEY_TI_VERSION_CHECK
  474. #endif
  475. #if defined(JSON_HEDLEY_TI_VERSION)
  476. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  477. #else
  478. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  479. #endif
  480. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  481. #undef JSON_HEDLEY_TI_CL2000_VERSION
  482. #endif
  483. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  484. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  485. #endif
  486. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  487. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  488. #endif
  489. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  490. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  491. #else
  492. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  493. #endif
  494. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  495. #undef JSON_HEDLEY_TI_CL430_VERSION
  496. #endif
  497. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  498. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  499. #endif
  500. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  501. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  502. #endif
  503. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  504. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  505. #else
  506. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  507. #endif
  508. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  509. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  510. #endif
  511. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  512. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  513. #endif
  514. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  515. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  516. #endif
  517. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  518. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  519. #else
  520. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  521. #endif
  522. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  523. #undef JSON_HEDLEY_TI_CL6X_VERSION
  524. #endif
  525. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  526. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  527. #endif
  528. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  529. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  530. #endif
  531. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  532. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  533. #else
  534. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  535. #endif
  536. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  537. #undef JSON_HEDLEY_TI_CL7X_VERSION
  538. #endif
  539. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  540. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  541. #endif
  542. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  543. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  544. #endif
  545. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  546. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  547. #else
  548. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  549. #endif
  550. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  551. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  552. #endif
  553. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  554. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  555. #endif
  556. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  557. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  558. #endif
  559. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  560. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  561. #else
  562. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  563. #endif
  564. #if defined(JSON_HEDLEY_CRAY_VERSION)
  565. #undef JSON_HEDLEY_CRAY_VERSION
  566. #endif
  567. #if defined(_CRAYC)
  568. #if defined(_RELEASE_PATCHLEVEL)
  569. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  570. #else
  571. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  572. #endif
  573. #endif
  574. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  575. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  576. #endif
  577. #if defined(JSON_HEDLEY_CRAY_VERSION)
  578. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  579. #else
  580. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  581. #endif
  582. #if defined(JSON_HEDLEY_IAR_VERSION)
  583. #undef JSON_HEDLEY_IAR_VERSION
  584. #endif
  585. #if defined(__IAR_SYSTEMS_ICC__)
  586. #if __VER__ > 1000
  587. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  588. #else
  589. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  590. #endif
  591. #endif
  592. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  593. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  594. #endif
  595. #if defined(JSON_HEDLEY_IAR_VERSION)
  596. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  597. #else
  598. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  599. #endif
  600. #if defined(JSON_HEDLEY_TINYC_VERSION)
  601. #undef JSON_HEDLEY_TINYC_VERSION
  602. #endif
  603. #if defined(__TINYC__)
  604. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  605. #endif
  606. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  607. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  608. #endif
  609. #if defined(JSON_HEDLEY_TINYC_VERSION)
  610. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  611. #else
  612. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  613. #endif
  614. #if defined(JSON_HEDLEY_DMC_VERSION)
  615. #undef JSON_HEDLEY_DMC_VERSION
  616. #endif
  617. #if defined(__DMC__)
  618. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  619. #endif
  620. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  621. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  622. #endif
  623. #if defined(JSON_HEDLEY_DMC_VERSION)
  624. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  625. #else
  626. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  627. #endif
  628. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  629. #undef JSON_HEDLEY_COMPCERT_VERSION
  630. #endif
  631. #if defined(__COMPCERT_VERSION__)
  632. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  633. #endif
  634. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  635. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  636. #endif
  637. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  638. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  639. #else
  640. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  641. #endif
  642. #if defined(JSON_HEDLEY_PELLES_VERSION)
  643. #undef JSON_HEDLEY_PELLES_VERSION
  644. #endif
  645. #if defined(__POCC__)
  646. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  647. #endif
  648. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  649. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  650. #endif
  651. #if defined(JSON_HEDLEY_PELLES_VERSION)
  652. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  653. #else
  654. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  655. #endif
  656. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  657. #undef JSON_HEDLEY_MCST_LCC_VERSION
  658. #endif
  659. #if defined(__LCC__) && defined(__LCC_MINOR__)
  660. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  661. #endif
  662. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  663. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  664. #endif
  665. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  666. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  667. #else
  668. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  669. #endif
  670. #if defined(JSON_HEDLEY_GCC_VERSION)
  671. #undef JSON_HEDLEY_GCC_VERSION
  672. #endif
  673. #if \
  674. defined(JSON_HEDLEY_GNUC_VERSION) && \
  675. !defined(__clang__) && \
  676. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  677. !defined(JSON_HEDLEY_PGI_VERSION) && \
  678. !defined(JSON_HEDLEY_ARM_VERSION) && \
  679. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  680. !defined(JSON_HEDLEY_TI_VERSION) && \
  681. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  682. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  683. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  684. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  685. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  686. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  687. !defined(__COMPCERT__) && \
  688. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  689. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  690. #endif
  691. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  692. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  693. #endif
  694. #if defined(JSON_HEDLEY_GCC_VERSION)
  695. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  696. #else
  697. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  698. #endif
  699. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  700. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  701. #endif
  702. #if \
  703. defined(__has_attribute) && \
  704. ( \
  705. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  706. )
  707. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  708. #else
  709. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  710. #endif
  711. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  712. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  713. #endif
  714. #if defined(__has_attribute)
  715. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  716. #else
  717. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  718. #endif
  719. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  720. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  721. #endif
  722. #if defined(__has_attribute)
  723. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  724. #else
  725. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  726. #endif
  727. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  728. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  729. #endif
  730. #if \
  731. defined(__has_cpp_attribute) && \
  732. defined(__cplusplus) && \
  733. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  734. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  735. #else
  736. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  737. #endif
  738. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  739. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  740. #endif
  741. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  742. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  743. #elif \
  744. !defined(JSON_HEDLEY_PGI_VERSION) && \
  745. !defined(JSON_HEDLEY_IAR_VERSION) && \
  746. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  747. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  748. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  749. #else
  750. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  751. #endif
  752. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  753. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  754. #endif
  755. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  756. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  757. #else
  758. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  759. #endif
  760. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  761. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  762. #endif
  763. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  764. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  765. #else
  766. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  767. #endif
  768. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  769. #undef JSON_HEDLEY_HAS_BUILTIN
  770. #endif
  771. #if defined(__has_builtin)
  772. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  773. #else
  774. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  775. #endif
  776. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  777. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  778. #endif
  779. #if defined(__has_builtin)
  780. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  781. #else
  782. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  783. #endif
  784. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  785. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  786. #endif
  787. #if defined(__has_builtin)
  788. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  789. #else
  790. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  791. #endif
  792. #if defined(JSON_HEDLEY_HAS_FEATURE)
  793. #undef JSON_HEDLEY_HAS_FEATURE
  794. #endif
  795. #if defined(__has_feature)
  796. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  797. #else
  798. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  799. #endif
  800. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  801. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  802. #endif
  803. #if defined(__has_feature)
  804. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  805. #else
  806. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  807. #endif
  808. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  809. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  810. #endif
  811. #if defined(__has_feature)
  812. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  813. #else
  814. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  815. #endif
  816. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  817. #undef JSON_HEDLEY_HAS_EXTENSION
  818. #endif
  819. #if defined(__has_extension)
  820. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  821. #else
  822. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  823. #endif
  824. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  825. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  826. #endif
  827. #if defined(__has_extension)
  828. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  829. #else
  830. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  831. #endif
  832. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  833. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  834. #endif
  835. #if defined(__has_extension)
  836. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  837. #else
  838. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  839. #endif
  840. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  841. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  842. #endif
  843. #if defined(__has_declspec_attribute)
  844. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  845. #else
  846. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  847. #endif
  848. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  849. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  850. #endif
  851. #if defined(__has_declspec_attribute)
  852. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  853. #else
  854. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  855. #endif
  856. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  857. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  858. #endif
  859. #if defined(__has_declspec_attribute)
  860. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  861. #else
  862. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  863. #endif
  864. #if defined(JSON_HEDLEY_HAS_WARNING)
  865. #undef JSON_HEDLEY_HAS_WARNING
  866. #endif
  867. #if defined(__has_warning)
  868. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  869. #else
  870. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  871. #endif
  872. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  873. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  874. #endif
  875. #if defined(__has_warning)
  876. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  877. #else
  878. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  879. #endif
  880. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  881. #undef JSON_HEDLEY_GCC_HAS_WARNING
  882. #endif
  883. #if defined(__has_warning)
  884. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  885. #else
  886. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  887. #endif
  888. #if \
  889. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  890. defined(__clang__) || \
  891. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  892. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  893. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  894. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  895. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  896. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  897. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  898. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  899. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  900. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  901. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  902. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  903. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  904. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  905. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  906. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  907. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  908. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  909. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  910. #else
  911. #define JSON_HEDLEY_PRAGMA(value)
  912. #endif
  913. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  914. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  915. #endif
  916. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  917. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  918. #endif
  919. #if defined(__clang__)
  920. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  921. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  922. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  923. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  924. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  925. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  926. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  927. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  928. #elif \
  929. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  930. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  931. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  932. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  933. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  934. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  935. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  936. #elif \
  937. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  938. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  939. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  940. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  941. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  942. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  943. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  944. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  945. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  946. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  947. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  948. #else
  949. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  950. #define JSON_HEDLEY_DIAGNOSTIC_POP
  951. #endif
  952. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  953. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  954. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  955. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  956. #endif
  957. #if defined(__cplusplus)
  958. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  959. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  960. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  961. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  962. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  963. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  964. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  965. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  966. xpr \
  967. JSON_HEDLEY_DIAGNOSTIC_POP
  968. # else
  969. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  970. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  971. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  972. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  973. xpr \
  974. JSON_HEDLEY_DIAGNOSTIC_POP
  975. # endif
  976. # else
  977. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  978. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  979. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  980. xpr \
  981. JSON_HEDLEY_DIAGNOSTIC_POP
  982. # endif
  983. # endif
  984. #endif
  985. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  986. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  987. #endif
  988. #if defined(JSON_HEDLEY_CONST_CAST)
  989. #undef JSON_HEDLEY_CONST_CAST
  990. #endif
  991. #if defined(__cplusplus)
  992. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  993. #elif \
  994. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  995. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  996. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  997. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  998. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  999. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  1000. ((T) (expr)); \
  1001. JSON_HEDLEY_DIAGNOSTIC_POP \
  1002. }))
  1003. #else
  1004. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  1005. #endif
  1006. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  1007. #undef JSON_HEDLEY_REINTERPRET_CAST
  1008. #endif
  1009. #if defined(__cplusplus)
  1010. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  1011. #else
  1012. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  1013. #endif
  1014. #if defined(JSON_HEDLEY_STATIC_CAST)
  1015. #undef JSON_HEDLEY_STATIC_CAST
  1016. #endif
  1017. #if defined(__cplusplus)
  1018. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  1019. #else
  1020. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  1021. #endif
  1022. #if defined(JSON_HEDLEY_CPP_CAST)
  1023. #undef JSON_HEDLEY_CPP_CAST
  1024. #endif
  1025. #if defined(__cplusplus)
  1026. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  1027. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1028. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1029. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  1030. ((T) (expr)) \
  1031. JSON_HEDLEY_DIAGNOSTIC_POP
  1032. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  1033. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1034. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1035. _Pragma("diag_suppress=Pe137") \
  1036. JSON_HEDLEY_DIAGNOSTIC_POP
  1037. # else
  1038. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  1039. # endif
  1040. #else
  1041. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  1042. #endif
  1043. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  1044. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1045. #endif
  1046. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  1047. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  1048. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1049. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  1050. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1051. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  1052. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1053. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  1054. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1055. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1056. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1057. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1058. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1059. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  1060. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1061. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1062. #elif \
  1063. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1064. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1065. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1066. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1067. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1068. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1069. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1070. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1071. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1072. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1073. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  1074. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  1075. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  1076. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  1077. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  1078. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  1079. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1080. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  1081. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  1082. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  1083. #else
  1084. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1085. #endif
  1086. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  1087. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1088. #endif
  1089. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1090. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  1091. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1092. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  1093. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1094. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  1095. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1096. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  1097. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1098. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  1099. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1100. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  1101. #elif \
  1102. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  1103. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1104. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1105. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  1106. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1107. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  1108. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1109. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1110. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  1111. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1112. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  1113. #else
  1114. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1115. #endif
  1116. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1117. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1118. #endif
  1119. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1120. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1121. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1122. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1123. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1124. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1125. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1126. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1127. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1128. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1129. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1130. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1131. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1132. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1133. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1134. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1135. #elif \
  1136. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1137. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1138. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1139. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1140. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1141. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1142. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1143. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1144. #else
  1145. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1146. #endif
  1147. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1148. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1149. #endif
  1150. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1151. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1152. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1153. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1154. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1155. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1156. #else
  1157. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1158. #endif
  1159. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1160. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1161. #endif
  1162. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1163. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1164. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1165. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1166. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1167. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1168. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1169. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1170. #else
  1171. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1172. #endif
  1173. #if defined(JSON_HEDLEY_DEPRECATED)
  1174. #undef JSON_HEDLEY_DEPRECATED
  1175. #endif
  1176. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1177. #undef JSON_HEDLEY_DEPRECATED_FOR
  1178. #endif
  1179. #if \
  1180. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1181. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1182. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1183. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1184. #elif \
  1185. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1186. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1187. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1188. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1189. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1190. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1191. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1192. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1193. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1194. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1195. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1196. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1197. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1198. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1199. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1200. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1201. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1202. #elif \
  1203. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1204. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1205. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1206. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1207. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1208. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1209. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1210. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1211. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1212. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1213. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1214. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1215. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1216. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1217. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1218. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1219. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1220. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1221. #elif \
  1222. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1223. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1224. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1225. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1226. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1227. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1228. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1229. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1230. #else
  1231. #define JSON_HEDLEY_DEPRECATED(since)
  1232. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1233. #endif
  1234. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1235. #undef JSON_HEDLEY_UNAVAILABLE
  1236. #endif
  1237. #if \
  1238. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1239. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1240. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1241. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1242. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1243. #else
  1244. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1245. #endif
  1246. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1247. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1248. #endif
  1249. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1250. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1251. #endif
  1252. #if \
  1253. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1254. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1255. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1256. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1257. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1258. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1259. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1260. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1261. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1262. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1263. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1264. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1265. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1266. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1267. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1268. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1269. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1270. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1271. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1272. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1273. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1274. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1275. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1276. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1277. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1278. #elif defined(_Check_return_) /* SAL */
  1279. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1280. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1281. #else
  1282. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1283. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1284. #endif
  1285. #if defined(JSON_HEDLEY_SENTINEL)
  1286. #undef JSON_HEDLEY_SENTINEL
  1287. #endif
  1288. #if \
  1289. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1290. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1291. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1292. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1293. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1294. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1295. #else
  1296. #define JSON_HEDLEY_SENTINEL(position)
  1297. #endif
  1298. #if defined(JSON_HEDLEY_NO_RETURN)
  1299. #undef JSON_HEDLEY_NO_RETURN
  1300. #endif
  1301. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1302. #define JSON_HEDLEY_NO_RETURN __noreturn
  1303. #elif \
  1304. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1305. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1306. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1307. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1308. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1309. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1310. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1311. #elif \
  1312. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1313. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1314. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1315. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1316. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1317. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1318. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1319. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1320. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1321. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1322. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1323. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1324. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1325. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1326. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1327. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1328. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1329. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1330. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1331. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1332. #elif \
  1333. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1334. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1335. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1336. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1337. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1338. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1339. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1340. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1341. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1342. #else
  1343. #define JSON_HEDLEY_NO_RETURN
  1344. #endif
  1345. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1346. #undef JSON_HEDLEY_NO_ESCAPE
  1347. #endif
  1348. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1349. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1350. #else
  1351. #define JSON_HEDLEY_NO_ESCAPE
  1352. #endif
  1353. #if defined(JSON_HEDLEY_UNREACHABLE)
  1354. #undef JSON_HEDLEY_UNREACHABLE
  1355. #endif
  1356. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1357. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1358. #endif
  1359. #if defined(JSON_HEDLEY_ASSUME)
  1360. #undef JSON_HEDLEY_ASSUME
  1361. #endif
  1362. #if \
  1363. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1364. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1365. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1366. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1367. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1368. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1369. #elif \
  1370. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1371. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1372. #if defined(__cplusplus)
  1373. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1374. #else
  1375. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1376. #endif
  1377. #endif
  1378. #if \
  1379. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1380. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1381. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1382. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1383. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1384. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1385. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1386. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1387. #elif defined(JSON_HEDLEY_ASSUME)
  1388. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1389. #endif
  1390. #if !defined(JSON_HEDLEY_ASSUME)
  1391. #if defined(JSON_HEDLEY_UNREACHABLE)
  1392. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1393. #else
  1394. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1395. #endif
  1396. #endif
  1397. #if defined(JSON_HEDLEY_UNREACHABLE)
  1398. #if \
  1399. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1400. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1401. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1402. #else
  1403. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1404. #endif
  1405. #else
  1406. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1407. #endif
  1408. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1409. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1410. #endif
  1411. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1412. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1413. #pragma clang diagnostic ignored "-Wpedantic"
  1414. #endif
  1415. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1416. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1417. #endif
  1418. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1419. #if defined(__clang__)
  1420. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1421. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1422. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1423. #endif
  1424. #endif
  1425. #if defined(JSON_HEDLEY_NON_NULL)
  1426. #undef JSON_HEDLEY_NON_NULL
  1427. #endif
  1428. #if \
  1429. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1430. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1431. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1432. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1433. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1434. #else
  1435. #define JSON_HEDLEY_NON_NULL(...)
  1436. #endif
  1437. JSON_HEDLEY_DIAGNOSTIC_POP
  1438. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1439. #undef JSON_HEDLEY_PRINTF_FORMAT
  1440. #endif
  1441. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1442. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1443. #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
  1444. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
  1445. #elif \
  1446. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1447. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1448. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1449. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1450. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1451. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1452. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1453. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1454. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1455. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1456. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1457. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1458. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1459. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1460. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1461. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1462. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1463. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1464. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1465. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1466. #else
  1467. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1468. #endif
  1469. #if defined(JSON_HEDLEY_CONSTEXPR)
  1470. #undef JSON_HEDLEY_CONSTEXPR
  1471. #endif
  1472. #if defined(__cplusplus)
  1473. #if __cplusplus >= 201103L
  1474. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1475. #endif
  1476. #endif
  1477. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1478. #define JSON_HEDLEY_CONSTEXPR
  1479. #endif
  1480. #if defined(JSON_HEDLEY_PREDICT)
  1481. #undef JSON_HEDLEY_PREDICT
  1482. #endif
  1483. #if defined(JSON_HEDLEY_LIKELY)
  1484. #undef JSON_HEDLEY_LIKELY
  1485. #endif
  1486. #if defined(JSON_HEDLEY_UNLIKELY)
  1487. #undef JSON_HEDLEY_UNLIKELY
  1488. #endif
  1489. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1490. #undef JSON_HEDLEY_UNPREDICTABLE
  1491. #endif
  1492. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1493. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1494. #endif
  1495. #if \
  1496. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1497. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1498. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1499. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1500. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1501. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1502. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1503. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1504. #elif \
  1505. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1506. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1507. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1508. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1509. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1510. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1511. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1512. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1513. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1514. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1515. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1516. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1517. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1518. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1519. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1520. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1521. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1522. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1523. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1524. (__extension__ ({ \
  1525. double hedley_probability_ = (probability); \
  1526. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1527. }))
  1528. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1529. (__extension__ ({ \
  1530. double hedley_probability_ = (probability); \
  1531. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1532. }))
  1533. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1534. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1535. #else
  1536. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1537. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1538. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1539. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1540. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1541. #endif
  1542. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1543. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1544. #endif
  1545. #if defined(JSON_HEDLEY_MALLOC)
  1546. #undef JSON_HEDLEY_MALLOC
  1547. #endif
  1548. #if \
  1549. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1550. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1551. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1552. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1553. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1554. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1555. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1556. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1557. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1558. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1559. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1560. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1561. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1562. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1563. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1564. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1565. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1566. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1567. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1568. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1569. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1570. #elif \
  1571. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1572. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1573. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1574. #else
  1575. #define JSON_HEDLEY_MALLOC
  1576. #endif
  1577. #if defined(JSON_HEDLEY_PURE)
  1578. #undef JSON_HEDLEY_PURE
  1579. #endif
  1580. #if \
  1581. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1582. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1583. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1584. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1585. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1586. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1587. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1588. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1589. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1590. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1591. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1592. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1593. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1594. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1595. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1596. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1597. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1598. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1599. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1600. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1601. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1602. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1603. #elif defined(__cplusplus) && \
  1604. ( \
  1605. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1606. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1607. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1608. )
  1609. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1610. #else
  1611. # define JSON_HEDLEY_PURE
  1612. #endif
  1613. #if defined(JSON_HEDLEY_CONST)
  1614. #undef JSON_HEDLEY_CONST
  1615. #endif
  1616. #if \
  1617. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1618. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1619. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1620. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1621. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1622. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1623. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1624. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1625. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1626. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1627. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1628. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1629. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1630. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1631. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1632. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1633. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1634. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1635. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1636. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1637. #elif \
  1638. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1639. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1640. #else
  1641. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1642. #endif
  1643. #if defined(JSON_HEDLEY_RESTRICT)
  1644. #undef JSON_HEDLEY_RESTRICT
  1645. #endif
  1646. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1647. #define JSON_HEDLEY_RESTRICT restrict
  1648. #elif \
  1649. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1650. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1651. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1652. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1653. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1654. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1655. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1656. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1657. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1658. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1659. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1660. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1661. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1662. defined(__clang__) || \
  1663. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1664. #define JSON_HEDLEY_RESTRICT __restrict
  1665. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1666. #define JSON_HEDLEY_RESTRICT _Restrict
  1667. #else
  1668. #define JSON_HEDLEY_RESTRICT
  1669. #endif
  1670. #if defined(JSON_HEDLEY_INLINE)
  1671. #undef JSON_HEDLEY_INLINE
  1672. #endif
  1673. #if \
  1674. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1675. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1676. #define JSON_HEDLEY_INLINE inline
  1677. #elif \
  1678. defined(JSON_HEDLEY_GCC_VERSION) || \
  1679. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1680. #define JSON_HEDLEY_INLINE __inline__
  1681. #elif \
  1682. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1683. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1684. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1685. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1686. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1687. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1688. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1689. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1690. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1691. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1692. #define JSON_HEDLEY_INLINE __inline
  1693. #else
  1694. #define JSON_HEDLEY_INLINE
  1695. #endif
  1696. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1697. #undef JSON_HEDLEY_ALWAYS_INLINE
  1698. #endif
  1699. #if \
  1700. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1701. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1702. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1703. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1704. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1705. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1706. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1707. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1708. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1709. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1710. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1711. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1712. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1713. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1714. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1715. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1716. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1717. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1718. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1719. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1720. #elif \
  1721. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1722. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1723. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1724. #elif defined(__cplusplus) && \
  1725. ( \
  1726. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1727. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1728. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1729. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1730. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1731. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1732. )
  1733. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1734. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1735. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1736. #else
  1737. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1738. #endif
  1739. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1740. #undef JSON_HEDLEY_NEVER_INLINE
  1741. #endif
  1742. #if \
  1743. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1744. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1745. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1746. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1747. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1748. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1749. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1750. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1751. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1752. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1753. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1754. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1755. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1756. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1757. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1758. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1759. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1760. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1761. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1762. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1763. #elif \
  1764. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1765. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1766. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1767. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1768. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1769. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1770. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1771. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1772. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1773. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1774. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1775. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1776. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1777. #else
  1778. #define JSON_HEDLEY_NEVER_INLINE
  1779. #endif
  1780. #if defined(JSON_HEDLEY_PRIVATE)
  1781. #undef JSON_HEDLEY_PRIVATE
  1782. #endif
  1783. #if defined(JSON_HEDLEY_PUBLIC)
  1784. #undef JSON_HEDLEY_PUBLIC
  1785. #endif
  1786. #if defined(JSON_HEDLEY_IMPORT)
  1787. #undef JSON_HEDLEY_IMPORT
  1788. #endif
  1789. #if defined(_WIN32) || defined(__CYGWIN__)
  1790. # define JSON_HEDLEY_PRIVATE
  1791. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1792. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1793. #else
  1794. # if \
  1795. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1796. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1797. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1798. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1799. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1800. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1801. ( \
  1802. defined(__TI_EABI__) && \
  1803. ( \
  1804. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1805. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1806. ) \
  1807. ) || \
  1808. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1809. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1810. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1811. # else
  1812. # define JSON_HEDLEY_PRIVATE
  1813. # define JSON_HEDLEY_PUBLIC
  1814. # endif
  1815. # define JSON_HEDLEY_IMPORT extern
  1816. #endif
  1817. #if defined(JSON_HEDLEY_NO_THROW)
  1818. #undef JSON_HEDLEY_NO_THROW
  1819. #endif
  1820. #if \
  1821. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1822. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1823. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1824. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1825. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1826. #elif \
  1827. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1828. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1829. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1830. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1831. #else
  1832. #define JSON_HEDLEY_NO_THROW
  1833. #endif
  1834. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1835. #undef JSON_HEDLEY_FALL_THROUGH
  1836. #endif
  1837. #if \
  1838. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1839. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1840. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1841. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1842. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1843. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1844. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1845. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1846. #elif defined(__fallthrough) /* SAL */
  1847. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1848. #else
  1849. #define JSON_HEDLEY_FALL_THROUGH
  1850. #endif
  1851. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1852. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1853. #endif
  1854. #if \
  1855. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1856. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1857. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1858. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1859. #elif defined(_Ret_notnull_) /* SAL */
  1860. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1861. #else
  1862. #define JSON_HEDLEY_RETURNS_NON_NULL
  1863. #endif
  1864. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1865. #undef JSON_HEDLEY_ARRAY_PARAM
  1866. #endif
  1867. #if \
  1868. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1869. !defined(__STDC_NO_VLA__) && \
  1870. !defined(__cplusplus) && \
  1871. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1872. !defined(JSON_HEDLEY_TINYC_VERSION)
  1873. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1874. #else
  1875. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1876. #endif
  1877. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1878. #undef JSON_HEDLEY_IS_CONSTANT
  1879. #endif
  1880. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1881. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1882. #endif
  1883. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1884. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1885. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1886. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1887. #endif
  1888. #if \
  1889. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1890. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1891. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1892. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1893. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1894. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1895. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1896. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1897. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1898. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1899. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1900. #endif
  1901. #if !defined(__cplusplus)
  1902. # if \
  1903. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1904. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1905. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1906. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1907. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1908. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1909. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1910. #if defined(__INTPTR_TYPE__)
  1911. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1912. #else
  1913. #include <stdint.h>
  1914. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1915. #endif
  1916. # elif \
  1917. ( \
  1918. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1919. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1920. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1921. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1922. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1923. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1924. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1925. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1926. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1927. #if defined(__INTPTR_TYPE__)
  1928. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1929. #else
  1930. #include <stdint.h>
  1931. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1932. #endif
  1933. # elif \
  1934. defined(JSON_HEDLEY_GCC_VERSION) || \
  1935. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1936. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1937. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1938. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1939. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1940. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1941. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1942. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1943. defined(__clang__)
  1944. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1945. sizeof(void) != \
  1946. sizeof(*( \
  1947. 1 ? \
  1948. ((void*) ((expr) * 0L) ) : \
  1949. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1950. ) \
  1951. ) \
  1952. )
  1953. # endif
  1954. #endif
  1955. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1956. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1957. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1958. #endif
  1959. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1960. #else
  1961. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1962. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1963. #endif
  1964. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1965. #endif
  1966. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1967. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1968. #endif
  1969. #if defined(JSON_HEDLEY_END_C_DECLS)
  1970. #undef JSON_HEDLEY_END_C_DECLS
  1971. #endif
  1972. #if defined(JSON_HEDLEY_C_DECL)
  1973. #undef JSON_HEDLEY_C_DECL
  1974. #endif
  1975. #if defined(__cplusplus)
  1976. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1977. #define JSON_HEDLEY_END_C_DECLS }
  1978. #define JSON_HEDLEY_C_DECL extern "C"
  1979. #else
  1980. #define JSON_HEDLEY_BEGIN_C_DECLS
  1981. #define JSON_HEDLEY_END_C_DECLS
  1982. #define JSON_HEDLEY_C_DECL
  1983. #endif
  1984. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1985. #undef JSON_HEDLEY_STATIC_ASSERT
  1986. #endif
  1987. #if \
  1988. !defined(__cplusplus) && ( \
  1989. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  1990. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1991. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  1992. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1993. defined(_Static_assert) \
  1994. )
  1995. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  1996. #elif \
  1997. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  1998. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  1999. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2000. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  2001. #else
  2002. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  2003. #endif
  2004. #if defined(JSON_HEDLEY_NULL)
  2005. #undef JSON_HEDLEY_NULL
  2006. #endif
  2007. #if defined(__cplusplus)
  2008. #if __cplusplus >= 201103L
  2009. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  2010. #elif defined(NULL)
  2011. #define JSON_HEDLEY_NULL NULL
  2012. #else
  2013. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  2014. #endif
  2015. #elif defined(NULL)
  2016. #define JSON_HEDLEY_NULL NULL
  2017. #else
  2018. #define JSON_HEDLEY_NULL ((void*) 0)
  2019. #endif
  2020. #if defined(JSON_HEDLEY_MESSAGE)
  2021. #undef JSON_HEDLEY_MESSAGE
  2022. #endif
  2023. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2024. # define JSON_HEDLEY_MESSAGE(msg) \
  2025. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2026. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2027. JSON_HEDLEY_PRAGMA(message msg) \
  2028. JSON_HEDLEY_DIAGNOSTIC_POP
  2029. #elif \
  2030. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  2031. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2032. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  2033. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  2034. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  2035. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  2036. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2037. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  2038. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2039. #else
  2040. # define JSON_HEDLEY_MESSAGE(msg)
  2041. #endif
  2042. #if defined(JSON_HEDLEY_WARNING)
  2043. #undef JSON_HEDLEY_WARNING
  2044. #endif
  2045. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2046. # define JSON_HEDLEY_WARNING(msg) \
  2047. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2048. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2049. JSON_HEDLEY_PRAGMA(clang warning msg) \
  2050. JSON_HEDLEY_DIAGNOSTIC_POP
  2051. #elif \
  2052. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  2053. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  2054. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2055. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  2056. #elif \
  2057. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  2058. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2059. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2060. #else
  2061. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  2062. #endif
  2063. #if defined(JSON_HEDLEY_REQUIRE)
  2064. #undef JSON_HEDLEY_REQUIRE
  2065. #endif
  2066. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  2067. #undef JSON_HEDLEY_REQUIRE_MSG
  2068. #endif
  2069. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  2070. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  2071. # define JSON_HEDLEY_REQUIRE(expr) \
  2072. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2073. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2074. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  2075. JSON_HEDLEY_DIAGNOSTIC_POP
  2076. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  2077. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2078. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2079. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  2080. JSON_HEDLEY_DIAGNOSTIC_POP
  2081. # else
  2082. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  2083. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  2084. # endif
  2085. #else
  2086. # define JSON_HEDLEY_REQUIRE(expr)
  2087. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  2088. #endif
  2089. #if defined(JSON_HEDLEY_FLAGS)
  2090. #undef JSON_HEDLEY_FLAGS
  2091. #endif
  2092. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  2093. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  2094. #else
  2095. #define JSON_HEDLEY_FLAGS
  2096. #endif
  2097. #if defined(JSON_HEDLEY_FLAGS_CAST)
  2098. #undef JSON_HEDLEY_FLAGS_CAST
  2099. #endif
  2100. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  2101. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  2102. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2103. _Pragma("warning(disable:188)") \
  2104. ((T) (expr)); \
  2105. JSON_HEDLEY_DIAGNOSTIC_POP \
  2106. }))
  2107. #else
  2108. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  2109. #endif
  2110. #if defined(JSON_HEDLEY_EMPTY_BASES)
  2111. #undef JSON_HEDLEY_EMPTY_BASES
  2112. #endif
  2113. #if \
  2114. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2115. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2116. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2117. #else
  2118. #define JSON_HEDLEY_EMPTY_BASES
  2119. #endif
  2120. /* Remaining macros are deprecated. */
  2121. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2122. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2123. #endif
  2124. #if defined(__clang__)
  2125. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2126. #else
  2127. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2128. #endif
  2129. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2130. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2131. #endif
  2132. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2133. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2134. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2135. #endif
  2136. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2137. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2138. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2139. #endif
  2140. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2141. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2142. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2143. #endif
  2144. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2145. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2146. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2147. #endif
  2148. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2149. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2150. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2151. #endif
  2152. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2153. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2154. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2155. #endif
  2156. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2157. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2158. // This file contains all internal macro definitions (except those affecting ABI)
  2159. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2160. // #include <nlohmann/detail/abi_macros.hpp>
  2161. // exclude unsupported compilers
  2162. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2163. #if defined(__clang__)
  2164. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2165. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2166. #endif
  2167. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2168. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2169. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2170. #endif
  2171. #endif
  2172. #endif
  2173. // C++ language standard detection
  2174. // if the user manually specified the used c++ version this is skipped
  2175. #if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2176. #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
  2177. #define JSON_HAS_CPP_20
  2178. #define JSON_HAS_CPP_17
  2179. #define JSON_HAS_CPP_14
  2180. #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2181. #define JSON_HAS_CPP_17
  2182. #define JSON_HAS_CPP_14
  2183. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2184. #define JSON_HAS_CPP_14
  2185. #endif
  2186. // the cpp 11 flag is always specified because it is the minimal required version
  2187. #define JSON_HAS_CPP_11
  2188. #endif
  2189. #ifdef __has_include
  2190. #if __has_include(<version>)
  2191. #include <version>
  2192. #endif
  2193. #endif
  2194. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2195. #ifdef JSON_HAS_CPP_17
  2196. #if defined(__cpp_lib_filesystem)
  2197. #define JSON_HAS_FILESYSTEM 1
  2198. #elif defined(__cpp_lib_experimental_filesystem)
  2199. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2200. #elif !defined(__has_include)
  2201. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2202. #elif __has_include(<filesystem>)
  2203. #define JSON_HAS_FILESYSTEM 1
  2204. #elif __has_include(<experimental/filesystem>)
  2205. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2206. #endif
  2207. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2208. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2209. #undef JSON_HAS_FILESYSTEM
  2210. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2211. #endif
  2212. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2213. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2214. #undef JSON_HAS_FILESYSTEM
  2215. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2216. #endif
  2217. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2218. #if defined(__clang_major__) && __clang_major__ < 7
  2219. #undef JSON_HAS_FILESYSTEM
  2220. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2221. #endif
  2222. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2223. #if defined(_MSC_VER) && _MSC_VER < 1914
  2224. #undef JSON_HAS_FILESYSTEM
  2225. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2226. #endif
  2227. // no filesystem support before iOS 13
  2228. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2229. #undef JSON_HAS_FILESYSTEM
  2230. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2231. #endif
  2232. // no filesystem support before macOS Catalina
  2233. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2234. #undef JSON_HAS_FILESYSTEM
  2235. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2236. #endif
  2237. #endif
  2238. #endif
  2239. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2240. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2241. #endif
  2242. #ifndef JSON_HAS_FILESYSTEM
  2243. #define JSON_HAS_FILESYSTEM 0
  2244. #endif
  2245. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2246. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2247. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2248. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2249. #else
  2250. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2251. #endif
  2252. #endif
  2253. #ifndef JSON_HAS_RANGES
  2254. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
  2255. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2256. #define JSON_HAS_RANGES 0
  2257. #elif defined(__cpp_lib_ranges)
  2258. #define JSON_HAS_RANGES 1
  2259. #else
  2260. #define JSON_HAS_RANGES 0
  2261. #endif
  2262. #endif
  2263. #ifndef JSON_HAS_STATIC_RTTI
  2264. #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
  2265. #define JSON_HAS_STATIC_RTTI 1
  2266. #else
  2267. #define JSON_HAS_STATIC_RTTI 0
  2268. #endif
  2269. #endif
  2270. #ifdef JSON_HAS_CPP_17
  2271. #define JSON_INLINE_VARIABLE inline
  2272. #else
  2273. #define JSON_INLINE_VARIABLE
  2274. #endif
  2275. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2276. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2277. #else
  2278. #define JSON_NO_UNIQUE_ADDRESS
  2279. #endif
  2280. // disable documentation warnings on clang
  2281. #if defined(__clang__)
  2282. #pragma clang diagnostic push
  2283. #pragma clang diagnostic ignored "-Wdocumentation"
  2284. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2285. #endif
  2286. // allow disabling exceptions
  2287. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2288. #define JSON_THROW(exception) throw exception
  2289. #define JSON_TRY try
  2290. #define JSON_CATCH(exception) catch(exception)
  2291. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2292. #else
  2293. #include <cstdlib>
  2294. #define JSON_THROW(exception) std::abort()
  2295. #define JSON_TRY if(true)
  2296. #define JSON_CATCH(exception) if(false)
  2297. #define JSON_INTERNAL_CATCH(exception) if(false)
  2298. #endif
  2299. // override exception macros
  2300. #if defined(JSON_THROW_USER)
  2301. #undef JSON_THROW
  2302. #define JSON_THROW JSON_THROW_USER
  2303. #endif
  2304. #if defined(JSON_TRY_USER)
  2305. #undef JSON_TRY
  2306. #define JSON_TRY JSON_TRY_USER
  2307. #endif
  2308. #if defined(JSON_CATCH_USER)
  2309. #undef JSON_CATCH
  2310. #define JSON_CATCH JSON_CATCH_USER
  2311. #undef JSON_INTERNAL_CATCH
  2312. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2313. #endif
  2314. #if defined(JSON_INTERNAL_CATCH_USER)
  2315. #undef JSON_INTERNAL_CATCH
  2316. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2317. #endif
  2318. // allow overriding assert
  2319. #if !defined(JSON_ASSERT)
  2320. #include <cassert> // assert
  2321. #define JSON_ASSERT(x) assert(x)
  2322. #endif
  2323. // allow to access some private functions (needed by the test suite)
  2324. #if defined(JSON_TESTS_PRIVATE)
  2325. #define JSON_PRIVATE_UNLESS_TESTED public
  2326. #else
  2327. #define JSON_PRIVATE_UNLESS_TESTED private
  2328. #endif
  2329. /*!
  2330. @brief macro to briefly define a mapping between an enum and JSON
  2331. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2332. @since version 3.4.0
  2333. */
  2334. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2335. template<typename BasicJsonType> \
  2336. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2337. { \
  2338. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2339. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2340. auto it = std::find_if(std::begin(m), std::end(m), \
  2341. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2342. { \
  2343. return ej_pair.first == e; \
  2344. }); \
  2345. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2346. } \
  2347. template<typename BasicJsonType> \
  2348. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2349. { \
  2350. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2351. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2352. auto it = std::find_if(std::begin(m), std::end(m), \
  2353. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2354. { \
  2355. return ej_pair.second == j; \
  2356. }); \
  2357. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2358. }
  2359. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2360. // may be removed in the future once the class is split.
  2361. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2362. template<template<typename, typename, typename...> class ObjectType, \
  2363. template<typename, typename...> class ArrayType, \
  2364. class StringType, class BooleanType, class NumberIntegerType, \
  2365. class NumberUnsignedType, class NumberFloatType, \
  2366. template<typename> class AllocatorType, \
  2367. template<typename, typename = void> class JSONSerializer, \
  2368. class BinaryType, \
  2369. class CustomBaseClass>
  2370. #define NLOHMANN_BASIC_JSON_TPL \
  2371. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2372. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2373. AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
  2374. // Macros to simplify conversion from/to types
  2375. #define NLOHMANN_JSON_EXPAND( x ) x
  2376. #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
  2377. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2378. NLOHMANN_JSON_PASTE64, \
  2379. NLOHMANN_JSON_PASTE63, \
  2380. NLOHMANN_JSON_PASTE62, \
  2381. NLOHMANN_JSON_PASTE61, \
  2382. NLOHMANN_JSON_PASTE60, \
  2383. NLOHMANN_JSON_PASTE59, \
  2384. NLOHMANN_JSON_PASTE58, \
  2385. NLOHMANN_JSON_PASTE57, \
  2386. NLOHMANN_JSON_PASTE56, \
  2387. NLOHMANN_JSON_PASTE55, \
  2388. NLOHMANN_JSON_PASTE54, \
  2389. NLOHMANN_JSON_PASTE53, \
  2390. NLOHMANN_JSON_PASTE52, \
  2391. NLOHMANN_JSON_PASTE51, \
  2392. NLOHMANN_JSON_PASTE50, \
  2393. NLOHMANN_JSON_PASTE49, \
  2394. NLOHMANN_JSON_PASTE48, \
  2395. NLOHMANN_JSON_PASTE47, \
  2396. NLOHMANN_JSON_PASTE46, \
  2397. NLOHMANN_JSON_PASTE45, \
  2398. NLOHMANN_JSON_PASTE44, \
  2399. NLOHMANN_JSON_PASTE43, \
  2400. NLOHMANN_JSON_PASTE42, \
  2401. NLOHMANN_JSON_PASTE41, \
  2402. NLOHMANN_JSON_PASTE40, \
  2403. NLOHMANN_JSON_PASTE39, \
  2404. NLOHMANN_JSON_PASTE38, \
  2405. NLOHMANN_JSON_PASTE37, \
  2406. NLOHMANN_JSON_PASTE36, \
  2407. NLOHMANN_JSON_PASTE35, \
  2408. NLOHMANN_JSON_PASTE34, \
  2409. NLOHMANN_JSON_PASTE33, \
  2410. NLOHMANN_JSON_PASTE32, \
  2411. NLOHMANN_JSON_PASTE31, \
  2412. NLOHMANN_JSON_PASTE30, \
  2413. NLOHMANN_JSON_PASTE29, \
  2414. NLOHMANN_JSON_PASTE28, \
  2415. NLOHMANN_JSON_PASTE27, \
  2416. NLOHMANN_JSON_PASTE26, \
  2417. NLOHMANN_JSON_PASTE25, \
  2418. NLOHMANN_JSON_PASTE24, \
  2419. NLOHMANN_JSON_PASTE23, \
  2420. NLOHMANN_JSON_PASTE22, \
  2421. NLOHMANN_JSON_PASTE21, \
  2422. NLOHMANN_JSON_PASTE20, \
  2423. NLOHMANN_JSON_PASTE19, \
  2424. NLOHMANN_JSON_PASTE18, \
  2425. NLOHMANN_JSON_PASTE17, \
  2426. NLOHMANN_JSON_PASTE16, \
  2427. NLOHMANN_JSON_PASTE15, \
  2428. NLOHMANN_JSON_PASTE14, \
  2429. NLOHMANN_JSON_PASTE13, \
  2430. NLOHMANN_JSON_PASTE12, \
  2431. NLOHMANN_JSON_PASTE11, \
  2432. NLOHMANN_JSON_PASTE10, \
  2433. NLOHMANN_JSON_PASTE9, \
  2434. NLOHMANN_JSON_PASTE8, \
  2435. NLOHMANN_JSON_PASTE7, \
  2436. NLOHMANN_JSON_PASTE6, \
  2437. NLOHMANN_JSON_PASTE5, \
  2438. NLOHMANN_JSON_PASTE4, \
  2439. NLOHMANN_JSON_PASTE3, \
  2440. NLOHMANN_JSON_PASTE2, \
  2441. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2442. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2443. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2444. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2445. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2446. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2447. #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)
  2448. #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)
  2449. #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)
  2450. #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)
  2451. #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)
  2452. #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)
  2453. #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)
  2454. #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)
  2455. #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)
  2456. #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)
  2457. #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)
  2458. #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)
  2459. #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)
  2460. #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)
  2461. #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)
  2462. #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)
  2463. #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)
  2464. #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)
  2465. #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)
  2466. #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)
  2467. #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)
  2468. #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)
  2469. #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)
  2470. #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)
  2471. #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)
  2472. #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)
  2473. #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)
  2474. #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)
  2475. #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)
  2476. #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)
  2477. #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)
  2478. #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)
  2479. #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)
  2480. #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)
  2481. #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)
  2482. #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)
  2483. #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)
  2484. #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)
  2485. #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)
  2486. #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)
  2487. #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)
  2488. #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)
  2489. #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)
  2490. #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)
  2491. #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)
  2492. #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)
  2493. #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)
  2494. #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)
  2495. #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)
  2496. #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)
  2497. #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)
  2498. #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)
  2499. #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)
  2500. #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)
  2501. #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)
  2502. #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)
  2503. #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)
  2504. #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)
  2505. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2506. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2507. #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
  2508. /*!
  2509. @brief macro
  2510. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2511. @since version 3.9.0
  2512. */
  2513. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2514. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2515. friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2516. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2517. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2518. friend void from_json(const nlohmann::json& 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__)) }
  2519. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2520. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2521. /*!
  2522. @brief macro
  2523. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2524. @since version 3.9.0
  2525. */
  2526. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2527. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2528. inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2529. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2530. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2531. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2532. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2533. inline void from_json(const nlohmann::json& 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__)) }
  2534. // inspired from https://stackoverflow.com/a/26745591
  2535. // allows to call any std function as if (e.g. with begin):
  2536. // using std::begin; begin(x);
  2537. //
  2538. // it allows using the detected idiom to retrieve the return type
  2539. // of such an expression
  2540. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2541. namespace detail { \
  2542. using std::std_name; \
  2543. \
  2544. template<typename... T> \
  2545. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2546. } \
  2547. \
  2548. namespace detail2 { \
  2549. struct std_name##_tag \
  2550. { \
  2551. }; \
  2552. \
  2553. template<typename... T> \
  2554. std_name##_tag std_name(T&&...); \
  2555. \
  2556. template<typename... T> \
  2557. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2558. \
  2559. template<typename... T> \
  2560. struct would_call_std_##std_name \
  2561. { \
  2562. static constexpr auto const value = ::nlohmann::detail:: \
  2563. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2564. }; \
  2565. } /* namespace detail2 */ \
  2566. \
  2567. template<typename... T> \
  2568. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2569. { \
  2570. }
  2571. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2572. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2573. #endif
  2574. #if JSON_USE_IMPLICIT_CONVERSIONS
  2575. #define JSON_EXPLICIT
  2576. #else
  2577. #define JSON_EXPLICIT explicit
  2578. #endif
  2579. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2580. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2581. #endif
  2582. #ifndef JSON_USE_GLOBAL_UDLS
  2583. #define JSON_USE_GLOBAL_UDLS 1
  2584. #endif
  2585. #if JSON_HAS_THREE_WAY_COMPARISON
  2586. #include <compare> // partial_ordering
  2587. #endif
  2588. NLOHMANN_JSON_NAMESPACE_BEGIN
  2589. namespace detail
  2590. {
  2591. ///////////////////////////
  2592. // JSON type enumeration //
  2593. ///////////////////////////
  2594. /*!
  2595. @brief the JSON type enumeration
  2596. This enumeration collects the different JSON types. It is internally used to
  2597. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2598. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2599. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2600. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2601. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2602. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2603. @ref basic_json::is_structured() rely on it.
  2604. @note There are three enumeration entries (number_integer, number_unsigned, and
  2605. number_float), because the library distinguishes these three types for numbers:
  2606. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2607. @ref basic_json::number_integer_t is used for signed integers, and
  2608. @ref basic_json::number_float_t is used for floating-point numbers or to
  2609. approximate integers which do not fit in the limits of their respective type.
  2610. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2611. value with the default value for a given type
  2612. @since version 1.0.0
  2613. */
  2614. enum class value_t : std::uint8_t
  2615. {
  2616. null, ///< null value
  2617. object, ///< object (unordered set of name/value pairs)
  2618. array, ///< array (ordered collection of values)
  2619. string, ///< string value
  2620. boolean, ///< boolean value
  2621. number_integer, ///< number value (signed integer)
  2622. number_unsigned, ///< number value (unsigned integer)
  2623. number_float, ///< number value (floating-point)
  2624. binary, ///< binary array (ordered collection of bytes)
  2625. discarded ///< discarded by the parser callback function
  2626. };
  2627. /*!
  2628. @brief comparison operator for JSON types
  2629. Returns an ordering that is similar to Python:
  2630. - order: null < boolean < number < object < array < string < binary
  2631. - furthermore, each type is not smaller than itself
  2632. - discarded values are not comparable
  2633. - binary is represented as a b"" string in python and directly comparable to a
  2634. string; however, making a binary array directly comparable with a string would
  2635. be surprising behavior in a JSON file.
  2636. @since version 1.0.0
  2637. */
  2638. #if JSON_HAS_THREE_WAY_COMPARISON
  2639. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2640. #else
  2641. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2642. #endif
  2643. {
  2644. static constexpr std::array<std::uint8_t, 9> order = {{
  2645. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2646. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2647. 6 /* binary */
  2648. }
  2649. };
  2650. const auto l_index = static_cast<std::size_t>(lhs);
  2651. const auto r_index = static_cast<std::size_t>(rhs);
  2652. #if JSON_HAS_THREE_WAY_COMPARISON
  2653. if (l_index < order.size() && r_index < order.size())
  2654. {
  2655. return order[l_index] <=> order[r_index]; // *NOPAD*
  2656. }
  2657. return std::partial_ordering::unordered;
  2658. #else
  2659. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2660. #endif
  2661. }
  2662. // GCC selects the built-in operator< over an operator rewritten from
  2663. // a user-defined spaceship operator
  2664. // Clang, MSVC, and ICC select the rewritten candidate
  2665. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2666. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2667. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2668. {
  2669. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2670. }
  2671. #endif
  2672. } // namespace detail
  2673. NLOHMANN_JSON_NAMESPACE_END
  2674. // #include <nlohmann/detail/string_escape.hpp>
  2675. // __ _____ _____ _____
  2676. // __| | __| | | | JSON for Modern C++
  2677. // | | |__ | | | | | | version 3.11.3
  2678. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2679. //
  2680. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2681. // SPDX-License-Identifier: MIT
  2682. // #include <nlohmann/detail/abi_macros.hpp>
  2683. NLOHMANN_JSON_NAMESPACE_BEGIN
  2684. namespace detail
  2685. {
  2686. /*!
  2687. @brief replace all occurrences of a substring by another string
  2688. @param[in,out] s the string to manipulate; changed so that all
  2689. occurrences of @a f are replaced with @a t
  2690. @param[in] f the substring to replace with @a t
  2691. @param[in] t the string to replace @a f
  2692. @pre The search string @a f must not be empty. **This precondition is
  2693. enforced with an assertion.**
  2694. @since version 2.0.0
  2695. */
  2696. template<typename StringType>
  2697. inline void replace_substring(StringType& s, const StringType& f,
  2698. const StringType& t)
  2699. {
  2700. JSON_ASSERT(!f.empty());
  2701. for (auto pos = s.find(f); // find first occurrence of f
  2702. pos != StringType::npos; // make sure f was found
  2703. s.replace(pos, f.size(), t), // replace with t, and
  2704. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2705. {}
  2706. }
  2707. /*!
  2708. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2709. * @param[in] s string to escape
  2710. * @return escaped string
  2711. *
  2712. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2713. */
  2714. template<typename StringType>
  2715. inline StringType escape(StringType s)
  2716. {
  2717. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2718. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2719. return s;
  2720. }
  2721. /*!
  2722. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2723. * @param[in] s string to unescape
  2724. * @return unescaped string
  2725. *
  2726. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2727. */
  2728. template<typename StringType>
  2729. static void unescape(StringType& s)
  2730. {
  2731. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2732. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2733. }
  2734. } // namespace detail
  2735. NLOHMANN_JSON_NAMESPACE_END
  2736. // #include <nlohmann/detail/input/position_t.hpp>
  2737. // __ _____ _____ _____
  2738. // __| | __| | | | JSON for Modern C++
  2739. // | | |__ | | | | | | version 3.11.3
  2740. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2741. //
  2742. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2743. // SPDX-License-Identifier: MIT
  2744. #include <cstddef> // size_t
  2745. // #include <nlohmann/detail/abi_macros.hpp>
  2746. NLOHMANN_JSON_NAMESPACE_BEGIN
  2747. namespace detail
  2748. {
  2749. /// struct to capture the start position of the current token
  2750. struct position_t
  2751. {
  2752. /// the total number of characters read
  2753. std::size_t chars_read_total = 0;
  2754. /// the number of characters read in the current line
  2755. std::size_t chars_read_current_line = 0;
  2756. /// the number of lines read
  2757. std::size_t lines_read = 0;
  2758. /// conversion to size_t to preserve SAX interface
  2759. constexpr operator size_t() const
  2760. {
  2761. return chars_read_total;
  2762. }
  2763. };
  2764. } // namespace detail
  2765. NLOHMANN_JSON_NAMESPACE_END
  2766. // #include <nlohmann/detail/macro_scope.hpp>
  2767. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2768. // __ _____ _____ _____
  2769. // __| | __| | | | JSON for Modern C++
  2770. // | | |__ | | | | | | version 3.11.3
  2771. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2772. //
  2773. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2774. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2775. // SPDX-License-Identifier: MIT
  2776. #include <array> // array
  2777. #include <cstddef> // size_t
  2778. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2779. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2780. // #include <nlohmann/detail/macro_scope.hpp>
  2781. NLOHMANN_JSON_NAMESPACE_BEGIN
  2782. namespace detail
  2783. {
  2784. template<typename T>
  2785. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2786. #ifdef JSON_HAS_CPP_14
  2787. // the following utilities are natively available in C++14
  2788. using std::enable_if_t;
  2789. using std::index_sequence;
  2790. using std::make_index_sequence;
  2791. using std::index_sequence_for;
  2792. #else
  2793. // alias templates to reduce boilerplate
  2794. template<bool B, typename T = void>
  2795. using enable_if_t = typename std::enable_if<B, T>::type;
  2796. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2797. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2798. //// START OF CODE FROM GOOGLE ABSEIL
  2799. // integer_sequence
  2800. //
  2801. // Class template representing a compile-time integer sequence. An instantiation
  2802. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2803. // type through its template arguments (which is a common need when
  2804. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2805. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2806. //
  2807. // Example:
  2808. //
  2809. // template< class T, T... Ints >
  2810. // void user_function(integer_sequence<T, Ints...>);
  2811. //
  2812. // int main()
  2813. // {
  2814. // // user_function's `T` will be deduced to `int` and `Ints...`
  2815. // // will be deduced to `0, 1, 2, 3, 4`.
  2816. // user_function(make_integer_sequence<int, 5>());
  2817. // }
  2818. template <typename T, T... Ints>
  2819. struct integer_sequence
  2820. {
  2821. using value_type = T;
  2822. static constexpr std::size_t size() noexcept
  2823. {
  2824. return sizeof...(Ints);
  2825. }
  2826. };
  2827. // index_sequence
  2828. //
  2829. // A helper template for an `integer_sequence` of `size_t`,
  2830. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2831. // `std::index_sequence`.
  2832. template <size_t... Ints>
  2833. using index_sequence = integer_sequence<size_t, Ints...>;
  2834. namespace utility_internal
  2835. {
  2836. template <typename Seq, size_t SeqSize, size_t Rem>
  2837. struct Extend;
  2838. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2839. template <typename T, T... Ints, size_t SeqSize>
  2840. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2841. {
  2842. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2843. };
  2844. template <typename T, T... Ints, size_t SeqSize>
  2845. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2846. {
  2847. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2848. };
  2849. // Recursion helper for 'make_integer_sequence<T, N>'.
  2850. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2851. template <typename T, size_t N>
  2852. struct Gen
  2853. {
  2854. using type =
  2855. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2856. };
  2857. template <typename T>
  2858. struct Gen<T, 0>
  2859. {
  2860. using type = integer_sequence<T>;
  2861. };
  2862. } // namespace utility_internal
  2863. // Compile-time sequences of integers
  2864. // make_integer_sequence
  2865. //
  2866. // This template alias is equivalent to
  2867. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2868. // replacement for C++14's `std::make_integer_sequence`.
  2869. template <typename T, T N>
  2870. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2871. // make_index_sequence
  2872. //
  2873. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2874. // and is designed to be a drop-in replacement for C++14's
  2875. // `std::make_index_sequence`.
  2876. template <size_t N>
  2877. using make_index_sequence = make_integer_sequence<size_t, N>;
  2878. // index_sequence_for
  2879. //
  2880. // Converts a typename pack into an index sequence of the same length, and
  2881. // is designed to be a drop-in replacement for C++14's
  2882. // `std::index_sequence_for()`
  2883. template <typename... Ts>
  2884. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2885. //// END OF CODE FROM GOOGLE ABSEIL
  2886. #endif
  2887. // dispatch utility (taken from ranges-v3)
  2888. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2889. template<> struct priority_tag<0> {};
  2890. // taken from ranges-v3
  2891. template<typename T>
  2892. struct static_const
  2893. {
  2894. static JSON_INLINE_VARIABLE constexpr T value{};
  2895. };
  2896. #ifndef JSON_HAS_CPP_17
  2897. template<typename T>
  2898. constexpr T static_const<T>::value;
  2899. #endif
  2900. template<typename T, typename... Args>
  2901. inline constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  2902. {
  2903. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  2904. }
  2905. } // namespace detail
  2906. NLOHMANN_JSON_NAMESPACE_END
  2907. // #include <nlohmann/detail/meta/type_traits.hpp>
  2908. // __ _____ _____ _____
  2909. // __| | __| | | | JSON for Modern C++
  2910. // | | |__ | | | | | | version 3.11.3
  2911. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2912. //
  2913. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2914. // SPDX-License-Identifier: MIT
  2915. #include <limits> // numeric_limits
  2916. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2917. #include <utility> // declval
  2918. #include <tuple> // tuple
  2919. #include <string> // char_traits
  2920. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2921. // __ _____ _____ _____
  2922. // __| | __| | | | JSON for Modern C++
  2923. // | | |__ | | | | | | version 3.11.3
  2924. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2925. //
  2926. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2927. // SPDX-License-Identifier: MIT
  2928. #include <iterator> // random_access_iterator_tag
  2929. // #include <nlohmann/detail/abi_macros.hpp>
  2930. // #include <nlohmann/detail/meta/void_t.hpp>
  2931. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2932. NLOHMANN_JSON_NAMESPACE_BEGIN
  2933. namespace detail
  2934. {
  2935. template<typename It, typename = void>
  2936. struct iterator_types {};
  2937. template<typename It>
  2938. struct iterator_types <
  2939. It,
  2940. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2941. typename It::reference, typename It::iterator_category >>
  2942. {
  2943. using difference_type = typename It::difference_type;
  2944. using value_type = typename It::value_type;
  2945. using pointer = typename It::pointer;
  2946. using reference = typename It::reference;
  2947. using iterator_category = typename It::iterator_category;
  2948. };
  2949. // This is required as some compilers implement std::iterator_traits in a way that
  2950. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2951. template<typename T, typename = void>
  2952. struct iterator_traits
  2953. {
  2954. };
  2955. template<typename T>
  2956. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2957. : iterator_types<T>
  2958. {
  2959. };
  2960. template<typename T>
  2961. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2962. {
  2963. using iterator_category = std::random_access_iterator_tag;
  2964. using value_type = T;
  2965. using difference_type = ptrdiff_t;
  2966. using pointer = T*;
  2967. using reference = T&;
  2968. };
  2969. } // namespace detail
  2970. NLOHMANN_JSON_NAMESPACE_END
  2971. // #include <nlohmann/detail/macro_scope.hpp>
  2972. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  2973. // __ _____ _____ _____
  2974. // __| | __| | | | JSON for Modern C++
  2975. // | | |__ | | | | | | version 3.11.3
  2976. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2977. //
  2978. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2979. // SPDX-License-Identifier: MIT
  2980. // #include <nlohmann/detail/macro_scope.hpp>
  2981. NLOHMANN_JSON_NAMESPACE_BEGIN
  2982. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  2983. NLOHMANN_JSON_NAMESPACE_END
  2984. // #include <nlohmann/detail/meta/call_std/end.hpp>
  2985. // __ _____ _____ _____
  2986. // __| | __| | | | JSON for Modern C++
  2987. // | | |__ | | | | | | version 3.11.3
  2988. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2989. //
  2990. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2991. // SPDX-License-Identifier: MIT
  2992. // #include <nlohmann/detail/macro_scope.hpp>
  2993. NLOHMANN_JSON_NAMESPACE_BEGIN
  2994. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  2995. NLOHMANN_JSON_NAMESPACE_END
  2996. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2997. // #include <nlohmann/detail/meta/detected.hpp>
  2998. // #include <nlohmann/json_fwd.hpp>
  2999. // __ _____ _____ _____
  3000. // __| | __| | | | JSON for Modern C++
  3001. // | | |__ | | | | | | version 3.11.3
  3002. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3003. //
  3004. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  3005. // SPDX-License-Identifier: MIT
  3006. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3007. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3008. #include <cstdint> // int64_t, uint64_t
  3009. #include <map> // map
  3010. #include <memory> // allocator
  3011. #include <string> // string
  3012. #include <vector> // vector
  3013. // #include <nlohmann/detail/abi_macros.hpp>
  3014. /*!
  3015. @brief namespace for Niels Lohmann
  3016. @see https://github.com/nlohmann
  3017. @since version 1.0.0
  3018. */
  3019. NLOHMANN_JSON_NAMESPACE_BEGIN
  3020. /*!
  3021. @brief default JSONSerializer template argument
  3022. This serializer ignores the template arguments and uses ADL
  3023. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3024. for serialization.
  3025. */
  3026. template<typename T = void, typename SFINAE = void>
  3027. struct adl_serializer;
  3028. /// a class to store JSON values
  3029. /// @sa https://json.nlohmann.me/api/basic_json/
  3030. template<template<typename U, typename V, typename... Args> class ObjectType =
  3031. std::map,
  3032. template<typename U, typename... Args> class ArrayType = std::vector,
  3033. class StringType = std::string, class BooleanType = bool,
  3034. class NumberIntegerType = std::int64_t,
  3035. class NumberUnsignedType = std::uint64_t,
  3036. class NumberFloatType = double,
  3037. template<typename U> class AllocatorType = std::allocator,
  3038. template<typename T, typename SFINAE = void> class JSONSerializer =
  3039. adl_serializer,
  3040. class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
  3041. class CustomBaseClass = void>
  3042. class basic_json;
  3043. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3044. /// @sa https://json.nlohmann.me/api/json_pointer/
  3045. template<typename RefStringType>
  3046. class json_pointer;
  3047. /*!
  3048. @brief default specialization
  3049. @sa https://json.nlohmann.me/api/json/
  3050. */
  3051. using json = basic_json<>;
  3052. /// @brief a minimal map-like container that preserves insertion order
  3053. /// @sa https://json.nlohmann.me/api/ordered_map/
  3054. template<class Key, class T, class IgnoredLess, class Allocator>
  3055. struct ordered_map;
  3056. /// @brief specialization that maintains the insertion order of object keys
  3057. /// @sa https://json.nlohmann.me/api/ordered_json/
  3058. using ordered_json = basic_json<nlohmann::ordered_map>;
  3059. NLOHMANN_JSON_NAMESPACE_END
  3060. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3061. NLOHMANN_JSON_NAMESPACE_BEGIN
  3062. /*!
  3063. @brief detail namespace with internal helper functions
  3064. This namespace collects functions that should not be exposed,
  3065. implementations of some @ref basic_json methods, and meta-programming helpers.
  3066. @since version 2.1.0
  3067. */
  3068. namespace detail
  3069. {
  3070. /////////////
  3071. // helpers //
  3072. /////////////
  3073. // Note to maintainers:
  3074. //
  3075. // Every trait in this file expects a non CV-qualified type.
  3076. // The only exceptions are in the 'aliases for detected' section
  3077. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3078. //
  3079. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3080. // (e.g. to_json(BasicJsonType&, const T&))
  3081. template<typename> struct is_basic_json : std::false_type {};
  3082. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3083. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3084. // used by exceptions create() member functions
  3085. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  3086. // false_type otherwise
  3087. template<typename BasicJsonContext>
  3088. struct is_basic_json_context :
  3089. std::integral_constant < bool,
  3090. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3091. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3092. {};
  3093. //////////////////////
  3094. // json_ref helpers //
  3095. //////////////////////
  3096. template<typename>
  3097. class json_ref;
  3098. template<typename>
  3099. struct is_json_ref : std::false_type {};
  3100. template<typename T>
  3101. struct is_json_ref<json_ref<T>> : std::true_type {};
  3102. //////////////////////////
  3103. // aliases for detected //
  3104. //////////////////////////
  3105. template<typename T>
  3106. using mapped_type_t = typename T::mapped_type;
  3107. template<typename T>
  3108. using key_type_t = typename T::key_type;
  3109. template<typename T>
  3110. using value_type_t = typename T::value_type;
  3111. template<typename T>
  3112. using difference_type_t = typename T::difference_type;
  3113. template<typename T>
  3114. using pointer_t = typename T::pointer;
  3115. template<typename T>
  3116. using reference_t = typename T::reference;
  3117. template<typename T>
  3118. using iterator_category_t = typename T::iterator_category;
  3119. template<typename T, typename... Args>
  3120. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3121. template<typename T, typename... Args>
  3122. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3123. template<typename T, typename U>
  3124. using get_template_function = decltype(std::declval<T>().template get<U>());
  3125. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3126. template<typename BasicJsonType, typename T, typename = void>
  3127. struct has_from_json : std::false_type {};
  3128. // trait checking if j.get<T> is valid
  3129. // use this trait instead of std::is_constructible or std::is_convertible,
  3130. // both rely on, or make use of implicit conversions, and thus fail when T
  3131. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3132. template <typename BasicJsonType, typename T>
  3133. struct is_getable
  3134. {
  3135. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3136. };
  3137. template<typename BasicJsonType, typename T>
  3138. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3139. {
  3140. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3141. static constexpr bool value =
  3142. is_detected_exact<void, from_json_function, serializer,
  3143. const BasicJsonType&, T&>::value;
  3144. };
  3145. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3146. // this overload is used for non-default-constructible user-defined-types
  3147. template<typename BasicJsonType, typename T, typename = void>
  3148. struct has_non_default_from_json : std::false_type {};
  3149. template<typename BasicJsonType, typename T>
  3150. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3151. {
  3152. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3153. static constexpr bool value =
  3154. is_detected_exact<T, from_json_function, serializer,
  3155. const BasicJsonType&>::value;
  3156. };
  3157. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3158. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3159. template<typename BasicJsonType, typename T, typename = void>
  3160. struct has_to_json : std::false_type {};
  3161. template<typename BasicJsonType, typename T>
  3162. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3163. {
  3164. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3165. static constexpr bool value =
  3166. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3167. T>::value;
  3168. };
  3169. template<typename T>
  3170. using detect_key_compare = typename T::key_compare;
  3171. template<typename T>
  3172. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3173. // obtains the actual object key comparator
  3174. template<typename BasicJsonType>
  3175. struct actual_object_comparator
  3176. {
  3177. using object_t = typename BasicJsonType::object_t;
  3178. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3179. using type = typename std::conditional < has_key_compare<object_t>::value,
  3180. typename object_t::key_compare, object_comparator_t>::type;
  3181. };
  3182. template<typename BasicJsonType>
  3183. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3184. /////////////////
  3185. // char_traits //
  3186. /////////////////
  3187. // Primary template of char_traits calls std char_traits
  3188. template<typename T>
  3189. struct char_traits : std::char_traits<T>
  3190. {};
  3191. // Explicitly define char traits for unsigned char since it is not standard
  3192. template<>
  3193. struct char_traits<unsigned char> : std::char_traits<char>
  3194. {
  3195. using char_type = unsigned char;
  3196. using int_type = uint64_t;
  3197. // Redefine to_int_type function
  3198. static int_type to_int_type(char_type c) noexcept
  3199. {
  3200. return static_cast<int_type>(c);
  3201. }
  3202. static char_type to_char_type(int_type i) noexcept
  3203. {
  3204. return static_cast<char_type>(i);
  3205. }
  3206. static constexpr int_type eof() noexcept
  3207. {
  3208. return static_cast<int_type>(EOF);
  3209. }
  3210. };
  3211. // Explicitly define char traits for signed char since it is not standard
  3212. template<>
  3213. struct char_traits<signed char> : std::char_traits<char>
  3214. {
  3215. using char_type = signed char;
  3216. using int_type = uint64_t;
  3217. // Redefine to_int_type function
  3218. static int_type to_int_type(char_type c) noexcept
  3219. {
  3220. return static_cast<int_type>(c);
  3221. }
  3222. static char_type to_char_type(int_type i) noexcept
  3223. {
  3224. return static_cast<char_type>(i);
  3225. }
  3226. static constexpr int_type eof() noexcept
  3227. {
  3228. return static_cast<int_type>(EOF);
  3229. }
  3230. };
  3231. ///////////////////
  3232. // is_ functions //
  3233. ///////////////////
  3234. // https://en.cppreference.com/w/cpp/types/conjunction
  3235. template<class...> struct conjunction : std::true_type { };
  3236. template<class B> struct conjunction<B> : B { };
  3237. template<class B, class... Bn>
  3238. struct conjunction<B, Bn...>
  3239. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3240. // https://en.cppreference.com/w/cpp/types/negation
  3241. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3242. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3243. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3244. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3245. template <typename T>
  3246. struct is_default_constructible : std::is_default_constructible<T> {};
  3247. template <typename T1, typename T2>
  3248. struct is_default_constructible<std::pair<T1, T2>>
  3249. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3250. template <typename T1, typename T2>
  3251. struct is_default_constructible<const std::pair<T1, T2>>
  3252. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3253. template <typename... Ts>
  3254. struct is_default_constructible<std::tuple<Ts...>>
  3255. : conjunction<is_default_constructible<Ts>...> {};
  3256. template <typename... Ts>
  3257. struct is_default_constructible<const std::tuple<Ts...>>
  3258. : conjunction<is_default_constructible<Ts>...> {};
  3259. template <typename T, typename... Args>
  3260. struct is_constructible : std::is_constructible<T, Args...> {};
  3261. template <typename T1, typename T2>
  3262. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3263. template <typename T1, typename T2>
  3264. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3265. template <typename... Ts>
  3266. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3267. template <typename... Ts>
  3268. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3269. template<typename T, typename = void>
  3270. struct is_iterator_traits : std::false_type {};
  3271. template<typename T>
  3272. struct is_iterator_traits<iterator_traits<T>>
  3273. {
  3274. private:
  3275. using traits = iterator_traits<T>;
  3276. public:
  3277. static constexpr auto value =
  3278. is_detected<value_type_t, traits>::value &&
  3279. is_detected<difference_type_t, traits>::value &&
  3280. is_detected<pointer_t, traits>::value &&
  3281. is_detected<iterator_category_t, traits>::value &&
  3282. is_detected<reference_t, traits>::value;
  3283. };
  3284. template<typename T>
  3285. struct is_range
  3286. {
  3287. private:
  3288. using t_ref = typename std::add_lvalue_reference<T>::type;
  3289. using iterator = detected_t<result_of_begin, t_ref>;
  3290. using sentinel = detected_t<result_of_end, t_ref>;
  3291. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3292. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3293. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3294. static constexpr auto is_iterator_begin =
  3295. is_iterator_traits<iterator_traits<iterator>>::value;
  3296. public:
  3297. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3298. };
  3299. template<typename R>
  3300. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3301. template<typename T>
  3302. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3303. // The following implementation of is_complete_type is taken from
  3304. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3305. // and is written by Xiang Fan who agreed to using it in this library.
  3306. template<typename T, typename = void>
  3307. struct is_complete_type : std::false_type {};
  3308. template<typename T>
  3309. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3310. template<typename BasicJsonType, typename CompatibleObjectType,
  3311. typename = void>
  3312. struct is_compatible_object_type_impl : std::false_type {};
  3313. template<typename BasicJsonType, typename CompatibleObjectType>
  3314. struct is_compatible_object_type_impl <
  3315. BasicJsonType, CompatibleObjectType,
  3316. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3317. is_detected<key_type_t, CompatibleObjectType>::value >>
  3318. {
  3319. using object_t = typename BasicJsonType::object_t;
  3320. // macOS's is_constructible does not play well with nonesuch...
  3321. static constexpr bool value =
  3322. is_constructible<typename object_t::key_type,
  3323. typename CompatibleObjectType::key_type>::value &&
  3324. is_constructible<typename object_t::mapped_type,
  3325. typename CompatibleObjectType::mapped_type>::value;
  3326. };
  3327. template<typename BasicJsonType, typename CompatibleObjectType>
  3328. struct is_compatible_object_type
  3329. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3330. template<typename BasicJsonType, typename ConstructibleObjectType,
  3331. typename = void>
  3332. struct is_constructible_object_type_impl : std::false_type {};
  3333. template<typename BasicJsonType, typename ConstructibleObjectType>
  3334. struct is_constructible_object_type_impl <
  3335. BasicJsonType, ConstructibleObjectType,
  3336. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3337. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3338. {
  3339. using object_t = typename BasicJsonType::object_t;
  3340. static constexpr bool value =
  3341. (is_default_constructible<ConstructibleObjectType>::value &&
  3342. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3343. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3344. (is_constructible<typename ConstructibleObjectType::key_type,
  3345. typename object_t::key_type>::value &&
  3346. std::is_same <
  3347. typename object_t::mapped_type,
  3348. typename ConstructibleObjectType::mapped_type >::value)) ||
  3349. (has_from_json<BasicJsonType,
  3350. typename ConstructibleObjectType::mapped_type>::value ||
  3351. has_non_default_from_json <
  3352. BasicJsonType,
  3353. typename ConstructibleObjectType::mapped_type >::value);
  3354. };
  3355. template<typename BasicJsonType, typename ConstructibleObjectType>
  3356. struct is_constructible_object_type
  3357. : is_constructible_object_type_impl<BasicJsonType,
  3358. ConstructibleObjectType> {};
  3359. template<typename BasicJsonType, typename CompatibleStringType>
  3360. struct is_compatible_string_type
  3361. {
  3362. static constexpr auto value =
  3363. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3364. };
  3365. template<typename BasicJsonType, typename ConstructibleStringType>
  3366. struct is_constructible_string_type
  3367. {
  3368. // launder type through decltype() to fix compilation failure on ICPC
  3369. #ifdef __INTEL_COMPILER
  3370. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3371. #else
  3372. using laundered_type = ConstructibleStringType;
  3373. #endif
  3374. static constexpr auto value =
  3375. conjunction <
  3376. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3377. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3378. value_type_t, laundered_type >>::value;
  3379. };
  3380. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3381. struct is_compatible_array_type_impl : std::false_type {};
  3382. template<typename BasicJsonType, typename CompatibleArrayType>
  3383. struct is_compatible_array_type_impl <
  3384. BasicJsonType, CompatibleArrayType,
  3385. enable_if_t <
  3386. is_detected<iterator_t, CompatibleArrayType>::value&&
  3387. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3388. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3389. // c.f. https://github.com/nlohmann/json/pull/3073
  3390. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3391. {
  3392. static constexpr bool value =
  3393. is_constructible<BasicJsonType,
  3394. range_value_t<CompatibleArrayType>>::value;
  3395. };
  3396. template<typename BasicJsonType, typename CompatibleArrayType>
  3397. struct is_compatible_array_type
  3398. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3399. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3400. struct is_constructible_array_type_impl : std::false_type {};
  3401. template<typename BasicJsonType, typename ConstructibleArrayType>
  3402. struct is_constructible_array_type_impl <
  3403. BasicJsonType, ConstructibleArrayType,
  3404. enable_if_t<std::is_same<ConstructibleArrayType,
  3405. typename BasicJsonType::value_type>::value >>
  3406. : std::true_type {};
  3407. template<typename BasicJsonType, typename ConstructibleArrayType>
  3408. struct is_constructible_array_type_impl <
  3409. BasicJsonType, ConstructibleArrayType,
  3410. enable_if_t < !std::is_same<ConstructibleArrayType,
  3411. typename BasicJsonType::value_type>::value&&
  3412. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3413. is_default_constructible<ConstructibleArrayType>::value&&
  3414. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3415. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3416. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3417. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3418. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3419. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3420. // c.f. https://github.com/nlohmann/json/pull/3073
  3421. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3422. is_complete_type <
  3423. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3424. {
  3425. using value_type = range_value_t<ConstructibleArrayType>;
  3426. static constexpr bool value =
  3427. std::is_same<value_type,
  3428. typename BasicJsonType::array_t::value_type>::value ||
  3429. has_from_json<BasicJsonType,
  3430. value_type>::value ||
  3431. has_non_default_from_json <
  3432. BasicJsonType,
  3433. value_type >::value;
  3434. };
  3435. template<typename BasicJsonType, typename ConstructibleArrayType>
  3436. struct is_constructible_array_type
  3437. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3438. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3439. typename = void>
  3440. struct is_compatible_integer_type_impl : std::false_type {};
  3441. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3442. struct is_compatible_integer_type_impl <
  3443. RealIntegerType, CompatibleNumberIntegerType,
  3444. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3445. std::is_integral<CompatibleNumberIntegerType>::value&&
  3446. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3447. {
  3448. // is there an assert somewhere on overflows?
  3449. using RealLimits = std::numeric_limits<RealIntegerType>;
  3450. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3451. static constexpr auto value =
  3452. is_constructible<RealIntegerType,
  3453. CompatibleNumberIntegerType>::value &&
  3454. CompatibleLimits::is_integer &&
  3455. RealLimits::is_signed == CompatibleLimits::is_signed;
  3456. };
  3457. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3458. struct is_compatible_integer_type
  3459. : is_compatible_integer_type_impl<RealIntegerType,
  3460. CompatibleNumberIntegerType> {};
  3461. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3462. struct is_compatible_type_impl: std::false_type {};
  3463. template<typename BasicJsonType, typename CompatibleType>
  3464. struct is_compatible_type_impl <
  3465. BasicJsonType, CompatibleType,
  3466. enable_if_t<is_complete_type<CompatibleType>::value >>
  3467. {
  3468. static constexpr bool value =
  3469. has_to_json<BasicJsonType, CompatibleType>::value;
  3470. };
  3471. template<typename BasicJsonType, typename CompatibleType>
  3472. struct is_compatible_type
  3473. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3474. template<typename T1, typename T2>
  3475. struct is_constructible_tuple : std::false_type {};
  3476. template<typename T1, typename... Args>
  3477. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3478. template<typename BasicJsonType, typename T>
  3479. struct is_json_iterator_of : std::false_type {};
  3480. template<typename BasicJsonType>
  3481. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3482. template<typename BasicJsonType>
  3483. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3484. {};
  3485. // checks if a given type T is a template specialization of Primary
  3486. template<template <typename...> class Primary, typename T>
  3487. struct is_specialization_of : std::false_type {};
  3488. template<template <typename...> class Primary, typename... Args>
  3489. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3490. template<typename T>
  3491. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3492. // checks if A and B are comparable using Compare functor
  3493. template<typename Compare, typename A, typename B, typename = void>
  3494. struct is_comparable : std::false_type {};
  3495. template<typename Compare, typename A, typename B>
  3496. struct is_comparable<Compare, A, B, void_t<
  3497. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3498. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3499. >> : std::true_type {};
  3500. template<typename T>
  3501. using detect_is_transparent = typename T::is_transparent;
  3502. // type trait to check if KeyType can be used as object key (without a BasicJsonType)
  3503. // see is_usable_as_basic_json_key_type below
  3504. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3505. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3506. using is_usable_as_key_type = typename std::conditional <
  3507. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3508. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3509. ObjectKeyType>::value)
  3510. && (!RequireTransparentComparator
  3511. || is_detected <detect_is_transparent, Comparator>::value)
  3512. && !is_json_pointer<KeyType>::value,
  3513. std::true_type,
  3514. std::false_type >::type;
  3515. // type trait to check if KeyType can be used as object key
  3516. // true if:
  3517. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3518. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3519. // - the comparator is transparent or RequireTransparentComparator is false
  3520. // - KeyType is not a JSON iterator or json_pointer
  3521. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3522. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3523. using is_usable_as_basic_json_key_type = typename std::conditional <
  3524. is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3525. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3526. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3527. && !is_json_iterator_of<BasicJsonType, KeyType>::value,
  3528. std::true_type,
  3529. std::false_type >::type;
  3530. template<typename ObjectType, typename KeyType>
  3531. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3532. // type trait to check if object_t has an erase() member functions accepting KeyType
  3533. template<typename BasicJsonType, typename KeyType>
  3534. using has_erase_with_key_type = typename std::conditional <
  3535. is_detected <
  3536. detect_erase_with_key_type,
  3537. typename BasicJsonType::object_t, KeyType >::value,
  3538. std::true_type,
  3539. std::false_type >::type;
  3540. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3541. // ordered_map inherits capacity() from std::vector)
  3542. template <typename T>
  3543. struct is_ordered_map
  3544. {
  3545. using one = char;
  3546. struct two
  3547. {
  3548. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3549. };
  3550. template <typename C> static one test( decltype(&C::capacity) ) ;
  3551. template <typename C> static two test(...);
  3552. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3553. };
  3554. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3555. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3556. T conditional_static_cast(U value)
  3557. {
  3558. return static_cast<T>(value);
  3559. }
  3560. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3561. T conditional_static_cast(U value)
  3562. {
  3563. return value;
  3564. }
  3565. template<typename... Types>
  3566. using all_integral = conjunction<std::is_integral<Types>...>;
  3567. template<typename... Types>
  3568. using all_signed = conjunction<std::is_signed<Types>...>;
  3569. template<typename... Types>
  3570. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3571. // there's a disjunction trait in another PR; replace when merged
  3572. template<typename... Types>
  3573. using same_sign = std::integral_constant < bool,
  3574. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3575. template<typename OfType, typename T>
  3576. using never_out_of_range = std::integral_constant < bool,
  3577. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3578. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3579. template<typename OfType, typename T,
  3580. bool OfTypeSigned = std::is_signed<OfType>::value,
  3581. bool TSigned = std::is_signed<T>::value>
  3582. struct value_in_range_of_impl2;
  3583. template<typename OfType, typename T>
  3584. struct value_in_range_of_impl2<OfType, T, false, false>
  3585. {
  3586. static constexpr bool test(T val)
  3587. {
  3588. using CommonType = typename std::common_type<OfType, T>::type;
  3589. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3590. }
  3591. };
  3592. template<typename OfType, typename T>
  3593. struct value_in_range_of_impl2<OfType, T, true, false>
  3594. {
  3595. static constexpr bool test(T val)
  3596. {
  3597. using CommonType = typename std::common_type<OfType, T>::type;
  3598. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3599. }
  3600. };
  3601. template<typename OfType, typename T>
  3602. struct value_in_range_of_impl2<OfType, T, false, true>
  3603. {
  3604. static constexpr bool test(T val)
  3605. {
  3606. using CommonType = typename std::common_type<OfType, T>::type;
  3607. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3608. }
  3609. };
  3610. template<typename OfType, typename T>
  3611. struct value_in_range_of_impl2<OfType, T, true, true>
  3612. {
  3613. static constexpr bool test(T val)
  3614. {
  3615. using CommonType = typename std::common_type<OfType, T>::type;
  3616. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3617. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3618. }
  3619. };
  3620. template<typename OfType, typename T,
  3621. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3622. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3623. struct value_in_range_of_impl1;
  3624. template<typename OfType, typename T>
  3625. struct value_in_range_of_impl1<OfType, T, false>
  3626. {
  3627. static constexpr bool test(T val)
  3628. {
  3629. return value_in_range_of_impl2<OfType, T>::test(val);
  3630. }
  3631. };
  3632. template<typename OfType, typename T>
  3633. struct value_in_range_of_impl1<OfType, T, true>
  3634. {
  3635. static constexpr bool test(T /*val*/)
  3636. {
  3637. return true;
  3638. }
  3639. };
  3640. template<typename OfType, typename T>
  3641. inline constexpr bool value_in_range_of(T val)
  3642. {
  3643. return value_in_range_of_impl1<OfType, T>::test(val);
  3644. }
  3645. template<bool Value>
  3646. using bool_constant = std::integral_constant<bool, Value>;
  3647. ///////////////////////////////////////////////////////////////////////////////
  3648. // is_c_string
  3649. ///////////////////////////////////////////////////////////////////////////////
  3650. namespace impl
  3651. {
  3652. template<typename T>
  3653. inline constexpr bool is_c_string()
  3654. {
  3655. using TUnExt = typename std::remove_extent<T>::type;
  3656. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3657. using TUnPtr = typename std::remove_pointer<T>::type;
  3658. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3659. return
  3660. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3661. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3662. }
  3663. } // namespace impl
  3664. // checks whether T is a [cv] char */[cv] char[] C string
  3665. template<typename T>
  3666. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3667. template<typename T>
  3668. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3669. ///////////////////////////////////////////////////////////////////////////////
  3670. // is_transparent
  3671. ///////////////////////////////////////////////////////////////////////////////
  3672. namespace impl
  3673. {
  3674. template<typename T>
  3675. inline constexpr bool is_transparent()
  3676. {
  3677. return is_detected<detect_is_transparent, T>::value;
  3678. }
  3679. } // namespace impl
  3680. // checks whether T has a member named is_transparent
  3681. template<typename T>
  3682. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3683. ///////////////////////////////////////////////////////////////////////////////
  3684. } // namespace detail
  3685. NLOHMANN_JSON_NAMESPACE_END
  3686. // #include <nlohmann/detail/string_concat.hpp>
  3687. // __ _____ _____ _____
  3688. // __| | __| | | | JSON for Modern C++
  3689. // | | |__ | | | | | | version 3.11.3
  3690. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3691. //
  3692. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  3693. // SPDX-License-Identifier: MIT
  3694. #include <cstring> // strlen
  3695. #include <string> // string
  3696. #include <utility> // forward
  3697. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3698. // #include <nlohmann/detail/meta/detected.hpp>
  3699. NLOHMANN_JSON_NAMESPACE_BEGIN
  3700. namespace detail
  3701. {
  3702. inline std::size_t concat_length()
  3703. {
  3704. return 0;
  3705. }
  3706. template<typename... Args>
  3707. inline std::size_t concat_length(const char* cstr, const Args& ... rest);
  3708. template<typename StringType, typename... Args>
  3709. inline std::size_t concat_length(const StringType& str, const Args& ... rest);
  3710. template<typename... Args>
  3711. inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
  3712. {
  3713. return 1 + concat_length(rest...);
  3714. }
  3715. template<typename... Args>
  3716. inline std::size_t concat_length(const char* cstr, const Args& ... rest)
  3717. {
  3718. // cppcheck-suppress ignoredReturnValue
  3719. return ::strlen(cstr) + concat_length(rest...);
  3720. }
  3721. template<typename StringType, typename... Args>
  3722. inline std::size_t concat_length(const StringType& str, const Args& ... rest)
  3723. {
  3724. return str.size() + concat_length(rest...);
  3725. }
  3726. template<typename OutStringType>
  3727. inline void concat_into(OutStringType& /*out*/)
  3728. {}
  3729. template<typename StringType, typename Arg>
  3730. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3731. template<typename StringType, typename Arg>
  3732. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3733. template<typename StringType, typename Arg>
  3734. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3735. template<typename StringType, typename Arg>
  3736. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3737. template<typename StringType, typename Arg>
  3738. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3739. template<typename StringType, typename Arg>
  3740. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3741. template<typename StringType, typename Arg>
  3742. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3743. template<typename StringType, typename Arg>
  3744. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3745. template < typename OutStringType, typename Arg, typename... Args,
  3746. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3747. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3748. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3749. template < typename OutStringType, typename Arg, typename... Args,
  3750. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3751. && !detect_string_can_append_op<OutStringType, Arg>::value
  3752. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3753. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3754. template < typename OutStringType, typename Arg, typename... Args,
  3755. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3756. && !detect_string_can_append_op<OutStringType, Arg>::value
  3757. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3758. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3759. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3760. template<typename OutStringType, typename Arg, typename... Args,
  3761. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3762. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3763. {
  3764. out.append(std::forward<Arg>(arg));
  3765. concat_into(out, std::forward<Args>(rest)...);
  3766. }
  3767. template < typename OutStringType, typename Arg, typename... Args,
  3768. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3769. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3770. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3771. {
  3772. out += std::forward<Arg>(arg);
  3773. concat_into(out, std::forward<Args>(rest)...);
  3774. }
  3775. template < typename OutStringType, typename Arg, typename... Args,
  3776. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3777. && !detect_string_can_append_op<OutStringType, Arg>::value
  3778. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3779. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3780. {
  3781. out.append(arg.begin(), arg.end());
  3782. concat_into(out, std::forward<Args>(rest)...);
  3783. }
  3784. template < typename OutStringType, typename Arg, typename... Args,
  3785. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3786. && !detect_string_can_append_op<OutStringType, Arg>::value
  3787. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3788. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3789. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3790. {
  3791. out.append(arg.data(), arg.size());
  3792. concat_into(out, std::forward<Args>(rest)...);
  3793. }
  3794. template<typename OutStringType = std::string, typename... Args>
  3795. inline OutStringType concat(Args && ... args)
  3796. {
  3797. OutStringType str;
  3798. str.reserve(concat_length(args...));
  3799. concat_into(str, std::forward<Args>(args)...);
  3800. return str;
  3801. }
  3802. } // namespace detail
  3803. NLOHMANN_JSON_NAMESPACE_END
  3804. NLOHMANN_JSON_NAMESPACE_BEGIN
  3805. namespace detail
  3806. {
  3807. ////////////////
  3808. // exceptions //
  3809. ////////////////
  3810. /// @brief general exception of the @ref basic_json class
  3811. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3812. class exception : public std::exception
  3813. {
  3814. public:
  3815. /// returns the explanatory string
  3816. const char* what() const noexcept override
  3817. {
  3818. return m.what();
  3819. }
  3820. /// the id of the exception
  3821. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3822. protected:
  3823. JSON_HEDLEY_NON_NULL(3)
  3824. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3825. static std::string name(const std::string& ename, int id_)
  3826. {
  3827. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3828. }
  3829. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3830. {
  3831. return "";
  3832. }
  3833. template<typename BasicJsonType>
  3834. static std::string diagnostics(const BasicJsonType* leaf_element)
  3835. {
  3836. #if JSON_DIAGNOSTICS
  3837. std::vector<std::string> tokens;
  3838. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3839. {
  3840. switch (current->m_parent->type())
  3841. {
  3842. case value_t::array:
  3843. {
  3844. for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
  3845. {
  3846. if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
  3847. {
  3848. tokens.emplace_back(std::to_string(i));
  3849. break;
  3850. }
  3851. }
  3852. break;
  3853. }
  3854. case value_t::object:
  3855. {
  3856. for (const auto& element : *current->m_parent->m_data.m_value.object)
  3857. {
  3858. if (&element.second == current)
  3859. {
  3860. tokens.emplace_back(element.first.c_str());
  3861. break;
  3862. }
  3863. }
  3864. break;
  3865. }
  3866. case value_t::null: // LCOV_EXCL_LINE
  3867. case value_t::string: // LCOV_EXCL_LINE
  3868. case value_t::boolean: // LCOV_EXCL_LINE
  3869. case value_t::number_integer: // LCOV_EXCL_LINE
  3870. case value_t::number_unsigned: // LCOV_EXCL_LINE
  3871. case value_t::number_float: // LCOV_EXCL_LINE
  3872. case value_t::binary: // LCOV_EXCL_LINE
  3873. case value_t::discarded: // LCOV_EXCL_LINE
  3874. default: // LCOV_EXCL_LINE
  3875. break; // LCOV_EXCL_LINE
  3876. }
  3877. }
  3878. if (tokens.empty())
  3879. {
  3880. return "";
  3881. }
  3882. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  3883. [](const std::string & a, const std::string & b)
  3884. {
  3885. return concat(a, '/', detail::escape(b));
  3886. });
  3887. return concat('(', str, ") ");
  3888. #else
  3889. static_cast<void>(leaf_element);
  3890. return "";
  3891. #endif
  3892. }
  3893. private:
  3894. /// an exception object as storage for error messages
  3895. std::runtime_error m;
  3896. };
  3897. /// @brief exception indicating a parse error
  3898. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  3899. class parse_error : public exception
  3900. {
  3901. public:
  3902. /*!
  3903. @brief create a parse error exception
  3904. @param[in] id_ the id of the exception
  3905. @param[in] pos the position where the error occurred (or with
  3906. chars_read_total=0 if the position cannot be
  3907. determined)
  3908. @param[in] what_arg the explanatory string
  3909. @return parse_error object
  3910. */
  3911. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3912. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  3913. {
  3914. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3915. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  3916. return {id_, pos.chars_read_total, w.c_str()};
  3917. }
  3918. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3919. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  3920. {
  3921. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3922. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  3923. ": ", exception::diagnostics(context), what_arg);
  3924. return {id_, byte_, w.c_str()};
  3925. }
  3926. /*!
  3927. @brief byte index of the parse error
  3928. The byte index of the last read character in the input file.
  3929. @note For an input with n bytes, 1 is the index of the first character and
  3930. n+1 is the index of the terminating null byte or the end of file.
  3931. This also holds true when reading a byte vector (CBOR or MessagePack).
  3932. */
  3933. const std::size_t byte;
  3934. private:
  3935. parse_error(int id_, std::size_t byte_, const char* what_arg)
  3936. : exception(id_, what_arg), byte(byte_) {}
  3937. static std::string position_string(const position_t& pos)
  3938. {
  3939. return concat(" at line ", std::to_string(pos.lines_read + 1),
  3940. ", column ", std::to_string(pos.chars_read_current_line));
  3941. }
  3942. };
  3943. /// @brief exception indicating errors with iterators
  3944. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  3945. class invalid_iterator : public exception
  3946. {
  3947. public:
  3948. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3949. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  3950. {
  3951. const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  3952. return {id_, w.c_str()};
  3953. }
  3954. private:
  3955. JSON_HEDLEY_NON_NULL(3)
  3956. invalid_iterator(int id_, const char* what_arg)
  3957. : exception(id_, what_arg) {}
  3958. };
  3959. /// @brief exception indicating executing a member function with a wrong type
  3960. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  3961. class type_error : public exception
  3962. {
  3963. public:
  3964. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3965. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3966. {
  3967. const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  3968. return {id_, w.c_str()};
  3969. }
  3970. private:
  3971. JSON_HEDLEY_NON_NULL(3)
  3972. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3973. };
  3974. /// @brief exception indicating access out of the defined range
  3975. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  3976. class out_of_range : public exception
  3977. {
  3978. public:
  3979. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3980. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  3981. {
  3982. const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  3983. return {id_, w.c_str()};
  3984. }
  3985. private:
  3986. JSON_HEDLEY_NON_NULL(3)
  3987. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3988. };
  3989. /// @brief exception indicating other library errors
  3990. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  3991. class other_error : public exception
  3992. {
  3993. public:
  3994. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3995. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3996. {
  3997. const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  3998. return {id_, w.c_str()};
  3999. }
  4000. private:
  4001. JSON_HEDLEY_NON_NULL(3)
  4002. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4003. };
  4004. } // namespace detail
  4005. NLOHMANN_JSON_NAMESPACE_END
  4006. // #include <nlohmann/detail/macro_scope.hpp>
  4007. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4008. // #include <nlohmann/detail/meta/identity_tag.hpp>
  4009. // __ _____ _____ _____
  4010. // __| | __| | | | JSON for Modern C++
  4011. // | | |__ | | | | | | version 3.11.3
  4012. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4013. //
  4014. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4015. // SPDX-License-Identifier: MIT
  4016. // #include <nlohmann/detail/abi_macros.hpp>
  4017. NLOHMANN_JSON_NAMESPACE_BEGIN
  4018. namespace detail
  4019. {
  4020. // dispatching helper struct
  4021. template <class T> struct identity_tag {};
  4022. } // namespace detail
  4023. NLOHMANN_JSON_NAMESPACE_END
  4024. // #include <nlohmann/detail/meta/std_fs.hpp>
  4025. // __ _____ _____ _____
  4026. // __| | __| | | | JSON for Modern C++
  4027. // | | |__ | | | | | | version 3.11.3
  4028. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4029. //
  4030. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4031. // SPDX-License-Identifier: MIT
  4032. // #include <nlohmann/detail/macro_scope.hpp>
  4033. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4034. #include <experimental/filesystem>
  4035. NLOHMANN_JSON_NAMESPACE_BEGIN
  4036. namespace detail
  4037. {
  4038. namespace std_fs = std::experimental::filesystem;
  4039. } // namespace detail
  4040. NLOHMANN_JSON_NAMESPACE_END
  4041. #elif JSON_HAS_FILESYSTEM
  4042. #include <filesystem>
  4043. NLOHMANN_JSON_NAMESPACE_BEGIN
  4044. namespace detail
  4045. {
  4046. namespace std_fs = std::filesystem;
  4047. } // namespace detail
  4048. NLOHMANN_JSON_NAMESPACE_END
  4049. #endif
  4050. // #include <nlohmann/detail/meta/type_traits.hpp>
  4051. // #include <nlohmann/detail/string_concat.hpp>
  4052. // #include <nlohmann/detail/value_t.hpp>
  4053. NLOHMANN_JSON_NAMESPACE_BEGIN
  4054. namespace detail
  4055. {
  4056. template<typename BasicJsonType>
  4057. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  4058. {
  4059. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  4060. {
  4061. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  4062. }
  4063. n = nullptr;
  4064. }
  4065. // overloads for basic_json template parameters
  4066. template < typename BasicJsonType, typename ArithmeticType,
  4067. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4068. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4069. int > = 0 >
  4070. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4071. {
  4072. switch (static_cast<value_t>(j))
  4073. {
  4074. case value_t::number_unsigned:
  4075. {
  4076. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4077. break;
  4078. }
  4079. case value_t::number_integer:
  4080. {
  4081. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4082. break;
  4083. }
  4084. case value_t::number_float:
  4085. {
  4086. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4087. break;
  4088. }
  4089. case value_t::null:
  4090. case value_t::object:
  4091. case value_t::array:
  4092. case value_t::string:
  4093. case value_t::boolean:
  4094. case value_t::binary:
  4095. case value_t::discarded:
  4096. default:
  4097. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4098. }
  4099. }
  4100. template<typename BasicJsonType>
  4101. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4102. {
  4103. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4104. {
  4105. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4106. }
  4107. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4108. }
  4109. template<typename BasicJsonType>
  4110. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4111. {
  4112. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4113. {
  4114. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4115. }
  4116. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4117. }
  4118. template <
  4119. typename BasicJsonType, typename StringType,
  4120. enable_if_t <
  4121. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4122. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4123. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4124. && !is_json_ref<StringType>::value, int > = 0 >
  4125. inline void from_json(const BasicJsonType& j, StringType& s)
  4126. {
  4127. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4128. {
  4129. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4130. }
  4131. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4132. }
  4133. template<typename BasicJsonType>
  4134. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4135. {
  4136. get_arithmetic_value(j, val);
  4137. }
  4138. template<typename BasicJsonType>
  4139. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4140. {
  4141. get_arithmetic_value(j, val);
  4142. }
  4143. template<typename BasicJsonType>
  4144. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4145. {
  4146. get_arithmetic_value(j, val);
  4147. }
  4148. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4149. template<typename BasicJsonType, typename EnumType,
  4150. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4151. inline void from_json(const BasicJsonType& j, EnumType& e)
  4152. {
  4153. typename std::underlying_type<EnumType>::type val;
  4154. get_arithmetic_value(j, val);
  4155. e = static_cast<EnumType>(val);
  4156. }
  4157. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4158. // forward_list doesn't have an insert method
  4159. template<typename BasicJsonType, typename T, typename Allocator,
  4160. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4161. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4162. {
  4163. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4164. {
  4165. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4166. }
  4167. l.clear();
  4168. std::transform(j.rbegin(), j.rend(),
  4169. std::front_inserter(l), [](const BasicJsonType & i)
  4170. {
  4171. return i.template get<T>();
  4172. });
  4173. }
  4174. // valarray doesn't have an insert method
  4175. template<typename BasicJsonType, typename T,
  4176. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4177. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4178. {
  4179. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4180. {
  4181. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4182. }
  4183. l.resize(j.size());
  4184. std::transform(j.begin(), j.end(), std::begin(l),
  4185. [](const BasicJsonType & elem)
  4186. {
  4187. return elem.template get<T>();
  4188. });
  4189. }
  4190. template<typename BasicJsonType, typename T, std::size_t N>
  4191. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4192. -> decltype(j.template get<T>(), void())
  4193. {
  4194. for (std::size_t i = 0; i < N; ++i)
  4195. {
  4196. arr[i] = j.at(i).template get<T>();
  4197. }
  4198. }
  4199. template<typename BasicJsonType>
  4200. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4201. {
  4202. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4203. }
  4204. template<typename BasicJsonType, typename T, std::size_t N>
  4205. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4206. priority_tag<2> /*unused*/)
  4207. -> decltype(j.template get<T>(), void())
  4208. {
  4209. for (std::size_t i = 0; i < N; ++i)
  4210. {
  4211. arr[i] = j.at(i).template get<T>();
  4212. }
  4213. }
  4214. template<typename BasicJsonType, typename ConstructibleArrayType,
  4215. enable_if_t<
  4216. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4217. int> = 0>
  4218. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4219. -> decltype(
  4220. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4221. j.template get<typename ConstructibleArrayType::value_type>(),
  4222. void())
  4223. {
  4224. using std::end;
  4225. ConstructibleArrayType ret;
  4226. ret.reserve(j.size());
  4227. std::transform(j.begin(), j.end(),
  4228. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4229. {
  4230. // get<BasicJsonType>() returns *this, this won't call a from_json
  4231. // method when value_type is BasicJsonType
  4232. return i.template get<typename ConstructibleArrayType::value_type>();
  4233. });
  4234. arr = std::move(ret);
  4235. }
  4236. template<typename BasicJsonType, typename ConstructibleArrayType,
  4237. enable_if_t<
  4238. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4239. int> = 0>
  4240. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4241. priority_tag<0> /*unused*/)
  4242. {
  4243. using std::end;
  4244. ConstructibleArrayType ret;
  4245. std::transform(
  4246. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4247. [](const BasicJsonType & i)
  4248. {
  4249. // get<BasicJsonType>() returns *this, this won't call a from_json
  4250. // method when value_type is BasicJsonType
  4251. return i.template get<typename ConstructibleArrayType::value_type>();
  4252. });
  4253. arr = std::move(ret);
  4254. }
  4255. template < typename BasicJsonType, typename ConstructibleArrayType,
  4256. enable_if_t <
  4257. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4258. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4259. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4260. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4261. !is_basic_json<ConstructibleArrayType>::value,
  4262. int > = 0 >
  4263. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4264. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4265. j.template get<typename ConstructibleArrayType::value_type>(),
  4266. void())
  4267. {
  4268. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4269. {
  4270. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4271. }
  4272. from_json_array_impl(j, arr, priority_tag<3> {});
  4273. }
  4274. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4275. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4276. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4277. {
  4278. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4279. }
  4280. template < typename BasicJsonType, typename T, std::size_t N >
  4281. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4282. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4283. {
  4284. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4285. {
  4286. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4287. }
  4288. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4289. }
  4290. template<typename BasicJsonType>
  4291. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4292. {
  4293. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4294. {
  4295. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4296. }
  4297. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4298. }
  4299. template<typename BasicJsonType, typename ConstructibleObjectType,
  4300. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4301. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4302. {
  4303. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4304. {
  4305. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4306. }
  4307. ConstructibleObjectType ret;
  4308. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4309. using value_type = typename ConstructibleObjectType::value_type;
  4310. std::transform(
  4311. inner_object->begin(), inner_object->end(),
  4312. std::inserter(ret, ret.begin()),
  4313. [](typename BasicJsonType::object_t::value_type const & p)
  4314. {
  4315. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4316. });
  4317. obj = std::move(ret);
  4318. }
  4319. // overload for arithmetic types, not chosen for basic_json template arguments
  4320. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4321. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4322. // an arithmetic type?
  4323. template < typename BasicJsonType, typename ArithmeticType,
  4324. enable_if_t <
  4325. std::is_arithmetic<ArithmeticType>::value&&
  4326. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4327. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4328. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4329. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4330. int > = 0 >
  4331. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4332. {
  4333. switch (static_cast<value_t>(j))
  4334. {
  4335. case value_t::number_unsigned:
  4336. {
  4337. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4338. break;
  4339. }
  4340. case value_t::number_integer:
  4341. {
  4342. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4343. break;
  4344. }
  4345. case value_t::number_float:
  4346. {
  4347. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4348. break;
  4349. }
  4350. case value_t::boolean:
  4351. {
  4352. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4353. break;
  4354. }
  4355. case value_t::null:
  4356. case value_t::object:
  4357. case value_t::array:
  4358. case value_t::string:
  4359. case value_t::binary:
  4360. case value_t::discarded:
  4361. default:
  4362. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4363. }
  4364. }
  4365. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4366. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4367. {
  4368. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4369. }
  4370. template < typename BasicJsonType, class A1, class A2 >
  4371. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4372. {
  4373. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4374. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4375. }
  4376. template<typename BasicJsonType, typename A1, typename A2>
  4377. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4378. {
  4379. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4380. }
  4381. template<typename BasicJsonType, typename... Args>
  4382. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4383. {
  4384. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4385. }
  4386. template<typename BasicJsonType, typename... Args>
  4387. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4388. {
  4389. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4390. }
  4391. template<typename BasicJsonType, typename TupleRelated>
  4392. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4393. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4394. {
  4395. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4396. {
  4397. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4398. }
  4399. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4400. }
  4401. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4402. typename = enable_if_t < !std::is_constructible <
  4403. typename BasicJsonType::string_t, Key >::value >>
  4404. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  4405. {
  4406. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4407. {
  4408. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4409. }
  4410. m.clear();
  4411. for (const auto& p : j)
  4412. {
  4413. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4414. {
  4415. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4416. }
  4417. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4418. }
  4419. }
  4420. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4421. typename = enable_if_t < !std::is_constructible <
  4422. typename BasicJsonType::string_t, Key >::value >>
  4423. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4424. {
  4425. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4426. {
  4427. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4428. }
  4429. m.clear();
  4430. for (const auto& p : j)
  4431. {
  4432. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4433. {
  4434. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4435. }
  4436. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4437. }
  4438. }
  4439. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4440. template<typename BasicJsonType>
  4441. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4442. {
  4443. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4444. {
  4445. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4446. }
  4447. p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4448. }
  4449. #endif
  4450. struct from_json_fn
  4451. {
  4452. template<typename BasicJsonType, typename T>
  4453. auto operator()(const BasicJsonType& j, T&& val) const
  4454. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4455. -> decltype(from_json(j, std::forward<T>(val)))
  4456. {
  4457. return from_json(j, std::forward<T>(val));
  4458. }
  4459. };
  4460. } // namespace detail
  4461. #ifndef JSON_HAS_CPP_17
  4462. /// namespace to hold default `from_json` function
  4463. /// to see why this is required:
  4464. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4465. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4466. {
  4467. #endif
  4468. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4469. detail::static_const<detail::from_json_fn>::value;
  4470. #ifndef JSON_HAS_CPP_17
  4471. } // namespace
  4472. #endif
  4473. NLOHMANN_JSON_NAMESPACE_END
  4474. // #include <nlohmann/detail/conversions/to_json.hpp>
  4475. // __ _____ _____ _____
  4476. // __| | __| | | | JSON for Modern C++
  4477. // | | |__ | | | | | | version 3.11.3
  4478. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4479. //
  4480. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4481. // SPDX-License-Identifier: MIT
  4482. #include <algorithm> // copy
  4483. #include <iterator> // begin, end
  4484. #include <string> // string
  4485. #include <tuple> // tuple, get
  4486. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4487. #include <utility> // move, forward, declval, pair
  4488. #include <valarray> // valarray
  4489. #include <vector> // vector
  4490. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4491. // __ _____ _____ _____
  4492. // __| | __| | | | JSON for Modern C++
  4493. // | | |__ | | | | | | version 3.11.3
  4494. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4495. //
  4496. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4497. // SPDX-License-Identifier: MIT
  4498. #include <cstddef> // size_t
  4499. #include <iterator> // input_iterator_tag
  4500. #include <string> // string, to_string
  4501. #include <tuple> // tuple_size, get, tuple_element
  4502. #include <utility> // move
  4503. #if JSON_HAS_RANGES
  4504. #include <ranges> // enable_borrowed_range
  4505. #endif
  4506. // #include <nlohmann/detail/abi_macros.hpp>
  4507. // #include <nlohmann/detail/meta/type_traits.hpp>
  4508. // #include <nlohmann/detail/value_t.hpp>
  4509. NLOHMANN_JSON_NAMESPACE_BEGIN
  4510. namespace detail
  4511. {
  4512. template<typename string_type>
  4513. void int_to_string( string_type& target, std::size_t value )
  4514. {
  4515. // For ADL
  4516. using std::to_string;
  4517. target = to_string(value);
  4518. }
  4519. template<typename IteratorType> class iteration_proxy_value
  4520. {
  4521. public:
  4522. using difference_type = std::ptrdiff_t;
  4523. using value_type = iteration_proxy_value;
  4524. using pointer = value_type *;
  4525. using reference = value_type &;
  4526. using iterator_category = std::input_iterator_tag;
  4527. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4528. private:
  4529. /// the iterator
  4530. IteratorType anchor{};
  4531. /// an index for arrays (used to create key names)
  4532. std::size_t array_index = 0;
  4533. /// last stringified array index
  4534. mutable std::size_t array_index_last = 0;
  4535. /// a string representation of the array index
  4536. mutable string_type array_index_str = "0";
  4537. /// an empty string (to return a reference for primitive values)
  4538. string_type empty_str{};
  4539. public:
  4540. explicit iteration_proxy_value() = default;
  4541. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4542. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4543. && std::is_nothrow_default_constructible<string_type>::value)
  4544. : anchor(std::move(it))
  4545. , array_index(array_index_)
  4546. {}
  4547. iteration_proxy_value(iteration_proxy_value const&) = default;
  4548. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4549. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4550. iteration_proxy_value(iteration_proxy_value&&)
  4551. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4552. && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4553. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4554. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4555. && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4556. ~iteration_proxy_value() = default;
  4557. /// dereference operator (needed for range-based for)
  4558. const iteration_proxy_value& operator*() const
  4559. {
  4560. return *this;
  4561. }
  4562. /// increment operator (needed for range-based for)
  4563. iteration_proxy_value& operator++()
  4564. {
  4565. ++anchor;
  4566. ++array_index;
  4567. return *this;
  4568. }
  4569. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4570. {
  4571. auto tmp = iteration_proxy_value(anchor, array_index);
  4572. ++anchor;
  4573. ++array_index;
  4574. return tmp;
  4575. }
  4576. /// equality operator (needed for InputIterator)
  4577. bool operator==(const iteration_proxy_value& o) const
  4578. {
  4579. return anchor == o.anchor;
  4580. }
  4581. /// inequality operator (needed for range-based for)
  4582. bool operator!=(const iteration_proxy_value& o) const
  4583. {
  4584. return anchor != o.anchor;
  4585. }
  4586. /// return key of the iterator
  4587. const string_type& key() const
  4588. {
  4589. JSON_ASSERT(anchor.m_object != nullptr);
  4590. switch (anchor.m_object->type())
  4591. {
  4592. // use integer array index as key
  4593. case value_t::array:
  4594. {
  4595. if (array_index != array_index_last)
  4596. {
  4597. int_to_string( array_index_str, array_index );
  4598. array_index_last = array_index;
  4599. }
  4600. return array_index_str;
  4601. }
  4602. // use key from the object
  4603. case value_t::object:
  4604. return anchor.key();
  4605. // use an empty key for all primitive types
  4606. case value_t::null:
  4607. case value_t::string:
  4608. case value_t::boolean:
  4609. case value_t::number_integer:
  4610. case value_t::number_unsigned:
  4611. case value_t::number_float:
  4612. case value_t::binary:
  4613. case value_t::discarded:
  4614. default:
  4615. return empty_str;
  4616. }
  4617. }
  4618. /// return value of the iterator
  4619. typename IteratorType::reference value() const
  4620. {
  4621. return anchor.value();
  4622. }
  4623. };
  4624. /// proxy class for the items() function
  4625. template<typename IteratorType> class iteration_proxy
  4626. {
  4627. private:
  4628. /// the container to iterate
  4629. typename IteratorType::pointer container = nullptr;
  4630. public:
  4631. explicit iteration_proxy() = default;
  4632. /// construct iteration proxy from a container
  4633. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4634. : container(&cont) {}
  4635. iteration_proxy(iteration_proxy const&) = default;
  4636. iteration_proxy& operator=(iteration_proxy const&) = default;
  4637. iteration_proxy(iteration_proxy&&) noexcept = default;
  4638. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4639. ~iteration_proxy() = default;
  4640. /// return iterator begin (needed for range-based for)
  4641. iteration_proxy_value<IteratorType> begin() const noexcept
  4642. {
  4643. return iteration_proxy_value<IteratorType>(container->begin());
  4644. }
  4645. /// return iterator end (needed for range-based for)
  4646. iteration_proxy_value<IteratorType> end() const noexcept
  4647. {
  4648. return iteration_proxy_value<IteratorType>(container->end());
  4649. }
  4650. };
  4651. // Structured Bindings Support
  4652. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4653. // And see https://github.com/nlohmann/json/pull/1391
  4654. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4655. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4656. {
  4657. return i.key();
  4658. }
  4659. // Structured Bindings Support
  4660. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4661. // And see https://github.com/nlohmann/json/pull/1391
  4662. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4663. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4664. {
  4665. return i.value();
  4666. }
  4667. } // namespace detail
  4668. NLOHMANN_JSON_NAMESPACE_END
  4669. // The Addition to the STD Namespace is required to add
  4670. // Structured Bindings Support to the iteration_proxy_value class
  4671. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4672. // And see https://github.com/nlohmann/json/pull/1391
  4673. namespace std
  4674. {
  4675. #if defined(__clang__)
  4676. // Fix: https://github.com/nlohmann/json/issues/1401
  4677. #pragma clang diagnostic push
  4678. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4679. #endif
  4680. template<typename IteratorType>
  4681. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  4682. : public std::integral_constant<std::size_t, 2> {};
  4683. template<std::size_t N, typename IteratorType>
  4684. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  4685. {
  4686. public:
  4687. using type = decltype(
  4688. get<N>(std::declval <
  4689. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4690. };
  4691. #if defined(__clang__)
  4692. #pragma clang diagnostic pop
  4693. #endif
  4694. } // namespace std
  4695. #if JSON_HAS_RANGES
  4696. template <typename IteratorType>
  4697. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4698. #endif
  4699. // #include <nlohmann/detail/macro_scope.hpp>
  4700. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4701. // #include <nlohmann/detail/meta/std_fs.hpp>
  4702. // #include <nlohmann/detail/meta/type_traits.hpp>
  4703. // #include <nlohmann/detail/value_t.hpp>
  4704. NLOHMANN_JSON_NAMESPACE_BEGIN
  4705. namespace detail
  4706. {
  4707. //////////////////
  4708. // constructors //
  4709. //////////////////
  4710. /*
  4711. * Note all external_constructor<>::construct functions need to call
  4712. * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
  4713. * allocated value (e.g., a string). See bug issue
  4714. * https://github.com/nlohmann/json/issues/2865 for more information.
  4715. */
  4716. template<value_t> struct external_constructor;
  4717. template<>
  4718. struct external_constructor<value_t::boolean>
  4719. {
  4720. template<typename BasicJsonType>
  4721. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4722. {
  4723. j.m_data.m_value.destroy(j.m_data.m_type);
  4724. j.m_data.m_type = value_t::boolean;
  4725. j.m_data.m_value = b;
  4726. j.assert_invariant();
  4727. }
  4728. };
  4729. template<>
  4730. struct external_constructor<value_t::string>
  4731. {
  4732. template<typename BasicJsonType>
  4733. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4734. {
  4735. j.m_data.m_value.destroy(j.m_data.m_type);
  4736. j.m_data.m_type = value_t::string;
  4737. j.m_data.m_value = s;
  4738. j.assert_invariant();
  4739. }
  4740. template<typename BasicJsonType>
  4741. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4742. {
  4743. j.m_data.m_value.destroy(j.m_data.m_type);
  4744. j.m_data.m_type = value_t::string;
  4745. j.m_data.m_value = std::move(s);
  4746. j.assert_invariant();
  4747. }
  4748. template < typename BasicJsonType, typename CompatibleStringType,
  4749. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4750. int > = 0 >
  4751. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4752. {
  4753. j.m_data.m_value.destroy(j.m_data.m_type);
  4754. j.m_data.m_type = value_t::string;
  4755. j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4756. j.assert_invariant();
  4757. }
  4758. };
  4759. template<>
  4760. struct external_constructor<value_t::binary>
  4761. {
  4762. template<typename BasicJsonType>
  4763. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4764. {
  4765. j.m_data.m_value.destroy(j.m_data.m_type);
  4766. j.m_data.m_type = value_t::binary;
  4767. j.m_data.m_value = typename BasicJsonType::binary_t(b);
  4768. j.assert_invariant();
  4769. }
  4770. template<typename BasicJsonType>
  4771. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4772. {
  4773. j.m_data.m_value.destroy(j.m_data.m_type);
  4774. j.m_data.m_type = value_t::binary;
  4775. j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
  4776. j.assert_invariant();
  4777. }
  4778. };
  4779. template<>
  4780. struct external_constructor<value_t::number_float>
  4781. {
  4782. template<typename BasicJsonType>
  4783. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4784. {
  4785. j.m_data.m_value.destroy(j.m_data.m_type);
  4786. j.m_data.m_type = value_t::number_float;
  4787. j.m_data.m_value = val;
  4788. j.assert_invariant();
  4789. }
  4790. };
  4791. template<>
  4792. struct external_constructor<value_t::number_unsigned>
  4793. {
  4794. template<typename BasicJsonType>
  4795. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4796. {
  4797. j.m_data.m_value.destroy(j.m_data.m_type);
  4798. j.m_data.m_type = value_t::number_unsigned;
  4799. j.m_data.m_value = val;
  4800. j.assert_invariant();
  4801. }
  4802. };
  4803. template<>
  4804. struct external_constructor<value_t::number_integer>
  4805. {
  4806. template<typename BasicJsonType>
  4807. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4808. {
  4809. j.m_data.m_value.destroy(j.m_data.m_type);
  4810. j.m_data.m_type = value_t::number_integer;
  4811. j.m_data.m_value = val;
  4812. j.assert_invariant();
  4813. }
  4814. };
  4815. template<>
  4816. struct external_constructor<value_t::array>
  4817. {
  4818. template<typename BasicJsonType>
  4819. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4820. {
  4821. j.m_data.m_value.destroy(j.m_data.m_type);
  4822. j.m_data.m_type = value_t::array;
  4823. j.m_data.m_value = arr;
  4824. j.set_parents();
  4825. j.assert_invariant();
  4826. }
  4827. template<typename BasicJsonType>
  4828. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4829. {
  4830. j.m_data.m_value.destroy(j.m_data.m_type);
  4831. j.m_data.m_type = value_t::array;
  4832. j.m_data.m_value = std::move(arr);
  4833. j.set_parents();
  4834. j.assert_invariant();
  4835. }
  4836. template < typename BasicJsonType, typename CompatibleArrayType,
  4837. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4838. int > = 0 >
  4839. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4840. {
  4841. using std::begin;
  4842. using std::end;
  4843. j.m_data.m_value.destroy(j.m_data.m_type);
  4844. j.m_data.m_type = value_t::array;
  4845. j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4846. j.set_parents();
  4847. j.assert_invariant();
  4848. }
  4849. template<typename BasicJsonType>
  4850. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4851. {
  4852. j.m_data.m_value.destroy(j.m_data.m_type);
  4853. j.m_data.m_type = value_t::array;
  4854. j.m_data.m_value = value_t::array;
  4855. j.m_data.m_value.array->reserve(arr.size());
  4856. for (const bool x : arr)
  4857. {
  4858. j.m_data.m_value.array->push_back(x);
  4859. j.set_parent(j.m_data.m_value.array->back());
  4860. }
  4861. j.assert_invariant();
  4862. }
  4863. template<typename BasicJsonType, typename T,
  4864. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4865. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4866. {
  4867. j.m_data.m_value.destroy(j.m_data.m_type);
  4868. j.m_data.m_type = value_t::array;
  4869. j.m_data.m_value = value_t::array;
  4870. j.m_data.m_value.array->resize(arr.size());
  4871. if (arr.size() > 0)
  4872. {
  4873. std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
  4874. }
  4875. j.set_parents();
  4876. j.assert_invariant();
  4877. }
  4878. };
  4879. template<>
  4880. struct external_constructor<value_t::object>
  4881. {
  4882. template<typename BasicJsonType>
  4883. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4884. {
  4885. j.m_data.m_value.destroy(j.m_data.m_type);
  4886. j.m_data.m_type = value_t::object;
  4887. j.m_data.m_value = obj;
  4888. j.set_parents();
  4889. j.assert_invariant();
  4890. }
  4891. template<typename BasicJsonType>
  4892. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4893. {
  4894. j.m_data.m_value.destroy(j.m_data.m_type);
  4895. j.m_data.m_type = value_t::object;
  4896. j.m_data.m_value = std::move(obj);
  4897. j.set_parents();
  4898. j.assert_invariant();
  4899. }
  4900. template < typename BasicJsonType, typename CompatibleObjectType,
  4901. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4902. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4903. {
  4904. using std::begin;
  4905. using std::end;
  4906. j.m_data.m_value.destroy(j.m_data.m_type);
  4907. j.m_data.m_type = value_t::object;
  4908. j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4909. j.set_parents();
  4910. j.assert_invariant();
  4911. }
  4912. };
  4913. /////////////
  4914. // to_json //
  4915. /////////////
  4916. template<typename BasicJsonType, typename T,
  4917. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4918. inline void to_json(BasicJsonType& j, T b) noexcept
  4919. {
  4920. external_constructor<value_t::boolean>::construct(j, b);
  4921. }
  4922. template < typename BasicJsonType, typename BoolRef,
  4923. enable_if_t <
  4924. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  4925. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  4926. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  4927. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  4928. typename BasicJsonType::boolean_t >::value))
  4929. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  4930. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  4931. {
  4932. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  4933. }
  4934. template<typename BasicJsonType, typename CompatibleString,
  4935. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4936. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  4937. {
  4938. external_constructor<value_t::string>::construct(j, s);
  4939. }
  4940. template<typename BasicJsonType>
  4941. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4942. {
  4943. external_constructor<value_t::string>::construct(j, std::move(s));
  4944. }
  4945. template<typename BasicJsonType, typename FloatType,
  4946. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4947. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  4948. {
  4949. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4950. }
  4951. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4952. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4953. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4954. {
  4955. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4956. }
  4957. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4958. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4959. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4960. {
  4961. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4962. }
  4963. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4964. template<typename BasicJsonType, typename EnumType,
  4965. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4966. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  4967. {
  4968. using underlying_type = typename std::underlying_type<EnumType>::type;
  4969. static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
  4970. external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
  4971. }
  4972. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4973. template<typename BasicJsonType>
  4974. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4975. {
  4976. external_constructor<value_t::array>::construct(j, e);
  4977. }
  4978. template < typename BasicJsonType, typename CompatibleArrayType,
  4979. enable_if_t < is_compatible_array_type<BasicJsonType,
  4980. CompatibleArrayType>::value&&
  4981. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4982. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4983. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4984. !is_basic_json<CompatibleArrayType>::value,
  4985. int > = 0 >
  4986. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4987. {
  4988. external_constructor<value_t::array>::construct(j, arr);
  4989. }
  4990. template<typename BasicJsonType>
  4991. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4992. {
  4993. external_constructor<value_t::binary>::construct(j, bin);
  4994. }
  4995. template<typename BasicJsonType, typename T,
  4996. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4997. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4998. {
  4999. external_constructor<value_t::array>::construct(j, std::move(arr));
  5000. }
  5001. template<typename BasicJsonType>
  5002. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  5003. {
  5004. external_constructor<value_t::array>::construct(j, std::move(arr));
  5005. }
  5006. template < typename BasicJsonType, typename CompatibleObjectType,
  5007. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  5008. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  5009. {
  5010. external_constructor<value_t::object>::construct(j, obj);
  5011. }
  5012. template<typename BasicJsonType>
  5013. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  5014. {
  5015. external_constructor<value_t::object>::construct(j, std::move(obj));
  5016. }
  5017. template <
  5018. typename BasicJsonType, typename T, std::size_t N,
  5019. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  5020. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5021. int > = 0 >
  5022. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5023. {
  5024. external_constructor<value_t::array>::construct(j, arr);
  5025. }
  5026. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  5027. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  5028. {
  5029. j = { p.first, p.second };
  5030. }
  5031. // for https://github.com/nlohmann/json/pull/1134
  5032. template<typename BasicJsonType, typename T,
  5033. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  5034. inline void to_json(BasicJsonType& j, const T& b)
  5035. {
  5036. j = { {b.key(), b.value()} };
  5037. }
  5038. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  5039. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  5040. {
  5041. j = { std::get<Idx>(t)... };
  5042. }
  5043. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  5044. inline void to_json(BasicJsonType& j, const T& t)
  5045. {
  5046. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  5047. }
  5048. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  5049. template<typename BasicJsonType>
  5050. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  5051. {
  5052. j = p.string();
  5053. }
  5054. #endif
  5055. struct to_json_fn
  5056. {
  5057. template<typename BasicJsonType, typename T>
  5058. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  5059. -> decltype(to_json(j, std::forward<T>(val)), void())
  5060. {
  5061. return to_json(j, std::forward<T>(val));
  5062. }
  5063. };
  5064. } // namespace detail
  5065. #ifndef JSON_HAS_CPP_17
  5066. /// namespace to hold default `to_json` function
  5067. /// to see why this is required:
  5068. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5069. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5070. {
  5071. #endif
  5072. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5073. detail::static_const<detail::to_json_fn>::value;
  5074. #ifndef JSON_HAS_CPP_17
  5075. } // namespace
  5076. #endif
  5077. NLOHMANN_JSON_NAMESPACE_END
  5078. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5079. NLOHMANN_JSON_NAMESPACE_BEGIN
  5080. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5081. template<typename ValueType, typename>
  5082. struct adl_serializer
  5083. {
  5084. /// @brief convert a JSON value to any value type
  5085. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5086. template<typename BasicJsonType, typename TargetType = ValueType>
  5087. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5088. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5089. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5090. {
  5091. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5092. }
  5093. /// @brief convert a JSON value to any value type
  5094. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5095. template<typename BasicJsonType, typename TargetType = ValueType>
  5096. static auto from_json(BasicJsonType && j) noexcept(
  5097. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5098. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5099. {
  5100. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5101. }
  5102. /// @brief convert any value type to a JSON value
  5103. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5104. template<typename BasicJsonType, typename TargetType = ValueType>
  5105. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5106. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5107. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5108. {
  5109. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5110. }
  5111. };
  5112. NLOHMANN_JSON_NAMESPACE_END
  5113. // #include <nlohmann/byte_container_with_subtype.hpp>
  5114. // __ _____ _____ _____
  5115. // __| | __| | | | JSON for Modern C++
  5116. // | | |__ | | | | | | version 3.11.3
  5117. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5118. //
  5119. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5120. // SPDX-License-Identifier: MIT
  5121. #include <cstdint> // uint8_t, uint64_t
  5122. #include <tuple> // tie
  5123. #include <utility> // move
  5124. // #include <nlohmann/detail/abi_macros.hpp>
  5125. NLOHMANN_JSON_NAMESPACE_BEGIN
  5126. /// @brief an internal type for a backed binary type
  5127. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5128. template<typename BinaryType>
  5129. class byte_container_with_subtype : public BinaryType
  5130. {
  5131. public:
  5132. using container_type = BinaryType;
  5133. using subtype_type = std::uint64_t;
  5134. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5135. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5136. : container_type()
  5137. {}
  5138. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5139. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5140. : container_type(b)
  5141. {}
  5142. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5143. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5144. : container_type(std::move(b))
  5145. {}
  5146. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5147. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5148. : container_type(b)
  5149. , m_subtype(subtype_)
  5150. , m_has_subtype(true)
  5151. {}
  5152. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5153. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5154. : container_type(std::move(b))
  5155. , m_subtype(subtype_)
  5156. , m_has_subtype(true)
  5157. {}
  5158. bool operator==(const byte_container_with_subtype& rhs) const
  5159. {
  5160. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5161. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5162. }
  5163. bool operator!=(const byte_container_with_subtype& rhs) const
  5164. {
  5165. return !(rhs == *this);
  5166. }
  5167. /// @brief sets the binary subtype
  5168. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5169. void set_subtype(subtype_type subtype_) noexcept
  5170. {
  5171. m_subtype = subtype_;
  5172. m_has_subtype = true;
  5173. }
  5174. /// @brief return the binary subtype
  5175. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5176. constexpr subtype_type subtype() const noexcept
  5177. {
  5178. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5179. }
  5180. /// @brief return whether the value has a subtype
  5181. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5182. constexpr bool has_subtype() const noexcept
  5183. {
  5184. return m_has_subtype;
  5185. }
  5186. /// @brief clears the binary subtype
  5187. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5188. void clear_subtype() noexcept
  5189. {
  5190. m_subtype = 0;
  5191. m_has_subtype = false;
  5192. }
  5193. private:
  5194. subtype_type m_subtype = 0;
  5195. bool m_has_subtype = false;
  5196. };
  5197. NLOHMANN_JSON_NAMESPACE_END
  5198. // #include <nlohmann/detail/conversions/from_json.hpp>
  5199. // #include <nlohmann/detail/conversions/to_json.hpp>
  5200. // #include <nlohmann/detail/exceptions.hpp>
  5201. // #include <nlohmann/detail/hash.hpp>
  5202. // __ _____ _____ _____
  5203. // __| | __| | | | JSON for Modern C++
  5204. // | | |__ | | | | | | version 3.11.3
  5205. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5206. //
  5207. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5208. // SPDX-License-Identifier: MIT
  5209. #include <cstdint> // uint8_t
  5210. #include <cstddef> // size_t
  5211. #include <functional> // hash
  5212. // #include <nlohmann/detail/abi_macros.hpp>
  5213. // #include <nlohmann/detail/value_t.hpp>
  5214. NLOHMANN_JSON_NAMESPACE_BEGIN
  5215. namespace detail
  5216. {
  5217. // boost::hash_combine
  5218. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5219. {
  5220. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5221. return seed;
  5222. }
  5223. /*!
  5224. @brief hash a JSON value
  5225. The hash function tries to rely on std::hash where possible. Furthermore, the
  5226. type of the JSON value is taken into account to have different hash values for
  5227. null, 0, 0U, and false, etc.
  5228. @tparam BasicJsonType basic_json specialization
  5229. @param j JSON value to hash
  5230. @return hash value of j
  5231. */
  5232. template<typename BasicJsonType>
  5233. std::size_t hash(const BasicJsonType& j)
  5234. {
  5235. using string_t = typename BasicJsonType::string_t;
  5236. using number_integer_t = typename BasicJsonType::number_integer_t;
  5237. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5238. using number_float_t = typename BasicJsonType::number_float_t;
  5239. const auto type = static_cast<std::size_t>(j.type());
  5240. switch (j.type())
  5241. {
  5242. case BasicJsonType::value_t::null:
  5243. case BasicJsonType::value_t::discarded:
  5244. {
  5245. return combine(type, 0);
  5246. }
  5247. case BasicJsonType::value_t::object:
  5248. {
  5249. auto seed = combine(type, j.size());
  5250. for (const auto& element : j.items())
  5251. {
  5252. const auto h = std::hash<string_t> {}(element.key());
  5253. seed = combine(seed, h);
  5254. seed = combine(seed, hash(element.value()));
  5255. }
  5256. return seed;
  5257. }
  5258. case BasicJsonType::value_t::array:
  5259. {
  5260. auto seed = combine(type, j.size());
  5261. for (const auto& element : j)
  5262. {
  5263. seed = combine(seed, hash(element));
  5264. }
  5265. return seed;
  5266. }
  5267. case BasicJsonType::value_t::string:
  5268. {
  5269. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5270. return combine(type, h);
  5271. }
  5272. case BasicJsonType::value_t::boolean:
  5273. {
  5274. const auto h = std::hash<bool> {}(j.template get<bool>());
  5275. return combine(type, h);
  5276. }
  5277. case BasicJsonType::value_t::number_integer:
  5278. {
  5279. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5280. return combine(type, h);
  5281. }
  5282. case BasicJsonType::value_t::number_unsigned:
  5283. {
  5284. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5285. return combine(type, h);
  5286. }
  5287. case BasicJsonType::value_t::number_float:
  5288. {
  5289. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5290. return combine(type, h);
  5291. }
  5292. case BasicJsonType::value_t::binary:
  5293. {
  5294. auto seed = combine(type, j.get_binary().size());
  5295. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5296. seed = combine(seed, h);
  5297. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5298. for (const auto byte : j.get_binary())
  5299. {
  5300. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5301. }
  5302. return seed;
  5303. }
  5304. default: // LCOV_EXCL_LINE
  5305. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5306. return 0; // LCOV_EXCL_LINE
  5307. }
  5308. }
  5309. } // namespace detail
  5310. NLOHMANN_JSON_NAMESPACE_END
  5311. // #include <nlohmann/detail/input/binary_reader.hpp>
  5312. // __ _____ _____ _____
  5313. // __| | __| | | | JSON for Modern C++
  5314. // | | |__ | | | | | | version 3.11.3
  5315. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5316. //
  5317. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5318. // SPDX-License-Identifier: MIT
  5319. #include <algorithm> // generate_n
  5320. #include <array> // array
  5321. #include <cmath> // ldexp
  5322. #include <cstddef> // size_t
  5323. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5324. #include <cstdio> // snprintf
  5325. #include <cstring> // memcpy
  5326. #include <iterator> // back_inserter
  5327. #include <limits> // numeric_limits
  5328. #include <string> // char_traits, string
  5329. #include <utility> // make_pair, move
  5330. #include <vector> // vector
  5331. // #include <nlohmann/detail/exceptions.hpp>
  5332. // #include <nlohmann/detail/input/input_adapters.hpp>
  5333. // __ _____ _____ _____
  5334. // __| | __| | | | JSON for Modern C++
  5335. // | | |__ | | | | | | version 3.11.3
  5336. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5337. //
  5338. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5339. // SPDX-License-Identifier: MIT
  5340. #include <array> // array
  5341. #include <cstddef> // size_t
  5342. #include <cstring> // strlen
  5343. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5344. #include <memory> // shared_ptr, make_shared, addressof
  5345. #include <numeric> // accumulate
  5346. #include <string> // string, char_traits
  5347. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5348. #include <utility> // pair, declval
  5349. #ifndef JSON_NO_IO
  5350. #include <cstdio> // FILE *
  5351. #include <istream> // istream
  5352. #endif // JSON_NO_IO
  5353. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5354. // #include <nlohmann/detail/macro_scope.hpp>
  5355. // #include <nlohmann/detail/meta/type_traits.hpp>
  5356. NLOHMANN_JSON_NAMESPACE_BEGIN
  5357. namespace detail
  5358. {
  5359. /// the supported input formats
  5360. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5361. ////////////////////
  5362. // input adapters //
  5363. ////////////////////
  5364. #ifndef JSON_NO_IO
  5365. /*!
  5366. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5367. buffer. This adapter is a very low level adapter.
  5368. */
  5369. class file_input_adapter
  5370. {
  5371. public:
  5372. using char_type = char;
  5373. JSON_HEDLEY_NON_NULL(2)
  5374. explicit file_input_adapter(std::FILE* f) noexcept
  5375. : m_file(f)
  5376. {
  5377. JSON_ASSERT(m_file != nullptr);
  5378. }
  5379. // make class move-only
  5380. file_input_adapter(const file_input_adapter&) = delete;
  5381. file_input_adapter(file_input_adapter&&) noexcept = default;
  5382. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5383. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5384. ~file_input_adapter() = default;
  5385. std::char_traits<char>::int_type get_character() noexcept
  5386. {
  5387. return std::fgetc(m_file);
  5388. }
  5389. private:
  5390. /// the file pointer to read from
  5391. std::FILE* m_file;
  5392. };
  5393. /*!
  5394. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5395. beginning of input. Does not support changing the underlying std::streambuf
  5396. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5397. subsequent use of standard std::istream operations to process any input
  5398. characters following those used in parsing the JSON input. Clears the
  5399. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5400. subsequent call for input from the std::istream.
  5401. */
  5402. class input_stream_adapter
  5403. {
  5404. public:
  5405. using char_type = char;
  5406. ~input_stream_adapter()
  5407. {
  5408. // clear stream flags; we use underlying streambuf I/O, do not
  5409. // maintain ifstream flags, except eof
  5410. if (is != nullptr)
  5411. {
  5412. is->clear(is->rdstate() & std::ios::eofbit);
  5413. }
  5414. }
  5415. explicit input_stream_adapter(std::istream& i)
  5416. : is(&i), sb(i.rdbuf())
  5417. {}
  5418. // delete because of pointer members
  5419. input_stream_adapter(const input_stream_adapter&) = delete;
  5420. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5421. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5422. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5423. : is(rhs.is), sb(rhs.sb)
  5424. {
  5425. rhs.is = nullptr;
  5426. rhs.sb = nullptr;
  5427. }
  5428. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5429. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5430. // end up as the same value, e.g. 0xFFFFFFFF.
  5431. std::char_traits<char>::int_type get_character()
  5432. {
  5433. auto res = sb->sbumpc();
  5434. // set eof manually, as we don't use the istream interface.
  5435. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5436. {
  5437. is->clear(is->rdstate() | std::ios::eofbit);
  5438. }
  5439. return res;
  5440. }
  5441. private:
  5442. /// the associated input stream
  5443. std::istream* is = nullptr;
  5444. std::streambuf* sb = nullptr;
  5445. };
  5446. #endif // JSON_NO_IO
  5447. // General-purpose iterator-based adapter. It might not be as fast as
  5448. // theoretically possible for some containers, but it is extremely versatile.
  5449. template<typename IteratorType>
  5450. class iterator_input_adapter
  5451. {
  5452. public:
  5453. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5454. iterator_input_adapter(IteratorType first, IteratorType last)
  5455. : current(std::move(first)), end(std::move(last))
  5456. {}
  5457. typename char_traits<char_type>::int_type get_character()
  5458. {
  5459. if (JSON_HEDLEY_LIKELY(current != end))
  5460. {
  5461. auto result = char_traits<char_type>::to_int_type(*current);
  5462. std::advance(current, 1);
  5463. return result;
  5464. }
  5465. return char_traits<char_type>::eof();
  5466. }
  5467. private:
  5468. IteratorType current;
  5469. IteratorType end;
  5470. template<typename BaseInputAdapter, size_t T>
  5471. friend struct wide_string_input_helper;
  5472. bool empty() const
  5473. {
  5474. return current == end;
  5475. }
  5476. };
  5477. template<typename BaseInputAdapter, size_t T>
  5478. struct wide_string_input_helper;
  5479. template<typename BaseInputAdapter>
  5480. struct wide_string_input_helper<BaseInputAdapter, 4>
  5481. {
  5482. // UTF-32
  5483. static void fill_buffer(BaseInputAdapter& input,
  5484. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5485. size_t& utf8_bytes_index,
  5486. size_t& utf8_bytes_filled)
  5487. {
  5488. utf8_bytes_index = 0;
  5489. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5490. {
  5491. utf8_bytes[0] = std::char_traits<char>::eof();
  5492. utf8_bytes_filled = 1;
  5493. }
  5494. else
  5495. {
  5496. // get the current character
  5497. const auto wc = input.get_character();
  5498. // UTF-32 to UTF-8 encoding
  5499. if (wc < 0x80)
  5500. {
  5501. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5502. utf8_bytes_filled = 1;
  5503. }
  5504. else if (wc <= 0x7FF)
  5505. {
  5506. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5507. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5508. utf8_bytes_filled = 2;
  5509. }
  5510. else if (wc <= 0xFFFF)
  5511. {
  5512. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5513. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5514. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5515. utf8_bytes_filled = 3;
  5516. }
  5517. else if (wc <= 0x10FFFF)
  5518. {
  5519. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5520. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5521. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5522. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5523. utf8_bytes_filled = 4;
  5524. }
  5525. else
  5526. {
  5527. // unknown character
  5528. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5529. utf8_bytes_filled = 1;
  5530. }
  5531. }
  5532. }
  5533. };
  5534. template<typename BaseInputAdapter>
  5535. struct wide_string_input_helper<BaseInputAdapter, 2>
  5536. {
  5537. // UTF-16
  5538. static void fill_buffer(BaseInputAdapter& input,
  5539. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5540. size_t& utf8_bytes_index,
  5541. size_t& utf8_bytes_filled)
  5542. {
  5543. utf8_bytes_index = 0;
  5544. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5545. {
  5546. utf8_bytes[0] = std::char_traits<char>::eof();
  5547. utf8_bytes_filled = 1;
  5548. }
  5549. else
  5550. {
  5551. // get the current character
  5552. const auto wc = input.get_character();
  5553. // UTF-16 to UTF-8 encoding
  5554. if (wc < 0x80)
  5555. {
  5556. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5557. utf8_bytes_filled = 1;
  5558. }
  5559. else if (wc <= 0x7FF)
  5560. {
  5561. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5562. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5563. utf8_bytes_filled = 2;
  5564. }
  5565. else if (0xD800 > wc || wc >= 0xE000)
  5566. {
  5567. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5568. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5569. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5570. utf8_bytes_filled = 3;
  5571. }
  5572. else
  5573. {
  5574. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5575. {
  5576. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5577. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5578. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5579. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5580. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5581. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5582. utf8_bytes_filled = 4;
  5583. }
  5584. else
  5585. {
  5586. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5587. utf8_bytes_filled = 1;
  5588. }
  5589. }
  5590. }
  5591. }
  5592. };
  5593. // Wraps another input adapter to convert wide character types into individual bytes.
  5594. template<typename BaseInputAdapter, typename WideCharType>
  5595. class wide_string_input_adapter
  5596. {
  5597. public:
  5598. using char_type = char;
  5599. wide_string_input_adapter(BaseInputAdapter base)
  5600. : base_adapter(base) {}
  5601. typename std::char_traits<char>::int_type get_character() noexcept
  5602. {
  5603. // check if buffer needs to be filled
  5604. if (utf8_bytes_index == utf8_bytes_filled)
  5605. {
  5606. fill_buffer<sizeof(WideCharType)>();
  5607. JSON_ASSERT(utf8_bytes_filled > 0);
  5608. JSON_ASSERT(utf8_bytes_index == 0);
  5609. }
  5610. // use buffer
  5611. JSON_ASSERT(utf8_bytes_filled > 0);
  5612. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5613. return utf8_bytes[utf8_bytes_index++];
  5614. }
  5615. private:
  5616. BaseInputAdapter base_adapter;
  5617. template<size_t T>
  5618. void fill_buffer()
  5619. {
  5620. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5621. }
  5622. /// a buffer for UTF-8 bytes
  5623. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5624. /// index to the utf8_codes array for the next valid byte
  5625. std::size_t utf8_bytes_index = 0;
  5626. /// number of valid bytes in the utf8_codes array
  5627. std::size_t utf8_bytes_filled = 0;
  5628. };
  5629. template<typename IteratorType, typename Enable = void>
  5630. struct iterator_input_adapter_factory
  5631. {
  5632. using iterator_type = IteratorType;
  5633. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5634. using adapter_type = iterator_input_adapter<iterator_type>;
  5635. static adapter_type create(IteratorType first, IteratorType last)
  5636. {
  5637. return adapter_type(std::move(first), std::move(last));
  5638. }
  5639. };
  5640. template<typename T>
  5641. struct is_iterator_of_multibyte
  5642. {
  5643. using value_type = typename std::iterator_traits<T>::value_type;
  5644. enum
  5645. {
  5646. value = sizeof(value_type) > 1
  5647. };
  5648. };
  5649. template<typename IteratorType>
  5650. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5651. {
  5652. using iterator_type = IteratorType;
  5653. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5654. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5655. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5656. static adapter_type create(IteratorType first, IteratorType last)
  5657. {
  5658. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5659. }
  5660. };
  5661. // General purpose iterator-based input
  5662. template<typename IteratorType>
  5663. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5664. {
  5665. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5666. return factory_type::create(first, last);
  5667. }
  5668. // Convenience shorthand from container to iterator
  5669. // Enables ADL on begin(container) and end(container)
  5670. // Encloses the using declarations in namespace for not to leak them to outside scope
  5671. namespace container_input_adapter_factory_impl
  5672. {
  5673. using std::begin;
  5674. using std::end;
  5675. template<typename ContainerType, typename Enable = void>
  5676. struct container_input_adapter_factory {};
  5677. template<typename ContainerType>
  5678. struct container_input_adapter_factory< ContainerType,
  5679. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5680. {
  5681. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5682. static adapter_type create(const ContainerType& container)
  5683. {
  5684. return input_adapter(begin(container), end(container));
  5685. }
  5686. };
  5687. } // namespace container_input_adapter_factory_impl
  5688. template<typename ContainerType>
  5689. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5690. {
  5691. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5692. }
  5693. #ifndef JSON_NO_IO
  5694. // Special cases with fast paths
  5695. inline file_input_adapter input_adapter(std::FILE* file)
  5696. {
  5697. return file_input_adapter(file);
  5698. }
  5699. inline input_stream_adapter input_adapter(std::istream& stream)
  5700. {
  5701. return input_stream_adapter(stream);
  5702. }
  5703. inline input_stream_adapter input_adapter(std::istream&& stream)
  5704. {
  5705. return input_stream_adapter(stream);
  5706. }
  5707. #endif // JSON_NO_IO
  5708. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5709. // Null-delimited strings, and the like.
  5710. template < typename CharT,
  5711. typename std::enable_if <
  5712. std::is_pointer<CharT>::value&&
  5713. !std::is_array<CharT>::value&&
  5714. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5715. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5716. int >::type = 0 >
  5717. contiguous_bytes_input_adapter input_adapter(CharT b)
  5718. {
  5719. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5720. const auto* ptr = reinterpret_cast<const char*>(b);
  5721. return input_adapter(ptr, ptr + length);
  5722. }
  5723. template<typename T, std::size_t N>
  5724. 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)
  5725. {
  5726. return input_adapter(array, array + N);
  5727. }
  5728. // This class only handles inputs of input_buffer_adapter type.
  5729. // It's required so that expressions like {ptr, len} can be implicitly cast
  5730. // to the correct adapter.
  5731. class span_input_adapter
  5732. {
  5733. public:
  5734. template < typename CharT,
  5735. typename std::enable_if <
  5736. std::is_pointer<CharT>::value&&
  5737. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5738. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5739. int >::type = 0 >
  5740. span_input_adapter(CharT b, std::size_t l)
  5741. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5742. template<class IteratorType,
  5743. typename std::enable_if<
  5744. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5745. int>::type = 0>
  5746. span_input_adapter(IteratorType first, IteratorType last)
  5747. : ia(input_adapter(first, last)) {}
  5748. contiguous_bytes_input_adapter&& get()
  5749. {
  5750. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5751. }
  5752. private:
  5753. contiguous_bytes_input_adapter ia;
  5754. };
  5755. } // namespace detail
  5756. NLOHMANN_JSON_NAMESPACE_END
  5757. // #include <nlohmann/detail/input/json_sax.hpp>
  5758. // __ _____ _____ _____
  5759. // __| | __| | | | JSON for Modern C++
  5760. // | | |__ | | | | | | version 3.11.3
  5761. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5762. //
  5763. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5764. // SPDX-License-Identifier: MIT
  5765. #include <cstddef>
  5766. #include <string> // string
  5767. #include <utility> // move
  5768. #include <vector> // vector
  5769. // #include <nlohmann/detail/exceptions.hpp>
  5770. // #include <nlohmann/detail/macro_scope.hpp>
  5771. // #include <nlohmann/detail/string_concat.hpp>
  5772. NLOHMANN_JSON_NAMESPACE_BEGIN
  5773. /*!
  5774. @brief SAX interface
  5775. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5776. Each function is called in different situations while the input is parsed. The
  5777. boolean return value informs the parser whether to continue processing the
  5778. input.
  5779. */
  5780. template<typename BasicJsonType>
  5781. struct json_sax
  5782. {
  5783. using number_integer_t = typename BasicJsonType::number_integer_t;
  5784. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5785. using number_float_t = typename BasicJsonType::number_float_t;
  5786. using string_t = typename BasicJsonType::string_t;
  5787. using binary_t = typename BasicJsonType::binary_t;
  5788. /*!
  5789. @brief a null value was read
  5790. @return whether parsing should proceed
  5791. */
  5792. virtual bool null() = 0;
  5793. /*!
  5794. @brief a boolean value was read
  5795. @param[in] val boolean value
  5796. @return whether parsing should proceed
  5797. */
  5798. virtual bool boolean(bool val) = 0;
  5799. /*!
  5800. @brief an integer number was read
  5801. @param[in] val integer value
  5802. @return whether parsing should proceed
  5803. */
  5804. virtual bool number_integer(number_integer_t val) = 0;
  5805. /*!
  5806. @brief an unsigned integer number was read
  5807. @param[in] val unsigned integer value
  5808. @return whether parsing should proceed
  5809. */
  5810. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5811. /*!
  5812. @brief a floating-point number was read
  5813. @param[in] val floating-point value
  5814. @param[in] s raw token value
  5815. @return whether parsing should proceed
  5816. */
  5817. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5818. /*!
  5819. @brief a string value was read
  5820. @param[in] val string value
  5821. @return whether parsing should proceed
  5822. @note It is safe to move the passed string value.
  5823. */
  5824. virtual bool string(string_t& val) = 0;
  5825. /*!
  5826. @brief a binary value was read
  5827. @param[in] val binary value
  5828. @return whether parsing should proceed
  5829. @note It is safe to move the passed binary value.
  5830. */
  5831. virtual bool binary(binary_t& val) = 0;
  5832. /*!
  5833. @brief the beginning of an object was read
  5834. @param[in] elements number of object elements or -1 if unknown
  5835. @return whether parsing should proceed
  5836. @note binary formats may report the number of elements
  5837. */
  5838. virtual bool start_object(std::size_t elements) = 0;
  5839. /*!
  5840. @brief an object key was read
  5841. @param[in] val object key
  5842. @return whether parsing should proceed
  5843. @note It is safe to move the passed string.
  5844. */
  5845. virtual bool key(string_t& val) = 0;
  5846. /*!
  5847. @brief the end of an object was read
  5848. @return whether parsing should proceed
  5849. */
  5850. virtual bool end_object() = 0;
  5851. /*!
  5852. @brief the beginning of an array was read
  5853. @param[in] elements number of array elements or -1 if unknown
  5854. @return whether parsing should proceed
  5855. @note binary formats may report the number of elements
  5856. */
  5857. virtual bool start_array(std::size_t elements) = 0;
  5858. /*!
  5859. @brief the end of an array was read
  5860. @return whether parsing should proceed
  5861. */
  5862. virtual bool end_array() = 0;
  5863. /*!
  5864. @brief a parse error occurred
  5865. @param[in] position the position in the input where the error occurs
  5866. @param[in] last_token the last read token
  5867. @param[in] ex an exception object describing the error
  5868. @return whether parsing should proceed (must return false)
  5869. */
  5870. virtual bool parse_error(std::size_t position,
  5871. const std::string& last_token,
  5872. const detail::exception& ex) = 0;
  5873. json_sax() = default;
  5874. json_sax(const json_sax&) = default;
  5875. json_sax(json_sax&&) noexcept = default;
  5876. json_sax& operator=(const json_sax&) = default;
  5877. json_sax& operator=(json_sax&&) noexcept = default;
  5878. virtual ~json_sax() = default;
  5879. };
  5880. namespace detail
  5881. {
  5882. /*!
  5883. @brief SAX implementation to create a JSON value from SAX events
  5884. This class implements the @ref json_sax interface and processes the SAX events
  5885. to create a JSON value which makes it basically a DOM parser. The structure or
  5886. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5887. a pointer to the respective array or object for each recursion depth.
  5888. After successful parsing, the value that is passed by reference to the
  5889. constructor contains the parsed value.
  5890. @tparam BasicJsonType the JSON type
  5891. */
  5892. template<typename BasicJsonType>
  5893. class json_sax_dom_parser
  5894. {
  5895. public:
  5896. using number_integer_t = typename BasicJsonType::number_integer_t;
  5897. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5898. using number_float_t = typename BasicJsonType::number_float_t;
  5899. using string_t = typename BasicJsonType::string_t;
  5900. using binary_t = typename BasicJsonType::binary_t;
  5901. /*!
  5902. @param[in,out] r reference to a JSON value that is manipulated while
  5903. parsing
  5904. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5905. */
  5906. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5907. : root(r), allow_exceptions(allow_exceptions_)
  5908. {}
  5909. // make class move-only
  5910. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5911. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5912. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5913. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5914. ~json_sax_dom_parser() = default;
  5915. bool null()
  5916. {
  5917. handle_value(nullptr);
  5918. return true;
  5919. }
  5920. bool boolean(bool val)
  5921. {
  5922. handle_value(val);
  5923. return true;
  5924. }
  5925. bool number_integer(number_integer_t val)
  5926. {
  5927. handle_value(val);
  5928. return true;
  5929. }
  5930. bool number_unsigned(number_unsigned_t val)
  5931. {
  5932. handle_value(val);
  5933. return true;
  5934. }
  5935. bool number_float(number_float_t val, const string_t& /*unused*/)
  5936. {
  5937. handle_value(val);
  5938. return true;
  5939. }
  5940. bool string(string_t& val)
  5941. {
  5942. handle_value(val);
  5943. return true;
  5944. }
  5945. bool binary(binary_t& val)
  5946. {
  5947. handle_value(std::move(val));
  5948. return true;
  5949. }
  5950. bool start_object(std::size_t len)
  5951. {
  5952. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5953. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5954. {
  5955. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5956. }
  5957. return true;
  5958. }
  5959. bool key(string_t& val)
  5960. {
  5961. JSON_ASSERT(!ref_stack.empty());
  5962. JSON_ASSERT(ref_stack.back()->is_object());
  5963. // add null at given key and store the reference for later
  5964. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
  5965. return true;
  5966. }
  5967. bool end_object()
  5968. {
  5969. JSON_ASSERT(!ref_stack.empty());
  5970. JSON_ASSERT(ref_stack.back()->is_object());
  5971. ref_stack.back()->set_parents();
  5972. ref_stack.pop_back();
  5973. return true;
  5974. }
  5975. bool start_array(std::size_t len)
  5976. {
  5977. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5978. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5979. {
  5980. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5981. }
  5982. return true;
  5983. }
  5984. bool end_array()
  5985. {
  5986. JSON_ASSERT(!ref_stack.empty());
  5987. JSON_ASSERT(ref_stack.back()->is_array());
  5988. ref_stack.back()->set_parents();
  5989. ref_stack.pop_back();
  5990. return true;
  5991. }
  5992. template<class Exception>
  5993. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5994. const Exception& ex)
  5995. {
  5996. errored = true;
  5997. static_cast<void>(ex);
  5998. if (allow_exceptions)
  5999. {
  6000. JSON_THROW(ex);
  6001. }
  6002. return false;
  6003. }
  6004. constexpr bool is_errored() const
  6005. {
  6006. return errored;
  6007. }
  6008. private:
  6009. /*!
  6010. @invariant If the ref stack is empty, then the passed value will be the new
  6011. root.
  6012. @invariant If the ref stack contains a value, then it is an array or an
  6013. object to which we can add elements
  6014. */
  6015. template<typename Value>
  6016. JSON_HEDLEY_RETURNS_NON_NULL
  6017. BasicJsonType* handle_value(Value&& v)
  6018. {
  6019. if (ref_stack.empty())
  6020. {
  6021. root = BasicJsonType(std::forward<Value>(v));
  6022. return &root;
  6023. }
  6024. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6025. if (ref_stack.back()->is_array())
  6026. {
  6027. ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
  6028. return &(ref_stack.back()->m_data.m_value.array->back());
  6029. }
  6030. JSON_ASSERT(ref_stack.back()->is_object());
  6031. JSON_ASSERT(object_element);
  6032. *object_element = BasicJsonType(std::forward<Value>(v));
  6033. return object_element;
  6034. }
  6035. /// the parsed JSON value
  6036. BasicJsonType& root;
  6037. /// stack to model hierarchy of values
  6038. std::vector<BasicJsonType*> ref_stack {};
  6039. /// helper to hold the reference for the next object element
  6040. BasicJsonType* object_element = nullptr;
  6041. /// whether a syntax error occurred
  6042. bool errored = false;
  6043. /// whether to throw exceptions in case of errors
  6044. const bool allow_exceptions = true;
  6045. };
  6046. template<typename BasicJsonType>
  6047. class json_sax_dom_callback_parser
  6048. {
  6049. public:
  6050. using number_integer_t = typename BasicJsonType::number_integer_t;
  6051. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6052. using number_float_t = typename BasicJsonType::number_float_t;
  6053. using string_t = typename BasicJsonType::string_t;
  6054. using binary_t = typename BasicJsonType::binary_t;
  6055. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  6056. using parse_event_t = typename BasicJsonType::parse_event_t;
  6057. json_sax_dom_callback_parser(BasicJsonType& r,
  6058. const parser_callback_t cb,
  6059. const bool allow_exceptions_ = true)
  6060. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  6061. {
  6062. keep_stack.push_back(true);
  6063. }
  6064. // make class move-only
  6065. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  6066. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6067. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  6068. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6069. ~json_sax_dom_callback_parser() = default;
  6070. bool null()
  6071. {
  6072. handle_value(nullptr);
  6073. return true;
  6074. }
  6075. bool boolean(bool val)
  6076. {
  6077. handle_value(val);
  6078. return true;
  6079. }
  6080. bool number_integer(number_integer_t val)
  6081. {
  6082. handle_value(val);
  6083. return true;
  6084. }
  6085. bool number_unsigned(number_unsigned_t val)
  6086. {
  6087. handle_value(val);
  6088. return true;
  6089. }
  6090. bool number_float(number_float_t val, const string_t& /*unused*/)
  6091. {
  6092. handle_value(val);
  6093. return true;
  6094. }
  6095. bool string(string_t& val)
  6096. {
  6097. handle_value(val);
  6098. return true;
  6099. }
  6100. bool binary(binary_t& val)
  6101. {
  6102. handle_value(std::move(val));
  6103. return true;
  6104. }
  6105. bool start_object(std::size_t len)
  6106. {
  6107. // check callback for object start
  6108. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  6109. keep_stack.push_back(keep);
  6110. auto val = handle_value(BasicJsonType::value_t::object, true);
  6111. ref_stack.push_back(val.second);
  6112. // check object limit
  6113. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6114. {
  6115. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  6116. }
  6117. return true;
  6118. }
  6119. bool key(string_t& val)
  6120. {
  6121. BasicJsonType k = BasicJsonType(val);
  6122. // check callback for key
  6123. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  6124. key_keep_stack.push_back(keep);
  6125. // add discarded value at given key and store the reference for later
  6126. if (keep && ref_stack.back())
  6127. {
  6128. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
  6129. }
  6130. return true;
  6131. }
  6132. bool end_object()
  6133. {
  6134. if (ref_stack.back())
  6135. {
  6136. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  6137. {
  6138. // discard object
  6139. *ref_stack.back() = discarded;
  6140. }
  6141. else
  6142. {
  6143. ref_stack.back()->set_parents();
  6144. }
  6145. }
  6146. JSON_ASSERT(!ref_stack.empty());
  6147. JSON_ASSERT(!keep_stack.empty());
  6148. ref_stack.pop_back();
  6149. keep_stack.pop_back();
  6150. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  6151. {
  6152. // remove discarded value
  6153. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  6154. {
  6155. if (it->is_discarded())
  6156. {
  6157. ref_stack.back()->erase(it);
  6158. break;
  6159. }
  6160. }
  6161. }
  6162. return true;
  6163. }
  6164. bool start_array(std::size_t len)
  6165. {
  6166. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  6167. keep_stack.push_back(keep);
  6168. auto val = handle_value(BasicJsonType::value_t::array, true);
  6169. ref_stack.push_back(val.second);
  6170. // check array limit
  6171. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6172. {
  6173. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  6174. }
  6175. return true;
  6176. }
  6177. bool end_array()
  6178. {
  6179. bool keep = true;
  6180. if (ref_stack.back())
  6181. {
  6182. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  6183. if (keep)
  6184. {
  6185. ref_stack.back()->set_parents();
  6186. }
  6187. else
  6188. {
  6189. // discard array
  6190. *ref_stack.back() = discarded;
  6191. }
  6192. }
  6193. JSON_ASSERT(!ref_stack.empty());
  6194. JSON_ASSERT(!keep_stack.empty());
  6195. ref_stack.pop_back();
  6196. keep_stack.pop_back();
  6197. // remove discarded value
  6198. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  6199. {
  6200. ref_stack.back()->m_data.m_value.array->pop_back();
  6201. }
  6202. return true;
  6203. }
  6204. template<class Exception>
  6205. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  6206. const Exception& ex)
  6207. {
  6208. errored = true;
  6209. static_cast<void>(ex);
  6210. if (allow_exceptions)
  6211. {
  6212. JSON_THROW(ex);
  6213. }
  6214. return false;
  6215. }
  6216. constexpr bool is_errored() const
  6217. {
  6218. return errored;
  6219. }
  6220. private:
  6221. /*!
  6222. @param[in] v value to add to the JSON value we build during parsing
  6223. @param[in] skip_callback whether we should skip calling the callback
  6224. function; this is required after start_array() and
  6225. start_object() SAX events, because otherwise we would call the
  6226. callback function with an empty array or object, respectively.
  6227. @invariant If the ref stack is empty, then the passed value will be the new
  6228. root.
  6229. @invariant If the ref stack contains a value, then it is an array or an
  6230. object to which we can add elements
  6231. @return pair of boolean (whether value should be kept) and pointer (to the
  6232. passed value in the ref_stack hierarchy; nullptr if not kept)
  6233. */
  6234. template<typename Value>
  6235. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  6236. {
  6237. JSON_ASSERT(!keep_stack.empty());
  6238. // do not handle this value if we know it would be added to a discarded
  6239. // container
  6240. if (!keep_stack.back())
  6241. {
  6242. return {false, nullptr};
  6243. }
  6244. // create value
  6245. auto value = BasicJsonType(std::forward<Value>(v));
  6246. // check callback
  6247. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  6248. // do not handle this value if we just learnt it shall be discarded
  6249. if (!keep)
  6250. {
  6251. return {false, nullptr};
  6252. }
  6253. if (ref_stack.empty())
  6254. {
  6255. root = std::move(value);
  6256. return {true, & root};
  6257. }
  6258. // skip this value if we already decided to skip the parent
  6259. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  6260. if (!ref_stack.back())
  6261. {
  6262. return {false, nullptr};
  6263. }
  6264. // we now only expect arrays and objects
  6265. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6266. // array
  6267. if (ref_stack.back()->is_array())
  6268. {
  6269. ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
  6270. return {true, & (ref_stack.back()->m_data.m_value.array->back())};
  6271. }
  6272. // object
  6273. JSON_ASSERT(ref_stack.back()->is_object());
  6274. // check if we should store an element for the current key
  6275. JSON_ASSERT(!key_keep_stack.empty());
  6276. const bool store_element = key_keep_stack.back();
  6277. key_keep_stack.pop_back();
  6278. if (!store_element)
  6279. {
  6280. return {false, nullptr};
  6281. }
  6282. JSON_ASSERT(object_element);
  6283. *object_element = std::move(value);
  6284. return {true, object_element};
  6285. }
  6286. /// the parsed JSON value
  6287. BasicJsonType& root;
  6288. /// stack to model hierarchy of values
  6289. std::vector<BasicJsonType*> ref_stack {};
  6290. /// stack to manage which values to keep
  6291. std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
  6292. /// stack to manage which object keys to keep
  6293. std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
  6294. /// helper to hold the reference for the next object element
  6295. BasicJsonType* object_element = nullptr;
  6296. /// whether a syntax error occurred
  6297. bool errored = false;
  6298. /// callback function
  6299. const parser_callback_t callback = nullptr;
  6300. /// whether to throw exceptions in case of errors
  6301. const bool allow_exceptions = true;
  6302. /// a discarded value for the callback
  6303. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  6304. };
  6305. template<typename BasicJsonType>
  6306. class json_sax_acceptor
  6307. {
  6308. public:
  6309. using number_integer_t = typename BasicJsonType::number_integer_t;
  6310. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6311. using number_float_t = typename BasicJsonType::number_float_t;
  6312. using string_t = typename BasicJsonType::string_t;
  6313. using binary_t = typename BasicJsonType::binary_t;
  6314. bool null()
  6315. {
  6316. return true;
  6317. }
  6318. bool boolean(bool /*unused*/)
  6319. {
  6320. return true;
  6321. }
  6322. bool number_integer(number_integer_t /*unused*/)
  6323. {
  6324. return true;
  6325. }
  6326. bool number_unsigned(number_unsigned_t /*unused*/)
  6327. {
  6328. return true;
  6329. }
  6330. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  6331. {
  6332. return true;
  6333. }
  6334. bool string(string_t& /*unused*/)
  6335. {
  6336. return true;
  6337. }
  6338. bool binary(binary_t& /*unused*/)
  6339. {
  6340. return true;
  6341. }
  6342. bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6343. {
  6344. return true;
  6345. }
  6346. bool key(string_t& /*unused*/)
  6347. {
  6348. return true;
  6349. }
  6350. bool end_object()
  6351. {
  6352. return true;
  6353. }
  6354. bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6355. {
  6356. return true;
  6357. }
  6358. bool end_array()
  6359. {
  6360. return true;
  6361. }
  6362. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  6363. {
  6364. return false;
  6365. }
  6366. };
  6367. } // namespace detail
  6368. NLOHMANN_JSON_NAMESPACE_END
  6369. // #include <nlohmann/detail/input/lexer.hpp>
  6370. // __ _____ _____ _____
  6371. // __| | __| | | | JSON for Modern C++
  6372. // | | |__ | | | | | | version 3.11.3
  6373. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6374. //
  6375. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  6376. // SPDX-License-Identifier: MIT
  6377. #include <array> // array
  6378. #include <clocale> // localeconv
  6379. #include <cstddef> // size_t
  6380. #include <cstdio> // snprintf
  6381. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6382. #include <initializer_list> // initializer_list
  6383. #include <string> // char_traits, string
  6384. #include <utility> // move
  6385. #include <vector> // vector
  6386. // #include <nlohmann/detail/input/input_adapters.hpp>
  6387. // #include <nlohmann/detail/input/position_t.hpp>
  6388. // #include <nlohmann/detail/macro_scope.hpp>
  6389. // #include <nlohmann/detail/meta/type_traits.hpp>
  6390. NLOHMANN_JSON_NAMESPACE_BEGIN
  6391. namespace detail
  6392. {
  6393. ///////////
  6394. // lexer //
  6395. ///////////
  6396. template<typename BasicJsonType>
  6397. class lexer_base
  6398. {
  6399. public:
  6400. /// token types for the parser
  6401. enum class token_type
  6402. {
  6403. uninitialized, ///< indicating the scanner is uninitialized
  6404. literal_true, ///< the `true` literal
  6405. literal_false, ///< the `false` literal
  6406. literal_null, ///< the `null` literal
  6407. value_string, ///< a string -- use get_string() for actual value
  6408. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6409. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6410. value_float, ///< an floating point number -- use get_number_float() for actual value
  6411. begin_array, ///< the character for array begin `[`
  6412. begin_object, ///< the character for object begin `{`
  6413. end_array, ///< the character for array end `]`
  6414. end_object, ///< the character for object end `}`
  6415. name_separator, ///< the name separator `:`
  6416. value_separator, ///< the value separator `,`
  6417. parse_error, ///< indicating a parse error
  6418. end_of_input, ///< indicating the end of the input buffer
  6419. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6420. };
  6421. /// return name of values of type token_type (only used for errors)
  6422. JSON_HEDLEY_RETURNS_NON_NULL
  6423. JSON_HEDLEY_CONST
  6424. static const char* token_type_name(const token_type t) noexcept
  6425. {
  6426. switch (t)
  6427. {
  6428. case token_type::uninitialized:
  6429. return "<uninitialized>";
  6430. case token_type::literal_true:
  6431. return "true literal";
  6432. case token_type::literal_false:
  6433. return "false literal";
  6434. case token_type::literal_null:
  6435. return "null literal";
  6436. case token_type::value_string:
  6437. return "string literal";
  6438. case token_type::value_unsigned:
  6439. case token_type::value_integer:
  6440. case token_type::value_float:
  6441. return "number literal";
  6442. case token_type::begin_array:
  6443. return "'['";
  6444. case token_type::begin_object:
  6445. return "'{'";
  6446. case token_type::end_array:
  6447. return "']'";
  6448. case token_type::end_object:
  6449. return "'}'";
  6450. case token_type::name_separator:
  6451. return "':'";
  6452. case token_type::value_separator:
  6453. return "','";
  6454. case token_type::parse_error:
  6455. return "<parse error>";
  6456. case token_type::end_of_input:
  6457. return "end of input";
  6458. case token_type::literal_or_value:
  6459. return "'[', '{', or a literal";
  6460. // LCOV_EXCL_START
  6461. default: // catch non-enum values
  6462. return "unknown token";
  6463. // LCOV_EXCL_STOP
  6464. }
  6465. }
  6466. };
  6467. /*!
  6468. @brief lexical analysis
  6469. This class organizes the lexical analysis during JSON deserialization.
  6470. */
  6471. template<typename BasicJsonType, typename InputAdapterType>
  6472. class lexer : public lexer_base<BasicJsonType>
  6473. {
  6474. using number_integer_t = typename BasicJsonType::number_integer_t;
  6475. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6476. using number_float_t = typename BasicJsonType::number_float_t;
  6477. using string_t = typename BasicJsonType::string_t;
  6478. using char_type = typename InputAdapterType::char_type;
  6479. using char_int_type = typename char_traits<char_type>::int_type;
  6480. public:
  6481. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6482. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6483. : ia(std::move(adapter))
  6484. , ignore_comments(ignore_comments_)
  6485. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6486. {}
  6487. // delete because of pointer members
  6488. lexer(const lexer&) = delete;
  6489. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6490. lexer& operator=(lexer&) = delete;
  6491. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6492. ~lexer() = default;
  6493. private:
  6494. /////////////////////
  6495. // locales
  6496. /////////////////////
  6497. /// return the locale-dependent decimal point
  6498. JSON_HEDLEY_PURE
  6499. static char get_decimal_point() noexcept
  6500. {
  6501. const auto* loc = localeconv();
  6502. JSON_ASSERT(loc != nullptr);
  6503. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6504. }
  6505. /////////////////////
  6506. // scan functions
  6507. /////////////////////
  6508. /*!
  6509. @brief get codepoint from 4 hex characters following `\u`
  6510. For input "\u c1 c2 c3 c4" the codepoint is:
  6511. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6512. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6513. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6514. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6515. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6516. between the ASCII value of the character and the desired integer value.
  6517. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6518. non-hex character)
  6519. */
  6520. int get_codepoint()
  6521. {
  6522. // this function only makes sense after reading `\u`
  6523. JSON_ASSERT(current == 'u');
  6524. int codepoint = 0;
  6525. const auto factors = { 12u, 8u, 4u, 0u };
  6526. for (const auto factor : factors)
  6527. {
  6528. get();
  6529. if (current >= '0' && current <= '9')
  6530. {
  6531. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6532. }
  6533. else if (current >= 'A' && current <= 'F')
  6534. {
  6535. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6536. }
  6537. else if (current >= 'a' && current <= 'f')
  6538. {
  6539. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6540. }
  6541. else
  6542. {
  6543. return -1;
  6544. }
  6545. }
  6546. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6547. return codepoint;
  6548. }
  6549. /*!
  6550. @brief check if the next byte(s) are inside a given range
  6551. Adds the current byte and, for each passed range, reads a new byte and
  6552. checks if it is inside the range. If a violation was detected, set up an
  6553. error message and return false. Otherwise, return true.
  6554. @param[in] ranges list of integers; interpreted as list of pairs of
  6555. inclusive lower and upper bound, respectively
  6556. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6557. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6558. @return true if and only if no range violation was detected
  6559. */
  6560. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6561. {
  6562. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6563. add(current);
  6564. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6565. {
  6566. get();
  6567. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
  6568. {
  6569. add(current);
  6570. }
  6571. else
  6572. {
  6573. error_message = "invalid string: ill-formed UTF-8 byte";
  6574. return false;
  6575. }
  6576. }
  6577. return true;
  6578. }
  6579. /*!
  6580. @brief scan a string literal
  6581. This function scans a string according to Sect. 7 of RFC 8259. While
  6582. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6583. function returns successfully, token_buffer is *not* null-terminated (as it
  6584. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6585. string.
  6586. @return token_type::value_string if string could be successfully scanned,
  6587. token_type::parse_error otherwise
  6588. @note In case of errors, variable error_message contains a textual
  6589. description.
  6590. */
  6591. token_type scan_string()
  6592. {
  6593. // reset token_buffer (ignore opening quote)
  6594. reset();
  6595. // we entered the function by reading an open quote
  6596. JSON_ASSERT(current == '\"');
  6597. while (true)
  6598. {
  6599. // get next character
  6600. switch (get())
  6601. {
  6602. // end of file while parsing string
  6603. case char_traits<char_type>::eof():
  6604. {
  6605. error_message = "invalid string: missing closing quote";
  6606. return token_type::parse_error;
  6607. }
  6608. // closing quote
  6609. case '\"':
  6610. {
  6611. return token_type::value_string;
  6612. }
  6613. // escapes
  6614. case '\\':
  6615. {
  6616. switch (get())
  6617. {
  6618. // quotation mark
  6619. case '\"':
  6620. add('\"');
  6621. break;
  6622. // reverse solidus
  6623. case '\\':
  6624. add('\\');
  6625. break;
  6626. // solidus
  6627. case '/':
  6628. add('/');
  6629. break;
  6630. // backspace
  6631. case 'b':
  6632. add('\b');
  6633. break;
  6634. // form feed
  6635. case 'f':
  6636. add('\f');
  6637. break;
  6638. // line feed
  6639. case 'n':
  6640. add('\n');
  6641. break;
  6642. // carriage return
  6643. case 'r':
  6644. add('\r');
  6645. break;
  6646. // tab
  6647. case 't':
  6648. add('\t');
  6649. break;
  6650. // unicode escapes
  6651. case 'u':
  6652. {
  6653. const int codepoint1 = get_codepoint();
  6654. int codepoint = codepoint1; // start with codepoint1
  6655. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6656. {
  6657. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6658. return token_type::parse_error;
  6659. }
  6660. // check if code point is a high surrogate
  6661. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6662. {
  6663. // expect next \uxxxx entry
  6664. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6665. {
  6666. const int codepoint2 = get_codepoint();
  6667. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6668. {
  6669. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6670. return token_type::parse_error;
  6671. }
  6672. // check if codepoint2 is a low surrogate
  6673. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6674. {
  6675. // overwrite codepoint
  6676. codepoint = static_cast<int>(
  6677. // high surrogate occupies the most significant 22 bits
  6678. (static_cast<unsigned int>(codepoint1) << 10u)
  6679. // low surrogate occupies the least significant 15 bits
  6680. + static_cast<unsigned int>(codepoint2)
  6681. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6682. // in the result, so we have to subtract with:
  6683. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6684. - 0x35FDC00u);
  6685. }
  6686. else
  6687. {
  6688. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6689. return token_type::parse_error;
  6690. }
  6691. }
  6692. else
  6693. {
  6694. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6695. return token_type::parse_error;
  6696. }
  6697. }
  6698. else
  6699. {
  6700. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6701. {
  6702. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6703. return token_type::parse_error;
  6704. }
  6705. }
  6706. // result of the above calculation yields a proper codepoint
  6707. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6708. // translate codepoint into bytes
  6709. if (codepoint < 0x80)
  6710. {
  6711. // 1-byte characters: 0xxxxxxx (ASCII)
  6712. add(static_cast<char_int_type>(codepoint));
  6713. }
  6714. else if (codepoint <= 0x7FF)
  6715. {
  6716. // 2-byte characters: 110xxxxx 10xxxxxx
  6717. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6718. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6719. }
  6720. else if (codepoint <= 0xFFFF)
  6721. {
  6722. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6723. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6724. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6725. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6726. }
  6727. else
  6728. {
  6729. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6730. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6731. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6732. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6733. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6734. }
  6735. break;
  6736. }
  6737. // other characters after escape
  6738. default:
  6739. error_message = "invalid string: forbidden character after backslash";
  6740. return token_type::parse_error;
  6741. }
  6742. break;
  6743. }
  6744. // invalid control characters
  6745. case 0x00:
  6746. {
  6747. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6748. return token_type::parse_error;
  6749. }
  6750. case 0x01:
  6751. {
  6752. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6753. return token_type::parse_error;
  6754. }
  6755. case 0x02:
  6756. {
  6757. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6758. return token_type::parse_error;
  6759. }
  6760. case 0x03:
  6761. {
  6762. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6763. return token_type::parse_error;
  6764. }
  6765. case 0x04:
  6766. {
  6767. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6768. return token_type::parse_error;
  6769. }
  6770. case 0x05:
  6771. {
  6772. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6773. return token_type::parse_error;
  6774. }
  6775. case 0x06:
  6776. {
  6777. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6778. return token_type::parse_error;
  6779. }
  6780. case 0x07:
  6781. {
  6782. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6783. return token_type::parse_error;
  6784. }
  6785. case 0x08:
  6786. {
  6787. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6788. return token_type::parse_error;
  6789. }
  6790. case 0x09:
  6791. {
  6792. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6793. return token_type::parse_error;
  6794. }
  6795. case 0x0A:
  6796. {
  6797. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6798. return token_type::parse_error;
  6799. }
  6800. case 0x0B:
  6801. {
  6802. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6803. return token_type::parse_error;
  6804. }
  6805. case 0x0C:
  6806. {
  6807. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6808. return token_type::parse_error;
  6809. }
  6810. case 0x0D:
  6811. {
  6812. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6813. return token_type::parse_error;
  6814. }
  6815. case 0x0E:
  6816. {
  6817. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6818. return token_type::parse_error;
  6819. }
  6820. case 0x0F:
  6821. {
  6822. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6823. return token_type::parse_error;
  6824. }
  6825. case 0x10:
  6826. {
  6827. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6828. return token_type::parse_error;
  6829. }
  6830. case 0x11:
  6831. {
  6832. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6833. return token_type::parse_error;
  6834. }
  6835. case 0x12:
  6836. {
  6837. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6838. return token_type::parse_error;
  6839. }
  6840. case 0x13:
  6841. {
  6842. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6843. return token_type::parse_error;
  6844. }
  6845. case 0x14:
  6846. {
  6847. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6848. return token_type::parse_error;
  6849. }
  6850. case 0x15:
  6851. {
  6852. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6853. return token_type::parse_error;
  6854. }
  6855. case 0x16:
  6856. {
  6857. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6858. return token_type::parse_error;
  6859. }
  6860. case 0x17:
  6861. {
  6862. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6863. return token_type::parse_error;
  6864. }
  6865. case 0x18:
  6866. {
  6867. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6868. return token_type::parse_error;
  6869. }
  6870. case 0x19:
  6871. {
  6872. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6873. return token_type::parse_error;
  6874. }
  6875. case 0x1A:
  6876. {
  6877. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6878. return token_type::parse_error;
  6879. }
  6880. case 0x1B:
  6881. {
  6882. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6883. return token_type::parse_error;
  6884. }
  6885. case 0x1C:
  6886. {
  6887. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6888. return token_type::parse_error;
  6889. }
  6890. case 0x1D:
  6891. {
  6892. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6893. return token_type::parse_error;
  6894. }
  6895. case 0x1E:
  6896. {
  6897. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6898. return token_type::parse_error;
  6899. }
  6900. case 0x1F:
  6901. {
  6902. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6903. return token_type::parse_error;
  6904. }
  6905. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6906. case 0x20:
  6907. case 0x21:
  6908. case 0x23:
  6909. case 0x24:
  6910. case 0x25:
  6911. case 0x26:
  6912. case 0x27:
  6913. case 0x28:
  6914. case 0x29:
  6915. case 0x2A:
  6916. case 0x2B:
  6917. case 0x2C:
  6918. case 0x2D:
  6919. case 0x2E:
  6920. case 0x2F:
  6921. case 0x30:
  6922. case 0x31:
  6923. case 0x32:
  6924. case 0x33:
  6925. case 0x34:
  6926. case 0x35:
  6927. case 0x36:
  6928. case 0x37:
  6929. case 0x38:
  6930. case 0x39:
  6931. case 0x3A:
  6932. case 0x3B:
  6933. case 0x3C:
  6934. case 0x3D:
  6935. case 0x3E:
  6936. case 0x3F:
  6937. case 0x40:
  6938. case 0x41:
  6939. case 0x42:
  6940. case 0x43:
  6941. case 0x44:
  6942. case 0x45:
  6943. case 0x46:
  6944. case 0x47:
  6945. case 0x48:
  6946. case 0x49:
  6947. case 0x4A:
  6948. case 0x4B:
  6949. case 0x4C:
  6950. case 0x4D:
  6951. case 0x4E:
  6952. case 0x4F:
  6953. case 0x50:
  6954. case 0x51:
  6955. case 0x52:
  6956. case 0x53:
  6957. case 0x54:
  6958. case 0x55:
  6959. case 0x56:
  6960. case 0x57:
  6961. case 0x58:
  6962. case 0x59:
  6963. case 0x5A:
  6964. case 0x5B:
  6965. case 0x5D:
  6966. case 0x5E:
  6967. case 0x5F:
  6968. case 0x60:
  6969. case 0x61:
  6970. case 0x62:
  6971. case 0x63:
  6972. case 0x64:
  6973. case 0x65:
  6974. case 0x66:
  6975. case 0x67:
  6976. case 0x68:
  6977. case 0x69:
  6978. case 0x6A:
  6979. case 0x6B:
  6980. case 0x6C:
  6981. case 0x6D:
  6982. case 0x6E:
  6983. case 0x6F:
  6984. case 0x70:
  6985. case 0x71:
  6986. case 0x72:
  6987. case 0x73:
  6988. case 0x74:
  6989. case 0x75:
  6990. case 0x76:
  6991. case 0x77:
  6992. case 0x78:
  6993. case 0x79:
  6994. case 0x7A:
  6995. case 0x7B:
  6996. case 0x7C:
  6997. case 0x7D:
  6998. case 0x7E:
  6999. case 0x7F:
  7000. {
  7001. add(current);
  7002. break;
  7003. }
  7004. // U+0080..U+07FF: bytes C2..DF 80..BF
  7005. case 0xC2:
  7006. case 0xC3:
  7007. case 0xC4:
  7008. case 0xC5:
  7009. case 0xC6:
  7010. case 0xC7:
  7011. case 0xC8:
  7012. case 0xC9:
  7013. case 0xCA:
  7014. case 0xCB:
  7015. case 0xCC:
  7016. case 0xCD:
  7017. case 0xCE:
  7018. case 0xCF:
  7019. case 0xD0:
  7020. case 0xD1:
  7021. case 0xD2:
  7022. case 0xD3:
  7023. case 0xD4:
  7024. case 0xD5:
  7025. case 0xD6:
  7026. case 0xD7:
  7027. case 0xD8:
  7028. case 0xD9:
  7029. case 0xDA:
  7030. case 0xDB:
  7031. case 0xDC:
  7032. case 0xDD:
  7033. case 0xDE:
  7034. case 0xDF:
  7035. {
  7036. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  7037. {
  7038. return token_type::parse_error;
  7039. }
  7040. break;
  7041. }
  7042. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  7043. case 0xE0:
  7044. {
  7045. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  7046. {
  7047. return token_type::parse_error;
  7048. }
  7049. break;
  7050. }
  7051. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  7052. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  7053. case 0xE1:
  7054. case 0xE2:
  7055. case 0xE3:
  7056. case 0xE4:
  7057. case 0xE5:
  7058. case 0xE6:
  7059. case 0xE7:
  7060. case 0xE8:
  7061. case 0xE9:
  7062. case 0xEA:
  7063. case 0xEB:
  7064. case 0xEC:
  7065. case 0xEE:
  7066. case 0xEF:
  7067. {
  7068. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  7069. {
  7070. return token_type::parse_error;
  7071. }
  7072. break;
  7073. }
  7074. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  7075. case 0xED:
  7076. {
  7077. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  7078. {
  7079. return token_type::parse_error;
  7080. }
  7081. break;
  7082. }
  7083. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  7084. case 0xF0:
  7085. {
  7086. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7087. {
  7088. return token_type::parse_error;
  7089. }
  7090. break;
  7091. }
  7092. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  7093. case 0xF1:
  7094. case 0xF2:
  7095. case 0xF3:
  7096. {
  7097. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7098. {
  7099. return token_type::parse_error;
  7100. }
  7101. break;
  7102. }
  7103. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  7104. case 0xF4:
  7105. {
  7106. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  7107. {
  7108. return token_type::parse_error;
  7109. }
  7110. break;
  7111. }
  7112. // remaining bytes (80..C1 and F5..FF) are ill-formed
  7113. default:
  7114. {
  7115. error_message = "invalid string: ill-formed UTF-8 byte";
  7116. return token_type::parse_error;
  7117. }
  7118. }
  7119. }
  7120. }
  7121. /*!
  7122. * @brief scan a comment
  7123. * @return whether comment could be scanned successfully
  7124. */
  7125. bool scan_comment()
  7126. {
  7127. switch (get())
  7128. {
  7129. // single-line comments skip input until a newline or EOF is read
  7130. case '/':
  7131. {
  7132. while (true)
  7133. {
  7134. switch (get())
  7135. {
  7136. case '\n':
  7137. case '\r':
  7138. case char_traits<char_type>::eof():
  7139. case '\0':
  7140. return true;
  7141. default:
  7142. break;
  7143. }
  7144. }
  7145. }
  7146. // multi-line comments skip input until */ is read
  7147. case '*':
  7148. {
  7149. while (true)
  7150. {
  7151. switch (get())
  7152. {
  7153. case char_traits<char_type>::eof():
  7154. case '\0':
  7155. {
  7156. error_message = "invalid comment; missing closing '*/'";
  7157. return false;
  7158. }
  7159. case '*':
  7160. {
  7161. switch (get())
  7162. {
  7163. case '/':
  7164. return true;
  7165. default:
  7166. {
  7167. unget();
  7168. continue;
  7169. }
  7170. }
  7171. }
  7172. default:
  7173. continue;
  7174. }
  7175. }
  7176. }
  7177. // unexpected character after reading '/'
  7178. default:
  7179. {
  7180. error_message = "invalid comment; expecting '/' or '*' after '/'";
  7181. return false;
  7182. }
  7183. }
  7184. }
  7185. JSON_HEDLEY_NON_NULL(2)
  7186. static void strtof(float& f, const char* str, char** endptr) noexcept
  7187. {
  7188. f = std::strtof(str, endptr);
  7189. }
  7190. JSON_HEDLEY_NON_NULL(2)
  7191. static void strtof(double& f, const char* str, char** endptr) noexcept
  7192. {
  7193. f = std::strtod(str, endptr);
  7194. }
  7195. JSON_HEDLEY_NON_NULL(2)
  7196. static void strtof(long double& f, const char* str, char** endptr) noexcept
  7197. {
  7198. f = std::strtold(str, endptr);
  7199. }
  7200. /*!
  7201. @brief scan a number literal
  7202. This function scans a string according to Sect. 6 of RFC 8259.
  7203. The function is realized with a deterministic finite state machine derived
  7204. from the grammar described in RFC 8259. Starting in state "init", the
  7205. input is read and used to determined the next state. Only state "done"
  7206. accepts the number. State "error" is a trap state to model errors. In the
  7207. table below, "anything" means any character but the ones listed before.
  7208. state | 0 | 1-9 | e E | + | - | . | anything
  7209. ---------|----------|----------|----------|---------|---------|----------|-----------
  7210. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  7211. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  7212. zero | done | done | exponent | done | done | decimal1 | done
  7213. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  7214. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  7215. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  7216. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  7217. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  7218. any2 | any2 | any2 | done | done | done | done | done
  7219. The state machine is realized with one label per state (prefixed with
  7220. "scan_number_") and `goto` statements between them. The state machine
  7221. contains cycles, but any cycle can be left when EOF is read. Therefore,
  7222. the function is guaranteed to terminate.
  7223. During scanning, the read bytes are stored in token_buffer. This string is
  7224. then converted to a signed integer, an unsigned integer, or a
  7225. floating-point number.
  7226. @return token_type::value_unsigned, token_type::value_integer, or
  7227. token_type::value_float if number could be successfully scanned,
  7228. token_type::parse_error otherwise
  7229. @note The scanner is independent of the current locale. Internally, the
  7230. locale's decimal point is used instead of `.` to work with the
  7231. locale-dependent converters.
  7232. */
  7233. token_type scan_number() // lgtm [cpp/use-of-goto]
  7234. {
  7235. // reset token_buffer to store the number's bytes
  7236. reset();
  7237. // the type of the parsed number; initially set to unsigned; will be
  7238. // changed if minus sign, decimal point or exponent is read
  7239. token_type number_type = token_type::value_unsigned;
  7240. // state (init): we just found out we need to scan a number
  7241. switch (current)
  7242. {
  7243. case '-':
  7244. {
  7245. add(current);
  7246. goto scan_number_minus;
  7247. }
  7248. case '0':
  7249. {
  7250. add(current);
  7251. goto scan_number_zero;
  7252. }
  7253. case '1':
  7254. case '2':
  7255. case '3':
  7256. case '4':
  7257. case '5':
  7258. case '6':
  7259. case '7':
  7260. case '8':
  7261. case '9':
  7262. {
  7263. add(current);
  7264. goto scan_number_any1;
  7265. }
  7266. // all other characters are rejected outside scan_number()
  7267. default: // LCOV_EXCL_LINE
  7268. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7269. }
  7270. scan_number_minus:
  7271. // state: we just parsed a leading minus sign
  7272. number_type = token_type::value_integer;
  7273. switch (get())
  7274. {
  7275. case '0':
  7276. {
  7277. add(current);
  7278. goto scan_number_zero;
  7279. }
  7280. case '1':
  7281. case '2':
  7282. case '3':
  7283. case '4':
  7284. case '5':
  7285. case '6':
  7286. case '7':
  7287. case '8':
  7288. case '9':
  7289. {
  7290. add(current);
  7291. goto scan_number_any1;
  7292. }
  7293. default:
  7294. {
  7295. error_message = "invalid number; expected digit after '-'";
  7296. return token_type::parse_error;
  7297. }
  7298. }
  7299. scan_number_zero:
  7300. // state: we just parse a zero (maybe with a leading minus sign)
  7301. switch (get())
  7302. {
  7303. case '.':
  7304. {
  7305. add(decimal_point_char);
  7306. goto scan_number_decimal1;
  7307. }
  7308. case 'e':
  7309. case 'E':
  7310. {
  7311. add(current);
  7312. goto scan_number_exponent;
  7313. }
  7314. default:
  7315. goto scan_number_done;
  7316. }
  7317. scan_number_any1:
  7318. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7319. switch (get())
  7320. {
  7321. case '0':
  7322. case '1':
  7323. case '2':
  7324. case '3':
  7325. case '4':
  7326. case '5':
  7327. case '6':
  7328. case '7':
  7329. case '8':
  7330. case '9':
  7331. {
  7332. add(current);
  7333. goto scan_number_any1;
  7334. }
  7335. case '.':
  7336. {
  7337. add(decimal_point_char);
  7338. goto scan_number_decimal1;
  7339. }
  7340. case 'e':
  7341. case 'E':
  7342. {
  7343. add(current);
  7344. goto scan_number_exponent;
  7345. }
  7346. default:
  7347. goto scan_number_done;
  7348. }
  7349. scan_number_decimal1:
  7350. // state: we just parsed a decimal point
  7351. number_type = token_type::value_float;
  7352. switch (get())
  7353. {
  7354. case '0':
  7355. case '1':
  7356. case '2':
  7357. case '3':
  7358. case '4':
  7359. case '5':
  7360. case '6':
  7361. case '7':
  7362. case '8':
  7363. case '9':
  7364. {
  7365. add(current);
  7366. goto scan_number_decimal2;
  7367. }
  7368. default:
  7369. {
  7370. error_message = "invalid number; expected digit after '.'";
  7371. return token_type::parse_error;
  7372. }
  7373. }
  7374. scan_number_decimal2:
  7375. // we just parsed at least one number after a decimal point
  7376. switch (get())
  7377. {
  7378. case '0':
  7379. case '1':
  7380. case '2':
  7381. case '3':
  7382. case '4':
  7383. case '5':
  7384. case '6':
  7385. case '7':
  7386. case '8':
  7387. case '9':
  7388. {
  7389. add(current);
  7390. goto scan_number_decimal2;
  7391. }
  7392. case 'e':
  7393. case 'E':
  7394. {
  7395. add(current);
  7396. goto scan_number_exponent;
  7397. }
  7398. default:
  7399. goto scan_number_done;
  7400. }
  7401. scan_number_exponent:
  7402. // we just parsed an exponent
  7403. number_type = token_type::value_float;
  7404. switch (get())
  7405. {
  7406. case '+':
  7407. case '-':
  7408. {
  7409. add(current);
  7410. goto scan_number_sign;
  7411. }
  7412. case '0':
  7413. case '1':
  7414. case '2':
  7415. case '3':
  7416. case '4':
  7417. case '5':
  7418. case '6':
  7419. case '7':
  7420. case '8':
  7421. case '9':
  7422. {
  7423. add(current);
  7424. goto scan_number_any2;
  7425. }
  7426. default:
  7427. {
  7428. error_message =
  7429. "invalid number; expected '+', '-', or digit after exponent";
  7430. return token_type::parse_error;
  7431. }
  7432. }
  7433. scan_number_sign:
  7434. // we just parsed an exponent sign
  7435. switch (get())
  7436. {
  7437. case '0':
  7438. case '1':
  7439. case '2':
  7440. case '3':
  7441. case '4':
  7442. case '5':
  7443. case '6':
  7444. case '7':
  7445. case '8':
  7446. case '9':
  7447. {
  7448. add(current);
  7449. goto scan_number_any2;
  7450. }
  7451. default:
  7452. {
  7453. error_message = "invalid number; expected digit after exponent sign";
  7454. return token_type::parse_error;
  7455. }
  7456. }
  7457. scan_number_any2:
  7458. // we just parsed a number after the exponent or exponent sign
  7459. switch (get())
  7460. {
  7461. case '0':
  7462. case '1':
  7463. case '2':
  7464. case '3':
  7465. case '4':
  7466. case '5':
  7467. case '6':
  7468. case '7':
  7469. case '8':
  7470. case '9':
  7471. {
  7472. add(current);
  7473. goto scan_number_any2;
  7474. }
  7475. default:
  7476. goto scan_number_done;
  7477. }
  7478. scan_number_done:
  7479. // unget the character after the number (we only read it to know that
  7480. // we are done scanning a number)
  7481. unget();
  7482. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7483. errno = 0;
  7484. // try to parse integers first and fall back to floats
  7485. if (number_type == token_type::value_unsigned)
  7486. {
  7487. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7488. // we checked the number format before
  7489. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7490. if (errno == 0)
  7491. {
  7492. value_unsigned = static_cast<number_unsigned_t>(x);
  7493. if (value_unsigned == x)
  7494. {
  7495. return token_type::value_unsigned;
  7496. }
  7497. }
  7498. }
  7499. else if (number_type == token_type::value_integer)
  7500. {
  7501. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7502. // we checked the number format before
  7503. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7504. if (errno == 0)
  7505. {
  7506. value_integer = static_cast<number_integer_t>(x);
  7507. if (value_integer == x)
  7508. {
  7509. return token_type::value_integer;
  7510. }
  7511. }
  7512. }
  7513. // this code is reached if we parse a floating-point number or if an
  7514. // integer conversion above failed
  7515. strtof(value_float, token_buffer.data(), &endptr);
  7516. // we checked the number format before
  7517. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7518. return token_type::value_float;
  7519. }
  7520. /*!
  7521. @param[in] literal_text the literal text to expect
  7522. @param[in] length the length of the passed literal text
  7523. @param[in] return_type the token type to return on success
  7524. */
  7525. JSON_HEDLEY_NON_NULL(2)
  7526. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7527. token_type return_type)
  7528. {
  7529. JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7530. for (std::size_t i = 1; i < length; ++i)
  7531. {
  7532. if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7533. {
  7534. error_message = "invalid literal";
  7535. return token_type::parse_error;
  7536. }
  7537. }
  7538. return return_type;
  7539. }
  7540. /////////////////////
  7541. // input management
  7542. /////////////////////
  7543. /// reset token_buffer; current character is beginning of token
  7544. void reset() noexcept
  7545. {
  7546. token_buffer.clear();
  7547. token_string.clear();
  7548. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7549. }
  7550. /*
  7551. @brief get next character from the input
  7552. This function provides the interface to the used input adapter. It does
  7553. not throw in case the input reached EOF, but returns a
  7554. `char_traits<char>::eof()` in that case. Stores the scanned characters
  7555. for use in error messages.
  7556. @return character read from the input
  7557. */
  7558. char_int_type get()
  7559. {
  7560. ++position.chars_read_total;
  7561. ++position.chars_read_current_line;
  7562. if (next_unget)
  7563. {
  7564. // just reset the next_unget variable and work with current
  7565. next_unget = false;
  7566. }
  7567. else
  7568. {
  7569. current = ia.get_character();
  7570. }
  7571. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7572. {
  7573. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7574. }
  7575. if (current == '\n')
  7576. {
  7577. ++position.lines_read;
  7578. position.chars_read_current_line = 0;
  7579. }
  7580. return current;
  7581. }
  7582. /*!
  7583. @brief unget current character (read it again on next get)
  7584. We implement unget by setting variable next_unget to true. The input is not
  7585. changed - we just simulate ungetting by modifying chars_read_total,
  7586. chars_read_current_line, and token_string. The next call to get() will
  7587. behave as if the unget character is read again.
  7588. */
  7589. void unget()
  7590. {
  7591. next_unget = true;
  7592. --position.chars_read_total;
  7593. // in case we "unget" a newline, we have to also decrement the lines_read
  7594. if (position.chars_read_current_line == 0)
  7595. {
  7596. if (position.lines_read > 0)
  7597. {
  7598. --position.lines_read;
  7599. }
  7600. }
  7601. else
  7602. {
  7603. --position.chars_read_current_line;
  7604. }
  7605. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7606. {
  7607. JSON_ASSERT(!token_string.empty());
  7608. token_string.pop_back();
  7609. }
  7610. }
  7611. /// add a character to token_buffer
  7612. void add(char_int_type c)
  7613. {
  7614. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7615. }
  7616. public:
  7617. /////////////////////
  7618. // value getters
  7619. /////////////////////
  7620. /// return integer value
  7621. constexpr number_integer_t get_number_integer() const noexcept
  7622. {
  7623. return value_integer;
  7624. }
  7625. /// return unsigned integer value
  7626. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7627. {
  7628. return value_unsigned;
  7629. }
  7630. /// return floating-point value
  7631. constexpr number_float_t get_number_float() const noexcept
  7632. {
  7633. return value_float;
  7634. }
  7635. /// return current string value (implicitly resets the token; useful only once)
  7636. string_t& get_string()
  7637. {
  7638. return token_buffer;
  7639. }
  7640. /////////////////////
  7641. // diagnostics
  7642. /////////////////////
  7643. /// return position of last read token
  7644. constexpr position_t get_position() const noexcept
  7645. {
  7646. return position;
  7647. }
  7648. /// return the last read token (for errors only). Will never contain EOF
  7649. /// (an arbitrary value that is not a valid char value, often -1), because
  7650. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7651. std::string get_token_string() const
  7652. {
  7653. // escape control characters
  7654. std::string result;
  7655. for (const auto c : token_string)
  7656. {
  7657. if (static_cast<unsigned char>(c) <= '\x1F')
  7658. {
  7659. // escape control characters
  7660. std::array<char, 9> cs{{}};
  7661. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7662. result += cs.data();
  7663. }
  7664. else
  7665. {
  7666. // add character as is
  7667. result.push_back(static_cast<std::string::value_type>(c));
  7668. }
  7669. }
  7670. return result;
  7671. }
  7672. /// return syntax error message
  7673. JSON_HEDLEY_RETURNS_NON_NULL
  7674. constexpr const char* get_error_message() const noexcept
  7675. {
  7676. return error_message;
  7677. }
  7678. /////////////////////
  7679. // actual scanner
  7680. /////////////////////
  7681. /*!
  7682. @brief skip the UTF-8 byte order mark
  7683. @return true iff there is no BOM or the correct BOM has been skipped
  7684. */
  7685. bool skip_bom()
  7686. {
  7687. if (get() == 0xEF)
  7688. {
  7689. // check if we completely parse the BOM
  7690. return get() == 0xBB && get() == 0xBF;
  7691. }
  7692. // the first character is not the beginning of the BOM; unget it to
  7693. // process is later
  7694. unget();
  7695. return true;
  7696. }
  7697. void skip_whitespace()
  7698. {
  7699. do
  7700. {
  7701. get();
  7702. }
  7703. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7704. }
  7705. token_type scan()
  7706. {
  7707. // initially, skip the BOM
  7708. if (position.chars_read_total == 0 && !skip_bom())
  7709. {
  7710. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7711. return token_type::parse_error;
  7712. }
  7713. // read next character and ignore whitespace
  7714. skip_whitespace();
  7715. // ignore comments
  7716. while (ignore_comments && current == '/')
  7717. {
  7718. if (!scan_comment())
  7719. {
  7720. return token_type::parse_error;
  7721. }
  7722. // skip following whitespace
  7723. skip_whitespace();
  7724. }
  7725. switch (current)
  7726. {
  7727. // structural characters
  7728. case '[':
  7729. return token_type::begin_array;
  7730. case ']':
  7731. return token_type::end_array;
  7732. case '{':
  7733. return token_type::begin_object;
  7734. case '}':
  7735. return token_type::end_object;
  7736. case ':':
  7737. return token_type::name_separator;
  7738. case ',':
  7739. return token_type::value_separator;
  7740. // literals
  7741. case 't':
  7742. {
  7743. 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')}};
  7744. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7745. }
  7746. case 'f':
  7747. {
  7748. 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')}};
  7749. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7750. }
  7751. case 'n':
  7752. {
  7753. 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')}};
  7754. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7755. }
  7756. // string
  7757. case '\"':
  7758. return scan_string();
  7759. // number
  7760. case '-':
  7761. case '0':
  7762. case '1':
  7763. case '2':
  7764. case '3':
  7765. case '4':
  7766. case '5':
  7767. case '6':
  7768. case '7':
  7769. case '8':
  7770. case '9':
  7771. return scan_number();
  7772. // end of input (the null byte is needed when parsing from
  7773. // string literals)
  7774. case '\0':
  7775. case char_traits<char_type>::eof():
  7776. return token_type::end_of_input;
  7777. // error
  7778. default:
  7779. error_message = "invalid literal";
  7780. return token_type::parse_error;
  7781. }
  7782. }
  7783. private:
  7784. /// input adapter
  7785. InputAdapterType ia;
  7786. /// whether comments should be ignored (true) or signaled as errors (false)
  7787. const bool ignore_comments = false;
  7788. /// the current character
  7789. char_int_type current = char_traits<char_type>::eof();
  7790. /// whether the next get() call should just return current
  7791. bool next_unget = false;
  7792. /// the start position of the current token
  7793. position_t position {};
  7794. /// raw input token string (for error messages)
  7795. std::vector<char_type> token_string {};
  7796. /// buffer for variable-length tokens (numbers, strings)
  7797. string_t token_buffer {};
  7798. /// a description of occurred lexer errors
  7799. const char* error_message = "";
  7800. // number values
  7801. number_integer_t value_integer = 0;
  7802. number_unsigned_t value_unsigned = 0;
  7803. number_float_t value_float = 0;
  7804. /// the decimal point
  7805. const char_int_type decimal_point_char = '.';
  7806. };
  7807. } // namespace detail
  7808. NLOHMANN_JSON_NAMESPACE_END
  7809. // #include <nlohmann/detail/macro_scope.hpp>
  7810. // #include <nlohmann/detail/meta/is_sax.hpp>
  7811. // __ _____ _____ _____
  7812. // __| | __| | | | JSON for Modern C++
  7813. // | | |__ | | | | | | version 3.11.3
  7814. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  7815. //
  7816. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7817. // SPDX-License-Identifier: MIT
  7818. #include <cstdint> // size_t
  7819. #include <utility> // declval
  7820. #include <string> // string
  7821. // #include <nlohmann/detail/abi_macros.hpp>
  7822. // #include <nlohmann/detail/meta/detected.hpp>
  7823. // #include <nlohmann/detail/meta/type_traits.hpp>
  7824. NLOHMANN_JSON_NAMESPACE_BEGIN
  7825. namespace detail
  7826. {
  7827. template<typename T>
  7828. using null_function_t = decltype(std::declval<T&>().null());
  7829. template<typename T>
  7830. using boolean_function_t =
  7831. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7832. template<typename T, typename Integer>
  7833. using number_integer_function_t =
  7834. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7835. template<typename T, typename Unsigned>
  7836. using number_unsigned_function_t =
  7837. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7838. template<typename T, typename Float, typename String>
  7839. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7840. std::declval<Float>(), std::declval<const String&>()));
  7841. template<typename T, typename String>
  7842. using string_function_t =
  7843. decltype(std::declval<T&>().string(std::declval<String&>()));
  7844. template<typename T, typename Binary>
  7845. using binary_function_t =
  7846. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7847. template<typename T>
  7848. using start_object_function_t =
  7849. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7850. template<typename T, typename String>
  7851. using key_function_t =
  7852. decltype(std::declval<T&>().key(std::declval<String&>()));
  7853. template<typename T>
  7854. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7855. template<typename T>
  7856. using start_array_function_t =
  7857. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7858. template<typename T>
  7859. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7860. template<typename T, typename Exception>
  7861. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7862. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7863. std::declval<const Exception&>()));
  7864. template<typename SAX, typename BasicJsonType>
  7865. struct is_sax
  7866. {
  7867. private:
  7868. static_assert(is_basic_json<BasicJsonType>::value,
  7869. "BasicJsonType must be of type basic_json<...>");
  7870. using number_integer_t = typename BasicJsonType::number_integer_t;
  7871. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7872. using number_float_t = typename BasicJsonType::number_float_t;
  7873. using string_t = typename BasicJsonType::string_t;
  7874. using binary_t = typename BasicJsonType::binary_t;
  7875. using exception_t = typename BasicJsonType::exception;
  7876. public:
  7877. static constexpr bool value =
  7878. is_detected_exact<bool, null_function_t, SAX>::value &&
  7879. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7880. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7881. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7882. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7883. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7884. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7885. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7886. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7887. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7888. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7889. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7890. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7891. };
  7892. template<typename SAX, typename BasicJsonType>
  7893. struct is_sax_static_asserts
  7894. {
  7895. private:
  7896. static_assert(is_basic_json<BasicJsonType>::value,
  7897. "BasicJsonType must be of type basic_json<...>");
  7898. using number_integer_t = typename BasicJsonType::number_integer_t;
  7899. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7900. using number_float_t = typename BasicJsonType::number_float_t;
  7901. using string_t = typename BasicJsonType::string_t;
  7902. using binary_t = typename BasicJsonType::binary_t;
  7903. using exception_t = typename BasicJsonType::exception;
  7904. public:
  7905. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7906. "Missing/invalid function: bool null()");
  7907. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7908. "Missing/invalid function: bool boolean(bool)");
  7909. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7910. "Missing/invalid function: bool boolean(bool)");
  7911. static_assert(
  7912. is_detected_exact<bool, number_integer_function_t, SAX,
  7913. number_integer_t>::value,
  7914. "Missing/invalid function: bool number_integer(number_integer_t)");
  7915. static_assert(
  7916. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7917. number_unsigned_t>::value,
  7918. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7919. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7920. number_float_t, string_t>::value,
  7921. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7922. static_assert(
  7923. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7924. "Missing/invalid function: bool string(string_t&)");
  7925. static_assert(
  7926. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7927. "Missing/invalid function: bool binary(binary_t&)");
  7928. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7929. "Missing/invalid function: bool start_object(std::size_t)");
  7930. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7931. "Missing/invalid function: bool key(string_t&)");
  7932. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7933. "Missing/invalid function: bool end_object()");
  7934. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7935. "Missing/invalid function: bool start_array(std::size_t)");
  7936. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7937. "Missing/invalid function: bool end_array()");
  7938. static_assert(
  7939. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7940. "Missing/invalid function: bool parse_error(std::size_t, const "
  7941. "std::string&, const exception&)");
  7942. };
  7943. } // namespace detail
  7944. NLOHMANN_JSON_NAMESPACE_END
  7945. // #include <nlohmann/detail/meta/type_traits.hpp>
  7946. // #include <nlohmann/detail/string_concat.hpp>
  7947. // #include <nlohmann/detail/value_t.hpp>
  7948. NLOHMANN_JSON_NAMESPACE_BEGIN
  7949. namespace detail
  7950. {
  7951. /// how to treat CBOR tags
  7952. enum class cbor_tag_handler_t
  7953. {
  7954. error, ///< throw a parse_error exception in case of a tag
  7955. ignore, ///< ignore tags
  7956. store ///< store tags as binary type
  7957. };
  7958. /*!
  7959. @brief determine system byte order
  7960. @return true if and only if system's byte order is little endian
  7961. @note from https://stackoverflow.com/a/1001328/266378
  7962. */
  7963. static inline bool little_endianness(int num = 1) noexcept
  7964. {
  7965. return *reinterpret_cast<char*>(&num) == 1;
  7966. }
  7967. ///////////////////
  7968. // binary reader //
  7969. ///////////////////
  7970. /*!
  7971. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7972. */
  7973. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7974. class binary_reader
  7975. {
  7976. using number_integer_t = typename BasicJsonType::number_integer_t;
  7977. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7978. using number_float_t = typename BasicJsonType::number_float_t;
  7979. using string_t = typename BasicJsonType::string_t;
  7980. using binary_t = typename BasicJsonType::binary_t;
  7981. using json_sax_t = SAX;
  7982. using char_type = typename InputAdapterType::char_type;
  7983. using char_int_type = typename char_traits<char_type>::int_type;
  7984. public:
  7985. /*!
  7986. @brief create a binary reader
  7987. @param[in] adapter input adapter to read from
  7988. */
  7989. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  7990. {
  7991. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7992. }
  7993. // make class move-only
  7994. binary_reader(const binary_reader&) = delete;
  7995. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7996. binary_reader& operator=(const binary_reader&) = delete;
  7997. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7998. ~binary_reader() = default;
  7999. /*!
  8000. @param[in] format the binary format to parse
  8001. @param[in] sax_ a SAX event processor
  8002. @param[in] strict whether to expect the input to be consumed completed
  8003. @param[in] tag_handler how to treat CBOR tags
  8004. @return whether parsing was successful
  8005. */
  8006. JSON_HEDLEY_NON_NULL(3)
  8007. bool sax_parse(const input_format_t format,
  8008. json_sax_t* sax_,
  8009. const bool strict = true,
  8010. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  8011. {
  8012. sax = sax_;
  8013. bool result = false;
  8014. switch (format)
  8015. {
  8016. case input_format_t::bson:
  8017. result = parse_bson_internal();
  8018. break;
  8019. case input_format_t::cbor:
  8020. result = parse_cbor_internal(true, tag_handler);
  8021. break;
  8022. case input_format_t::msgpack:
  8023. result = parse_msgpack_internal();
  8024. break;
  8025. case input_format_t::ubjson:
  8026. case input_format_t::bjdata:
  8027. result = parse_ubjson_internal();
  8028. break;
  8029. case input_format_t::json: // LCOV_EXCL_LINE
  8030. default: // LCOV_EXCL_LINE
  8031. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8032. }
  8033. // strict mode: next byte must be EOF
  8034. if (result && strict)
  8035. {
  8036. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  8037. {
  8038. get_ignore_noop();
  8039. }
  8040. else
  8041. {
  8042. get();
  8043. }
  8044. if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
  8045. {
  8046. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  8047. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  8048. }
  8049. }
  8050. return result;
  8051. }
  8052. private:
  8053. //////////
  8054. // BSON //
  8055. //////////
  8056. /*!
  8057. @brief Reads in a BSON-object and passes it to the SAX-parser.
  8058. @return whether a valid BSON-value was passed to the SAX parser
  8059. */
  8060. bool parse_bson_internal()
  8061. {
  8062. std::int32_t document_size{};
  8063. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8064. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  8065. {
  8066. return false;
  8067. }
  8068. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8069. {
  8070. return false;
  8071. }
  8072. return sax->end_object();
  8073. }
  8074. /*!
  8075. @brief Parses a C-style string from the BSON input.
  8076. @param[in,out] result A reference to the string variable where the read
  8077. string is to be stored.
  8078. @return `true` if the \x00-byte indicating the end of the string was
  8079. encountered before the EOF; false` indicates an unexpected EOF.
  8080. */
  8081. bool get_bson_cstr(string_t& result)
  8082. {
  8083. auto out = std::back_inserter(result);
  8084. while (true)
  8085. {
  8086. get();
  8087. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8088. {
  8089. return false;
  8090. }
  8091. if (current == 0x00)
  8092. {
  8093. return true;
  8094. }
  8095. *out++ = static_cast<typename string_t::value_type>(current);
  8096. }
  8097. }
  8098. /*!
  8099. @brief Parses a zero-terminated string of length @a len from the BSON
  8100. input.
  8101. @param[in] len The length (including the zero-byte at the end) of the
  8102. string to be read.
  8103. @param[in,out] result A reference to the string variable where the read
  8104. string is to be stored.
  8105. @tparam NumberType The type of the length @a len
  8106. @pre len >= 1
  8107. @return `true` if the string was successfully parsed
  8108. */
  8109. template<typename NumberType>
  8110. bool get_bson_string(const NumberType len, string_t& result)
  8111. {
  8112. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8113. {
  8114. auto last_token = get_token_string();
  8115. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8116. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8117. }
  8118. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
  8119. }
  8120. /*!
  8121. @brief Parses a byte array input of length @a len from the BSON input.
  8122. @param[in] len The length of the byte array to be read.
  8123. @param[in,out] result A reference to the binary variable where the read
  8124. array is to be stored.
  8125. @tparam NumberType The type of the length @a len
  8126. @pre len >= 0
  8127. @return `true` if the byte array was successfully parsed
  8128. */
  8129. template<typename NumberType>
  8130. bool get_bson_binary(const NumberType len, binary_t& result)
  8131. {
  8132. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8133. {
  8134. auto last_token = get_token_string();
  8135. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8136. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8137. }
  8138. // All BSON binary values have a subtype
  8139. std::uint8_t subtype{};
  8140. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8141. result.set_subtype(subtype);
  8142. return get_binary(input_format_t::bson, len, result);
  8143. }
  8144. /*!
  8145. @brief Read a BSON document element of the given @a element_type.
  8146. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8147. @param[in] element_type_parse_position The position in the input stream,
  8148. where the `element_type` was read.
  8149. @warning Not all BSON element types are supported yet. An unsupported
  8150. @a element_type will give rise to a parse_error.114:
  8151. Unsupported BSON record type 0x...
  8152. @return whether a valid BSON-object/array was passed to the SAX parser
  8153. */
  8154. bool parse_bson_element_internal(const char_int_type element_type,
  8155. const std::size_t element_type_parse_position)
  8156. {
  8157. switch (element_type)
  8158. {
  8159. case 0x01: // double
  8160. {
  8161. double number{};
  8162. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8163. }
  8164. case 0x02: // string
  8165. {
  8166. std::int32_t len{};
  8167. string_t value;
  8168. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8169. }
  8170. case 0x03: // object
  8171. {
  8172. return parse_bson_internal();
  8173. }
  8174. case 0x04: // array
  8175. {
  8176. return parse_bson_array();
  8177. }
  8178. case 0x05: // binary
  8179. {
  8180. std::int32_t len{};
  8181. binary_t value;
  8182. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8183. }
  8184. case 0x08: // boolean
  8185. {
  8186. return sax->boolean(get() != 0);
  8187. }
  8188. case 0x0A: // null
  8189. {
  8190. return sax->null();
  8191. }
  8192. case 0x10: // int32
  8193. {
  8194. std::int32_t value{};
  8195. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8196. }
  8197. case 0x12: // int64
  8198. {
  8199. std::int64_t value{};
  8200. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8201. }
  8202. default: // anything else not supported (yet)
  8203. {
  8204. std::array<char, 3> cr{{}};
  8205. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8206. const std::string cr_str{cr.data()};
  8207. return sax->parse_error(element_type_parse_position, cr_str,
  8208. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8209. }
  8210. }
  8211. }
  8212. /*!
  8213. @brief Read a BSON element list (as specified in the BSON-spec)
  8214. The same binary layout is used for objects and arrays, hence it must be
  8215. indicated with the argument @a is_array which one is expected
  8216. (true --> array, false --> object).
  8217. @param[in] is_array Determines if the element list being read is to be
  8218. treated as an object (@a is_array == false), or as an
  8219. array (@a is_array == true).
  8220. @return whether a valid BSON-object/array was passed to the SAX parser
  8221. */
  8222. bool parse_bson_element_list(const bool is_array)
  8223. {
  8224. string_t key;
  8225. while (auto element_type = get())
  8226. {
  8227. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8228. {
  8229. return false;
  8230. }
  8231. const std::size_t element_type_parse_position = chars_read;
  8232. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8233. {
  8234. return false;
  8235. }
  8236. if (!is_array && !sax->key(key))
  8237. {
  8238. return false;
  8239. }
  8240. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8241. {
  8242. return false;
  8243. }
  8244. // get_bson_cstr only appends
  8245. key.clear();
  8246. }
  8247. return true;
  8248. }
  8249. /*!
  8250. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8251. @return whether a valid BSON-array was passed to the SAX parser
  8252. */
  8253. bool parse_bson_array()
  8254. {
  8255. std::int32_t document_size{};
  8256. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8257. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  8258. {
  8259. return false;
  8260. }
  8261. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8262. {
  8263. return false;
  8264. }
  8265. return sax->end_array();
  8266. }
  8267. //////////
  8268. // CBOR //
  8269. //////////
  8270. /*!
  8271. @param[in] get_char whether a new character should be retrieved from the
  8272. input (true) or whether the last read character should
  8273. be considered instead (false)
  8274. @param[in] tag_handler how CBOR tags should be treated
  8275. @return whether a valid CBOR value was passed to the SAX parser
  8276. */
  8277. bool parse_cbor_internal(const bool get_char,
  8278. const cbor_tag_handler_t tag_handler)
  8279. {
  8280. switch (get_char ? get() : current)
  8281. {
  8282. // EOF
  8283. case char_traits<char_type>::eof():
  8284. return unexpect_eof(input_format_t::cbor, "value");
  8285. // Integer 0x00..0x17 (0..23)
  8286. case 0x00:
  8287. case 0x01:
  8288. case 0x02:
  8289. case 0x03:
  8290. case 0x04:
  8291. case 0x05:
  8292. case 0x06:
  8293. case 0x07:
  8294. case 0x08:
  8295. case 0x09:
  8296. case 0x0A:
  8297. case 0x0B:
  8298. case 0x0C:
  8299. case 0x0D:
  8300. case 0x0E:
  8301. case 0x0F:
  8302. case 0x10:
  8303. case 0x11:
  8304. case 0x12:
  8305. case 0x13:
  8306. case 0x14:
  8307. case 0x15:
  8308. case 0x16:
  8309. case 0x17:
  8310. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8311. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  8312. {
  8313. std::uint8_t number{};
  8314. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8315. }
  8316. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  8317. {
  8318. std::uint16_t number{};
  8319. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8320. }
  8321. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  8322. {
  8323. std::uint32_t number{};
  8324. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8325. }
  8326. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  8327. {
  8328. std::uint64_t number{};
  8329. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8330. }
  8331. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  8332. case 0x20:
  8333. case 0x21:
  8334. case 0x22:
  8335. case 0x23:
  8336. case 0x24:
  8337. case 0x25:
  8338. case 0x26:
  8339. case 0x27:
  8340. case 0x28:
  8341. case 0x29:
  8342. case 0x2A:
  8343. case 0x2B:
  8344. case 0x2C:
  8345. case 0x2D:
  8346. case 0x2E:
  8347. case 0x2F:
  8348. case 0x30:
  8349. case 0x31:
  8350. case 0x32:
  8351. case 0x33:
  8352. case 0x34:
  8353. case 0x35:
  8354. case 0x36:
  8355. case 0x37:
  8356. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8357. case 0x38: // Negative integer (one-byte uint8_t follows)
  8358. {
  8359. std::uint8_t number{};
  8360. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8361. }
  8362. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8363. {
  8364. std::uint16_t number{};
  8365. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8366. }
  8367. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8368. {
  8369. std::uint32_t number{};
  8370. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8371. }
  8372. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8373. {
  8374. std::uint64_t number{};
  8375. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8376. - static_cast<number_integer_t>(number));
  8377. }
  8378. // Binary data (0x00..0x17 bytes follow)
  8379. case 0x40:
  8380. case 0x41:
  8381. case 0x42:
  8382. case 0x43:
  8383. case 0x44:
  8384. case 0x45:
  8385. case 0x46:
  8386. case 0x47:
  8387. case 0x48:
  8388. case 0x49:
  8389. case 0x4A:
  8390. case 0x4B:
  8391. case 0x4C:
  8392. case 0x4D:
  8393. case 0x4E:
  8394. case 0x4F:
  8395. case 0x50:
  8396. case 0x51:
  8397. case 0x52:
  8398. case 0x53:
  8399. case 0x54:
  8400. case 0x55:
  8401. case 0x56:
  8402. case 0x57:
  8403. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8404. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8405. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8406. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8407. case 0x5F: // Binary data (indefinite length)
  8408. {
  8409. binary_t b;
  8410. return get_cbor_binary(b) && sax->binary(b);
  8411. }
  8412. // UTF-8 string (0x00..0x17 bytes follow)
  8413. case 0x60:
  8414. case 0x61:
  8415. case 0x62:
  8416. case 0x63:
  8417. case 0x64:
  8418. case 0x65:
  8419. case 0x66:
  8420. case 0x67:
  8421. case 0x68:
  8422. case 0x69:
  8423. case 0x6A:
  8424. case 0x6B:
  8425. case 0x6C:
  8426. case 0x6D:
  8427. case 0x6E:
  8428. case 0x6F:
  8429. case 0x70:
  8430. case 0x71:
  8431. case 0x72:
  8432. case 0x73:
  8433. case 0x74:
  8434. case 0x75:
  8435. case 0x76:
  8436. case 0x77:
  8437. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8438. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8439. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8440. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8441. case 0x7F: // UTF-8 string (indefinite length)
  8442. {
  8443. string_t s;
  8444. return get_cbor_string(s) && sax->string(s);
  8445. }
  8446. // array (0x00..0x17 data items follow)
  8447. case 0x80:
  8448. case 0x81:
  8449. case 0x82:
  8450. case 0x83:
  8451. case 0x84:
  8452. case 0x85:
  8453. case 0x86:
  8454. case 0x87:
  8455. case 0x88:
  8456. case 0x89:
  8457. case 0x8A:
  8458. case 0x8B:
  8459. case 0x8C:
  8460. case 0x8D:
  8461. case 0x8E:
  8462. case 0x8F:
  8463. case 0x90:
  8464. case 0x91:
  8465. case 0x92:
  8466. case 0x93:
  8467. case 0x94:
  8468. case 0x95:
  8469. case 0x96:
  8470. case 0x97:
  8471. return get_cbor_array(
  8472. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8473. case 0x98: // array (one-byte uint8_t for n follows)
  8474. {
  8475. std::uint8_t len{};
  8476. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8477. }
  8478. case 0x99: // array (two-byte uint16_t for n follow)
  8479. {
  8480. std::uint16_t len{};
  8481. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8482. }
  8483. case 0x9A: // array (four-byte uint32_t for n follow)
  8484. {
  8485. std::uint32_t len{};
  8486. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8487. }
  8488. case 0x9B: // array (eight-byte uint64_t for n follow)
  8489. {
  8490. std::uint64_t len{};
  8491. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8492. }
  8493. case 0x9F: // array (indefinite length)
  8494. return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
  8495. // map (0x00..0x17 pairs of data items follow)
  8496. case 0xA0:
  8497. case 0xA1:
  8498. case 0xA2:
  8499. case 0xA3:
  8500. case 0xA4:
  8501. case 0xA5:
  8502. case 0xA6:
  8503. case 0xA7:
  8504. case 0xA8:
  8505. case 0xA9:
  8506. case 0xAA:
  8507. case 0xAB:
  8508. case 0xAC:
  8509. case 0xAD:
  8510. case 0xAE:
  8511. case 0xAF:
  8512. case 0xB0:
  8513. case 0xB1:
  8514. case 0xB2:
  8515. case 0xB3:
  8516. case 0xB4:
  8517. case 0xB5:
  8518. case 0xB6:
  8519. case 0xB7:
  8520. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8521. case 0xB8: // map (one-byte uint8_t for n follows)
  8522. {
  8523. std::uint8_t len{};
  8524. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8525. }
  8526. case 0xB9: // map (two-byte uint16_t for n follow)
  8527. {
  8528. std::uint16_t len{};
  8529. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8530. }
  8531. case 0xBA: // map (four-byte uint32_t for n follow)
  8532. {
  8533. std::uint32_t len{};
  8534. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8535. }
  8536. case 0xBB: // map (eight-byte uint64_t for n follow)
  8537. {
  8538. std::uint64_t len{};
  8539. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8540. }
  8541. case 0xBF: // map (indefinite length)
  8542. return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
  8543. case 0xC6: // tagged item
  8544. case 0xC7:
  8545. case 0xC8:
  8546. case 0xC9:
  8547. case 0xCA:
  8548. case 0xCB:
  8549. case 0xCC:
  8550. case 0xCD:
  8551. case 0xCE:
  8552. case 0xCF:
  8553. case 0xD0:
  8554. case 0xD1:
  8555. case 0xD2:
  8556. case 0xD3:
  8557. case 0xD4:
  8558. case 0xD8: // tagged item (1 bytes follow)
  8559. case 0xD9: // tagged item (2 bytes follow)
  8560. case 0xDA: // tagged item (4 bytes follow)
  8561. case 0xDB: // tagged item (8 bytes follow)
  8562. {
  8563. switch (tag_handler)
  8564. {
  8565. case cbor_tag_handler_t::error:
  8566. {
  8567. auto last_token = get_token_string();
  8568. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8569. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8570. }
  8571. case cbor_tag_handler_t::ignore:
  8572. {
  8573. // ignore binary subtype
  8574. switch (current)
  8575. {
  8576. case 0xD8:
  8577. {
  8578. std::uint8_t subtype_to_ignore{};
  8579. get_number(input_format_t::cbor, subtype_to_ignore);
  8580. break;
  8581. }
  8582. case 0xD9:
  8583. {
  8584. std::uint16_t subtype_to_ignore{};
  8585. get_number(input_format_t::cbor, subtype_to_ignore);
  8586. break;
  8587. }
  8588. case 0xDA:
  8589. {
  8590. std::uint32_t subtype_to_ignore{};
  8591. get_number(input_format_t::cbor, subtype_to_ignore);
  8592. break;
  8593. }
  8594. case 0xDB:
  8595. {
  8596. std::uint64_t subtype_to_ignore{};
  8597. get_number(input_format_t::cbor, subtype_to_ignore);
  8598. break;
  8599. }
  8600. default:
  8601. break;
  8602. }
  8603. return parse_cbor_internal(true, tag_handler);
  8604. }
  8605. case cbor_tag_handler_t::store:
  8606. {
  8607. binary_t b;
  8608. // use binary subtype and store in binary container
  8609. switch (current)
  8610. {
  8611. case 0xD8:
  8612. {
  8613. std::uint8_t subtype{};
  8614. get_number(input_format_t::cbor, subtype);
  8615. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8616. break;
  8617. }
  8618. case 0xD9:
  8619. {
  8620. std::uint16_t subtype{};
  8621. get_number(input_format_t::cbor, subtype);
  8622. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8623. break;
  8624. }
  8625. case 0xDA:
  8626. {
  8627. std::uint32_t subtype{};
  8628. get_number(input_format_t::cbor, subtype);
  8629. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8630. break;
  8631. }
  8632. case 0xDB:
  8633. {
  8634. std::uint64_t subtype{};
  8635. get_number(input_format_t::cbor, subtype);
  8636. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8637. break;
  8638. }
  8639. default:
  8640. return parse_cbor_internal(true, tag_handler);
  8641. }
  8642. get();
  8643. return get_cbor_binary(b) && sax->binary(b);
  8644. }
  8645. default: // LCOV_EXCL_LINE
  8646. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8647. return false; // LCOV_EXCL_LINE
  8648. }
  8649. }
  8650. case 0xF4: // false
  8651. return sax->boolean(false);
  8652. case 0xF5: // true
  8653. return sax->boolean(true);
  8654. case 0xF6: // null
  8655. return sax->null();
  8656. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  8657. {
  8658. const auto byte1_raw = get();
  8659. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8660. {
  8661. return false;
  8662. }
  8663. const auto byte2_raw = get();
  8664. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8665. {
  8666. return false;
  8667. }
  8668. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  8669. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  8670. // code from RFC 7049, Appendix D, Figure 3:
  8671. // As half-precision floating-point numbers were only added
  8672. // to IEEE 754 in 2008, today's programming platforms often
  8673. // still only have limited support for them. It is very
  8674. // easy to include at least decoding support for them even
  8675. // without such support. An example of a small decoder for
  8676. // half-precision floating-point numbers in the C language
  8677. // is shown in Fig. 3.
  8678. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  8679. const double val = [&half]
  8680. {
  8681. const int exp = (half >> 10u) & 0x1Fu;
  8682. const unsigned int mant = half & 0x3FFu;
  8683. JSON_ASSERT(0 <= exp&& exp <= 32);
  8684. JSON_ASSERT(mant <= 1024);
  8685. switch (exp)
  8686. {
  8687. case 0:
  8688. return std::ldexp(mant, -24);
  8689. case 31:
  8690. return (mant == 0)
  8691. ? std::numeric_limits<double>::infinity()
  8692. : std::numeric_limits<double>::quiet_NaN();
  8693. default:
  8694. return std::ldexp(mant + 1024, exp - 25);
  8695. }
  8696. }();
  8697. return sax->number_float((half & 0x8000u) != 0
  8698. ? static_cast<number_float_t>(-val)
  8699. : static_cast<number_float_t>(val), "");
  8700. }
  8701. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  8702. {
  8703. float number{};
  8704. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8705. }
  8706. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8707. {
  8708. double number{};
  8709. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8710. }
  8711. default: // anything else (0xFF is handled inside the other types)
  8712. {
  8713. auto last_token = get_token_string();
  8714. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8715. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8716. }
  8717. }
  8718. }
  8719. /*!
  8720. @brief reads a CBOR string
  8721. This function first reads starting bytes to determine the expected
  8722. string length and then copies this number of bytes into a string.
  8723. Additionally, CBOR's strings with indefinite lengths are supported.
  8724. @param[out] result created string
  8725. @return whether string creation completed
  8726. */
  8727. bool get_cbor_string(string_t& result)
  8728. {
  8729. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8730. {
  8731. return false;
  8732. }
  8733. switch (current)
  8734. {
  8735. // UTF-8 string (0x00..0x17 bytes follow)
  8736. case 0x60:
  8737. case 0x61:
  8738. case 0x62:
  8739. case 0x63:
  8740. case 0x64:
  8741. case 0x65:
  8742. case 0x66:
  8743. case 0x67:
  8744. case 0x68:
  8745. case 0x69:
  8746. case 0x6A:
  8747. case 0x6B:
  8748. case 0x6C:
  8749. case 0x6D:
  8750. case 0x6E:
  8751. case 0x6F:
  8752. case 0x70:
  8753. case 0x71:
  8754. case 0x72:
  8755. case 0x73:
  8756. case 0x74:
  8757. case 0x75:
  8758. case 0x76:
  8759. case 0x77:
  8760. {
  8761. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8762. }
  8763. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8764. {
  8765. std::uint8_t len{};
  8766. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8767. }
  8768. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8769. {
  8770. std::uint16_t len{};
  8771. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8772. }
  8773. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8774. {
  8775. std::uint32_t len{};
  8776. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8777. }
  8778. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8779. {
  8780. std::uint64_t len{};
  8781. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8782. }
  8783. case 0x7F: // UTF-8 string (indefinite length)
  8784. {
  8785. while (get() != 0xFF)
  8786. {
  8787. string_t chunk;
  8788. if (!get_cbor_string(chunk))
  8789. {
  8790. return false;
  8791. }
  8792. result.append(chunk);
  8793. }
  8794. return true;
  8795. }
  8796. default:
  8797. {
  8798. auto last_token = get_token_string();
  8799. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8800. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  8801. }
  8802. }
  8803. }
  8804. /*!
  8805. @brief reads a CBOR byte array
  8806. This function first reads starting bytes to determine the expected
  8807. byte array length and then copies this number of bytes into the byte array.
  8808. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8809. @param[out] result created byte array
  8810. @return whether byte array creation completed
  8811. */
  8812. bool get_cbor_binary(binary_t& result)
  8813. {
  8814. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8815. {
  8816. return false;
  8817. }
  8818. switch (current)
  8819. {
  8820. // Binary data (0x00..0x17 bytes follow)
  8821. case 0x40:
  8822. case 0x41:
  8823. case 0x42:
  8824. case 0x43:
  8825. case 0x44:
  8826. case 0x45:
  8827. case 0x46:
  8828. case 0x47:
  8829. case 0x48:
  8830. case 0x49:
  8831. case 0x4A:
  8832. case 0x4B:
  8833. case 0x4C:
  8834. case 0x4D:
  8835. case 0x4E:
  8836. case 0x4F:
  8837. case 0x50:
  8838. case 0x51:
  8839. case 0x52:
  8840. case 0x53:
  8841. case 0x54:
  8842. case 0x55:
  8843. case 0x56:
  8844. case 0x57:
  8845. {
  8846. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8847. }
  8848. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8849. {
  8850. std::uint8_t len{};
  8851. return get_number(input_format_t::cbor, len) &&
  8852. get_binary(input_format_t::cbor, len, result);
  8853. }
  8854. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8855. {
  8856. std::uint16_t len{};
  8857. return get_number(input_format_t::cbor, len) &&
  8858. get_binary(input_format_t::cbor, len, result);
  8859. }
  8860. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8861. {
  8862. std::uint32_t len{};
  8863. return get_number(input_format_t::cbor, len) &&
  8864. get_binary(input_format_t::cbor, len, result);
  8865. }
  8866. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8867. {
  8868. std::uint64_t len{};
  8869. return get_number(input_format_t::cbor, len) &&
  8870. get_binary(input_format_t::cbor, len, result);
  8871. }
  8872. case 0x5F: // Binary data (indefinite length)
  8873. {
  8874. while (get() != 0xFF)
  8875. {
  8876. binary_t chunk;
  8877. if (!get_cbor_binary(chunk))
  8878. {
  8879. return false;
  8880. }
  8881. result.insert(result.end(), chunk.begin(), chunk.end());
  8882. }
  8883. return true;
  8884. }
  8885. default:
  8886. {
  8887. auto last_token = get_token_string();
  8888. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8889. 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));
  8890. }
  8891. }
  8892. }
  8893. /*!
  8894. @param[in] len the length of the array or static_cast<std::size_t>(-1) for an
  8895. array of indefinite size
  8896. @param[in] tag_handler how CBOR tags should be treated
  8897. @return whether array creation completed
  8898. */
  8899. bool get_cbor_array(const std::size_t len,
  8900. const cbor_tag_handler_t tag_handler)
  8901. {
  8902. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8903. {
  8904. return false;
  8905. }
  8906. if (len != static_cast<std::size_t>(-1))
  8907. {
  8908. for (std::size_t i = 0; i < len; ++i)
  8909. {
  8910. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8911. {
  8912. return false;
  8913. }
  8914. }
  8915. }
  8916. else
  8917. {
  8918. while (get() != 0xFF)
  8919. {
  8920. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8921. {
  8922. return false;
  8923. }
  8924. }
  8925. }
  8926. return sax->end_array();
  8927. }
  8928. /*!
  8929. @param[in] len the length of the object or static_cast<std::size_t>(-1) for an
  8930. object of indefinite size
  8931. @param[in] tag_handler how CBOR tags should be treated
  8932. @return whether object creation completed
  8933. */
  8934. bool get_cbor_object(const std::size_t len,
  8935. const cbor_tag_handler_t tag_handler)
  8936. {
  8937. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8938. {
  8939. return false;
  8940. }
  8941. if (len != 0)
  8942. {
  8943. string_t key;
  8944. if (len != static_cast<std::size_t>(-1))
  8945. {
  8946. for (std::size_t i = 0; i < len; ++i)
  8947. {
  8948. get();
  8949. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8950. {
  8951. return false;
  8952. }
  8953. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8954. {
  8955. return false;
  8956. }
  8957. key.clear();
  8958. }
  8959. }
  8960. else
  8961. {
  8962. while (get() != 0xFF)
  8963. {
  8964. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8965. {
  8966. return false;
  8967. }
  8968. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8969. {
  8970. return false;
  8971. }
  8972. key.clear();
  8973. }
  8974. }
  8975. }
  8976. return sax->end_object();
  8977. }
  8978. /////////////
  8979. // MsgPack //
  8980. /////////////
  8981. /*!
  8982. @return whether a valid MessagePack value was passed to the SAX parser
  8983. */
  8984. bool parse_msgpack_internal()
  8985. {
  8986. switch (get())
  8987. {
  8988. // EOF
  8989. case char_traits<char_type>::eof():
  8990. return unexpect_eof(input_format_t::msgpack, "value");
  8991. // positive fixint
  8992. case 0x00:
  8993. case 0x01:
  8994. case 0x02:
  8995. case 0x03:
  8996. case 0x04:
  8997. case 0x05:
  8998. case 0x06:
  8999. case 0x07:
  9000. case 0x08:
  9001. case 0x09:
  9002. case 0x0A:
  9003. case 0x0B:
  9004. case 0x0C:
  9005. case 0x0D:
  9006. case 0x0E:
  9007. case 0x0F:
  9008. case 0x10:
  9009. case 0x11:
  9010. case 0x12:
  9011. case 0x13:
  9012. case 0x14:
  9013. case 0x15:
  9014. case 0x16:
  9015. case 0x17:
  9016. case 0x18:
  9017. case 0x19:
  9018. case 0x1A:
  9019. case 0x1B:
  9020. case 0x1C:
  9021. case 0x1D:
  9022. case 0x1E:
  9023. case 0x1F:
  9024. case 0x20:
  9025. case 0x21:
  9026. case 0x22:
  9027. case 0x23:
  9028. case 0x24:
  9029. case 0x25:
  9030. case 0x26:
  9031. case 0x27:
  9032. case 0x28:
  9033. case 0x29:
  9034. case 0x2A:
  9035. case 0x2B:
  9036. case 0x2C:
  9037. case 0x2D:
  9038. case 0x2E:
  9039. case 0x2F:
  9040. case 0x30:
  9041. case 0x31:
  9042. case 0x32:
  9043. case 0x33:
  9044. case 0x34:
  9045. case 0x35:
  9046. case 0x36:
  9047. case 0x37:
  9048. case 0x38:
  9049. case 0x39:
  9050. case 0x3A:
  9051. case 0x3B:
  9052. case 0x3C:
  9053. case 0x3D:
  9054. case 0x3E:
  9055. case 0x3F:
  9056. case 0x40:
  9057. case 0x41:
  9058. case 0x42:
  9059. case 0x43:
  9060. case 0x44:
  9061. case 0x45:
  9062. case 0x46:
  9063. case 0x47:
  9064. case 0x48:
  9065. case 0x49:
  9066. case 0x4A:
  9067. case 0x4B:
  9068. case 0x4C:
  9069. case 0x4D:
  9070. case 0x4E:
  9071. case 0x4F:
  9072. case 0x50:
  9073. case 0x51:
  9074. case 0x52:
  9075. case 0x53:
  9076. case 0x54:
  9077. case 0x55:
  9078. case 0x56:
  9079. case 0x57:
  9080. case 0x58:
  9081. case 0x59:
  9082. case 0x5A:
  9083. case 0x5B:
  9084. case 0x5C:
  9085. case 0x5D:
  9086. case 0x5E:
  9087. case 0x5F:
  9088. case 0x60:
  9089. case 0x61:
  9090. case 0x62:
  9091. case 0x63:
  9092. case 0x64:
  9093. case 0x65:
  9094. case 0x66:
  9095. case 0x67:
  9096. case 0x68:
  9097. case 0x69:
  9098. case 0x6A:
  9099. case 0x6B:
  9100. case 0x6C:
  9101. case 0x6D:
  9102. case 0x6E:
  9103. case 0x6F:
  9104. case 0x70:
  9105. case 0x71:
  9106. case 0x72:
  9107. case 0x73:
  9108. case 0x74:
  9109. case 0x75:
  9110. case 0x76:
  9111. case 0x77:
  9112. case 0x78:
  9113. case 0x79:
  9114. case 0x7A:
  9115. case 0x7B:
  9116. case 0x7C:
  9117. case 0x7D:
  9118. case 0x7E:
  9119. case 0x7F:
  9120. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9121. // fixmap
  9122. case 0x80:
  9123. case 0x81:
  9124. case 0x82:
  9125. case 0x83:
  9126. case 0x84:
  9127. case 0x85:
  9128. case 0x86:
  9129. case 0x87:
  9130. case 0x88:
  9131. case 0x89:
  9132. case 0x8A:
  9133. case 0x8B:
  9134. case 0x8C:
  9135. case 0x8D:
  9136. case 0x8E:
  9137. case 0x8F:
  9138. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9139. // fixarray
  9140. case 0x90:
  9141. case 0x91:
  9142. case 0x92:
  9143. case 0x93:
  9144. case 0x94:
  9145. case 0x95:
  9146. case 0x96:
  9147. case 0x97:
  9148. case 0x98:
  9149. case 0x99:
  9150. case 0x9A:
  9151. case 0x9B:
  9152. case 0x9C:
  9153. case 0x9D:
  9154. case 0x9E:
  9155. case 0x9F:
  9156. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9157. // fixstr
  9158. case 0xA0:
  9159. case 0xA1:
  9160. case 0xA2:
  9161. case 0xA3:
  9162. case 0xA4:
  9163. case 0xA5:
  9164. case 0xA6:
  9165. case 0xA7:
  9166. case 0xA8:
  9167. case 0xA9:
  9168. case 0xAA:
  9169. case 0xAB:
  9170. case 0xAC:
  9171. case 0xAD:
  9172. case 0xAE:
  9173. case 0xAF:
  9174. case 0xB0:
  9175. case 0xB1:
  9176. case 0xB2:
  9177. case 0xB3:
  9178. case 0xB4:
  9179. case 0xB5:
  9180. case 0xB6:
  9181. case 0xB7:
  9182. case 0xB8:
  9183. case 0xB9:
  9184. case 0xBA:
  9185. case 0xBB:
  9186. case 0xBC:
  9187. case 0xBD:
  9188. case 0xBE:
  9189. case 0xBF:
  9190. case 0xD9: // str 8
  9191. case 0xDA: // str 16
  9192. case 0xDB: // str 32
  9193. {
  9194. string_t s;
  9195. return get_msgpack_string(s) && sax->string(s);
  9196. }
  9197. case 0xC0: // nil
  9198. return sax->null();
  9199. case 0xC2: // false
  9200. return sax->boolean(false);
  9201. case 0xC3: // true
  9202. return sax->boolean(true);
  9203. case 0xC4: // bin 8
  9204. case 0xC5: // bin 16
  9205. case 0xC6: // bin 32
  9206. case 0xC7: // ext 8
  9207. case 0xC8: // ext 16
  9208. case 0xC9: // ext 32
  9209. case 0xD4: // fixext 1
  9210. case 0xD5: // fixext 2
  9211. case 0xD6: // fixext 4
  9212. case 0xD7: // fixext 8
  9213. case 0xD8: // fixext 16
  9214. {
  9215. binary_t b;
  9216. return get_msgpack_binary(b) && sax->binary(b);
  9217. }
  9218. case 0xCA: // float 32
  9219. {
  9220. float number{};
  9221. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9222. }
  9223. case 0xCB: // float 64
  9224. {
  9225. double number{};
  9226. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9227. }
  9228. case 0xCC: // uint 8
  9229. {
  9230. std::uint8_t number{};
  9231. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9232. }
  9233. case 0xCD: // uint 16
  9234. {
  9235. std::uint16_t number{};
  9236. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9237. }
  9238. case 0xCE: // uint 32
  9239. {
  9240. std::uint32_t number{};
  9241. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9242. }
  9243. case 0xCF: // uint 64
  9244. {
  9245. std::uint64_t number{};
  9246. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9247. }
  9248. case 0xD0: // int 8
  9249. {
  9250. std::int8_t number{};
  9251. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9252. }
  9253. case 0xD1: // int 16
  9254. {
  9255. std::int16_t number{};
  9256. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9257. }
  9258. case 0xD2: // int 32
  9259. {
  9260. std::int32_t number{};
  9261. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9262. }
  9263. case 0xD3: // int 64
  9264. {
  9265. std::int64_t number{};
  9266. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9267. }
  9268. case 0xDC: // array 16
  9269. {
  9270. std::uint16_t len{};
  9271. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9272. }
  9273. case 0xDD: // array 32
  9274. {
  9275. std::uint32_t len{};
  9276. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9277. }
  9278. case 0xDE: // map 16
  9279. {
  9280. std::uint16_t len{};
  9281. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9282. }
  9283. case 0xDF: // map 32
  9284. {
  9285. std::uint32_t len{};
  9286. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9287. }
  9288. // negative fixint
  9289. case 0xE0:
  9290. case 0xE1:
  9291. case 0xE2:
  9292. case 0xE3:
  9293. case 0xE4:
  9294. case 0xE5:
  9295. case 0xE6:
  9296. case 0xE7:
  9297. case 0xE8:
  9298. case 0xE9:
  9299. case 0xEA:
  9300. case 0xEB:
  9301. case 0xEC:
  9302. case 0xED:
  9303. case 0xEE:
  9304. case 0xEF:
  9305. case 0xF0:
  9306. case 0xF1:
  9307. case 0xF2:
  9308. case 0xF3:
  9309. case 0xF4:
  9310. case 0xF5:
  9311. case 0xF6:
  9312. case 0xF7:
  9313. case 0xF8:
  9314. case 0xF9:
  9315. case 0xFA:
  9316. case 0xFB:
  9317. case 0xFC:
  9318. case 0xFD:
  9319. case 0xFE:
  9320. case 0xFF:
  9321. return sax->number_integer(static_cast<std::int8_t>(current));
  9322. default: // anything else
  9323. {
  9324. auto last_token = get_token_string();
  9325. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9326. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9327. }
  9328. }
  9329. }
  9330. /*!
  9331. @brief reads a MessagePack string
  9332. This function first reads starting bytes to determine the expected
  9333. string length and then copies this number of bytes into a string.
  9334. @param[out] result created string
  9335. @return whether string creation completed
  9336. */
  9337. bool get_msgpack_string(string_t& result)
  9338. {
  9339. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  9340. {
  9341. return false;
  9342. }
  9343. switch (current)
  9344. {
  9345. // fixstr
  9346. case 0xA0:
  9347. case 0xA1:
  9348. case 0xA2:
  9349. case 0xA3:
  9350. case 0xA4:
  9351. case 0xA5:
  9352. case 0xA6:
  9353. case 0xA7:
  9354. case 0xA8:
  9355. case 0xA9:
  9356. case 0xAA:
  9357. case 0xAB:
  9358. case 0xAC:
  9359. case 0xAD:
  9360. case 0xAE:
  9361. case 0xAF:
  9362. case 0xB0:
  9363. case 0xB1:
  9364. case 0xB2:
  9365. case 0xB3:
  9366. case 0xB4:
  9367. case 0xB5:
  9368. case 0xB6:
  9369. case 0xB7:
  9370. case 0xB8:
  9371. case 0xB9:
  9372. case 0xBA:
  9373. case 0xBB:
  9374. case 0xBC:
  9375. case 0xBD:
  9376. case 0xBE:
  9377. case 0xBF:
  9378. {
  9379. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9380. }
  9381. case 0xD9: // str 8
  9382. {
  9383. std::uint8_t len{};
  9384. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9385. }
  9386. case 0xDA: // str 16
  9387. {
  9388. std::uint16_t len{};
  9389. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9390. }
  9391. case 0xDB: // str 32
  9392. {
  9393. std::uint32_t len{};
  9394. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9395. }
  9396. default:
  9397. {
  9398. auto last_token = get_token_string();
  9399. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9400. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9401. }
  9402. }
  9403. }
  9404. /*!
  9405. @brief reads a MessagePack byte array
  9406. This function first reads starting bytes to determine the expected
  9407. byte array length and then copies this number of bytes into a byte array.
  9408. @param[out] result created byte array
  9409. @return whether byte array creation completed
  9410. */
  9411. bool get_msgpack_binary(binary_t& result)
  9412. {
  9413. // helper function to set the subtype
  9414. auto assign_and_return_true = [&result](std::int8_t subtype)
  9415. {
  9416. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9417. return true;
  9418. };
  9419. switch (current)
  9420. {
  9421. case 0xC4: // bin 8
  9422. {
  9423. std::uint8_t len{};
  9424. return get_number(input_format_t::msgpack, len) &&
  9425. get_binary(input_format_t::msgpack, len, result);
  9426. }
  9427. case 0xC5: // bin 16
  9428. {
  9429. std::uint16_t len{};
  9430. return get_number(input_format_t::msgpack, len) &&
  9431. get_binary(input_format_t::msgpack, len, result);
  9432. }
  9433. case 0xC6: // bin 32
  9434. {
  9435. std::uint32_t len{};
  9436. return get_number(input_format_t::msgpack, len) &&
  9437. get_binary(input_format_t::msgpack, len, result);
  9438. }
  9439. case 0xC7: // ext 8
  9440. {
  9441. std::uint8_t len{};
  9442. std::int8_t subtype{};
  9443. return get_number(input_format_t::msgpack, len) &&
  9444. get_number(input_format_t::msgpack, subtype) &&
  9445. get_binary(input_format_t::msgpack, len, result) &&
  9446. assign_and_return_true(subtype);
  9447. }
  9448. case 0xC8: // ext 16
  9449. {
  9450. std::uint16_t len{};
  9451. std::int8_t subtype{};
  9452. return get_number(input_format_t::msgpack, len) &&
  9453. get_number(input_format_t::msgpack, subtype) &&
  9454. get_binary(input_format_t::msgpack, len, result) &&
  9455. assign_and_return_true(subtype);
  9456. }
  9457. case 0xC9: // ext 32
  9458. {
  9459. std::uint32_t len{};
  9460. std::int8_t subtype{};
  9461. return get_number(input_format_t::msgpack, len) &&
  9462. get_number(input_format_t::msgpack, subtype) &&
  9463. get_binary(input_format_t::msgpack, len, result) &&
  9464. assign_and_return_true(subtype);
  9465. }
  9466. case 0xD4: // fixext 1
  9467. {
  9468. std::int8_t subtype{};
  9469. return get_number(input_format_t::msgpack, subtype) &&
  9470. get_binary(input_format_t::msgpack, 1, result) &&
  9471. assign_and_return_true(subtype);
  9472. }
  9473. case 0xD5: // fixext 2
  9474. {
  9475. std::int8_t subtype{};
  9476. return get_number(input_format_t::msgpack, subtype) &&
  9477. get_binary(input_format_t::msgpack, 2, result) &&
  9478. assign_and_return_true(subtype);
  9479. }
  9480. case 0xD6: // fixext 4
  9481. {
  9482. std::int8_t subtype{};
  9483. return get_number(input_format_t::msgpack, subtype) &&
  9484. get_binary(input_format_t::msgpack, 4, result) &&
  9485. assign_and_return_true(subtype);
  9486. }
  9487. case 0xD7: // fixext 8
  9488. {
  9489. std::int8_t subtype{};
  9490. return get_number(input_format_t::msgpack, subtype) &&
  9491. get_binary(input_format_t::msgpack, 8, result) &&
  9492. assign_and_return_true(subtype);
  9493. }
  9494. case 0xD8: // fixext 16
  9495. {
  9496. std::int8_t subtype{};
  9497. return get_number(input_format_t::msgpack, subtype) &&
  9498. get_binary(input_format_t::msgpack, 16, result) &&
  9499. assign_and_return_true(subtype);
  9500. }
  9501. default: // LCOV_EXCL_LINE
  9502. return false; // LCOV_EXCL_LINE
  9503. }
  9504. }
  9505. /*!
  9506. @param[in] len the length of the array
  9507. @return whether array creation completed
  9508. */
  9509. bool get_msgpack_array(const std::size_t len)
  9510. {
  9511. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9512. {
  9513. return false;
  9514. }
  9515. for (std::size_t i = 0; i < len; ++i)
  9516. {
  9517. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9518. {
  9519. return false;
  9520. }
  9521. }
  9522. return sax->end_array();
  9523. }
  9524. /*!
  9525. @param[in] len the length of the object
  9526. @return whether object creation completed
  9527. */
  9528. bool get_msgpack_object(const std::size_t len)
  9529. {
  9530. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9531. {
  9532. return false;
  9533. }
  9534. string_t key;
  9535. for (std::size_t i = 0; i < len; ++i)
  9536. {
  9537. get();
  9538. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  9539. {
  9540. return false;
  9541. }
  9542. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9543. {
  9544. return false;
  9545. }
  9546. key.clear();
  9547. }
  9548. return sax->end_object();
  9549. }
  9550. ////////////
  9551. // UBJSON //
  9552. ////////////
  9553. /*!
  9554. @param[in] get_char whether a new character should be retrieved from the
  9555. input (true, default) or whether the last read
  9556. character should be considered instead
  9557. @return whether a valid UBJSON value was passed to the SAX parser
  9558. */
  9559. bool parse_ubjson_internal(const bool get_char = true)
  9560. {
  9561. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  9562. }
  9563. /*!
  9564. @brief reads a UBJSON string
  9565. This function is either called after reading the 'S' byte explicitly
  9566. indicating a string, or in case of an object key where the 'S' byte can be
  9567. left out.
  9568. @param[out] result created string
  9569. @param[in] get_char whether a new character should be retrieved from the
  9570. input (true, default) or whether the last read
  9571. character should be considered instead
  9572. @return whether string creation completed
  9573. */
  9574. bool get_ubjson_string(string_t& result, const bool get_char = true)
  9575. {
  9576. if (get_char)
  9577. {
  9578. get(); // TODO(niels): may we ignore N here?
  9579. }
  9580. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9581. {
  9582. return false;
  9583. }
  9584. switch (current)
  9585. {
  9586. case 'U':
  9587. {
  9588. std::uint8_t len{};
  9589. return get_number(input_format, len) && get_string(input_format, len, result);
  9590. }
  9591. case 'i':
  9592. {
  9593. std::int8_t len{};
  9594. return get_number(input_format, len) && get_string(input_format, len, result);
  9595. }
  9596. case 'I':
  9597. {
  9598. std::int16_t len{};
  9599. return get_number(input_format, len) && get_string(input_format, len, result);
  9600. }
  9601. case 'l':
  9602. {
  9603. std::int32_t len{};
  9604. return get_number(input_format, len) && get_string(input_format, len, result);
  9605. }
  9606. case 'L':
  9607. {
  9608. std::int64_t len{};
  9609. return get_number(input_format, len) && get_string(input_format, len, result);
  9610. }
  9611. case 'u':
  9612. {
  9613. if (input_format != input_format_t::bjdata)
  9614. {
  9615. break;
  9616. }
  9617. std::uint16_t len{};
  9618. return get_number(input_format, len) && get_string(input_format, len, result);
  9619. }
  9620. case 'm':
  9621. {
  9622. if (input_format != input_format_t::bjdata)
  9623. {
  9624. break;
  9625. }
  9626. std::uint32_t len{};
  9627. return get_number(input_format, len) && get_string(input_format, len, result);
  9628. }
  9629. case 'M':
  9630. {
  9631. if (input_format != input_format_t::bjdata)
  9632. {
  9633. break;
  9634. }
  9635. std::uint64_t len{};
  9636. return get_number(input_format, len) && get_string(input_format, len, result);
  9637. }
  9638. default:
  9639. break;
  9640. }
  9641. auto last_token = get_token_string();
  9642. std::string message;
  9643. if (input_format != input_format_t::bjdata)
  9644. {
  9645. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  9646. }
  9647. else
  9648. {
  9649. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  9650. }
  9651. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  9652. }
  9653. /*!
  9654. @param[out] dim an integer vector storing the ND array dimensions
  9655. @return whether reading ND array size vector is successful
  9656. */
  9657. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  9658. {
  9659. std::pair<std::size_t, char_int_type> size_and_type;
  9660. size_t dimlen = 0;
  9661. bool no_ndarray = true;
  9662. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  9663. {
  9664. return false;
  9665. }
  9666. if (size_and_type.first != npos)
  9667. {
  9668. if (size_and_type.second != 0)
  9669. {
  9670. if (size_and_type.second != 'N')
  9671. {
  9672. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9673. {
  9674. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  9675. {
  9676. return false;
  9677. }
  9678. dim.push_back(dimlen);
  9679. }
  9680. }
  9681. }
  9682. else
  9683. {
  9684. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9685. {
  9686. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  9687. {
  9688. return false;
  9689. }
  9690. dim.push_back(dimlen);
  9691. }
  9692. }
  9693. }
  9694. else
  9695. {
  9696. while (current != ']')
  9697. {
  9698. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  9699. {
  9700. return false;
  9701. }
  9702. dim.push_back(dimlen);
  9703. get_ignore_noop();
  9704. }
  9705. }
  9706. return true;
  9707. }
  9708. /*!
  9709. @param[out] result determined size
  9710. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  9711. or ndarray dimension is not allowed; `false` means ndarray
  9712. is allowed; for output, `true` means an ndarray is found;
  9713. is_ndarray can only return `true` when its initial value
  9714. is `false`
  9715. @param[in] prefix type marker if already read, otherwise set to 0
  9716. @return whether size determination completed
  9717. */
  9718. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  9719. {
  9720. if (prefix == 0)
  9721. {
  9722. prefix = get_ignore_noop();
  9723. }
  9724. switch (prefix)
  9725. {
  9726. case 'U':
  9727. {
  9728. std::uint8_t number{};
  9729. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9730. {
  9731. return false;
  9732. }
  9733. result = static_cast<std::size_t>(number);
  9734. return true;
  9735. }
  9736. case 'i':
  9737. {
  9738. std::int8_t number{};
  9739. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9740. {
  9741. return false;
  9742. }
  9743. if (number < 0)
  9744. {
  9745. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9746. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9747. }
  9748. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  9749. return true;
  9750. }
  9751. case 'I':
  9752. {
  9753. std::int16_t number{};
  9754. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9755. {
  9756. return false;
  9757. }
  9758. if (number < 0)
  9759. {
  9760. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9761. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9762. }
  9763. result = static_cast<std::size_t>(number);
  9764. return true;
  9765. }
  9766. case 'l':
  9767. {
  9768. std::int32_t number{};
  9769. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9770. {
  9771. return false;
  9772. }
  9773. if (number < 0)
  9774. {
  9775. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9776. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9777. }
  9778. result = static_cast<std::size_t>(number);
  9779. return true;
  9780. }
  9781. case 'L':
  9782. {
  9783. std::int64_t number{};
  9784. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9785. {
  9786. return false;
  9787. }
  9788. if (number < 0)
  9789. {
  9790. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9791. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9792. }
  9793. if (!value_in_range_of<std::size_t>(number))
  9794. {
  9795. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9796. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9797. }
  9798. result = static_cast<std::size_t>(number);
  9799. return true;
  9800. }
  9801. case 'u':
  9802. {
  9803. if (input_format != input_format_t::bjdata)
  9804. {
  9805. break;
  9806. }
  9807. std::uint16_t number{};
  9808. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9809. {
  9810. return false;
  9811. }
  9812. result = static_cast<std::size_t>(number);
  9813. return true;
  9814. }
  9815. case 'm':
  9816. {
  9817. if (input_format != input_format_t::bjdata)
  9818. {
  9819. break;
  9820. }
  9821. std::uint32_t number{};
  9822. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9823. {
  9824. return false;
  9825. }
  9826. result = conditional_static_cast<std::size_t>(number);
  9827. return true;
  9828. }
  9829. case 'M':
  9830. {
  9831. if (input_format != input_format_t::bjdata)
  9832. {
  9833. break;
  9834. }
  9835. std::uint64_t number{};
  9836. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9837. {
  9838. return false;
  9839. }
  9840. if (!value_in_range_of<std::size_t>(number))
  9841. {
  9842. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9843. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9844. }
  9845. result = detail::conditional_static_cast<std::size_t>(number);
  9846. return true;
  9847. }
  9848. case '[':
  9849. {
  9850. if (input_format != input_format_t::bjdata)
  9851. {
  9852. break;
  9853. }
  9854. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  9855. {
  9856. 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));
  9857. }
  9858. std::vector<size_t> dim;
  9859. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  9860. {
  9861. return false;
  9862. }
  9863. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  9864. {
  9865. result = dim.at(dim.size() - 1);
  9866. return true;
  9867. }
  9868. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  9869. {
  9870. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  9871. {
  9872. if ( i == 0 )
  9873. {
  9874. result = 0;
  9875. return true;
  9876. }
  9877. }
  9878. string_t key = "_ArraySize_";
  9879. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  9880. {
  9881. return false;
  9882. }
  9883. result = 1;
  9884. for (auto i : dim)
  9885. {
  9886. result *= i;
  9887. if (result == 0 || result == npos) // because dim elements shall not have zeros, result = 0 means overflow happened; it also can't be npos as it is used to initialize size in get_ubjson_size_type()
  9888. {
  9889. 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));
  9890. }
  9891. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  9892. {
  9893. return false;
  9894. }
  9895. }
  9896. is_ndarray = true;
  9897. return sax->end_array();
  9898. }
  9899. result = 0;
  9900. return true;
  9901. }
  9902. default:
  9903. break;
  9904. }
  9905. auto last_token = get_token_string();
  9906. std::string message;
  9907. if (input_format != input_format_t::bjdata)
  9908. {
  9909. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  9910. }
  9911. else
  9912. {
  9913. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  9914. }
  9915. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  9916. }
  9917. /*!
  9918. @brief determine the type and size for a container
  9919. In the optimized UBJSON format, a type and a size can be provided to allow
  9920. for a more compact representation.
  9921. @param[out] result pair of the size and the type
  9922. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  9923. @return whether pair creation completed
  9924. */
  9925. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  9926. {
  9927. result.first = npos; // size
  9928. result.second = 0; // type
  9929. bool is_ndarray = false;
  9930. get_ignore_noop();
  9931. if (current == '$')
  9932. {
  9933. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  9934. if (input_format == input_format_t::bjdata
  9935. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  9936. {
  9937. auto last_token = get_token_string();
  9938. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9939. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  9940. }
  9941. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  9942. {
  9943. return false;
  9944. }
  9945. get_ignore_noop();
  9946. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  9947. {
  9948. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9949. {
  9950. return false;
  9951. }
  9952. auto last_token = get_token_string();
  9953. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9954. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  9955. }
  9956. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9957. if (input_format == input_format_t::bjdata && is_ndarray)
  9958. {
  9959. if (inside_ndarray)
  9960. {
  9961. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9962. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  9963. }
  9964. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  9965. }
  9966. return is_error;
  9967. }
  9968. if (current == '#')
  9969. {
  9970. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9971. if (input_format == input_format_t::bjdata && is_ndarray)
  9972. {
  9973. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9974. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  9975. }
  9976. return is_error;
  9977. }
  9978. return true;
  9979. }
  9980. /*!
  9981. @param prefix the previously read or set type prefix
  9982. @return whether value creation completed
  9983. */
  9984. bool get_ubjson_value(const char_int_type prefix)
  9985. {
  9986. switch (prefix)
  9987. {
  9988. case char_traits<char_type>::eof(): // EOF
  9989. return unexpect_eof(input_format, "value");
  9990. case 'T': // true
  9991. return sax->boolean(true);
  9992. case 'F': // false
  9993. return sax->boolean(false);
  9994. case 'Z': // null
  9995. return sax->null();
  9996. case 'U':
  9997. {
  9998. std::uint8_t number{};
  9999. return get_number(input_format, number) && sax->number_unsigned(number);
  10000. }
  10001. case 'i':
  10002. {
  10003. std::int8_t number{};
  10004. return get_number(input_format, number) && sax->number_integer(number);
  10005. }
  10006. case 'I':
  10007. {
  10008. std::int16_t number{};
  10009. return get_number(input_format, number) && sax->number_integer(number);
  10010. }
  10011. case 'l':
  10012. {
  10013. std::int32_t number{};
  10014. return get_number(input_format, number) && sax->number_integer(number);
  10015. }
  10016. case 'L':
  10017. {
  10018. std::int64_t number{};
  10019. return get_number(input_format, number) && sax->number_integer(number);
  10020. }
  10021. case 'u':
  10022. {
  10023. if (input_format != input_format_t::bjdata)
  10024. {
  10025. break;
  10026. }
  10027. std::uint16_t number{};
  10028. return get_number(input_format, number) && sax->number_unsigned(number);
  10029. }
  10030. case 'm':
  10031. {
  10032. if (input_format != input_format_t::bjdata)
  10033. {
  10034. break;
  10035. }
  10036. std::uint32_t number{};
  10037. return get_number(input_format, number) && sax->number_unsigned(number);
  10038. }
  10039. case 'M':
  10040. {
  10041. if (input_format != input_format_t::bjdata)
  10042. {
  10043. break;
  10044. }
  10045. std::uint64_t number{};
  10046. return get_number(input_format, number) && sax->number_unsigned(number);
  10047. }
  10048. case 'h':
  10049. {
  10050. if (input_format != input_format_t::bjdata)
  10051. {
  10052. break;
  10053. }
  10054. const auto byte1_raw = get();
  10055. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10056. {
  10057. return false;
  10058. }
  10059. const auto byte2_raw = get();
  10060. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10061. {
  10062. return false;
  10063. }
  10064. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  10065. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10066. // code from RFC 7049, Appendix D, Figure 3:
  10067. // As half-precision floating-point numbers were only added
  10068. // to IEEE 754 in 2008, today's programming platforms often
  10069. // still only have limited support for them. It is very
  10070. // easy to include at least decoding support for them even
  10071. // without such support. An example of a small decoder for
  10072. // half-precision floating-point numbers in the C language
  10073. // is shown in Fig. 3.
  10074. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10075. const double val = [&half]
  10076. {
  10077. const int exp = (half >> 10u) & 0x1Fu;
  10078. const unsigned int mant = half & 0x3FFu;
  10079. JSON_ASSERT(0 <= exp&& exp <= 32);
  10080. JSON_ASSERT(mant <= 1024);
  10081. switch (exp)
  10082. {
  10083. case 0:
  10084. return std::ldexp(mant, -24);
  10085. case 31:
  10086. return (mant == 0)
  10087. ? std::numeric_limits<double>::infinity()
  10088. : std::numeric_limits<double>::quiet_NaN();
  10089. default:
  10090. return std::ldexp(mant + 1024, exp - 25);
  10091. }
  10092. }();
  10093. return sax->number_float((half & 0x8000u) != 0
  10094. ? static_cast<number_float_t>(-val)
  10095. : static_cast<number_float_t>(val), "");
  10096. }
  10097. case 'd':
  10098. {
  10099. float number{};
  10100. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10101. }
  10102. case 'D':
  10103. {
  10104. double number{};
  10105. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10106. }
  10107. case 'H':
  10108. {
  10109. return get_ubjson_high_precision_number();
  10110. }
  10111. case 'C': // char
  10112. {
  10113. get();
  10114. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10115. {
  10116. return false;
  10117. }
  10118. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10119. {
  10120. auto last_token = get_token_string();
  10121. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10122. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10123. }
  10124. string_t s(1, static_cast<typename string_t::value_type>(current));
  10125. return sax->string(s);
  10126. }
  10127. case 'S': // string
  10128. {
  10129. string_t s;
  10130. return get_ubjson_string(s) && sax->string(s);
  10131. }
  10132. case '[': // array
  10133. return get_ubjson_array();
  10134. case '{': // object
  10135. return get_ubjson_object();
  10136. default: // anything else
  10137. break;
  10138. }
  10139. auto last_token = get_token_string();
  10140. 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));
  10141. }
  10142. /*!
  10143. @return whether array creation completed
  10144. */
  10145. bool get_ubjson_array()
  10146. {
  10147. std::pair<std::size_t, char_int_type> size_and_type;
  10148. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10149. {
  10150. return false;
  10151. }
  10152. // 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):
  10153. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10154. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10155. {
  10156. 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
  10157. 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)
  10158. {
  10159. return p.first < t;
  10160. });
  10161. string_t key = "_ArrayType_";
  10162. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10163. {
  10164. auto last_token = get_token_string();
  10165. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10166. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10167. }
  10168. string_t type = it->second; // sax->string() takes a reference
  10169. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10170. {
  10171. return false;
  10172. }
  10173. if (size_and_type.second == 'C')
  10174. {
  10175. size_and_type.second = 'U';
  10176. }
  10177. key = "_ArrayData_";
  10178. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10179. {
  10180. return false;
  10181. }
  10182. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10183. {
  10184. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10185. {
  10186. return false;
  10187. }
  10188. }
  10189. return (sax->end_array() && sax->end_object());
  10190. }
  10191. if (size_and_type.first != npos)
  10192. {
  10193. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10194. {
  10195. return false;
  10196. }
  10197. if (size_and_type.second != 0)
  10198. {
  10199. if (size_and_type.second != 'N')
  10200. {
  10201. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10202. {
  10203. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10204. {
  10205. return false;
  10206. }
  10207. }
  10208. }
  10209. }
  10210. else
  10211. {
  10212. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10213. {
  10214. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10215. {
  10216. return false;
  10217. }
  10218. }
  10219. }
  10220. }
  10221. else
  10222. {
  10223. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10224. {
  10225. return false;
  10226. }
  10227. while (current != ']')
  10228. {
  10229. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10230. {
  10231. return false;
  10232. }
  10233. get_ignore_noop();
  10234. }
  10235. }
  10236. return sax->end_array();
  10237. }
  10238. /*!
  10239. @return whether object creation completed
  10240. */
  10241. bool get_ubjson_object()
  10242. {
  10243. std::pair<std::size_t, char_int_type> size_and_type;
  10244. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10245. {
  10246. return false;
  10247. }
  10248. // do not accept ND-array size in objects in BJData
  10249. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10250. {
  10251. auto last_token = get_token_string();
  10252. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10253. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10254. }
  10255. string_t key;
  10256. if (size_and_type.first != npos)
  10257. {
  10258. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10259. {
  10260. return false;
  10261. }
  10262. if (size_and_type.second != 0)
  10263. {
  10264. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10265. {
  10266. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10267. {
  10268. return false;
  10269. }
  10270. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10271. {
  10272. return false;
  10273. }
  10274. key.clear();
  10275. }
  10276. }
  10277. else
  10278. {
  10279. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10280. {
  10281. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10282. {
  10283. return false;
  10284. }
  10285. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10286. {
  10287. return false;
  10288. }
  10289. key.clear();
  10290. }
  10291. }
  10292. }
  10293. else
  10294. {
  10295. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10296. {
  10297. return false;
  10298. }
  10299. while (current != '}')
  10300. {
  10301. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  10302. {
  10303. return false;
  10304. }
  10305. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10306. {
  10307. return false;
  10308. }
  10309. get_ignore_noop();
  10310. key.clear();
  10311. }
  10312. }
  10313. return sax->end_object();
  10314. }
  10315. // Note, no reader for UBJSON binary types is implemented because they do
  10316. // not exist
  10317. bool get_ubjson_high_precision_number()
  10318. {
  10319. // get size of following number string
  10320. std::size_t size{};
  10321. bool no_ndarray = true;
  10322. auto res = get_ubjson_size_value(size, no_ndarray);
  10323. if (JSON_HEDLEY_UNLIKELY(!res))
  10324. {
  10325. return res;
  10326. }
  10327. // get number string
  10328. std::vector<char> number_vector;
  10329. for (std::size_t i = 0; i < size; ++i)
  10330. {
  10331. get();
  10332. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10333. {
  10334. return false;
  10335. }
  10336. number_vector.push_back(static_cast<char>(current));
  10337. }
  10338. // parse number string
  10339. using ia_type = decltype(detail::input_adapter(number_vector));
  10340. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  10341. const auto result_number = number_lexer.scan();
  10342. const auto number_string = number_lexer.get_token_string();
  10343. const auto result_remainder = number_lexer.scan();
  10344. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10345. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10346. {
  10347. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10348. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10349. }
  10350. switch (result_number)
  10351. {
  10352. case token_type::value_integer:
  10353. return sax->number_integer(number_lexer.get_number_integer());
  10354. case token_type::value_unsigned:
  10355. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10356. case token_type::value_float:
  10357. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10358. case token_type::uninitialized:
  10359. case token_type::literal_true:
  10360. case token_type::literal_false:
  10361. case token_type::literal_null:
  10362. case token_type::value_string:
  10363. case token_type::begin_array:
  10364. case token_type::begin_object:
  10365. case token_type::end_array:
  10366. case token_type::end_object:
  10367. case token_type::name_separator:
  10368. case token_type::value_separator:
  10369. case token_type::parse_error:
  10370. case token_type::end_of_input:
  10371. case token_type::literal_or_value:
  10372. default:
  10373. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10374. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10375. }
  10376. }
  10377. ///////////////////////
  10378. // Utility functions //
  10379. ///////////////////////
  10380. /*!
  10381. @brief get next character from the input
  10382. This function provides the interface to the used input adapter. It does
  10383. not throw in case the input reached EOF, but returns a -'ve valued
  10384. `char_traits<char_type>::eof()` in that case.
  10385. @return character read from the input
  10386. */
  10387. char_int_type get()
  10388. {
  10389. ++chars_read;
  10390. return current = ia.get_character();
  10391. }
  10392. /*!
  10393. @return character read from the input after ignoring all 'N' entries
  10394. */
  10395. char_int_type get_ignore_noop()
  10396. {
  10397. do
  10398. {
  10399. get();
  10400. }
  10401. while (current == 'N');
  10402. return current;
  10403. }
  10404. /*
  10405. @brief read a number from the input
  10406. @tparam NumberType the type of the number
  10407. @param[in] format the current format (for diagnostics)
  10408. @param[out] result number of type @a NumberType
  10409. @return whether conversion completed
  10410. @note This function needs to respect the system's endianness, because
  10411. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  10412. (big endian) and therefore need reordering on little endian systems.
  10413. On the other hand, BSON and BJData use little endian and should reorder
  10414. on big endian systems.
  10415. */
  10416. template<typename NumberType, bool InputIsLittleEndian = false>
  10417. bool get_number(const input_format_t format, NumberType& result)
  10418. {
  10419. // step 1: read input into array with system's byte order
  10420. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  10421. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  10422. {
  10423. get();
  10424. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  10425. {
  10426. return false;
  10427. }
  10428. // reverse byte order prior to conversion if necessary
  10429. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  10430. {
  10431. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  10432. }
  10433. else
  10434. {
  10435. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  10436. }
  10437. }
  10438. // step 2: convert array into number of type T and return
  10439. std::memcpy(&result, vec.data(), sizeof(NumberType));
  10440. return true;
  10441. }
  10442. /*!
  10443. @brief create a string by reading characters from the input
  10444. @tparam NumberType the type of the number
  10445. @param[in] format the current format (for diagnostics)
  10446. @param[in] len number of characters to read
  10447. @param[out] result string created by reading @a len bytes
  10448. @return whether string creation completed
  10449. @note We can not reserve @a len bytes for the result, because @a len
  10450. may be too large. Usually, @ref unexpect_eof() detects the end of
  10451. the input before we run out of string memory.
  10452. */
  10453. template<typename NumberType>
  10454. bool get_string(const input_format_t format,
  10455. const NumberType len,
  10456. string_t& result)
  10457. {
  10458. bool success = true;
  10459. for (NumberType i = 0; i < len; i++)
  10460. {
  10461. get();
  10462. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  10463. {
  10464. success = false;
  10465. break;
  10466. }
  10467. result.push_back(static_cast<typename string_t::value_type>(current));
  10468. }
  10469. return success;
  10470. }
  10471. /*!
  10472. @brief create a byte array by reading bytes from the input
  10473. @tparam NumberType the type of the number
  10474. @param[in] format the current format (for diagnostics)
  10475. @param[in] len number of bytes to read
  10476. @param[out] result byte array created by reading @a len bytes
  10477. @return whether byte array creation completed
  10478. @note We can not reserve @a len bytes for the result, because @a len
  10479. may be too large. Usually, @ref unexpect_eof() detects the end of
  10480. the input before we run out of memory.
  10481. */
  10482. template<typename NumberType>
  10483. bool get_binary(const input_format_t format,
  10484. const NumberType len,
  10485. binary_t& result)
  10486. {
  10487. bool success = true;
  10488. for (NumberType i = 0; i < len; i++)
  10489. {
  10490. get();
  10491. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  10492. {
  10493. success = false;
  10494. break;
  10495. }
  10496. result.push_back(static_cast<std::uint8_t>(current));
  10497. }
  10498. return success;
  10499. }
  10500. /*!
  10501. @param[in] format the current format (for diagnostics)
  10502. @param[in] context further context information (for diagnostics)
  10503. @return whether the last read character is not EOF
  10504. */
  10505. JSON_HEDLEY_NON_NULL(3)
  10506. bool unexpect_eof(const input_format_t format, const char* context) const
  10507. {
  10508. if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
  10509. {
  10510. return sax->parse_error(chars_read, "<end of file>",
  10511. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10512. }
  10513. return true;
  10514. }
  10515. /*!
  10516. @return a string representation of the last read byte
  10517. */
  10518. std::string get_token_string() const
  10519. {
  10520. std::array<char, 3> cr{{}};
  10521. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  10522. return std::string{cr.data()};
  10523. }
  10524. /*!
  10525. @param[in] format the current format
  10526. @param[in] detail a detailed error message
  10527. @param[in] context further context information
  10528. @return a message string to use in the parse_error exceptions
  10529. */
  10530. std::string exception_message(const input_format_t format,
  10531. const std::string& detail,
  10532. const std::string& context) const
  10533. {
  10534. std::string error_msg = "syntax error while parsing ";
  10535. switch (format)
  10536. {
  10537. case input_format_t::cbor:
  10538. error_msg += "CBOR";
  10539. break;
  10540. case input_format_t::msgpack:
  10541. error_msg += "MessagePack";
  10542. break;
  10543. case input_format_t::ubjson:
  10544. error_msg += "UBJSON";
  10545. break;
  10546. case input_format_t::bson:
  10547. error_msg += "BSON";
  10548. break;
  10549. case input_format_t::bjdata:
  10550. error_msg += "BJData";
  10551. break;
  10552. case input_format_t::json: // LCOV_EXCL_LINE
  10553. default: // LCOV_EXCL_LINE
  10554. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  10555. }
  10556. return concat(error_msg, ' ', context, ": ", detail);
  10557. }
  10558. private:
  10559. static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
  10560. /// input adapter
  10561. InputAdapterType ia;
  10562. /// the current character
  10563. char_int_type current = char_traits<char_type>::eof();
  10564. /// the number of characters read
  10565. std::size_t chars_read = 0;
  10566. /// whether we can assume little endianness
  10567. const bool is_little_endian = little_endianness();
  10568. /// input format
  10569. const input_format_t input_format = input_format_t::json;
  10570. /// the SAX parser
  10571. json_sax_t* sax = nullptr;
  10572. // excluded markers in bjdata optimized type
  10573. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  10574. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  10575. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  10576. make_array<bjd_type>( \
  10577. bjd_type{'C', "char"}, \
  10578. bjd_type{'D', "double"}, \
  10579. bjd_type{'I', "int16"}, \
  10580. bjd_type{'L', "int64"}, \
  10581. bjd_type{'M', "uint64"}, \
  10582. bjd_type{'U', "uint8"}, \
  10583. bjd_type{'d', "single"}, \
  10584. bjd_type{'i', "int8"}, \
  10585. bjd_type{'l', "int32"}, \
  10586. bjd_type{'m', "uint32"}, \
  10587. bjd_type{'u', "uint16"})
  10588. JSON_PRIVATE_UNLESS_TESTED:
  10589. // lookup tables
  10590. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10591. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  10592. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  10593. using bjd_type = std::pair<char_int_type, string_t>;
  10594. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10595. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  10596. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  10597. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  10598. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  10599. };
  10600. #ifndef JSON_HAS_CPP_17
  10601. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  10602. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  10603. #endif
  10604. } // namespace detail
  10605. NLOHMANN_JSON_NAMESPACE_END
  10606. // #include <nlohmann/detail/input/input_adapters.hpp>
  10607. // #include <nlohmann/detail/input/lexer.hpp>
  10608. // #include <nlohmann/detail/input/parser.hpp>
  10609. // __ _____ _____ _____
  10610. // __| | __| | | | JSON for Modern C++
  10611. // | | |__ | | | | | | version 3.11.3
  10612. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10613. //
  10614. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  10615. // SPDX-License-Identifier: MIT
  10616. #include <cmath> // isfinite
  10617. #include <cstdint> // uint8_t
  10618. #include <functional> // function
  10619. #include <string> // string
  10620. #include <utility> // move
  10621. #include <vector> // vector
  10622. // #include <nlohmann/detail/exceptions.hpp>
  10623. // #include <nlohmann/detail/input/input_adapters.hpp>
  10624. // #include <nlohmann/detail/input/json_sax.hpp>
  10625. // #include <nlohmann/detail/input/lexer.hpp>
  10626. // #include <nlohmann/detail/macro_scope.hpp>
  10627. // #include <nlohmann/detail/meta/is_sax.hpp>
  10628. // #include <nlohmann/detail/string_concat.hpp>
  10629. // #include <nlohmann/detail/value_t.hpp>
  10630. NLOHMANN_JSON_NAMESPACE_BEGIN
  10631. namespace detail
  10632. {
  10633. ////////////
  10634. // parser //
  10635. ////////////
  10636. enum class parse_event_t : std::uint8_t
  10637. {
  10638. /// the parser read `{` and started to process a JSON object
  10639. object_start,
  10640. /// the parser read `}` and finished processing a JSON object
  10641. object_end,
  10642. /// the parser read `[` and started to process a JSON array
  10643. array_start,
  10644. /// the parser read `]` and finished processing a JSON array
  10645. array_end,
  10646. /// the parser read a key of a value in an object
  10647. key,
  10648. /// the parser finished reading a JSON value
  10649. value
  10650. };
  10651. template<typename BasicJsonType>
  10652. using parser_callback_t =
  10653. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  10654. /*!
  10655. @brief syntax analysis
  10656. This class implements a recursive descent parser.
  10657. */
  10658. template<typename BasicJsonType, typename InputAdapterType>
  10659. class parser
  10660. {
  10661. using number_integer_t = typename BasicJsonType::number_integer_t;
  10662. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  10663. using number_float_t = typename BasicJsonType::number_float_t;
  10664. using string_t = typename BasicJsonType::string_t;
  10665. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  10666. using token_type = typename lexer_t::token_type;
  10667. public:
  10668. /// a parser reading from an input adapter
  10669. explicit parser(InputAdapterType&& adapter,
  10670. const parser_callback_t<BasicJsonType> cb = nullptr,
  10671. const bool allow_exceptions_ = true,
  10672. const bool skip_comments = false)
  10673. : callback(cb)
  10674. , m_lexer(std::move(adapter), skip_comments)
  10675. , allow_exceptions(allow_exceptions_)
  10676. {
  10677. // read first token
  10678. get_token();
  10679. }
  10680. /*!
  10681. @brief public parser interface
  10682. @param[in] strict whether to expect the last token to be EOF
  10683. @param[in,out] result parsed JSON value
  10684. @throw parse_error.101 in case of an unexpected token
  10685. @throw parse_error.102 if to_unicode fails or surrogate error
  10686. @throw parse_error.103 if to_unicode fails
  10687. */
  10688. void parse(const bool strict, BasicJsonType& result)
  10689. {
  10690. if (callback)
  10691. {
  10692. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  10693. sax_parse_internal(&sdp);
  10694. // in strict mode, input must be completely read
  10695. if (strict && (get_token() != token_type::end_of_input))
  10696. {
  10697. sdp.parse_error(m_lexer.get_position(),
  10698. m_lexer.get_token_string(),
  10699. parse_error::create(101, m_lexer.get_position(),
  10700. exception_message(token_type::end_of_input, "value"), nullptr));
  10701. }
  10702. // in case of an error, return discarded value
  10703. if (sdp.is_errored())
  10704. {
  10705. result = value_t::discarded;
  10706. return;
  10707. }
  10708. // set top-level value to null if it was discarded by the callback
  10709. // function
  10710. if (result.is_discarded())
  10711. {
  10712. result = nullptr;
  10713. }
  10714. }
  10715. else
  10716. {
  10717. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  10718. sax_parse_internal(&sdp);
  10719. // in strict mode, input must be completely read
  10720. if (strict && (get_token() != token_type::end_of_input))
  10721. {
  10722. sdp.parse_error(m_lexer.get_position(),
  10723. m_lexer.get_token_string(),
  10724. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10725. }
  10726. // in case of an error, return discarded value
  10727. if (sdp.is_errored())
  10728. {
  10729. result = value_t::discarded;
  10730. return;
  10731. }
  10732. }
  10733. result.assert_invariant();
  10734. }
  10735. /*!
  10736. @brief public accept interface
  10737. @param[in] strict whether to expect the last token to be EOF
  10738. @return whether the input is a proper JSON text
  10739. */
  10740. bool accept(const bool strict = true)
  10741. {
  10742. json_sax_acceptor<BasicJsonType> sax_acceptor;
  10743. return sax_parse(&sax_acceptor, strict);
  10744. }
  10745. template<typename SAX>
  10746. JSON_HEDLEY_NON_NULL(2)
  10747. bool sax_parse(SAX* sax, const bool strict = true)
  10748. {
  10749. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  10750. const bool result = sax_parse_internal(sax);
  10751. // strict mode: next byte must be EOF
  10752. if (result && strict && (get_token() != token_type::end_of_input))
  10753. {
  10754. return sax->parse_error(m_lexer.get_position(),
  10755. m_lexer.get_token_string(),
  10756. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10757. }
  10758. return result;
  10759. }
  10760. private:
  10761. template<typename SAX>
  10762. JSON_HEDLEY_NON_NULL(2)
  10763. bool sax_parse_internal(SAX* sax)
  10764. {
  10765. // stack to remember the hierarchy of structured values we are parsing
  10766. // true = array; false = object
  10767. std::vector<bool> states;
  10768. // value to avoid a goto (see comment where set to true)
  10769. bool skip_to_state_evaluation = false;
  10770. while (true)
  10771. {
  10772. if (!skip_to_state_evaluation)
  10773. {
  10774. // invariant: get_token() was called before each iteration
  10775. switch (last_token)
  10776. {
  10777. case token_type::begin_object:
  10778. {
  10779. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10780. {
  10781. return false;
  10782. }
  10783. // closing } -> we are done
  10784. if (get_token() == token_type::end_object)
  10785. {
  10786. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10787. {
  10788. return false;
  10789. }
  10790. break;
  10791. }
  10792. // parse key
  10793. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  10794. {
  10795. return sax->parse_error(m_lexer.get_position(),
  10796. m_lexer.get_token_string(),
  10797. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10798. }
  10799. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10800. {
  10801. return false;
  10802. }
  10803. // parse separator (:)
  10804. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10805. {
  10806. return sax->parse_error(m_lexer.get_position(),
  10807. m_lexer.get_token_string(),
  10808. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10809. }
  10810. // remember we are now inside an object
  10811. states.push_back(false);
  10812. // parse values
  10813. get_token();
  10814. continue;
  10815. }
  10816. case token_type::begin_array:
  10817. {
  10818. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10819. {
  10820. return false;
  10821. }
  10822. // closing ] -> we are done
  10823. if (get_token() == token_type::end_array)
  10824. {
  10825. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10826. {
  10827. return false;
  10828. }
  10829. break;
  10830. }
  10831. // remember we are now inside an array
  10832. states.push_back(true);
  10833. // parse values (no need to call get_token)
  10834. continue;
  10835. }
  10836. case token_type::value_float:
  10837. {
  10838. const auto res = m_lexer.get_number_float();
  10839. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  10840. {
  10841. return sax->parse_error(m_lexer.get_position(),
  10842. m_lexer.get_token_string(),
  10843. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  10844. }
  10845. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  10846. {
  10847. return false;
  10848. }
  10849. break;
  10850. }
  10851. case token_type::literal_false:
  10852. {
  10853. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  10854. {
  10855. return false;
  10856. }
  10857. break;
  10858. }
  10859. case token_type::literal_null:
  10860. {
  10861. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  10862. {
  10863. return false;
  10864. }
  10865. break;
  10866. }
  10867. case token_type::literal_true:
  10868. {
  10869. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  10870. {
  10871. return false;
  10872. }
  10873. break;
  10874. }
  10875. case token_type::value_integer:
  10876. {
  10877. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  10878. {
  10879. return false;
  10880. }
  10881. break;
  10882. }
  10883. case token_type::value_string:
  10884. {
  10885. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  10886. {
  10887. return false;
  10888. }
  10889. break;
  10890. }
  10891. case token_type::value_unsigned:
  10892. {
  10893. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  10894. {
  10895. return false;
  10896. }
  10897. break;
  10898. }
  10899. case token_type::parse_error:
  10900. {
  10901. // using "uninitialized" to avoid "expected" message
  10902. return sax->parse_error(m_lexer.get_position(),
  10903. m_lexer.get_token_string(),
  10904. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  10905. }
  10906. case token_type::end_of_input:
  10907. {
  10908. if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
  10909. {
  10910. return sax->parse_error(m_lexer.get_position(),
  10911. m_lexer.get_token_string(),
  10912. parse_error::create(101, m_lexer.get_position(),
  10913. "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
  10914. }
  10915. return sax->parse_error(m_lexer.get_position(),
  10916. m_lexer.get_token_string(),
  10917. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10918. }
  10919. case token_type::uninitialized:
  10920. case token_type::end_array:
  10921. case token_type::end_object:
  10922. case token_type::name_separator:
  10923. case token_type::value_separator:
  10924. case token_type::literal_or_value:
  10925. default: // the last token was unexpected
  10926. {
  10927. return sax->parse_error(m_lexer.get_position(),
  10928. m_lexer.get_token_string(),
  10929. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10930. }
  10931. }
  10932. }
  10933. else
  10934. {
  10935. skip_to_state_evaluation = false;
  10936. }
  10937. // we reached this line after we successfully parsed a value
  10938. if (states.empty())
  10939. {
  10940. // empty stack: we reached the end of the hierarchy: done
  10941. return true;
  10942. }
  10943. if (states.back()) // array
  10944. {
  10945. // comma -> next value
  10946. if (get_token() == token_type::value_separator)
  10947. {
  10948. // parse a new value
  10949. get_token();
  10950. continue;
  10951. }
  10952. // closing ]
  10953. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  10954. {
  10955. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10956. {
  10957. return false;
  10958. }
  10959. // We are done with this array. Before we can parse a
  10960. // new value, we need to evaluate the new state first.
  10961. // By setting skip_to_state_evaluation to false, we
  10962. // are effectively jumping to the beginning of this if.
  10963. JSON_ASSERT(!states.empty());
  10964. states.pop_back();
  10965. skip_to_state_evaluation = true;
  10966. continue;
  10967. }
  10968. return sax->parse_error(m_lexer.get_position(),
  10969. m_lexer.get_token_string(),
  10970. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  10971. }
  10972. // states.back() is false -> object
  10973. // comma -> next value
  10974. if (get_token() == token_type::value_separator)
  10975. {
  10976. // parse key
  10977. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  10978. {
  10979. return sax->parse_error(m_lexer.get_position(),
  10980. m_lexer.get_token_string(),
  10981. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10982. }
  10983. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10984. {
  10985. return false;
  10986. }
  10987. // parse separator (:)
  10988. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10989. {
  10990. return sax->parse_error(m_lexer.get_position(),
  10991. m_lexer.get_token_string(),
  10992. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10993. }
  10994. // parse values
  10995. get_token();
  10996. continue;
  10997. }
  10998. // closing }
  10999. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  11000. {
  11001. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  11002. {
  11003. return false;
  11004. }
  11005. // We are done with this object. Before we can parse a
  11006. // new value, we need to evaluate the new state first.
  11007. // By setting skip_to_state_evaluation to false, we
  11008. // are effectively jumping to the beginning of this if.
  11009. JSON_ASSERT(!states.empty());
  11010. states.pop_back();
  11011. skip_to_state_evaluation = true;
  11012. continue;
  11013. }
  11014. return sax->parse_error(m_lexer.get_position(),
  11015. m_lexer.get_token_string(),
  11016. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  11017. }
  11018. }
  11019. /// get next token from lexer
  11020. token_type get_token()
  11021. {
  11022. return last_token = m_lexer.scan();
  11023. }
  11024. std::string exception_message(const token_type expected, const std::string& context)
  11025. {
  11026. std::string error_msg = "syntax error ";
  11027. if (!context.empty())
  11028. {
  11029. error_msg += concat("while parsing ", context, ' ');
  11030. }
  11031. error_msg += "- ";
  11032. if (last_token == token_type::parse_error)
  11033. {
  11034. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  11035. m_lexer.get_token_string(), '\'');
  11036. }
  11037. else
  11038. {
  11039. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  11040. }
  11041. if (expected != token_type::uninitialized)
  11042. {
  11043. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  11044. }
  11045. return error_msg;
  11046. }
  11047. private:
  11048. /// callback function
  11049. const parser_callback_t<BasicJsonType> callback = nullptr;
  11050. /// the type of the last read token
  11051. token_type last_token = token_type::uninitialized;
  11052. /// the lexer
  11053. lexer_t m_lexer;
  11054. /// whether to throw exceptions in case of errors
  11055. const bool allow_exceptions = true;
  11056. };
  11057. } // namespace detail
  11058. NLOHMANN_JSON_NAMESPACE_END
  11059. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11060. // __ _____ _____ _____
  11061. // __| | __| | | | JSON for Modern C++
  11062. // | | |__ | | | | | | version 3.11.3
  11063. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11064. //
  11065. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11066. // SPDX-License-Identifier: MIT
  11067. // #include <nlohmann/detail/abi_macros.hpp>
  11068. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11069. // __ _____ _____ _____
  11070. // __| | __| | | | JSON for Modern C++
  11071. // | | |__ | | | | | | version 3.11.3
  11072. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11073. //
  11074. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11075. // SPDX-License-Identifier: MIT
  11076. #include <cstddef> // ptrdiff_t
  11077. #include <limits> // numeric_limits
  11078. // #include <nlohmann/detail/macro_scope.hpp>
  11079. NLOHMANN_JSON_NAMESPACE_BEGIN
  11080. namespace detail
  11081. {
  11082. /*
  11083. @brief an iterator for primitive JSON types
  11084. This class models an iterator for primitive JSON types (boolean, number,
  11085. string). It's only purpose is to allow the iterator/const_iterator classes
  11086. to "iterate" over primitive values. Internally, the iterator is modeled by
  11087. a `difference_type` variable. Value begin_value (`0`) models the begin,
  11088. end_value (`1`) models past the end.
  11089. */
  11090. class primitive_iterator_t
  11091. {
  11092. private:
  11093. using difference_type = std::ptrdiff_t;
  11094. static constexpr difference_type begin_value = 0;
  11095. static constexpr difference_type end_value = begin_value + 1;
  11096. JSON_PRIVATE_UNLESS_TESTED:
  11097. /// iterator as signed integer type
  11098. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11099. public:
  11100. constexpr difference_type get_value() const noexcept
  11101. {
  11102. return m_it;
  11103. }
  11104. /// set iterator to a defined beginning
  11105. void set_begin() noexcept
  11106. {
  11107. m_it = begin_value;
  11108. }
  11109. /// set iterator to a defined past the end
  11110. void set_end() noexcept
  11111. {
  11112. m_it = end_value;
  11113. }
  11114. /// return whether the iterator can be dereferenced
  11115. constexpr bool is_begin() const noexcept
  11116. {
  11117. return m_it == begin_value;
  11118. }
  11119. /// return whether the iterator is at end
  11120. constexpr bool is_end() const noexcept
  11121. {
  11122. return m_it == end_value;
  11123. }
  11124. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11125. {
  11126. return lhs.m_it == rhs.m_it;
  11127. }
  11128. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11129. {
  11130. return lhs.m_it < rhs.m_it;
  11131. }
  11132. primitive_iterator_t operator+(difference_type n) noexcept
  11133. {
  11134. auto result = *this;
  11135. result += n;
  11136. return result;
  11137. }
  11138. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11139. {
  11140. return lhs.m_it - rhs.m_it;
  11141. }
  11142. primitive_iterator_t& operator++() noexcept
  11143. {
  11144. ++m_it;
  11145. return *this;
  11146. }
  11147. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11148. {
  11149. auto result = *this;
  11150. ++m_it;
  11151. return result;
  11152. }
  11153. primitive_iterator_t& operator--() noexcept
  11154. {
  11155. --m_it;
  11156. return *this;
  11157. }
  11158. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11159. {
  11160. auto result = *this;
  11161. --m_it;
  11162. return result;
  11163. }
  11164. primitive_iterator_t& operator+=(difference_type n) noexcept
  11165. {
  11166. m_it += n;
  11167. return *this;
  11168. }
  11169. primitive_iterator_t& operator-=(difference_type n) noexcept
  11170. {
  11171. m_it -= n;
  11172. return *this;
  11173. }
  11174. };
  11175. } // namespace detail
  11176. NLOHMANN_JSON_NAMESPACE_END
  11177. NLOHMANN_JSON_NAMESPACE_BEGIN
  11178. namespace detail
  11179. {
  11180. /*!
  11181. @brief an iterator value
  11182. @note This structure could easily be a union, but MSVC currently does not allow
  11183. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11184. */
  11185. template<typename BasicJsonType> struct internal_iterator
  11186. {
  11187. /// iterator for JSON objects
  11188. typename BasicJsonType::object_t::iterator object_iterator {};
  11189. /// iterator for JSON arrays
  11190. typename BasicJsonType::array_t::iterator array_iterator {};
  11191. /// generic iterator for all other types
  11192. primitive_iterator_t primitive_iterator {};
  11193. };
  11194. } // namespace detail
  11195. NLOHMANN_JSON_NAMESPACE_END
  11196. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11197. // __ _____ _____ _____
  11198. // __| | __| | | | JSON for Modern C++
  11199. // | | |__ | | | | | | version 3.11.3
  11200. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11201. //
  11202. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11203. // SPDX-License-Identifier: MIT
  11204. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11205. #include <type_traits> // conditional, is_const, remove_const
  11206. // #include <nlohmann/detail/exceptions.hpp>
  11207. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11208. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11209. // #include <nlohmann/detail/macro_scope.hpp>
  11210. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11211. // #include <nlohmann/detail/meta/type_traits.hpp>
  11212. // #include <nlohmann/detail/value_t.hpp>
  11213. NLOHMANN_JSON_NAMESPACE_BEGIN
  11214. namespace detail
  11215. {
  11216. // forward declare, to be able to friend it later on
  11217. template<typename IteratorType> class iteration_proxy;
  11218. template<typename IteratorType> class iteration_proxy_value;
  11219. /*!
  11220. @brief a template for a bidirectional iterator for the @ref basic_json class
  11221. This class implements a both iterators (iterator and const_iterator) for the
  11222. @ref basic_json class.
  11223. @note An iterator is called *initialized* when a pointer to a JSON value has
  11224. been set (e.g., by a constructor or a copy assignment). If the iterator is
  11225. default-constructed, it is *uninitialized* and most methods are undefined.
  11226. **The library uses assertions to detect calls on uninitialized iterators.**
  11227. @requirement The class satisfies the following concept requirements:
  11228. -
  11229. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11230. The iterator that can be moved can be moved in both directions (i.e.
  11231. incremented and decremented).
  11232. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  11233. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  11234. */
  11235. template<typename BasicJsonType>
  11236. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  11237. {
  11238. /// the iterator with BasicJsonType of different const-ness
  11239. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  11240. /// allow basic_json to access private members
  11241. friend other_iter_impl;
  11242. friend BasicJsonType;
  11243. friend iteration_proxy<iter_impl>;
  11244. friend iteration_proxy_value<iter_impl>;
  11245. using object_t = typename BasicJsonType::object_t;
  11246. using array_t = typename BasicJsonType::array_t;
  11247. // make sure BasicJsonType is basic_json or const basic_json
  11248. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  11249. "iter_impl only accepts (const) basic_json");
  11250. // superficial check for the LegacyBidirectionalIterator named requirement
  11251. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  11252. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  11253. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  11254. public:
  11255. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  11256. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  11257. /// A user-defined iterator should provide publicly accessible typedefs named
  11258. /// iterator_category, value_type, difference_type, pointer, and reference.
  11259. /// Note that value_type is required to be non-const, even for constant iterators.
  11260. using iterator_category = std::bidirectional_iterator_tag;
  11261. /// the type of the values when the iterator is dereferenced
  11262. using value_type = typename BasicJsonType::value_type;
  11263. /// a type to represent differences between iterators
  11264. using difference_type = typename BasicJsonType::difference_type;
  11265. /// defines a pointer to the type iterated over (value_type)
  11266. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  11267. typename BasicJsonType::const_pointer,
  11268. typename BasicJsonType::pointer>::type;
  11269. /// defines a reference to the type iterated over (value_type)
  11270. using reference =
  11271. typename std::conditional<std::is_const<BasicJsonType>::value,
  11272. typename BasicJsonType::const_reference,
  11273. typename BasicJsonType::reference>::type;
  11274. iter_impl() = default;
  11275. ~iter_impl() = default;
  11276. iter_impl(iter_impl&&) noexcept = default;
  11277. iter_impl& operator=(iter_impl&&) noexcept = default;
  11278. /*!
  11279. @brief constructor for a given JSON instance
  11280. @param[in] object pointer to a JSON object for this iterator
  11281. @pre object != nullptr
  11282. @post The iterator is initialized; i.e. `m_object != nullptr`.
  11283. */
  11284. explicit iter_impl(pointer object) noexcept : m_object(object)
  11285. {
  11286. JSON_ASSERT(m_object != nullptr);
  11287. switch (m_object->m_data.m_type)
  11288. {
  11289. case value_t::object:
  11290. {
  11291. m_it.object_iterator = typename object_t::iterator();
  11292. break;
  11293. }
  11294. case value_t::array:
  11295. {
  11296. m_it.array_iterator = typename array_t::iterator();
  11297. break;
  11298. }
  11299. case value_t::null:
  11300. case value_t::string:
  11301. case value_t::boolean:
  11302. case value_t::number_integer:
  11303. case value_t::number_unsigned:
  11304. case value_t::number_float:
  11305. case value_t::binary:
  11306. case value_t::discarded:
  11307. default:
  11308. {
  11309. m_it.primitive_iterator = primitive_iterator_t();
  11310. break;
  11311. }
  11312. }
  11313. }
  11314. /*!
  11315. @note The conventional copy constructor and copy assignment are implicitly
  11316. defined. Combined with the following converting constructor and
  11317. assignment, they support: (1) copy from iterator to iterator, (2)
  11318. copy from const iterator to const iterator, and (3) conversion from
  11319. iterator to const iterator. However conversion from const iterator
  11320. to iterator is not defined.
  11321. */
  11322. /*!
  11323. @brief const copy constructor
  11324. @param[in] other const iterator to copy from
  11325. @note This copy constructor had to be defined explicitly to circumvent a bug
  11326. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  11327. information refer to: https://github.com/nlohmann/json/issues/1608
  11328. */
  11329. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  11330. : m_object(other.m_object), m_it(other.m_it)
  11331. {}
  11332. /*!
  11333. @brief converting assignment
  11334. @param[in] other const iterator to copy from
  11335. @return const/non-const iterator
  11336. @note It is not checked whether @a other is initialized.
  11337. */
  11338. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  11339. {
  11340. if (&other != this)
  11341. {
  11342. m_object = other.m_object;
  11343. m_it = other.m_it;
  11344. }
  11345. return *this;
  11346. }
  11347. /*!
  11348. @brief converting constructor
  11349. @param[in] other non-const iterator to copy from
  11350. @note It is not checked whether @a other is initialized.
  11351. */
  11352. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  11353. : m_object(other.m_object), m_it(other.m_it)
  11354. {}
  11355. /*!
  11356. @brief converting assignment
  11357. @param[in] other non-const iterator to copy from
  11358. @return const/non-const iterator
  11359. @note It is not checked whether @a other is initialized.
  11360. */
  11361. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  11362. {
  11363. m_object = other.m_object;
  11364. m_it = other.m_it;
  11365. return *this;
  11366. }
  11367. JSON_PRIVATE_UNLESS_TESTED:
  11368. /*!
  11369. @brief set the iterator to the first value
  11370. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11371. */
  11372. void set_begin() noexcept
  11373. {
  11374. JSON_ASSERT(m_object != nullptr);
  11375. switch (m_object->m_data.m_type)
  11376. {
  11377. case value_t::object:
  11378. {
  11379. m_it.object_iterator = m_object->m_data.m_value.object->begin();
  11380. break;
  11381. }
  11382. case value_t::array:
  11383. {
  11384. m_it.array_iterator = m_object->m_data.m_value.array->begin();
  11385. break;
  11386. }
  11387. case value_t::null:
  11388. {
  11389. // set to end so begin()==end() is true: null is empty
  11390. m_it.primitive_iterator.set_end();
  11391. break;
  11392. }
  11393. case value_t::string:
  11394. case value_t::boolean:
  11395. case value_t::number_integer:
  11396. case value_t::number_unsigned:
  11397. case value_t::number_float:
  11398. case value_t::binary:
  11399. case value_t::discarded:
  11400. default:
  11401. {
  11402. m_it.primitive_iterator.set_begin();
  11403. break;
  11404. }
  11405. }
  11406. }
  11407. /*!
  11408. @brief set the iterator past the last value
  11409. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11410. */
  11411. void set_end() noexcept
  11412. {
  11413. JSON_ASSERT(m_object != nullptr);
  11414. switch (m_object->m_data.m_type)
  11415. {
  11416. case value_t::object:
  11417. {
  11418. m_it.object_iterator = m_object->m_data.m_value.object->end();
  11419. break;
  11420. }
  11421. case value_t::array:
  11422. {
  11423. m_it.array_iterator = m_object->m_data.m_value.array->end();
  11424. break;
  11425. }
  11426. case value_t::null:
  11427. case value_t::string:
  11428. case value_t::boolean:
  11429. case value_t::number_integer:
  11430. case value_t::number_unsigned:
  11431. case value_t::number_float:
  11432. case value_t::binary:
  11433. case value_t::discarded:
  11434. default:
  11435. {
  11436. m_it.primitive_iterator.set_end();
  11437. break;
  11438. }
  11439. }
  11440. }
  11441. public:
  11442. /*!
  11443. @brief return a reference to the value pointed to by the iterator
  11444. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11445. */
  11446. reference operator*() const
  11447. {
  11448. JSON_ASSERT(m_object != nullptr);
  11449. switch (m_object->m_data.m_type)
  11450. {
  11451. case value_t::object:
  11452. {
  11453. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  11454. return m_it.object_iterator->second;
  11455. }
  11456. case value_t::array:
  11457. {
  11458. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  11459. return *m_it.array_iterator;
  11460. }
  11461. case value_t::null:
  11462. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11463. case value_t::string:
  11464. case value_t::boolean:
  11465. case value_t::number_integer:
  11466. case value_t::number_unsigned:
  11467. case value_t::number_float:
  11468. case value_t::binary:
  11469. case value_t::discarded:
  11470. default:
  11471. {
  11472. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11473. {
  11474. return *m_object;
  11475. }
  11476. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11477. }
  11478. }
  11479. }
  11480. /*!
  11481. @brief dereference the iterator
  11482. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11483. */
  11484. pointer operator->() const
  11485. {
  11486. JSON_ASSERT(m_object != nullptr);
  11487. switch (m_object->m_data.m_type)
  11488. {
  11489. case value_t::object:
  11490. {
  11491. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  11492. return &(m_it.object_iterator->second);
  11493. }
  11494. case value_t::array:
  11495. {
  11496. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  11497. return &*m_it.array_iterator;
  11498. }
  11499. case value_t::null:
  11500. case value_t::string:
  11501. case value_t::boolean:
  11502. case value_t::number_integer:
  11503. case value_t::number_unsigned:
  11504. case value_t::number_float:
  11505. case value_t::binary:
  11506. case value_t::discarded:
  11507. default:
  11508. {
  11509. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11510. {
  11511. return m_object;
  11512. }
  11513. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11514. }
  11515. }
  11516. }
  11517. /*!
  11518. @brief post-increment (it++)
  11519. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11520. */
  11521. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  11522. {
  11523. auto result = *this;
  11524. ++(*this);
  11525. return result;
  11526. }
  11527. /*!
  11528. @brief pre-increment (++it)
  11529. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11530. */
  11531. iter_impl& operator++()
  11532. {
  11533. JSON_ASSERT(m_object != nullptr);
  11534. switch (m_object->m_data.m_type)
  11535. {
  11536. case value_t::object:
  11537. {
  11538. std::advance(m_it.object_iterator, 1);
  11539. break;
  11540. }
  11541. case value_t::array:
  11542. {
  11543. std::advance(m_it.array_iterator, 1);
  11544. break;
  11545. }
  11546. case value_t::null:
  11547. case value_t::string:
  11548. case value_t::boolean:
  11549. case value_t::number_integer:
  11550. case value_t::number_unsigned:
  11551. case value_t::number_float:
  11552. case value_t::binary:
  11553. case value_t::discarded:
  11554. default:
  11555. {
  11556. ++m_it.primitive_iterator;
  11557. break;
  11558. }
  11559. }
  11560. return *this;
  11561. }
  11562. /*!
  11563. @brief post-decrement (it--)
  11564. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11565. */
  11566. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  11567. {
  11568. auto result = *this;
  11569. --(*this);
  11570. return result;
  11571. }
  11572. /*!
  11573. @brief pre-decrement (--it)
  11574. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11575. */
  11576. iter_impl& operator--()
  11577. {
  11578. JSON_ASSERT(m_object != nullptr);
  11579. switch (m_object->m_data.m_type)
  11580. {
  11581. case value_t::object:
  11582. {
  11583. std::advance(m_it.object_iterator, -1);
  11584. break;
  11585. }
  11586. case value_t::array:
  11587. {
  11588. std::advance(m_it.array_iterator, -1);
  11589. break;
  11590. }
  11591. case value_t::null:
  11592. case value_t::string:
  11593. case value_t::boolean:
  11594. case value_t::number_integer:
  11595. case value_t::number_unsigned:
  11596. case value_t::number_float:
  11597. case value_t::binary:
  11598. case value_t::discarded:
  11599. default:
  11600. {
  11601. --m_it.primitive_iterator;
  11602. break;
  11603. }
  11604. }
  11605. return *this;
  11606. }
  11607. /*!
  11608. @brief comparison: equal
  11609. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11610. */
  11611. 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 >
  11612. bool operator==(const IterImpl& other) const
  11613. {
  11614. // if objects are not the same, the comparison is undefined
  11615. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11616. {
  11617. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11618. }
  11619. JSON_ASSERT(m_object != nullptr);
  11620. switch (m_object->m_data.m_type)
  11621. {
  11622. case value_t::object:
  11623. return (m_it.object_iterator == other.m_it.object_iterator);
  11624. case value_t::array:
  11625. return (m_it.array_iterator == other.m_it.array_iterator);
  11626. case value_t::null:
  11627. case value_t::string:
  11628. case value_t::boolean:
  11629. case value_t::number_integer:
  11630. case value_t::number_unsigned:
  11631. case value_t::number_float:
  11632. case value_t::binary:
  11633. case value_t::discarded:
  11634. default:
  11635. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  11636. }
  11637. }
  11638. /*!
  11639. @brief comparison: not equal
  11640. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11641. */
  11642. 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 >
  11643. bool operator!=(const IterImpl& other) const
  11644. {
  11645. return !operator==(other);
  11646. }
  11647. /*!
  11648. @brief comparison: smaller
  11649. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11650. */
  11651. bool operator<(const iter_impl& other) const
  11652. {
  11653. // if objects are not the same, the comparison is undefined
  11654. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11655. {
  11656. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11657. }
  11658. JSON_ASSERT(m_object != nullptr);
  11659. switch (m_object->m_data.m_type)
  11660. {
  11661. case value_t::object:
  11662. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  11663. case value_t::array:
  11664. return (m_it.array_iterator < other.m_it.array_iterator);
  11665. case value_t::null:
  11666. case value_t::string:
  11667. case value_t::boolean:
  11668. case value_t::number_integer:
  11669. case value_t::number_unsigned:
  11670. case value_t::number_float:
  11671. case value_t::binary:
  11672. case value_t::discarded:
  11673. default:
  11674. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  11675. }
  11676. }
  11677. /*!
  11678. @brief comparison: less than or equal
  11679. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11680. */
  11681. bool operator<=(const iter_impl& other) const
  11682. {
  11683. return !other.operator < (*this);
  11684. }
  11685. /*!
  11686. @brief comparison: greater than
  11687. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11688. */
  11689. bool operator>(const iter_impl& other) const
  11690. {
  11691. return !operator<=(other);
  11692. }
  11693. /*!
  11694. @brief comparison: greater than or equal
  11695. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11696. */
  11697. bool operator>=(const iter_impl& other) const
  11698. {
  11699. return !operator<(other);
  11700. }
  11701. /*!
  11702. @brief add to iterator
  11703. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11704. */
  11705. iter_impl& operator+=(difference_type i)
  11706. {
  11707. JSON_ASSERT(m_object != nullptr);
  11708. switch (m_object->m_data.m_type)
  11709. {
  11710. case value_t::object:
  11711. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11712. case value_t::array:
  11713. {
  11714. std::advance(m_it.array_iterator, i);
  11715. break;
  11716. }
  11717. case value_t::null:
  11718. case value_t::string:
  11719. case value_t::boolean:
  11720. case value_t::number_integer:
  11721. case value_t::number_unsigned:
  11722. case value_t::number_float:
  11723. case value_t::binary:
  11724. case value_t::discarded:
  11725. default:
  11726. {
  11727. m_it.primitive_iterator += i;
  11728. break;
  11729. }
  11730. }
  11731. return *this;
  11732. }
  11733. /*!
  11734. @brief subtract from iterator
  11735. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11736. */
  11737. iter_impl& operator-=(difference_type i)
  11738. {
  11739. return operator+=(-i);
  11740. }
  11741. /*!
  11742. @brief add to iterator
  11743. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11744. */
  11745. iter_impl operator+(difference_type i) const
  11746. {
  11747. auto result = *this;
  11748. result += i;
  11749. return result;
  11750. }
  11751. /*!
  11752. @brief addition of distance and iterator
  11753. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11754. */
  11755. friend iter_impl operator+(difference_type i, const iter_impl& it)
  11756. {
  11757. auto result = it;
  11758. result += i;
  11759. return result;
  11760. }
  11761. /*!
  11762. @brief subtract from iterator
  11763. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11764. */
  11765. iter_impl operator-(difference_type i) const
  11766. {
  11767. auto result = *this;
  11768. result -= i;
  11769. return result;
  11770. }
  11771. /*!
  11772. @brief return difference
  11773. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11774. */
  11775. difference_type operator-(const iter_impl& other) const
  11776. {
  11777. JSON_ASSERT(m_object != nullptr);
  11778. switch (m_object->m_data.m_type)
  11779. {
  11780. case value_t::object:
  11781. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11782. case value_t::array:
  11783. return m_it.array_iterator - other.m_it.array_iterator;
  11784. case value_t::null:
  11785. case value_t::string:
  11786. case value_t::boolean:
  11787. case value_t::number_integer:
  11788. case value_t::number_unsigned:
  11789. case value_t::number_float:
  11790. case value_t::binary:
  11791. case value_t::discarded:
  11792. default:
  11793. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  11794. }
  11795. }
  11796. /*!
  11797. @brief access to successor
  11798. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11799. */
  11800. reference operator[](difference_type n) const
  11801. {
  11802. JSON_ASSERT(m_object != nullptr);
  11803. switch (m_object->m_data.m_type)
  11804. {
  11805. case value_t::object:
  11806. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  11807. case value_t::array:
  11808. return *std::next(m_it.array_iterator, n);
  11809. case value_t::null:
  11810. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11811. case value_t::string:
  11812. case value_t::boolean:
  11813. case value_t::number_integer:
  11814. case value_t::number_unsigned:
  11815. case value_t::number_float:
  11816. case value_t::binary:
  11817. case value_t::discarded:
  11818. default:
  11819. {
  11820. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  11821. {
  11822. return *m_object;
  11823. }
  11824. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11825. }
  11826. }
  11827. }
  11828. /*!
  11829. @brief return the key of an object iterator
  11830. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11831. */
  11832. const typename object_t::key_type& key() const
  11833. {
  11834. JSON_ASSERT(m_object != nullptr);
  11835. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  11836. {
  11837. return m_it.object_iterator->first;
  11838. }
  11839. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  11840. }
  11841. /*!
  11842. @brief return the value of an iterator
  11843. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11844. */
  11845. reference value() const
  11846. {
  11847. return operator*();
  11848. }
  11849. JSON_PRIVATE_UNLESS_TESTED:
  11850. /// associated JSON instance
  11851. pointer m_object = nullptr;
  11852. /// the actual iterator of the associated instance
  11853. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  11854. };
  11855. } // namespace detail
  11856. NLOHMANN_JSON_NAMESPACE_END
  11857. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  11858. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  11859. // __ _____ _____ _____
  11860. // __| | __| | | | JSON for Modern C++
  11861. // | | |__ | | | | | | version 3.11.3
  11862. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11863. //
  11864. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11865. // SPDX-License-Identifier: MIT
  11866. #include <cstddef> // ptrdiff_t
  11867. #include <iterator> // reverse_iterator
  11868. #include <utility> // declval
  11869. // #include <nlohmann/detail/abi_macros.hpp>
  11870. NLOHMANN_JSON_NAMESPACE_BEGIN
  11871. namespace detail
  11872. {
  11873. //////////////////////
  11874. // reverse_iterator //
  11875. //////////////////////
  11876. /*!
  11877. @brief a template for a reverse iterator class
  11878. @tparam Base the base iterator type to reverse. Valid types are @ref
  11879. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  11880. create @ref const_reverse_iterator).
  11881. @requirement The class satisfies the following concept requirements:
  11882. -
  11883. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11884. The iterator that can be moved can be moved in both directions (i.e.
  11885. incremented and decremented).
  11886. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  11887. It is possible to write to the pointed-to element (only if @a Base is
  11888. @ref iterator).
  11889. @since version 1.0.0
  11890. */
  11891. template<typename Base>
  11892. class json_reverse_iterator : public std::reverse_iterator<Base>
  11893. {
  11894. public:
  11895. using difference_type = std::ptrdiff_t;
  11896. /// shortcut to the reverse iterator adapter
  11897. using base_iterator = std::reverse_iterator<Base>;
  11898. /// the reference type for the pointed-to element
  11899. using reference = typename Base::reference;
  11900. /// create reverse iterator from iterator
  11901. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  11902. : base_iterator(it) {}
  11903. /// create reverse iterator from base class
  11904. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  11905. /// post-increment (it++)
  11906. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  11907. {
  11908. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  11909. }
  11910. /// pre-increment (++it)
  11911. json_reverse_iterator& operator++()
  11912. {
  11913. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  11914. }
  11915. /// post-decrement (it--)
  11916. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  11917. {
  11918. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  11919. }
  11920. /// pre-decrement (--it)
  11921. json_reverse_iterator& operator--()
  11922. {
  11923. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  11924. }
  11925. /// add to iterator
  11926. json_reverse_iterator& operator+=(difference_type i)
  11927. {
  11928. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  11929. }
  11930. /// add to iterator
  11931. json_reverse_iterator operator+(difference_type i) const
  11932. {
  11933. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  11934. }
  11935. /// subtract from iterator
  11936. json_reverse_iterator operator-(difference_type i) const
  11937. {
  11938. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  11939. }
  11940. /// return difference
  11941. difference_type operator-(const json_reverse_iterator& other) const
  11942. {
  11943. return base_iterator(*this) - base_iterator(other);
  11944. }
  11945. /// access to successor
  11946. reference operator[](difference_type n) const
  11947. {
  11948. return *(this->operator+(n));
  11949. }
  11950. /// return the key of an object iterator
  11951. auto key() const -> decltype(std::declval<Base>().key())
  11952. {
  11953. auto it = --this->base();
  11954. return it.key();
  11955. }
  11956. /// return the value of an iterator
  11957. reference value() const
  11958. {
  11959. auto it = --this->base();
  11960. return it.operator * ();
  11961. }
  11962. };
  11963. } // namespace detail
  11964. NLOHMANN_JSON_NAMESPACE_END
  11965. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11966. // #include <nlohmann/detail/json_custom_base_class.hpp>
  11967. // __ _____ _____ _____
  11968. // __| | __| | | | JSON for Modern C++
  11969. // | | |__ | | | | | | version 3.11.3
  11970. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11971. //
  11972. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11973. // SPDX-License-Identifier: MIT
  11974. #include <type_traits> // conditional, is_same
  11975. // #include <nlohmann/detail/abi_macros.hpp>
  11976. NLOHMANN_JSON_NAMESPACE_BEGIN
  11977. namespace detail
  11978. {
  11979. /*!
  11980. @brief Default base class of the @ref basic_json class.
  11981. So that the correct implementations of the copy / move ctors / assign operators
  11982. of @ref basic_json do not require complex case distinctions
  11983. (no base class / custom base class used as customization point),
  11984. @ref basic_json always has a base class.
  11985. By default, this class is used because it is empty and thus has no effect
  11986. on the behavior of @ref basic_json.
  11987. */
  11988. struct json_default_base {};
  11989. template<class T>
  11990. using json_base_class = typename std::conditional <
  11991. std::is_same<T, void>::value,
  11992. json_default_base,
  11993. T
  11994. >::type;
  11995. } // namespace detail
  11996. NLOHMANN_JSON_NAMESPACE_END
  11997. // #include <nlohmann/detail/json_pointer.hpp>
  11998. // __ _____ _____ _____
  11999. // __| | __| | | | JSON for Modern C++
  12000. // | | |__ | | | | | | version 3.11.3
  12001. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12002. //
  12003. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12004. // SPDX-License-Identifier: MIT
  12005. #include <algorithm> // all_of
  12006. #include <cctype> // isdigit
  12007. #include <cerrno> // errno, ERANGE
  12008. #include <cstdlib> // strtoull
  12009. #ifndef JSON_NO_IO
  12010. #include <iosfwd> // ostream
  12011. #endif // JSON_NO_IO
  12012. #include <limits> // max
  12013. #include <numeric> // accumulate
  12014. #include <string> // string
  12015. #include <utility> // move
  12016. #include <vector> // vector
  12017. // #include <nlohmann/detail/exceptions.hpp>
  12018. // #include <nlohmann/detail/macro_scope.hpp>
  12019. // #include <nlohmann/detail/string_concat.hpp>
  12020. // #include <nlohmann/detail/string_escape.hpp>
  12021. // #include <nlohmann/detail/value_t.hpp>
  12022. NLOHMANN_JSON_NAMESPACE_BEGIN
  12023. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  12024. /// @sa https://json.nlohmann.me/api/json_pointer/
  12025. template<typename RefStringType>
  12026. class json_pointer
  12027. {
  12028. // allow basic_json to access private members
  12029. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12030. friend class basic_json;
  12031. template<typename>
  12032. friend class json_pointer;
  12033. template<typename T>
  12034. struct string_t_helper
  12035. {
  12036. using type = T;
  12037. };
  12038. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12039. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  12040. {
  12041. using type = StringType;
  12042. };
  12043. public:
  12044. // for backwards compatibility accept BasicJsonType
  12045. using string_t = typename string_t_helper<RefStringType>::type;
  12046. /// @brief create JSON pointer
  12047. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  12048. explicit json_pointer(const string_t& s = "")
  12049. : reference_tokens(split(s))
  12050. {}
  12051. /// @brief return a string representation of the JSON pointer
  12052. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  12053. string_t to_string() const
  12054. {
  12055. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  12056. string_t{},
  12057. [](const string_t& a, const string_t& b)
  12058. {
  12059. return detail::concat(a, '/', detail::escape(b));
  12060. });
  12061. }
  12062. /// @brief return a string representation of the JSON pointer
  12063. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  12064. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  12065. operator string_t() const
  12066. {
  12067. return to_string();
  12068. }
  12069. #ifndef JSON_NO_IO
  12070. /// @brief write string representation of the JSON pointer to stream
  12071. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  12072. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  12073. {
  12074. o << ptr.to_string();
  12075. return o;
  12076. }
  12077. #endif
  12078. /// @brief append another JSON pointer at the end of this JSON pointer
  12079. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12080. json_pointer& operator/=(const json_pointer& ptr)
  12081. {
  12082. reference_tokens.insert(reference_tokens.end(),
  12083. ptr.reference_tokens.begin(),
  12084. ptr.reference_tokens.end());
  12085. return *this;
  12086. }
  12087. /// @brief append an unescaped reference token at the end of this JSON pointer
  12088. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12089. json_pointer& operator/=(string_t token)
  12090. {
  12091. push_back(std::move(token));
  12092. return *this;
  12093. }
  12094. /// @brief append an array index at the end of this JSON pointer
  12095. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12096. json_pointer& operator/=(std::size_t array_idx)
  12097. {
  12098. return *this /= std::to_string(array_idx);
  12099. }
  12100. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  12101. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12102. friend json_pointer operator/(const json_pointer& lhs,
  12103. const json_pointer& rhs)
  12104. {
  12105. return json_pointer(lhs) /= rhs;
  12106. }
  12107. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  12108. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12109. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12110. {
  12111. return json_pointer(lhs) /= std::move(token);
  12112. }
  12113. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12114. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12115. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12116. {
  12117. return json_pointer(lhs) /= array_idx;
  12118. }
  12119. /// @brief returns the parent of this JSON pointer
  12120. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12121. json_pointer parent_pointer() const
  12122. {
  12123. if (empty())
  12124. {
  12125. return *this;
  12126. }
  12127. json_pointer res = *this;
  12128. res.pop_back();
  12129. return res;
  12130. }
  12131. /// @brief remove last reference token
  12132. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12133. void pop_back()
  12134. {
  12135. if (JSON_HEDLEY_UNLIKELY(empty()))
  12136. {
  12137. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12138. }
  12139. reference_tokens.pop_back();
  12140. }
  12141. /// @brief return last reference token
  12142. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12143. const string_t& back() const
  12144. {
  12145. if (JSON_HEDLEY_UNLIKELY(empty()))
  12146. {
  12147. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12148. }
  12149. return reference_tokens.back();
  12150. }
  12151. /// @brief append an unescaped token at the end of the reference pointer
  12152. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12153. void push_back(const string_t& token)
  12154. {
  12155. reference_tokens.push_back(token);
  12156. }
  12157. /// @brief append an unescaped token at the end of the reference pointer
  12158. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12159. void push_back(string_t&& token)
  12160. {
  12161. reference_tokens.push_back(std::move(token));
  12162. }
  12163. /// @brief return whether pointer points to the root document
  12164. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12165. bool empty() const noexcept
  12166. {
  12167. return reference_tokens.empty();
  12168. }
  12169. private:
  12170. /*!
  12171. @param[in] s reference token to be converted into an array index
  12172. @return integer representation of @a s
  12173. @throw parse_error.106 if an array index begins with '0'
  12174. @throw parse_error.109 if an array index begins not with a digit
  12175. @throw out_of_range.404 if string @a s could not be converted to an integer
  12176. @throw out_of_range.410 if an array index exceeds size_type
  12177. */
  12178. template<typename BasicJsonType>
  12179. static typename BasicJsonType::size_type array_index(const string_t& s)
  12180. {
  12181. using size_type = typename BasicJsonType::size_type;
  12182. // error condition (cf. RFC 6901, Sect. 4)
  12183. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12184. {
  12185. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12186. }
  12187. // error condition (cf. RFC 6901, Sect. 4)
  12188. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12189. {
  12190. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12191. }
  12192. const char* p = s.c_str();
  12193. char* p_end = nullptr;
  12194. errno = 0; // strtoull doesn't reset errno
  12195. const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12196. if (p == p_end // invalid input or empty string
  12197. || errno == ERANGE // out of range
  12198. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12199. {
  12200. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12201. }
  12202. // only triggered on special platforms (like 32bit), see also
  12203. // https://github.com/nlohmann/json/pull/2203
  12204. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12205. {
  12206. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12207. }
  12208. return static_cast<size_type>(res);
  12209. }
  12210. JSON_PRIVATE_UNLESS_TESTED:
  12211. json_pointer top() const
  12212. {
  12213. if (JSON_HEDLEY_UNLIKELY(empty()))
  12214. {
  12215. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12216. }
  12217. json_pointer result = *this;
  12218. result.reference_tokens = {reference_tokens[0]};
  12219. return result;
  12220. }
  12221. private:
  12222. /*!
  12223. @brief create and return a reference to the pointed to value
  12224. @complexity Linear in the number of reference tokens.
  12225. @throw parse_error.109 if array index is not a number
  12226. @throw type_error.313 if value cannot be unflattened
  12227. */
  12228. template<typename BasicJsonType>
  12229. BasicJsonType& get_and_create(BasicJsonType& j) const
  12230. {
  12231. auto* result = &j;
  12232. // in case no reference tokens exist, return a reference to the JSON value
  12233. // j which will be overwritten by a primitive value
  12234. for (const auto& reference_token : reference_tokens)
  12235. {
  12236. switch (result->type())
  12237. {
  12238. case detail::value_t::null:
  12239. {
  12240. if (reference_token == "0")
  12241. {
  12242. // start a new array if reference token is 0
  12243. result = &result->operator[](0);
  12244. }
  12245. else
  12246. {
  12247. // start a new object otherwise
  12248. result = &result->operator[](reference_token);
  12249. }
  12250. break;
  12251. }
  12252. case detail::value_t::object:
  12253. {
  12254. // create an entry in the object
  12255. result = &result->operator[](reference_token);
  12256. break;
  12257. }
  12258. case detail::value_t::array:
  12259. {
  12260. // create an entry in the array
  12261. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  12262. break;
  12263. }
  12264. /*
  12265. The following code is only reached if there exists a reference
  12266. token _and_ the current value is primitive. In this case, we have
  12267. an error situation, because primitive values may only occur as
  12268. single value; that is, with an empty list of reference tokens.
  12269. */
  12270. case detail::value_t::string:
  12271. case detail::value_t::boolean:
  12272. case detail::value_t::number_integer:
  12273. case detail::value_t::number_unsigned:
  12274. case detail::value_t::number_float:
  12275. case detail::value_t::binary:
  12276. case detail::value_t::discarded:
  12277. default:
  12278. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  12279. }
  12280. }
  12281. return *result;
  12282. }
  12283. /*!
  12284. @brief return a reference to the pointed to value
  12285. @note This version does not throw if a value is not present, but tries to
  12286. create nested values instead. For instance, calling this function
  12287. with pointer `"/this/that"` on a null value is equivalent to calling
  12288. `operator[]("this").operator[]("that")` on that value, effectively
  12289. changing the null value to an object.
  12290. @param[in] ptr a JSON value
  12291. @return reference to the JSON value pointed to by the JSON pointer
  12292. @complexity Linear in the length of the JSON pointer.
  12293. @throw parse_error.106 if an array index begins with '0'
  12294. @throw parse_error.109 if an array index was not a number
  12295. @throw out_of_range.404 if the JSON pointer can not be resolved
  12296. */
  12297. template<typename BasicJsonType>
  12298. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  12299. {
  12300. for (const auto& reference_token : reference_tokens)
  12301. {
  12302. // convert null values to arrays or objects before continuing
  12303. if (ptr->is_null())
  12304. {
  12305. // check if reference token is a number
  12306. const bool nums =
  12307. std::all_of(reference_token.begin(), reference_token.end(),
  12308. [](const unsigned char x)
  12309. {
  12310. return std::isdigit(x);
  12311. });
  12312. // change value to array for numbers or "-" or to object otherwise
  12313. *ptr = (nums || reference_token == "-")
  12314. ? detail::value_t::array
  12315. : detail::value_t::object;
  12316. }
  12317. switch (ptr->type())
  12318. {
  12319. case detail::value_t::object:
  12320. {
  12321. // use unchecked object access
  12322. ptr = &ptr->operator[](reference_token);
  12323. break;
  12324. }
  12325. case detail::value_t::array:
  12326. {
  12327. if (reference_token == "-")
  12328. {
  12329. // explicitly treat "-" as index beyond the end
  12330. ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
  12331. }
  12332. else
  12333. {
  12334. // convert array index to number; unchecked access
  12335. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12336. }
  12337. break;
  12338. }
  12339. case detail::value_t::null:
  12340. case detail::value_t::string:
  12341. case detail::value_t::boolean:
  12342. case detail::value_t::number_integer:
  12343. case detail::value_t::number_unsigned:
  12344. case detail::value_t::number_float:
  12345. case detail::value_t::binary:
  12346. case detail::value_t::discarded:
  12347. default:
  12348. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12349. }
  12350. }
  12351. return *ptr;
  12352. }
  12353. /*!
  12354. @throw parse_error.106 if an array index begins with '0'
  12355. @throw parse_error.109 if an array index was not a number
  12356. @throw out_of_range.402 if the array index '-' is used
  12357. @throw out_of_range.404 if the JSON pointer can not be resolved
  12358. */
  12359. template<typename BasicJsonType>
  12360. BasicJsonType& get_checked(BasicJsonType* ptr) const
  12361. {
  12362. for (const auto& reference_token : reference_tokens)
  12363. {
  12364. switch (ptr->type())
  12365. {
  12366. case detail::value_t::object:
  12367. {
  12368. // note: at performs range check
  12369. ptr = &ptr->at(reference_token);
  12370. break;
  12371. }
  12372. case detail::value_t::array:
  12373. {
  12374. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12375. {
  12376. // "-" always fails the range check
  12377. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12378. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  12379. ") is out of range"), ptr));
  12380. }
  12381. // note: at performs range check
  12382. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12383. break;
  12384. }
  12385. case detail::value_t::null:
  12386. case detail::value_t::string:
  12387. case detail::value_t::boolean:
  12388. case detail::value_t::number_integer:
  12389. case detail::value_t::number_unsigned:
  12390. case detail::value_t::number_float:
  12391. case detail::value_t::binary:
  12392. case detail::value_t::discarded:
  12393. default:
  12394. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12395. }
  12396. }
  12397. return *ptr;
  12398. }
  12399. /*!
  12400. @brief return a const reference to the pointed to value
  12401. @param[in] ptr a JSON value
  12402. @return const reference to the JSON value pointed to by the JSON
  12403. pointer
  12404. @throw parse_error.106 if an array index begins with '0'
  12405. @throw parse_error.109 if an array index was not a number
  12406. @throw out_of_range.402 if the array index '-' is used
  12407. @throw out_of_range.404 if the JSON pointer can not be resolved
  12408. */
  12409. template<typename BasicJsonType>
  12410. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  12411. {
  12412. for (const auto& reference_token : reference_tokens)
  12413. {
  12414. switch (ptr->type())
  12415. {
  12416. case detail::value_t::object:
  12417. {
  12418. // use unchecked object access
  12419. ptr = &ptr->operator[](reference_token);
  12420. break;
  12421. }
  12422. case detail::value_t::array:
  12423. {
  12424. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12425. {
  12426. // "-" cannot be used for const access
  12427. 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));
  12428. }
  12429. // use unchecked array access
  12430. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12431. break;
  12432. }
  12433. case detail::value_t::null:
  12434. case detail::value_t::string:
  12435. case detail::value_t::boolean:
  12436. case detail::value_t::number_integer:
  12437. case detail::value_t::number_unsigned:
  12438. case detail::value_t::number_float:
  12439. case detail::value_t::binary:
  12440. case detail::value_t::discarded:
  12441. default:
  12442. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12443. }
  12444. }
  12445. return *ptr;
  12446. }
  12447. /*!
  12448. @throw parse_error.106 if an array index begins with '0'
  12449. @throw parse_error.109 if an array index was not a number
  12450. @throw out_of_range.402 if the array index '-' is used
  12451. @throw out_of_range.404 if the JSON pointer can not be resolved
  12452. */
  12453. template<typename BasicJsonType>
  12454. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  12455. {
  12456. for (const auto& reference_token : reference_tokens)
  12457. {
  12458. switch (ptr->type())
  12459. {
  12460. case detail::value_t::object:
  12461. {
  12462. // note: at performs range check
  12463. ptr = &ptr->at(reference_token);
  12464. break;
  12465. }
  12466. case detail::value_t::array:
  12467. {
  12468. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12469. {
  12470. // "-" always fails the range check
  12471. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12472. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  12473. ") is out of range"), ptr));
  12474. }
  12475. // note: at performs range check
  12476. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12477. break;
  12478. }
  12479. case detail::value_t::null:
  12480. case detail::value_t::string:
  12481. case detail::value_t::boolean:
  12482. case detail::value_t::number_integer:
  12483. case detail::value_t::number_unsigned:
  12484. case detail::value_t::number_float:
  12485. case detail::value_t::binary:
  12486. case detail::value_t::discarded:
  12487. default:
  12488. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12489. }
  12490. }
  12491. return *ptr;
  12492. }
  12493. /*!
  12494. @throw parse_error.106 if an array index begins with '0'
  12495. @throw parse_error.109 if an array index was not a number
  12496. */
  12497. template<typename BasicJsonType>
  12498. bool contains(const BasicJsonType* ptr) const
  12499. {
  12500. for (const auto& reference_token : reference_tokens)
  12501. {
  12502. switch (ptr->type())
  12503. {
  12504. case detail::value_t::object:
  12505. {
  12506. if (!ptr->contains(reference_token))
  12507. {
  12508. // we did not find the key in the object
  12509. return false;
  12510. }
  12511. ptr = &ptr->operator[](reference_token);
  12512. break;
  12513. }
  12514. case detail::value_t::array:
  12515. {
  12516. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12517. {
  12518. // "-" always fails the range check
  12519. return false;
  12520. }
  12521. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  12522. {
  12523. // invalid char
  12524. return false;
  12525. }
  12526. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  12527. {
  12528. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  12529. {
  12530. // first char should be between '1' and '9'
  12531. return false;
  12532. }
  12533. for (std::size_t i = 1; i < reference_token.size(); i++)
  12534. {
  12535. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  12536. {
  12537. // other char should be between '0' and '9'
  12538. return false;
  12539. }
  12540. }
  12541. }
  12542. const auto idx = array_index<BasicJsonType>(reference_token);
  12543. if (idx >= ptr->size())
  12544. {
  12545. // index out of range
  12546. return false;
  12547. }
  12548. ptr = &ptr->operator[](idx);
  12549. break;
  12550. }
  12551. case detail::value_t::null:
  12552. case detail::value_t::string:
  12553. case detail::value_t::boolean:
  12554. case detail::value_t::number_integer:
  12555. case detail::value_t::number_unsigned:
  12556. case detail::value_t::number_float:
  12557. case detail::value_t::binary:
  12558. case detail::value_t::discarded:
  12559. default:
  12560. {
  12561. // we do not expect primitive values if there is still a
  12562. // reference token to process
  12563. return false;
  12564. }
  12565. }
  12566. }
  12567. // no reference token left means we found a primitive value
  12568. return true;
  12569. }
  12570. /*!
  12571. @brief split the string input to reference tokens
  12572. @note This function is only called by the json_pointer constructor.
  12573. All exceptions below are documented there.
  12574. @throw parse_error.107 if the pointer is not empty or begins with '/'
  12575. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  12576. */
  12577. static std::vector<string_t> split(const string_t& reference_string)
  12578. {
  12579. std::vector<string_t> result;
  12580. // special case: empty reference string -> no reference tokens
  12581. if (reference_string.empty())
  12582. {
  12583. return result;
  12584. }
  12585. // check if nonempty reference string begins with slash
  12586. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  12587. {
  12588. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  12589. }
  12590. // extract the reference tokens:
  12591. // - slash: position of the last read slash (or end of string)
  12592. // - start: position after the previous slash
  12593. for (
  12594. // search for the first slash after the first character
  12595. std::size_t slash = reference_string.find_first_of('/', 1),
  12596. // set the beginning of the first reference token
  12597. start = 1;
  12598. // we can stop if start == 0 (if slash == string_t::npos)
  12599. start != 0;
  12600. // set the beginning of the next reference token
  12601. // (will eventually be 0 if slash == string_t::npos)
  12602. start = (slash == string_t::npos) ? 0 : slash + 1,
  12603. // find next slash
  12604. slash = reference_string.find_first_of('/', start))
  12605. {
  12606. // use the text between the beginning of the reference token
  12607. // (start) and the last slash (slash).
  12608. auto reference_token = reference_string.substr(start, slash - start);
  12609. // check reference tokens are properly escaped
  12610. for (std::size_t pos = reference_token.find_first_of('~');
  12611. pos != string_t::npos;
  12612. pos = reference_token.find_first_of('~', pos + 1))
  12613. {
  12614. JSON_ASSERT(reference_token[pos] == '~');
  12615. // ~ must be followed by 0 or 1
  12616. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  12617. (reference_token[pos + 1] != '0' &&
  12618. reference_token[pos + 1] != '1')))
  12619. {
  12620. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  12621. }
  12622. }
  12623. // finally, store the reference token
  12624. detail::unescape(reference_token);
  12625. result.push_back(reference_token);
  12626. }
  12627. return result;
  12628. }
  12629. private:
  12630. /*!
  12631. @param[in] reference_string the reference string to the current value
  12632. @param[in] value the value to consider
  12633. @param[in,out] result the result object to insert values to
  12634. @note Empty objects or arrays are flattened to `null`.
  12635. */
  12636. template<typename BasicJsonType>
  12637. static void flatten(const string_t& reference_string,
  12638. const BasicJsonType& value,
  12639. BasicJsonType& result)
  12640. {
  12641. switch (value.type())
  12642. {
  12643. case detail::value_t::array:
  12644. {
  12645. if (value.m_data.m_value.array->empty())
  12646. {
  12647. // flatten empty array as null
  12648. result[reference_string] = nullptr;
  12649. }
  12650. else
  12651. {
  12652. // iterate array and use index as reference string
  12653. for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
  12654. {
  12655. flatten(detail::concat(reference_string, '/', std::to_string(i)),
  12656. value.m_data.m_value.array->operator[](i), result);
  12657. }
  12658. }
  12659. break;
  12660. }
  12661. case detail::value_t::object:
  12662. {
  12663. if (value.m_data.m_value.object->empty())
  12664. {
  12665. // flatten empty object as null
  12666. result[reference_string] = nullptr;
  12667. }
  12668. else
  12669. {
  12670. // iterate object and use keys as reference string
  12671. for (const auto& element : *value.m_data.m_value.object)
  12672. {
  12673. flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result);
  12674. }
  12675. }
  12676. break;
  12677. }
  12678. case detail::value_t::null:
  12679. case detail::value_t::string:
  12680. case detail::value_t::boolean:
  12681. case detail::value_t::number_integer:
  12682. case detail::value_t::number_unsigned:
  12683. case detail::value_t::number_float:
  12684. case detail::value_t::binary:
  12685. case detail::value_t::discarded:
  12686. default:
  12687. {
  12688. // add primitive value with its reference string
  12689. result[reference_string] = value;
  12690. break;
  12691. }
  12692. }
  12693. }
  12694. /*!
  12695. @param[in] value flattened JSON
  12696. @return unflattened JSON
  12697. @throw parse_error.109 if array index is not a number
  12698. @throw type_error.314 if value is not an object
  12699. @throw type_error.315 if object values are not primitive
  12700. @throw type_error.313 if value cannot be unflattened
  12701. */
  12702. template<typename BasicJsonType>
  12703. static BasicJsonType
  12704. unflatten(const BasicJsonType& value)
  12705. {
  12706. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  12707. {
  12708. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  12709. }
  12710. BasicJsonType result;
  12711. // iterate the JSON object values
  12712. for (const auto& element : *value.m_data.m_value.object)
  12713. {
  12714. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  12715. {
  12716. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  12717. }
  12718. // assign value to reference pointed to by JSON pointer; Note that if
  12719. // the JSON pointer is "" (i.e., points to the whole value), function
  12720. // get_and_create returns a reference to result itself. An assignment
  12721. // will then create a primitive value.
  12722. json_pointer(element.first).get_and_create(result) = element.second;
  12723. }
  12724. return result;
  12725. }
  12726. // can't use conversion operator because of ambiguity
  12727. json_pointer<string_t> convert() const&
  12728. {
  12729. json_pointer<string_t> result;
  12730. result.reference_tokens = reference_tokens;
  12731. return result;
  12732. }
  12733. json_pointer<string_t> convert()&&
  12734. {
  12735. json_pointer<string_t> result;
  12736. result.reference_tokens = std::move(reference_tokens);
  12737. return result;
  12738. }
  12739. public:
  12740. #if JSON_HAS_THREE_WAY_COMPARISON
  12741. /// @brief compares two JSON pointers for equality
  12742. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12743. template<typename RefStringTypeRhs>
  12744. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  12745. {
  12746. return reference_tokens == rhs.reference_tokens;
  12747. }
  12748. /// @brief compares JSON pointer and string for equality
  12749. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12750. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  12751. bool operator==(const string_t& rhs) const
  12752. {
  12753. return *this == json_pointer(rhs);
  12754. }
  12755. /// @brief 3-way compares two JSON pointers
  12756. template<typename RefStringTypeRhs>
  12757. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  12758. {
  12759. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  12760. }
  12761. #else
  12762. /// @brief compares two JSON pointers for equality
  12763. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12764. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12765. // NOLINTNEXTLINE(readability-redundant-declaration)
  12766. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12767. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12768. /// @brief compares JSON pointer and string for equality
  12769. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12770. template<typename RefStringTypeLhs, typename StringType>
  12771. // NOLINTNEXTLINE(readability-redundant-declaration)
  12772. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12773. const StringType& rhs);
  12774. /// @brief compares string and JSON pointer for equality
  12775. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12776. template<typename RefStringTypeRhs, typename StringType>
  12777. // NOLINTNEXTLINE(readability-redundant-declaration)
  12778. friend bool operator==(const StringType& lhs,
  12779. const json_pointer<RefStringTypeRhs>& rhs);
  12780. /// @brief compares two JSON pointers for inequality
  12781. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12782. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12783. // NOLINTNEXTLINE(readability-redundant-declaration)
  12784. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12785. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12786. /// @brief compares JSON pointer and string for inequality
  12787. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12788. template<typename RefStringTypeLhs, typename StringType>
  12789. // NOLINTNEXTLINE(readability-redundant-declaration)
  12790. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12791. const StringType& rhs);
  12792. /// @brief compares string and JSON pointer for inequality
  12793. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12794. template<typename RefStringTypeRhs, typename StringType>
  12795. // NOLINTNEXTLINE(readability-redundant-declaration)
  12796. friend bool operator!=(const StringType& lhs,
  12797. const json_pointer<RefStringTypeRhs>& rhs);
  12798. /// @brief compares two JSON pointer for less-than
  12799. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12800. // NOLINTNEXTLINE(readability-redundant-declaration)
  12801. friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12802. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12803. #endif
  12804. private:
  12805. /// the reference tokens
  12806. std::vector<string_t> reference_tokens;
  12807. };
  12808. #if !JSON_HAS_THREE_WAY_COMPARISON
  12809. // functions cannot be defined inside class due to ODR violations
  12810. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12811. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12812. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12813. {
  12814. return lhs.reference_tokens == rhs.reference_tokens;
  12815. }
  12816. template<typename RefStringTypeLhs,
  12817. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12818. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12819. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12820. const StringType& rhs)
  12821. {
  12822. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  12823. }
  12824. template<typename RefStringTypeRhs,
  12825. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12826. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12827. inline bool operator==(const StringType& lhs,
  12828. const json_pointer<RefStringTypeRhs>& rhs)
  12829. {
  12830. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  12831. }
  12832. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12833. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12834. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12835. {
  12836. return !(lhs == rhs);
  12837. }
  12838. template<typename RefStringTypeLhs,
  12839. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12840. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12841. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12842. const StringType& rhs)
  12843. {
  12844. return !(lhs == rhs);
  12845. }
  12846. template<typename RefStringTypeRhs,
  12847. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12848. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12849. inline bool operator!=(const StringType& lhs,
  12850. const json_pointer<RefStringTypeRhs>& rhs)
  12851. {
  12852. return !(lhs == rhs);
  12853. }
  12854. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12855. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12856. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12857. {
  12858. return lhs.reference_tokens < rhs.reference_tokens;
  12859. }
  12860. #endif
  12861. NLOHMANN_JSON_NAMESPACE_END
  12862. // #include <nlohmann/detail/json_ref.hpp>
  12863. // __ _____ _____ _____
  12864. // __| | __| | | | JSON for Modern C++
  12865. // | | |__ | | | | | | version 3.11.3
  12866. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12867. //
  12868. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12869. // SPDX-License-Identifier: MIT
  12870. #include <initializer_list>
  12871. #include <utility>
  12872. // #include <nlohmann/detail/abi_macros.hpp>
  12873. // #include <nlohmann/detail/meta/type_traits.hpp>
  12874. NLOHMANN_JSON_NAMESPACE_BEGIN
  12875. namespace detail
  12876. {
  12877. template<typename BasicJsonType>
  12878. class json_ref
  12879. {
  12880. public:
  12881. using value_type = BasicJsonType;
  12882. json_ref(value_type&& value)
  12883. : owned_value(std::move(value))
  12884. {}
  12885. json_ref(const value_type& value)
  12886. : value_ref(&value)
  12887. {}
  12888. json_ref(std::initializer_list<json_ref> init)
  12889. : owned_value(init)
  12890. {}
  12891. template <
  12892. class... Args,
  12893. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  12894. json_ref(Args && ... args)
  12895. : owned_value(std::forward<Args>(args)...)
  12896. {}
  12897. // class should be movable only
  12898. json_ref(json_ref&&) noexcept = default;
  12899. json_ref(const json_ref&) = delete;
  12900. json_ref& operator=(const json_ref&) = delete;
  12901. json_ref& operator=(json_ref&&) = delete;
  12902. ~json_ref() = default;
  12903. value_type moved_or_copied() const
  12904. {
  12905. if (value_ref == nullptr)
  12906. {
  12907. return std::move(owned_value);
  12908. }
  12909. return *value_ref;
  12910. }
  12911. value_type const& operator*() const
  12912. {
  12913. return value_ref ? *value_ref : owned_value;
  12914. }
  12915. value_type const* operator->() const
  12916. {
  12917. return &** this;
  12918. }
  12919. private:
  12920. mutable value_type owned_value = nullptr;
  12921. value_type const* value_ref = nullptr;
  12922. };
  12923. } // namespace detail
  12924. NLOHMANN_JSON_NAMESPACE_END
  12925. // #include <nlohmann/detail/macro_scope.hpp>
  12926. // #include <nlohmann/detail/string_concat.hpp>
  12927. // #include <nlohmann/detail/string_escape.hpp>
  12928. // #include <nlohmann/detail/meta/cpp_future.hpp>
  12929. // #include <nlohmann/detail/meta/type_traits.hpp>
  12930. // #include <nlohmann/detail/output/binary_writer.hpp>
  12931. // __ _____ _____ _____
  12932. // __| | __| | | | JSON for Modern C++
  12933. // | | |__ | | | | | | version 3.11.3
  12934. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12935. //
  12936. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12937. // SPDX-License-Identifier: MIT
  12938. #include <algorithm> // reverse
  12939. #include <array> // array
  12940. #include <map> // map
  12941. #include <cmath> // isnan, isinf
  12942. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  12943. #include <cstring> // memcpy
  12944. #include <limits> // numeric_limits
  12945. #include <string> // string
  12946. #include <utility> // move
  12947. #include <vector> // vector
  12948. // #include <nlohmann/detail/input/binary_reader.hpp>
  12949. // #include <nlohmann/detail/macro_scope.hpp>
  12950. // #include <nlohmann/detail/output/output_adapters.hpp>
  12951. // __ _____ _____ _____
  12952. // __| | __| | | | JSON for Modern C++
  12953. // | | |__ | | | | | | version 3.11.3
  12954. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12955. //
  12956. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12957. // SPDX-License-Identifier: MIT
  12958. #include <algorithm> // copy
  12959. #include <cstddef> // size_t
  12960. #include <iterator> // back_inserter
  12961. #include <memory> // shared_ptr, make_shared
  12962. #include <string> // basic_string
  12963. #include <vector> // vector
  12964. #ifndef JSON_NO_IO
  12965. #include <ios> // streamsize
  12966. #include <ostream> // basic_ostream
  12967. #endif // JSON_NO_IO
  12968. // #include <nlohmann/detail/macro_scope.hpp>
  12969. NLOHMANN_JSON_NAMESPACE_BEGIN
  12970. namespace detail
  12971. {
  12972. /// abstract output adapter interface
  12973. template<typename CharType> struct output_adapter_protocol
  12974. {
  12975. virtual void write_character(CharType c) = 0;
  12976. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  12977. virtual ~output_adapter_protocol() = default;
  12978. output_adapter_protocol() = default;
  12979. output_adapter_protocol(const output_adapter_protocol&) = default;
  12980. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  12981. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  12982. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  12983. };
  12984. /// a type to simplify interfaces
  12985. template<typename CharType>
  12986. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  12987. /// output adapter for byte vectors
  12988. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  12989. class output_vector_adapter : public output_adapter_protocol<CharType>
  12990. {
  12991. public:
  12992. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  12993. : v(vec)
  12994. {}
  12995. void write_character(CharType c) override
  12996. {
  12997. v.push_back(c);
  12998. }
  12999. JSON_HEDLEY_NON_NULL(2)
  13000. void write_characters(const CharType* s, std::size_t length) override
  13001. {
  13002. v.insert(v.end(), s, s + length);
  13003. }
  13004. private:
  13005. std::vector<CharType, AllocatorType>& v;
  13006. };
  13007. #ifndef JSON_NO_IO
  13008. /// output adapter for output streams
  13009. template<typename CharType>
  13010. class output_stream_adapter : public output_adapter_protocol<CharType>
  13011. {
  13012. public:
  13013. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  13014. : stream(s)
  13015. {}
  13016. void write_character(CharType c) override
  13017. {
  13018. stream.put(c);
  13019. }
  13020. JSON_HEDLEY_NON_NULL(2)
  13021. void write_characters(const CharType* s, std::size_t length) override
  13022. {
  13023. stream.write(s, static_cast<std::streamsize>(length));
  13024. }
  13025. private:
  13026. std::basic_ostream<CharType>& stream;
  13027. };
  13028. #endif // JSON_NO_IO
  13029. /// output adapter for basic_string
  13030. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13031. class output_string_adapter : public output_adapter_protocol<CharType>
  13032. {
  13033. public:
  13034. explicit output_string_adapter(StringType& s) noexcept
  13035. : str(s)
  13036. {}
  13037. void write_character(CharType c) override
  13038. {
  13039. str.push_back(c);
  13040. }
  13041. JSON_HEDLEY_NON_NULL(2)
  13042. void write_characters(const CharType* s, std::size_t length) override
  13043. {
  13044. str.append(s, length);
  13045. }
  13046. private:
  13047. StringType& str;
  13048. };
  13049. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13050. class output_adapter
  13051. {
  13052. public:
  13053. template<typename AllocatorType = std::allocator<CharType>>
  13054. output_adapter(std::vector<CharType, AllocatorType>& vec)
  13055. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  13056. #ifndef JSON_NO_IO
  13057. output_adapter(std::basic_ostream<CharType>& s)
  13058. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  13059. #endif // JSON_NO_IO
  13060. output_adapter(StringType& s)
  13061. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  13062. operator output_adapter_t<CharType>()
  13063. {
  13064. return oa;
  13065. }
  13066. private:
  13067. output_adapter_t<CharType> oa = nullptr;
  13068. };
  13069. } // namespace detail
  13070. NLOHMANN_JSON_NAMESPACE_END
  13071. // #include <nlohmann/detail/string_concat.hpp>
  13072. NLOHMANN_JSON_NAMESPACE_BEGIN
  13073. namespace detail
  13074. {
  13075. ///////////////////
  13076. // binary writer //
  13077. ///////////////////
  13078. /*!
  13079. @brief serialization to CBOR and MessagePack values
  13080. */
  13081. template<typename BasicJsonType, typename CharType>
  13082. class binary_writer
  13083. {
  13084. using string_t = typename BasicJsonType::string_t;
  13085. using binary_t = typename BasicJsonType::binary_t;
  13086. using number_float_t = typename BasicJsonType::number_float_t;
  13087. public:
  13088. /*!
  13089. @brief create a binary writer
  13090. @param[in] adapter output adapter to write to
  13091. */
  13092. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  13093. {
  13094. JSON_ASSERT(oa);
  13095. }
  13096. /*!
  13097. @param[in] j JSON value to serialize
  13098. @pre j.type() == value_t::object
  13099. */
  13100. void write_bson(const BasicJsonType& j)
  13101. {
  13102. switch (j.type())
  13103. {
  13104. case value_t::object:
  13105. {
  13106. write_bson_object(*j.m_data.m_value.object);
  13107. break;
  13108. }
  13109. case value_t::null:
  13110. case value_t::array:
  13111. case value_t::string:
  13112. case value_t::boolean:
  13113. case value_t::number_integer:
  13114. case value_t::number_unsigned:
  13115. case value_t::number_float:
  13116. case value_t::binary:
  13117. case value_t::discarded:
  13118. default:
  13119. {
  13120. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13121. }
  13122. }
  13123. }
  13124. /*!
  13125. @param[in] j JSON value to serialize
  13126. */
  13127. void write_cbor(const BasicJsonType& j)
  13128. {
  13129. switch (j.type())
  13130. {
  13131. case value_t::null:
  13132. {
  13133. oa->write_character(to_char_type(0xF6));
  13134. break;
  13135. }
  13136. case value_t::boolean:
  13137. {
  13138. oa->write_character(j.m_data.m_value.boolean
  13139. ? to_char_type(0xF5)
  13140. : to_char_type(0xF4));
  13141. break;
  13142. }
  13143. case value_t::number_integer:
  13144. {
  13145. if (j.m_data.m_value.number_integer >= 0)
  13146. {
  13147. // CBOR does not differentiate between positive signed
  13148. // integers and unsigned integers. Therefore, we used the
  13149. // code from the value_t::number_unsigned case here.
  13150. if (j.m_data.m_value.number_integer <= 0x17)
  13151. {
  13152. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13153. }
  13154. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13155. {
  13156. oa->write_character(to_char_type(0x18));
  13157. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13158. }
  13159. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13160. {
  13161. oa->write_character(to_char_type(0x19));
  13162. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13163. }
  13164. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13165. {
  13166. oa->write_character(to_char_type(0x1A));
  13167. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13168. }
  13169. else
  13170. {
  13171. oa->write_character(to_char_type(0x1B));
  13172. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13173. }
  13174. }
  13175. else
  13176. {
  13177. // The conversions below encode the sign in the first
  13178. // byte, and the value is converted to a positive number.
  13179. const auto positive_number = -1 - j.m_data.m_value.number_integer;
  13180. if (j.m_data.m_value.number_integer >= -24)
  13181. {
  13182. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13183. }
  13184. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13185. {
  13186. oa->write_character(to_char_type(0x38));
  13187. write_number(static_cast<std::uint8_t>(positive_number));
  13188. }
  13189. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13190. {
  13191. oa->write_character(to_char_type(0x39));
  13192. write_number(static_cast<std::uint16_t>(positive_number));
  13193. }
  13194. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13195. {
  13196. oa->write_character(to_char_type(0x3A));
  13197. write_number(static_cast<std::uint32_t>(positive_number));
  13198. }
  13199. else
  13200. {
  13201. oa->write_character(to_char_type(0x3B));
  13202. write_number(static_cast<std::uint64_t>(positive_number));
  13203. }
  13204. }
  13205. break;
  13206. }
  13207. case value_t::number_unsigned:
  13208. {
  13209. if (j.m_data.m_value.number_unsigned <= 0x17)
  13210. {
  13211. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13212. }
  13213. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13214. {
  13215. oa->write_character(to_char_type(0x18));
  13216. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13217. }
  13218. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13219. {
  13220. oa->write_character(to_char_type(0x19));
  13221. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
  13222. }
  13223. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13224. {
  13225. oa->write_character(to_char_type(0x1A));
  13226. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
  13227. }
  13228. else
  13229. {
  13230. oa->write_character(to_char_type(0x1B));
  13231. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
  13232. }
  13233. break;
  13234. }
  13235. case value_t::number_float:
  13236. {
  13237. if (std::isnan(j.m_data.m_value.number_float))
  13238. {
  13239. // NaN is 0xf97e00 in CBOR
  13240. oa->write_character(to_char_type(0xF9));
  13241. oa->write_character(to_char_type(0x7E));
  13242. oa->write_character(to_char_type(0x00));
  13243. }
  13244. else if (std::isinf(j.m_data.m_value.number_float))
  13245. {
  13246. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  13247. oa->write_character(to_char_type(0xf9));
  13248. oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  13249. oa->write_character(to_char_type(0x00));
  13250. }
  13251. else
  13252. {
  13253. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
  13254. }
  13255. break;
  13256. }
  13257. case value_t::string:
  13258. {
  13259. // step 1: write control byte and the string length
  13260. const auto N = j.m_data.m_value.string->size();
  13261. if (N <= 0x17)
  13262. {
  13263. write_number(static_cast<std::uint8_t>(0x60 + N));
  13264. }
  13265. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13266. {
  13267. oa->write_character(to_char_type(0x78));
  13268. write_number(static_cast<std::uint8_t>(N));
  13269. }
  13270. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13271. {
  13272. oa->write_character(to_char_type(0x79));
  13273. write_number(static_cast<std::uint16_t>(N));
  13274. }
  13275. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13276. {
  13277. oa->write_character(to_char_type(0x7A));
  13278. write_number(static_cast<std::uint32_t>(N));
  13279. }
  13280. // LCOV_EXCL_START
  13281. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13282. {
  13283. oa->write_character(to_char_type(0x7B));
  13284. write_number(static_cast<std::uint64_t>(N));
  13285. }
  13286. // LCOV_EXCL_STOP
  13287. // step 2: write the string
  13288. oa->write_characters(
  13289. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13290. j.m_data.m_value.string->size());
  13291. break;
  13292. }
  13293. case value_t::array:
  13294. {
  13295. // step 1: write control byte and the array size
  13296. const auto N = j.m_data.m_value.array->size();
  13297. if (N <= 0x17)
  13298. {
  13299. write_number(static_cast<std::uint8_t>(0x80 + N));
  13300. }
  13301. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13302. {
  13303. oa->write_character(to_char_type(0x98));
  13304. write_number(static_cast<std::uint8_t>(N));
  13305. }
  13306. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13307. {
  13308. oa->write_character(to_char_type(0x99));
  13309. write_number(static_cast<std::uint16_t>(N));
  13310. }
  13311. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13312. {
  13313. oa->write_character(to_char_type(0x9A));
  13314. write_number(static_cast<std::uint32_t>(N));
  13315. }
  13316. // LCOV_EXCL_START
  13317. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13318. {
  13319. oa->write_character(to_char_type(0x9B));
  13320. write_number(static_cast<std::uint64_t>(N));
  13321. }
  13322. // LCOV_EXCL_STOP
  13323. // step 2: write each element
  13324. for (const auto& el : *j.m_data.m_value.array)
  13325. {
  13326. write_cbor(el);
  13327. }
  13328. break;
  13329. }
  13330. case value_t::binary:
  13331. {
  13332. if (j.m_data.m_value.binary->has_subtype())
  13333. {
  13334. if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  13335. {
  13336. write_number(static_cast<std::uint8_t>(0xd8));
  13337. write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
  13338. }
  13339. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  13340. {
  13341. write_number(static_cast<std::uint8_t>(0xd9));
  13342. write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
  13343. }
  13344. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  13345. {
  13346. write_number(static_cast<std::uint8_t>(0xda));
  13347. write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
  13348. }
  13349. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  13350. {
  13351. write_number(static_cast<std::uint8_t>(0xdb));
  13352. write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
  13353. }
  13354. }
  13355. // step 1: write control byte and the binary array size
  13356. const auto N = j.m_data.m_value.binary->size();
  13357. if (N <= 0x17)
  13358. {
  13359. write_number(static_cast<std::uint8_t>(0x40 + N));
  13360. }
  13361. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13362. {
  13363. oa->write_character(to_char_type(0x58));
  13364. write_number(static_cast<std::uint8_t>(N));
  13365. }
  13366. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13367. {
  13368. oa->write_character(to_char_type(0x59));
  13369. write_number(static_cast<std::uint16_t>(N));
  13370. }
  13371. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13372. {
  13373. oa->write_character(to_char_type(0x5A));
  13374. write_number(static_cast<std::uint32_t>(N));
  13375. }
  13376. // LCOV_EXCL_START
  13377. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13378. {
  13379. oa->write_character(to_char_type(0x5B));
  13380. write_number(static_cast<std::uint64_t>(N));
  13381. }
  13382. // LCOV_EXCL_STOP
  13383. // step 2: write each element
  13384. oa->write_characters(
  13385. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13386. N);
  13387. break;
  13388. }
  13389. case value_t::object:
  13390. {
  13391. // step 1: write control byte and the object size
  13392. const auto N = j.m_data.m_value.object->size();
  13393. if (N <= 0x17)
  13394. {
  13395. write_number(static_cast<std::uint8_t>(0xA0 + N));
  13396. }
  13397. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13398. {
  13399. oa->write_character(to_char_type(0xB8));
  13400. write_number(static_cast<std::uint8_t>(N));
  13401. }
  13402. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13403. {
  13404. oa->write_character(to_char_type(0xB9));
  13405. write_number(static_cast<std::uint16_t>(N));
  13406. }
  13407. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13408. {
  13409. oa->write_character(to_char_type(0xBA));
  13410. write_number(static_cast<std::uint32_t>(N));
  13411. }
  13412. // LCOV_EXCL_START
  13413. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13414. {
  13415. oa->write_character(to_char_type(0xBB));
  13416. write_number(static_cast<std::uint64_t>(N));
  13417. }
  13418. // LCOV_EXCL_STOP
  13419. // step 2: write each element
  13420. for (const auto& el : *j.m_data.m_value.object)
  13421. {
  13422. write_cbor(el.first);
  13423. write_cbor(el.second);
  13424. }
  13425. break;
  13426. }
  13427. case value_t::discarded:
  13428. default:
  13429. break;
  13430. }
  13431. }
  13432. /*!
  13433. @param[in] j JSON value to serialize
  13434. */
  13435. void write_msgpack(const BasicJsonType& j)
  13436. {
  13437. switch (j.type())
  13438. {
  13439. case value_t::null: // nil
  13440. {
  13441. oa->write_character(to_char_type(0xC0));
  13442. break;
  13443. }
  13444. case value_t::boolean: // true and false
  13445. {
  13446. oa->write_character(j.m_data.m_value.boolean
  13447. ? to_char_type(0xC3)
  13448. : to_char_type(0xC2));
  13449. break;
  13450. }
  13451. case value_t::number_integer:
  13452. {
  13453. if (j.m_data.m_value.number_integer >= 0)
  13454. {
  13455. // MessagePack does not differentiate between positive
  13456. // signed integers and unsigned integers. Therefore, we used
  13457. // the code from the value_t::number_unsigned case here.
  13458. if (j.m_data.m_value.number_unsigned < 128)
  13459. {
  13460. // positive fixnum
  13461. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13462. }
  13463. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13464. {
  13465. // uint 8
  13466. oa->write_character(to_char_type(0xCC));
  13467. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13468. }
  13469. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13470. {
  13471. // uint 16
  13472. oa->write_character(to_char_type(0xCD));
  13473. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13474. }
  13475. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13476. {
  13477. // uint 32
  13478. oa->write_character(to_char_type(0xCE));
  13479. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13480. }
  13481. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13482. {
  13483. // uint 64
  13484. oa->write_character(to_char_type(0xCF));
  13485. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13486. }
  13487. }
  13488. else
  13489. {
  13490. if (j.m_data.m_value.number_integer >= -32)
  13491. {
  13492. // negative fixnum
  13493. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  13494. }
  13495. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  13496. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  13497. {
  13498. // int 8
  13499. oa->write_character(to_char_type(0xD0));
  13500. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  13501. }
  13502. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  13503. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  13504. {
  13505. // int 16
  13506. oa->write_character(to_char_type(0xD1));
  13507. write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
  13508. }
  13509. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  13510. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  13511. {
  13512. // int 32
  13513. oa->write_character(to_char_type(0xD2));
  13514. write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
  13515. }
  13516. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  13517. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  13518. {
  13519. // int 64
  13520. oa->write_character(to_char_type(0xD3));
  13521. write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
  13522. }
  13523. }
  13524. break;
  13525. }
  13526. case value_t::number_unsigned:
  13527. {
  13528. if (j.m_data.m_value.number_unsigned < 128)
  13529. {
  13530. // positive fixnum
  13531. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13532. }
  13533. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13534. {
  13535. // uint 8
  13536. oa->write_character(to_char_type(0xCC));
  13537. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13538. }
  13539. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13540. {
  13541. // uint 16
  13542. oa->write_character(to_char_type(0xCD));
  13543. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13544. }
  13545. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13546. {
  13547. // uint 32
  13548. oa->write_character(to_char_type(0xCE));
  13549. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13550. }
  13551. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13552. {
  13553. // uint 64
  13554. oa->write_character(to_char_type(0xCF));
  13555. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13556. }
  13557. break;
  13558. }
  13559. case value_t::number_float:
  13560. {
  13561. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
  13562. break;
  13563. }
  13564. case value_t::string:
  13565. {
  13566. // step 1: write control byte and the string length
  13567. const auto N = j.m_data.m_value.string->size();
  13568. if (N <= 31)
  13569. {
  13570. // fixstr
  13571. write_number(static_cast<std::uint8_t>(0xA0 | N));
  13572. }
  13573. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13574. {
  13575. // str 8
  13576. oa->write_character(to_char_type(0xD9));
  13577. write_number(static_cast<std::uint8_t>(N));
  13578. }
  13579. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13580. {
  13581. // str 16
  13582. oa->write_character(to_char_type(0xDA));
  13583. write_number(static_cast<std::uint16_t>(N));
  13584. }
  13585. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13586. {
  13587. // str 32
  13588. oa->write_character(to_char_type(0xDB));
  13589. write_number(static_cast<std::uint32_t>(N));
  13590. }
  13591. // step 2: write the string
  13592. oa->write_characters(
  13593. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13594. j.m_data.m_value.string->size());
  13595. break;
  13596. }
  13597. case value_t::array:
  13598. {
  13599. // step 1: write control byte and the array size
  13600. const auto N = j.m_data.m_value.array->size();
  13601. if (N <= 15)
  13602. {
  13603. // fixarray
  13604. write_number(static_cast<std::uint8_t>(0x90 | N));
  13605. }
  13606. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13607. {
  13608. // array 16
  13609. oa->write_character(to_char_type(0xDC));
  13610. write_number(static_cast<std::uint16_t>(N));
  13611. }
  13612. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13613. {
  13614. // array 32
  13615. oa->write_character(to_char_type(0xDD));
  13616. write_number(static_cast<std::uint32_t>(N));
  13617. }
  13618. // step 2: write each element
  13619. for (const auto& el : *j.m_data.m_value.array)
  13620. {
  13621. write_msgpack(el);
  13622. }
  13623. break;
  13624. }
  13625. case value_t::binary:
  13626. {
  13627. // step 0: determine if the binary type has a set subtype to
  13628. // determine whether or not to use the ext or fixext types
  13629. const bool use_ext = j.m_data.m_value.binary->has_subtype();
  13630. // step 1: write control byte and the byte string length
  13631. const auto N = j.m_data.m_value.binary->size();
  13632. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13633. {
  13634. std::uint8_t output_type{};
  13635. bool fixed = true;
  13636. if (use_ext)
  13637. {
  13638. switch (N)
  13639. {
  13640. case 1:
  13641. output_type = 0xD4; // fixext 1
  13642. break;
  13643. case 2:
  13644. output_type = 0xD5; // fixext 2
  13645. break;
  13646. case 4:
  13647. output_type = 0xD6; // fixext 4
  13648. break;
  13649. case 8:
  13650. output_type = 0xD7; // fixext 8
  13651. break;
  13652. case 16:
  13653. output_type = 0xD8; // fixext 16
  13654. break;
  13655. default:
  13656. output_type = 0xC7; // ext 8
  13657. fixed = false;
  13658. break;
  13659. }
  13660. }
  13661. else
  13662. {
  13663. output_type = 0xC4; // bin 8
  13664. fixed = false;
  13665. }
  13666. oa->write_character(to_char_type(output_type));
  13667. if (!fixed)
  13668. {
  13669. write_number(static_cast<std::uint8_t>(N));
  13670. }
  13671. }
  13672. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13673. {
  13674. const std::uint8_t output_type = use_ext
  13675. ? 0xC8 // ext 16
  13676. : 0xC5; // bin 16
  13677. oa->write_character(to_char_type(output_type));
  13678. write_number(static_cast<std::uint16_t>(N));
  13679. }
  13680. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13681. {
  13682. const std::uint8_t output_type = use_ext
  13683. ? 0xC9 // ext 32
  13684. : 0xC6; // bin 32
  13685. oa->write_character(to_char_type(output_type));
  13686. write_number(static_cast<std::uint32_t>(N));
  13687. }
  13688. // step 1.5: if this is an ext type, write the subtype
  13689. if (use_ext)
  13690. {
  13691. write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
  13692. }
  13693. // step 2: write the byte string
  13694. oa->write_characters(
  13695. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13696. N);
  13697. break;
  13698. }
  13699. case value_t::object:
  13700. {
  13701. // step 1: write control byte and the object size
  13702. const auto N = j.m_data.m_value.object->size();
  13703. if (N <= 15)
  13704. {
  13705. // fixmap
  13706. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  13707. }
  13708. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13709. {
  13710. // map 16
  13711. oa->write_character(to_char_type(0xDE));
  13712. write_number(static_cast<std::uint16_t>(N));
  13713. }
  13714. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13715. {
  13716. // map 32
  13717. oa->write_character(to_char_type(0xDF));
  13718. write_number(static_cast<std::uint32_t>(N));
  13719. }
  13720. // step 2: write each element
  13721. for (const auto& el : *j.m_data.m_value.object)
  13722. {
  13723. write_msgpack(el.first);
  13724. write_msgpack(el.second);
  13725. }
  13726. break;
  13727. }
  13728. case value_t::discarded:
  13729. default:
  13730. break;
  13731. }
  13732. }
  13733. /*!
  13734. @param[in] j JSON value to serialize
  13735. @param[in] use_count whether to use '#' prefixes (optimized format)
  13736. @param[in] use_type whether to use '$' prefixes (optimized format)
  13737. @param[in] add_prefix whether prefixes need to be used for this value
  13738. @param[in] use_bjdata whether write in BJData format, default is false
  13739. */
  13740. void write_ubjson(const BasicJsonType& j, const bool use_count,
  13741. const bool use_type, const bool add_prefix = true,
  13742. const bool use_bjdata = false)
  13743. {
  13744. switch (j.type())
  13745. {
  13746. case value_t::null:
  13747. {
  13748. if (add_prefix)
  13749. {
  13750. oa->write_character(to_char_type('Z'));
  13751. }
  13752. break;
  13753. }
  13754. case value_t::boolean:
  13755. {
  13756. if (add_prefix)
  13757. {
  13758. oa->write_character(j.m_data.m_value.boolean
  13759. ? to_char_type('T')
  13760. : to_char_type('F'));
  13761. }
  13762. break;
  13763. }
  13764. case value_t::number_integer:
  13765. {
  13766. write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
  13767. break;
  13768. }
  13769. case value_t::number_unsigned:
  13770. {
  13771. write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
  13772. break;
  13773. }
  13774. case value_t::number_float:
  13775. {
  13776. write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
  13777. break;
  13778. }
  13779. case value_t::string:
  13780. {
  13781. if (add_prefix)
  13782. {
  13783. oa->write_character(to_char_type('S'));
  13784. }
  13785. write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
  13786. oa->write_characters(
  13787. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13788. j.m_data.m_value.string->size());
  13789. break;
  13790. }
  13791. case value_t::array:
  13792. {
  13793. if (add_prefix)
  13794. {
  13795. oa->write_character(to_char_type('['));
  13796. }
  13797. bool prefix_required = true;
  13798. if (use_type && !j.m_data.m_value.array->empty())
  13799. {
  13800. JSON_ASSERT(use_count);
  13801. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13802. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  13803. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13804. {
  13805. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13806. });
  13807. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13808. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13809. {
  13810. prefix_required = false;
  13811. oa->write_character(to_char_type('$'));
  13812. oa->write_character(first_prefix);
  13813. }
  13814. }
  13815. if (use_count)
  13816. {
  13817. oa->write_character(to_char_type('#'));
  13818. write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
  13819. }
  13820. for (const auto& el : *j.m_data.m_value.array)
  13821. {
  13822. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  13823. }
  13824. if (!use_count)
  13825. {
  13826. oa->write_character(to_char_type(']'));
  13827. }
  13828. break;
  13829. }
  13830. case value_t::binary:
  13831. {
  13832. if (add_prefix)
  13833. {
  13834. oa->write_character(to_char_type('['));
  13835. }
  13836. if (use_type && !j.m_data.m_value.binary->empty())
  13837. {
  13838. JSON_ASSERT(use_count);
  13839. oa->write_character(to_char_type('$'));
  13840. oa->write_character('U');
  13841. }
  13842. if (use_count)
  13843. {
  13844. oa->write_character(to_char_type('#'));
  13845. write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
  13846. }
  13847. if (use_type)
  13848. {
  13849. oa->write_characters(
  13850. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13851. j.m_data.m_value.binary->size());
  13852. }
  13853. else
  13854. {
  13855. for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
  13856. {
  13857. oa->write_character(to_char_type('U'));
  13858. oa->write_character(j.m_data.m_value.binary->data()[i]);
  13859. }
  13860. }
  13861. if (!use_count)
  13862. {
  13863. oa->write_character(to_char_type(']'));
  13864. }
  13865. break;
  13866. }
  13867. case value_t::object:
  13868. {
  13869. 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())
  13870. {
  13871. if (!write_bjdata_ndarray(*j.m_data.m_value.object, use_count, use_type)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  13872. {
  13873. break;
  13874. }
  13875. }
  13876. if (add_prefix)
  13877. {
  13878. oa->write_character(to_char_type('{'));
  13879. }
  13880. bool prefix_required = true;
  13881. if (use_type && !j.m_data.m_value.object->empty())
  13882. {
  13883. JSON_ASSERT(use_count);
  13884. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13885. const bool same_prefix = std::all_of(j.begin(), j.end(),
  13886. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13887. {
  13888. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13889. });
  13890. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13891. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13892. {
  13893. prefix_required = false;
  13894. oa->write_character(to_char_type('$'));
  13895. oa->write_character(first_prefix);
  13896. }
  13897. }
  13898. if (use_count)
  13899. {
  13900. oa->write_character(to_char_type('#'));
  13901. write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
  13902. }
  13903. for (const auto& el : *j.m_data.m_value.object)
  13904. {
  13905. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  13906. oa->write_characters(
  13907. reinterpret_cast<const CharType*>(el.first.c_str()),
  13908. el.first.size());
  13909. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  13910. }
  13911. if (!use_count)
  13912. {
  13913. oa->write_character(to_char_type('}'));
  13914. }
  13915. break;
  13916. }
  13917. case value_t::discarded:
  13918. default:
  13919. break;
  13920. }
  13921. }
  13922. private:
  13923. //////////
  13924. // BSON //
  13925. //////////
  13926. /*!
  13927. @return The size of a BSON document entry header, including the id marker
  13928. and the entry name size (and its null-terminator).
  13929. */
  13930. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  13931. {
  13932. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  13933. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  13934. {
  13935. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  13936. static_cast<void>(j);
  13937. }
  13938. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  13939. }
  13940. /*!
  13941. @brief Writes the given @a element_type and @a name to the output adapter
  13942. */
  13943. void write_bson_entry_header(const string_t& name,
  13944. const std::uint8_t element_type)
  13945. {
  13946. oa->write_character(to_char_type(element_type)); // boolean
  13947. oa->write_characters(
  13948. reinterpret_cast<const CharType*>(name.c_str()),
  13949. name.size() + 1u);
  13950. }
  13951. /*!
  13952. @brief Writes a BSON element with key @a name and boolean value @a value
  13953. */
  13954. void write_bson_boolean(const string_t& name,
  13955. const bool value)
  13956. {
  13957. write_bson_entry_header(name, 0x08);
  13958. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  13959. }
  13960. /*!
  13961. @brief Writes a BSON element with key @a name and double value @a value
  13962. */
  13963. void write_bson_double(const string_t& name,
  13964. const double value)
  13965. {
  13966. write_bson_entry_header(name, 0x01);
  13967. write_number<double>(value, true);
  13968. }
  13969. /*!
  13970. @return The size of the BSON-encoded string in @a value
  13971. */
  13972. static std::size_t calc_bson_string_size(const string_t& value)
  13973. {
  13974. return sizeof(std::int32_t) + value.size() + 1ul;
  13975. }
  13976. /*!
  13977. @brief Writes a BSON element with key @a name and string value @a value
  13978. */
  13979. void write_bson_string(const string_t& name,
  13980. const string_t& value)
  13981. {
  13982. write_bson_entry_header(name, 0x02);
  13983. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  13984. oa->write_characters(
  13985. reinterpret_cast<const CharType*>(value.c_str()),
  13986. value.size() + 1);
  13987. }
  13988. /*!
  13989. @brief Writes a BSON element with key @a name and null value
  13990. */
  13991. void write_bson_null(const string_t& name)
  13992. {
  13993. write_bson_entry_header(name, 0x0A);
  13994. }
  13995. /*!
  13996. @return The size of the BSON-encoded integer @a value
  13997. */
  13998. static std::size_t calc_bson_integer_size(const std::int64_t value)
  13999. {
  14000. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  14001. ? sizeof(std::int32_t)
  14002. : sizeof(std::int64_t);
  14003. }
  14004. /*!
  14005. @brief Writes a BSON element with key @a name and integer @a value
  14006. */
  14007. void write_bson_integer(const string_t& name,
  14008. const std::int64_t value)
  14009. {
  14010. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  14011. {
  14012. write_bson_entry_header(name, 0x10); // int32
  14013. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  14014. }
  14015. else
  14016. {
  14017. write_bson_entry_header(name, 0x12); // int64
  14018. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  14019. }
  14020. }
  14021. /*!
  14022. @return The size of the BSON-encoded unsigned integer in @a j
  14023. */
  14024. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  14025. {
  14026. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14027. ? sizeof(std::int32_t)
  14028. : sizeof(std::int64_t);
  14029. }
  14030. /*!
  14031. @brief Writes a BSON element with key @a name and unsigned @a value
  14032. */
  14033. void write_bson_unsigned(const string_t& name,
  14034. const BasicJsonType& j)
  14035. {
  14036. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14037. {
  14038. write_bson_entry_header(name, 0x10 /* int32 */);
  14039. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
  14040. }
  14041. else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14042. {
  14043. write_bson_entry_header(name, 0x12 /* int64 */);
  14044. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
  14045. }
  14046. else
  14047. {
  14048. JSON_THROW(out_of_range::create(407, concat("integer number ", std::to_string(j.m_data.m_value.number_unsigned), " cannot be represented by BSON as it does not fit int64"), &j));
  14049. }
  14050. }
  14051. /*!
  14052. @brief Writes a BSON element with key @a name and object @a value
  14053. */
  14054. void write_bson_object_entry(const string_t& name,
  14055. const typename BasicJsonType::object_t& value)
  14056. {
  14057. write_bson_entry_header(name, 0x03); // object
  14058. write_bson_object(value);
  14059. }
  14060. /*!
  14061. @return The size of the BSON-encoded array @a value
  14062. */
  14063. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  14064. {
  14065. std::size_t array_index = 0ul;
  14066. 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)
  14067. {
  14068. return result + calc_bson_element_size(std::to_string(array_index++), el);
  14069. });
  14070. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  14071. }
  14072. /*!
  14073. @return The size of the BSON-encoded binary array @a value
  14074. */
  14075. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  14076. {
  14077. return sizeof(std::int32_t) + value.size() + 1ul;
  14078. }
  14079. /*!
  14080. @brief Writes a BSON element with key @a name and array @a value
  14081. */
  14082. void write_bson_array(const string_t& name,
  14083. const typename BasicJsonType::array_t& value)
  14084. {
  14085. write_bson_entry_header(name, 0x04); // array
  14086. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  14087. std::size_t array_index = 0ul;
  14088. for (const auto& el : value)
  14089. {
  14090. write_bson_element(std::to_string(array_index++), el);
  14091. }
  14092. oa->write_character(to_char_type(0x00));
  14093. }
  14094. /*!
  14095. @brief Writes a BSON element with key @a name and binary value @a value
  14096. */
  14097. void write_bson_binary(const string_t& name,
  14098. const binary_t& value)
  14099. {
  14100. write_bson_entry_header(name, 0x05);
  14101. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  14102. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  14103. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  14104. }
  14105. /*!
  14106. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  14107. @return The calculated size for the BSON document entry for @a j with the given @a name.
  14108. */
  14109. static std::size_t calc_bson_element_size(const string_t& name,
  14110. const BasicJsonType& j)
  14111. {
  14112. const auto header_size = calc_bson_entry_header_size(name, j);
  14113. switch (j.type())
  14114. {
  14115. case value_t::object:
  14116. return header_size + calc_bson_object_size(*j.m_data.m_value.object);
  14117. case value_t::array:
  14118. return header_size + calc_bson_array_size(*j.m_data.m_value.array);
  14119. case value_t::binary:
  14120. return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
  14121. case value_t::boolean:
  14122. return header_size + 1ul;
  14123. case value_t::number_float:
  14124. return header_size + 8ul;
  14125. case value_t::number_integer:
  14126. return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
  14127. case value_t::number_unsigned:
  14128. return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
  14129. case value_t::string:
  14130. return header_size + calc_bson_string_size(*j.m_data.m_value.string);
  14131. case value_t::null:
  14132. return header_size + 0ul;
  14133. // LCOV_EXCL_START
  14134. case value_t::discarded:
  14135. default:
  14136. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14137. return 0ul;
  14138. // LCOV_EXCL_STOP
  14139. }
  14140. }
  14141. /*!
  14142. @brief Serializes the JSON value @a j to BSON and associates it with the
  14143. key @a name.
  14144. @param name The name to associate with the JSON entity @a j within the
  14145. current BSON document
  14146. */
  14147. void write_bson_element(const string_t& name,
  14148. const BasicJsonType& j)
  14149. {
  14150. switch (j.type())
  14151. {
  14152. case value_t::object:
  14153. return write_bson_object_entry(name, *j.m_data.m_value.object);
  14154. case value_t::array:
  14155. return write_bson_array(name, *j.m_data.m_value.array);
  14156. case value_t::binary:
  14157. return write_bson_binary(name, *j.m_data.m_value.binary);
  14158. case value_t::boolean:
  14159. return write_bson_boolean(name, j.m_data.m_value.boolean);
  14160. case value_t::number_float:
  14161. return write_bson_double(name, j.m_data.m_value.number_float);
  14162. case value_t::number_integer:
  14163. return write_bson_integer(name, j.m_data.m_value.number_integer);
  14164. case value_t::number_unsigned:
  14165. return write_bson_unsigned(name, j);
  14166. case value_t::string:
  14167. return write_bson_string(name, *j.m_data.m_value.string);
  14168. case value_t::null:
  14169. return write_bson_null(name);
  14170. // LCOV_EXCL_START
  14171. case value_t::discarded:
  14172. default:
  14173. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14174. return;
  14175. // LCOV_EXCL_STOP
  14176. }
  14177. }
  14178. /*!
  14179. @brief Calculates the size of the BSON serialization of the given
  14180. JSON-object @a j.
  14181. @param[in] value JSON value to serialize
  14182. @pre value.type() == value_t::object
  14183. */
  14184. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14185. {
  14186. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14187. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14188. {
  14189. return result += calc_bson_element_size(el.first, el.second);
  14190. });
  14191. return sizeof(std::int32_t) + document_size + 1ul;
  14192. }
  14193. /*!
  14194. @param[in] value JSON value to serialize
  14195. @pre value.type() == value_t::object
  14196. */
  14197. void write_bson_object(const typename BasicJsonType::object_t& value)
  14198. {
  14199. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  14200. for (const auto& el : value)
  14201. {
  14202. write_bson_element(el.first, el.second);
  14203. }
  14204. oa->write_character(to_char_type(0x00));
  14205. }
  14206. //////////
  14207. // CBOR //
  14208. //////////
  14209. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  14210. {
  14211. return to_char_type(0xFA); // Single-Precision Float
  14212. }
  14213. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  14214. {
  14215. return to_char_type(0xFB); // Double-Precision Float
  14216. }
  14217. /////////////
  14218. // MsgPack //
  14219. /////////////
  14220. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  14221. {
  14222. return to_char_type(0xCA); // float 32
  14223. }
  14224. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  14225. {
  14226. return to_char_type(0xCB); // float 64
  14227. }
  14228. ////////////
  14229. // UBJSON //
  14230. ////////////
  14231. // UBJSON: write number (floating point)
  14232. template<typename NumberType, typename std::enable_if<
  14233. std::is_floating_point<NumberType>::value, int>::type = 0>
  14234. void write_number_with_ubjson_prefix(const NumberType n,
  14235. const bool add_prefix,
  14236. const bool use_bjdata)
  14237. {
  14238. if (add_prefix)
  14239. {
  14240. oa->write_character(get_ubjson_float_prefix(n));
  14241. }
  14242. write_number(n, use_bjdata);
  14243. }
  14244. // UBJSON: write number (unsigned integer)
  14245. template<typename NumberType, typename std::enable_if<
  14246. std::is_unsigned<NumberType>::value, int>::type = 0>
  14247. void write_number_with_ubjson_prefix(const NumberType n,
  14248. const bool add_prefix,
  14249. const bool use_bjdata)
  14250. {
  14251. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14252. {
  14253. if (add_prefix)
  14254. {
  14255. oa->write_character(to_char_type('i')); // int8
  14256. }
  14257. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14258. }
  14259. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  14260. {
  14261. if (add_prefix)
  14262. {
  14263. oa->write_character(to_char_type('U')); // uint8
  14264. }
  14265. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14266. }
  14267. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14268. {
  14269. if (add_prefix)
  14270. {
  14271. oa->write_character(to_char_type('I')); // int16
  14272. }
  14273. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14274. }
  14275. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  14276. {
  14277. if (add_prefix)
  14278. {
  14279. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14280. }
  14281. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  14282. }
  14283. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14284. {
  14285. if (add_prefix)
  14286. {
  14287. oa->write_character(to_char_type('l')); // int32
  14288. }
  14289. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14290. }
  14291. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  14292. {
  14293. if (add_prefix)
  14294. {
  14295. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14296. }
  14297. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  14298. }
  14299. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14300. {
  14301. if (add_prefix)
  14302. {
  14303. oa->write_character(to_char_type('L')); // int64
  14304. }
  14305. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14306. }
  14307. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  14308. {
  14309. if (add_prefix)
  14310. {
  14311. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  14312. }
  14313. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  14314. }
  14315. else
  14316. {
  14317. if (add_prefix)
  14318. {
  14319. oa->write_character(to_char_type('H')); // high-precision number
  14320. }
  14321. const auto number = BasicJsonType(n).dump();
  14322. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14323. for (std::size_t i = 0; i < number.size(); ++i)
  14324. {
  14325. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14326. }
  14327. }
  14328. }
  14329. // UBJSON: write number (signed integer)
  14330. template < typename NumberType, typename std::enable_if <
  14331. std::is_signed<NumberType>::value&&
  14332. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  14333. void write_number_with_ubjson_prefix(const NumberType n,
  14334. const bool add_prefix,
  14335. const bool use_bjdata)
  14336. {
  14337. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  14338. {
  14339. if (add_prefix)
  14340. {
  14341. oa->write_character(to_char_type('i')); // int8
  14342. }
  14343. write_number(static_cast<std::int8_t>(n), use_bjdata);
  14344. }
  14345. 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)()))
  14346. {
  14347. if (add_prefix)
  14348. {
  14349. oa->write_character(to_char_type('U')); // uint8
  14350. }
  14351. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14352. }
  14353. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  14354. {
  14355. if (add_prefix)
  14356. {
  14357. oa->write_character(to_char_type('I')); // int16
  14358. }
  14359. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14360. }
  14361. 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)())))
  14362. {
  14363. if (add_prefix)
  14364. {
  14365. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14366. }
  14367. write_number(static_cast<uint16_t>(n), use_bjdata);
  14368. }
  14369. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  14370. {
  14371. if (add_prefix)
  14372. {
  14373. oa->write_character(to_char_type('l')); // int32
  14374. }
  14375. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14376. }
  14377. 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)())))
  14378. {
  14379. if (add_prefix)
  14380. {
  14381. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14382. }
  14383. write_number(static_cast<uint32_t>(n), use_bjdata);
  14384. }
  14385. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  14386. {
  14387. if (add_prefix)
  14388. {
  14389. oa->write_character(to_char_type('L')); // int64
  14390. }
  14391. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14392. }
  14393. // LCOV_EXCL_START
  14394. else
  14395. {
  14396. if (add_prefix)
  14397. {
  14398. oa->write_character(to_char_type('H')); // high-precision number
  14399. }
  14400. const auto number = BasicJsonType(n).dump();
  14401. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14402. for (std::size_t i = 0; i < number.size(); ++i)
  14403. {
  14404. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14405. }
  14406. }
  14407. // LCOV_EXCL_STOP
  14408. }
  14409. /*!
  14410. @brief determine the type prefix of container values
  14411. */
  14412. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  14413. {
  14414. switch (j.type())
  14415. {
  14416. case value_t::null:
  14417. return 'Z';
  14418. case value_t::boolean:
  14419. return j.m_data.m_value.boolean ? 'T' : 'F';
  14420. case value_t::number_integer:
  14421. {
  14422. 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)())
  14423. {
  14424. return 'i';
  14425. }
  14426. 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)())
  14427. {
  14428. return 'U';
  14429. }
  14430. 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)())
  14431. {
  14432. return 'I';
  14433. }
  14434. 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)()))
  14435. {
  14436. return 'u';
  14437. }
  14438. 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)())
  14439. {
  14440. return 'l';
  14441. }
  14442. 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)()))
  14443. {
  14444. return 'm';
  14445. }
  14446. 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)())
  14447. {
  14448. return 'L';
  14449. }
  14450. // anything else is treated as high-precision number
  14451. return 'H'; // LCOV_EXCL_LINE
  14452. }
  14453. case value_t::number_unsigned:
  14454. {
  14455. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14456. {
  14457. return 'i';
  14458. }
  14459. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  14460. {
  14461. return 'U';
  14462. }
  14463. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14464. {
  14465. return 'I';
  14466. }
  14467. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  14468. {
  14469. return 'u';
  14470. }
  14471. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14472. {
  14473. return 'l';
  14474. }
  14475. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  14476. {
  14477. return 'm';
  14478. }
  14479. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14480. {
  14481. return 'L';
  14482. }
  14483. if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14484. {
  14485. return 'M';
  14486. }
  14487. // anything else is treated as high-precision number
  14488. return 'H'; // LCOV_EXCL_LINE
  14489. }
  14490. case value_t::number_float:
  14491. return get_ubjson_float_prefix(j.m_data.m_value.number_float);
  14492. case value_t::string:
  14493. return 'S';
  14494. case value_t::array: // fallthrough
  14495. case value_t::binary:
  14496. return '[';
  14497. case value_t::object:
  14498. return '{';
  14499. case value_t::discarded:
  14500. default: // discarded values
  14501. return 'N';
  14502. }
  14503. }
  14504. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  14505. {
  14506. return 'd'; // float 32
  14507. }
  14508. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  14509. {
  14510. return 'D'; // float 64
  14511. }
  14512. /*!
  14513. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  14514. */
  14515. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  14516. {
  14517. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  14518. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  14519. };
  14520. string_t key = "_ArrayType_";
  14521. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  14522. if (it == bjdtype.end())
  14523. {
  14524. return true;
  14525. }
  14526. CharType dtype = it->second;
  14527. key = "_ArraySize_";
  14528. std::size_t len = (value.at(key).empty() ? 0 : 1);
  14529. for (const auto& el : value.at(key))
  14530. {
  14531. len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
  14532. }
  14533. key = "_ArrayData_";
  14534. if (value.at(key).size() != len)
  14535. {
  14536. return true;
  14537. }
  14538. oa->write_character('[');
  14539. oa->write_character('$');
  14540. oa->write_character(dtype);
  14541. oa->write_character('#');
  14542. key = "_ArraySize_";
  14543. write_ubjson(value.at(key), use_count, use_type, true, true);
  14544. key = "_ArrayData_";
  14545. if (dtype == 'U' || dtype == 'C')
  14546. {
  14547. for (const auto& el : value.at(key))
  14548. {
  14549. write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
  14550. }
  14551. }
  14552. else if (dtype == 'i')
  14553. {
  14554. for (const auto& el : value.at(key))
  14555. {
  14556. write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
  14557. }
  14558. }
  14559. else if (dtype == 'u')
  14560. {
  14561. for (const auto& el : value.at(key))
  14562. {
  14563. write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
  14564. }
  14565. }
  14566. else if (dtype == 'I')
  14567. {
  14568. for (const auto& el : value.at(key))
  14569. {
  14570. write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
  14571. }
  14572. }
  14573. else if (dtype == 'm')
  14574. {
  14575. for (const auto& el : value.at(key))
  14576. {
  14577. write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
  14578. }
  14579. }
  14580. else if (dtype == 'l')
  14581. {
  14582. for (const auto& el : value.at(key))
  14583. {
  14584. write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
  14585. }
  14586. }
  14587. else if (dtype == 'M')
  14588. {
  14589. for (const auto& el : value.at(key))
  14590. {
  14591. write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
  14592. }
  14593. }
  14594. else if (dtype == 'L')
  14595. {
  14596. for (const auto& el : value.at(key))
  14597. {
  14598. write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
  14599. }
  14600. }
  14601. else if (dtype == 'd')
  14602. {
  14603. for (const auto& el : value.at(key))
  14604. {
  14605. write_number(static_cast<float>(el.m_data.m_value.number_float), true);
  14606. }
  14607. }
  14608. else if (dtype == 'D')
  14609. {
  14610. for (const auto& el : value.at(key))
  14611. {
  14612. write_number(static_cast<double>(el.m_data.m_value.number_float), true);
  14613. }
  14614. }
  14615. return false;
  14616. }
  14617. ///////////////////////
  14618. // Utility functions //
  14619. ///////////////////////
  14620. /*
  14621. @brief write a number to output input
  14622. @param[in] n number of type @a NumberType
  14623. @param[in] OutputIsLittleEndian Set to true if output data is
  14624. required to be little endian
  14625. @tparam NumberType the type of the number
  14626. @note This function needs to respect the system's endianness, because bytes
  14627. in CBOR, MessagePack, and UBJSON are stored in network order (big
  14628. endian) and therefore need reordering on little endian systems.
  14629. On the other hand, BSON and BJData use little endian and should reorder
  14630. on big endian systems.
  14631. */
  14632. template<typename NumberType>
  14633. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  14634. {
  14635. // step 1: write number to array of length NumberType
  14636. std::array<CharType, sizeof(NumberType)> vec{};
  14637. std::memcpy(vec.data(), &n, sizeof(NumberType));
  14638. // step 2: write array to output (with possible reordering)
  14639. if (is_little_endian != OutputIsLittleEndian)
  14640. {
  14641. // reverse byte order prior to conversion if necessary
  14642. std::reverse(vec.begin(), vec.end());
  14643. }
  14644. oa->write_characters(vec.data(), sizeof(NumberType));
  14645. }
  14646. void write_compact_float(const number_float_t n, detail::input_format_t format)
  14647. {
  14648. #ifdef __GNUC__
  14649. #pragma GCC diagnostic push
  14650. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14651. #endif
  14652. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  14653. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  14654. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  14655. {
  14656. oa->write_character(format == detail::input_format_t::cbor
  14657. ? get_cbor_float_prefix(static_cast<float>(n))
  14658. : get_msgpack_float_prefix(static_cast<float>(n)));
  14659. write_number(static_cast<float>(n));
  14660. }
  14661. else
  14662. {
  14663. oa->write_character(format == detail::input_format_t::cbor
  14664. ? get_cbor_float_prefix(n)
  14665. : get_msgpack_float_prefix(n));
  14666. write_number(n);
  14667. }
  14668. #ifdef __GNUC__
  14669. #pragma GCC diagnostic pop
  14670. #endif
  14671. }
  14672. public:
  14673. // The following to_char_type functions are implement the conversion
  14674. // between uint8_t and CharType. In case CharType is not unsigned,
  14675. // such a conversion is required to allow values greater than 128.
  14676. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  14677. template < typename C = CharType,
  14678. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  14679. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14680. {
  14681. return *reinterpret_cast<char*>(&x);
  14682. }
  14683. template < typename C = CharType,
  14684. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  14685. static CharType to_char_type(std::uint8_t x) noexcept
  14686. {
  14687. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  14688. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  14689. CharType result;
  14690. std::memcpy(&result, &x, sizeof(x));
  14691. return result;
  14692. }
  14693. template<typename C = CharType,
  14694. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  14695. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14696. {
  14697. return x;
  14698. }
  14699. template < typename InputCharType, typename C = CharType,
  14700. enable_if_t <
  14701. std::is_signed<C>::value &&
  14702. std::is_signed<char>::value &&
  14703. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  14704. > * = nullptr >
  14705. static constexpr CharType to_char_type(InputCharType x) noexcept
  14706. {
  14707. return x;
  14708. }
  14709. private:
  14710. /// whether we can assume little endianness
  14711. const bool is_little_endian = little_endianness();
  14712. /// the output
  14713. output_adapter_t<CharType> oa = nullptr;
  14714. };
  14715. } // namespace detail
  14716. NLOHMANN_JSON_NAMESPACE_END
  14717. // #include <nlohmann/detail/output/output_adapters.hpp>
  14718. // #include <nlohmann/detail/output/serializer.hpp>
  14719. // __ _____ _____ _____
  14720. // __| | __| | | | JSON for Modern C++
  14721. // | | |__ | | | | | | version 3.11.3
  14722. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14723. //
  14724. // SPDX-FileCopyrightText: 2008-2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  14725. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  14726. // SPDX-License-Identifier: MIT
  14727. #include <algorithm> // reverse, remove, fill, find, none_of
  14728. #include <array> // array
  14729. #include <clocale> // localeconv, lconv
  14730. #include <cmath> // labs, isfinite, isnan, signbit
  14731. #include <cstddef> // size_t, ptrdiff_t
  14732. #include <cstdint> // uint8_t
  14733. #include <cstdio> // snprintf
  14734. #include <limits> // numeric_limits
  14735. #include <string> // string, char_traits
  14736. #include <iomanip> // setfill, setw
  14737. #include <type_traits> // is_same
  14738. #include <utility> // move
  14739. // #include <nlohmann/detail/conversions/to_chars.hpp>
  14740. // __ _____ _____ _____
  14741. // __| | __| | | | JSON for Modern C++
  14742. // | | |__ | | | | | | version 3.11.3
  14743. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14744. //
  14745. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  14746. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  14747. // SPDX-License-Identifier: MIT
  14748. #include <array> // array
  14749. #include <cmath> // signbit, isfinite
  14750. #include <cstdint> // intN_t, uintN_t
  14751. #include <cstring> // memcpy, memmove
  14752. #include <limits> // numeric_limits
  14753. #include <type_traits> // conditional
  14754. // #include <nlohmann/detail/macro_scope.hpp>
  14755. NLOHMANN_JSON_NAMESPACE_BEGIN
  14756. namespace detail
  14757. {
  14758. /*!
  14759. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  14760. conversion.
  14761. This implementation is a slightly modified version of the reference
  14762. implementation which may be obtained from
  14763. http://florian.loitsch.com/publications (bench.tar.gz).
  14764. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  14765. For a detailed description of the algorithm see:
  14766. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  14767. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  14768. Language Design and Implementation, PLDI 2010
  14769. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  14770. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  14771. Design and Implementation, PLDI 1996
  14772. */
  14773. namespace dtoa_impl
  14774. {
  14775. template<typename Target, typename Source>
  14776. Target reinterpret_bits(const Source source)
  14777. {
  14778. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  14779. Target target;
  14780. std::memcpy(&target, &source, sizeof(Source));
  14781. return target;
  14782. }
  14783. struct diyfp // f * 2^e
  14784. {
  14785. static constexpr int kPrecision = 64; // = q
  14786. std::uint64_t f = 0;
  14787. int e = 0;
  14788. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  14789. /*!
  14790. @brief returns x - y
  14791. @pre x.e == y.e and x.f >= y.f
  14792. */
  14793. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  14794. {
  14795. JSON_ASSERT(x.e == y.e);
  14796. JSON_ASSERT(x.f >= y.f);
  14797. return {x.f - y.f, x.e};
  14798. }
  14799. /*!
  14800. @brief returns x * y
  14801. @note The result is rounded. (Only the upper q bits are returned.)
  14802. */
  14803. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  14804. {
  14805. static_assert(kPrecision == 64, "internal error");
  14806. // Computes:
  14807. // f = round((x.f * y.f) / 2^q)
  14808. // e = x.e + y.e + q
  14809. // Emulate the 64-bit * 64-bit multiplication:
  14810. //
  14811. // p = u * v
  14812. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  14813. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  14814. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  14815. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  14816. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  14817. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  14818. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  14819. //
  14820. // (Since Q might be larger than 2^32 - 1)
  14821. //
  14822. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  14823. //
  14824. // (Q_hi + H does not overflow a 64-bit int)
  14825. //
  14826. // = p_lo + 2^64 p_hi
  14827. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  14828. const std::uint64_t u_hi = x.f >> 32u;
  14829. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  14830. const std::uint64_t v_hi = y.f >> 32u;
  14831. const std::uint64_t p0 = u_lo * v_lo;
  14832. const std::uint64_t p1 = u_lo * v_hi;
  14833. const std::uint64_t p2 = u_hi * v_lo;
  14834. const std::uint64_t p3 = u_hi * v_hi;
  14835. const std::uint64_t p0_hi = p0 >> 32u;
  14836. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  14837. const std::uint64_t p1_hi = p1 >> 32u;
  14838. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  14839. const std::uint64_t p2_hi = p2 >> 32u;
  14840. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  14841. // The full product might now be computed as
  14842. //
  14843. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  14844. // p_lo = p0_lo + (Q << 32)
  14845. //
  14846. // But in this particular case here, the full p_lo is not required.
  14847. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  14848. // Q_hi + 1 does not overflow).
  14849. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  14850. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  14851. return {h, x.e + y.e + 64};
  14852. }
  14853. /*!
  14854. @brief normalize x such that the significand is >= 2^(q-1)
  14855. @pre x.f != 0
  14856. */
  14857. static diyfp normalize(diyfp x) noexcept
  14858. {
  14859. JSON_ASSERT(x.f != 0);
  14860. while ((x.f >> 63u) == 0)
  14861. {
  14862. x.f <<= 1u;
  14863. x.e--;
  14864. }
  14865. return x;
  14866. }
  14867. /*!
  14868. @brief normalize x such that the result has the exponent E
  14869. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  14870. */
  14871. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  14872. {
  14873. const int delta = x.e - target_exponent;
  14874. JSON_ASSERT(delta >= 0);
  14875. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  14876. return {x.f << delta, target_exponent};
  14877. }
  14878. };
  14879. struct boundaries
  14880. {
  14881. diyfp w;
  14882. diyfp minus;
  14883. diyfp plus;
  14884. };
  14885. /*!
  14886. Compute the (normalized) diyfp representing the input number 'value' and its
  14887. boundaries.
  14888. @pre value must be finite and positive
  14889. */
  14890. template<typename FloatType>
  14891. boundaries compute_boundaries(FloatType value)
  14892. {
  14893. JSON_ASSERT(std::isfinite(value));
  14894. JSON_ASSERT(value > 0);
  14895. // Convert the IEEE representation into a diyfp.
  14896. //
  14897. // If v is denormal:
  14898. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  14899. // If v is normalized:
  14900. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  14901. static_assert(std::numeric_limits<FloatType>::is_iec559,
  14902. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  14903. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  14904. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  14905. constexpr int kMinExp = 1 - kBias;
  14906. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  14907. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  14908. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  14909. const std::uint64_t E = bits >> (kPrecision - 1);
  14910. const std::uint64_t F = bits & (kHiddenBit - 1);
  14911. const bool is_denormal = E == 0;
  14912. const diyfp v = is_denormal
  14913. ? diyfp(F, kMinExp)
  14914. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  14915. // Compute the boundaries m- and m+ of the floating-point value
  14916. // v = f * 2^e.
  14917. //
  14918. // Determine v- and v+, the floating-point predecessor and successor if v,
  14919. // respectively.
  14920. //
  14921. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  14922. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  14923. //
  14924. // v+ = v + 2^e
  14925. //
  14926. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  14927. // between m- and m+ round to v, regardless of how the input rounding
  14928. // algorithm breaks ties.
  14929. //
  14930. // ---+-------------+-------------+-------------+-------------+--- (A)
  14931. // v- m- v m+ v+
  14932. //
  14933. // -----------------+------+------+-------------+-------------+--- (B)
  14934. // v- m- v m+ v+
  14935. const bool lower_boundary_is_closer = F == 0 && E > 1;
  14936. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  14937. const diyfp m_minus = lower_boundary_is_closer
  14938. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  14939. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  14940. // Determine the normalized w+ = m+.
  14941. const diyfp w_plus = diyfp::normalize(m_plus);
  14942. // Determine w- = m- such that e_(w-) = e_(w+).
  14943. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  14944. return {diyfp::normalize(v), w_minus, w_plus};
  14945. }
  14946. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  14947. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  14948. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  14949. //
  14950. // alpha <= e = e_c + e_w + q <= gamma
  14951. //
  14952. // or
  14953. //
  14954. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  14955. // <= f_c * f_w * 2^gamma
  14956. //
  14957. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  14958. //
  14959. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  14960. //
  14961. // or
  14962. //
  14963. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  14964. //
  14965. // The choice of (alpha,gamma) determines the size of the table and the form of
  14966. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  14967. // in practice:
  14968. //
  14969. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  14970. // processed independently: An integral part p1, and a fractional part p2:
  14971. //
  14972. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  14973. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  14974. // = p1 + p2 * 2^e
  14975. //
  14976. // The conversion of p1 into decimal form requires a series of divisions and
  14977. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  14978. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  14979. // achieved by choosing
  14980. //
  14981. // -e >= 32 or e <= -32 := gamma
  14982. //
  14983. // In order to convert the fractional part
  14984. //
  14985. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14986. //
  14987. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  14988. // d[-i] are extracted in order:
  14989. //
  14990. // (10 * p2) div 2^-e = d[-1]
  14991. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  14992. //
  14993. // The multiplication by 10 must not overflow. It is sufficient to choose
  14994. //
  14995. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  14996. //
  14997. // Since p2 = f mod 2^-e < 2^-e,
  14998. //
  14999. // -e <= 60 or e >= -60 := alpha
  15000. constexpr int kAlpha = -60;
  15001. constexpr int kGamma = -32;
  15002. struct cached_power // c = f * 2^e ~= 10^k
  15003. {
  15004. std::uint64_t f;
  15005. int e;
  15006. int k;
  15007. };
  15008. /*!
  15009. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  15010. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  15011. satisfies (Definition 3.2 from [1])
  15012. alpha <= e_c + e + q <= gamma.
  15013. */
  15014. inline cached_power get_cached_power_for_binary_exponent(int e)
  15015. {
  15016. // Now
  15017. //
  15018. // alpha <= e_c + e + q <= gamma (1)
  15019. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  15020. //
  15021. // and since the c's are normalized, 2^(q-1) <= f_c,
  15022. //
  15023. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  15024. // ==> 2^(alpha - e - 1) <= c
  15025. //
  15026. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  15027. //
  15028. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  15029. // = ceil( (alpha - e - 1) * log_10(2) )
  15030. //
  15031. // From the paper:
  15032. // "In theory the result of the procedure could be wrong since c is rounded,
  15033. // and the computation itself is approximated [...]. In practice, however,
  15034. // this simple function is sufficient."
  15035. //
  15036. // For IEEE double precision floating-point numbers converted into
  15037. // normalized diyfp's w = f * 2^e, with q = 64,
  15038. //
  15039. // e >= -1022 (min IEEE exponent)
  15040. // -52 (p - 1)
  15041. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  15042. // -11 (normalize the diyfp)
  15043. // = -1137
  15044. //
  15045. // and
  15046. //
  15047. // e <= +1023 (max IEEE exponent)
  15048. // -52 (p - 1)
  15049. // -11 (normalize the diyfp)
  15050. // = 960
  15051. //
  15052. // This binary exponent range [-1137,960] results in a decimal exponent
  15053. // range [-307,324]. One does not need to store a cached power for each
  15054. // k in this range. For each such k it suffices to find a cached power
  15055. // such that the exponent of the product lies in [alpha,gamma].
  15056. // This implies that the difference of the decimal exponents of adjacent
  15057. // table entries must be less than or equal to
  15058. //
  15059. // floor( (gamma - alpha) * log_10(2) ) = 8.
  15060. //
  15061. // (A smaller distance gamma-alpha would require a larger table.)
  15062. // NB:
  15063. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  15064. constexpr int kCachedPowersMinDecExp = -300;
  15065. constexpr int kCachedPowersDecStep = 8;
  15066. static constexpr std::array<cached_power, 79> kCachedPowers =
  15067. {
  15068. {
  15069. { 0xAB70FE17C79AC6CA, -1060, -300 },
  15070. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  15071. { 0xBE5691EF416BD60C, -1007, -284 },
  15072. { 0x8DD01FAD907FFC3C, -980, -276 },
  15073. { 0xD3515C2831559A83, -954, -268 },
  15074. { 0x9D71AC8FADA6C9B5, -927, -260 },
  15075. { 0xEA9C227723EE8BCB, -901, -252 },
  15076. { 0xAECC49914078536D, -874, -244 },
  15077. { 0x823C12795DB6CE57, -847, -236 },
  15078. { 0xC21094364DFB5637, -821, -228 },
  15079. { 0x9096EA6F3848984F, -794, -220 },
  15080. { 0xD77485CB25823AC7, -768, -212 },
  15081. { 0xA086CFCD97BF97F4, -741, -204 },
  15082. { 0xEF340A98172AACE5, -715, -196 },
  15083. { 0xB23867FB2A35B28E, -688, -188 },
  15084. { 0x84C8D4DFD2C63F3B, -661, -180 },
  15085. { 0xC5DD44271AD3CDBA, -635, -172 },
  15086. { 0x936B9FCEBB25C996, -608, -164 },
  15087. { 0xDBAC6C247D62A584, -582, -156 },
  15088. { 0xA3AB66580D5FDAF6, -555, -148 },
  15089. { 0xF3E2F893DEC3F126, -529, -140 },
  15090. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  15091. { 0x87625F056C7C4A8B, -475, -124 },
  15092. { 0xC9BCFF6034C13053, -449, -116 },
  15093. { 0x964E858C91BA2655, -422, -108 },
  15094. { 0xDFF9772470297EBD, -396, -100 },
  15095. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  15096. { 0xF8A95FCF88747D94, -343, -84 },
  15097. { 0xB94470938FA89BCF, -316, -76 },
  15098. { 0x8A08F0F8BF0F156B, -289, -68 },
  15099. { 0xCDB02555653131B6, -263, -60 },
  15100. { 0x993FE2C6D07B7FAC, -236, -52 },
  15101. { 0xE45C10C42A2B3B06, -210, -44 },
  15102. { 0xAA242499697392D3, -183, -36 },
  15103. { 0xFD87B5F28300CA0E, -157, -28 },
  15104. { 0xBCE5086492111AEB, -130, -20 },
  15105. { 0x8CBCCC096F5088CC, -103, -12 },
  15106. { 0xD1B71758E219652C, -77, -4 },
  15107. { 0x9C40000000000000, -50, 4 },
  15108. { 0xE8D4A51000000000, -24, 12 },
  15109. { 0xAD78EBC5AC620000, 3, 20 },
  15110. { 0x813F3978F8940984, 30, 28 },
  15111. { 0xC097CE7BC90715B3, 56, 36 },
  15112. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15113. { 0xD5D238A4ABE98068, 109, 52 },
  15114. { 0x9F4F2726179A2245, 136, 60 },
  15115. { 0xED63A231D4C4FB27, 162, 68 },
  15116. { 0xB0DE65388CC8ADA8, 189, 76 },
  15117. { 0x83C7088E1AAB65DB, 216, 84 },
  15118. { 0xC45D1DF942711D9A, 242, 92 },
  15119. { 0x924D692CA61BE758, 269, 100 },
  15120. { 0xDA01EE641A708DEA, 295, 108 },
  15121. { 0xA26DA3999AEF774A, 322, 116 },
  15122. { 0xF209787BB47D6B85, 348, 124 },
  15123. { 0xB454E4A179DD1877, 375, 132 },
  15124. { 0x865B86925B9BC5C2, 402, 140 },
  15125. { 0xC83553C5C8965D3D, 428, 148 },
  15126. { 0x952AB45CFA97A0B3, 455, 156 },
  15127. { 0xDE469FBD99A05FE3, 481, 164 },
  15128. { 0xA59BC234DB398C25, 508, 172 },
  15129. { 0xF6C69A72A3989F5C, 534, 180 },
  15130. { 0xB7DCBF5354E9BECE, 561, 188 },
  15131. { 0x88FCF317F22241E2, 588, 196 },
  15132. { 0xCC20CE9BD35C78A5, 614, 204 },
  15133. { 0x98165AF37B2153DF, 641, 212 },
  15134. { 0xE2A0B5DC971F303A, 667, 220 },
  15135. { 0xA8D9D1535CE3B396, 694, 228 },
  15136. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15137. { 0xBB764C4CA7A44410, 747, 244 },
  15138. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15139. { 0xD01FEF10A657842C, 800, 260 },
  15140. { 0x9B10A4E5E9913129, 827, 268 },
  15141. { 0xE7109BFBA19C0C9D, 853, 276 },
  15142. { 0xAC2820D9623BF429, 880, 284 },
  15143. { 0x80444B5E7AA7CF85, 907, 292 },
  15144. { 0xBF21E44003ACDD2D, 933, 300 },
  15145. { 0x8E679C2F5E44FF8F, 960, 308 },
  15146. { 0xD433179D9C8CB841, 986, 316 },
  15147. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15148. }
  15149. };
  15150. // This computation gives exactly the same results for k as
  15151. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15152. // for |e| <= 1500, but doesn't require floating-point operations.
  15153. // NB: log_10(2) ~= 78913 / 2^18
  15154. JSON_ASSERT(e >= -1500);
  15155. JSON_ASSERT(e <= 1500);
  15156. const int f = kAlpha - e - 1;
  15157. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  15158. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15159. JSON_ASSERT(index >= 0);
  15160. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15161. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15162. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15163. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15164. return cached;
  15165. }
  15166. /*!
  15167. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15168. For n == 0, returns 1 and sets pow10 := 1.
  15169. */
  15170. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15171. {
  15172. // LCOV_EXCL_START
  15173. if (n >= 1000000000)
  15174. {
  15175. pow10 = 1000000000;
  15176. return 10;
  15177. }
  15178. // LCOV_EXCL_STOP
  15179. if (n >= 100000000)
  15180. {
  15181. pow10 = 100000000;
  15182. return 9;
  15183. }
  15184. if (n >= 10000000)
  15185. {
  15186. pow10 = 10000000;
  15187. return 8;
  15188. }
  15189. if (n >= 1000000)
  15190. {
  15191. pow10 = 1000000;
  15192. return 7;
  15193. }
  15194. if (n >= 100000)
  15195. {
  15196. pow10 = 100000;
  15197. return 6;
  15198. }
  15199. if (n >= 10000)
  15200. {
  15201. pow10 = 10000;
  15202. return 5;
  15203. }
  15204. if (n >= 1000)
  15205. {
  15206. pow10 = 1000;
  15207. return 4;
  15208. }
  15209. if (n >= 100)
  15210. {
  15211. pow10 = 100;
  15212. return 3;
  15213. }
  15214. if (n >= 10)
  15215. {
  15216. pow10 = 10;
  15217. return 2;
  15218. }
  15219. pow10 = 1;
  15220. return 1;
  15221. }
  15222. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  15223. std::uint64_t rest, std::uint64_t ten_k)
  15224. {
  15225. JSON_ASSERT(len >= 1);
  15226. JSON_ASSERT(dist <= delta);
  15227. JSON_ASSERT(rest <= delta);
  15228. JSON_ASSERT(ten_k > 0);
  15229. // <--------------------------- delta ---->
  15230. // <---- dist --------->
  15231. // --------------[------------------+-------------------]--------------
  15232. // M- w M+
  15233. //
  15234. // ten_k
  15235. // <------>
  15236. // <---- rest ---->
  15237. // --------------[------------------+----+--------------]--------------
  15238. // w V
  15239. // = buf * 10^k
  15240. //
  15241. // ten_k represents a unit-in-the-last-place in the decimal representation
  15242. // stored in buf.
  15243. // Decrement buf by ten_k while this takes buf closer to w.
  15244. // The tests are written in this order to avoid overflow in unsigned
  15245. // integer arithmetic.
  15246. while (rest < dist
  15247. && delta - rest >= ten_k
  15248. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  15249. {
  15250. JSON_ASSERT(buf[len - 1] != '0');
  15251. buf[len - 1]--;
  15252. rest += ten_k;
  15253. }
  15254. }
  15255. /*!
  15256. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  15257. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  15258. */
  15259. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  15260. diyfp M_minus, diyfp w, diyfp M_plus)
  15261. {
  15262. static_assert(kAlpha >= -60, "internal error");
  15263. static_assert(kGamma <= -32, "internal error");
  15264. // Generates the digits (and the exponent) of a decimal floating-point
  15265. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  15266. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  15267. //
  15268. // <--------------------------- delta ---->
  15269. // <---- dist --------->
  15270. // --------------[------------------+-------------------]--------------
  15271. // M- w M+
  15272. //
  15273. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  15274. // V is in [M-,M+].
  15275. JSON_ASSERT(M_plus.e >= kAlpha);
  15276. JSON_ASSERT(M_plus.e <= kGamma);
  15277. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  15278. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  15279. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  15280. //
  15281. // M+ = f * 2^e
  15282. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  15283. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  15284. // = p1 + p2 * 2^e
  15285. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  15286. 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.)
  15287. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  15288. // 1)
  15289. //
  15290. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  15291. JSON_ASSERT(p1 > 0);
  15292. std::uint32_t pow10{};
  15293. const int k = find_largest_pow10(p1, pow10);
  15294. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  15295. //
  15296. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  15297. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  15298. //
  15299. // M+ = p1 + p2 * 2^e
  15300. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  15301. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  15302. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  15303. //
  15304. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  15305. //
  15306. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  15307. //
  15308. // but stop as soon as
  15309. //
  15310. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  15311. int n = k;
  15312. while (n > 0)
  15313. {
  15314. // Invariants:
  15315. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  15316. // pow10 = 10^(n-1) <= p1 < 10^n
  15317. //
  15318. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  15319. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  15320. //
  15321. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  15322. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  15323. //
  15324. JSON_ASSERT(d <= 9);
  15325. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15326. //
  15327. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  15328. //
  15329. p1 = r;
  15330. n--;
  15331. //
  15332. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  15333. // pow10 = 10^n
  15334. //
  15335. // Now check if enough digits have been generated.
  15336. // Compute
  15337. //
  15338. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  15339. //
  15340. // Note:
  15341. // Since rest and delta share the same exponent e, it suffices to
  15342. // compare the significands.
  15343. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  15344. if (rest <= delta)
  15345. {
  15346. // V = buffer * 10^n, with M- <= V <= M+.
  15347. decimal_exponent += n;
  15348. // We may now just stop. But instead look if the buffer could be
  15349. // decremented to bring V closer to w.
  15350. //
  15351. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  15352. // The rounding procedure works with diyfp's with an implicit
  15353. // exponent of e.
  15354. //
  15355. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  15356. //
  15357. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  15358. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  15359. return;
  15360. }
  15361. pow10 /= 10;
  15362. //
  15363. // pow10 = 10^(n-1) <= p1 < 10^n
  15364. // Invariants restored.
  15365. }
  15366. // 2)
  15367. //
  15368. // The digits of the integral part have been generated:
  15369. //
  15370. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  15371. // = buffer + p2 * 2^e
  15372. //
  15373. // Now generate the digits of the fractional part p2 * 2^e.
  15374. //
  15375. // Note:
  15376. // No decimal point is generated: the exponent is adjusted instead.
  15377. //
  15378. // p2 actually represents the fraction
  15379. //
  15380. // p2 * 2^e
  15381. // = p2 / 2^-e
  15382. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15383. //
  15384. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  15385. //
  15386. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  15387. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  15388. //
  15389. // using
  15390. //
  15391. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  15392. // = ( d) * 2^-e + ( r)
  15393. //
  15394. // or
  15395. // 10^m * p2 * 2^e = d + r * 2^e
  15396. //
  15397. // i.e.
  15398. //
  15399. // M+ = buffer + p2 * 2^e
  15400. // = buffer + 10^-m * (d + r * 2^e)
  15401. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  15402. //
  15403. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  15404. JSON_ASSERT(p2 > delta);
  15405. int m = 0;
  15406. for (;;)
  15407. {
  15408. // Invariant:
  15409. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  15410. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  15411. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  15412. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  15413. //
  15414. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  15415. p2 *= 10;
  15416. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  15417. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  15418. //
  15419. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  15420. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  15421. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15422. //
  15423. JSON_ASSERT(d <= 9);
  15424. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15425. //
  15426. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15427. //
  15428. p2 = r;
  15429. m++;
  15430. //
  15431. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  15432. // Invariant restored.
  15433. // Check if enough digits have been generated.
  15434. //
  15435. // 10^-m * p2 * 2^e <= delta * 2^e
  15436. // p2 * 2^e <= 10^m * delta * 2^e
  15437. // p2 <= 10^m * delta
  15438. delta *= 10;
  15439. dist *= 10;
  15440. if (p2 <= delta)
  15441. {
  15442. break;
  15443. }
  15444. }
  15445. // V = buffer * 10^-m, with M- <= V <= M+.
  15446. decimal_exponent -= m;
  15447. // 1 ulp in the decimal representation is now 10^-m.
  15448. // Since delta and dist are now scaled by 10^m, we need to do the
  15449. // same with ulp in order to keep the units in sync.
  15450. //
  15451. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  15452. //
  15453. const std::uint64_t ten_m = one.f;
  15454. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  15455. // By construction this algorithm generates the shortest possible decimal
  15456. // number (Loitsch, Theorem 6.2) which rounds back to w.
  15457. // For an input number of precision p, at least
  15458. //
  15459. // N = 1 + ceil(p * log_10(2))
  15460. //
  15461. // decimal digits are sufficient to identify all binary floating-point
  15462. // numbers (Matula, "In-and-Out conversions").
  15463. // This implies that the algorithm does not produce more than N decimal
  15464. // digits.
  15465. //
  15466. // N = 17 for p = 53 (IEEE double precision)
  15467. // N = 9 for p = 24 (IEEE single precision)
  15468. }
  15469. /*!
  15470. v = buf * 10^decimal_exponent
  15471. len is the length of the buffer (number of decimal digits)
  15472. The buffer must be large enough, i.e. >= max_digits10.
  15473. */
  15474. JSON_HEDLEY_NON_NULL(1)
  15475. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  15476. diyfp m_minus, diyfp v, diyfp m_plus)
  15477. {
  15478. JSON_ASSERT(m_plus.e == m_minus.e);
  15479. JSON_ASSERT(m_plus.e == v.e);
  15480. // --------(-----------------------+-----------------------)-------- (A)
  15481. // m- v m+
  15482. //
  15483. // --------------------(-----------+-----------------------)-------- (B)
  15484. // m- v m+
  15485. //
  15486. // First scale v (and m- and m+) such that the exponent is in the range
  15487. // [alpha, gamma].
  15488. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  15489. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  15490. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  15491. const diyfp w = diyfp::mul(v, c_minus_k);
  15492. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  15493. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  15494. // ----(---+---)---------------(---+---)---------------(---+---)----
  15495. // w- w w+
  15496. // = c*m- = c*v = c*m+
  15497. //
  15498. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  15499. // w+ are now off by a small amount.
  15500. // In fact:
  15501. //
  15502. // w - v * 10^k < 1 ulp
  15503. //
  15504. // To account for this inaccuracy, add resp. subtract 1 ulp.
  15505. //
  15506. // --------+---[---------------(---+---)---------------]---+--------
  15507. // w- M- w M+ w+
  15508. //
  15509. // Now any number in [M-, M+] (bounds included) will round to w when input,
  15510. // regardless of how the input rounding algorithm breaks ties.
  15511. //
  15512. // And digit_gen generates the shortest possible such number in [M-, M+].
  15513. // Note that this does not mean that Grisu2 always generates the shortest
  15514. // possible number in the interval (m-, m+).
  15515. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  15516. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  15517. decimal_exponent = -cached.k; // = -(-k) = k
  15518. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  15519. }
  15520. /*!
  15521. v = buf * 10^decimal_exponent
  15522. len is the length of the buffer (number of decimal digits)
  15523. The buffer must be large enough, i.e. >= max_digits10.
  15524. */
  15525. template<typename FloatType>
  15526. JSON_HEDLEY_NON_NULL(1)
  15527. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  15528. {
  15529. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  15530. "internal error: not enough precision");
  15531. JSON_ASSERT(std::isfinite(value));
  15532. JSON_ASSERT(value > 0);
  15533. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  15534. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  15535. // decimal representations are not exactly "short".
  15536. //
  15537. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  15538. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  15539. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  15540. // does.
  15541. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  15542. // representation using the corresponding std::from_chars function recovers value exactly". That
  15543. // indicates that single precision floating-point numbers should be recovered using
  15544. // 'std::strtof'.
  15545. //
  15546. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  15547. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  15548. // value is off by 1 ulp.
  15549. #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
  15550. const boundaries w = compute_boundaries(static_cast<double>(value));
  15551. #else
  15552. const boundaries w = compute_boundaries(value);
  15553. #endif
  15554. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  15555. }
  15556. /*!
  15557. @brief appends a decimal representation of e to buf
  15558. @return a pointer to the element following the exponent.
  15559. @pre -1000 < e < 1000
  15560. */
  15561. JSON_HEDLEY_NON_NULL(1)
  15562. JSON_HEDLEY_RETURNS_NON_NULL
  15563. inline char* append_exponent(char* buf, int e)
  15564. {
  15565. JSON_ASSERT(e > -1000);
  15566. JSON_ASSERT(e < 1000);
  15567. if (e < 0)
  15568. {
  15569. e = -e;
  15570. *buf++ = '-';
  15571. }
  15572. else
  15573. {
  15574. *buf++ = '+';
  15575. }
  15576. auto k = static_cast<std::uint32_t>(e);
  15577. if (k < 10)
  15578. {
  15579. // Always print at least two digits in the exponent.
  15580. // This is for compatibility with printf("%g").
  15581. *buf++ = '0';
  15582. *buf++ = static_cast<char>('0' + k);
  15583. }
  15584. else if (k < 100)
  15585. {
  15586. *buf++ = static_cast<char>('0' + k / 10);
  15587. k %= 10;
  15588. *buf++ = static_cast<char>('0' + k);
  15589. }
  15590. else
  15591. {
  15592. *buf++ = static_cast<char>('0' + k / 100);
  15593. k %= 100;
  15594. *buf++ = static_cast<char>('0' + k / 10);
  15595. k %= 10;
  15596. *buf++ = static_cast<char>('0' + k);
  15597. }
  15598. return buf;
  15599. }
  15600. /*!
  15601. @brief prettify v = buf * 10^decimal_exponent
  15602. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  15603. notation. Otherwise it will be printed in exponential notation.
  15604. @pre min_exp < 0
  15605. @pre max_exp > 0
  15606. */
  15607. JSON_HEDLEY_NON_NULL(1)
  15608. JSON_HEDLEY_RETURNS_NON_NULL
  15609. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  15610. int min_exp, int max_exp)
  15611. {
  15612. JSON_ASSERT(min_exp < 0);
  15613. JSON_ASSERT(max_exp > 0);
  15614. const int k = len;
  15615. const int n = len + decimal_exponent;
  15616. // v = buf * 10^(n-k)
  15617. // k is the length of the buffer (number of decimal digits)
  15618. // n is the position of the decimal point relative to the start of the buffer.
  15619. if (k <= n && n <= max_exp)
  15620. {
  15621. // digits[000]
  15622. // len <= max_exp + 2
  15623. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  15624. // Make it look like a floating-point number (#362, #378)
  15625. buf[n + 0] = '.';
  15626. buf[n + 1] = '0';
  15627. return buf + (static_cast<size_t>(n) + 2);
  15628. }
  15629. if (0 < n && n <= max_exp)
  15630. {
  15631. // dig.its
  15632. // len <= max_digits10 + 1
  15633. JSON_ASSERT(k > n);
  15634. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  15635. buf[n] = '.';
  15636. return buf + (static_cast<size_t>(k) + 1U);
  15637. }
  15638. if (min_exp < n && n <= 0)
  15639. {
  15640. // 0.[000]digits
  15641. // len <= 2 + (-min_exp - 1) + max_digits10
  15642. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  15643. buf[0] = '0';
  15644. buf[1] = '.';
  15645. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  15646. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  15647. }
  15648. if (k == 1)
  15649. {
  15650. // dE+123
  15651. // len <= 1 + 5
  15652. buf += 1;
  15653. }
  15654. else
  15655. {
  15656. // d.igitsE+123
  15657. // len <= max_digits10 + 1 + 5
  15658. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  15659. buf[1] = '.';
  15660. buf += 1 + static_cast<size_t>(k);
  15661. }
  15662. *buf++ = 'e';
  15663. return append_exponent(buf, n - 1);
  15664. }
  15665. } // namespace dtoa_impl
  15666. /*!
  15667. @brief generates a decimal representation of the floating-point number value in [first, last).
  15668. The format of the resulting decimal representation is similar to printf's %g
  15669. format. Returns an iterator pointing past-the-end of the decimal representation.
  15670. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  15671. @note The buffer must be large enough.
  15672. @note The result is NOT null-terminated.
  15673. */
  15674. template<typename FloatType>
  15675. JSON_HEDLEY_NON_NULL(1, 2)
  15676. JSON_HEDLEY_RETURNS_NON_NULL
  15677. char* to_chars(char* first, const char* last, FloatType value)
  15678. {
  15679. static_cast<void>(last); // maybe unused - fix warning
  15680. JSON_ASSERT(std::isfinite(value));
  15681. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  15682. if (std::signbit(value))
  15683. {
  15684. value = -value;
  15685. *first++ = '-';
  15686. }
  15687. #ifdef __GNUC__
  15688. #pragma GCC diagnostic push
  15689. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15690. #endif
  15691. if (value == 0) // +-0
  15692. {
  15693. *first++ = '0';
  15694. // Make it look like a floating-point number (#362, #378)
  15695. *first++ = '.';
  15696. *first++ = '0';
  15697. return first;
  15698. }
  15699. #ifdef __GNUC__
  15700. #pragma GCC diagnostic pop
  15701. #endif
  15702. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  15703. // Compute v = buffer * 10^decimal_exponent.
  15704. // The decimal digits are stored in the buffer, which needs to be interpreted
  15705. // as an unsigned decimal integer.
  15706. // len is the length of the buffer, i.e. the number of decimal digits.
  15707. int len = 0;
  15708. int decimal_exponent = 0;
  15709. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  15710. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  15711. // Format the buffer like printf("%.*g", prec, value)
  15712. constexpr int kMinExp = -4;
  15713. // Use digits10 here to increase compatibility with version 2.
  15714. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  15715. JSON_ASSERT(last - first >= kMaxExp + 2);
  15716. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  15717. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  15718. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  15719. }
  15720. } // namespace detail
  15721. NLOHMANN_JSON_NAMESPACE_END
  15722. // #include <nlohmann/detail/exceptions.hpp>
  15723. // #include <nlohmann/detail/macro_scope.hpp>
  15724. // #include <nlohmann/detail/meta/cpp_future.hpp>
  15725. // #include <nlohmann/detail/output/binary_writer.hpp>
  15726. // #include <nlohmann/detail/output/output_adapters.hpp>
  15727. // #include <nlohmann/detail/string_concat.hpp>
  15728. // #include <nlohmann/detail/value_t.hpp>
  15729. NLOHMANN_JSON_NAMESPACE_BEGIN
  15730. namespace detail
  15731. {
  15732. ///////////////////
  15733. // serialization //
  15734. ///////////////////
  15735. /// how to treat decoding errors
  15736. enum class error_handler_t
  15737. {
  15738. strict, ///< throw a type_error exception in case of invalid UTF-8
  15739. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  15740. ignore ///< ignore invalid UTF-8 sequences
  15741. };
  15742. template<typename BasicJsonType>
  15743. class serializer
  15744. {
  15745. using string_t = typename BasicJsonType::string_t;
  15746. using number_float_t = typename BasicJsonType::number_float_t;
  15747. using number_integer_t = typename BasicJsonType::number_integer_t;
  15748. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  15749. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  15750. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  15751. static constexpr std::uint8_t UTF8_REJECT = 1;
  15752. public:
  15753. /*!
  15754. @param[in] s output stream to serialize to
  15755. @param[in] ichar indentation character to use
  15756. @param[in] error_handler_ how to react on decoding errors
  15757. */
  15758. serializer(output_adapter_t<char> s, const char ichar,
  15759. error_handler_t error_handler_ = error_handler_t::strict)
  15760. : o(std::move(s))
  15761. , loc(std::localeconv())
  15762. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  15763. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  15764. , indent_char(ichar)
  15765. , indent_string(512, indent_char)
  15766. , error_handler(error_handler_)
  15767. {}
  15768. // delete because of pointer members
  15769. serializer(const serializer&) = delete;
  15770. serializer& operator=(const serializer&) = delete;
  15771. serializer(serializer&&) = delete;
  15772. serializer& operator=(serializer&&) = delete;
  15773. ~serializer() = default;
  15774. /*!
  15775. @brief internal implementation of the serialization function
  15776. This function is called by the public member function dump and organizes
  15777. the serialization internally. The indentation level is propagated as
  15778. additional parameter. In case of arrays and objects, the function is
  15779. called recursively.
  15780. - strings and object keys are escaped using `escape_string()`
  15781. - integer numbers are converted implicitly via `operator<<`
  15782. - floating-point numbers are converted to a string using `"%g"` format
  15783. - binary values are serialized as objects containing the subtype and the
  15784. byte array
  15785. @param[in] val value to serialize
  15786. @param[in] pretty_print whether the output shall be pretty-printed
  15787. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  15788. in the output are escaped with `\uXXXX` sequences, and the result consists
  15789. of ASCII characters only.
  15790. @param[in] indent_step the indent level
  15791. @param[in] current_indent the current indent level (only used internally)
  15792. */
  15793. void dump(const BasicJsonType& val,
  15794. const bool pretty_print,
  15795. const bool ensure_ascii,
  15796. const unsigned int indent_step,
  15797. const unsigned int current_indent = 0)
  15798. {
  15799. switch (val.m_data.m_type)
  15800. {
  15801. case value_t::object:
  15802. {
  15803. if (val.m_data.m_value.object->empty())
  15804. {
  15805. o->write_characters("{}", 2);
  15806. return;
  15807. }
  15808. if (pretty_print)
  15809. {
  15810. o->write_characters("{\n", 2);
  15811. // variable to hold indentation for recursive calls
  15812. const auto new_indent = current_indent + indent_step;
  15813. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15814. {
  15815. indent_string.resize(indent_string.size() * 2, ' ');
  15816. }
  15817. // first n-1 elements
  15818. auto i = val.m_data.m_value.object->cbegin();
  15819. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  15820. {
  15821. o->write_characters(indent_string.c_str(), new_indent);
  15822. o->write_character('\"');
  15823. dump_escaped(i->first, ensure_ascii);
  15824. o->write_characters("\": ", 3);
  15825. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15826. o->write_characters(",\n", 2);
  15827. }
  15828. // last element
  15829. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  15830. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  15831. o->write_characters(indent_string.c_str(), new_indent);
  15832. o->write_character('\"');
  15833. dump_escaped(i->first, ensure_ascii);
  15834. o->write_characters("\": ", 3);
  15835. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15836. o->write_character('\n');
  15837. o->write_characters(indent_string.c_str(), current_indent);
  15838. o->write_character('}');
  15839. }
  15840. else
  15841. {
  15842. o->write_character('{');
  15843. // first n-1 elements
  15844. auto i = val.m_data.m_value.object->cbegin();
  15845. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  15846. {
  15847. o->write_character('\"');
  15848. dump_escaped(i->first, ensure_ascii);
  15849. o->write_characters("\":", 2);
  15850. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15851. o->write_character(',');
  15852. }
  15853. // last element
  15854. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  15855. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  15856. o->write_character('\"');
  15857. dump_escaped(i->first, ensure_ascii);
  15858. o->write_characters("\":", 2);
  15859. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15860. o->write_character('}');
  15861. }
  15862. return;
  15863. }
  15864. case value_t::array:
  15865. {
  15866. if (val.m_data.m_value.array->empty())
  15867. {
  15868. o->write_characters("[]", 2);
  15869. return;
  15870. }
  15871. if (pretty_print)
  15872. {
  15873. o->write_characters("[\n", 2);
  15874. // variable to hold indentation for recursive calls
  15875. const auto new_indent = current_indent + indent_step;
  15876. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15877. {
  15878. indent_string.resize(indent_string.size() * 2, ' ');
  15879. }
  15880. // first n-1 elements
  15881. for (auto i = val.m_data.m_value.array->cbegin();
  15882. i != val.m_data.m_value.array->cend() - 1; ++i)
  15883. {
  15884. o->write_characters(indent_string.c_str(), new_indent);
  15885. dump(*i, true, ensure_ascii, indent_step, new_indent);
  15886. o->write_characters(",\n", 2);
  15887. }
  15888. // last element
  15889. JSON_ASSERT(!val.m_data.m_value.array->empty());
  15890. o->write_characters(indent_string.c_str(), new_indent);
  15891. dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  15892. o->write_character('\n');
  15893. o->write_characters(indent_string.c_str(), current_indent);
  15894. o->write_character(']');
  15895. }
  15896. else
  15897. {
  15898. o->write_character('[');
  15899. // first n-1 elements
  15900. for (auto i = val.m_data.m_value.array->cbegin();
  15901. i != val.m_data.m_value.array->cend() - 1; ++i)
  15902. {
  15903. dump(*i, false, ensure_ascii, indent_step, current_indent);
  15904. o->write_character(',');
  15905. }
  15906. // last element
  15907. JSON_ASSERT(!val.m_data.m_value.array->empty());
  15908. dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  15909. o->write_character(']');
  15910. }
  15911. return;
  15912. }
  15913. case value_t::string:
  15914. {
  15915. o->write_character('\"');
  15916. dump_escaped(*val.m_data.m_value.string, ensure_ascii);
  15917. o->write_character('\"');
  15918. return;
  15919. }
  15920. case value_t::binary:
  15921. {
  15922. if (pretty_print)
  15923. {
  15924. o->write_characters("{\n", 2);
  15925. // variable to hold indentation for recursive calls
  15926. const auto new_indent = current_indent + indent_step;
  15927. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15928. {
  15929. indent_string.resize(indent_string.size() * 2, ' ');
  15930. }
  15931. o->write_characters(indent_string.c_str(), new_indent);
  15932. o->write_characters("\"bytes\": [", 10);
  15933. if (!val.m_data.m_value.binary->empty())
  15934. {
  15935. for (auto i = val.m_data.m_value.binary->cbegin();
  15936. i != val.m_data.m_value.binary->cend() - 1; ++i)
  15937. {
  15938. dump_integer(*i);
  15939. o->write_characters(", ", 2);
  15940. }
  15941. dump_integer(val.m_data.m_value.binary->back());
  15942. }
  15943. o->write_characters("],\n", 3);
  15944. o->write_characters(indent_string.c_str(), new_indent);
  15945. o->write_characters("\"subtype\": ", 11);
  15946. if (val.m_data.m_value.binary->has_subtype())
  15947. {
  15948. dump_integer(val.m_data.m_value.binary->subtype());
  15949. }
  15950. else
  15951. {
  15952. o->write_characters("null", 4);
  15953. }
  15954. o->write_character('\n');
  15955. o->write_characters(indent_string.c_str(), current_indent);
  15956. o->write_character('}');
  15957. }
  15958. else
  15959. {
  15960. o->write_characters("{\"bytes\":[", 10);
  15961. if (!val.m_data.m_value.binary->empty())
  15962. {
  15963. for (auto i = val.m_data.m_value.binary->cbegin();
  15964. i != val.m_data.m_value.binary->cend() - 1; ++i)
  15965. {
  15966. dump_integer(*i);
  15967. o->write_character(',');
  15968. }
  15969. dump_integer(val.m_data.m_value.binary->back());
  15970. }
  15971. o->write_characters("],\"subtype\":", 12);
  15972. if (val.m_data.m_value.binary->has_subtype())
  15973. {
  15974. dump_integer(val.m_data.m_value.binary->subtype());
  15975. o->write_character('}');
  15976. }
  15977. else
  15978. {
  15979. o->write_characters("null}", 5);
  15980. }
  15981. }
  15982. return;
  15983. }
  15984. case value_t::boolean:
  15985. {
  15986. if (val.m_data.m_value.boolean)
  15987. {
  15988. o->write_characters("true", 4);
  15989. }
  15990. else
  15991. {
  15992. o->write_characters("false", 5);
  15993. }
  15994. return;
  15995. }
  15996. case value_t::number_integer:
  15997. {
  15998. dump_integer(val.m_data.m_value.number_integer);
  15999. return;
  16000. }
  16001. case value_t::number_unsigned:
  16002. {
  16003. dump_integer(val.m_data.m_value.number_unsigned);
  16004. return;
  16005. }
  16006. case value_t::number_float:
  16007. {
  16008. dump_float(val.m_data.m_value.number_float);
  16009. return;
  16010. }
  16011. case value_t::discarded:
  16012. {
  16013. o->write_characters("<discarded>", 11);
  16014. return;
  16015. }
  16016. case value_t::null:
  16017. {
  16018. o->write_characters("null", 4);
  16019. return;
  16020. }
  16021. default: // LCOV_EXCL_LINE
  16022. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16023. }
  16024. }
  16025. JSON_PRIVATE_UNLESS_TESTED:
  16026. /*!
  16027. @brief dump escaped string
  16028. Escape a string by replacing certain special characters by a sequence of an
  16029. escape character (backslash) and another character and other control
  16030. characters by a sequence of "\u" followed by a four-digit hex
  16031. representation. The escaped string is written to output stream @a o.
  16032. @param[in] s the string to escape
  16033. @param[in] ensure_ascii whether to escape non-ASCII characters with
  16034. \uXXXX sequences
  16035. @complexity Linear in the length of string @a s.
  16036. */
  16037. void dump_escaped(const string_t& s, const bool ensure_ascii)
  16038. {
  16039. std::uint32_t codepoint{};
  16040. std::uint8_t state = UTF8_ACCEPT;
  16041. std::size_t bytes = 0; // number of bytes written to string_buffer
  16042. // number of bytes written at the point of the last valid byte
  16043. std::size_t bytes_after_last_accept = 0;
  16044. std::size_t undumped_chars = 0;
  16045. for (std::size_t i = 0; i < s.size(); ++i)
  16046. {
  16047. const auto byte = static_cast<std::uint8_t>(s[i]);
  16048. switch (decode(state, codepoint, byte))
  16049. {
  16050. case UTF8_ACCEPT: // decode found a new code point
  16051. {
  16052. switch (codepoint)
  16053. {
  16054. case 0x08: // backspace
  16055. {
  16056. string_buffer[bytes++] = '\\';
  16057. string_buffer[bytes++] = 'b';
  16058. break;
  16059. }
  16060. case 0x09: // horizontal tab
  16061. {
  16062. string_buffer[bytes++] = '\\';
  16063. string_buffer[bytes++] = 't';
  16064. break;
  16065. }
  16066. case 0x0A: // newline
  16067. {
  16068. string_buffer[bytes++] = '\\';
  16069. string_buffer[bytes++] = 'n';
  16070. break;
  16071. }
  16072. case 0x0C: // formfeed
  16073. {
  16074. string_buffer[bytes++] = '\\';
  16075. string_buffer[bytes++] = 'f';
  16076. break;
  16077. }
  16078. case 0x0D: // carriage return
  16079. {
  16080. string_buffer[bytes++] = '\\';
  16081. string_buffer[bytes++] = 'r';
  16082. break;
  16083. }
  16084. case 0x22: // quotation mark
  16085. {
  16086. string_buffer[bytes++] = '\\';
  16087. string_buffer[bytes++] = '\"';
  16088. break;
  16089. }
  16090. case 0x5C: // reverse solidus
  16091. {
  16092. string_buffer[bytes++] = '\\';
  16093. string_buffer[bytes++] = '\\';
  16094. break;
  16095. }
  16096. default:
  16097. {
  16098. // escape control characters (0x00..0x1F) or, if
  16099. // ensure_ascii parameter is used, non-ASCII characters
  16100. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  16101. {
  16102. if (codepoint <= 0xFFFF)
  16103. {
  16104. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16105. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  16106. static_cast<std::uint16_t>(codepoint)));
  16107. bytes += 6;
  16108. }
  16109. else
  16110. {
  16111. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16112. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16113. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16114. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16115. bytes += 12;
  16116. }
  16117. }
  16118. else
  16119. {
  16120. // copy byte to buffer (all previous bytes
  16121. // been copied have in default case above)
  16122. string_buffer[bytes++] = s[i];
  16123. }
  16124. break;
  16125. }
  16126. }
  16127. // write buffer and reset index; there must be 13 bytes
  16128. // left, as this is the maximal number of bytes to be
  16129. // written ("\uxxxx\uxxxx\0") for one code point
  16130. if (string_buffer.size() - bytes < 13)
  16131. {
  16132. o->write_characters(string_buffer.data(), bytes);
  16133. bytes = 0;
  16134. }
  16135. // remember the byte position of this accept
  16136. bytes_after_last_accept = bytes;
  16137. undumped_chars = 0;
  16138. break;
  16139. }
  16140. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16141. {
  16142. switch (error_handler)
  16143. {
  16144. case error_handler_t::strict:
  16145. {
  16146. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16147. }
  16148. case error_handler_t::ignore:
  16149. case error_handler_t::replace:
  16150. {
  16151. // in case we saw this character the first time, we
  16152. // would like to read it again, because the byte
  16153. // may be OK for itself, but just not OK for the
  16154. // previous sequence
  16155. if (undumped_chars > 0)
  16156. {
  16157. --i;
  16158. }
  16159. // reset length buffer to the last accepted index;
  16160. // thus removing/ignoring the invalid characters
  16161. bytes = bytes_after_last_accept;
  16162. if (error_handler == error_handler_t::replace)
  16163. {
  16164. // add a replacement character
  16165. if (ensure_ascii)
  16166. {
  16167. string_buffer[bytes++] = '\\';
  16168. string_buffer[bytes++] = 'u';
  16169. string_buffer[bytes++] = 'f';
  16170. string_buffer[bytes++] = 'f';
  16171. string_buffer[bytes++] = 'f';
  16172. string_buffer[bytes++] = 'd';
  16173. }
  16174. else
  16175. {
  16176. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16177. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16178. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16179. }
  16180. // write buffer and reset index; there must be 13 bytes
  16181. // left, as this is the maximal number of bytes to be
  16182. // written ("\uxxxx\uxxxx\0") for one code point
  16183. if (string_buffer.size() - bytes < 13)
  16184. {
  16185. o->write_characters(string_buffer.data(), bytes);
  16186. bytes = 0;
  16187. }
  16188. bytes_after_last_accept = bytes;
  16189. }
  16190. undumped_chars = 0;
  16191. // continue processing the string
  16192. state = UTF8_ACCEPT;
  16193. break;
  16194. }
  16195. default: // LCOV_EXCL_LINE
  16196. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16197. }
  16198. break;
  16199. }
  16200. default: // decode found yet incomplete multi-byte code point
  16201. {
  16202. if (!ensure_ascii)
  16203. {
  16204. // code point will not be escaped - copy byte to buffer
  16205. string_buffer[bytes++] = s[i];
  16206. }
  16207. ++undumped_chars;
  16208. break;
  16209. }
  16210. }
  16211. }
  16212. // we finished processing the string
  16213. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  16214. {
  16215. // write buffer
  16216. if (bytes > 0)
  16217. {
  16218. o->write_characters(string_buffer.data(), bytes);
  16219. }
  16220. }
  16221. else
  16222. {
  16223. // we finish reading, but do not accept: string was incomplete
  16224. switch (error_handler)
  16225. {
  16226. case error_handler_t::strict:
  16227. {
  16228. 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));
  16229. }
  16230. case error_handler_t::ignore:
  16231. {
  16232. // write all accepted bytes
  16233. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16234. break;
  16235. }
  16236. case error_handler_t::replace:
  16237. {
  16238. // write all accepted bytes
  16239. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16240. // add a replacement character
  16241. if (ensure_ascii)
  16242. {
  16243. o->write_characters("\\ufffd", 6);
  16244. }
  16245. else
  16246. {
  16247. o->write_characters("\xEF\xBF\xBD", 3);
  16248. }
  16249. break;
  16250. }
  16251. default: // LCOV_EXCL_LINE
  16252. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16253. }
  16254. }
  16255. }
  16256. private:
  16257. /*!
  16258. @brief count digits
  16259. Count the number of decimal (base 10) digits for an input unsigned integer.
  16260. @param[in] x unsigned integer number to count its digits
  16261. @return number of decimal digits
  16262. */
  16263. inline unsigned int count_digits(number_unsigned_t x) noexcept
  16264. {
  16265. unsigned int n_digits = 1;
  16266. for (;;)
  16267. {
  16268. if (x < 10)
  16269. {
  16270. return n_digits;
  16271. }
  16272. if (x < 100)
  16273. {
  16274. return n_digits + 1;
  16275. }
  16276. if (x < 1000)
  16277. {
  16278. return n_digits + 2;
  16279. }
  16280. if (x < 10000)
  16281. {
  16282. return n_digits + 3;
  16283. }
  16284. x = x / 10000u;
  16285. n_digits += 4;
  16286. }
  16287. }
  16288. /*!
  16289. * @brief convert a byte to a uppercase hex representation
  16290. * @param[in] byte byte to represent
  16291. * @return representation ("00".."FF")
  16292. */
  16293. static std::string hex_bytes(std::uint8_t byte)
  16294. {
  16295. std::string result = "FF";
  16296. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  16297. result[0] = nibble_to_hex[byte / 16];
  16298. result[1] = nibble_to_hex[byte % 16];
  16299. return result;
  16300. }
  16301. // templates to avoid warnings about useless casts
  16302. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  16303. bool is_negative_number(NumberType x)
  16304. {
  16305. return x < 0;
  16306. }
  16307. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  16308. bool is_negative_number(NumberType /*unused*/)
  16309. {
  16310. return false;
  16311. }
  16312. /*!
  16313. @brief dump an integer
  16314. Dump a given integer to output stream @a o. Works internally with
  16315. @a number_buffer.
  16316. @param[in] x integer number (signed or unsigned) to dump
  16317. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  16318. */
  16319. template < typename NumberType, detail::enable_if_t <
  16320. std::is_integral<NumberType>::value ||
  16321. std::is_same<NumberType, number_unsigned_t>::value ||
  16322. std::is_same<NumberType, number_integer_t>::value ||
  16323. std::is_same<NumberType, binary_char_t>::value,
  16324. int > = 0 >
  16325. void dump_integer(NumberType x)
  16326. {
  16327. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  16328. {
  16329. {
  16330. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  16331. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  16332. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  16333. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  16334. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  16335. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  16336. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  16337. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  16338. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  16339. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  16340. }
  16341. };
  16342. // special case for "0"
  16343. if (x == 0)
  16344. {
  16345. o->write_character('0');
  16346. return;
  16347. }
  16348. // use a pointer to fill the buffer
  16349. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16350. number_unsigned_t abs_value;
  16351. unsigned int n_chars{};
  16352. if (is_negative_number(x))
  16353. {
  16354. *buffer_ptr = '-';
  16355. abs_value = remove_sign(static_cast<number_integer_t>(x));
  16356. // account one more byte for the minus sign
  16357. n_chars = 1 + count_digits(abs_value);
  16358. }
  16359. else
  16360. {
  16361. abs_value = static_cast<number_unsigned_t>(x);
  16362. n_chars = count_digits(abs_value);
  16363. }
  16364. // spare 1 byte for '\0'
  16365. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  16366. // jump to the end to generate the string from backward,
  16367. // so we later avoid reversing the result
  16368. buffer_ptr += n_chars;
  16369. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  16370. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  16371. while (abs_value >= 100)
  16372. {
  16373. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  16374. abs_value /= 100;
  16375. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16376. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16377. }
  16378. if (abs_value >= 10)
  16379. {
  16380. const auto digits_index = static_cast<unsigned>(abs_value);
  16381. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16382. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16383. }
  16384. else
  16385. {
  16386. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  16387. }
  16388. o->write_characters(number_buffer.data(), n_chars);
  16389. }
  16390. /*!
  16391. @brief dump a floating-point number
  16392. Dump a given floating-point number to output stream @a o. Works internally
  16393. with @a number_buffer.
  16394. @param[in] x floating-point number to dump
  16395. */
  16396. void dump_float(number_float_t x)
  16397. {
  16398. // NaN / inf
  16399. if (!std::isfinite(x))
  16400. {
  16401. o->write_characters("null", 4);
  16402. return;
  16403. }
  16404. // If number_float_t is an IEEE-754 single or double precision number,
  16405. // use the Grisu2 algorithm to produce short numbers which are
  16406. // guaranteed to round-trip, using strtof and strtod, resp.
  16407. //
  16408. // NB: The test below works if <long double> == <double>.
  16409. static constexpr bool is_ieee_single_or_double
  16410. = (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) ||
  16411. (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);
  16412. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  16413. }
  16414. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  16415. {
  16416. auto* begin = number_buffer.data();
  16417. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  16418. o->write_characters(begin, static_cast<size_t>(end - begin));
  16419. }
  16420. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  16421. {
  16422. // get number of digits for a float -> text -> float round-trip
  16423. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  16424. // the actual conversion
  16425. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16426. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  16427. // negative value indicates an error
  16428. JSON_ASSERT(len > 0);
  16429. // check if buffer was large enough
  16430. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  16431. // erase thousands separator
  16432. if (thousands_sep != '\0')
  16433. {
  16434. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16435. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  16436. std::fill(end, number_buffer.end(), '\0');
  16437. JSON_ASSERT((end - number_buffer.begin()) <= len);
  16438. len = (end - number_buffer.begin());
  16439. }
  16440. // convert decimal point to '.'
  16441. if (decimal_point != '\0' && decimal_point != '.')
  16442. {
  16443. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16444. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  16445. if (dec_pos != number_buffer.end())
  16446. {
  16447. *dec_pos = '.';
  16448. }
  16449. }
  16450. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  16451. // determine if we need to append ".0"
  16452. const bool value_is_int_like =
  16453. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  16454. [](char c)
  16455. {
  16456. return c == '.' || c == 'e';
  16457. });
  16458. if (value_is_int_like)
  16459. {
  16460. o->write_characters(".0", 2);
  16461. }
  16462. }
  16463. /*!
  16464. @brief check whether a string is UTF-8 encoded
  16465. The function checks each byte of a string whether it is UTF-8 encoded. The
  16466. result of the check is stored in the @a state parameter. The function must
  16467. be called initially with state 0 (accept). State 1 means the string must
  16468. be rejected, because the current byte is not allowed. If the string is
  16469. completely processed, but the state is non-zero, the string ended
  16470. prematurely; that is, the last byte indicated more bytes should have
  16471. followed.
  16472. @param[in,out] state the state of the decoding
  16473. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  16474. @param[in] byte next byte to decode
  16475. @return new state
  16476. @note The function has been edited: a std::array is used.
  16477. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  16478. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  16479. */
  16480. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  16481. {
  16482. static const std::array<std::uint8_t, 400> utf8d =
  16483. {
  16484. {
  16485. 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
  16486. 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
  16487. 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
  16488. 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
  16489. 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
  16490. 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
  16491. 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
  16492. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  16493. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  16494. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  16495. 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
  16496. 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
  16497. 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
  16498. 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
  16499. }
  16500. };
  16501. JSON_ASSERT(byte < utf8d.size());
  16502. const std::uint8_t type = utf8d[byte];
  16503. codep = (state != UTF8_ACCEPT)
  16504. ? (byte & 0x3fu) | (codep << 6u)
  16505. : (0xFFu >> type) & (byte);
  16506. const std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  16507. JSON_ASSERT(index < utf8d.size());
  16508. state = utf8d[index];
  16509. return state;
  16510. }
  16511. /*
  16512. * Overload to make the compiler happy while it is instantiating
  16513. * dump_integer for number_unsigned_t.
  16514. * Must never be called.
  16515. */
  16516. number_unsigned_t remove_sign(number_unsigned_t x)
  16517. {
  16518. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16519. return x; // LCOV_EXCL_LINE
  16520. }
  16521. /*
  16522. * Helper function for dump_integer
  16523. *
  16524. * This function takes a negative signed integer and returns its absolute
  16525. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  16526. * not directly remove the sign of an arbitrary signed integer as the
  16527. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  16528. * #1708 for details.
  16529. */
  16530. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  16531. {
  16532. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  16533. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  16534. }
  16535. private:
  16536. /// the output of the serializer
  16537. output_adapter_t<char> o = nullptr;
  16538. /// a (hopefully) large enough character buffer
  16539. std::array<char, 64> number_buffer{{}};
  16540. /// the locale
  16541. const std::lconv* loc = nullptr;
  16542. /// the locale's thousand separator character
  16543. const char thousands_sep = '\0';
  16544. /// the locale's decimal point character
  16545. const char decimal_point = '\0';
  16546. /// string buffer
  16547. std::array<char, 512> string_buffer{{}};
  16548. /// the indentation character
  16549. const char indent_char;
  16550. /// the indentation string
  16551. string_t indent_string;
  16552. /// error_handler how to react on decoding errors
  16553. const error_handler_t error_handler;
  16554. };
  16555. } // namespace detail
  16556. NLOHMANN_JSON_NAMESPACE_END
  16557. // #include <nlohmann/detail/value_t.hpp>
  16558. // #include <nlohmann/json_fwd.hpp>
  16559. // #include <nlohmann/ordered_map.hpp>
  16560. // __ _____ _____ _____
  16561. // __| | __| | | | JSON for Modern C++
  16562. // | | |__ | | | | | | version 3.11.3
  16563. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  16564. //
  16565. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  16566. // SPDX-License-Identifier: MIT
  16567. #include <functional> // equal_to, less
  16568. #include <initializer_list> // initializer_list
  16569. #include <iterator> // input_iterator_tag, iterator_traits
  16570. #include <memory> // allocator
  16571. #include <stdexcept> // for out_of_range
  16572. #include <type_traits> // enable_if, is_convertible
  16573. #include <utility> // pair
  16574. #include <vector> // vector
  16575. // #include <nlohmann/detail/macro_scope.hpp>
  16576. // #include <nlohmann/detail/meta/type_traits.hpp>
  16577. NLOHMANN_JSON_NAMESPACE_BEGIN
  16578. /// ordered_map: a minimal map-like container that preserves insertion order
  16579. /// for use within nlohmann::basic_json<ordered_map>
  16580. template <class Key, class T, class IgnoredLess = std::less<Key>,
  16581. class Allocator = std::allocator<std::pair<const Key, T>>>
  16582. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  16583. {
  16584. using key_type = Key;
  16585. using mapped_type = T;
  16586. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  16587. using iterator = typename Container::iterator;
  16588. using const_iterator = typename Container::const_iterator;
  16589. using size_type = typename Container::size_type;
  16590. using value_type = typename Container::value_type;
  16591. #ifdef JSON_HAS_CPP_14
  16592. using key_compare = std::equal_to<>;
  16593. #else
  16594. using key_compare = std::equal_to<Key>;
  16595. #endif
  16596. // Explicit constructors instead of `using Container::Container`
  16597. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  16598. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  16599. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  16600. template <class It>
  16601. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  16602. : Container{first, last, alloc} {}
  16603. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  16604. : Container{init, alloc} {}
  16605. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  16606. {
  16607. for (auto it = this->begin(); it != this->end(); ++it)
  16608. {
  16609. if (m_compare(it->first, key))
  16610. {
  16611. return {it, false};
  16612. }
  16613. }
  16614. Container::emplace_back(key, std::forward<T>(t));
  16615. return {std::prev(this->end()), true};
  16616. }
  16617. template<class KeyType, detail::enable_if_t<
  16618. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16619. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  16620. {
  16621. for (auto it = this->begin(); it != this->end(); ++it)
  16622. {
  16623. if (m_compare(it->first, key))
  16624. {
  16625. return {it, false};
  16626. }
  16627. }
  16628. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  16629. return {std::prev(this->end()), true};
  16630. }
  16631. T& operator[](const key_type& key)
  16632. {
  16633. return emplace(key, T{}).first->second;
  16634. }
  16635. template<class KeyType, detail::enable_if_t<
  16636. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16637. T & operator[](KeyType && key)
  16638. {
  16639. return emplace(std::forward<KeyType>(key), T{}).first->second;
  16640. }
  16641. const T& operator[](const key_type& key) const
  16642. {
  16643. return at(key);
  16644. }
  16645. template<class KeyType, detail::enable_if_t<
  16646. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16647. const T & operator[](KeyType && key) const
  16648. {
  16649. return at(std::forward<KeyType>(key));
  16650. }
  16651. T& at(const key_type& key)
  16652. {
  16653. for (auto it = this->begin(); it != this->end(); ++it)
  16654. {
  16655. if (m_compare(it->first, key))
  16656. {
  16657. return it->second;
  16658. }
  16659. }
  16660. JSON_THROW(std::out_of_range("key not found"));
  16661. }
  16662. template<class KeyType, detail::enable_if_t<
  16663. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16664. T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16665. {
  16666. for (auto it = this->begin(); it != this->end(); ++it)
  16667. {
  16668. if (m_compare(it->first, key))
  16669. {
  16670. return it->second;
  16671. }
  16672. }
  16673. JSON_THROW(std::out_of_range("key not found"));
  16674. }
  16675. const T& at(const key_type& key) const
  16676. {
  16677. for (auto it = this->begin(); it != this->end(); ++it)
  16678. {
  16679. if (m_compare(it->first, key))
  16680. {
  16681. return it->second;
  16682. }
  16683. }
  16684. JSON_THROW(std::out_of_range("key not found"));
  16685. }
  16686. template<class KeyType, detail::enable_if_t<
  16687. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16688. const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  16689. {
  16690. for (auto it = this->begin(); it != this->end(); ++it)
  16691. {
  16692. if (m_compare(it->first, key))
  16693. {
  16694. return it->second;
  16695. }
  16696. }
  16697. JSON_THROW(std::out_of_range("key not found"));
  16698. }
  16699. size_type erase(const key_type& key)
  16700. {
  16701. for (auto it = this->begin(); it != this->end(); ++it)
  16702. {
  16703. if (m_compare(it->first, key))
  16704. {
  16705. // Since we cannot move const Keys, re-construct them in place
  16706. for (auto next = it; ++next != this->end(); ++it)
  16707. {
  16708. it->~value_type(); // Destroy but keep allocation
  16709. new (&*it) value_type{std::move(*next)};
  16710. }
  16711. Container::pop_back();
  16712. return 1;
  16713. }
  16714. }
  16715. return 0;
  16716. }
  16717. template<class KeyType, detail::enable_if_t<
  16718. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16719. size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16720. {
  16721. for (auto it = this->begin(); it != this->end(); ++it)
  16722. {
  16723. if (m_compare(it->first, key))
  16724. {
  16725. // Since we cannot move const Keys, re-construct them in place
  16726. for (auto next = it; ++next != this->end(); ++it)
  16727. {
  16728. it->~value_type(); // Destroy but keep allocation
  16729. new (&*it) value_type{std::move(*next)};
  16730. }
  16731. Container::pop_back();
  16732. return 1;
  16733. }
  16734. }
  16735. return 0;
  16736. }
  16737. iterator erase(iterator pos)
  16738. {
  16739. return erase(pos, std::next(pos));
  16740. }
  16741. iterator erase(iterator first, iterator last)
  16742. {
  16743. if (first == last)
  16744. {
  16745. return first;
  16746. }
  16747. const auto elements_affected = std::distance(first, last);
  16748. const auto offset = std::distance(Container::begin(), first);
  16749. // This is the start situation. We need to delete elements_affected
  16750. // elements (3 in this example: e, f, g), and need to return an
  16751. // iterator past the last deleted element (h in this example).
  16752. // Note that offset is the distance from the start of the vector
  16753. // to first. We will need this later.
  16754. // [ a, b, c, d, e, f, g, h, i, j ]
  16755. // ^ ^
  16756. // first last
  16757. // Since we cannot move const Keys, we re-construct them in place.
  16758. // We start at first and re-construct (viz. copy) the elements from
  16759. // the back of the vector. Example for first iteration:
  16760. // ,--------.
  16761. // v | destroy e and re-construct with h
  16762. // [ a, b, c, d, e, f, g, h, i, j ]
  16763. // ^ ^
  16764. // it it + elements_affected
  16765. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  16766. {
  16767. it->~value_type(); // destroy but keep allocation
  16768. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  16769. }
  16770. // [ a, b, c, d, h, i, j, h, i, j ]
  16771. // ^ ^
  16772. // first last
  16773. // remove the unneeded elements at the end of the vector
  16774. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  16775. // [ a, b, c, d, h, i, j ]
  16776. // ^ ^
  16777. // first last
  16778. // first is now pointing past the last deleted element, but we cannot
  16779. // use this iterator, because it may have been invalidated by the
  16780. // resize call. Instead, we can return begin() + offset.
  16781. return Container::begin() + offset;
  16782. }
  16783. size_type count(const key_type& key) const
  16784. {
  16785. for (auto it = this->begin(); it != this->end(); ++it)
  16786. {
  16787. if (m_compare(it->first, key))
  16788. {
  16789. return 1;
  16790. }
  16791. }
  16792. return 0;
  16793. }
  16794. template<class KeyType, detail::enable_if_t<
  16795. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16796. size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  16797. {
  16798. for (auto it = this->begin(); it != this->end(); ++it)
  16799. {
  16800. if (m_compare(it->first, key))
  16801. {
  16802. return 1;
  16803. }
  16804. }
  16805. return 0;
  16806. }
  16807. iterator find(const key_type& key)
  16808. {
  16809. for (auto it = this->begin(); it != this->end(); ++it)
  16810. {
  16811. if (m_compare(it->first, key))
  16812. {
  16813. return it;
  16814. }
  16815. }
  16816. return Container::end();
  16817. }
  16818. template<class KeyType, detail::enable_if_t<
  16819. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16820. iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16821. {
  16822. for (auto it = this->begin(); it != this->end(); ++it)
  16823. {
  16824. if (m_compare(it->first, key))
  16825. {
  16826. return it;
  16827. }
  16828. }
  16829. return Container::end();
  16830. }
  16831. const_iterator find(const key_type& key) const
  16832. {
  16833. for (auto it = this->begin(); it != this->end(); ++it)
  16834. {
  16835. if (m_compare(it->first, key))
  16836. {
  16837. return it;
  16838. }
  16839. }
  16840. return Container::end();
  16841. }
  16842. std::pair<iterator, bool> insert( value_type&& value )
  16843. {
  16844. return emplace(value.first, std::move(value.second));
  16845. }
  16846. std::pair<iterator, bool> insert( const value_type& value )
  16847. {
  16848. for (auto it = this->begin(); it != this->end(); ++it)
  16849. {
  16850. if (m_compare(it->first, value.first))
  16851. {
  16852. return {it, false};
  16853. }
  16854. }
  16855. Container::push_back(value);
  16856. return {--this->end(), true};
  16857. }
  16858. template<typename InputIt>
  16859. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  16860. std::input_iterator_tag>::value>::type;
  16861. template<typename InputIt, typename = require_input_iter<InputIt>>
  16862. void insert(InputIt first, InputIt last)
  16863. {
  16864. for (auto it = first; it != last; ++it)
  16865. {
  16866. insert(*it);
  16867. }
  16868. }
  16869. private:
  16870. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  16871. };
  16872. NLOHMANN_JSON_NAMESPACE_END
  16873. #if defined(JSON_HAS_CPP_17)
  16874. #if JSON_HAS_STATIC_RTTI
  16875. #include <any>
  16876. #endif
  16877. #include <string_view>
  16878. #endif
  16879. /*!
  16880. @brief namespace for Niels Lohmann
  16881. @see https://github.com/nlohmann
  16882. @since version 1.0.0
  16883. */
  16884. NLOHMANN_JSON_NAMESPACE_BEGIN
  16885. /*!
  16886. @brief a class to store JSON values
  16887. @internal
  16888. @invariant The member variables @a m_value and @a m_type have the following
  16889. relationship:
  16890. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  16891. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  16892. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  16893. The invariants are checked by member function assert_invariant().
  16894. @note ObjectType trick from https://stackoverflow.com/a/9860911
  16895. @endinternal
  16896. @since version 1.0.0
  16897. @nosubgrouping
  16898. */
  16899. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  16900. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  16901. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  16902. {
  16903. private:
  16904. template<detail::value_t> friend struct detail::external_constructor;
  16905. template<typename>
  16906. friend class ::nlohmann::json_pointer;
  16907. // can be restored when json_pointer backwards compatibility is removed
  16908. // friend ::nlohmann::json_pointer<StringType>;
  16909. template<typename BasicJsonType, typename InputType>
  16910. friend class ::nlohmann::detail::parser;
  16911. friend ::nlohmann::detail::serializer<basic_json>;
  16912. template<typename BasicJsonType>
  16913. friend class ::nlohmann::detail::iter_impl;
  16914. template<typename BasicJsonType, typename CharType>
  16915. friend class ::nlohmann::detail::binary_writer;
  16916. template<typename BasicJsonType, typename InputType, typename SAX>
  16917. friend class ::nlohmann::detail::binary_reader;
  16918. template<typename BasicJsonType>
  16919. friend class ::nlohmann::detail::json_sax_dom_parser;
  16920. template<typename BasicJsonType>
  16921. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  16922. friend class ::nlohmann::detail::exception;
  16923. /// workaround type for MSVC
  16924. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  16925. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  16926. JSON_PRIVATE_UNLESS_TESTED:
  16927. // convenience aliases for types residing in namespace detail;
  16928. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  16929. template<typename InputAdapterType>
  16930. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  16931. InputAdapterType adapter,
  16932. detail::parser_callback_t<basic_json>cb = nullptr,
  16933. const bool allow_exceptions = true,
  16934. const bool ignore_comments = false
  16935. )
  16936. {
  16937. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  16938. std::move(cb), allow_exceptions, ignore_comments);
  16939. }
  16940. private:
  16941. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  16942. template<typename BasicJsonType>
  16943. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  16944. template<typename BasicJsonType>
  16945. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  16946. template<typename Iterator>
  16947. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  16948. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  16949. template<typename CharType>
  16950. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  16951. template<typename InputType>
  16952. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  16953. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  16954. JSON_PRIVATE_UNLESS_TESTED:
  16955. using serializer = ::nlohmann::detail::serializer<basic_json>;
  16956. public:
  16957. using value_t = detail::value_t;
  16958. /// JSON Pointer, see @ref nlohmann::json_pointer
  16959. using json_pointer = ::nlohmann::json_pointer<StringType>;
  16960. template<typename T, typename SFINAE>
  16961. using json_serializer = JSONSerializer<T, SFINAE>;
  16962. /// how to treat decoding errors
  16963. using error_handler_t = detail::error_handler_t;
  16964. /// how to treat CBOR tags
  16965. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  16966. /// helper type for initializer lists of basic_json values
  16967. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  16968. using input_format_t = detail::input_format_t;
  16969. /// SAX interface type, see @ref nlohmann::json_sax
  16970. using json_sax_t = json_sax<basic_json>;
  16971. ////////////////
  16972. // exceptions //
  16973. ////////////////
  16974. /// @name exceptions
  16975. /// Classes to implement user-defined exceptions.
  16976. /// @{
  16977. using exception = detail::exception;
  16978. using parse_error = detail::parse_error;
  16979. using invalid_iterator = detail::invalid_iterator;
  16980. using type_error = detail::type_error;
  16981. using out_of_range = detail::out_of_range;
  16982. using other_error = detail::other_error;
  16983. /// @}
  16984. /////////////////////
  16985. // container types //
  16986. /////////////////////
  16987. /// @name container types
  16988. /// The canonic container types to use @ref basic_json like any other STL
  16989. /// container.
  16990. /// @{
  16991. /// the type of elements in a basic_json container
  16992. using value_type = basic_json;
  16993. /// the type of an element reference
  16994. using reference = value_type&;
  16995. /// the type of an element const reference
  16996. using const_reference = const value_type&;
  16997. /// a type to represent differences between iterators
  16998. using difference_type = std::ptrdiff_t;
  16999. /// a type to represent container sizes
  17000. using size_type = std::size_t;
  17001. /// the allocator type
  17002. using allocator_type = AllocatorType<basic_json>;
  17003. /// the type of an element pointer
  17004. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  17005. /// the type of an element const pointer
  17006. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  17007. /// an iterator for a basic_json container
  17008. using iterator = iter_impl<basic_json>;
  17009. /// a const iterator for a basic_json container
  17010. using const_iterator = iter_impl<const basic_json>;
  17011. /// a reverse iterator for a basic_json container
  17012. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  17013. /// a const reverse iterator for a basic_json container
  17014. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  17015. /// @}
  17016. /// @brief returns the allocator associated with the container
  17017. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  17018. static allocator_type get_allocator()
  17019. {
  17020. return allocator_type();
  17021. }
  17022. /// @brief returns version information on the library
  17023. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  17024. JSON_HEDLEY_WARN_UNUSED_RESULT
  17025. static basic_json meta()
  17026. {
  17027. basic_json result;
  17028. result["copyright"] = "(C) 2013-2023 Niels Lohmann";
  17029. result["name"] = "JSON for Modern C++";
  17030. result["url"] = "https://github.com/nlohmann/json";
  17031. result["version"]["string"] =
  17032. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  17033. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  17034. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  17035. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  17036. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  17037. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  17038. #ifdef _WIN32
  17039. result["platform"] = "win32";
  17040. #elif defined __linux__
  17041. result["platform"] = "linux";
  17042. #elif defined __APPLE__
  17043. result["platform"] = "apple";
  17044. #elif defined __unix__
  17045. result["platform"] = "unix";
  17046. #else
  17047. result["platform"] = "unknown";
  17048. #endif
  17049. #if defined(__ICC) || defined(__INTEL_COMPILER)
  17050. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  17051. #elif defined(__clang__)
  17052. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  17053. #elif defined(__GNUC__) || defined(__GNUG__)
  17054. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  17055. std::to_string(__GNUC__), '.',
  17056. std::to_string(__GNUC_MINOR__), '.',
  17057. std::to_string(__GNUC_PATCHLEVEL__))
  17058. }
  17059. };
  17060. #elif defined(__HP_cc) || defined(__HP_aCC)
  17061. result["compiler"] = "hp"
  17062. #elif defined(__IBMCPP__)
  17063. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  17064. #elif defined(_MSC_VER)
  17065. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  17066. #elif defined(__PGI)
  17067. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  17068. #elif defined(__SUNPRO_CC)
  17069. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  17070. #else
  17071. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  17072. #endif
  17073. #if defined(_MSVC_LANG)
  17074. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  17075. #elif defined(__cplusplus)
  17076. result["compiler"]["c++"] = std::to_string(__cplusplus);
  17077. #else
  17078. result["compiler"]["c++"] = "unknown";
  17079. #endif
  17080. return result;
  17081. }
  17082. ///////////////////////////
  17083. // JSON value data types //
  17084. ///////////////////////////
  17085. /// @name JSON value data types
  17086. /// The data types to store a JSON value. These types are derived from
  17087. /// the template arguments passed to class @ref basic_json.
  17088. /// @{
  17089. /// @brief default object key comparator type
  17090. /// The actual object key comparator type (@ref object_comparator_t) may be
  17091. /// different.
  17092. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  17093. #if defined(JSON_HAS_CPP_14)
  17094. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  17095. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  17096. using default_object_comparator_t = std::less<>;
  17097. #else
  17098. using default_object_comparator_t = std::less<StringType>;
  17099. #endif
  17100. /// @brief a type for an object
  17101. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  17102. using object_t = ObjectType<StringType,
  17103. basic_json,
  17104. default_object_comparator_t,
  17105. AllocatorType<std::pair<const StringType,
  17106. basic_json>>>;
  17107. /// @brief a type for an array
  17108. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  17109. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  17110. /// @brief a type for a string
  17111. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  17112. using string_t = StringType;
  17113. /// @brief a type for a boolean
  17114. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17115. using boolean_t = BooleanType;
  17116. /// @brief a type for a number (integer)
  17117. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17118. using number_integer_t = NumberIntegerType;
  17119. /// @brief a type for a number (unsigned)
  17120. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17121. using number_unsigned_t = NumberUnsignedType;
  17122. /// @brief a type for a number (floating-point)
  17123. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17124. using number_float_t = NumberFloatType;
  17125. /// @brief a type for a packed binary type
  17126. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17127. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17128. /// @brief object key comparator type
  17129. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17130. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17131. /// @}
  17132. private:
  17133. /// helper for exception-safe object creation
  17134. template<typename T, typename... Args>
  17135. JSON_HEDLEY_RETURNS_NON_NULL
  17136. static T* create(Args&& ... args)
  17137. {
  17138. AllocatorType<T> alloc;
  17139. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17140. auto deleter = [&](T * obj)
  17141. {
  17142. AllocatorTraits::deallocate(alloc, obj, 1);
  17143. };
  17144. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17145. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17146. JSON_ASSERT(obj != nullptr);
  17147. return obj.release();
  17148. }
  17149. ////////////////////////
  17150. // JSON value storage //
  17151. ////////////////////////
  17152. JSON_PRIVATE_UNLESS_TESTED:
  17153. /*!
  17154. @brief a JSON value
  17155. The actual storage for a JSON value of the @ref basic_json class. This
  17156. union combines the different storage types for the JSON value types
  17157. defined in @ref value_t.
  17158. JSON type | value_t type | used type
  17159. --------- | --------------- | ------------------------
  17160. object | object | pointer to @ref object_t
  17161. array | array | pointer to @ref array_t
  17162. string | string | pointer to @ref string_t
  17163. boolean | boolean | @ref boolean_t
  17164. number | number_integer | @ref number_integer_t
  17165. number | number_unsigned | @ref number_unsigned_t
  17166. number | number_float | @ref number_float_t
  17167. binary | binary | pointer to @ref binary_t
  17168. null | null | *no value is stored*
  17169. @note Variable-length types (objects, arrays, and strings) are stored as
  17170. pointers. The size of the union should not exceed 64 bits if the default
  17171. value types are used.
  17172. @since version 1.0.0
  17173. */
  17174. union json_value
  17175. {
  17176. /// object (stored with pointer to save storage)
  17177. object_t* object;
  17178. /// array (stored with pointer to save storage)
  17179. array_t* array;
  17180. /// string (stored with pointer to save storage)
  17181. string_t* string;
  17182. /// binary (stored with pointer to save storage)
  17183. binary_t* binary;
  17184. /// boolean
  17185. boolean_t boolean;
  17186. /// number (integer)
  17187. number_integer_t number_integer;
  17188. /// number (unsigned integer)
  17189. number_unsigned_t number_unsigned;
  17190. /// number (floating-point)
  17191. number_float_t number_float;
  17192. /// default constructor (for null values)
  17193. json_value() = default;
  17194. /// constructor for booleans
  17195. json_value(boolean_t v) noexcept : boolean(v) {}
  17196. /// constructor for numbers (integer)
  17197. json_value(number_integer_t v) noexcept : number_integer(v) {}
  17198. /// constructor for numbers (unsigned)
  17199. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  17200. /// constructor for numbers (floating-point)
  17201. json_value(number_float_t v) noexcept : number_float(v) {}
  17202. /// constructor for empty values of a given type
  17203. json_value(value_t t)
  17204. {
  17205. switch (t)
  17206. {
  17207. case value_t::object:
  17208. {
  17209. object = create<object_t>();
  17210. break;
  17211. }
  17212. case value_t::array:
  17213. {
  17214. array = create<array_t>();
  17215. break;
  17216. }
  17217. case value_t::string:
  17218. {
  17219. string = create<string_t>("");
  17220. break;
  17221. }
  17222. case value_t::binary:
  17223. {
  17224. binary = create<binary_t>();
  17225. break;
  17226. }
  17227. case value_t::boolean:
  17228. {
  17229. boolean = static_cast<boolean_t>(false);
  17230. break;
  17231. }
  17232. case value_t::number_integer:
  17233. {
  17234. number_integer = static_cast<number_integer_t>(0);
  17235. break;
  17236. }
  17237. case value_t::number_unsigned:
  17238. {
  17239. number_unsigned = static_cast<number_unsigned_t>(0);
  17240. break;
  17241. }
  17242. case value_t::number_float:
  17243. {
  17244. number_float = static_cast<number_float_t>(0.0);
  17245. break;
  17246. }
  17247. case value_t::null:
  17248. {
  17249. object = nullptr; // silence warning, see #821
  17250. break;
  17251. }
  17252. case value_t::discarded:
  17253. default:
  17254. {
  17255. object = nullptr; // silence warning, see #821
  17256. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  17257. {
  17258. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.11.3", nullptr)); // LCOV_EXCL_LINE
  17259. }
  17260. break;
  17261. }
  17262. }
  17263. }
  17264. /// constructor for strings
  17265. json_value(const string_t& value) : string(create<string_t>(value)) {}
  17266. /// constructor for rvalue strings
  17267. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  17268. /// constructor for objects
  17269. json_value(const object_t& value) : object(create<object_t>(value)) {}
  17270. /// constructor for rvalue objects
  17271. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  17272. /// constructor for arrays
  17273. json_value(const array_t& value) : array(create<array_t>(value)) {}
  17274. /// constructor for rvalue arrays
  17275. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  17276. /// constructor for binary arrays
  17277. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  17278. /// constructor for rvalue binary arrays
  17279. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  17280. /// constructor for binary arrays (internal type)
  17281. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  17282. /// constructor for rvalue binary arrays (internal type)
  17283. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  17284. void destroy(value_t t)
  17285. {
  17286. if (
  17287. (t == value_t::object && object == nullptr) ||
  17288. (t == value_t::array && array == nullptr) ||
  17289. (t == value_t::string && string == nullptr) ||
  17290. (t == value_t::binary && binary == nullptr)
  17291. )
  17292. {
  17293. //not initialized (e.g. due to exception in the ctor)
  17294. return;
  17295. }
  17296. if (t == value_t::array || t == value_t::object)
  17297. {
  17298. // flatten the current json_value to a heap-allocated stack
  17299. std::vector<basic_json> stack;
  17300. // move the top-level items to stack
  17301. if (t == value_t::array)
  17302. {
  17303. stack.reserve(array->size());
  17304. std::move(array->begin(), array->end(), std::back_inserter(stack));
  17305. }
  17306. else
  17307. {
  17308. stack.reserve(object->size());
  17309. for (auto&& it : *object)
  17310. {
  17311. stack.push_back(std::move(it.second));
  17312. }
  17313. }
  17314. while (!stack.empty())
  17315. {
  17316. // move the last item to local variable to be processed
  17317. basic_json current_item(std::move(stack.back()));
  17318. stack.pop_back();
  17319. // if current_item is array/object, move
  17320. // its children to the stack to be processed later
  17321. if (current_item.is_array())
  17322. {
  17323. std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
  17324. current_item.m_data.m_value.array->clear();
  17325. }
  17326. else if (current_item.is_object())
  17327. {
  17328. for (auto&& it : *current_item.m_data.m_value.object)
  17329. {
  17330. stack.push_back(std::move(it.second));
  17331. }
  17332. current_item.m_data.m_value.object->clear();
  17333. }
  17334. // it's now safe that current_item get destructed
  17335. // since it doesn't have any children
  17336. }
  17337. }
  17338. switch (t)
  17339. {
  17340. case value_t::object:
  17341. {
  17342. AllocatorType<object_t> alloc;
  17343. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  17344. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  17345. break;
  17346. }
  17347. case value_t::array:
  17348. {
  17349. AllocatorType<array_t> alloc;
  17350. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  17351. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  17352. break;
  17353. }
  17354. case value_t::string:
  17355. {
  17356. AllocatorType<string_t> alloc;
  17357. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  17358. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  17359. break;
  17360. }
  17361. case value_t::binary:
  17362. {
  17363. AllocatorType<binary_t> alloc;
  17364. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  17365. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  17366. break;
  17367. }
  17368. case value_t::null:
  17369. case value_t::boolean:
  17370. case value_t::number_integer:
  17371. case value_t::number_unsigned:
  17372. case value_t::number_float:
  17373. case value_t::discarded:
  17374. default:
  17375. {
  17376. break;
  17377. }
  17378. }
  17379. }
  17380. };
  17381. private:
  17382. /*!
  17383. @brief checks the class invariants
  17384. This function asserts the class invariants. It needs to be called at the
  17385. end of every constructor to make sure that created objects respect the
  17386. invariant. Furthermore, it has to be called each time the type of a JSON
  17387. value is changed, because the invariant expresses a relationship between
  17388. @a m_type and @a m_value.
  17389. Furthermore, the parent relation is checked for arrays and objects: If
  17390. @a check_parents true and the value is an array or object, then the
  17391. container's elements must have the current value as parent.
  17392. @param[in] check_parents whether the parent relation should be checked.
  17393. The value is true by default and should only be set to false
  17394. during destruction of objects when the invariant does not
  17395. need to hold.
  17396. */
  17397. void assert_invariant(bool check_parents = true) const noexcept
  17398. {
  17399. JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
  17400. JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
  17401. JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
  17402. JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
  17403. #if JSON_DIAGNOSTICS
  17404. JSON_TRY
  17405. {
  17406. // cppcheck-suppress assertWithSideEffect
  17407. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  17408. {
  17409. return j.m_parent == this;
  17410. }));
  17411. }
  17412. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  17413. #endif
  17414. static_cast<void>(check_parents);
  17415. }
  17416. void set_parents()
  17417. {
  17418. #if JSON_DIAGNOSTICS
  17419. switch (m_data.m_type)
  17420. {
  17421. case value_t::array:
  17422. {
  17423. for (auto& element : *m_data.m_value.array)
  17424. {
  17425. element.m_parent = this;
  17426. }
  17427. break;
  17428. }
  17429. case value_t::object:
  17430. {
  17431. for (auto& element : *m_data.m_value.object)
  17432. {
  17433. element.second.m_parent = this;
  17434. }
  17435. break;
  17436. }
  17437. case value_t::null:
  17438. case value_t::string:
  17439. case value_t::boolean:
  17440. case value_t::number_integer:
  17441. case value_t::number_unsigned:
  17442. case value_t::number_float:
  17443. case value_t::binary:
  17444. case value_t::discarded:
  17445. default:
  17446. break;
  17447. }
  17448. #endif
  17449. }
  17450. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  17451. {
  17452. #if JSON_DIAGNOSTICS
  17453. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  17454. {
  17455. (it + i)->m_parent = this;
  17456. }
  17457. #else
  17458. static_cast<void>(count_set_parents);
  17459. #endif
  17460. return it;
  17461. }
  17462. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  17463. {
  17464. #if JSON_DIAGNOSTICS
  17465. if (old_capacity != static_cast<std::size_t>(-1))
  17466. {
  17467. // see https://github.com/nlohmann/json/issues/2838
  17468. JSON_ASSERT(type() == value_t::array);
  17469. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  17470. {
  17471. // capacity has changed: update all parents
  17472. set_parents();
  17473. return j;
  17474. }
  17475. }
  17476. // ordered_json uses a vector internally, so pointers could have
  17477. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  17478. #ifdef JSON_HEDLEY_MSVC_VERSION
  17479. #pragma warning(push )
  17480. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  17481. #endif
  17482. if (detail::is_ordered_map<object_t>::value)
  17483. {
  17484. set_parents();
  17485. return j;
  17486. }
  17487. #ifdef JSON_HEDLEY_MSVC_VERSION
  17488. #pragma warning( pop )
  17489. #endif
  17490. j.m_parent = this;
  17491. #else
  17492. static_cast<void>(j);
  17493. static_cast<void>(old_capacity);
  17494. #endif
  17495. return j;
  17496. }
  17497. public:
  17498. //////////////////////////
  17499. // JSON parser callback //
  17500. //////////////////////////
  17501. /// @brief parser event types
  17502. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  17503. using parse_event_t = detail::parse_event_t;
  17504. /// @brief per-element parser callback type
  17505. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  17506. using parser_callback_t = detail::parser_callback_t<basic_json>;
  17507. //////////////////
  17508. // constructors //
  17509. //////////////////
  17510. /// @name constructors and destructors
  17511. /// Constructors of class @ref basic_json, copy/move constructor, copy
  17512. /// assignment, static functions creating objects, and the destructor.
  17513. /// @{
  17514. /// @brief create an empty value with a given type
  17515. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17516. basic_json(const value_t v)
  17517. : m_data(v)
  17518. {
  17519. assert_invariant();
  17520. }
  17521. /// @brief create a null object
  17522. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17523. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  17524. : basic_json(value_t::null)
  17525. {
  17526. assert_invariant();
  17527. }
  17528. /// @brief create a JSON value from compatible types
  17529. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17530. template < typename CompatibleType,
  17531. typename U = detail::uncvref_t<CompatibleType>,
  17532. detail::enable_if_t <
  17533. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  17534. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  17535. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  17536. std::forward<CompatibleType>(val))))
  17537. {
  17538. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  17539. set_parents();
  17540. assert_invariant();
  17541. }
  17542. /// @brief create a JSON value from an existing one
  17543. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17544. template < typename BasicJsonType,
  17545. detail::enable_if_t <
  17546. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  17547. basic_json(const BasicJsonType& val)
  17548. {
  17549. using other_boolean_t = typename BasicJsonType::boolean_t;
  17550. using other_number_float_t = typename BasicJsonType::number_float_t;
  17551. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  17552. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  17553. using other_string_t = typename BasicJsonType::string_t;
  17554. using other_object_t = typename BasicJsonType::object_t;
  17555. using other_array_t = typename BasicJsonType::array_t;
  17556. using other_binary_t = typename BasicJsonType::binary_t;
  17557. switch (val.type())
  17558. {
  17559. case value_t::boolean:
  17560. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  17561. break;
  17562. case value_t::number_float:
  17563. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  17564. break;
  17565. case value_t::number_integer:
  17566. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  17567. break;
  17568. case value_t::number_unsigned:
  17569. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  17570. break;
  17571. case value_t::string:
  17572. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  17573. break;
  17574. case value_t::object:
  17575. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  17576. break;
  17577. case value_t::array:
  17578. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  17579. break;
  17580. case value_t::binary:
  17581. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  17582. break;
  17583. case value_t::null:
  17584. *this = nullptr;
  17585. break;
  17586. case value_t::discarded:
  17587. m_data.m_type = value_t::discarded;
  17588. break;
  17589. default: // LCOV_EXCL_LINE
  17590. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17591. }
  17592. JSON_ASSERT(m_data.m_type == val.type());
  17593. set_parents();
  17594. assert_invariant();
  17595. }
  17596. /// @brief create a container (array or object) from an initializer list
  17597. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17598. basic_json(initializer_list_t init,
  17599. bool type_deduction = true,
  17600. value_t manual_type = value_t::array)
  17601. {
  17602. // check if each element is an array with two elements whose first
  17603. // element is a string
  17604. bool is_an_object = std::all_of(init.begin(), init.end(),
  17605. [](const detail::json_ref<basic_json>& element_ref)
  17606. {
  17607. // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
  17608. // (many string types can be constructed from 0 via its null-pointer guise, so we get a
  17609. // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
  17610. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
  17611. });
  17612. // adjust type if type deduction is not wanted
  17613. if (!type_deduction)
  17614. {
  17615. // if array is wanted, do not create an object though possible
  17616. if (manual_type == value_t::array)
  17617. {
  17618. is_an_object = false;
  17619. }
  17620. // if object is wanted but impossible, throw an exception
  17621. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  17622. {
  17623. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  17624. }
  17625. }
  17626. if (is_an_object)
  17627. {
  17628. // the initializer list is a list of pairs -> create object
  17629. m_data.m_type = value_t::object;
  17630. m_data.m_value = value_t::object;
  17631. for (auto& element_ref : init)
  17632. {
  17633. auto element = element_ref.moved_or_copied();
  17634. m_data.m_value.object->emplace(
  17635. std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
  17636. std::move((*element.m_data.m_value.array)[1]));
  17637. }
  17638. }
  17639. else
  17640. {
  17641. // the initializer list describes an array -> create array
  17642. m_data.m_type = value_t::array;
  17643. m_data.m_value.array = create<array_t>(init.begin(), init.end());
  17644. }
  17645. set_parents();
  17646. assert_invariant();
  17647. }
  17648. /// @brief explicitly create a binary array (without subtype)
  17649. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17650. JSON_HEDLEY_WARN_UNUSED_RESULT
  17651. static basic_json binary(const typename binary_t::container_type& init)
  17652. {
  17653. auto res = basic_json();
  17654. res.m_data.m_type = value_t::binary;
  17655. res.m_data.m_value = init;
  17656. return res;
  17657. }
  17658. /// @brief explicitly create a binary array (with subtype)
  17659. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17660. JSON_HEDLEY_WARN_UNUSED_RESULT
  17661. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  17662. {
  17663. auto res = basic_json();
  17664. res.m_data.m_type = value_t::binary;
  17665. res.m_data.m_value = binary_t(init, subtype);
  17666. return res;
  17667. }
  17668. /// @brief explicitly create a binary array
  17669. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17670. JSON_HEDLEY_WARN_UNUSED_RESULT
  17671. static basic_json binary(typename binary_t::container_type&& init)
  17672. {
  17673. auto res = basic_json();
  17674. res.m_data.m_type = value_t::binary;
  17675. res.m_data.m_value = std::move(init);
  17676. return res;
  17677. }
  17678. /// @brief explicitly create a binary array (with subtype)
  17679. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17680. JSON_HEDLEY_WARN_UNUSED_RESULT
  17681. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  17682. {
  17683. auto res = basic_json();
  17684. res.m_data.m_type = value_t::binary;
  17685. res.m_data.m_value = binary_t(std::move(init), subtype);
  17686. return res;
  17687. }
  17688. /// @brief explicitly create an array from an initializer list
  17689. /// @sa https://json.nlohmann.me/api/basic_json/array/
  17690. JSON_HEDLEY_WARN_UNUSED_RESULT
  17691. static basic_json array(initializer_list_t init = {})
  17692. {
  17693. return basic_json(init, false, value_t::array);
  17694. }
  17695. /// @brief explicitly create an object from an initializer list
  17696. /// @sa https://json.nlohmann.me/api/basic_json/object/
  17697. JSON_HEDLEY_WARN_UNUSED_RESULT
  17698. static basic_json object(initializer_list_t init = {})
  17699. {
  17700. return basic_json(init, false, value_t::object);
  17701. }
  17702. /// @brief construct an array with count copies of given value
  17703. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17704. basic_json(size_type cnt, const basic_json& val):
  17705. m_data{cnt, val}
  17706. {
  17707. set_parents();
  17708. assert_invariant();
  17709. }
  17710. /// @brief construct a JSON container given an iterator range
  17711. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17712. template < class InputIT, typename std::enable_if <
  17713. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  17714. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  17715. basic_json(InputIT first, InputIT last)
  17716. {
  17717. JSON_ASSERT(first.m_object != nullptr);
  17718. JSON_ASSERT(last.m_object != nullptr);
  17719. // make sure iterator fits the current value
  17720. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  17721. {
  17722. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  17723. }
  17724. // copy type from first iterator
  17725. m_data.m_type = first.m_object->m_data.m_type;
  17726. // check if iterator range is complete for primitive values
  17727. switch (m_data.m_type)
  17728. {
  17729. case value_t::boolean:
  17730. case value_t::number_float:
  17731. case value_t::number_integer:
  17732. case value_t::number_unsigned:
  17733. case value_t::string:
  17734. {
  17735. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  17736. || !last.m_it.primitive_iterator.is_end()))
  17737. {
  17738. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  17739. }
  17740. break;
  17741. }
  17742. case value_t::null:
  17743. case value_t::object:
  17744. case value_t::array:
  17745. case value_t::binary:
  17746. case value_t::discarded:
  17747. default:
  17748. break;
  17749. }
  17750. switch (m_data.m_type)
  17751. {
  17752. case value_t::number_integer:
  17753. {
  17754. m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
  17755. break;
  17756. }
  17757. case value_t::number_unsigned:
  17758. {
  17759. m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
  17760. break;
  17761. }
  17762. case value_t::number_float:
  17763. {
  17764. m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
  17765. break;
  17766. }
  17767. case value_t::boolean:
  17768. {
  17769. m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
  17770. break;
  17771. }
  17772. case value_t::string:
  17773. {
  17774. m_data.m_value = *first.m_object->m_data.m_value.string;
  17775. break;
  17776. }
  17777. case value_t::object:
  17778. {
  17779. m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
  17780. last.m_it.object_iterator);
  17781. break;
  17782. }
  17783. case value_t::array:
  17784. {
  17785. m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
  17786. last.m_it.array_iterator);
  17787. break;
  17788. }
  17789. case value_t::binary:
  17790. {
  17791. m_data.m_value = *first.m_object->m_data.m_value.binary;
  17792. break;
  17793. }
  17794. case value_t::null:
  17795. case value_t::discarded:
  17796. default:
  17797. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  17798. }
  17799. set_parents();
  17800. assert_invariant();
  17801. }
  17802. ///////////////////////////////////////
  17803. // other constructors and destructor //
  17804. ///////////////////////////////////////
  17805. template<typename JsonRef,
  17806. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  17807. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  17808. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  17809. /// @brief copy constructor
  17810. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17811. basic_json(const basic_json& other)
  17812. : json_base_class_t(other)
  17813. {
  17814. m_data.m_type = other.m_data.m_type;
  17815. // check of passed value is valid
  17816. other.assert_invariant();
  17817. switch (m_data.m_type)
  17818. {
  17819. case value_t::object:
  17820. {
  17821. m_data.m_value = *other.m_data.m_value.object;
  17822. break;
  17823. }
  17824. case value_t::array:
  17825. {
  17826. m_data.m_value = *other.m_data.m_value.array;
  17827. break;
  17828. }
  17829. case value_t::string:
  17830. {
  17831. m_data.m_value = *other.m_data.m_value.string;
  17832. break;
  17833. }
  17834. case value_t::boolean:
  17835. {
  17836. m_data.m_value = other.m_data.m_value.boolean;
  17837. break;
  17838. }
  17839. case value_t::number_integer:
  17840. {
  17841. m_data.m_value = other.m_data.m_value.number_integer;
  17842. break;
  17843. }
  17844. case value_t::number_unsigned:
  17845. {
  17846. m_data.m_value = other.m_data.m_value.number_unsigned;
  17847. break;
  17848. }
  17849. case value_t::number_float:
  17850. {
  17851. m_data.m_value = other.m_data.m_value.number_float;
  17852. break;
  17853. }
  17854. case value_t::binary:
  17855. {
  17856. m_data.m_value = *other.m_data.m_value.binary;
  17857. break;
  17858. }
  17859. case value_t::null:
  17860. case value_t::discarded:
  17861. default:
  17862. break;
  17863. }
  17864. set_parents();
  17865. assert_invariant();
  17866. }
  17867. /// @brief move constructor
  17868. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17869. basic_json(basic_json&& other) noexcept
  17870. : json_base_class_t(std::forward<json_base_class_t>(other)),
  17871. m_data(std::move(other.m_data))
  17872. {
  17873. // check that passed value is valid
  17874. other.assert_invariant(false);
  17875. // invalidate payload
  17876. other.m_data.m_type = value_t::null;
  17877. other.m_data.m_value = {};
  17878. set_parents();
  17879. assert_invariant();
  17880. }
  17881. /// @brief copy assignment
  17882. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  17883. basic_json& operator=(basic_json other) noexcept (
  17884. std::is_nothrow_move_constructible<value_t>::value&&
  17885. std::is_nothrow_move_assignable<value_t>::value&&
  17886. std::is_nothrow_move_constructible<json_value>::value&&
  17887. std::is_nothrow_move_assignable<json_value>::value&&
  17888. std::is_nothrow_move_assignable<json_base_class_t>::value
  17889. )
  17890. {
  17891. // check that passed value is valid
  17892. other.assert_invariant();
  17893. using std::swap;
  17894. swap(m_data.m_type, other.m_data.m_type);
  17895. swap(m_data.m_value, other.m_data.m_value);
  17896. json_base_class_t::operator=(std::move(other));
  17897. set_parents();
  17898. assert_invariant();
  17899. return *this;
  17900. }
  17901. /// @brief destructor
  17902. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  17903. ~basic_json() noexcept
  17904. {
  17905. assert_invariant(false);
  17906. }
  17907. /// @}
  17908. public:
  17909. ///////////////////////
  17910. // object inspection //
  17911. ///////////////////////
  17912. /// @name object inspection
  17913. /// Functions to inspect the type of a JSON value.
  17914. /// @{
  17915. /// @brief serialization
  17916. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  17917. string_t dump(const int indent = -1,
  17918. const char indent_char = ' ',
  17919. const bool ensure_ascii = false,
  17920. const error_handler_t error_handler = error_handler_t::strict) const
  17921. {
  17922. string_t result;
  17923. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17924. if (indent >= 0)
  17925. {
  17926. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17927. }
  17928. else
  17929. {
  17930. s.dump(*this, false, ensure_ascii, 0);
  17931. }
  17932. return result;
  17933. }
  17934. /// @brief return the type of the JSON value (explicit)
  17935. /// @sa https://json.nlohmann.me/api/basic_json/type/
  17936. constexpr value_t type() const noexcept
  17937. {
  17938. return m_data.m_type;
  17939. }
  17940. /// @brief return whether type is primitive
  17941. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  17942. constexpr bool is_primitive() const noexcept
  17943. {
  17944. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17945. }
  17946. /// @brief return whether type is structured
  17947. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  17948. constexpr bool is_structured() const noexcept
  17949. {
  17950. return is_array() || is_object();
  17951. }
  17952. /// @brief return whether value is null
  17953. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  17954. constexpr bool is_null() const noexcept
  17955. {
  17956. return m_data.m_type == value_t::null;
  17957. }
  17958. /// @brief return whether value is a boolean
  17959. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  17960. constexpr bool is_boolean() const noexcept
  17961. {
  17962. return m_data.m_type == value_t::boolean;
  17963. }
  17964. /// @brief return whether value is a number
  17965. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  17966. constexpr bool is_number() const noexcept
  17967. {
  17968. return is_number_integer() || is_number_float();
  17969. }
  17970. /// @brief return whether value is an integer number
  17971. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  17972. constexpr bool is_number_integer() const noexcept
  17973. {
  17974. return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
  17975. }
  17976. /// @brief return whether value is an unsigned integer number
  17977. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  17978. constexpr bool is_number_unsigned() const noexcept
  17979. {
  17980. return m_data.m_type == value_t::number_unsigned;
  17981. }
  17982. /// @brief return whether value is a floating-point number
  17983. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  17984. constexpr bool is_number_float() const noexcept
  17985. {
  17986. return m_data.m_type == value_t::number_float;
  17987. }
  17988. /// @brief return whether value is an object
  17989. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  17990. constexpr bool is_object() const noexcept
  17991. {
  17992. return m_data.m_type == value_t::object;
  17993. }
  17994. /// @brief return whether value is an array
  17995. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  17996. constexpr bool is_array() const noexcept
  17997. {
  17998. return m_data.m_type == value_t::array;
  17999. }
  18000. /// @brief return whether value is a string
  18001. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  18002. constexpr bool is_string() const noexcept
  18003. {
  18004. return m_data.m_type == value_t::string;
  18005. }
  18006. /// @brief return whether value is a binary array
  18007. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  18008. constexpr bool is_binary() const noexcept
  18009. {
  18010. return m_data.m_type == value_t::binary;
  18011. }
  18012. /// @brief return whether value is discarded
  18013. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  18014. constexpr bool is_discarded() const noexcept
  18015. {
  18016. return m_data.m_type == value_t::discarded;
  18017. }
  18018. /// @brief return the type of the JSON value (implicit)
  18019. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  18020. constexpr operator value_t() const noexcept
  18021. {
  18022. return m_data.m_type;
  18023. }
  18024. /// @}
  18025. private:
  18026. //////////////////
  18027. // value access //
  18028. //////////////////
  18029. /// get a boolean (explicit)
  18030. boolean_t get_impl(boolean_t* /*unused*/) const
  18031. {
  18032. if (JSON_HEDLEY_LIKELY(is_boolean()))
  18033. {
  18034. return m_data.m_value.boolean;
  18035. }
  18036. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  18037. }
  18038. /// get a pointer to the value (object)
  18039. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  18040. {
  18041. return is_object() ? m_data.m_value.object : nullptr;
  18042. }
  18043. /// get a pointer to the value (object)
  18044. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  18045. {
  18046. return is_object() ? m_data.m_value.object : nullptr;
  18047. }
  18048. /// get a pointer to the value (array)
  18049. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  18050. {
  18051. return is_array() ? m_data.m_value.array : nullptr;
  18052. }
  18053. /// get a pointer to the value (array)
  18054. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  18055. {
  18056. return is_array() ? m_data.m_value.array : nullptr;
  18057. }
  18058. /// get a pointer to the value (string)
  18059. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  18060. {
  18061. return is_string() ? m_data.m_value.string : nullptr;
  18062. }
  18063. /// get a pointer to the value (string)
  18064. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  18065. {
  18066. return is_string() ? m_data.m_value.string : nullptr;
  18067. }
  18068. /// get a pointer to the value (boolean)
  18069. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  18070. {
  18071. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18072. }
  18073. /// get a pointer to the value (boolean)
  18074. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  18075. {
  18076. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18077. }
  18078. /// get a pointer to the value (integer number)
  18079. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  18080. {
  18081. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  18082. }
  18083. /// get a pointer to the value (integer number)
  18084. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  18085. {
  18086. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  18087. }
  18088. /// get a pointer to the value (unsigned number)
  18089. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  18090. {
  18091. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18092. }
  18093. /// get a pointer to the value (unsigned number)
  18094. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  18095. {
  18096. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18097. }
  18098. /// get a pointer to the value (floating-point number)
  18099. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  18100. {
  18101. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18102. }
  18103. /// get a pointer to the value (floating-point number)
  18104. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  18105. {
  18106. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18107. }
  18108. /// get a pointer to the value (binary)
  18109. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  18110. {
  18111. return is_binary() ? m_data.m_value.binary : nullptr;
  18112. }
  18113. /// get a pointer to the value (binary)
  18114. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  18115. {
  18116. return is_binary() ? m_data.m_value.binary : nullptr;
  18117. }
  18118. /*!
  18119. @brief helper function to implement get_ref()
  18120. This function helps to implement get_ref() without code duplication for
  18121. const and non-const overloads
  18122. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  18123. @throw type_error.303 if ReferenceType does not match underlying value
  18124. type of the current JSON
  18125. */
  18126. template<typename ReferenceType, typename ThisType>
  18127. static ReferenceType get_ref_impl(ThisType& obj)
  18128. {
  18129. // delegate the call to get_ptr<>()
  18130. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18131. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18132. {
  18133. return *ptr;
  18134. }
  18135. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18136. }
  18137. public:
  18138. /// @name value access
  18139. /// Direct access to the stored value of a JSON value.
  18140. /// @{
  18141. /// @brief get a pointer value (implicit)
  18142. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18143. template<typename PointerType, typename std::enable_if<
  18144. std::is_pointer<PointerType>::value, int>::type = 0>
  18145. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18146. {
  18147. // delegate the call to get_impl_ptr<>()
  18148. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18149. }
  18150. /// @brief get a pointer value (implicit)
  18151. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18152. template < typename PointerType, typename std::enable_if <
  18153. std::is_pointer<PointerType>::value&&
  18154. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18155. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18156. {
  18157. // delegate the call to get_impl_ptr<>() const
  18158. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18159. }
  18160. private:
  18161. /*!
  18162. @brief get a value (explicit)
  18163. Explicit type conversion between the JSON value and a compatible value
  18164. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18165. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18166. The value is converted by calling the @ref json_serializer<ValueType>
  18167. `from_json()` method.
  18168. The function is equivalent to executing
  18169. @code {.cpp}
  18170. ValueType ret;
  18171. JSONSerializer<ValueType>::from_json(*this, ret);
  18172. return ret;
  18173. @endcode
  18174. This overloads is chosen if:
  18175. - @a ValueType is not @ref basic_json,
  18176. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18177. `void from_json(const basic_json&, ValueType&)`, and
  18178. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  18179. the form `ValueType from_json(const basic_json&)`
  18180. @tparam ValueType the returned value type
  18181. @return copy of the JSON value, converted to @a ValueType
  18182. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18183. @liveexample{The example below shows several conversions from JSON values
  18184. to other types. There a few things to note: (1) Floating-point numbers can
  18185. be converted to integers\, (2) A JSON array can be converted to a standard
  18186. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18187. associative containers such as `std::unordered_map<std::string\,
  18188. json>`.,get__ValueType_const}
  18189. @since version 2.1.0
  18190. */
  18191. template < typename ValueType,
  18192. detail::enable_if_t <
  18193. detail::is_default_constructible<ValueType>::value&&
  18194. detail::has_from_json<basic_json_t, ValueType>::value,
  18195. int > = 0 >
  18196. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  18197. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  18198. {
  18199. auto ret = ValueType();
  18200. JSONSerializer<ValueType>::from_json(*this, ret);
  18201. return ret;
  18202. }
  18203. /*!
  18204. @brief get a value (explicit); special case
  18205. Explicit type conversion between the JSON value and a compatible value
  18206. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18207. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18208. The value is converted by calling the @ref json_serializer<ValueType>
  18209. `from_json()` method.
  18210. The function is equivalent to executing
  18211. @code {.cpp}
  18212. return JSONSerializer<ValueType>::from_json(*this);
  18213. @endcode
  18214. This overloads is chosen if:
  18215. - @a ValueType is not @ref basic_json and
  18216. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18217. `ValueType from_json(const basic_json&)`
  18218. @note If @ref json_serializer<ValueType> has both overloads of
  18219. `from_json()`, this one is chosen.
  18220. @tparam ValueType the returned value type
  18221. @return copy of the JSON value, converted to @a ValueType
  18222. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18223. @since version 2.1.0
  18224. */
  18225. template < typename ValueType,
  18226. detail::enable_if_t <
  18227. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  18228. int > = 0 >
  18229. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  18230. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  18231. {
  18232. return JSONSerializer<ValueType>::from_json(*this);
  18233. }
  18234. /*!
  18235. @brief get special-case overload
  18236. This overloads converts the current @ref basic_json in a different
  18237. @ref basic_json type
  18238. @tparam BasicJsonType == @ref basic_json
  18239. @return a copy of *this, converted into @a BasicJsonType
  18240. @complexity Depending on the implementation of the called `from_json()`
  18241. method.
  18242. @since version 3.2.0
  18243. */
  18244. template < typename BasicJsonType,
  18245. detail::enable_if_t <
  18246. detail::is_basic_json<BasicJsonType>::value,
  18247. int > = 0 >
  18248. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  18249. {
  18250. return *this;
  18251. }
  18252. /*!
  18253. @brief get special-case overload
  18254. This overloads avoids a lot of template boilerplate, it can be seen as the
  18255. identity method
  18256. @tparam BasicJsonType == @ref basic_json
  18257. @return a copy of *this
  18258. @complexity Constant.
  18259. @since version 2.1.0
  18260. */
  18261. template<typename BasicJsonType,
  18262. detail::enable_if_t<
  18263. std::is_same<BasicJsonType, basic_json_t>::value,
  18264. int> = 0>
  18265. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  18266. {
  18267. return *this;
  18268. }
  18269. /*!
  18270. @brief get a pointer value (explicit)
  18271. @copydoc get()
  18272. */
  18273. template<typename PointerType,
  18274. detail::enable_if_t<
  18275. std::is_pointer<PointerType>::value,
  18276. int> = 0>
  18277. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  18278. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  18279. {
  18280. // delegate the call to get_ptr
  18281. return get_ptr<PointerType>();
  18282. }
  18283. public:
  18284. /*!
  18285. @brief get a (pointer) value (explicit)
  18286. Performs explicit type conversion between the JSON value and a compatible value if required.
  18287. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  18288. No copies are made.
  18289. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  18290. from the current @ref basic_json.
  18291. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  18292. method.
  18293. @tparam ValueTypeCV the provided value type
  18294. @tparam ValueType the returned value type
  18295. @return copy of the JSON value, converted to @tparam ValueType if necessary
  18296. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  18297. @since version 2.1.0
  18298. */
  18299. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  18300. #if defined(JSON_HAS_CPP_14)
  18301. constexpr
  18302. #endif
  18303. auto get() const noexcept(
  18304. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  18305. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  18306. {
  18307. // we cannot static_assert on ValueTypeCV being non-const, because
  18308. // there is support for get<const basic_json_t>(), which is why we
  18309. // still need the uncvref
  18310. static_assert(!std::is_reference<ValueTypeCV>::value,
  18311. "get() cannot be used with reference types, you might want to use get_ref()");
  18312. return get_impl<ValueType>(detail::priority_tag<4> {});
  18313. }
  18314. /*!
  18315. @brief get a pointer value (explicit)
  18316. Explicit pointer access to the internally stored JSON value. No copies are
  18317. made.
  18318. @warning The pointer becomes invalid if the underlying JSON object
  18319. changes.
  18320. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  18321. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  18322. @ref number_unsigned_t, or @ref number_float_t.
  18323. @return pointer to the internally stored JSON value if the requested
  18324. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  18325. @complexity Constant.
  18326. @liveexample{The example below shows how pointers to internal values of a
  18327. JSON value can be requested. Note that no type conversions are made and a
  18328. `nullptr` is returned if the value and the requested pointer type does not
  18329. match.,get__PointerType}
  18330. @sa see @ref get_ptr() for explicit pointer-member access
  18331. @since version 1.0.0
  18332. */
  18333. template<typename PointerType, typename std::enable_if<
  18334. std::is_pointer<PointerType>::value, int>::type = 0>
  18335. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  18336. {
  18337. // delegate the call to get_ptr
  18338. return get_ptr<PointerType>();
  18339. }
  18340. /// @brief get a value (explicit)
  18341. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  18342. template < typename ValueType,
  18343. detail::enable_if_t <
  18344. !detail::is_basic_json<ValueType>::value&&
  18345. detail::has_from_json<basic_json_t, ValueType>::value,
  18346. int > = 0 >
  18347. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  18348. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  18349. {
  18350. JSONSerializer<ValueType>::from_json(*this, v);
  18351. return v;
  18352. }
  18353. // specialization to allow calling get_to with a basic_json value
  18354. // see https://github.com/nlohmann/json/issues/2175
  18355. template<typename ValueType,
  18356. detail::enable_if_t <
  18357. detail::is_basic_json<ValueType>::value,
  18358. int> = 0>
  18359. ValueType & get_to(ValueType& v) const
  18360. {
  18361. v = *this;
  18362. return v;
  18363. }
  18364. template <
  18365. typename T, std::size_t N,
  18366. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18367. detail::enable_if_t <
  18368. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  18369. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18370. noexcept(noexcept(JSONSerializer<Array>::from_json(
  18371. std::declval<const basic_json_t&>(), v)))
  18372. {
  18373. JSONSerializer<Array>::from_json(*this, v);
  18374. return v;
  18375. }
  18376. /// @brief get a reference value (implicit)
  18377. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18378. template<typename ReferenceType, typename std::enable_if<
  18379. std::is_reference<ReferenceType>::value, int>::type = 0>
  18380. ReferenceType get_ref()
  18381. {
  18382. // delegate call to get_ref_impl
  18383. return get_ref_impl<ReferenceType>(*this);
  18384. }
  18385. /// @brief get a reference value (implicit)
  18386. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18387. template < typename ReferenceType, typename std::enable_if <
  18388. std::is_reference<ReferenceType>::value&&
  18389. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  18390. ReferenceType get_ref() const
  18391. {
  18392. // delegate call to get_ref_impl
  18393. return get_ref_impl<ReferenceType>(*this);
  18394. }
  18395. /*!
  18396. @brief get a value (implicit)
  18397. Implicit type conversion between the JSON value and a compatible value.
  18398. The call is realized by calling @ref get() const.
  18399. @tparam ValueType non-pointer type compatible to the JSON value, for
  18400. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  18401. `std::vector` types for JSON arrays. The character type of @ref string_t
  18402. as well as an initializer list of this type is excluded to avoid
  18403. ambiguities as these types implicitly convert to `std::string`.
  18404. @return copy of the JSON value, converted to type @a ValueType
  18405. @throw type_error.302 in case passed type @a ValueType is incompatible
  18406. to the JSON value type (e.g., the JSON value is of type boolean, but a
  18407. string is requested); see example below
  18408. @complexity Linear in the size of the JSON value.
  18409. @liveexample{The example below shows several conversions from JSON values
  18410. to other types. There a few things to note: (1) Floating-point numbers can
  18411. be converted to integers\, (2) A JSON array can be converted to a standard
  18412. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18413. associative containers such as `std::unordered_map<std::string\,
  18414. json>`.,operator__ValueType}
  18415. @since version 1.0.0
  18416. */
  18417. template < typename ValueType, typename std::enable_if <
  18418. detail::conjunction <
  18419. detail::negation<std::is_pointer<ValueType>>,
  18420. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  18421. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  18422. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  18423. detail::negation<detail::is_basic_json<ValueType>>,
  18424. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  18425. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  18426. detail::negation<std::is_same<ValueType, std::string_view>>,
  18427. #endif
  18428. #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
  18429. detail::negation<std::is_same<ValueType, std::any>>,
  18430. #endif
  18431. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  18432. >::value, int >::type = 0 >
  18433. JSON_EXPLICIT operator ValueType() const
  18434. {
  18435. // delegate the call to get<>() const
  18436. return get<ValueType>();
  18437. }
  18438. /// @brief get a binary value
  18439. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18440. binary_t& get_binary()
  18441. {
  18442. if (!is_binary())
  18443. {
  18444. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18445. }
  18446. return *get_ptr<binary_t*>();
  18447. }
  18448. /// @brief get a binary value
  18449. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18450. const binary_t& get_binary() const
  18451. {
  18452. if (!is_binary())
  18453. {
  18454. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18455. }
  18456. return *get_ptr<const binary_t*>();
  18457. }
  18458. /// @}
  18459. ////////////////////
  18460. // element access //
  18461. ////////////////////
  18462. /// @name element access
  18463. /// Access to the JSON value.
  18464. /// @{
  18465. /// @brief access specified array element with bounds checking
  18466. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18467. reference at(size_type idx)
  18468. {
  18469. // at only works for arrays
  18470. if (JSON_HEDLEY_LIKELY(is_array()))
  18471. {
  18472. JSON_TRY
  18473. {
  18474. return set_parent(m_data.m_value.array->at(idx));
  18475. }
  18476. JSON_CATCH (std::out_of_range&)
  18477. {
  18478. // create better exception explanation
  18479. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18480. }
  18481. }
  18482. else
  18483. {
  18484. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18485. }
  18486. }
  18487. /// @brief access specified array element with bounds checking
  18488. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18489. const_reference at(size_type idx) const
  18490. {
  18491. // at only works for arrays
  18492. if (JSON_HEDLEY_LIKELY(is_array()))
  18493. {
  18494. JSON_TRY
  18495. {
  18496. return m_data.m_value.array->at(idx);
  18497. }
  18498. JSON_CATCH (std::out_of_range&)
  18499. {
  18500. // create better exception explanation
  18501. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18502. }
  18503. }
  18504. else
  18505. {
  18506. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18507. }
  18508. }
  18509. /// @brief access specified object element with bounds checking
  18510. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18511. reference at(const typename object_t::key_type& key)
  18512. {
  18513. // at only works for objects
  18514. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18515. {
  18516. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18517. }
  18518. auto it = m_data.m_value.object->find(key);
  18519. if (it == m_data.m_value.object->end())
  18520. {
  18521. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18522. }
  18523. return set_parent(it->second);
  18524. }
  18525. /// @brief access specified object element with bounds checking
  18526. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18527. template<class KeyType, detail::enable_if_t<
  18528. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18529. reference at(KeyType && key)
  18530. {
  18531. // at only works for objects
  18532. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18533. {
  18534. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18535. }
  18536. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18537. if (it == m_data.m_value.object->end())
  18538. {
  18539. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18540. }
  18541. return set_parent(it->second);
  18542. }
  18543. /// @brief access specified object element with bounds checking
  18544. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18545. const_reference at(const typename object_t::key_type& key) const
  18546. {
  18547. // at only works for objects
  18548. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18549. {
  18550. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18551. }
  18552. auto it = m_data.m_value.object->find(key);
  18553. if (it == m_data.m_value.object->end())
  18554. {
  18555. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18556. }
  18557. return it->second;
  18558. }
  18559. /// @brief access specified object element with bounds checking
  18560. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18561. template<class KeyType, detail::enable_if_t<
  18562. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18563. const_reference at(KeyType && key) const
  18564. {
  18565. // at only works for objects
  18566. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18567. {
  18568. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18569. }
  18570. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18571. if (it == m_data.m_value.object->end())
  18572. {
  18573. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18574. }
  18575. return it->second;
  18576. }
  18577. /// @brief access specified array element
  18578. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18579. reference operator[](size_type idx)
  18580. {
  18581. // implicitly convert null value to an empty array
  18582. if (is_null())
  18583. {
  18584. m_data.m_type = value_t::array;
  18585. m_data.m_value.array = create<array_t>();
  18586. assert_invariant();
  18587. }
  18588. // operator[] only works for arrays
  18589. if (JSON_HEDLEY_LIKELY(is_array()))
  18590. {
  18591. // fill up array with null values if given idx is outside range
  18592. if (idx >= m_data.m_value.array->size())
  18593. {
  18594. #if JSON_DIAGNOSTICS
  18595. // remember array size & capacity before resizing
  18596. const auto old_size = m_data.m_value.array->size();
  18597. const auto old_capacity = m_data.m_value.array->capacity();
  18598. #endif
  18599. m_data.m_value.array->resize(idx + 1);
  18600. #if JSON_DIAGNOSTICS
  18601. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  18602. {
  18603. // capacity has changed: update all parents
  18604. set_parents();
  18605. }
  18606. else
  18607. {
  18608. // set parent for values added above
  18609. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  18610. }
  18611. #endif
  18612. assert_invariant();
  18613. }
  18614. return m_data.m_value.array->operator[](idx);
  18615. }
  18616. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18617. }
  18618. /// @brief access specified array element
  18619. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18620. const_reference operator[](size_type idx) const
  18621. {
  18622. // const operator[] only works for arrays
  18623. if (JSON_HEDLEY_LIKELY(is_array()))
  18624. {
  18625. return m_data.m_value.array->operator[](idx);
  18626. }
  18627. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18628. }
  18629. /// @brief access specified object element
  18630. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18631. reference operator[](typename object_t::key_type key)
  18632. {
  18633. // implicitly convert null value to an empty object
  18634. if (is_null())
  18635. {
  18636. m_data.m_type = value_t::object;
  18637. m_data.m_value.object = create<object_t>();
  18638. assert_invariant();
  18639. }
  18640. // operator[] only works for objects
  18641. if (JSON_HEDLEY_LIKELY(is_object()))
  18642. {
  18643. auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
  18644. return set_parent(result.first->second);
  18645. }
  18646. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18647. }
  18648. /// @brief access specified object element
  18649. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18650. const_reference operator[](const typename object_t::key_type& key) const
  18651. {
  18652. // const operator[] only works for objects
  18653. if (JSON_HEDLEY_LIKELY(is_object()))
  18654. {
  18655. auto it = m_data.m_value.object->find(key);
  18656. JSON_ASSERT(it != m_data.m_value.object->end());
  18657. return it->second;
  18658. }
  18659. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18660. }
  18661. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  18662. // (they seemingly cannot be constrained to resolve the ambiguity)
  18663. template<typename T>
  18664. reference operator[](T* key)
  18665. {
  18666. return operator[](typename object_t::key_type(key));
  18667. }
  18668. template<typename T>
  18669. const_reference operator[](T* key) const
  18670. {
  18671. return operator[](typename object_t::key_type(key));
  18672. }
  18673. /// @brief access specified object element
  18674. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18675. template<class KeyType, detail::enable_if_t<
  18676. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18677. reference operator[](KeyType && key)
  18678. {
  18679. // implicitly convert null value to an empty object
  18680. if (is_null())
  18681. {
  18682. m_data.m_type = value_t::object;
  18683. m_data.m_value.object = create<object_t>();
  18684. assert_invariant();
  18685. }
  18686. // operator[] only works for objects
  18687. if (JSON_HEDLEY_LIKELY(is_object()))
  18688. {
  18689. auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  18690. return set_parent(result.first->second);
  18691. }
  18692. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18693. }
  18694. /// @brief access specified object element
  18695. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18696. template<class KeyType, detail::enable_if_t<
  18697. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18698. const_reference operator[](KeyType && key) const
  18699. {
  18700. // const operator[] only works for objects
  18701. if (JSON_HEDLEY_LIKELY(is_object()))
  18702. {
  18703. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18704. JSON_ASSERT(it != m_data.m_value.object->end());
  18705. return it->second;
  18706. }
  18707. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18708. }
  18709. private:
  18710. template<typename KeyType>
  18711. using is_comparable_with_object_key = detail::is_comparable <
  18712. object_comparator_t, const typename object_t::key_type&, KeyType >;
  18713. template<typename ValueType>
  18714. using value_return_type = std::conditional <
  18715. detail::is_c_string_uncvref<ValueType>::value,
  18716. string_t, typename std::decay<ValueType>::type >;
  18717. public:
  18718. /// @brief access specified object element with default value
  18719. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18720. template < class ValueType, detail::enable_if_t <
  18721. !detail::is_transparent<object_comparator_t>::value
  18722. && detail::is_getable<basic_json_t, ValueType>::value
  18723. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18724. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  18725. {
  18726. // value only works for objects
  18727. if (JSON_HEDLEY_LIKELY(is_object()))
  18728. {
  18729. // if key is found, return value and given default value otherwise
  18730. const auto it = find(key);
  18731. if (it != end())
  18732. {
  18733. return it->template get<ValueType>();
  18734. }
  18735. return default_value;
  18736. }
  18737. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18738. }
  18739. /// @brief access specified object element with default value
  18740. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18741. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18742. detail::enable_if_t <
  18743. !detail::is_transparent<object_comparator_t>::value
  18744. && detail::is_getable<basic_json_t, ReturnType>::value
  18745. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18746. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  18747. {
  18748. // value only works for objects
  18749. if (JSON_HEDLEY_LIKELY(is_object()))
  18750. {
  18751. // if key is found, return value and given default value otherwise
  18752. const auto it = find(key);
  18753. if (it != end())
  18754. {
  18755. return it->template get<ReturnType>();
  18756. }
  18757. return std::forward<ValueType>(default_value);
  18758. }
  18759. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18760. }
  18761. /// @brief access specified object element with default value
  18762. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18763. template < class ValueType, class KeyType, detail::enable_if_t <
  18764. detail::is_transparent<object_comparator_t>::value
  18765. && !detail::is_json_pointer<KeyType>::value
  18766. && is_comparable_with_object_key<KeyType>::value
  18767. && detail::is_getable<basic_json_t, ValueType>::value
  18768. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18769. ValueType value(KeyType && key, const ValueType& default_value) const
  18770. {
  18771. // value only works for objects
  18772. if (JSON_HEDLEY_LIKELY(is_object()))
  18773. {
  18774. // if key is found, return value and given default value otherwise
  18775. const auto it = find(std::forward<KeyType>(key));
  18776. if (it != end())
  18777. {
  18778. return it->template get<ValueType>();
  18779. }
  18780. return default_value;
  18781. }
  18782. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18783. }
  18784. /// @brief access specified object element via JSON Pointer with default value
  18785. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18786. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  18787. detail::enable_if_t <
  18788. detail::is_transparent<object_comparator_t>::value
  18789. && !detail::is_json_pointer<KeyType>::value
  18790. && is_comparable_with_object_key<KeyType>::value
  18791. && detail::is_getable<basic_json_t, ReturnType>::value
  18792. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18793. ReturnType value(KeyType && key, ValueType && default_value) const
  18794. {
  18795. // value only works for objects
  18796. if (JSON_HEDLEY_LIKELY(is_object()))
  18797. {
  18798. // if key is found, return value and given default value otherwise
  18799. const auto it = find(std::forward<KeyType>(key));
  18800. if (it != end())
  18801. {
  18802. return it->template get<ReturnType>();
  18803. }
  18804. return std::forward<ValueType>(default_value);
  18805. }
  18806. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18807. }
  18808. /// @brief access specified object element via JSON Pointer with default value
  18809. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18810. template < class ValueType, detail::enable_if_t <
  18811. detail::is_getable<basic_json_t, ValueType>::value
  18812. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18813. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18814. {
  18815. // value only works for objects
  18816. if (JSON_HEDLEY_LIKELY(is_object()))
  18817. {
  18818. // if pointer resolves a value, return it or use default value
  18819. JSON_TRY
  18820. {
  18821. return ptr.get_checked(this).template get<ValueType>();
  18822. }
  18823. JSON_INTERNAL_CATCH (out_of_range&)
  18824. {
  18825. return default_value;
  18826. }
  18827. }
  18828. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18829. }
  18830. /// @brief access specified object element via JSON Pointer with default value
  18831. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18832. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18833. detail::enable_if_t <
  18834. detail::is_getable<basic_json_t, ReturnType>::value
  18835. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18836. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  18837. {
  18838. // value only works for objects
  18839. if (JSON_HEDLEY_LIKELY(is_object()))
  18840. {
  18841. // if pointer resolves a value, return it or use default value
  18842. JSON_TRY
  18843. {
  18844. return ptr.get_checked(this).template get<ReturnType>();
  18845. }
  18846. JSON_INTERNAL_CATCH (out_of_range&)
  18847. {
  18848. return std::forward<ValueType>(default_value);
  18849. }
  18850. }
  18851. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18852. }
  18853. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  18854. detail::is_basic_json<BasicJsonType>::value
  18855. && detail::is_getable<basic_json_t, ValueType>::value
  18856. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18857. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18858. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  18859. {
  18860. return value(ptr.convert(), default_value);
  18861. }
  18862. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  18863. detail::enable_if_t <
  18864. detail::is_basic_json<BasicJsonType>::value
  18865. && detail::is_getable<basic_json_t, ReturnType>::value
  18866. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18867. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18868. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  18869. {
  18870. return value(ptr.convert(), std::forward<ValueType>(default_value));
  18871. }
  18872. /// @brief access the first element
  18873. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18874. reference front()
  18875. {
  18876. return *begin();
  18877. }
  18878. /// @brief access the first element
  18879. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18880. const_reference front() const
  18881. {
  18882. return *cbegin();
  18883. }
  18884. /// @brief access the last element
  18885. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18886. reference back()
  18887. {
  18888. auto tmp = end();
  18889. --tmp;
  18890. return *tmp;
  18891. }
  18892. /// @brief access the last element
  18893. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18894. const_reference back() const
  18895. {
  18896. auto tmp = cend();
  18897. --tmp;
  18898. return *tmp;
  18899. }
  18900. /// @brief remove element given an iterator
  18901. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18902. template < class IteratorType, detail::enable_if_t <
  18903. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18904. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18905. IteratorType erase(IteratorType pos)
  18906. {
  18907. // make sure iterator fits the current value
  18908. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18909. {
  18910. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18911. }
  18912. IteratorType result = end();
  18913. switch (m_data.m_type)
  18914. {
  18915. case value_t::boolean:
  18916. case value_t::number_float:
  18917. case value_t::number_integer:
  18918. case value_t::number_unsigned:
  18919. case value_t::string:
  18920. case value_t::binary:
  18921. {
  18922. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18923. {
  18924. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  18925. }
  18926. if (is_string())
  18927. {
  18928. AllocatorType<string_t> alloc;
  18929. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  18930. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  18931. m_data.m_value.string = nullptr;
  18932. }
  18933. else if (is_binary())
  18934. {
  18935. AllocatorType<binary_t> alloc;
  18936. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  18937. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  18938. m_data.m_value.binary = nullptr;
  18939. }
  18940. m_data.m_type = value_t::null;
  18941. assert_invariant();
  18942. break;
  18943. }
  18944. case value_t::object:
  18945. {
  18946. result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
  18947. break;
  18948. }
  18949. case value_t::array:
  18950. {
  18951. result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
  18952. break;
  18953. }
  18954. case value_t::null:
  18955. case value_t::discarded:
  18956. default:
  18957. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18958. }
  18959. return result;
  18960. }
  18961. /// @brief remove elements given an iterator range
  18962. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18963. template < class IteratorType, detail::enable_if_t <
  18964. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18965. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18966. IteratorType erase(IteratorType first, IteratorType last)
  18967. {
  18968. // make sure iterator fits the current value
  18969. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18970. {
  18971. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  18972. }
  18973. IteratorType result = end();
  18974. switch (m_data.m_type)
  18975. {
  18976. case value_t::boolean:
  18977. case value_t::number_float:
  18978. case value_t::number_integer:
  18979. case value_t::number_unsigned:
  18980. case value_t::string:
  18981. case value_t::binary:
  18982. {
  18983. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18984. || !last.m_it.primitive_iterator.is_end()))
  18985. {
  18986. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  18987. }
  18988. if (is_string())
  18989. {
  18990. AllocatorType<string_t> alloc;
  18991. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  18992. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  18993. m_data.m_value.string = nullptr;
  18994. }
  18995. else if (is_binary())
  18996. {
  18997. AllocatorType<binary_t> alloc;
  18998. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  18999. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  19000. m_data.m_value.binary = nullptr;
  19001. }
  19002. m_data.m_type = value_t::null;
  19003. assert_invariant();
  19004. break;
  19005. }
  19006. case value_t::object:
  19007. {
  19008. result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
  19009. last.m_it.object_iterator);
  19010. break;
  19011. }
  19012. case value_t::array:
  19013. {
  19014. result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
  19015. last.m_it.array_iterator);
  19016. break;
  19017. }
  19018. case value_t::null:
  19019. case value_t::discarded:
  19020. default:
  19021. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19022. }
  19023. return result;
  19024. }
  19025. private:
  19026. template < typename KeyType, detail::enable_if_t <
  19027. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19028. size_type erase_internal(KeyType && key)
  19029. {
  19030. // this erase only works for objects
  19031. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19032. {
  19033. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19034. }
  19035. return m_data.m_value.object->erase(std::forward<KeyType>(key));
  19036. }
  19037. template < typename KeyType, detail::enable_if_t <
  19038. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19039. size_type erase_internal(KeyType && key)
  19040. {
  19041. // this erase only works for objects
  19042. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19043. {
  19044. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19045. }
  19046. const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19047. if (it != m_data.m_value.object->end())
  19048. {
  19049. m_data.m_value.object->erase(it);
  19050. return 1;
  19051. }
  19052. return 0;
  19053. }
  19054. public:
  19055. /// @brief remove element from a JSON object given a key
  19056. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19057. size_type erase(const typename object_t::key_type& key)
  19058. {
  19059. // the indirection via erase_internal() is added to avoid making this
  19060. // function a template and thus de-rank it during overload resolution
  19061. return erase_internal(key);
  19062. }
  19063. /// @brief remove element from a JSON object given a key
  19064. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19065. template<class KeyType, detail::enable_if_t<
  19066. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19067. size_type erase(KeyType && key)
  19068. {
  19069. return erase_internal(std::forward<KeyType>(key));
  19070. }
  19071. /// @brief remove element from a JSON array given an index
  19072. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19073. void erase(const size_type idx)
  19074. {
  19075. // this erase only works for arrays
  19076. if (JSON_HEDLEY_LIKELY(is_array()))
  19077. {
  19078. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  19079. {
  19080. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19081. }
  19082. m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
  19083. }
  19084. else
  19085. {
  19086. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19087. }
  19088. }
  19089. /// @}
  19090. ////////////
  19091. // lookup //
  19092. ////////////
  19093. /// @name lookup
  19094. /// @{
  19095. /// @brief find an element in a JSON object
  19096. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19097. iterator find(const typename object_t::key_type& key)
  19098. {
  19099. auto result = end();
  19100. if (is_object())
  19101. {
  19102. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19103. }
  19104. return result;
  19105. }
  19106. /// @brief find an element in a JSON object
  19107. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19108. const_iterator find(const typename object_t::key_type& key) const
  19109. {
  19110. auto result = cend();
  19111. if (is_object())
  19112. {
  19113. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19114. }
  19115. return result;
  19116. }
  19117. /// @brief find an element in a JSON object
  19118. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19119. template<class KeyType, detail::enable_if_t<
  19120. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19121. iterator find(KeyType && key)
  19122. {
  19123. auto result = end();
  19124. if (is_object())
  19125. {
  19126. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19127. }
  19128. return result;
  19129. }
  19130. /// @brief find an element in a JSON object
  19131. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19132. template<class KeyType, detail::enable_if_t<
  19133. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19134. const_iterator find(KeyType && key) const
  19135. {
  19136. auto result = cend();
  19137. if (is_object())
  19138. {
  19139. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19140. }
  19141. return result;
  19142. }
  19143. /// @brief returns the number of occurrences of a key in a JSON object
  19144. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19145. size_type count(const typename object_t::key_type& key) const
  19146. {
  19147. // return 0 for all nonobject types
  19148. return is_object() ? m_data.m_value.object->count(key) : 0;
  19149. }
  19150. /// @brief returns the number of occurrences of a key in a JSON object
  19151. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19152. template<class KeyType, detail::enable_if_t<
  19153. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19154. size_type count(KeyType && key) const
  19155. {
  19156. // return 0 for all nonobject types
  19157. return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
  19158. }
  19159. /// @brief check the existence of an element in a JSON object
  19160. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19161. bool contains(const typename object_t::key_type& key) const
  19162. {
  19163. return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
  19164. }
  19165. /// @brief check the existence of an element in a JSON object
  19166. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19167. template<class KeyType, detail::enable_if_t<
  19168. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19169. bool contains(KeyType && key) const
  19170. {
  19171. return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
  19172. }
  19173. /// @brief check the existence of an element in a JSON object given a JSON pointer
  19174. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19175. bool contains(const json_pointer& ptr) const
  19176. {
  19177. return ptr.contains(this);
  19178. }
  19179. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  19180. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19181. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  19182. {
  19183. return ptr.contains(this);
  19184. }
  19185. /// @}
  19186. ///////////////
  19187. // iterators //
  19188. ///////////////
  19189. /// @name iterators
  19190. /// @{
  19191. /// @brief returns an iterator to the first element
  19192. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19193. iterator begin() noexcept
  19194. {
  19195. iterator result(this);
  19196. result.set_begin();
  19197. return result;
  19198. }
  19199. /// @brief returns an iterator to the first element
  19200. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19201. const_iterator begin() const noexcept
  19202. {
  19203. return cbegin();
  19204. }
  19205. /// @brief returns a const iterator to the first element
  19206. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  19207. const_iterator cbegin() const noexcept
  19208. {
  19209. const_iterator result(this);
  19210. result.set_begin();
  19211. return result;
  19212. }
  19213. /// @brief returns an iterator to one past the last element
  19214. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19215. iterator end() noexcept
  19216. {
  19217. iterator result(this);
  19218. result.set_end();
  19219. return result;
  19220. }
  19221. /// @brief returns an iterator to one past the last element
  19222. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19223. const_iterator end() const noexcept
  19224. {
  19225. return cend();
  19226. }
  19227. /// @brief returns an iterator to one past the last element
  19228. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  19229. const_iterator cend() const noexcept
  19230. {
  19231. const_iterator result(this);
  19232. result.set_end();
  19233. return result;
  19234. }
  19235. /// @brief returns an iterator to the reverse-beginning
  19236. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19237. reverse_iterator rbegin() noexcept
  19238. {
  19239. return reverse_iterator(end());
  19240. }
  19241. /// @brief returns an iterator to the reverse-beginning
  19242. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19243. const_reverse_iterator rbegin() const noexcept
  19244. {
  19245. return crbegin();
  19246. }
  19247. /// @brief returns an iterator to the reverse-end
  19248. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19249. reverse_iterator rend() noexcept
  19250. {
  19251. return reverse_iterator(begin());
  19252. }
  19253. /// @brief returns an iterator to the reverse-end
  19254. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19255. const_reverse_iterator rend() const noexcept
  19256. {
  19257. return crend();
  19258. }
  19259. /// @brief returns a const reverse iterator to the last element
  19260. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  19261. const_reverse_iterator crbegin() const noexcept
  19262. {
  19263. return const_reverse_iterator(cend());
  19264. }
  19265. /// @brief returns a const reverse iterator to one before the first
  19266. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  19267. const_reverse_iterator crend() const noexcept
  19268. {
  19269. return const_reverse_iterator(cbegin());
  19270. }
  19271. public:
  19272. /// @brief wrapper to access iterator member functions in range-based for
  19273. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19274. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19275. /// version 4.0.0 of the library. Please use @ref items() instead;
  19276. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19277. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19278. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19279. {
  19280. return ref.items();
  19281. }
  19282. /// @brief wrapper to access iterator member functions in range-based for
  19283. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19284. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19285. /// version 4.0.0 of the library. Please use @ref items() instead;
  19286. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19287. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19288. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19289. {
  19290. return ref.items();
  19291. }
  19292. /// @brief helper to access iterator member functions in range-based for
  19293. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19294. iteration_proxy<iterator> items() noexcept
  19295. {
  19296. return iteration_proxy<iterator>(*this);
  19297. }
  19298. /// @brief helper to access iterator member functions in range-based for
  19299. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19300. iteration_proxy<const_iterator> items() const noexcept
  19301. {
  19302. return iteration_proxy<const_iterator>(*this);
  19303. }
  19304. /// @}
  19305. //////////////
  19306. // capacity //
  19307. //////////////
  19308. /// @name capacity
  19309. /// @{
  19310. /// @brief checks whether the container is empty.
  19311. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  19312. bool empty() const noexcept
  19313. {
  19314. switch (m_data.m_type)
  19315. {
  19316. case value_t::null:
  19317. {
  19318. // null values are empty
  19319. return true;
  19320. }
  19321. case value_t::array:
  19322. {
  19323. // delegate call to array_t::empty()
  19324. return m_data.m_value.array->empty();
  19325. }
  19326. case value_t::object:
  19327. {
  19328. // delegate call to object_t::empty()
  19329. return m_data.m_value.object->empty();
  19330. }
  19331. case value_t::string:
  19332. case value_t::boolean:
  19333. case value_t::number_integer:
  19334. case value_t::number_unsigned:
  19335. case value_t::number_float:
  19336. case value_t::binary:
  19337. case value_t::discarded:
  19338. default:
  19339. {
  19340. // all other types are nonempty
  19341. return false;
  19342. }
  19343. }
  19344. }
  19345. /// @brief returns the number of elements
  19346. /// @sa https://json.nlohmann.me/api/basic_json/size/
  19347. size_type size() const noexcept
  19348. {
  19349. switch (m_data.m_type)
  19350. {
  19351. case value_t::null:
  19352. {
  19353. // null values are empty
  19354. return 0;
  19355. }
  19356. case value_t::array:
  19357. {
  19358. // delegate call to array_t::size()
  19359. return m_data.m_value.array->size();
  19360. }
  19361. case value_t::object:
  19362. {
  19363. // delegate call to object_t::size()
  19364. return m_data.m_value.object->size();
  19365. }
  19366. case value_t::string:
  19367. case value_t::boolean:
  19368. case value_t::number_integer:
  19369. case value_t::number_unsigned:
  19370. case value_t::number_float:
  19371. case value_t::binary:
  19372. case value_t::discarded:
  19373. default:
  19374. {
  19375. // all other types have size 1
  19376. return 1;
  19377. }
  19378. }
  19379. }
  19380. /// @brief returns the maximum possible number of elements
  19381. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  19382. size_type max_size() const noexcept
  19383. {
  19384. switch (m_data.m_type)
  19385. {
  19386. case value_t::array:
  19387. {
  19388. // delegate call to array_t::max_size()
  19389. return m_data.m_value.array->max_size();
  19390. }
  19391. case value_t::object:
  19392. {
  19393. // delegate call to object_t::max_size()
  19394. return m_data.m_value.object->max_size();
  19395. }
  19396. case value_t::null:
  19397. case value_t::string:
  19398. case value_t::boolean:
  19399. case value_t::number_integer:
  19400. case value_t::number_unsigned:
  19401. case value_t::number_float:
  19402. case value_t::binary:
  19403. case value_t::discarded:
  19404. default:
  19405. {
  19406. // all other types have max_size() == size()
  19407. return size();
  19408. }
  19409. }
  19410. }
  19411. /// @}
  19412. ///////////////
  19413. // modifiers //
  19414. ///////////////
  19415. /// @name modifiers
  19416. /// @{
  19417. /// @brief clears the contents
  19418. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  19419. void clear() noexcept
  19420. {
  19421. switch (m_data.m_type)
  19422. {
  19423. case value_t::number_integer:
  19424. {
  19425. m_data.m_value.number_integer = 0;
  19426. break;
  19427. }
  19428. case value_t::number_unsigned:
  19429. {
  19430. m_data.m_value.number_unsigned = 0;
  19431. break;
  19432. }
  19433. case value_t::number_float:
  19434. {
  19435. m_data.m_value.number_float = 0.0;
  19436. break;
  19437. }
  19438. case value_t::boolean:
  19439. {
  19440. m_data.m_value.boolean = false;
  19441. break;
  19442. }
  19443. case value_t::string:
  19444. {
  19445. m_data.m_value.string->clear();
  19446. break;
  19447. }
  19448. case value_t::binary:
  19449. {
  19450. m_data.m_value.binary->clear();
  19451. break;
  19452. }
  19453. case value_t::array:
  19454. {
  19455. m_data.m_value.array->clear();
  19456. break;
  19457. }
  19458. case value_t::object:
  19459. {
  19460. m_data.m_value.object->clear();
  19461. break;
  19462. }
  19463. case value_t::null:
  19464. case value_t::discarded:
  19465. default:
  19466. break;
  19467. }
  19468. }
  19469. /// @brief add an object to an array
  19470. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19471. void push_back(basic_json&& val)
  19472. {
  19473. // push_back only works for null objects or arrays
  19474. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19475. {
  19476. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19477. }
  19478. // transform null object into an array
  19479. if (is_null())
  19480. {
  19481. m_data.m_type = value_t::array;
  19482. m_data.m_value = value_t::array;
  19483. assert_invariant();
  19484. }
  19485. // add element to array (move semantics)
  19486. const auto old_capacity = m_data.m_value.array->capacity();
  19487. m_data.m_value.array->push_back(std::move(val));
  19488. set_parent(m_data.m_value.array->back(), old_capacity);
  19489. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  19490. }
  19491. /// @brief add an object to an array
  19492. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19493. reference operator+=(basic_json&& val)
  19494. {
  19495. push_back(std::move(val));
  19496. return *this;
  19497. }
  19498. /// @brief add an object to an array
  19499. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19500. void push_back(const basic_json& val)
  19501. {
  19502. // push_back only works for null objects or arrays
  19503. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19504. {
  19505. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19506. }
  19507. // transform null object into an array
  19508. if (is_null())
  19509. {
  19510. m_data.m_type = value_t::array;
  19511. m_data.m_value = value_t::array;
  19512. assert_invariant();
  19513. }
  19514. // add element to array
  19515. const auto old_capacity = m_data.m_value.array->capacity();
  19516. m_data.m_value.array->push_back(val);
  19517. set_parent(m_data.m_value.array->back(), old_capacity);
  19518. }
  19519. /// @brief add an object to an array
  19520. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19521. reference operator+=(const basic_json& val)
  19522. {
  19523. push_back(val);
  19524. return *this;
  19525. }
  19526. /// @brief add an object to an object
  19527. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19528. void push_back(const typename object_t::value_type& val)
  19529. {
  19530. // push_back only works for null objects or objects
  19531. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19532. {
  19533. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19534. }
  19535. // transform null object into an object
  19536. if (is_null())
  19537. {
  19538. m_data.m_type = value_t::object;
  19539. m_data.m_value = value_t::object;
  19540. assert_invariant();
  19541. }
  19542. // add element to object
  19543. auto res = m_data.m_value.object->insert(val);
  19544. set_parent(res.first->second);
  19545. }
  19546. /// @brief add an object to an object
  19547. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19548. reference operator+=(const typename object_t::value_type& val)
  19549. {
  19550. push_back(val);
  19551. return *this;
  19552. }
  19553. /// @brief add an object to an object
  19554. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19555. void push_back(initializer_list_t init)
  19556. {
  19557. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  19558. {
  19559. basic_json&& key = init.begin()->moved_or_copied();
  19560. push_back(typename object_t::value_type(
  19561. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  19562. }
  19563. else
  19564. {
  19565. push_back(basic_json(init));
  19566. }
  19567. }
  19568. /// @brief add an object to an object
  19569. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19570. reference operator+=(initializer_list_t init)
  19571. {
  19572. push_back(init);
  19573. return *this;
  19574. }
  19575. /// @brief add an object to an array
  19576. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  19577. template<class... Args>
  19578. reference emplace_back(Args&& ... args)
  19579. {
  19580. // emplace_back only works for null objects or arrays
  19581. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19582. {
  19583. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  19584. }
  19585. // transform null object into an array
  19586. if (is_null())
  19587. {
  19588. m_data.m_type = value_t::array;
  19589. m_data.m_value = value_t::array;
  19590. assert_invariant();
  19591. }
  19592. // add element to array (perfect forwarding)
  19593. const auto old_capacity = m_data.m_value.array->capacity();
  19594. m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
  19595. return set_parent(m_data.m_value.array->back(), old_capacity);
  19596. }
  19597. /// @brief add an object to an object if key does not exist
  19598. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  19599. template<class... Args>
  19600. std::pair<iterator, bool> emplace(Args&& ... args)
  19601. {
  19602. // emplace only works for null objects or arrays
  19603. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19604. {
  19605. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  19606. }
  19607. // transform null object into an object
  19608. if (is_null())
  19609. {
  19610. m_data.m_type = value_t::object;
  19611. m_data.m_value = value_t::object;
  19612. assert_invariant();
  19613. }
  19614. // add element to array (perfect forwarding)
  19615. auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
  19616. set_parent(res.first->second);
  19617. // create result iterator and set iterator to the result of emplace
  19618. auto it = begin();
  19619. it.m_it.object_iterator = res.first;
  19620. // return pair of iterator and boolean
  19621. return {it, res.second};
  19622. }
  19623. /// Helper for insertion of an iterator
  19624. /// @note: This uses std::distance to support GCC 4.8,
  19625. /// see https://github.com/nlohmann/json/pull/1257
  19626. template<typename... Args>
  19627. iterator insert_iterator(const_iterator pos, Args&& ... args)
  19628. {
  19629. iterator result(this);
  19630. JSON_ASSERT(m_data.m_value.array != nullptr);
  19631. auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
  19632. m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  19633. result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
  19634. // This could have been written as:
  19635. // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  19636. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  19637. set_parents();
  19638. return result;
  19639. }
  19640. /// @brief inserts element into array
  19641. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19642. iterator insert(const_iterator pos, const basic_json& val)
  19643. {
  19644. // insert only works for arrays
  19645. if (JSON_HEDLEY_LIKELY(is_array()))
  19646. {
  19647. // check if iterator pos fits to this JSON value
  19648. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19649. {
  19650. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19651. }
  19652. // insert to array and return iterator
  19653. return insert_iterator(pos, val);
  19654. }
  19655. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19656. }
  19657. /// @brief inserts element into array
  19658. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19659. iterator insert(const_iterator pos, basic_json&& val)
  19660. {
  19661. return insert(pos, val);
  19662. }
  19663. /// @brief inserts copies of element into array
  19664. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19665. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  19666. {
  19667. // insert only works for arrays
  19668. if (JSON_HEDLEY_LIKELY(is_array()))
  19669. {
  19670. // check if iterator pos fits to this JSON value
  19671. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19672. {
  19673. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19674. }
  19675. // insert to array and return iterator
  19676. return insert_iterator(pos, cnt, val);
  19677. }
  19678. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19679. }
  19680. /// @brief inserts range of elements into array
  19681. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19682. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  19683. {
  19684. // insert only works for arrays
  19685. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19686. {
  19687. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19688. }
  19689. // check if iterator pos fits to this JSON value
  19690. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19691. {
  19692. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19693. }
  19694. // check if range iterators belong to the same JSON object
  19695. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19696. {
  19697. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19698. }
  19699. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  19700. {
  19701. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  19702. }
  19703. // insert to array and return iterator
  19704. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  19705. }
  19706. /// @brief inserts elements from initializer list into array
  19707. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19708. iterator insert(const_iterator pos, initializer_list_t ilist)
  19709. {
  19710. // insert only works for arrays
  19711. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19712. {
  19713. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19714. }
  19715. // check if iterator pos fits to this JSON value
  19716. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19717. {
  19718. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19719. }
  19720. // insert to array and return iterator
  19721. return insert_iterator(pos, ilist.begin(), ilist.end());
  19722. }
  19723. /// @brief inserts range of elements into object
  19724. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19725. void insert(const_iterator first, const_iterator last)
  19726. {
  19727. // insert only works for objects
  19728. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19729. {
  19730. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19731. }
  19732. // check if range iterators belong to the same JSON object
  19733. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19734. {
  19735. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19736. }
  19737. // passed iterators must belong to objects
  19738. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19739. {
  19740. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  19741. }
  19742. m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  19743. }
  19744. /// @brief updates a JSON object from another object, overwriting existing keys
  19745. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19746. void update(const_reference j, bool merge_objects = false)
  19747. {
  19748. update(j.begin(), j.end(), merge_objects);
  19749. }
  19750. /// @brief updates a JSON object from another object, overwriting existing keys
  19751. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19752. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  19753. {
  19754. // implicitly convert null value to an empty object
  19755. if (is_null())
  19756. {
  19757. m_data.m_type = value_t::object;
  19758. m_data.m_value.object = create<object_t>();
  19759. assert_invariant();
  19760. }
  19761. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19762. {
  19763. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  19764. }
  19765. // check if range iterators belong to the same JSON object
  19766. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19767. {
  19768. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19769. }
  19770. // passed iterators must belong to objects
  19771. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19772. {
  19773. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  19774. }
  19775. for (auto it = first; it != last; ++it)
  19776. {
  19777. if (merge_objects && it.value().is_object())
  19778. {
  19779. auto it2 = m_data.m_value.object->find(it.key());
  19780. if (it2 != m_data.m_value.object->end())
  19781. {
  19782. it2->second.update(it.value(), true);
  19783. continue;
  19784. }
  19785. }
  19786. m_data.m_value.object->operator[](it.key()) = it.value();
  19787. #if JSON_DIAGNOSTICS
  19788. m_data.m_value.object->operator[](it.key()).m_parent = this;
  19789. #endif
  19790. }
  19791. }
  19792. /// @brief exchanges the values
  19793. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19794. void swap(reference other) noexcept (
  19795. std::is_nothrow_move_constructible<value_t>::value&&
  19796. std::is_nothrow_move_assignable<value_t>::value&&
  19797. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19798. std::is_nothrow_move_assignable<json_value>::value
  19799. )
  19800. {
  19801. std::swap(m_data.m_type, other.m_data.m_type);
  19802. std::swap(m_data.m_value, other.m_data.m_value);
  19803. set_parents();
  19804. other.set_parents();
  19805. assert_invariant();
  19806. }
  19807. /// @brief exchanges the values
  19808. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19809. friend void swap(reference left, reference right) noexcept (
  19810. std::is_nothrow_move_constructible<value_t>::value&&
  19811. std::is_nothrow_move_assignable<value_t>::value&&
  19812. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19813. std::is_nothrow_move_assignable<json_value>::value
  19814. )
  19815. {
  19816. left.swap(right);
  19817. }
  19818. /// @brief exchanges the values
  19819. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19820. void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19821. {
  19822. // swap only works for arrays
  19823. if (JSON_HEDLEY_LIKELY(is_array()))
  19824. {
  19825. using std::swap;
  19826. swap(*(m_data.m_value.array), other);
  19827. }
  19828. else
  19829. {
  19830. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  19831. }
  19832. }
  19833. /// @brief exchanges the values
  19834. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19835. void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19836. {
  19837. // swap only works for objects
  19838. if (JSON_HEDLEY_LIKELY(is_object()))
  19839. {
  19840. using std::swap;
  19841. swap(*(m_data.m_value.object), other);
  19842. }
  19843. else
  19844. {
  19845. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  19846. }
  19847. }
  19848. /// @brief exchanges the values
  19849. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19850. void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19851. {
  19852. // swap only works for strings
  19853. if (JSON_HEDLEY_LIKELY(is_string()))
  19854. {
  19855. using std::swap;
  19856. swap(*(m_data.m_value.string), other);
  19857. }
  19858. else
  19859. {
  19860. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  19861. }
  19862. }
  19863. /// @brief exchanges the values
  19864. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19865. void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19866. {
  19867. // swap only works for strings
  19868. if (JSON_HEDLEY_LIKELY(is_binary()))
  19869. {
  19870. using std::swap;
  19871. swap(*(m_data.m_value.binary), other);
  19872. }
  19873. else
  19874. {
  19875. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  19876. }
  19877. }
  19878. /// @brief exchanges the values
  19879. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19880. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  19881. {
  19882. // swap only works for strings
  19883. if (JSON_HEDLEY_LIKELY(is_binary()))
  19884. {
  19885. using std::swap;
  19886. swap(*(m_data.m_value.binary), other);
  19887. }
  19888. else
  19889. {
  19890. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  19891. }
  19892. }
  19893. /// @}
  19894. //////////////////////////////////////////
  19895. // lexicographical comparison operators //
  19896. //////////////////////////////////////////
  19897. /// @name lexicographical comparison operators
  19898. /// @{
  19899. // note parentheses around operands are necessary; see
  19900. // https://github.com/nlohmann/json/issues/1530
  19901. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  19902. const auto lhs_type = lhs.type(); \
  19903. const auto rhs_type = rhs.type(); \
  19904. \
  19905. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  19906. { \
  19907. switch (lhs_type) \
  19908. { \
  19909. case value_t::array: \
  19910. return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
  19911. \
  19912. case value_t::object: \
  19913. return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
  19914. \
  19915. case value_t::null: \
  19916. return (null_result); \
  19917. \
  19918. case value_t::string: \
  19919. return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
  19920. \
  19921. case value_t::boolean: \
  19922. return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
  19923. \
  19924. case value_t::number_integer: \
  19925. return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
  19926. \
  19927. case value_t::number_unsigned: \
  19928. return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
  19929. \
  19930. case value_t::number_float: \
  19931. return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
  19932. \
  19933. case value_t::binary: \
  19934. return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
  19935. \
  19936. case value_t::discarded: \
  19937. default: \
  19938. return (unordered_result); \
  19939. } \
  19940. } \
  19941. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  19942. { \
  19943. return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
  19944. } \
  19945. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  19946. { \
  19947. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
  19948. } \
  19949. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  19950. { \
  19951. return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
  19952. } \
  19953. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  19954. { \
  19955. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
  19956. } \
  19957. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  19958. { \
  19959. return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
  19960. } \
  19961. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  19962. { \
  19963. return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
  19964. } \
  19965. else if(compares_unordered(lhs, rhs))\
  19966. {\
  19967. return (unordered_result);\
  19968. }\
  19969. \
  19970. return (default_result);
  19971. JSON_PRIVATE_UNLESS_TESTED:
  19972. // returns true if:
  19973. // - any operand is NaN and the other operand is of number type
  19974. // - any operand is discarded
  19975. // in legacy mode, discarded values are considered ordered if
  19976. // an operation is computed as an odd number of inverses of others
  19977. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  19978. {
  19979. if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
  19980. || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
  19981. {
  19982. return true;
  19983. }
  19984. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19985. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  19986. #else
  19987. static_cast<void>(inverse);
  19988. return lhs.is_discarded() || rhs.is_discarded();
  19989. #endif
  19990. }
  19991. private:
  19992. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  19993. {
  19994. return compares_unordered(*this, rhs, inverse);
  19995. }
  19996. public:
  19997. #if JSON_HAS_THREE_WAY_COMPARISON
  19998. /// @brief comparison: equal
  19999. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20000. bool operator==(const_reference rhs) const noexcept
  20001. {
  20002. #ifdef __GNUC__
  20003. #pragma GCC diagnostic push
  20004. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20005. #endif
  20006. const_reference lhs = *this;
  20007. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20008. #ifdef __GNUC__
  20009. #pragma GCC diagnostic pop
  20010. #endif
  20011. }
  20012. /// @brief comparison: equal
  20013. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20014. template<typename ScalarType>
  20015. requires std::is_scalar_v<ScalarType>
  20016. bool operator==(ScalarType rhs) const noexcept
  20017. {
  20018. return *this == basic_json(rhs);
  20019. }
  20020. /// @brief comparison: not equal
  20021. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20022. bool operator!=(const_reference rhs) const noexcept
  20023. {
  20024. if (compares_unordered(rhs, true))
  20025. {
  20026. return false;
  20027. }
  20028. return !operator==(rhs);
  20029. }
  20030. /// @brief comparison: 3-way
  20031. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20032. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  20033. {
  20034. const_reference lhs = *this;
  20035. // default_result is used if we cannot compare values. In that case,
  20036. // we compare types.
  20037. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  20038. std::partial_ordering::equivalent,
  20039. std::partial_ordering::unordered,
  20040. lhs_type <=> rhs_type) // *NOPAD*
  20041. }
  20042. /// @brief comparison: 3-way
  20043. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20044. template<typename ScalarType>
  20045. requires std::is_scalar_v<ScalarType>
  20046. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  20047. {
  20048. return *this <=> basic_json(rhs); // *NOPAD*
  20049. }
  20050. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20051. // all operators that are computed as an odd number of inverses of others
  20052. // need to be overloaded to emulate the legacy comparison behavior
  20053. /// @brief comparison: less than or equal
  20054. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20055. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20056. bool operator<=(const_reference rhs) const noexcept
  20057. {
  20058. if (compares_unordered(rhs, true))
  20059. {
  20060. return false;
  20061. }
  20062. return !(rhs < *this);
  20063. }
  20064. /// @brief comparison: less than or equal
  20065. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20066. template<typename ScalarType>
  20067. requires std::is_scalar_v<ScalarType>
  20068. bool operator<=(ScalarType rhs) const noexcept
  20069. {
  20070. return *this <= basic_json(rhs);
  20071. }
  20072. /// @brief comparison: greater than or equal
  20073. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20074. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20075. bool operator>=(const_reference rhs) const noexcept
  20076. {
  20077. if (compares_unordered(rhs, true))
  20078. {
  20079. return false;
  20080. }
  20081. return !(*this < rhs);
  20082. }
  20083. /// @brief comparison: greater than or equal
  20084. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20085. template<typename ScalarType>
  20086. requires std::is_scalar_v<ScalarType>
  20087. bool operator>=(ScalarType rhs) const noexcept
  20088. {
  20089. return *this >= basic_json(rhs);
  20090. }
  20091. #endif
  20092. #else
  20093. /// @brief comparison: equal
  20094. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20095. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20096. {
  20097. #ifdef __GNUC__
  20098. #pragma GCC diagnostic push
  20099. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20100. #endif
  20101. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20102. #ifdef __GNUC__
  20103. #pragma GCC diagnostic pop
  20104. #endif
  20105. }
  20106. /// @brief comparison: equal
  20107. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20108. template<typename ScalarType, typename std::enable_if<
  20109. std::is_scalar<ScalarType>::value, int>::type = 0>
  20110. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20111. {
  20112. return lhs == basic_json(rhs);
  20113. }
  20114. /// @brief comparison: equal
  20115. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20116. template<typename ScalarType, typename std::enable_if<
  20117. std::is_scalar<ScalarType>::value, int>::type = 0>
  20118. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20119. {
  20120. return basic_json(lhs) == rhs;
  20121. }
  20122. /// @brief comparison: not equal
  20123. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20124. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20125. {
  20126. if (compares_unordered(lhs, rhs, true))
  20127. {
  20128. return false;
  20129. }
  20130. return !(lhs == rhs);
  20131. }
  20132. /// @brief comparison: not equal
  20133. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20134. template<typename ScalarType, typename std::enable_if<
  20135. std::is_scalar<ScalarType>::value, int>::type = 0>
  20136. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20137. {
  20138. return lhs != basic_json(rhs);
  20139. }
  20140. /// @brief comparison: not equal
  20141. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20142. template<typename ScalarType, typename std::enable_if<
  20143. std::is_scalar<ScalarType>::value, int>::type = 0>
  20144. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20145. {
  20146. return basic_json(lhs) != rhs;
  20147. }
  20148. /// @brief comparison: less than
  20149. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20150. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20151. {
  20152. // default_result is used if we cannot compare values. In that case,
  20153. // we compare types. Note we have to call the operator explicitly,
  20154. // because MSVC has problems otherwise.
  20155. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20156. }
  20157. /// @brief comparison: less than
  20158. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20159. template<typename ScalarType, typename std::enable_if<
  20160. std::is_scalar<ScalarType>::value, int>::type = 0>
  20161. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20162. {
  20163. return lhs < basic_json(rhs);
  20164. }
  20165. /// @brief comparison: less than
  20166. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20167. template<typename ScalarType, typename std::enable_if<
  20168. std::is_scalar<ScalarType>::value, int>::type = 0>
  20169. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20170. {
  20171. return basic_json(lhs) < rhs;
  20172. }
  20173. /// @brief comparison: less than or equal
  20174. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20175. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20176. {
  20177. if (compares_unordered(lhs, rhs, true))
  20178. {
  20179. return false;
  20180. }
  20181. return !(rhs < lhs);
  20182. }
  20183. /// @brief comparison: less than or equal
  20184. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20185. template<typename ScalarType, typename std::enable_if<
  20186. std::is_scalar<ScalarType>::value, int>::type = 0>
  20187. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20188. {
  20189. return lhs <= basic_json(rhs);
  20190. }
  20191. /// @brief comparison: less than or equal
  20192. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20193. template<typename ScalarType, typename std::enable_if<
  20194. std::is_scalar<ScalarType>::value, int>::type = 0>
  20195. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20196. {
  20197. return basic_json(lhs) <= rhs;
  20198. }
  20199. /// @brief comparison: greater than
  20200. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20201. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20202. {
  20203. // double inverse
  20204. if (compares_unordered(lhs, rhs))
  20205. {
  20206. return false;
  20207. }
  20208. return !(lhs <= rhs);
  20209. }
  20210. /// @brief comparison: greater than
  20211. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20212. template<typename ScalarType, typename std::enable_if<
  20213. std::is_scalar<ScalarType>::value, int>::type = 0>
  20214. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  20215. {
  20216. return lhs > basic_json(rhs);
  20217. }
  20218. /// @brief comparison: greater than
  20219. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20220. template<typename ScalarType, typename std::enable_if<
  20221. std::is_scalar<ScalarType>::value, int>::type = 0>
  20222. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  20223. {
  20224. return basic_json(lhs) > rhs;
  20225. }
  20226. /// @brief comparison: greater than or equal
  20227. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20228. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  20229. {
  20230. if (compares_unordered(lhs, rhs, true))
  20231. {
  20232. return false;
  20233. }
  20234. return !(lhs < rhs);
  20235. }
  20236. /// @brief comparison: greater than or equal
  20237. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20238. template<typename ScalarType, typename std::enable_if<
  20239. std::is_scalar<ScalarType>::value, int>::type = 0>
  20240. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  20241. {
  20242. return lhs >= basic_json(rhs);
  20243. }
  20244. /// @brief comparison: greater than or equal
  20245. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20246. template<typename ScalarType, typename std::enable_if<
  20247. std::is_scalar<ScalarType>::value, int>::type = 0>
  20248. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  20249. {
  20250. return basic_json(lhs) >= rhs;
  20251. }
  20252. #endif
  20253. #undef JSON_IMPLEMENT_OPERATOR
  20254. /// @}
  20255. ///////////////////
  20256. // serialization //
  20257. ///////////////////
  20258. /// @name serialization
  20259. /// @{
  20260. #ifndef JSON_NO_IO
  20261. /// @brief serialize to stream
  20262. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20263. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20264. {
  20265. // read width member and use it as indentation parameter if nonzero
  20266. const bool pretty_print = o.width() > 0;
  20267. const auto indentation = pretty_print ? o.width() : 0;
  20268. // reset width to 0 for subsequent calls to this stream
  20269. o.width(0);
  20270. // do the actual serialization
  20271. serializer s(detail::output_adapter<char>(o), o.fill());
  20272. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20273. return o;
  20274. }
  20275. /// @brief serialize to stream
  20276. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20277. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  20278. /// version 4.0.0 of the library. Please use
  20279. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  20280. /// replace calls like `j >> o;` with `o << j;`.
  20281. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20282. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20283. {
  20284. return o << j;
  20285. }
  20286. #endif // JSON_NO_IO
  20287. /// @}
  20288. /////////////////////
  20289. // deserialization //
  20290. /////////////////////
  20291. /// @name deserialization
  20292. /// @{
  20293. /// @brief deserialize from a compatible input
  20294. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20295. template<typename InputType>
  20296. JSON_HEDLEY_WARN_UNUSED_RESULT
  20297. static basic_json parse(InputType&& i,
  20298. const parser_callback_t cb = nullptr,
  20299. const bool allow_exceptions = true,
  20300. const bool ignore_comments = false)
  20301. {
  20302. basic_json result;
  20303. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20304. return result;
  20305. }
  20306. /// @brief deserialize from a pair of character iterators
  20307. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20308. template<typename IteratorType>
  20309. JSON_HEDLEY_WARN_UNUSED_RESULT
  20310. static basic_json parse(IteratorType first,
  20311. IteratorType last,
  20312. const parser_callback_t cb = nullptr,
  20313. const bool allow_exceptions = true,
  20314. const bool ignore_comments = false)
  20315. {
  20316. basic_json result;
  20317. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20318. return result;
  20319. }
  20320. JSON_HEDLEY_WARN_UNUSED_RESULT
  20321. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20322. static basic_json parse(detail::span_input_adapter&& i,
  20323. const parser_callback_t cb = nullptr,
  20324. const bool allow_exceptions = true,
  20325. const bool ignore_comments = false)
  20326. {
  20327. basic_json result;
  20328. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  20329. return result;
  20330. }
  20331. /// @brief check if the input is valid JSON
  20332. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20333. template<typename InputType>
  20334. static bool accept(InputType&& i,
  20335. const bool ignore_comments = false)
  20336. {
  20337. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20338. }
  20339. /// @brief check if the input is valid JSON
  20340. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20341. template<typename IteratorType>
  20342. static bool accept(IteratorType first, IteratorType last,
  20343. const bool ignore_comments = false)
  20344. {
  20345. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  20346. }
  20347. JSON_HEDLEY_WARN_UNUSED_RESULT
  20348. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  20349. static bool accept(detail::span_input_adapter&& i,
  20350. const bool ignore_comments = false)
  20351. {
  20352. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  20353. }
  20354. /// @brief generate SAX events
  20355. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20356. template <typename InputType, typename SAX>
  20357. JSON_HEDLEY_NON_NULL(2)
  20358. static bool sax_parse(InputType&& i, SAX* sax,
  20359. input_format_t format = input_format_t::json,
  20360. const bool strict = true,
  20361. const bool ignore_comments = false)
  20362. {
  20363. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20364. return format == input_format_t::json
  20365. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20366. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20367. }
  20368. /// @brief generate SAX events
  20369. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20370. template<class IteratorType, class SAX>
  20371. JSON_HEDLEY_NON_NULL(3)
  20372. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  20373. input_format_t format = input_format_t::json,
  20374. const bool strict = true,
  20375. const bool ignore_comments = false)
  20376. {
  20377. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20378. return format == input_format_t::json
  20379. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20380. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20381. }
  20382. /// @brief generate SAX events
  20383. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20384. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  20385. /// version 4.0.0 of the library. Please use
  20386. /// sax_parse(ptr, ptr + len) instead.
  20387. template <typename SAX>
  20388. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  20389. JSON_HEDLEY_NON_NULL(2)
  20390. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  20391. input_format_t format = input_format_t::json,
  20392. const bool strict = true,
  20393. const bool ignore_comments = false)
  20394. {
  20395. auto ia = i.get();
  20396. return format == input_format_t::json
  20397. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20398. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20399. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20400. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20401. }
  20402. #ifndef JSON_NO_IO
  20403. /// @brief deserialize from stream
  20404. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20405. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  20406. /// version 4.0.0 of the library. Please use
  20407. /// operator>>(std::istream&, basic_json&) instead; that is,
  20408. /// replace calls like `j << i;` with `i >> j;`.
  20409. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  20410. friend std::istream& operator<<(basic_json& j, std::istream& i)
  20411. {
  20412. return operator>>(i, j);
  20413. }
  20414. /// @brief deserialize from stream
  20415. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20416. friend std::istream& operator>>(std::istream& i, basic_json& j)
  20417. {
  20418. parser(detail::input_adapter(i)).parse(false, j);
  20419. return i;
  20420. }
  20421. #endif // JSON_NO_IO
  20422. /// @}
  20423. ///////////////////////////
  20424. // convenience functions //
  20425. ///////////////////////////
  20426. /// @brief return the type as string
  20427. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  20428. JSON_HEDLEY_RETURNS_NON_NULL
  20429. const char* type_name() const noexcept
  20430. {
  20431. switch (m_data.m_type)
  20432. {
  20433. case value_t::null:
  20434. return "null";
  20435. case value_t::object:
  20436. return "object";
  20437. case value_t::array:
  20438. return "array";
  20439. case value_t::string:
  20440. return "string";
  20441. case value_t::boolean:
  20442. return "boolean";
  20443. case value_t::binary:
  20444. return "binary";
  20445. case value_t::discarded:
  20446. return "discarded";
  20447. case value_t::number_integer:
  20448. case value_t::number_unsigned:
  20449. case value_t::number_float:
  20450. default:
  20451. return "number";
  20452. }
  20453. }
  20454. JSON_PRIVATE_UNLESS_TESTED:
  20455. //////////////////////
  20456. // member variables //
  20457. //////////////////////
  20458. struct data
  20459. {
  20460. /// the type of the current element
  20461. value_t m_type = value_t::null;
  20462. /// the value of the current element
  20463. json_value m_value = {};
  20464. data(const value_t v)
  20465. : m_type(v), m_value(v)
  20466. {
  20467. }
  20468. data(size_type cnt, const basic_json& val)
  20469. : m_type(value_t::array)
  20470. {
  20471. m_value.array = create<array_t>(cnt, val);
  20472. }
  20473. data() noexcept = default;
  20474. data(data&&) noexcept = default;
  20475. data(const data&) noexcept = delete;
  20476. data& operator=(data&&) noexcept = delete;
  20477. data& operator=(const data&) noexcept = delete;
  20478. ~data() noexcept
  20479. {
  20480. m_value.destroy(m_type);
  20481. }
  20482. };
  20483. data m_data = {};
  20484. #if JSON_DIAGNOSTICS
  20485. /// a pointer to a parent value (for debugging purposes)
  20486. basic_json* m_parent = nullptr;
  20487. #endif
  20488. //////////////////////////////////////////
  20489. // binary serialization/deserialization //
  20490. //////////////////////////////////////////
  20491. /// @name binary serialization/deserialization support
  20492. /// @{
  20493. public:
  20494. /// @brief create a CBOR serialization of a given JSON value
  20495. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20496. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  20497. {
  20498. std::vector<std::uint8_t> result;
  20499. to_cbor(j, result);
  20500. return result;
  20501. }
  20502. /// @brief create a CBOR serialization of a given JSON value
  20503. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20504. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20505. {
  20506. binary_writer<std::uint8_t>(o).write_cbor(j);
  20507. }
  20508. /// @brief create a CBOR serialization of a given JSON value
  20509. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20510. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  20511. {
  20512. binary_writer<char>(o).write_cbor(j);
  20513. }
  20514. /// @brief create a MessagePack serialization of a given JSON value
  20515. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20516. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  20517. {
  20518. std::vector<std::uint8_t> result;
  20519. to_msgpack(j, result);
  20520. return result;
  20521. }
  20522. /// @brief create a MessagePack serialization of a given JSON value
  20523. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20524. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20525. {
  20526. binary_writer<std::uint8_t>(o).write_msgpack(j);
  20527. }
  20528. /// @brief create a MessagePack serialization of a given JSON value
  20529. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20530. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  20531. {
  20532. binary_writer<char>(o).write_msgpack(j);
  20533. }
  20534. /// @brief create a UBJSON serialization of a given JSON value
  20535. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20536. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  20537. const bool use_size = false,
  20538. const bool use_type = false)
  20539. {
  20540. std::vector<std::uint8_t> result;
  20541. to_ubjson(j, result, use_size, use_type);
  20542. return result;
  20543. }
  20544. /// @brief create a UBJSON serialization of a given JSON value
  20545. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20546. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20547. const bool use_size = false, const bool use_type = false)
  20548. {
  20549. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  20550. }
  20551. /// @brief create a UBJSON serialization of a given JSON value
  20552. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20553. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  20554. const bool use_size = false, const bool use_type = false)
  20555. {
  20556. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  20557. }
  20558. /// @brief create a BJData serialization of a given JSON value
  20559. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20560. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  20561. const bool use_size = false,
  20562. const bool use_type = false)
  20563. {
  20564. std::vector<std::uint8_t> result;
  20565. to_bjdata(j, result, use_size, use_type);
  20566. return result;
  20567. }
  20568. /// @brief create a BJData serialization of a given JSON value
  20569. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20570. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20571. const bool use_size = false, const bool use_type = false)
  20572. {
  20573. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  20574. }
  20575. /// @brief create a BJData serialization of a given JSON value
  20576. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20577. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  20578. const bool use_size = false, const bool use_type = false)
  20579. {
  20580. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  20581. }
  20582. /// @brief create a BSON serialization of a given JSON value
  20583. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20584. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  20585. {
  20586. std::vector<std::uint8_t> result;
  20587. to_bson(j, result);
  20588. return result;
  20589. }
  20590. /// @brief create a BSON serialization of a given JSON value
  20591. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20592. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20593. {
  20594. binary_writer<std::uint8_t>(o).write_bson(j);
  20595. }
  20596. /// @brief create a BSON serialization of a given JSON value
  20597. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20598. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  20599. {
  20600. binary_writer<char>(o).write_bson(j);
  20601. }
  20602. /// @brief create a JSON value from an input in CBOR format
  20603. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20604. template<typename InputType>
  20605. JSON_HEDLEY_WARN_UNUSED_RESULT
  20606. static basic_json from_cbor(InputType&& i,
  20607. const bool strict = true,
  20608. const bool allow_exceptions = true,
  20609. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20610. {
  20611. basic_json result;
  20612. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20613. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20614. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20615. return res ? result : basic_json(value_t::discarded);
  20616. }
  20617. /// @brief create a JSON value from an input in CBOR format
  20618. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20619. template<typename IteratorType>
  20620. JSON_HEDLEY_WARN_UNUSED_RESULT
  20621. static basic_json from_cbor(IteratorType first, IteratorType last,
  20622. const bool strict = true,
  20623. const bool allow_exceptions = true,
  20624. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20625. {
  20626. basic_json result;
  20627. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20628. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20629. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20630. return res ? result : basic_json(value_t::discarded);
  20631. }
  20632. template<typename T>
  20633. JSON_HEDLEY_WARN_UNUSED_RESULT
  20634. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20635. static basic_json from_cbor(const T* ptr, std::size_t len,
  20636. const bool strict = true,
  20637. const bool allow_exceptions = true,
  20638. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20639. {
  20640. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  20641. }
  20642. JSON_HEDLEY_WARN_UNUSED_RESULT
  20643. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20644. static basic_json from_cbor(detail::span_input_adapter&& i,
  20645. const bool strict = true,
  20646. const bool allow_exceptions = true,
  20647. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20648. {
  20649. basic_json result;
  20650. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20651. auto ia = i.get();
  20652. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20653. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20654. return res ? result : basic_json(value_t::discarded);
  20655. }
  20656. /// @brief create a JSON value from an input in MessagePack format
  20657. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20658. template<typename InputType>
  20659. JSON_HEDLEY_WARN_UNUSED_RESULT
  20660. static basic_json from_msgpack(InputType&& i,
  20661. const bool strict = true,
  20662. const bool allow_exceptions = true)
  20663. {
  20664. basic_json result;
  20665. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20666. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20667. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20668. return res ? result : basic_json(value_t::discarded);
  20669. }
  20670. /// @brief create a JSON value from an input in MessagePack format
  20671. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20672. template<typename IteratorType>
  20673. JSON_HEDLEY_WARN_UNUSED_RESULT
  20674. static basic_json from_msgpack(IteratorType first, IteratorType last,
  20675. const bool strict = true,
  20676. const bool allow_exceptions = true)
  20677. {
  20678. basic_json result;
  20679. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20680. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20681. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20682. return res ? result : basic_json(value_t::discarded);
  20683. }
  20684. template<typename T>
  20685. JSON_HEDLEY_WARN_UNUSED_RESULT
  20686. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20687. static basic_json from_msgpack(const T* ptr, std::size_t len,
  20688. const bool strict = true,
  20689. const bool allow_exceptions = true)
  20690. {
  20691. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  20692. }
  20693. JSON_HEDLEY_WARN_UNUSED_RESULT
  20694. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20695. static basic_json from_msgpack(detail::span_input_adapter&& i,
  20696. const bool strict = true,
  20697. const bool allow_exceptions = true)
  20698. {
  20699. basic_json result;
  20700. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20701. auto ia = i.get();
  20702. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20703. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20704. return res ? result : basic_json(value_t::discarded);
  20705. }
  20706. /// @brief create a JSON value from an input in UBJSON format
  20707. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20708. template<typename InputType>
  20709. JSON_HEDLEY_WARN_UNUSED_RESULT
  20710. static basic_json from_ubjson(InputType&& i,
  20711. const bool strict = true,
  20712. const bool allow_exceptions = true)
  20713. {
  20714. basic_json result;
  20715. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20716. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20717. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20718. return res ? result : basic_json(value_t::discarded);
  20719. }
  20720. /// @brief create a JSON value from an input in UBJSON format
  20721. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20722. template<typename IteratorType>
  20723. JSON_HEDLEY_WARN_UNUSED_RESULT
  20724. static basic_json from_ubjson(IteratorType first, IteratorType last,
  20725. const bool strict = true,
  20726. const bool allow_exceptions = true)
  20727. {
  20728. basic_json result;
  20729. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20730. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20731. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20732. return res ? result : basic_json(value_t::discarded);
  20733. }
  20734. template<typename T>
  20735. JSON_HEDLEY_WARN_UNUSED_RESULT
  20736. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20737. static basic_json from_ubjson(const T* ptr, std::size_t len,
  20738. const bool strict = true,
  20739. const bool allow_exceptions = true)
  20740. {
  20741. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  20742. }
  20743. JSON_HEDLEY_WARN_UNUSED_RESULT
  20744. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20745. static basic_json from_ubjson(detail::span_input_adapter&& i,
  20746. const bool strict = true,
  20747. const bool allow_exceptions = true)
  20748. {
  20749. basic_json result;
  20750. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20751. auto ia = i.get();
  20752. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20753. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20754. return res ? result : basic_json(value_t::discarded);
  20755. }
  20756. /// @brief create a JSON value from an input in BJData format
  20757. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20758. template<typename InputType>
  20759. JSON_HEDLEY_WARN_UNUSED_RESULT
  20760. static basic_json from_bjdata(InputType&& i,
  20761. const bool strict = true,
  20762. const bool allow_exceptions = true)
  20763. {
  20764. basic_json result;
  20765. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20766. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20767. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20768. return res ? result : basic_json(value_t::discarded);
  20769. }
  20770. /// @brief create a JSON value from an input in BJData format
  20771. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20772. template<typename IteratorType>
  20773. JSON_HEDLEY_WARN_UNUSED_RESULT
  20774. static basic_json from_bjdata(IteratorType first, IteratorType last,
  20775. const bool strict = true,
  20776. const bool allow_exceptions = true)
  20777. {
  20778. basic_json result;
  20779. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20780. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20781. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20782. return res ? result : basic_json(value_t::discarded);
  20783. }
  20784. /// @brief create a JSON value from an input in BSON format
  20785. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20786. template<typename InputType>
  20787. JSON_HEDLEY_WARN_UNUSED_RESULT
  20788. static basic_json from_bson(InputType&& i,
  20789. const bool strict = true,
  20790. const bool allow_exceptions = true)
  20791. {
  20792. basic_json result;
  20793. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20794. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20795. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20796. return res ? result : basic_json(value_t::discarded);
  20797. }
  20798. /// @brief create a JSON value from an input in BSON format
  20799. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20800. template<typename IteratorType>
  20801. JSON_HEDLEY_WARN_UNUSED_RESULT
  20802. static basic_json from_bson(IteratorType first, IteratorType last,
  20803. const bool strict = true,
  20804. const bool allow_exceptions = true)
  20805. {
  20806. basic_json result;
  20807. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20808. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20809. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20810. return res ? result : basic_json(value_t::discarded);
  20811. }
  20812. template<typename T>
  20813. JSON_HEDLEY_WARN_UNUSED_RESULT
  20814. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20815. static basic_json from_bson(const T* ptr, std::size_t len,
  20816. const bool strict = true,
  20817. const bool allow_exceptions = true)
  20818. {
  20819. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  20820. }
  20821. JSON_HEDLEY_WARN_UNUSED_RESULT
  20822. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20823. static basic_json from_bson(detail::span_input_adapter&& i,
  20824. const bool strict = true,
  20825. const bool allow_exceptions = true)
  20826. {
  20827. basic_json result;
  20828. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20829. auto ia = i.get();
  20830. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20831. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20832. return res ? result : basic_json(value_t::discarded);
  20833. }
  20834. /// @}
  20835. //////////////////////////
  20836. // JSON Pointer support //
  20837. //////////////////////////
  20838. /// @name JSON Pointer functions
  20839. /// @{
  20840. /// @brief access specified element via JSON Pointer
  20841. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20842. reference operator[](const json_pointer& ptr)
  20843. {
  20844. return ptr.get_unchecked(this);
  20845. }
  20846. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20847. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20848. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20849. {
  20850. return ptr.get_unchecked(this);
  20851. }
  20852. /// @brief access specified element via JSON Pointer
  20853. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20854. const_reference operator[](const json_pointer& ptr) const
  20855. {
  20856. return ptr.get_unchecked(this);
  20857. }
  20858. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20859. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20860. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20861. {
  20862. return ptr.get_unchecked(this);
  20863. }
  20864. /// @brief access specified element via JSON Pointer
  20865. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20866. reference at(const json_pointer& ptr)
  20867. {
  20868. return ptr.get_checked(this);
  20869. }
  20870. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20871. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20872. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20873. {
  20874. return ptr.get_checked(this);
  20875. }
  20876. /// @brief access specified element via JSON Pointer
  20877. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20878. const_reference at(const json_pointer& ptr) const
  20879. {
  20880. return ptr.get_checked(this);
  20881. }
  20882. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20883. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20884. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20885. {
  20886. return ptr.get_checked(this);
  20887. }
  20888. /// @brief return flattened JSON value
  20889. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  20890. basic_json flatten() const
  20891. {
  20892. basic_json result(value_t::object);
  20893. json_pointer::flatten("", *this, result);
  20894. return result;
  20895. }
  20896. /// @brief unflatten a previously flattened JSON value
  20897. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  20898. basic_json unflatten() const
  20899. {
  20900. return json_pointer::unflatten(*this);
  20901. }
  20902. /// @}
  20903. //////////////////////////
  20904. // JSON Patch functions //
  20905. //////////////////////////
  20906. /// @name JSON Patch functions
  20907. /// @{
  20908. /// @brief applies a JSON patch in-place without copying the object
  20909. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20910. void patch_inplace(const basic_json& json_patch)
  20911. {
  20912. basic_json& result = *this;
  20913. // the valid JSON Patch operations
  20914. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  20915. const auto get_op = [](const std::string & op)
  20916. {
  20917. if (op == "add")
  20918. {
  20919. return patch_operations::add;
  20920. }
  20921. if (op == "remove")
  20922. {
  20923. return patch_operations::remove;
  20924. }
  20925. if (op == "replace")
  20926. {
  20927. return patch_operations::replace;
  20928. }
  20929. if (op == "move")
  20930. {
  20931. return patch_operations::move;
  20932. }
  20933. if (op == "copy")
  20934. {
  20935. return patch_operations::copy;
  20936. }
  20937. if (op == "test")
  20938. {
  20939. return patch_operations::test;
  20940. }
  20941. return patch_operations::invalid;
  20942. };
  20943. // wrapper for "add" operation; add value at ptr
  20944. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  20945. {
  20946. // adding to the root of the target document means replacing it
  20947. if (ptr.empty())
  20948. {
  20949. result = val;
  20950. return;
  20951. }
  20952. // make sure the top element of the pointer exists
  20953. json_pointer const top_pointer = ptr.top();
  20954. if (top_pointer != ptr)
  20955. {
  20956. result.at(top_pointer);
  20957. }
  20958. // get reference to parent of JSON pointer ptr
  20959. const auto last_path = ptr.back();
  20960. ptr.pop_back();
  20961. // parent must exist when performing patch add per RFC6902 specs
  20962. basic_json& parent = result.at(ptr);
  20963. switch (parent.m_data.m_type)
  20964. {
  20965. case value_t::null:
  20966. case value_t::object:
  20967. {
  20968. // use operator[] to add value
  20969. parent[last_path] = val;
  20970. break;
  20971. }
  20972. case value_t::array:
  20973. {
  20974. if (last_path == "-")
  20975. {
  20976. // special case: append to back
  20977. parent.push_back(val);
  20978. }
  20979. else
  20980. {
  20981. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  20982. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  20983. {
  20984. // avoid undefined behavior
  20985. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  20986. }
  20987. // default case: insert add offset
  20988. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  20989. }
  20990. break;
  20991. }
  20992. // if there exists a parent it cannot be primitive
  20993. case value_t::string: // LCOV_EXCL_LINE
  20994. case value_t::boolean: // LCOV_EXCL_LINE
  20995. case value_t::number_integer: // LCOV_EXCL_LINE
  20996. case value_t::number_unsigned: // LCOV_EXCL_LINE
  20997. case value_t::number_float: // LCOV_EXCL_LINE
  20998. case value_t::binary: // LCOV_EXCL_LINE
  20999. case value_t::discarded: // LCOV_EXCL_LINE
  21000. default: // LCOV_EXCL_LINE
  21001. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  21002. }
  21003. };
  21004. // wrapper for "remove" operation; remove value at ptr
  21005. const auto operation_remove = [this, & result](json_pointer & ptr)
  21006. {
  21007. // get reference to parent of JSON pointer ptr
  21008. const auto last_path = ptr.back();
  21009. ptr.pop_back();
  21010. basic_json& parent = result.at(ptr);
  21011. // remove child
  21012. if (parent.is_object())
  21013. {
  21014. // perform range check
  21015. auto it = parent.find(last_path);
  21016. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  21017. {
  21018. parent.erase(it);
  21019. }
  21020. else
  21021. {
  21022. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  21023. }
  21024. }
  21025. else if (parent.is_array())
  21026. {
  21027. // note erase performs range check
  21028. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  21029. }
  21030. };
  21031. // type check: top level value must be an array
  21032. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  21033. {
  21034. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  21035. }
  21036. // iterate and apply the operations
  21037. for (const auto& val : json_patch)
  21038. {
  21039. // wrapper to get a value for an operation
  21040. const auto get_value = [&val](const std::string & op,
  21041. const std::string & member,
  21042. bool string_type) -> basic_json &
  21043. {
  21044. // find value
  21045. auto it = val.m_data.m_value.object->find(member);
  21046. // context-sensitive error message
  21047. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
  21048. // check if desired value is present
  21049. if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
  21050. {
  21051. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21052. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  21053. }
  21054. // check if result is of type string
  21055. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  21056. {
  21057. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21058. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  21059. }
  21060. // no error: return value
  21061. return it->second;
  21062. };
  21063. // type check: every element of the array must be an object
  21064. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  21065. {
  21066. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  21067. }
  21068. // collect mandatory members
  21069. const auto op = get_value("op", "op", true).template get<std::string>();
  21070. const auto path = get_value(op, "path", true).template get<std::string>();
  21071. json_pointer ptr(path);
  21072. switch (get_op(op))
  21073. {
  21074. case patch_operations::add:
  21075. {
  21076. operation_add(ptr, get_value("add", "value", false));
  21077. break;
  21078. }
  21079. case patch_operations::remove:
  21080. {
  21081. operation_remove(ptr);
  21082. break;
  21083. }
  21084. case patch_operations::replace:
  21085. {
  21086. // the "path" location must exist - use at()
  21087. result.at(ptr) = get_value("replace", "value", false);
  21088. break;
  21089. }
  21090. case patch_operations::move:
  21091. {
  21092. const auto from_path = get_value("move", "from", true).template get<std::string>();
  21093. json_pointer from_ptr(from_path);
  21094. // the "from" location must exist - use at()
  21095. basic_json const v = result.at(from_ptr);
  21096. // The move operation is functionally identical to a
  21097. // "remove" operation on the "from" location, followed
  21098. // immediately by an "add" operation at the target
  21099. // location with the value that was just removed.
  21100. operation_remove(from_ptr);
  21101. operation_add(ptr, v);
  21102. break;
  21103. }
  21104. case patch_operations::copy:
  21105. {
  21106. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  21107. const json_pointer from_ptr(from_path);
  21108. // the "from" location must exist - use at()
  21109. basic_json const v = result.at(from_ptr);
  21110. // The copy is functionally identical to an "add"
  21111. // operation at the target location using the value
  21112. // specified in the "from" member.
  21113. operation_add(ptr, v);
  21114. break;
  21115. }
  21116. case patch_operations::test:
  21117. {
  21118. bool success = false;
  21119. JSON_TRY
  21120. {
  21121. // check if "value" matches the one at "path"
  21122. // the "path" location must exist - use at()
  21123. success = (result.at(ptr) == get_value("test", "value", false));
  21124. }
  21125. JSON_INTERNAL_CATCH (out_of_range&)
  21126. {
  21127. // ignore out of range errors: success remains false
  21128. }
  21129. // throw an exception if test fails
  21130. if (JSON_HEDLEY_UNLIKELY(!success))
  21131. {
  21132. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  21133. }
  21134. break;
  21135. }
  21136. case patch_operations::invalid:
  21137. default:
  21138. {
  21139. // op must be "add", "remove", "replace", "move", "copy", or
  21140. // "test"
  21141. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  21142. }
  21143. }
  21144. }
  21145. }
  21146. /// @brief applies a JSON patch to a copy of the current object
  21147. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21148. basic_json patch(const basic_json& json_patch) const
  21149. {
  21150. basic_json result = *this;
  21151. result.patch_inplace(json_patch);
  21152. return result;
  21153. }
  21154. /// @brief creates a diff as a JSON patch
  21155. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  21156. JSON_HEDLEY_WARN_UNUSED_RESULT
  21157. static basic_json diff(const basic_json& source, const basic_json& target,
  21158. const std::string& path = "")
  21159. {
  21160. // the patch
  21161. basic_json result(value_t::array);
  21162. // if the values are the same, return empty patch
  21163. if (source == target)
  21164. {
  21165. return result;
  21166. }
  21167. if (source.type() != target.type())
  21168. {
  21169. // different types: replace value
  21170. result.push_back(
  21171. {
  21172. {"op", "replace"}, {"path", path}, {"value", target}
  21173. });
  21174. return result;
  21175. }
  21176. switch (source.type())
  21177. {
  21178. case value_t::array:
  21179. {
  21180. // first pass: traverse common elements
  21181. std::size_t i = 0;
  21182. while (i < source.size() && i < target.size())
  21183. {
  21184. // recursive call to compare array values at index i
  21185. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  21186. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21187. ++i;
  21188. }
  21189. // We now reached the end of at least one array
  21190. // in a second pass, traverse the remaining elements
  21191. // remove my remaining elements
  21192. const auto end_index = static_cast<difference_type>(result.size());
  21193. while (i < source.size())
  21194. {
  21195. // add operations in reverse order to avoid invalid
  21196. // indices
  21197. result.insert(result.begin() + end_index, object(
  21198. {
  21199. {"op", "remove"},
  21200. {"path", detail::concat(path, '/', std::to_string(i))}
  21201. }));
  21202. ++i;
  21203. }
  21204. // add other remaining elements
  21205. while (i < target.size())
  21206. {
  21207. result.push_back(
  21208. {
  21209. {"op", "add"},
  21210. {"path", detail::concat(path, "/-")},
  21211. {"value", target[i]}
  21212. });
  21213. ++i;
  21214. }
  21215. break;
  21216. }
  21217. case value_t::object:
  21218. {
  21219. // first pass: traverse this object's elements
  21220. for (auto it = source.cbegin(); it != source.cend(); ++it)
  21221. {
  21222. // escape the key name to be used in a JSON patch
  21223. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21224. if (target.find(it.key()) != target.end())
  21225. {
  21226. // recursive call to compare object values at key it
  21227. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  21228. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21229. }
  21230. else
  21231. {
  21232. // found a key that is not in o -> remove it
  21233. result.push_back(object(
  21234. {
  21235. {"op", "remove"}, {"path", path_key}
  21236. }));
  21237. }
  21238. }
  21239. // second pass: traverse other object's elements
  21240. for (auto it = target.cbegin(); it != target.cend(); ++it)
  21241. {
  21242. if (source.find(it.key()) == source.end())
  21243. {
  21244. // found a key that is not in this -> add it
  21245. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21246. result.push_back(
  21247. {
  21248. {"op", "add"}, {"path", path_key},
  21249. {"value", it.value()}
  21250. });
  21251. }
  21252. }
  21253. break;
  21254. }
  21255. case value_t::null:
  21256. case value_t::string:
  21257. case value_t::boolean:
  21258. case value_t::number_integer:
  21259. case value_t::number_unsigned:
  21260. case value_t::number_float:
  21261. case value_t::binary:
  21262. case value_t::discarded:
  21263. default:
  21264. {
  21265. // both primitive type: replace value
  21266. result.push_back(
  21267. {
  21268. {"op", "replace"}, {"path", path}, {"value", target}
  21269. });
  21270. break;
  21271. }
  21272. }
  21273. return result;
  21274. }
  21275. /// @}
  21276. ////////////////////////////////
  21277. // JSON Merge Patch functions //
  21278. ////////////////////////////////
  21279. /// @name JSON Merge Patch functions
  21280. /// @{
  21281. /// @brief applies a JSON Merge Patch
  21282. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  21283. void merge_patch(const basic_json& apply_patch)
  21284. {
  21285. if (apply_patch.is_object())
  21286. {
  21287. if (!is_object())
  21288. {
  21289. *this = object();
  21290. }
  21291. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  21292. {
  21293. if (it.value().is_null())
  21294. {
  21295. erase(it.key());
  21296. }
  21297. else
  21298. {
  21299. operator[](it.key()).merge_patch(it.value());
  21300. }
  21301. }
  21302. }
  21303. else
  21304. {
  21305. *this = apply_patch;
  21306. }
  21307. }
  21308. /// @}
  21309. };
  21310. /// @brief user-defined to_string function for JSON values
  21311. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  21312. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21313. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  21314. {
  21315. return j.dump();
  21316. }
  21317. inline namespace literals
  21318. {
  21319. inline namespace json_literals
  21320. {
  21321. /// @brief user-defined string literal for JSON values
  21322. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  21323. JSON_HEDLEY_NON_NULL(1)
  21324. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21325. inline nlohmann::json operator ""_json(const char* s, std::size_t n)
  21326. #else
  21327. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  21328. #endif
  21329. {
  21330. return nlohmann::json::parse(s, s + n);
  21331. }
  21332. /// @brief user-defined string literal for JSON pointer
  21333. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  21334. JSON_HEDLEY_NON_NULL(1)
  21335. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21336. inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
  21337. #else
  21338. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  21339. #endif
  21340. {
  21341. return nlohmann::json::json_pointer(std::string(s, n));
  21342. }
  21343. } // namespace json_literals
  21344. } // namespace literals
  21345. NLOHMANN_JSON_NAMESPACE_END
  21346. ///////////////////////
  21347. // nonmember support //
  21348. ///////////////////////
  21349. namespace std // NOLINT(cert-dcl58-cpp)
  21350. {
  21351. /// @brief hash value for JSON objects
  21352. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  21353. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21354. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  21355. {
  21356. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  21357. {
  21358. return nlohmann::detail::hash(j);
  21359. }
  21360. };
  21361. // specialization for std::less<value_t>
  21362. template<>
  21363. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  21364. {
  21365. /*!
  21366. @brief compare two value_t enum values
  21367. @since version 3.0.0
  21368. */
  21369. bool operator()(::nlohmann::detail::value_t lhs,
  21370. ::nlohmann::detail::value_t rhs) const noexcept
  21371. {
  21372. #if JSON_HAS_THREE_WAY_COMPARISON
  21373. return std::is_lt(lhs <=> rhs); // *NOPAD*
  21374. #else
  21375. return ::nlohmann::detail::operator<(lhs, rhs);
  21376. #endif
  21377. }
  21378. };
  21379. // C++20 prohibit function specialization in the std namespace.
  21380. #ifndef JSON_HAS_CPP_20
  21381. /// @brief exchanges the values of two JSON objects
  21382. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  21383. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21384. 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)
  21385. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  21386. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  21387. {
  21388. j1.swap(j2);
  21389. }
  21390. #endif
  21391. } // namespace std
  21392. #if JSON_USE_GLOBAL_UDLS
  21393. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21394. using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21395. using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21396. #else
  21397. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21398. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21399. #endif
  21400. #endif
  21401. // #include <nlohmann/detail/macro_unscope.hpp>
  21402. // __ _____ _____ _____
  21403. // __| | __| | | | JSON for Modern C++
  21404. // | | |__ | | | | | | version 3.11.3
  21405. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21406. //
  21407. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  21408. // SPDX-License-Identifier: MIT
  21409. // restore clang diagnostic settings
  21410. #if defined(__clang__)
  21411. #pragma clang diagnostic pop
  21412. #endif
  21413. // clean up
  21414. #undef JSON_ASSERT
  21415. #undef JSON_INTERNAL_CATCH
  21416. #undef JSON_THROW
  21417. #undef JSON_PRIVATE_UNLESS_TESTED
  21418. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21419. #undef NLOHMANN_BASIC_JSON_TPL
  21420. #undef JSON_EXPLICIT
  21421. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  21422. #undef JSON_INLINE_VARIABLE
  21423. #undef JSON_NO_UNIQUE_ADDRESS
  21424. #undef JSON_DISABLE_ENUM_SERIALIZATION
  21425. #undef JSON_USE_GLOBAL_UDLS
  21426. #ifndef JSON_TEST_KEEP_MACROS
  21427. #undef JSON_CATCH
  21428. #undef JSON_TRY
  21429. #undef JSON_HAS_CPP_11
  21430. #undef JSON_HAS_CPP_14
  21431. #undef JSON_HAS_CPP_17
  21432. #undef JSON_HAS_CPP_20
  21433. #undef JSON_HAS_FILESYSTEM
  21434. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  21435. #undef JSON_HAS_THREE_WAY_COMPARISON
  21436. #undef JSON_HAS_RANGES
  21437. #undef JSON_HAS_STATIC_RTTI
  21438. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  21439. #endif
  21440. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  21441. // __ _____ _____ _____
  21442. // __| | __| | | | JSON for Modern C++
  21443. // | | |__ | | | | | | version 3.11.3
  21444. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21445. //
  21446. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  21447. // SPDX-License-Identifier: MIT
  21448. #undef JSON_HEDLEY_ALWAYS_INLINE
  21449. #undef JSON_HEDLEY_ARM_VERSION
  21450. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  21451. #undef JSON_HEDLEY_ARRAY_PARAM
  21452. #undef JSON_HEDLEY_ASSUME
  21453. #undef JSON_HEDLEY_BEGIN_C_DECLS
  21454. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  21455. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  21456. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  21457. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  21458. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  21459. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  21460. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  21461. #undef JSON_HEDLEY_COMPCERT_VERSION
  21462. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  21463. #undef JSON_HEDLEY_CONCAT
  21464. #undef JSON_HEDLEY_CONCAT3
  21465. #undef JSON_HEDLEY_CONCAT3_EX
  21466. #undef JSON_HEDLEY_CONCAT_EX
  21467. #undef JSON_HEDLEY_CONST
  21468. #undef JSON_HEDLEY_CONSTEXPR
  21469. #undef JSON_HEDLEY_CONST_CAST
  21470. #undef JSON_HEDLEY_CPP_CAST
  21471. #undef JSON_HEDLEY_CRAY_VERSION
  21472. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  21473. #undef JSON_HEDLEY_C_DECL
  21474. #undef JSON_HEDLEY_DEPRECATED
  21475. #undef JSON_HEDLEY_DEPRECATED_FOR
  21476. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  21477. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  21478. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  21479. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  21480. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  21481. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  21482. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  21483. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  21484. #undef JSON_HEDLEY_DMC_VERSION
  21485. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  21486. #undef JSON_HEDLEY_EMPTY_BASES
  21487. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  21488. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  21489. #undef JSON_HEDLEY_END_C_DECLS
  21490. #undef JSON_HEDLEY_FLAGS
  21491. #undef JSON_HEDLEY_FLAGS_CAST
  21492. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  21493. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  21494. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  21495. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  21496. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  21497. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  21498. #undef JSON_HEDLEY_GCC_HAS_WARNING
  21499. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  21500. #undef JSON_HEDLEY_GCC_VERSION
  21501. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  21502. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  21503. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  21504. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  21505. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  21506. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  21507. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  21508. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  21509. #undef JSON_HEDLEY_GNUC_VERSION
  21510. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  21511. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  21512. #undef JSON_HEDLEY_HAS_BUILTIN
  21513. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  21514. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  21515. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  21516. #undef JSON_HEDLEY_HAS_EXTENSION
  21517. #undef JSON_HEDLEY_HAS_FEATURE
  21518. #undef JSON_HEDLEY_HAS_WARNING
  21519. #undef JSON_HEDLEY_IAR_VERSION
  21520. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  21521. #undef JSON_HEDLEY_IBM_VERSION
  21522. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  21523. #undef JSON_HEDLEY_IMPORT
  21524. #undef JSON_HEDLEY_INLINE
  21525. #undef JSON_HEDLEY_INTEL_CL_VERSION
  21526. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  21527. #undef JSON_HEDLEY_INTEL_VERSION
  21528. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  21529. #undef JSON_HEDLEY_IS_CONSTANT
  21530. #undef JSON_HEDLEY_IS_CONSTEXPR_
  21531. #undef JSON_HEDLEY_LIKELY
  21532. #undef JSON_HEDLEY_MALLOC
  21533. #undef JSON_HEDLEY_MCST_LCC_VERSION
  21534. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  21535. #undef JSON_HEDLEY_MESSAGE
  21536. #undef JSON_HEDLEY_MSVC_VERSION
  21537. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  21538. #undef JSON_HEDLEY_NEVER_INLINE
  21539. #undef JSON_HEDLEY_NON_NULL
  21540. #undef JSON_HEDLEY_NO_ESCAPE
  21541. #undef JSON_HEDLEY_NO_RETURN
  21542. #undef JSON_HEDLEY_NO_THROW
  21543. #undef JSON_HEDLEY_NULL
  21544. #undef JSON_HEDLEY_PELLES_VERSION
  21545. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  21546. #undef JSON_HEDLEY_PGI_VERSION
  21547. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  21548. #undef JSON_HEDLEY_PREDICT
  21549. #undef JSON_HEDLEY_PRINTF_FORMAT
  21550. #undef JSON_HEDLEY_PRIVATE
  21551. #undef JSON_HEDLEY_PUBLIC
  21552. #undef JSON_HEDLEY_PURE
  21553. #undef JSON_HEDLEY_REINTERPRET_CAST
  21554. #undef JSON_HEDLEY_REQUIRE
  21555. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  21556. #undef JSON_HEDLEY_REQUIRE_MSG
  21557. #undef JSON_HEDLEY_RESTRICT
  21558. #undef JSON_HEDLEY_RETURNS_NON_NULL
  21559. #undef JSON_HEDLEY_SENTINEL
  21560. #undef JSON_HEDLEY_STATIC_ASSERT
  21561. #undef JSON_HEDLEY_STATIC_CAST
  21562. #undef JSON_HEDLEY_STRINGIFY
  21563. #undef JSON_HEDLEY_STRINGIFY_EX
  21564. #undef JSON_HEDLEY_SUNPRO_VERSION
  21565. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  21566. #undef JSON_HEDLEY_TINYC_VERSION
  21567. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  21568. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  21569. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  21570. #undef JSON_HEDLEY_TI_CL2000_VERSION
  21571. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  21572. #undef JSON_HEDLEY_TI_CL430_VERSION
  21573. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  21574. #undef JSON_HEDLEY_TI_CL6X_VERSION
  21575. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  21576. #undef JSON_HEDLEY_TI_CL7X_VERSION
  21577. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  21578. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  21579. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  21580. #undef JSON_HEDLEY_TI_VERSION
  21581. #undef JSON_HEDLEY_TI_VERSION_CHECK
  21582. #undef JSON_HEDLEY_UNAVAILABLE
  21583. #undef JSON_HEDLEY_UNLIKELY
  21584. #undef JSON_HEDLEY_UNPREDICTABLE
  21585. #undef JSON_HEDLEY_UNREACHABLE
  21586. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  21587. #undef JSON_HEDLEY_VERSION
  21588. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  21589. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  21590. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  21591. #undef JSON_HEDLEY_VERSION_ENCODE
  21592. #undef JSON_HEDLEY_WARNING
  21593. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  21594. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  21595. #undef JSON_HEDLEY_FALL_THROUGH
  21596. #endif // INCLUDE_NLOHMANN_JSON_HPP_