json.hpp 888 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 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.2
  34. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  35. //
  36. // SPDX-FileCopyrightText: 2013-2022 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.2
  43. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  44. //
  45. // SPDX-FileCopyrightText: 2013-2022 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 != 2
  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 2 // 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.2
  126. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  127. //
  128. // SPDX-FileCopyrightText: 2013-2022 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.2
  145. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  146. //
  147. // SPDX-FileCopyrightText: 2013-2022 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.2
  161. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  162. //
  163. // SPDX-FileCopyrightText: 2013-2022 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.2
  173. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  174. //
  175. // SPDX-FileCopyrightText: 2013-2022 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.2
  182. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  183. //
  184. // SPDX-FileCopyrightText: 2013-2022 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.2
  191. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  192. //
  193. // SPDX-FileCopyrightText: 2013-2022 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.2
  255. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  256. //
  257. // SPDX-FileCopyrightText: 2013-2022 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. #ifdef JSON_HAS_CPP_17
  2264. #define JSON_INLINE_VARIABLE inline
  2265. #else
  2266. #define JSON_INLINE_VARIABLE
  2267. #endif
  2268. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2269. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2270. #else
  2271. #define JSON_NO_UNIQUE_ADDRESS
  2272. #endif
  2273. // disable documentation warnings on clang
  2274. #if defined(__clang__)
  2275. #pragma clang diagnostic push
  2276. #pragma clang diagnostic ignored "-Wdocumentation"
  2277. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2278. #endif
  2279. // allow disabling exceptions
  2280. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2281. #define JSON_THROW(exception) throw exception
  2282. #define JSON_TRY try
  2283. #define JSON_CATCH(exception) catch(exception)
  2284. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2285. #else
  2286. #include <cstdlib>
  2287. #define JSON_THROW(exception) std::abort()
  2288. #define JSON_TRY if(true)
  2289. #define JSON_CATCH(exception) if(false)
  2290. #define JSON_INTERNAL_CATCH(exception) if(false)
  2291. #endif
  2292. // override exception macros
  2293. #if defined(JSON_THROW_USER)
  2294. #undef JSON_THROW
  2295. #define JSON_THROW JSON_THROW_USER
  2296. #endif
  2297. #if defined(JSON_TRY_USER)
  2298. #undef JSON_TRY
  2299. #define JSON_TRY JSON_TRY_USER
  2300. #endif
  2301. #if defined(JSON_CATCH_USER)
  2302. #undef JSON_CATCH
  2303. #define JSON_CATCH JSON_CATCH_USER
  2304. #undef JSON_INTERNAL_CATCH
  2305. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2306. #endif
  2307. #if defined(JSON_INTERNAL_CATCH_USER)
  2308. #undef JSON_INTERNAL_CATCH
  2309. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2310. #endif
  2311. // allow overriding assert
  2312. #if !defined(JSON_ASSERT)
  2313. #include <cassert> // assert
  2314. #define JSON_ASSERT(x) assert(x)
  2315. #endif
  2316. // allow to access some private functions (needed by the test suite)
  2317. #if defined(JSON_TESTS_PRIVATE)
  2318. #define JSON_PRIVATE_UNLESS_TESTED public
  2319. #else
  2320. #define JSON_PRIVATE_UNLESS_TESTED private
  2321. #endif
  2322. /*!
  2323. @brief macro to briefly define a mapping between an enum and JSON
  2324. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2325. @since version 3.4.0
  2326. */
  2327. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2328. template<typename BasicJsonType> \
  2329. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2330. { \
  2331. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2332. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2333. auto it = std::find_if(std::begin(m), std::end(m), \
  2334. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2335. { \
  2336. return ej_pair.first == e; \
  2337. }); \
  2338. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2339. } \
  2340. template<typename BasicJsonType> \
  2341. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2342. { \
  2343. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2344. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2345. auto it = std::find_if(std::begin(m), std::end(m), \
  2346. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2347. { \
  2348. return ej_pair.second == j; \
  2349. }); \
  2350. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2351. }
  2352. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2353. // may be removed in the future once the class is split.
  2354. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2355. template<template<typename, typename, typename...> class ObjectType, \
  2356. template<typename, typename...> class ArrayType, \
  2357. class StringType, class BooleanType, class NumberIntegerType, \
  2358. class NumberUnsignedType, class NumberFloatType, \
  2359. template<typename> class AllocatorType, \
  2360. template<typename, typename = void> class JSONSerializer, \
  2361. class BinaryType, \
  2362. class CustomBaseClass>
  2363. #define NLOHMANN_BASIC_JSON_TPL \
  2364. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2365. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2366. AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
  2367. // Macros to simplify conversion from/to types
  2368. #define NLOHMANN_JSON_EXPAND( x ) x
  2369. #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
  2370. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2371. NLOHMANN_JSON_PASTE64, \
  2372. NLOHMANN_JSON_PASTE63, \
  2373. NLOHMANN_JSON_PASTE62, \
  2374. NLOHMANN_JSON_PASTE61, \
  2375. NLOHMANN_JSON_PASTE60, \
  2376. NLOHMANN_JSON_PASTE59, \
  2377. NLOHMANN_JSON_PASTE58, \
  2378. NLOHMANN_JSON_PASTE57, \
  2379. NLOHMANN_JSON_PASTE56, \
  2380. NLOHMANN_JSON_PASTE55, \
  2381. NLOHMANN_JSON_PASTE54, \
  2382. NLOHMANN_JSON_PASTE53, \
  2383. NLOHMANN_JSON_PASTE52, \
  2384. NLOHMANN_JSON_PASTE51, \
  2385. NLOHMANN_JSON_PASTE50, \
  2386. NLOHMANN_JSON_PASTE49, \
  2387. NLOHMANN_JSON_PASTE48, \
  2388. NLOHMANN_JSON_PASTE47, \
  2389. NLOHMANN_JSON_PASTE46, \
  2390. NLOHMANN_JSON_PASTE45, \
  2391. NLOHMANN_JSON_PASTE44, \
  2392. NLOHMANN_JSON_PASTE43, \
  2393. NLOHMANN_JSON_PASTE42, \
  2394. NLOHMANN_JSON_PASTE41, \
  2395. NLOHMANN_JSON_PASTE40, \
  2396. NLOHMANN_JSON_PASTE39, \
  2397. NLOHMANN_JSON_PASTE38, \
  2398. NLOHMANN_JSON_PASTE37, \
  2399. NLOHMANN_JSON_PASTE36, \
  2400. NLOHMANN_JSON_PASTE35, \
  2401. NLOHMANN_JSON_PASTE34, \
  2402. NLOHMANN_JSON_PASTE33, \
  2403. NLOHMANN_JSON_PASTE32, \
  2404. NLOHMANN_JSON_PASTE31, \
  2405. NLOHMANN_JSON_PASTE30, \
  2406. NLOHMANN_JSON_PASTE29, \
  2407. NLOHMANN_JSON_PASTE28, \
  2408. NLOHMANN_JSON_PASTE27, \
  2409. NLOHMANN_JSON_PASTE26, \
  2410. NLOHMANN_JSON_PASTE25, \
  2411. NLOHMANN_JSON_PASTE24, \
  2412. NLOHMANN_JSON_PASTE23, \
  2413. NLOHMANN_JSON_PASTE22, \
  2414. NLOHMANN_JSON_PASTE21, \
  2415. NLOHMANN_JSON_PASTE20, \
  2416. NLOHMANN_JSON_PASTE19, \
  2417. NLOHMANN_JSON_PASTE18, \
  2418. NLOHMANN_JSON_PASTE17, \
  2419. NLOHMANN_JSON_PASTE16, \
  2420. NLOHMANN_JSON_PASTE15, \
  2421. NLOHMANN_JSON_PASTE14, \
  2422. NLOHMANN_JSON_PASTE13, \
  2423. NLOHMANN_JSON_PASTE12, \
  2424. NLOHMANN_JSON_PASTE11, \
  2425. NLOHMANN_JSON_PASTE10, \
  2426. NLOHMANN_JSON_PASTE9, \
  2427. NLOHMANN_JSON_PASTE8, \
  2428. NLOHMANN_JSON_PASTE7, \
  2429. NLOHMANN_JSON_PASTE6, \
  2430. NLOHMANN_JSON_PASTE5, \
  2431. NLOHMANN_JSON_PASTE4, \
  2432. NLOHMANN_JSON_PASTE3, \
  2433. NLOHMANN_JSON_PASTE2, \
  2434. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2435. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2436. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2437. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2438. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2439. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2440. #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)
  2441. #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)
  2442. #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)
  2443. #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)
  2444. #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)
  2445. #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)
  2446. #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)
  2447. #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)
  2448. #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)
  2449. #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)
  2450. #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)
  2451. #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)
  2452. #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)
  2453. #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)
  2454. #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)
  2455. #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)
  2456. #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)
  2457. #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)
  2458. #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)
  2459. #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)
  2460. #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)
  2461. #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)
  2462. #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)
  2463. #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)
  2464. #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)
  2465. #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)
  2466. #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)
  2467. #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)
  2468. #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)
  2469. #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)
  2470. #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)
  2471. #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)
  2472. #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)
  2473. #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)
  2474. #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)
  2475. #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)
  2476. #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)
  2477. #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)
  2478. #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)
  2479. #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)
  2480. #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)
  2481. #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)
  2482. #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)
  2483. #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)
  2484. #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)
  2485. #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)
  2486. #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)
  2487. #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)
  2488. #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)
  2489. #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)
  2490. #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)
  2491. #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)
  2492. #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)
  2493. #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)
  2494. #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)
  2495. #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)
  2496. #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)
  2497. #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)
  2498. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2499. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2500. #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
  2501. /*!
  2502. @brief macro
  2503. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2504. @since version 3.9.0
  2505. */
  2506. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2507. 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__)) } \
  2508. 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__)) }
  2509. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2510. 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__)) } \
  2511. 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__)) }
  2512. /*!
  2513. @brief macro
  2514. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2515. @since version 3.9.0
  2516. */
  2517. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2518. 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__)) } \
  2519. 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__)) }
  2520. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2521. 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__)) } \
  2522. 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__)) }
  2523. // inspired from https://stackoverflow.com/a/26745591
  2524. // allows to call any std function as if (e.g. with begin):
  2525. // using std::begin; begin(x);
  2526. //
  2527. // it allows using the detected idiom to retrieve the return type
  2528. // of such an expression
  2529. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2530. namespace detail { \
  2531. using std::std_name; \
  2532. \
  2533. template<typename... T> \
  2534. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2535. } \
  2536. \
  2537. namespace detail2 { \
  2538. struct std_name##_tag \
  2539. { \
  2540. }; \
  2541. \
  2542. template<typename... T> \
  2543. std_name##_tag std_name(T&&...); \
  2544. \
  2545. template<typename... T> \
  2546. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2547. \
  2548. template<typename... T> \
  2549. struct would_call_std_##std_name \
  2550. { \
  2551. static constexpr auto const value = ::nlohmann::detail:: \
  2552. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2553. }; \
  2554. } /* namespace detail2 */ \
  2555. \
  2556. template<typename... T> \
  2557. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2558. { \
  2559. }
  2560. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2561. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2562. #endif
  2563. #if JSON_USE_IMPLICIT_CONVERSIONS
  2564. #define JSON_EXPLICIT
  2565. #else
  2566. #define JSON_EXPLICIT explicit
  2567. #endif
  2568. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2569. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2570. #endif
  2571. #ifndef JSON_USE_GLOBAL_UDLS
  2572. #define JSON_USE_GLOBAL_UDLS 1
  2573. #endif
  2574. #if JSON_HAS_THREE_WAY_COMPARISON
  2575. #include <compare> // partial_ordering
  2576. #endif
  2577. NLOHMANN_JSON_NAMESPACE_BEGIN
  2578. namespace detail
  2579. {
  2580. ///////////////////////////
  2581. // JSON type enumeration //
  2582. ///////////////////////////
  2583. /*!
  2584. @brief the JSON type enumeration
  2585. This enumeration collects the different JSON types. It is internally used to
  2586. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2587. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2588. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2589. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2590. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2591. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2592. @ref basic_json::is_structured() rely on it.
  2593. @note There are three enumeration entries (number_integer, number_unsigned, and
  2594. number_float), because the library distinguishes these three types for numbers:
  2595. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2596. @ref basic_json::number_integer_t is used for signed integers, and
  2597. @ref basic_json::number_float_t is used for floating-point numbers or to
  2598. approximate integers which do not fit in the limits of their respective type.
  2599. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2600. value with the default value for a given type
  2601. @since version 1.0.0
  2602. */
  2603. enum class value_t : std::uint8_t
  2604. {
  2605. null, ///< null value
  2606. object, ///< object (unordered set of name/value pairs)
  2607. array, ///< array (ordered collection of values)
  2608. string, ///< string value
  2609. boolean, ///< boolean value
  2610. number_integer, ///< number value (signed integer)
  2611. number_unsigned, ///< number value (unsigned integer)
  2612. number_float, ///< number value (floating-point)
  2613. binary, ///< binary array (ordered collection of bytes)
  2614. discarded ///< discarded by the parser callback function
  2615. };
  2616. /*!
  2617. @brief comparison operator for JSON types
  2618. Returns an ordering that is similar to Python:
  2619. - order: null < boolean < number < object < array < string < binary
  2620. - furthermore, each type is not smaller than itself
  2621. - discarded values are not comparable
  2622. - binary is represented as a b"" string in python and directly comparable to a
  2623. string; however, making a binary array directly comparable with a string would
  2624. be surprising behavior in a JSON file.
  2625. @since version 1.0.0
  2626. */
  2627. #if JSON_HAS_THREE_WAY_COMPARISON
  2628. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2629. #else
  2630. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2631. #endif
  2632. {
  2633. static constexpr std::array<std::uint8_t, 9> order = {{
  2634. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2635. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2636. 6 /* binary */
  2637. }
  2638. };
  2639. const auto l_index = static_cast<std::size_t>(lhs);
  2640. const auto r_index = static_cast<std::size_t>(rhs);
  2641. #if JSON_HAS_THREE_WAY_COMPARISON
  2642. if (l_index < order.size() && r_index < order.size())
  2643. {
  2644. return order[l_index] <=> order[r_index]; // *NOPAD*
  2645. }
  2646. return std::partial_ordering::unordered;
  2647. #else
  2648. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2649. #endif
  2650. }
  2651. // GCC selects the built-in operator< over an operator rewritten from
  2652. // a user-defined spaceship operator
  2653. // Clang, MSVC, and ICC select the rewritten candidate
  2654. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2655. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2656. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2657. {
  2658. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2659. }
  2660. #endif
  2661. } // namespace detail
  2662. NLOHMANN_JSON_NAMESPACE_END
  2663. // #include <nlohmann/detail/string_escape.hpp>
  2664. // __ _____ _____ _____
  2665. // __| | __| | | | JSON for Modern C++
  2666. // | | |__ | | | | | | version 3.11.2
  2667. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2668. //
  2669. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2670. // SPDX-License-Identifier: MIT
  2671. // #include <nlohmann/detail/abi_macros.hpp>
  2672. NLOHMANN_JSON_NAMESPACE_BEGIN
  2673. namespace detail
  2674. {
  2675. /*!
  2676. @brief replace all occurrences of a substring by another string
  2677. @param[in,out] s the string to manipulate; changed so that all
  2678. occurrences of @a f are replaced with @a t
  2679. @param[in] f the substring to replace with @a t
  2680. @param[in] t the string to replace @a f
  2681. @pre The search string @a f must not be empty. **This precondition is
  2682. enforced with an assertion.**
  2683. @since version 2.0.0
  2684. */
  2685. template<typename StringType>
  2686. inline void replace_substring(StringType& s, const StringType& f,
  2687. const StringType& t)
  2688. {
  2689. JSON_ASSERT(!f.empty());
  2690. for (auto pos = s.find(f); // find first occurrence of f
  2691. pos != StringType::npos; // make sure f was found
  2692. s.replace(pos, f.size(), t), // replace with t, and
  2693. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2694. {}
  2695. }
  2696. /*!
  2697. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2698. * @param[in] s string to escape
  2699. * @return escaped string
  2700. *
  2701. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2702. */
  2703. template<typename StringType>
  2704. inline StringType escape(StringType s)
  2705. {
  2706. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2707. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2708. return s;
  2709. }
  2710. /*!
  2711. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2712. * @param[in] s string to unescape
  2713. * @return unescaped string
  2714. *
  2715. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2716. */
  2717. template<typename StringType>
  2718. static void unescape(StringType& s)
  2719. {
  2720. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2721. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2722. }
  2723. } // namespace detail
  2724. NLOHMANN_JSON_NAMESPACE_END
  2725. // #include <nlohmann/detail/input/position_t.hpp>
  2726. // __ _____ _____ _____
  2727. // __| | __| | | | JSON for Modern C++
  2728. // | | |__ | | | | | | version 3.11.2
  2729. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2730. //
  2731. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2732. // SPDX-License-Identifier: MIT
  2733. #include <cstddef> // size_t
  2734. // #include <nlohmann/detail/abi_macros.hpp>
  2735. NLOHMANN_JSON_NAMESPACE_BEGIN
  2736. namespace detail
  2737. {
  2738. /// struct to capture the start position of the current token
  2739. struct position_t
  2740. {
  2741. /// the total number of characters read
  2742. std::size_t chars_read_total = 0;
  2743. /// the number of characters read in the current line
  2744. std::size_t chars_read_current_line = 0;
  2745. /// the number of lines read
  2746. std::size_t lines_read = 0;
  2747. /// conversion to size_t to preserve SAX interface
  2748. constexpr operator size_t() const
  2749. {
  2750. return chars_read_total;
  2751. }
  2752. };
  2753. } // namespace detail
  2754. NLOHMANN_JSON_NAMESPACE_END
  2755. // #include <nlohmann/detail/macro_scope.hpp>
  2756. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2757. // __ _____ _____ _____
  2758. // __| | __| | | | JSON for Modern C++
  2759. // | | |__ | | | | | | version 3.11.2
  2760. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2761. //
  2762. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2763. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2764. // SPDX-License-Identifier: MIT
  2765. #include <array> // array
  2766. #include <cstddef> // size_t
  2767. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2768. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2769. // #include <nlohmann/detail/macro_scope.hpp>
  2770. NLOHMANN_JSON_NAMESPACE_BEGIN
  2771. namespace detail
  2772. {
  2773. template<typename T>
  2774. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2775. #ifdef JSON_HAS_CPP_14
  2776. // the following utilities are natively available in C++14
  2777. using std::enable_if_t;
  2778. using std::index_sequence;
  2779. using std::make_index_sequence;
  2780. using std::index_sequence_for;
  2781. #else
  2782. // alias templates to reduce boilerplate
  2783. template<bool B, typename T = void>
  2784. using enable_if_t = typename std::enable_if<B, T>::type;
  2785. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2786. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2787. //// START OF CODE FROM GOOGLE ABSEIL
  2788. // integer_sequence
  2789. //
  2790. // Class template representing a compile-time integer sequence. An instantiation
  2791. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2792. // type through its template arguments (which is a common need when
  2793. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2794. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2795. //
  2796. // Example:
  2797. //
  2798. // template< class T, T... Ints >
  2799. // void user_function(integer_sequence<T, Ints...>);
  2800. //
  2801. // int main()
  2802. // {
  2803. // // user_function's `T` will be deduced to `int` and `Ints...`
  2804. // // will be deduced to `0, 1, 2, 3, 4`.
  2805. // user_function(make_integer_sequence<int, 5>());
  2806. // }
  2807. template <typename T, T... Ints>
  2808. struct integer_sequence
  2809. {
  2810. using value_type = T;
  2811. static constexpr std::size_t size() noexcept
  2812. {
  2813. return sizeof...(Ints);
  2814. }
  2815. };
  2816. // index_sequence
  2817. //
  2818. // A helper template for an `integer_sequence` of `size_t`,
  2819. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2820. // `std::index_sequence`.
  2821. template <size_t... Ints>
  2822. using index_sequence = integer_sequence<size_t, Ints...>;
  2823. namespace utility_internal
  2824. {
  2825. template <typename Seq, size_t SeqSize, size_t Rem>
  2826. struct Extend;
  2827. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2828. template <typename T, T... Ints, size_t SeqSize>
  2829. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2830. {
  2831. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2832. };
  2833. template <typename T, T... Ints, size_t SeqSize>
  2834. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2835. {
  2836. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2837. };
  2838. // Recursion helper for 'make_integer_sequence<T, N>'.
  2839. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2840. template <typename T, size_t N>
  2841. struct Gen
  2842. {
  2843. using type =
  2844. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2845. };
  2846. template <typename T>
  2847. struct Gen<T, 0>
  2848. {
  2849. using type = integer_sequence<T>;
  2850. };
  2851. } // namespace utility_internal
  2852. // Compile-time sequences of integers
  2853. // make_integer_sequence
  2854. //
  2855. // This template alias is equivalent to
  2856. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2857. // replacement for C++14's `std::make_integer_sequence`.
  2858. template <typename T, T N>
  2859. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2860. // make_index_sequence
  2861. //
  2862. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2863. // and is designed to be a drop-in replacement for C++14's
  2864. // `std::make_index_sequence`.
  2865. template <size_t N>
  2866. using make_index_sequence = make_integer_sequence<size_t, N>;
  2867. // index_sequence_for
  2868. //
  2869. // Converts a typename pack into an index sequence of the same length, and
  2870. // is designed to be a drop-in replacement for C++14's
  2871. // `std::index_sequence_for()`
  2872. template <typename... Ts>
  2873. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2874. //// END OF CODE FROM GOOGLE ABSEIL
  2875. #endif
  2876. // dispatch utility (taken from ranges-v3)
  2877. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2878. template<> struct priority_tag<0> {};
  2879. // taken from ranges-v3
  2880. template<typename T>
  2881. struct static_const
  2882. {
  2883. static JSON_INLINE_VARIABLE constexpr T value{};
  2884. };
  2885. #ifndef JSON_HAS_CPP_17
  2886. template<typename T>
  2887. constexpr T static_const<T>::value;
  2888. #endif
  2889. template<typename T, typename... Args>
  2890. inline constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  2891. {
  2892. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  2893. }
  2894. } // namespace detail
  2895. NLOHMANN_JSON_NAMESPACE_END
  2896. // #include <nlohmann/detail/meta/type_traits.hpp>
  2897. // __ _____ _____ _____
  2898. // __| | __| | | | JSON for Modern C++
  2899. // | | |__ | | | | | | version 3.11.2
  2900. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2901. //
  2902. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2903. // SPDX-License-Identifier: MIT
  2904. #include <limits> // numeric_limits
  2905. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2906. #include <utility> // declval
  2907. #include <tuple> // tuple
  2908. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2909. // __ _____ _____ _____
  2910. // __| | __| | | | JSON for Modern C++
  2911. // | | |__ | | | | | | version 3.11.2
  2912. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2913. //
  2914. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2915. // SPDX-License-Identifier: MIT
  2916. #include <iterator> // random_access_iterator_tag
  2917. // #include <nlohmann/detail/abi_macros.hpp>
  2918. // #include <nlohmann/detail/meta/void_t.hpp>
  2919. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2920. NLOHMANN_JSON_NAMESPACE_BEGIN
  2921. namespace detail
  2922. {
  2923. template<typename It, typename = void>
  2924. struct iterator_types {};
  2925. template<typename It>
  2926. struct iterator_types <
  2927. It,
  2928. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2929. typename It::reference, typename It::iterator_category >>
  2930. {
  2931. using difference_type = typename It::difference_type;
  2932. using value_type = typename It::value_type;
  2933. using pointer = typename It::pointer;
  2934. using reference = typename It::reference;
  2935. using iterator_category = typename It::iterator_category;
  2936. };
  2937. // This is required as some compilers implement std::iterator_traits in a way that
  2938. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2939. template<typename T, typename = void>
  2940. struct iterator_traits
  2941. {
  2942. };
  2943. template<typename T>
  2944. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2945. : iterator_types<T>
  2946. {
  2947. };
  2948. template<typename T>
  2949. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2950. {
  2951. using iterator_category = std::random_access_iterator_tag;
  2952. using value_type = T;
  2953. using difference_type = ptrdiff_t;
  2954. using pointer = T*;
  2955. using reference = T&;
  2956. };
  2957. } // namespace detail
  2958. NLOHMANN_JSON_NAMESPACE_END
  2959. // #include <nlohmann/detail/macro_scope.hpp>
  2960. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  2961. // __ _____ _____ _____
  2962. // __| | __| | | | JSON for Modern C++
  2963. // | | |__ | | | | | | version 3.11.2
  2964. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2965. //
  2966. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2967. // SPDX-License-Identifier: MIT
  2968. // #include <nlohmann/detail/macro_scope.hpp>
  2969. NLOHMANN_JSON_NAMESPACE_BEGIN
  2970. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  2971. NLOHMANN_JSON_NAMESPACE_END
  2972. // #include <nlohmann/detail/meta/call_std/end.hpp>
  2973. // __ _____ _____ _____
  2974. // __| | __| | | | JSON for Modern C++
  2975. // | | |__ | | | | | | version 3.11.2
  2976. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2977. //
  2978. // SPDX-FileCopyrightText: 2013-2022 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(end);
  2983. NLOHMANN_JSON_NAMESPACE_END
  2984. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2985. // #include <nlohmann/detail/meta/detected.hpp>
  2986. // #include <nlohmann/json_fwd.hpp>
  2987. // __ _____ _____ _____
  2988. // __| | __| | | | JSON for Modern C++
  2989. // | | |__ | | | | | | version 3.11.2
  2990. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2991. //
  2992. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2993. // SPDX-License-Identifier: MIT
  2994. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2995. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2996. #include <cstdint> // int64_t, uint64_t
  2997. #include <map> // map
  2998. #include <memory> // allocator
  2999. #include <string> // string
  3000. #include <vector> // vector
  3001. // #include <nlohmann/detail/abi_macros.hpp>
  3002. /*!
  3003. @brief namespace for Niels Lohmann
  3004. @see https://github.com/nlohmann
  3005. @since version 1.0.0
  3006. */
  3007. NLOHMANN_JSON_NAMESPACE_BEGIN
  3008. /*!
  3009. @brief default JSONSerializer template argument
  3010. This serializer ignores the template arguments and uses ADL
  3011. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3012. for serialization.
  3013. */
  3014. template<typename T = void, typename SFINAE = void>
  3015. struct adl_serializer;
  3016. /// a class to store JSON values
  3017. /// @sa https://json.nlohmann.me/api/basic_json/
  3018. template<template<typename U, typename V, typename... Args> class ObjectType =
  3019. std::map,
  3020. template<typename U, typename... Args> class ArrayType = std::vector,
  3021. class StringType = std::string, class BooleanType = bool,
  3022. class NumberIntegerType = std::int64_t,
  3023. class NumberUnsignedType = std::uint64_t,
  3024. class NumberFloatType = double,
  3025. template<typename U> class AllocatorType = std::allocator,
  3026. template<typename T, typename SFINAE = void> class JSONSerializer =
  3027. adl_serializer,
  3028. class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
  3029. class CustomBaseClass = void>
  3030. class basic_json;
  3031. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3032. /// @sa https://json.nlohmann.me/api/json_pointer/
  3033. template<typename RefStringType>
  3034. class json_pointer;
  3035. /*!
  3036. @brief default specialization
  3037. @sa https://json.nlohmann.me/api/json/
  3038. */
  3039. using json = basic_json<>;
  3040. /// @brief a minimal map-like container that preserves insertion order
  3041. /// @sa https://json.nlohmann.me/api/ordered_map/
  3042. template<class Key, class T, class IgnoredLess, class Allocator>
  3043. struct ordered_map;
  3044. /// @brief specialization that maintains the insertion order of object keys
  3045. /// @sa https://json.nlohmann.me/api/ordered_json/
  3046. using ordered_json = basic_json<nlohmann::ordered_map>;
  3047. NLOHMANN_JSON_NAMESPACE_END
  3048. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3049. NLOHMANN_JSON_NAMESPACE_BEGIN
  3050. /*!
  3051. @brief detail namespace with internal helper functions
  3052. This namespace collects functions that should not be exposed,
  3053. implementations of some @ref basic_json methods, and meta-programming helpers.
  3054. @since version 2.1.0
  3055. */
  3056. namespace detail
  3057. {
  3058. /////////////
  3059. // helpers //
  3060. /////////////
  3061. // Note to maintainers:
  3062. //
  3063. // Every trait in this file expects a non CV-qualified type.
  3064. // The only exceptions are in the 'aliases for detected' section
  3065. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3066. //
  3067. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3068. // (e.g. to_json(BasicJsonType&, const T&))
  3069. template<typename> struct is_basic_json : std::false_type {};
  3070. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3071. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3072. // used by exceptions create() member functions
  3073. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  3074. // false_type otherwise
  3075. template<typename BasicJsonContext>
  3076. struct is_basic_json_context :
  3077. std::integral_constant < bool,
  3078. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3079. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3080. {};
  3081. //////////////////////
  3082. // json_ref helpers //
  3083. //////////////////////
  3084. template<typename>
  3085. class json_ref;
  3086. template<typename>
  3087. struct is_json_ref : std::false_type {};
  3088. template<typename T>
  3089. struct is_json_ref<json_ref<T>> : std::true_type {};
  3090. //////////////////////////
  3091. // aliases for detected //
  3092. //////////////////////////
  3093. template<typename T>
  3094. using mapped_type_t = typename T::mapped_type;
  3095. template<typename T>
  3096. using key_type_t = typename T::key_type;
  3097. template<typename T>
  3098. using value_type_t = typename T::value_type;
  3099. template<typename T>
  3100. using difference_type_t = typename T::difference_type;
  3101. template<typename T>
  3102. using pointer_t = typename T::pointer;
  3103. template<typename T>
  3104. using reference_t = typename T::reference;
  3105. template<typename T>
  3106. using iterator_category_t = typename T::iterator_category;
  3107. template<typename T, typename... Args>
  3108. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3109. template<typename T, typename... Args>
  3110. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3111. template<typename T, typename U>
  3112. using get_template_function = decltype(std::declval<T>().template get<U>());
  3113. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3114. template<typename BasicJsonType, typename T, typename = void>
  3115. struct has_from_json : std::false_type {};
  3116. // trait checking if j.get<T> is valid
  3117. // use this trait instead of std::is_constructible or std::is_convertible,
  3118. // both rely on, or make use of implicit conversions, and thus fail when T
  3119. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3120. template <typename BasicJsonType, typename T>
  3121. struct is_getable
  3122. {
  3123. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3124. };
  3125. template<typename BasicJsonType, typename T>
  3126. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3127. {
  3128. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3129. static constexpr bool value =
  3130. is_detected_exact<void, from_json_function, serializer,
  3131. const BasicJsonType&, T&>::value;
  3132. };
  3133. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3134. // this overload is used for non-default-constructible user-defined-types
  3135. template<typename BasicJsonType, typename T, typename = void>
  3136. struct has_non_default_from_json : std::false_type {};
  3137. template<typename BasicJsonType, typename T>
  3138. struct has_non_default_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<T, from_json_function, serializer,
  3143. const BasicJsonType&>::value;
  3144. };
  3145. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3146. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3147. template<typename BasicJsonType, typename T, typename = void>
  3148. struct has_to_json : std::false_type {};
  3149. template<typename BasicJsonType, typename T>
  3150. struct has_to_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<void, to_json_function, serializer, BasicJsonType&,
  3155. T>::value;
  3156. };
  3157. template<typename T>
  3158. using detect_key_compare = typename T::key_compare;
  3159. template<typename T>
  3160. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3161. // obtains the actual object key comparator
  3162. template<typename BasicJsonType>
  3163. struct actual_object_comparator
  3164. {
  3165. using object_t = typename BasicJsonType::object_t;
  3166. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3167. using type = typename std::conditional < has_key_compare<object_t>::value,
  3168. typename object_t::key_compare, object_comparator_t>::type;
  3169. };
  3170. template<typename BasicJsonType>
  3171. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3172. ///////////////////
  3173. // is_ functions //
  3174. ///////////////////
  3175. // https://en.cppreference.com/w/cpp/types/conjunction
  3176. template<class...> struct conjunction : std::true_type { };
  3177. template<class B> struct conjunction<B> : B { };
  3178. template<class B, class... Bn>
  3179. struct conjunction<B, Bn...>
  3180. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3181. // https://en.cppreference.com/w/cpp/types/negation
  3182. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3183. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3184. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3185. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3186. template <typename T>
  3187. struct is_default_constructible : std::is_default_constructible<T> {};
  3188. template <typename T1, typename T2>
  3189. struct is_default_constructible<std::pair<T1, T2>>
  3190. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3191. template <typename T1, typename T2>
  3192. struct is_default_constructible<const std::pair<T1, T2>>
  3193. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3194. template <typename... Ts>
  3195. struct is_default_constructible<std::tuple<Ts...>>
  3196. : conjunction<is_default_constructible<Ts>...> {};
  3197. template <typename... Ts>
  3198. struct is_default_constructible<const std::tuple<Ts...>>
  3199. : conjunction<is_default_constructible<Ts>...> {};
  3200. template <typename T, typename... Args>
  3201. struct is_constructible : std::is_constructible<T, Args...> {};
  3202. template <typename T1, typename T2>
  3203. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3204. template <typename T1, typename T2>
  3205. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3206. template <typename... Ts>
  3207. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3208. template <typename... Ts>
  3209. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3210. template<typename T, typename = void>
  3211. struct is_iterator_traits : std::false_type {};
  3212. template<typename T>
  3213. struct is_iterator_traits<iterator_traits<T>>
  3214. {
  3215. private:
  3216. using traits = iterator_traits<T>;
  3217. public:
  3218. static constexpr auto value =
  3219. is_detected<value_type_t, traits>::value &&
  3220. is_detected<difference_type_t, traits>::value &&
  3221. is_detected<pointer_t, traits>::value &&
  3222. is_detected<iterator_category_t, traits>::value &&
  3223. is_detected<reference_t, traits>::value;
  3224. };
  3225. template<typename T>
  3226. struct is_range
  3227. {
  3228. private:
  3229. using t_ref = typename std::add_lvalue_reference<T>::type;
  3230. using iterator = detected_t<result_of_begin, t_ref>;
  3231. using sentinel = detected_t<result_of_end, t_ref>;
  3232. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3233. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3234. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3235. static constexpr auto is_iterator_begin =
  3236. is_iterator_traits<iterator_traits<iterator>>::value;
  3237. public:
  3238. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3239. };
  3240. template<typename R>
  3241. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3242. template<typename T>
  3243. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3244. // The following implementation of is_complete_type is taken from
  3245. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3246. // and is written by Xiang Fan who agreed to using it in this library.
  3247. template<typename T, typename = void>
  3248. struct is_complete_type : std::false_type {};
  3249. template<typename T>
  3250. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3251. template<typename BasicJsonType, typename CompatibleObjectType,
  3252. typename = void>
  3253. struct is_compatible_object_type_impl : std::false_type {};
  3254. template<typename BasicJsonType, typename CompatibleObjectType>
  3255. struct is_compatible_object_type_impl <
  3256. BasicJsonType, CompatibleObjectType,
  3257. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3258. is_detected<key_type_t, CompatibleObjectType>::value >>
  3259. {
  3260. using object_t = typename BasicJsonType::object_t;
  3261. // macOS's is_constructible does not play well with nonesuch...
  3262. static constexpr bool value =
  3263. is_constructible<typename object_t::key_type,
  3264. typename CompatibleObjectType::key_type>::value &&
  3265. is_constructible<typename object_t::mapped_type,
  3266. typename CompatibleObjectType::mapped_type>::value;
  3267. };
  3268. template<typename BasicJsonType, typename CompatibleObjectType>
  3269. struct is_compatible_object_type
  3270. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3271. template<typename BasicJsonType, typename ConstructibleObjectType,
  3272. typename = void>
  3273. struct is_constructible_object_type_impl : std::false_type {};
  3274. template<typename BasicJsonType, typename ConstructibleObjectType>
  3275. struct is_constructible_object_type_impl <
  3276. BasicJsonType, ConstructibleObjectType,
  3277. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3278. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3279. {
  3280. using object_t = typename BasicJsonType::object_t;
  3281. static constexpr bool value =
  3282. (is_default_constructible<ConstructibleObjectType>::value &&
  3283. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3284. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3285. (is_constructible<typename ConstructibleObjectType::key_type,
  3286. typename object_t::key_type>::value &&
  3287. std::is_same <
  3288. typename object_t::mapped_type,
  3289. typename ConstructibleObjectType::mapped_type >::value)) ||
  3290. (has_from_json<BasicJsonType,
  3291. typename ConstructibleObjectType::mapped_type>::value ||
  3292. has_non_default_from_json <
  3293. BasicJsonType,
  3294. typename ConstructibleObjectType::mapped_type >::value);
  3295. };
  3296. template<typename BasicJsonType, typename ConstructibleObjectType>
  3297. struct is_constructible_object_type
  3298. : is_constructible_object_type_impl<BasicJsonType,
  3299. ConstructibleObjectType> {};
  3300. template<typename BasicJsonType, typename CompatibleStringType>
  3301. struct is_compatible_string_type
  3302. {
  3303. static constexpr auto value =
  3304. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3305. };
  3306. template<typename BasicJsonType, typename ConstructibleStringType>
  3307. struct is_constructible_string_type
  3308. {
  3309. // launder type through decltype() to fix compilation failure on ICPC
  3310. #ifdef __INTEL_COMPILER
  3311. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3312. #else
  3313. using laundered_type = ConstructibleStringType;
  3314. #endif
  3315. static constexpr auto value =
  3316. conjunction <
  3317. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3318. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3319. value_type_t, laundered_type >>::value;
  3320. };
  3321. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3322. struct is_compatible_array_type_impl : std::false_type {};
  3323. template<typename BasicJsonType, typename CompatibleArrayType>
  3324. struct is_compatible_array_type_impl <
  3325. BasicJsonType, CompatibleArrayType,
  3326. enable_if_t <
  3327. is_detected<iterator_t, CompatibleArrayType>::value&&
  3328. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3329. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3330. // c.f. https://github.com/nlohmann/json/pull/3073
  3331. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3332. {
  3333. static constexpr bool value =
  3334. is_constructible<BasicJsonType,
  3335. range_value_t<CompatibleArrayType>>::value;
  3336. };
  3337. template<typename BasicJsonType, typename CompatibleArrayType>
  3338. struct is_compatible_array_type
  3339. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3340. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3341. struct is_constructible_array_type_impl : std::false_type {};
  3342. template<typename BasicJsonType, typename ConstructibleArrayType>
  3343. struct is_constructible_array_type_impl <
  3344. BasicJsonType, ConstructibleArrayType,
  3345. enable_if_t<std::is_same<ConstructibleArrayType,
  3346. typename BasicJsonType::value_type>::value >>
  3347. : std::true_type {};
  3348. template<typename BasicJsonType, typename ConstructibleArrayType>
  3349. struct is_constructible_array_type_impl <
  3350. BasicJsonType, ConstructibleArrayType,
  3351. enable_if_t < !std::is_same<ConstructibleArrayType,
  3352. typename BasicJsonType::value_type>::value&&
  3353. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3354. is_default_constructible<ConstructibleArrayType>::value&&
  3355. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3356. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3357. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3358. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3359. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3360. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3361. // c.f. https://github.com/nlohmann/json/pull/3073
  3362. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3363. is_complete_type <
  3364. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3365. {
  3366. using value_type = range_value_t<ConstructibleArrayType>;
  3367. static constexpr bool value =
  3368. std::is_same<value_type,
  3369. typename BasicJsonType::array_t::value_type>::value ||
  3370. has_from_json<BasicJsonType,
  3371. value_type>::value ||
  3372. has_non_default_from_json <
  3373. BasicJsonType,
  3374. value_type >::value;
  3375. };
  3376. template<typename BasicJsonType, typename ConstructibleArrayType>
  3377. struct is_constructible_array_type
  3378. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3379. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3380. typename = void>
  3381. struct is_compatible_integer_type_impl : std::false_type {};
  3382. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3383. struct is_compatible_integer_type_impl <
  3384. RealIntegerType, CompatibleNumberIntegerType,
  3385. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3386. std::is_integral<CompatibleNumberIntegerType>::value&&
  3387. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3388. {
  3389. // is there an assert somewhere on overflows?
  3390. using RealLimits = std::numeric_limits<RealIntegerType>;
  3391. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3392. static constexpr auto value =
  3393. is_constructible<RealIntegerType,
  3394. CompatibleNumberIntegerType>::value &&
  3395. CompatibleLimits::is_integer &&
  3396. RealLimits::is_signed == CompatibleLimits::is_signed;
  3397. };
  3398. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3399. struct is_compatible_integer_type
  3400. : is_compatible_integer_type_impl<RealIntegerType,
  3401. CompatibleNumberIntegerType> {};
  3402. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3403. struct is_compatible_type_impl: std::false_type {};
  3404. template<typename BasicJsonType, typename CompatibleType>
  3405. struct is_compatible_type_impl <
  3406. BasicJsonType, CompatibleType,
  3407. enable_if_t<is_complete_type<CompatibleType>::value >>
  3408. {
  3409. static constexpr bool value =
  3410. has_to_json<BasicJsonType, CompatibleType>::value;
  3411. };
  3412. template<typename BasicJsonType, typename CompatibleType>
  3413. struct is_compatible_type
  3414. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3415. template<typename T1, typename T2>
  3416. struct is_constructible_tuple : std::false_type {};
  3417. template<typename T1, typename... Args>
  3418. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3419. template<typename BasicJsonType, typename T>
  3420. struct is_json_iterator_of : std::false_type {};
  3421. template<typename BasicJsonType>
  3422. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3423. template<typename BasicJsonType>
  3424. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3425. {};
  3426. // checks if a given type T is a template specialization of Primary
  3427. template<template <typename...> class Primary, typename T>
  3428. struct is_specialization_of : std::false_type {};
  3429. template<template <typename...> class Primary, typename... Args>
  3430. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3431. template<typename T>
  3432. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3433. // checks if A and B are comparable using Compare functor
  3434. template<typename Compare, typename A, typename B, typename = void>
  3435. struct is_comparable : std::false_type {};
  3436. template<typename Compare, typename A, typename B>
  3437. struct is_comparable<Compare, A, B, void_t<
  3438. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3439. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3440. >> : std::true_type {};
  3441. template<typename T>
  3442. using detect_is_transparent = typename T::is_transparent;
  3443. // type trait to check if KeyType can be used as object key (without a BasicJsonType)
  3444. // see is_usable_as_basic_json_key_type below
  3445. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3446. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3447. using is_usable_as_key_type = typename std::conditional <
  3448. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3449. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3450. ObjectKeyType>::value)
  3451. && (!RequireTransparentComparator
  3452. || is_detected <detect_is_transparent, Comparator>::value)
  3453. && !is_json_pointer<KeyType>::value,
  3454. std::true_type,
  3455. std::false_type >::type;
  3456. // type trait to check if KeyType can be used as object key
  3457. // true if:
  3458. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3459. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3460. // - the comparator is transparent or RequireTransparentComparator is false
  3461. // - KeyType is not a JSON iterator or json_pointer
  3462. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3463. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3464. using is_usable_as_basic_json_key_type = typename std::conditional <
  3465. is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3466. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3467. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3468. && !is_json_iterator_of<BasicJsonType, KeyType>::value,
  3469. std::true_type,
  3470. std::false_type >::type;
  3471. template<typename ObjectType, typename KeyType>
  3472. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3473. // type trait to check if object_t has an erase() member functions accepting KeyType
  3474. template<typename BasicJsonType, typename KeyType>
  3475. using has_erase_with_key_type = typename std::conditional <
  3476. is_detected <
  3477. detect_erase_with_key_type,
  3478. typename BasicJsonType::object_t, KeyType >::value,
  3479. std::true_type,
  3480. std::false_type >::type;
  3481. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3482. // ordered_map inherits capacity() from std::vector)
  3483. template <typename T>
  3484. struct is_ordered_map
  3485. {
  3486. using one = char;
  3487. struct two
  3488. {
  3489. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3490. };
  3491. template <typename C> static one test( decltype(&C::capacity) ) ;
  3492. template <typename C> static two test(...);
  3493. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3494. };
  3495. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3496. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3497. T conditional_static_cast(U value)
  3498. {
  3499. return static_cast<T>(value);
  3500. }
  3501. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3502. T conditional_static_cast(U value)
  3503. {
  3504. return value;
  3505. }
  3506. template<typename... Types>
  3507. using all_integral = conjunction<std::is_integral<Types>...>;
  3508. template<typename... Types>
  3509. using all_signed = conjunction<std::is_signed<Types>...>;
  3510. template<typename... Types>
  3511. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3512. // there's a disjunction trait in another PR; replace when merged
  3513. template<typename... Types>
  3514. using same_sign = std::integral_constant < bool,
  3515. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3516. template<typename OfType, typename T>
  3517. using never_out_of_range = std::integral_constant < bool,
  3518. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3519. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3520. template<typename OfType, typename T,
  3521. bool OfTypeSigned = std::is_signed<OfType>::value,
  3522. bool TSigned = std::is_signed<T>::value>
  3523. struct value_in_range_of_impl2;
  3524. template<typename OfType, typename T>
  3525. struct value_in_range_of_impl2<OfType, T, false, false>
  3526. {
  3527. static constexpr bool test(T val)
  3528. {
  3529. using CommonType = typename std::common_type<OfType, T>::type;
  3530. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3531. }
  3532. };
  3533. template<typename OfType, typename T>
  3534. struct value_in_range_of_impl2<OfType, T, true, false>
  3535. {
  3536. static constexpr bool test(T val)
  3537. {
  3538. using CommonType = typename std::common_type<OfType, T>::type;
  3539. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3540. }
  3541. };
  3542. template<typename OfType, typename T>
  3543. struct value_in_range_of_impl2<OfType, T, false, true>
  3544. {
  3545. static constexpr bool test(T val)
  3546. {
  3547. using CommonType = typename std::common_type<OfType, T>::type;
  3548. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3549. }
  3550. };
  3551. template<typename OfType, typename T>
  3552. struct value_in_range_of_impl2<OfType, T, true, true>
  3553. {
  3554. static constexpr bool test(T val)
  3555. {
  3556. using CommonType = typename std::common_type<OfType, T>::type;
  3557. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3558. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3559. }
  3560. };
  3561. template<typename OfType, typename T,
  3562. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3563. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3564. struct value_in_range_of_impl1;
  3565. template<typename OfType, typename T>
  3566. struct value_in_range_of_impl1<OfType, T, false>
  3567. {
  3568. static constexpr bool test(T val)
  3569. {
  3570. return value_in_range_of_impl2<OfType, T>::test(val);
  3571. }
  3572. };
  3573. template<typename OfType, typename T>
  3574. struct value_in_range_of_impl1<OfType, T, true>
  3575. {
  3576. static constexpr bool test(T /*val*/)
  3577. {
  3578. return true;
  3579. }
  3580. };
  3581. template<typename OfType, typename T>
  3582. inline constexpr bool value_in_range_of(T val)
  3583. {
  3584. return value_in_range_of_impl1<OfType, T>::test(val);
  3585. }
  3586. template<bool Value>
  3587. using bool_constant = std::integral_constant<bool, Value>;
  3588. ///////////////////////////////////////////////////////////////////////////////
  3589. // is_c_string
  3590. ///////////////////////////////////////////////////////////////////////////////
  3591. namespace impl
  3592. {
  3593. template<typename T>
  3594. inline constexpr bool is_c_string()
  3595. {
  3596. using TUnExt = typename std::remove_extent<T>::type;
  3597. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3598. using TUnPtr = typename std::remove_pointer<T>::type;
  3599. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3600. return
  3601. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3602. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3603. }
  3604. } // namespace impl
  3605. // checks whether T is a [cv] char */[cv] char[] C string
  3606. template<typename T>
  3607. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3608. template<typename T>
  3609. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3610. ///////////////////////////////////////////////////////////////////////////////
  3611. // is_transparent
  3612. ///////////////////////////////////////////////////////////////////////////////
  3613. namespace impl
  3614. {
  3615. template<typename T>
  3616. inline constexpr bool is_transparent()
  3617. {
  3618. return is_detected<detect_is_transparent, T>::value;
  3619. }
  3620. } // namespace impl
  3621. // checks whether T has a member named is_transparent
  3622. template<typename T>
  3623. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3624. ///////////////////////////////////////////////////////////////////////////////
  3625. } // namespace detail
  3626. NLOHMANN_JSON_NAMESPACE_END
  3627. // #include <nlohmann/detail/string_concat.hpp>
  3628. // __ _____ _____ _____
  3629. // __| | __| | | | JSON for Modern C++
  3630. // | | |__ | | | | | | version 3.11.2
  3631. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3632. //
  3633. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3634. // SPDX-License-Identifier: MIT
  3635. #include <cstring> // strlen
  3636. #include <string> // string
  3637. #include <utility> // forward
  3638. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3639. // #include <nlohmann/detail/meta/detected.hpp>
  3640. NLOHMANN_JSON_NAMESPACE_BEGIN
  3641. namespace detail
  3642. {
  3643. inline std::size_t concat_length()
  3644. {
  3645. return 0;
  3646. }
  3647. template<typename... Args>
  3648. inline std::size_t concat_length(const char* cstr, Args&& ... rest);
  3649. template<typename StringType, typename... Args>
  3650. inline std::size_t concat_length(const StringType& str, Args&& ... rest);
  3651. template<typename... Args>
  3652. inline std::size_t concat_length(const char /*c*/, Args&& ... rest)
  3653. {
  3654. return 1 + concat_length(std::forward<Args>(rest)...);
  3655. }
  3656. template<typename... Args>
  3657. inline std::size_t concat_length(const char* cstr, Args&& ... rest)
  3658. {
  3659. // cppcheck-suppress ignoredReturnValue
  3660. return ::strlen(cstr) + concat_length(std::forward<Args>(rest)...);
  3661. }
  3662. template<typename StringType, typename... Args>
  3663. inline std::size_t concat_length(const StringType& str, Args&& ... rest)
  3664. {
  3665. return str.size() + concat_length(std::forward<Args>(rest)...);
  3666. }
  3667. template<typename OutStringType>
  3668. inline void concat_into(OutStringType& /*out*/)
  3669. {}
  3670. template<typename StringType, typename Arg>
  3671. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3672. template<typename StringType, typename Arg>
  3673. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3674. template<typename StringType, typename Arg>
  3675. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3676. template<typename StringType, typename Arg>
  3677. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3678. template<typename StringType, typename Arg>
  3679. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3680. template<typename StringType, typename Arg>
  3681. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3682. template<typename StringType, typename Arg>
  3683. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3684. template<typename StringType, typename Arg>
  3685. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3686. template < typename OutStringType, typename Arg, typename... Args,
  3687. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3688. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3689. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3690. template < typename OutStringType, typename Arg, typename... Args,
  3691. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3692. && !detect_string_can_append_op<OutStringType, Arg>::value
  3693. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3694. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3695. template < typename OutStringType, typename Arg, typename... Args,
  3696. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3697. && !detect_string_can_append_op<OutStringType, Arg>::value
  3698. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3699. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3700. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3701. template<typename OutStringType, typename Arg, typename... Args,
  3702. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3703. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3704. {
  3705. out.append(std::forward<Arg>(arg));
  3706. concat_into(out, std::forward<Args>(rest)...);
  3707. }
  3708. template < typename OutStringType, typename Arg, typename... Args,
  3709. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3710. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3711. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3712. {
  3713. out += std::forward<Arg>(arg);
  3714. concat_into(out, std::forward<Args>(rest)...);
  3715. }
  3716. template < typename OutStringType, typename Arg, typename... Args,
  3717. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3718. && !detect_string_can_append_op<OutStringType, Arg>::value
  3719. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3720. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3721. {
  3722. out.append(arg.begin(), arg.end());
  3723. concat_into(out, std::forward<Args>(rest)...);
  3724. }
  3725. template < typename OutStringType, typename Arg, typename... Args,
  3726. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3727. && !detect_string_can_append_op<OutStringType, Arg>::value
  3728. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3729. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3730. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3731. {
  3732. out.append(arg.data(), arg.size());
  3733. concat_into(out, std::forward<Args>(rest)...);
  3734. }
  3735. template<typename OutStringType = std::string, typename... Args>
  3736. inline OutStringType concat(Args && ... args)
  3737. {
  3738. OutStringType str;
  3739. str.reserve(concat_length(std::forward<Args>(args)...));
  3740. concat_into(str, std::forward<Args>(args)...);
  3741. return str;
  3742. }
  3743. } // namespace detail
  3744. NLOHMANN_JSON_NAMESPACE_END
  3745. NLOHMANN_JSON_NAMESPACE_BEGIN
  3746. namespace detail
  3747. {
  3748. ////////////////
  3749. // exceptions //
  3750. ////////////////
  3751. /// @brief general exception of the @ref basic_json class
  3752. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3753. class exception : public std::exception
  3754. {
  3755. public:
  3756. /// returns the explanatory string
  3757. const char* what() const noexcept override
  3758. {
  3759. return m.what();
  3760. }
  3761. /// the id of the exception
  3762. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3763. protected:
  3764. JSON_HEDLEY_NON_NULL(3)
  3765. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3766. static std::string name(const std::string& ename, int id_)
  3767. {
  3768. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3769. }
  3770. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3771. {
  3772. return "";
  3773. }
  3774. template<typename BasicJsonType>
  3775. static std::string diagnostics(const BasicJsonType* leaf_element)
  3776. {
  3777. #if JSON_DIAGNOSTICS
  3778. std::vector<std::string> tokens;
  3779. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3780. {
  3781. switch (current->m_parent->type())
  3782. {
  3783. case value_t::array:
  3784. {
  3785. for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
  3786. {
  3787. if (&current->m_parent->m_value.array->operator[](i) == current)
  3788. {
  3789. tokens.emplace_back(std::to_string(i));
  3790. break;
  3791. }
  3792. }
  3793. break;
  3794. }
  3795. case value_t::object:
  3796. {
  3797. for (const auto& element : *current->m_parent->m_value.object)
  3798. {
  3799. if (&element.second == current)
  3800. {
  3801. tokens.emplace_back(element.first.c_str());
  3802. break;
  3803. }
  3804. }
  3805. break;
  3806. }
  3807. case value_t::null: // LCOV_EXCL_LINE
  3808. case value_t::string: // LCOV_EXCL_LINE
  3809. case value_t::boolean: // LCOV_EXCL_LINE
  3810. case value_t::number_integer: // LCOV_EXCL_LINE
  3811. case value_t::number_unsigned: // LCOV_EXCL_LINE
  3812. case value_t::number_float: // LCOV_EXCL_LINE
  3813. case value_t::binary: // LCOV_EXCL_LINE
  3814. case value_t::discarded: // LCOV_EXCL_LINE
  3815. default: // LCOV_EXCL_LINE
  3816. break; // LCOV_EXCL_LINE
  3817. }
  3818. }
  3819. if (tokens.empty())
  3820. {
  3821. return "";
  3822. }
  3823. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  3824. [](const std::string & a, const std::string & b)
  3825. {
  3826. return concat(a, '/', detail::escape(b));
  3827. });
  3828. return concat('(', str, ") ");
  3829. #else
  3830. static_cast<void>(leaf_element);
  3831. return "";
  3832. #endif
  3833. }
  3834. private:
  3835. /// an exception object as storage for error messages
  3836. std::runtime_error m;
  3837. };
  3838. /// @brief exception indicating a parse error
  3839. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  3840. class parse_error : public exception
  3841. {
  3842. public:
  3843. /*!
  3844. @brief create a parse error exception
  3845. @param[in] id_ the id of the exception
  3846. @param[in] pos the position where the error occurred (or with
  3847. chars_read_total=0 if the position cannot be
  3848. determined)
  3849. @param[in] what_arg the explanatory string
  3850. @return parse_error object
  3851. */
  3852. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3853. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  3854. {
  3855. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3856. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  3857. return {id_, pos.chars_read_total, w.c_str()};
  3858. }
  3859. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3860. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  3861. {
  3862. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3863. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  3864. ": ", exception::diagnostics(context), what_arg);
  3865. return {id_, byte_, w.c_str()};
  3866. }
  3867. /*!
  3868. @brief byte index of the parse error
  3869. The byte index of the last read character in the input file.
  3870. @note For an input with n bytes, 1 is the index of the first character and
  3871. n+1 is the index of the terminating null byte or the end of file.
  3872. This also holds true when reading a byte vector (CBOR or MessagePack).
  3873. */
  3874. const std::size_t byte;
  3875. private:
  3876. parse_error(int id_, std::size_t byte_, const char* what_arg)
  3877. : exception(id_, what_arg), byte(byte_) {}
  3878. static std::string position_string(const position_t& pos)
  3879. {
  3880. return concat(" at line ", std::to_string(pos.lines_read + 1),
  3881. ", column ", std::to_string(pos.chars_read_current_line));
  3882. }
  3883. };
  3884. /// @brief exception indicating errors with iterators
  3885. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  3886. class invalid_iterator : public exception
  3887. {
  3888. public:
  3889. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3890. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  3891. {
  3892. const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  3893. return {id_, w.c_str()};
  3894. }
  3895. private:
  3896. JSON_HEDLEY_NON_NULL(3)
  3897. invalid_iterator(int id_, const char* what_arg)
  3898. : exception(id_, what_arg) {}
  3899. };
  3900. /// @brief exception indicating executing a member function with a wrong type
  3901. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  3902. class type_error : public exception
  3903. {
  3904. public:
  3905. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3906. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3907. {
  3908. const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  3909. return {id_, w.c_str()};
  3910. }
  3911. private:
  3912. JSON_HEDLEY_NON_NULL(3)
  3913. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3914. };
  3915. /// @brief exception indicating access out of the defined range
  3916. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  3917. class out_of_range : public exception
  3918. {
  3919. public:
  3920. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3921. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  3922. {
  3923. const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  3924. return {id_, w.c_str()};
  3925. }
  3926. private:
  3927. JSON_HEDLEY_NON_NULL(3)
  3928. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3929. };
  3930. /// @brief exception indicating other library errors
  3931. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  3932. class other_error : public exception
  3933. {
  3934. public:
  3935. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3936. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3937. {
  3938. const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  3939. return {id_, w.c_str()};
  3940. }
  3941. private:
  3942. JSON_HEDLEY_NON_NULL(3)
  3943. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3944. };
  3945. } // namespace detail
  3946. NLOHMANN_JSON_NAMESPACE_END
  3947. // #include <nlohmann/detail/macro_scope.hpp>
  3948. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3949. // #include <nlohmann/detail/meta/identity_tag.hpp>
  3950. // __ _____ _____ _____
  3951. // __| | __| | | | JSON for Modern C++
  3952. // | | |__ | | | | | | version 3.11.2
  3953. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3954. //
  3955. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3956. // SPDX-License-Identifier: MIT
  3957. // #include <nlohmann/detail/abi_macros.hpp>
  3958. NLOHMANN_JSON_NAMESPACE_BEGIN
  3959. namespace detail
  3960. {
  3961. // dispatching helper struct
  3962. template <class T> struct identity_tag {};
  3963. } // namespace detail
  3964. NLOHMANN_JSON_NAMESPACE_END
  3965. // #include <nlohmann/detail/meta/std_fs.hpp>
  3966. // __ _____ _____ _____
  3967. // __| | __| | | | JSON for Modern C++
  3968. // | | |__ | | | | | | version 3.11.2
  3969. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3970. //
  3971. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3972. // SPDX-License-Identifier: MIT
  3973. // #include <nlohmann/detail/macro_scope.hpp>
  3974. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  3975. #include <experimental/filesystem>
  3976. NLOHMANN_JSON_NAMESPACE_BEGIN
  3977. namespace detail
  3978. {
  3979. namespace std_fs = std::experimental::filesystem;
  3980. } // namespace detail
  3981. NLOHMANN_JSON_NAMESPACE_END
  3982. #elif JSON_HAS_FILESYSTEM
  3983. #include <filesystem>
  3984. NLOHMANN_JSON_NAMESPACE_BEGIN
  3985. namespace detail
  3986. {
  3987. namespace std_fs = std::filesystem;
  3988. } // namespace detail
  3989. NLOHMANN_JSON_NAMESPACE_END
  3990. #endif
  3991. // #include <nlohmann/detail/meta/type_traits.hpp>
  3992. // #include <nlohmann/detail/string_concat.hpp>
  3993. // #include <nlohmann/detail/value_t.hpp>
  3994. NLOHMANN_JSON_NAMESPACE_BEGIN
  3995. namespace detail
  3996. {
  3997. template<typename BasicJsonType>
  3998. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  3999. {
  4000. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  4001. {
  4002. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  4003. }
  4004. n = nullptr;
  4005. }
  4006. // overloads for basic_json template parameters
  4007. template < typename BasicJsonType, typename ArithmeticType,
  4008. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4009. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4010. int > = 0 >
  4011. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4012. {
  4013. switch (static_cast<value_t>(j))
  4014. {
  4015. case value_t::number_unsigned:
  4016. {
  4017. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4018. break;
  4019. }
  4020. case value_t::number_integer:
  4021. {
  4022. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4023. break;
  4024. }
  4025. case value_t::number_float:
  4026. {
  4027. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4028. break;
  4029. }
  4030. case value_t::null:
  4031. case value_t::object:
  4032. case value_t::array:
  4033. case value_t::string:
  4034. case value_t::boolean:
  4035. case value_t::binary:
  4036. case value_t::discarded:
  4037. default:
  4038. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4039. }
  4040. }
  4041. template<typename BasicJsonType>
  4042. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4043. {
  4044. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4045. {
  4046. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4047. }
  4048. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4049. }
  4050. template<typename BasicJsonType>
  4051. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4052. {
  4053. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4054. {
  4055. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4056. }
  4057. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4058. }
  4059. template <
  4060. typename BasicJsonType, typename StringType,
  4061. enable_if_t <
  4062. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4063. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4064. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4065. && !is_json_ref<StringType>::value, int > = 0 >
  4066. inline void from_json(const BasicJsonType& j, StringType& s)
  4067. {
  4068. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4069. {
  4070. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4071. }
  4072. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4073. }
  4074. template<typename BasicJsonType>
  4075. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4076. {
  4077. get_arithmetic_value(j, val);
  4078. }
  4079. template<typename BasicJsonType>
  4080. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4081. {
  4082. get_arithmetic_value(j, val);
  4083. }
  4084. template<typename BasicJsonType>
  4085. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4086. {
  4087. get_arithmetic_value(j, val);
  4088. }
  4089. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4090. template<typename BasicJsonType, typename EnumType,
  4091. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4092. inline void from_json(const BasicJsonType& j, EnumType& e)
  4093. {
  4094. typename std::underlying_type<EnumType>::type val;
  4095. get_arithmetic_value(j, val);
  4096. e = static_cast<EnumType>(val);
  4097. }
  4098. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4099. // forward_list doesn't have an insert method
  4100. template<typename BasicJsonType, typename T, typename Allocator,
  4101. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4102. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4103. {
  4104. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4105. {
  4106. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4107. }
  4108. l.clear();
  4109. std::transform(j.rbegin(), j.rend(),
  4110. std::front_inserter(l), [](const BasicJsonType & i)
  4111. {
  4112. return i.template get<T>();
  4113. });
  4114. }
  4115. // valarray doesn't have an insert method
  4116. template<typename BasicJsonType, typename T,
  4117. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4118. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4119. {
  4120. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4121. {
  4122. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4123. }
  4124. l.resize(j.size());
  4125. std::transform(j.begin(), j.end(), std::begin(l),
  4126. [](const BasicJsonType & elem)
  4127. {
  4128. return elem.template get<T>();
  4129. });
  4130. }
  4131. template<typename BasicJsonType, typename T, std::size_t N>
  4132. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4133. -> decltype(j.template get<T>(), void())
  4134. {
  4135. for (std::size_t i = 0; i < N; ++i)
  4136. {
  4137. arr[i] = j.at(i).template get<T>();
  4138. }
  4139. }
  4140. template<typename BasicJsonType>
  4141. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4142. {
  4143. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4144. }
  4145. template<typename BasicJsonType, typename T, std::size_t N>
  4146. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4147. priority_tag<2> /*unused*/)
  4148. -> decltype(j.template get<T>(), void())
  4149. {
  4150. for (std::size_t i = 0; i < N; ++i)
  4151. {
  4152. arr[i] = j.at(i).template get<T>();
  4153. }
  4154. }
  4155. template<typename BasicJsonType, typename ConstructibleArrayType,
  4156. enable_if_t<
  4157. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4158. int> = 0>
  4159. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4160. -> decltype(
  4161. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4162. j.template get<typename ConstructibleArrayType::value_type>(),
  4163. void())
  4164. {
  4165. using std::end;
  4166. ConstructibleArrayType ret;
  4167. ret.reserve(j.size());
  4168. std::transform(j.begin(), j.end(),
  4169. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4170. {
  4171. // get<BasicJsonType>() returns *this, this won't call a from_json
  4172. // method when value_type is BasicJsonType
  4173. return i.template get<typename ConstructibleArrayType::value_type>();
  4174. });
  4175. arr = std::move(ret);
  4176. }
  4177. template<typename BasicJsonType, typename ConstructibleArrayType,
  4178. enable_if_t<
  4179. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4180. int> = 0>
  4181. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4182. priority_tag<0> /*unused*/)
  4183. {
  4184. using std::end;
  4185. ConstructibleArrayType ret;
  4186. std::transform(
  4187. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4188. [](const BasicJsonType & i)
  4189. {
  4190. // get<BasicJsonType>() returns *this, this won't call a from_json
  4191. // method when value_type is BasicJsonType
  4192. return i.template get<typename ConstructibleArrayType::value_type>();
  4193. });
  4194. arr = std::move(ret);
  4195. }
  4196. template < typename BasicJsonType, typename ConstructibleArrayType,
  4197. enable_if_t <
  4198. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4199. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4200. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4201. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4202. !is_basic_json<ConstructibleArrayType>::value,
  4203. int > = 0 >
  4204. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4205. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4206. j.template get<typename ConstructibleArrayType::value_type>(),
  4207. void())
  4208. {
  4209. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4210. {
  4211. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4212. }
  4213. from_json_array_impl(j, arr, priority_tag<3> {});
  4214. }
  4215. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4216. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4217. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4218. {
  4219. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4220. }
  4221. template < typename BasicJsonType, typename T, std::size_t N >
  4222. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4223. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4224. {
  4225. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4226. {
  4227. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4228. }
  4229. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4230. }
  4231. template<typename BasicJsonType>
  4232. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4233. {
  4234. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4235. {
  4236. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4237. }
  4238. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4239. }
  4240. template<typename BasicJsonType, typename ConstructibleObjectType,
  4241. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4242. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4243. {
  4244. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4245. {
  4246. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4247. }
  4248. ConstructibleObjectType ret;
  4249. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4250. using value_type = typename ConstructibleObjectType::value_type;
  4251. std::transform(
  4252. inner_object->begin(), inner_object->end(),
  4253. std::inserter(ret, ret.begin()),
  4254. [](typename BasicJsonType::object_t::value_type const & p)
  4255. {
  4256. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4257. });
  4258. obj = std::move(ret);
  4259. }
  4260. // overload for arithmetic types, not chosen for basic_json template arguments
  4261. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4262. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4263. // an arithmetic type?
  4264. template < typename BasicJsonType, typename ArithmeticType,
  4265. enable_if_t <
  4266. std::is_arithmetic<ArithmeticType>::value&&
  4267. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4268. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4269. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4270. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4271. int > = 0 >
  4272. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4273. {
  4274. switch (static_cast<value_t>(j))
  4275. {
  4276. case value_t::number_unsigned:
  4277. {
  4278. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4279. break;
  4280. }
  4281. case value_t::number_integer:
  4282. {
  4283. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4284. break;
  4285. }
  4286. case value_t::number_float:
  4287. {
  4288. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4289. break;
  4290. }
  4291. case value_t::boolean:
  4292. {
  4293. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4294. break;
  4295. }
  4296. case value_t::null:
  4297. case value_t::object:
  4298. case value_t::array:
  4299. case value_t::string:
  4300. case value_t::binary:
  4301. case value_t::discarded:
  4302. default:
  4303. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4304. }
  4305. }
  4306. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4307. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4308. {
  4309. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4310. }
  4311. template < typename BasicJsonType, class A1, class A2 >
  4312. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4313. {
  4314. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4315. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4316. }
  4317. template<typename BasicJsonType, typename A1, typename A2>
  4318. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4319. {
  4320. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4321. }
  4322. template<typename BasicJsonType, typename... Args>
  4323. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4324. {
  4325. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4326. }
  4327. template<typename BasicJsonType, typename... Args>
  4328. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4329. {
  4330. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4331. }
  4332. template<typename BasicJsonType, typename TupleRelated>
  4333. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4334. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4335. {
  4336. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4337. {
  4338. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4339. }
  4340. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4341. }
  4342. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4343. typename = enable_if_t < !std::is_constructible <
  4344. typename BasicJsonType::string_t, Key >::value >>
  4345. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  4346. {
  4347. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4348. {
  4349. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4350. }
  4351. m.clear();
  4352. for (const auto& p : j)
  4353. {
  4354. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4355. {
  4356. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4357. }
  4358. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4359. }
  4360. }
  4361. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4362. typename = enable_if_t < !std::is_constructible <
  4363. typename BasicJsonType::string_t, Key >::value >>
  4364. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4365. {
  4366. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4367. {
  4368. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4369. }
  4370. m.clear();
  4371. for (const auto& p : j)
  4372. {
  4373. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4374. {
  4375. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4376. }
  4377. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4378. }
  4379. }
  4380. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4381. template<typename BasicJsonType>
  4382. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4383. {
  4384. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4385. {
  4386. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4387. }
  4388. p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4389. }
  4390. #endif
  4391. struct from_json_fn
  4392. {
  4393. template<typename BasicJsonType, typename T>
  4394. auto operator()(const BasicJsonType& j, T&& val) const
  4395. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4396. -> decltype(from_json(j, std::forward<T>(val)))
  4397. {
  4398. return from_json(j, std::forward<T>(val));
  4399. }
  4400. };
  4401. } // namespace detail
  4402. #ifndef JSON_HAS_CPP_17
  4403. /// namespace to hold default `from_json` function
  4404. /// to see why this is required:
  4405. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4406. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4407. {
  4408. #endif
  4409. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4410. detail::static_const<detail::from_json_fn>::value;
  4411. #ifndef JSON_HAS_CPP_17
  4412. } // namespace
  4413. #endif
  4414. NLOHMANN_JSON_NAMESPACE_END
  4415. // #include <nlohmann/detail/conversions/to_json.hpp>
  4416. // __ _____ _____ _____
  4417. // __| | __| | | | JSON for Modern C++
  4418. // | | |__ | | | | | | version 3.11.2
  4419. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4420. //
  4421. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  4422. // SPDX-License-Identifier: MIT
  4423. #include <algorithm> // copy
  4424. #include <iterator> // begin, end
  4425. #include <string> // string
  4426. #include <tuple> // tuple, get
  4427. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4428. #include <utility> // move, forward, declval, pair
  4429. #include <valarray> // valarray
  4430. #include <vector> // vector
  4431. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4432. // __ _____ _____ _____
  4433. // __| | __| | | | JSON for Modern C++
  4434. // | | |__ | | | | | | version 3.11.2
  4435. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4436. //
  4437. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  4438. // SPDX-License-Identifier: MIT
  4439. #include <cstddef> // size_t
  4440. #include <iterator> // input_iterator_tag
  4441. #include <string> // string, to_string
  4442. #include <tuple> // tuple_size, get, tuple_element
  4443. #include <utility> // move
  4444. #if JSON_HAS_RANGES
  4445. #include <ranges> // enable_borrowed_range
  4446. #endif
  4447. // #include <nlohmann/detail/abi_macros.hpp>
  4448. // #include <nlohmann/detail/meta/type_traits.hpp>
  4449. // #include <nlohmann/detail/value_t.hpp>
  4450. NLOHMANN_JSON_NAMESPACE_BEGIN
  4451. namespace detail
  4452. {
  4453. template<typename string_type>
  4454. void int_to_string( string_type& target, std::size_t value )
  4455. {
  4456. // For ADL
  4457. using std::to_string;
  4458. target = to_string(value);
  4459. }
  4460. template<typename IteratorType> class iteration_proxy_value
  4461. {
  4462. public:
  4463. using difference_type = std::ptrdiff_t;
  4464. using value_type = iteration_proxy_value;
  4465. using pointer = value_type *;
  4466. using reference = value_type &;
  4467. using iterator_category = std::input_iterator_tag;
  4468. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4469. private:
  4470. /// the iterator
  4471. IteratorType anchor{};
  4472. /// an index for arrays (used to create key names)
  4473. std::size_t array_index = 0;
  4474. /// last stringified array index
  4475. mutable std::size_t array_index_last = 0;
  4476. /// a string representation of the array index
  4477. mutable string_type array_index_str = "0";
  4478. /// an empty string (to return a reference for primitive values)
  4479. string_type empty_str{};
  4480. public:
  4481. explicit iteration_proxy_value() = default;
  4482. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4483. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4484. && std::is_nothrow_default_constructible<string_type>::value)
  4485. : anchor(std::move(it))
  4486. , array_index(array_index_)
  4487. {}
  4488. iteration_proxy_value(iteration_proxy_value const&) = default;
  4489. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4490. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4491. iteration_proxy_value(iteration_proxy_value&&)
  4492. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4493. && std::is_nothrow_move_constructible<string_type>::value) = default;
  4494. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4495. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4496. && std::is_nothrow_move_assignable<string_type>::value) = default;
  4497. ~iteration_proxy_value() = default;
  4498. /// dereference operator (needed for range-based for)
  4499. const iteration_proxy_value& operator*() const
  4500. {
  4501. return *this;
  4502. }
  4503. /// increment operator (needed for range-based for)
  4504. iteration_proxy_value& operator++()
  4505. {
  4506. ++anchor;
  4507. ++array_index;
  4508. return *this;
  4509. }
  4510. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4511. {
  4512. auto tmp = iteration_proxy_value(anchor, array_index);
  4513. ++anchor;
  4514. ++array_index;
  4515. return tmp;
  4516. }
  4517. /// equality operator (needed for InputIterator)
  4518. bool operator==(const iteration_proxy_value& o) const
  4519. {
  4520. return anchor == o.anchor;
  4521. }
  4522. /// inequality operator (needed for range-based for)
  4523. bool operator!=(const iteration_proxy_value& o) const
  4524. {
  4525. return anchor != o.anchor;
  4526. }
  4527. /// return key of the iterator
  4528. const string_type& key() const
  4529. {
  4530. JSON_ASSERT(anchor.m_object != nullptr);
  4531. switch (anchor.m_object->type())
  4532. {
  4533. // use integer array index as key
  4534. case value_t::array:
  4535. {
  4536. if (array_index != array_index_last)
  4537. {
  4538. int_to_string( array_index_str, array_index );
  4539. array_index_last = array_index;
  4540. }
  4541. return array_index_str;
  4542. }
  4543. // use key from the object
  4544. case value_t::object:
  4545. return anchor.key();
  4546. // use an empty key for all primitive types
  4547. case value_t::null:
  4548. case value_t::string:
  4549. case value_t::boolean:
  4550. case value_t::number_integer:
  4551. case value_t::number_unsigned:
  4552. case value_t::number_float:
  4553. case value_t::binary:
  4554. case value_t::discarded:
  4555. default:
  4556. return empty_str;
  4557. }
  4558. }
  4559. /// return value of the iterator
  4560. typename IteratorType::reference value() const
  4561. {
  4562. return anchor.value();
  4563. }
  4564. };
  4565. /// proxy class for the items() function
  4566. template<typename IteratorType> class iteration_proxy
  4567. {
  4568. private:
  4569. /// the container to iterate
  4570. typename IteratorType::pointer container = nullptr;
  4571. public:
  4572. explicit iteration_proxy() = default;
  4573. /// construct iteration proxy from a container
  4574. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4575. : container(&cont) {}
  4576. iteration_proxy(iteration_proxy const&) = default;
  4577. iteration_proxy& operator=(iteration_proxy const&) = default;
  4578. iteration_proxy(iteration_proxy&&) noexcept = default;
  4579. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4580. ~iteration_proxy() = default;
  4581. /// return iterator begin (needed for range-based for)
  4582. iteration_proxy_value<IteratorType> begin() const noexcept
  4583. {
  4584. return iteration_proxy_value<IteratorType>(container->begin());
  4585. }
  4586. /// return iterator end (needed for range-based for)
  4587. iteration_proxy_value<IteratorType> end() const noexcept
  4588. {
  4589. return iteration_proxy_value<IteratorType>(container->end());
  4590. }
  4591. };
  4592. // Structured Bindings Support
  4593. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4594. // And see https://github.com/nlohmann/json/pull/1391
  4595. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4596. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4597. {
  4598. return i.key();
  4599. }
  4600. // Structured Bindings Support
  4601. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4602. // And see https://github.com/nlohmann/json/pull/1391
  4603. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4604. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4605. {
  4606. return i.value();
  4607. }
  4608. } // namespace detail
  4609. NLOHMANN_JSON_NAMESPACE_END
  4610. // The Addition to the STD Namespace is required to add
  4611. // Structured Bindings Support to the iteration_proxy_value class
  4612. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4613. // And see https://github.com/nlohmann/json/pull/1391
  4614. namespace std
  4615. {
  4616. #if defined(__clang__)
  4617. // Fix: https://github.com/nlohmann/json/issues/1401
  4618. #pragma clang diagnostic push
  4619. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4620. #endif
  4621. template<typename IteratorType>
  4622. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  4623. : public std::integral_constant<std::size_t, 2> {};
  4624. template<std::size_t N, typename IteratorType>
  4625. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  4626. {
  4627. public:
  4628. using type = decltype(
  4629. get<N>(std::declval <
  4630. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4631. };
  4632. #if defined(__clang__)
  4633. #pragma clang diagnostic pop
  4634. #endif
  4635. } // namespace std
  4636. #if JSON_HAS_RANGES
  4637. template <typename IteratorType>
  4638. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4639. #endif
  4640. // #include <nlohmann/detail/macro_scope.hpp>
  4641. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4642. // #include <nlohmann/detail/meta/std_fs.hpp>
  4643. // #include <nlohmann/detail/meta/type_traits.hpp>
  4644. // #include <nlohmann/detail/value_t.hpp>
  4645. NLOHMANN_JSON_NAMESPACE_BEGIN
  4646. namespace detail
  4647. {
  4648. //////////////////
  4649. // constructors //
  4650. //////////////////
  4651. /*
  4652. * Note all external_constructor<>::construct functions need to call
  4653. * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an
  4654. * allocated value (e.g., a string). See bug issue
  4655. * https://github.com/nlohmann/json/issues/2865 for more information.
  4656. */
  4657. template<value_t> struct external_constructor;
  4658. template<>
  4659. struct external_constructor<value_t::boolean>
  4660. {
  4661. template<typename BasicJsonType>
  4662. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4663. {
  4664. j.m_value.destroy(j.m_type);
  4665. j.m_type = value_t::boolean;
  4666. j.m_value = b;
  4667. j.assert_invariant();
  4668. }
  4669. };
  4670. template<>
  4671. struct external_constructor<value_t::string>
  4672. {
  4673. template<typename BasicJsonType>
  4674. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4675. {
  4676. j.m_value.destroy(j.m_type);
  4677. j.m_type = value_t::string;
  4678. j.m_value = s;
  4679. j.assert_invariant();
  4680. }
  4681. template<typename BasicJsonType>
  4682. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4683. {
  4684. j.m_value.destroy(j.m_type);
  4685. j.m_type = value_t::string;
  4686. j.m_value = std::move(s);
  4687. j.assert_invariant();
  4688. }
  4689. template < typename BasicJsonType, typename CompatibleStringType,
  4690. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4691. int > = 0 >
  4692. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4693. {
  4694. j.m_value.destroy(j.m_type);
  4695. j.m_type = value_t::string;
  4696. j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4697. j.assert_invariant();
  4698. }
  4699. };
  4700. template<>
  4701. struct external_constructor<value_t::binary>
  4702. {
  4703. template<typename BasicJsonType>
  4704. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4705. {
  4706. j.m_value.destroy(j.m_type);
  4707. j.m_type = value_t::binary;
  4708. j.m_value = typename BasicJsonType::binary_t(b);
  4709. j.assert_invariant();
  4710. }
  4711. template<typename BasicJsonType>
  4712. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4713. {
  4714. j.m_value.destroy(j.m_type);
  4715. j.m_type = value_t::binary;
  4716. j.m_value = typename BasicJsonType::binary_t(std::move(b));
  4717. j.assert_invariant();
  4718. }
  4719. };
  4720. template<>
  4721. struct external_constructor<value_t::number_float>
  4722. {
  4723. template<typename BasicJsonType>
  4724. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4725. {
  4726. j.m_value.destroy(j.m_type);
  4727. j.m_type = value_t::number_float;
  4728. j.m_value = val;
  4729. j.assert_invariant();
  4730. }
  4731. };
  4732. template<>
  4733. struct external_constructor<value_t::number_unsigned>
  4734. {
  4735. template<typename BasicJsonType>
  4736. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4737. {
  4738. j.m_value.destroy(j.m_type);
  4739. j.m_type = value_t::number_unsigned;
  4740. j.m_value = val;
  4741. j.assert_invariant();
  4742. }
  4743. };
  4744. template<>
  4745. struct external_constructor<value_t::number_integer>
  4746. {
  4747. template<typename BasicJsonType>
  4748. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4749. {
  4750. j.m_value.destroy(j.m_type);
  4751. j.m_type = value_t::number_integer;
  4752. j.m_value = val;
  4753. j.assert_invariant();
  4754. }
  4755. };
  4756. template<>
  4757. struct external_constructor<value_t::array>
  4758. {
  4759. template<typename BasicJsonType>
  4760. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4761. {
  4762. j.m_value.destroy(j.m_type);
  4763. j.m_type = value_t::array;
  4764. j.m_value = arr;
  4765. j.set_parents();
  4766. j.assert_invariant();
  4767. }
  4768. template<typename BasicJsonType>
  4769. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4770. {
  4771. j.m_value.destroy(j.m_type);
  4772. j.m_type = value_t::array;
  4773. j.m_value = std::move(arr);
  4774. j.set_parents();
  4775. j.assert_invariant();
  4776. }
  4777. template < typename BasicJsonType, typename CompatibleArrayType,
  4778. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4779. int > = 0 >
  4780. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4781. {
  4782. using std::begin;
  4783. using std::end;
  4784. j.m_value.destroy(j.m_type);
  4785. j.m_type = value_t::array;
  4786. j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4787. j.set_parents();
  4788. j.assert_invariant();
  4789. }
  4790. template<typename BasicJsonType>
  4791. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4792. {
  4793. j.m_value.destroy(j.m_type);
  4794. j.m_type = value_t::array;
  4795. j.m_value = value_t::array;
  4796. j.m_value.array->reserve(arr.size());
  4797. for (const bool x : arr)
  4798. {
  4799. j.m_value.array->push_back(x);
  4800. j.set_parent(j.m_value.array->back());
  4801. }
  4802. j.assert_invariant();
  4803. }
  4804. template<typename BasicJsonType, typename T,
  4805. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4806. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4807. {
  4808. j.m_value.destroy(j.m_type);
  4809. j.m_type = value_t::array;
  4810. j.m_value = value_t::array;
  4811. j.m_value.array->resize(arr.size());
  4812. if (arr.size() > 0)
  4813. {
  4814. std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
  4815. }
  4816. j.set_parents();
  4817. j.assert_invariant();
  4818. }
  4819. };
  4820. template<>
  4821. struct external_constructor<value_t::object>
  4822. {
  4823. template<typename BasicJsonType>
  4824. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4825. {
  4826. j.m_value.destroy(j.m_type);
  4827. j.m_type = value_t::object;
  4828. j.m_value = obj;
  4829. j.set_parents();
  4830. j.assert_invariant();
  4831. }
  4832. template<typename BasicJsonType>
  4833. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4834. {
  4835. j.m_value.destroy(j.m_type);
  4836. j.m_type = value_t::object;
  4837. j.m_value = std::move(obj);
  4838. j.set_parents();
  4839. j.assert_invariant();
  4840. }
  4841. template < typename BasicJsonType, typename CompatibleObjectType,
  4842. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4843. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4844. {
  4845. using std::begin;
  4846. using std::end;
  4847. j.m_value.destroy(j.m_type);
  4848. j.m_type = value_t::object;
  4849. j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4850. j.set_parents();
  4851. j.assert_invariant();
  4852. }
  4853. };
  4854. /////////////
  4855. // to_json //
  4856. /////////////
  4857. template<typename BasicJsonType, typename T,
  4858. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4859. inline void to_json(BasicJsonType& j, T b) noexcept
  4860. {
  4861. external_constructor<value_t::boolean>::construct(j, b);
  4862. }
  4863. template < typename BasicJsonType, typename BoolRef,
  4864. enable_if_t <
  4865. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  4866. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  4867. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  4868. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  4869. typename BasicJsonType::boolean_t >::value))
  4870. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  4871. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  4872. {
  4873. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  4874. }
  4875. template<typename BasicJsonType, typename CompatibleString,
  4876. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4877. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  4878. {
  4879. external_constructor<value_t::string>::construct(j, s);
  4880. }
  4881. template<typename BasicJsonType>
  4882. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4883. {
  4884. external_constructor<value_t::string>::construct(j, std::move(s));
  4885. }
  4886. template<typename BasicJsonType, typename FloatType,
  4887. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4888. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  4889. {
  4890. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4891. }
  4892. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4893. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4894. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4895. {
  4896. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4897. }
  4898. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4899. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4900. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4901. {
  4902. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4903. }
  4904. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4905. template<typename BasicJsonType, typename EnumType,
  4906. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4907. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  4908. {
  4909. using underlying_type = typename std::underlying_type<EnumType>::type;
  4910. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  4911. }
  4912. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4913. template<typename BasicJsonType>
  4914. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4915. {
  4916. external_constructor<value_t::array>::construct(j, e);
  4917. }
  4918. template < typename BasicJsonType, typename CompatibleArrayType,
  4919. enable_if_t < is_compatible_array_type<BasicJsonType,
  4920. CompatibleArrayType>::value&&
  4921. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4922. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4923. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4924. !is_basic_json<CompatibleArrayType>::value,
  4925. int > = 0 >
  4926. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4927. {
  4928. external_constructor<value_t::array>::construct(j, arr);
  4929. }
  4930. template<typename BasicJsonType>
  4931. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4932. {
  4933. external_constructor<value_t::binary>::construct(j, bin);
  4934. }
  4935. template<typename BasicJsonType, typename T,
  4936. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4937. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4938. {
  4939. external_constructor<value_t::array>::construct(j, std::move(arr));
  4940. }
  4941. template<typename BasicJsonType>
  4942. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4943. {
  4944. external_constructor<value_t::array>::construct(j, std::move(arr));
  4945. }
  4946. template < typename BasicJsonType, typename CompatibleObjectType,
  4947. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  4948. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  4949. {
  4950. external_constructor<value_t::object>::construct(j, obj);
  4951. }
  4952. template<typename BasicJsonType>
  4953. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4954. {
  4955. external_constructor<value_t::object>::construct(j, std::move(obj));
  4956. }
  4957. template <
  4958. typename BasicJsonType, typename T, std::size_t N,
  4959. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  4960. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4961. int > = 0 >
  4962. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4963. {
  4964. external_constructor<value_t::array>::construct(j, arr);
  4965. }
  4966. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  4967. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  4968. {
  4969. j = { p.first, p.second };
  4970. }
  4971. // for https://github.com/nlohmann/json/pull/1134
  4972. template<typename BasicJsonType, typename T,
  4973. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  4974. inline void to_json(BasicJsonType& j, const T& b)
  4975. {
  4976. j = { {b.key(), b.value()} };
  4977. }
  4978. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  4979. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  4980. {
  4981. j = { std::get<Idx>(t)... };
  4982. }
  4983. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  4984. inline void to_json(BasicJsonType& j, const T& t)
  4985. {
  4986. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  4987. }
  4988. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4989. template<typename BasicJsonType>
  4990. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  4991. {
  4992. j = p.string();
  4993. }
  4994. #endif
  4995. struct to_json_fn
  4996. {
  4997. template<typename BasicJsonType, typename T>
  4998. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  4999. -> decltype(to_json(j, std::forward<T>(val)), void())
  5000. {
  5001. return to_json(j, std::forward<T>(val));
  5002. }
  5003. };
  5004. } // namespace detail
  5005. #ifndef JSON_HAS_CPP_17
  5006. /// namespace to hold default `to_json` function
  5007. /// to see why this is required:
  5008. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5009. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5010. {
  5011. #endif
  5012. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5013. detail::static_const<detail::to_json_fn>::value;
  5014. #ifndef JSON_HAS_CPP_17
  5015. } // namespace
  5016. #endif
  5017. NLOHMANN_JSON_NAMESPACE_END
  5018. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5019. NLOHMANN_JSON_NAMESPACE_BEGIN
  5020. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5021. template<typename ValueType, typename>
  5022. struct adl_serializer
  5023. {
  5024. /// @brief convert a JSON value to any value type
  5025. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5026. template<typename BasicJsonType, typename TargetType = ValueType>
  5027. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5028. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5029. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5030. {
  5031. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5032. }
  5033. /// @brief convert a JSON value to any value type
  5034. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5035. template<typename BasicJsonType, typename TargetType = ValueType>
  5036. static auto from_json(BasicJsonType && j) noexcept(
  5037. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5038. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5039. {
  5040. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5041. }
  5042. /// @brief convert any value type to a JSON value
  5043. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5044. template<typename BasicJsonType, typename TargetType = ValueType>
  5045. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5046. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5047. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5048. {
  5049. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5050. }
  5051. };
  5052. NLOHMANN_JSON_NAMESPACE_END
  5053. // #include <nlohmann/byte_container_with_subtype.hpp>
  5054. // __ _____ _____ _____
  5055. // __| | __| | | | JSON for Modern C++
  5056. // | | |__ | | | | | | version 3.11.2
  5057. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5058. //
  5059. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5060. // SPDX-License-Identifier: MIT
  5061. #include <cstdint> // uint8_t, uint64_t
  5062. #include <tuple> // tie
  5063. #include <utility> // move
  5064. // #include <nlohmann/detail/abi_macros.hpp>
  5065. NLOHMANN_JSON_NAMESPACE_BEGIN
  5066. /// @brief an internal type for a backed binary type
  5067. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5068. template<typename BinaryType>
  5069. class byte_container_with_subtype : public BinaryType
  5070. {
  5071. public:
  5072. using container_type = BinaryType;
  5073. using subtype_type = std::uint64_t;
  5074. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5075. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5076. : container_type()
  5077. {}
  5078. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5079. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5080. : container_type(b)
  5081. {}
  5082. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5083. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5084. : container_type(std::move(b))
  5085. {}
  5086. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5087. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5088. : container_type(b)
  5089. , m_subtype(subtype_)
  5090. , m_has_subtype(true)
  5091. {}
  5092. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5093. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5094. : container_type(std::move(b))
  5095. , m_subtype(subtype_)
  5096. , m_has_subtype(true)
  5097. {}
  5098. bool operator==(const byte_container_with_subtype& rhs) const
  5099. {
  5100. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5101. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5102. }
  5103. bool operator!=(const byte_container_with_subtype& rhs) const
  5104. {
  5105. return !(rhs == *this);
  5106. }
  5107. /// @brief sets the binary subtype
  5108. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5109. void set_subtype(subtype_type subtype_) noexcept
  5110. {
  5111. m_subtype = subtype_;
  5112. m_has_subtype = true;
  5113. }
  5114. /// @brief return the binary subtype
  5115. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5116. constexpr subtype_type subtype() const noexcept
  5117. {
  5118. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5119. }
  5120. /// @brief return whether the value has a subtype
  5121. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5122. constexpr bool has_subtype() const noexcept
  5123. {
  5124. return m_has_subtype;
  5125. }
  5126. /// @brief clears the binary subtype
  5127. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5128. void clear_subtype() noexcept
  5129. {
  5130. m_subtype = 0;
  5131. m_has_subtype = false;
  5132. }
  5133. private:
  5134. subtype_type m_subtype = 0;
  5135. bool m_has_subtype = false;
  5136. };
  5137. NLOHMANN_JSON_NAMESPACE_END
  5138. // #include <nlohmann/detail/conversions/from_json.hpp>
  5139. // #include <nlohmann/detail/conversions/to_json.hpp>
  5140. // #include <nlohmann/detail/exceptions.hpp>
  5141. // #include <nlohmann/detail/hash.hpp>
  5142. // __ _____ _____ _____
  5143. // __| | __| | | | JSON for Modern C++
  5144. // | | |__ | | | | | | version 3.11.2
  5145. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5146. //
  5147. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5148. // SPDX-License-Identifier: MIT
  5149. #include <cstdint> // uint8_t
  5150. #include <cstddef> // size_t
  5151. #include <functional> // hash
  5152. // #include <nlohmann/detail/abi_macros.hpp>
  5153. // #include <nlohmann/detail/value_t.hpp>
  5154. NLOHMANN_JSON_NAMESPACE_BEGIN
  5155. namespace detail
  5156. {
  5157. // boost::hash_combine
  5158. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5159. {
  5160. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5161. return seed;
  5162. }
  5163. /*!
  5164. @brief hash a JSON value
  5165. The hash function tries to rely on std::hash where possible. Furthermore, the
  5166. type of the JSON value is taken into account to have different hash values for
  5167. null, 0, 0U, and false, etc.
  5168. @tparam BasicJsonType basic_json specialization
  5169. @param j JSON value to hash
  5170. @return hash value of j
  5171. */
  5172. template<typename BasicJsonType>
  5173. std::size_t hash(const BasicJsonType& j)
  5174. {
  5175. using string_t = typename BasicJsonType::string_t;
  5176. using number_integer_t = typename BasicJsonType::number_integer_t;
  5177. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5178. using number_float_t = typename BasicJsonType::number_float_t;
  5179. const auto type = static_cast<std::size_t>(j.type());
  5180. switch (j.type())
  5181. {
  5182. case BasicJsonType::value_t::null:
  5183. case BasicJsonType::value_t::discarded:
  5184. {
  5185. return combine(type, 0);
  5186. }
  5187. case BasicJsonType::value_t::object:
  5188. {
  5189. auto seed = combine(type, j.size());
  5190. for (const auto& element : j.items())
  5191. {
  5192. const auto h = std::hash<string_t> {}(element.key());
  5193. seed = combine(seed, h);
  5194. seed = combine(seed, hash(element.value()));
  5195. }
  5196. return seed;
  5197. }
  5198. case BasicJsonType::value_t::array:
  5199. {
  5200. auto seed = combine(type, j.size());
  5201. for (const auto& element : j)
  5202. {
  5203. seed = combine(seed, hash(element));
  5204. }
  5205. return seed;
  5206. }
  5207. case BasicJsonType::value_t::string:
  5208. {
  5209. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5210. return combine(type, h);
  5211. }
  5212. case BasicJsonType::value_t::boolean:
  5213. {
  5214. const auto h = std::hash<bool> {}(j.template get<bool>());
  5215. return combine(type, h);
  5216. }
  5217. case BasicJsonType::value_t::number_integer:
  5218. {
  5219. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5220. return combine(type, h);
  5221. }
  5222. case BasicJsonType::value_t::number_unsigned:
  5223. {
  5224. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5225. return combine(type, h);
  5226. }
  5227. case BasicJsonType::value_t::number_float:
  5228. {
  5229. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5230. return combine(type, h);
  5231. }
  5232. case BasicJsonType::value_t::binary:
  5233. {
  5234. auto seed = combine(type, j.get_binary().size());
  5235. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5236. seed = combine(seed, h);
  5237. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5238. for (const auto byte : j.get_binary())
  5239. {
  5240. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5241. }
  5242. return seed;
  5243. }
  5244. default: // LCOV_EXCL_LINE
  5245. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5246. return 0; // LCOV_EXCL_LINE
  5247. }
  5248. }
  5249. } // namespace detail
  5250. NLOHMANN_JSON_NAMESPACE_END
  5251. // #include <nlohmann/detail/input/binary_reader.hpp>
  5252. // __ _____ _____ _____
  5253. // __| | __| | | | JSON for Modern C++
  5254. // | | |__ | | | | | | version 3.11.2
  5255. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5256. //
  5257. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5258. // SPDX-License-Identifier: MIT
  5259. #include <algorithm> // generate_n
  5260. #include <array> // array
  5261. #include <cmath> // ldexp
  5262. #include <cstddef> // size_t
  5263. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5264. #include <cstdio> // snprintf
  5265. #include <cstring> // memcpy
  5266. #include <iterator> // back_inserter
  5267. #include <limits> // numeric_limits
  5268. #include <string> // char_traits, string
  5269. #include <utility> // make_pair, move
  5270. #include <vector> // vector
  5271. // #include <nlohmann/detail/exceptions.hpp>
  5272. // #include <nlohmann/detail/input/input_adapters.hpp>
  5273. // __ _____ _____ _____
  5274. // __| | __| | | | JSON for Modern C++
  5275. // | | |__ | | | | | | version 3.11.2
  5276. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5277. //
  5278. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5279. // SPDX-License-Identifier: MIT
  5280. #include <array> // array
  5281. #include <cstddef> // size_t
  5282. #include <cstring> // strlen
  5283. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5284. #include <memory> // shared_ptr, make_shared, addressof
  5285. #include <numeric> // accumulate
  5286. #include <string> // string, char_traits
  5287. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5288. #include <utility> // pair, declval
  5289. #ifndef JSON_NO_IO
  5290. #include <cstdio> // FILE *
  5291. #include <istream> // istream
  5292. #endif // JSON_NO_IO
  5293. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5294. // #include <nlohmann/detail/macro_scope.hpp>
  5295. NLOHMANN_JSON_NAMESPACE_BEGIN
  5296. namespace detail
  5297. {
  5298. /// the supported input formats
  5299. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5300. ////////////////////
  5301. // input adapters //
  5302. ////////////////////
  5303. #ifndef JSON_NO_IO
  5304. /*!
  5305. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5306. buffer. This adapter is a very low level adapter.
  5307. */
  5308. class file_input_adapter
  5309. {
  5310. public:
  5311. using char_type = char;
  5312. JSON_HEDLEY_NON_NULL(2)
  5313. explicit file_input_adapter(std::FILE* f) noexcept
  5314. : m_file(f)
  5315. {
  5316. JSON_ASSERT(m_file != nullptr);
  5317. }
  5318. // make class move-only
  5319. file_input_adapter(const file_input_adapter&) = delete;
  5320. file_input_adapter(file_input_adapter&&) noexcept = default;
  5321. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5322. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5323. ~file_input_adapter() = default;
  5324. std::char_traits<char>::int_type get_character() noexcept
  5325. {
  5326. return std::fgetc(m_file);
  5327. }
  5328. private:
  5329. /// the file pointer to read from
  5330. std::FILE* m_file;
  5331. };
  5332. /*!
  5333. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5334. beginning of input. Does not support changing the underlying std::streambuf
  5335. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5336. subsequent use of standard std::istream operations to process any input
  5337. characters following those used in parsing the JSON input. Clears the
  5338. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5339. subsequent call for input from the std::istream.
  5340. */
  5341. class input_stream_adapter
  5342. {
  5343. public:
  5344. using char_type = char;
  5345. ~input_stream_adapter()
  5346. {
  5347. // clear stream flags; we use underlying streambuf I/O, do not
  5348. // maintain ifstream flags, except eof
  5349. if (is != nullptr)
  5350. {
  5351. is->clear(is->rdstate() & std::ios::eofbit);
  5352. }
  5353. }
  5354. explicit input_stream_adapter(std::istream& i)
  5355. : is(&i), sb(i.rdbuf())
  5356. {}
  5357. // delete because of pointer members
  5358. input_stream_adapter(const input_stream_adapter&) = delete;
  5359. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5360. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5361. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5362. : is(rhs.is), sb(rhs.sb)
  5363. {
  5364. rhs.is = nullptr;
  5365. rhs.sb = nullptr;
  5366. }
  5367. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5368. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5369. // end up as the same value, e.g. 0xFFFFFFFF.
  5370. std::char_traits<char>::int_type get_character()
  5371. {
  5372. auto res = sb->sbumpc();
  5373. // set eof manually, as we don't use the istream interface.
  5374. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5375. {
  5376. is->clear(is->rdstate() | std::ios::eofbit);
  5377. }
  5378. return res;
  5379. }
  5380. private:
  5381. /// the associated input stream
  5382. std::istream* is = nullptr;
  5383. std::streambuf* sb = nullptr;
  5384. };
  5385. #endif // JSON_NO_IO
  5386. // General-purpose iterator-based adapter. It might not be as fast as
  5387. // theoretically possible for some containers, but it is extremely versatile.
  5388. template<typename IteratorType>
  5389. class iterator_input_adapter
  5390. {
  5391. public:
  5392. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5393. iterator_input_adapter(IteratorType first, IteratorType last)
  5394. : current(std::move(first)), end(std::move(last))
  5395. {}
  5396. typename std::char_traits<char_type>::int_type get_character()
  5397. {
  5398. if (JSON_HEDLEY_LIKELY(current != end))
  5399. {
  5400. auto result = std::char_traits<char_type>::to_int_type(*current);
  5401. std::advance(current, 1);
  5402. return result;
  5403. }
  5404. return std::char_traits<char_type>::eof();
  5405. }
  5406. private:
  5407. IteratorType current;
  5408. IteratorType end;
  5409. template<typename BaseInputAdapter, size_t T>
  5410. friend struct wide_string_input_helper;
  5411. bool empty() const
  5412. {
  5413. return current == end;
  5414. }
  5415. };
  5416. template<typename BaseInputAdapter, size_t T>
  5417. struct wide_string_input_helper;
  5418. template<typename BaseInputAdapter>
  5419. struct wide_string_input_helper<BaseInputAdapter, 4>
  5420. {
  5421. // UTF-32
  5422. static void fill_buffer(BaseInputAdapter& input,
  5423. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5424. size_t& utf8_bytes_index,
  5425. size_t& utf8_bytes_filled)
  5426. {
  5427. utf8_bytes_index = 0;
  5428. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5429. {
  5430. utf8_bytes[0] = std::char_traits<char>::eof();
  5431. utf8_bytes_filled = 1;
  5432. }
  5433. else
  5434. {
  5435. // get the current character
  5436. const auto wc = input.get_character();
  5437. // UTF-32 to UTF-8 encoding
  5438. if (wc < 0x80)
  5439. {
  5440. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5441. utf8_bytes_filled = 1;
  5442. }
  5443. else if (wc <= 0x7FF)
  5444. {
  5445. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5446. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5447. utf8_bytes_filled = 2;
  5448. }
  5449. else if (wc <= 0xFFFF)
  5450. {
  5451. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5452. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5453. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5454. utf8_bytes_filled = 3;
  5455. }
  5456. else if (wc <= 0x10FFFF)
  5457. {
  5458. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5459. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5460. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5461. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5462. utf8_bytes_filled = 4;
  5463. }
  5464. else
  5465. {
  5466. // unknown character
  5467. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5468. utf8_bytes_filled = 1;
  5469. }
  5470. }
  5471. }
  5472. };
  5473. template<typename BaseInputAdapter>
  5474. struct wide_string_input_helper<BaseInputAdapter, 2>
  5475. {
  5476. // UTF-16
  5477. static void fill_buffer(BaseInputAdapter& input,
  5478. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5479. size_t& utf8_bytes_index,
  5480. size_t& utf8_bytes_filled)
  5481. {
  5482. utf8_bytes_index = 0;
  5483. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5484. {
  5485. utf8_bytes[0] = std::char_traits<char>::eof();
  5486. utf8_bytes_filled = 1;
  5487. }
  5488. else
  5489. {
  5490. // get the current character
  5491. const auto wc = input.get_character();
  5492. // UTF-16 to UTF-8 encoding
  5493. if (wc < 0x80)
  5494. {
  5495. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5496. utf8_bytes_filled = 1;
  5497. }
  5498. else if (wc <= 0x7FF)
  5499. {
  5500. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5501. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5502. utf8_bytes_filled = 2;
  5503. }
  5504. else if (0xD800 > wc || wc >= 0xE000)
  5505. {
  5506. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5507. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5508. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5509. utf8_bytes_filled = 3;
  5510. }
  5511. else
  5512. {
  5513. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5514. {
  5515. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5516. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5517. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5518. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5519. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5520. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5521. utf8_bytes_filled = 4;
  5522. }
  5523. else
  5524. {
  5525. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5526. utf8_bytes_filled = 1;
  5527. }
  5528. }
  5529. }
  5530. }
  5531. };
  5532. // Wraps another input apdater to convert wide character types into individual bytes.
  5533. template<typename BaseInputAdapter, typename WideCharType>
  5534. class wide_string_input_adapter
  5535. {
  5536. public:
  5537. using char_type = char;
  5538. wide_string_input_adapter(BaseInputAdapter base)
  5539. : base_adapter(base) {}
  5540. typename std::char_traits<char>::int_type get_character() noexcept
  5541. {
  5542. // check if buffer needs to be filled
  5543. if (utf8_bytes_index == utf8_bytes_filled)
  5544. {
  5545. fill_buffer<sizeof(WideCharType)>();
  5546. JSON_ASSERT(utf8_bytes_filled > 0);
  5547. JSON_ASSERT(utf8_bytes_index == 0);
  5548. }
  5549. // use buffer
  5550. JSON_ASSERT(utf8_bytes_filled > 0);
  5551. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5552. return utf8_bytes[utf8_bytes_index++];
  5553. }
  5554. private:
  5555. BaseInputAdapter base_adapter;
  5556. template<size_t T>
  5557. void fill_buffer()
  5558. {
  5559. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5560. }
  5561. /// a buffer for UTF-8 bytes
  5562. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5563. /// index to the utf8_codes array for the next valid byte
  5564. std::size_t utf8_bytes_index = 0;
  5565. /// number of valid bytes in the utf8_codes array
  5566. std::size_t utf8_bytes_filled = 0;
  5567. };
  5568. template<typename IteratorType, typename Enable = void>
  5569. struct iterator_input_adapter_factory
  5570. {
  5571. using iterator_type = IteratorType;
  5572. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5573. using adapter_type = iterator_input_adapter<iterator_type>;
  5574. static adapter_type create(IteratorType first, IteratorType last)
  5575. {
  5576. return adapter_type(std::move(first), std::move(last));
  5577. }
  5578. };
  5579. template<typename T>
  5580. struct is_iterator_of_multibyte
  5581. {
  5582. using value_type = typename std::iterator_traits<T>::value_type;
  5583. enum
  5584. {
  5585. value = sizeof(value_type) > 1
  5586. };
  5587. };
  5588. template<typename IteratorType>
  5589. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5590. {
  5591. using iterator_type = IteratorType;
  5592. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5593. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5594. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5595. static adapter_type create(IteratorType first, IteratorType last)
  5596. {
  5597. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5598. }
  5599. };
  5600. // General purpose iterator-based input
  5601. template<typename IteratorType>
  5602. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5603. {
  5604. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5605. return factory_type::create(first, last);
  5606. }
  5607. // Convenience shorthand from container to iterator
  5608. // Enables ADL on begin(container) and end(container)
  5609. // Encloses the using declarations in namespace for not to leak them to outside scope
  5610. namespace container_input_adapter_factory_impl
  5611. {
  5612. using std::begin;
  5613. using std::end;
  5614. template<typename ContainerType, typename Enable = void>
  5615. struct container_input_adapter_factory {};
  5616. template<typename ContainerType>
  5617. struct container_input_adapter_factory< ContainerType,
  5618. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5619. {
  5620. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5621. static adapter_type create(const ContainerType& container)
  5622. {
  5623. return input_adapter(begin(container), end(container));
  5624. }
  5625. };
  5626. } // namespace container_input_adapter_factory_impl
  5627. template<typename ContainerType>
  5628. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5629. {
  5630. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5631. }
  5632. #ifndef JSON_NO_IO
  5633. // Special cases with fast paths
  5634. inline file_input_adapter input_adapter(std::FILE* file)
  5635. {
  5636. return file_input_adapter(file);
  5637. }
  5638. inline input_stream_adapter input_adapter(std::istream& stream)
  5639. {
  5640. return input_stream_adapter(stream);
  5641. }
  5642. inline input_stream_adapter input_adapter(std::istream&& stream)
  5643. {
  5644. return input_stream_adapter(stream);
  5645. }
  5646. #endif // JSON_NO_IO
  5647. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5648. // Null-delimited strings, and the like.
  5649. template < typename CharT,
  5650. typename std::enable_if <
  5651. std::is_pointer<CharT>::value&&
  5652. !std::is_array<CharT>::value&&
  5653. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5654. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5655. int >::type = 0 >
  5656. contiguous_bytes_input_adapter input_adapter(CharT b)
  5657. {
  5658. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5659. const auto* ptr = reinterpret_cast<const char*>(b);
  5660. return input_adapter(ptr, ptr + length);
  5661. }
  5662. template<typename T, std::size_t N>
  5663. 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)
  5664. {
  5665. return input_adapter(array, array + N);
  5666. }
  5667. // This class only handles inputs of input_buffer_adapter type.
  5668. // It's required so that expressions like {ptr, len} can be implicitly cast
  5669. // to the correct adapter.
  5670. class span_input_adapter
  5671. {
  5672. public:
  5673. template < typename CharT,
  5674. typename std::enable_if <
  5675. std::is_pointer<CharT>::value&&
  5676. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5677. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5678. int >::type = 0 >
  5679. span_input_adapter(CharT b, std::size_t l)
  5680. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5681. template<class IteratorType,
  5682. typename std::enable_if<
  5683. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5684. int>::type = 0>
  5685. span_input_adapter(IteratorType first, IteratorType last)
  5686. : ia(input_adapter(first, last)) {}
  5687. contiguous_bytes_input_adapter&& get()
  5688. {
  5689. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5690. }
  5691. private:
  5692. contiguous_bytes_input_adapter ia;
  5693. };
  5694. } // namespace detail
  5695. NLOHMANN_JSON_NAMESPACE_END
  5696. // #include <nlohmann/detail/input/json_sax.hpp>
  5697. // __ _____ _____ _____
  5698. // __| | __| | | | JSON for Modern C++
  5699. // | | |__ | | | | | | version 3.11.2
  5700. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5701. //
  5702. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5703. // SPDX-License-Identifier: MIT
  5704. #include <cstddef>
  5705. #include <string> // string
  5706. #include <utility> // move
  5707. #include <vector> // vector
  5708. // #include <nlohmann/detail/exceptions.hpp>
  5709. // #include <nlohmann/detail/macro_scope.hpp>
  5710. // #include <nlohmann/detail/string_concat.hpp>
  5711. NLOHMANN_JSON_NAMESPACE_BEGIN
  5712. /*!
  5713. @brief SAX interface
  5714. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5715. Each function is called in different situations while the input is parsed. The
  5716. boolean return value informs the parser whether to continue processing the
  5717. input.
  5718. */
  5719. template<typename BasicJsonType>
  5720. struct json_sax
  5721. {
  5722. using number_integer_t = typename BasicJsonType::number_integer_t;
  5723. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5724. using number_float_t = typename BasicJsonType::number_float_t;
  5725. using string_t = typename BasicJsonType::string_t;
  5726. using binary_t = typename BasicJsonType::binary_t;
  5727. /*!
  5728. @brief a null value was read
  5729. @return whether parsing should proceed
  5730. */
  5731. virtual bool null() = 0;
  5732. /*!
  5733. @brief a boolean value was read
  5734. @param[in] val boolean value
  5735. @return whether parsing should proceed
  5736. */
  5737. virtual bool boolean(bool val) = 0;
  5738. /*!
  5739. @brief an integer number was read
  5740. @param[in] val integer value
  5741. @return whether parsing should proceed
  5742. */
  5743. virtual bool number_integer(number_integer_t val) = 0;
  5744. /*!
  5745. @brief an unsigned integer number was read
  5746. @param[in] val unsigned integer value
  5747. @return whether parsing should proceed
  5748. */
  5749. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5750. /*!
  5751. @brief a floating-point number was read
  5752. @param[in] val floating-point value
  5753. @param[in] s raw token value
  5754. @return whether parsing should proceed
  5755. */
  5756. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5757. /*!
  5758. @brief a string value was read
  5759. @param[in] val string value
  5760. @return whether parsing should proceed
  5761. @note It is safe to move the passed string value.
  5762. */
  5763. virtual bool string(string_t& val) = 0;
  5764. /*!
  5765. @brief a binary value was read
  5766. @param[in] val binary value
  5767. @return whether parsing should proceed
  5768. @note It is safe to move the passed binary value.
  5769. */
  5770. virtual bool binary(binary_t& val) = 0;
  5771. /*!
  5772. @brief the beginning of an object was read
  5773. @param[in] elements number of object elements or -1 if unknown
  5774. @return whether parsing should proceed
  5775. @note binary formats may report the number of elements
  5776. */
  5777. virtual bool start_object(std::size_t elements) = 0;
  5778. /*!
  5779. @brief an object key was read
  5780. @param[in] val object key
  5781. @return whether parsing should proceed
  5782. @note It is safe to move the passed string.
  5783. */
  5784. virtual bool key(string_t& val) = 0;
  5785. /*!
  5786. @brief the end of an object was read
  5787. @return whether parsing should proceed
  5788. */
  5789. virtual bool end_object() = 0;
  5790. /*!
  5791. @brief the beginning of an array was read
  5792. @param[in] elements number of array elements or -1 if unknown
  5793. @return whether parsing should proceed
  5794. @note binary formats may report the number of elements
  5795. */
  5796. virtual bool start_array(std::size_t elements) = 0;
  5797. /*!
  5798. @brief the end of an array was read
  5799. @return whether parsing should proceed
  5800. */
  5801. virtual bool end_array() = 0;
  5802. /*!
  5803. @brief a parse error occurred
  5804. @param[in] position the position in the input where the error occurs
  5805. @param[in] last_token the last read token
  5806. @param[in] ex an exception object describing the error
  5807. @return whether parsing should proceed (must return false)
  5808. */
  5809. virtual bool parse_error(std::size_t position,
  5810. const std::string& last_token,
  5811. const detail::exception& ex) = 0;
  5812. json_sax() = default;
  5813. json_sax(const json_sax&) = default;
  5814. json_sax(json_sax&&) noexcept = default;
  5815. json_sax& operator=(const json_sax&) = default;
  5816. json_sax& operator=(json_sax&&) noexcept = default;
  5817. virtual ~json_sax() = default;
  5818. };
  5819. namespace detail
  5820. {
  5821. /*!
  5822. @brief SAX implementation to create a JSON value from SAX events
  5823. This class implements the @ref json_sax interface and processes the SAX events
  5824. to create a JSON value which makes it basically a DOM parser. The structure or
  5825. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5826. a pointer to the respective array or object for each recursion depth.
  5827. After successful parsing, the value that is passed by reference to the
  5828. constructor contains the parsed value.
  5829. @tparam BasicJsonType the JSON type
  5830. */
  5831. template<typename BasicJsonType>
  5832. class json_sax_dom_parser
  5833. {
  5834. public:
  5835. using number_integer_t = typename BasicJsonType::number_integer_t;
  5836. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5837. using number_float_t = typename BasicJsonType::number_float_t;
  5838. using string_t = typename BasicJsonType::string_t;
  5839. using binary_t = typename BasicJsonType::binary_t;
  5840. /*!
  5841. @param[in,out] r reference to a JSON value that is manipulated while
  5842. parsing
  5843. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5844. */
  5845. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5846. : root(r), allow_exceptions(allow_exceptions_)
  5847. {}
  5848. // make class move-only
  5849. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5850. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5851. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5852. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5853. ~json_sax_dom_parser() = default;
  5854. bool null()
  5855. {
  5856. handle_value(nullptr);
  5857. return true;
  5858. }
  5859. bool boolean(bool val)
  5860. {
  5861. handle_value(val);
  5862. return true;
  5863. }
  5864. bool number_integer(number_integer_t val)
  5865. {
  5866. handle_value(val);
  5867. return true;
  5868. }
  5869. bool number_unsigned(number_unsigned_t val)
  5870. {
  5871. handle_value(val);
  5872. return true;
  5873. }
  5874. bool number_float(number_float_t val, const string_t& /*unused*/)
  5875. {
  5876. handle_value(val);
  5877. return true;
  5878. }
  5879. bool string(string_t& val)
  5880. {
  5881. handle_value(val);
  5882. return true;
  5883. }
  5884. bool binary(binary_t& val)
  5885. {
  5886. handle_value(std::move(val));
  5887. return true;
  5888. }
  5889. bool start_object(std::size_t len)
  5890. {
  5891. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5892. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5893. {
  5894. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5895. }
  5896. return true;
  5897. }
  5898. bool key(string_t& val)
  5899. {
  5900. JSON_ASSERT(!ref_stack.empty());
  5901. JSON_ASSERT(ref_stack.back()->is_object());
  5902. // add null at given key and store the reference for later
  5903. object_element = &(ref_stack.back()->m_value.object->operator[](val));
  5904. return true;
  5905. }
  5906. bool end_object()
  5907. {
  5908. JSON_ASSERT(!ref_stack.empty());
  5909. JSON_ASSERT(ref_stack.back()->is_object());
  5910. ref_stack.back()->set_parents();
  5911. ref_stack.pop_back();
  5912. return true;
  5913. }
  5914. bool start_array(std::size_t len)
  5915. {
  5916. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5917. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5918. {
  5919. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5920. }
  5921. return true;
  5922. }
  5923. bool end_array()
  5924. {
  5925. JSON_ASSERT(!ref_stack.empty());
  5926. JSON_ASSERT(ref_stack.back()->is_array());
  5927. ref_stack.back()->set_parents();
  5928. ref_stack.pop_back();
  5929. return true;
  5930. }
  5931. template<class Exception>
  5932. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5933. const Exception& ex)
  5934. {
  5935. errored = true;
  5936. static_cast<void>(ex);
  5937. if (allow_exceptions)
  5938. {
  5939. JSON_THROW(ex);
  5940. }
  5941. return false;
  5942. }
  5943. constexpr bool is_errored() const
  5944. {
  5945. return errored;
  5946. }
  5947. private:
  5948. /*!
  5949. @invariant If the ref stack is empty, then the passed value will be the new
  5950. root.
  5951. @invariant If the ref stack contains a value, then it is an array or an
  5952. object to which we can add elements
  5953. */
  5954. template<typename Value>
  5955. JSON_HEDLEY_RETURNS_NON_NULL
  5956. BasicJsonType* handle_value(Value&& v)
  5957. {
  5958. if (ref_stack.empty())
  5959. {
  5960. root = BasicJsonType(std::forward<Value>(v));
  5961. return &root;
  5962. }
  5963. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5964. if (ref_stack.back()->is_array())
  5965. {
  5966. ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
  5967. return &(ref_stack.back()->m_value.array->back());
  5968. }
  5969. JSON_ASSERT(ref_stack.back()->is_object());
  5970. JSON_ASSERT(object_element);
  5971. *object_element = BasicJsonType(std::forward<Value>(v));
  5972. return object_element;
  5973. }
  5974. /// the parsed JSON value
  5975. BasicJsonType& root;
  5976. /// stack to model hierarchy of values
  5977. std::vector<BasicJsonType*> ref_stack {};
  5978. /// helper to hold the reference for the next object element
  5979. BasicJsonType* object_element = nullptr;
  5980. /// whether a syntax error occurred
  5981. bool errored = false;
  5982. /// whether to throw exceptions in case of errors
  5983. const bool allow_exceptions = true;
  5984. };
  5985. template<typename BasicJsonType>
  5986. class json_sax_dom_callback_parser
  5987. {
  5988. public:
  5989. using number_integer_t = typename BasicJsonType::number_integer_t;
  5990. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5991. using number_float_t = typename BasicJsonType::number_float_t;
  5992. using string_t = typename BasicJsonType::string_t;
  5993. using binary_t = typename BasicJsonType::binary_t;
  5994. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  5995. using parse_event_t = typename BasicJsonType::parse_event_t;
  5996. json_sax_dom_callback_parser(BasicJsonType& r,
  5997. const parser_callback_t cb,
  5998. const bool allow_exceptions_ = true)
  5999. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  6000. {
  6001. keep_stack.push_back(true);
  6002. }
  6003. // make class move-only
  6004. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  6005. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6006. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  6007. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6008. ~json_sax_dom_callback_parser() = default;
  6009. bool null()
  6010. {
  6011. handle_value(nullptr);
  6012. return true;
  6013. }
  6014. bool boolean(bool val)
  6015. {
  6016. handle_value(val);
  6017. return true;
  6018. }
  6019. bool number_integer(number_integer_t val)
  6020. {
  6021. handle_value(val);
  6022. return true;
  6023. }
  6024. bool number_unsigned(number_unsigned_t val)
  6025. {
  6026. handle_value(val);
  6027. return true;
  6028. }
  6029. bool number_float(number_float_t val, const string_t& /*unused*/)
  6030. {
  6031. handle_value(val);
  6032. return true;
  6033. }
  6034. bool string(string_t& val)
  6035. {
  6036. handle_value(val);
  6037. return true;
  6038. }
  6039. bool binary(binary_t& val)
  6040. {
  6041. handle_value(std::move(val));
  6042. return true;
  6043. }
  6044. bool start_object(std::size_t len)
  6045. {
  6046. // check callback for object start
  6047. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  6048. keep_stack.push_back(keep);
  6049. auto val = handle_value(BasicJsonType::value_t::object, true);
  6050. ref_stack.push_back(val.second);
  6051. // check object limit
  6052. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6053. {
  6054. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  6055. }
  6056. return true;
  6057. }
  6058. bool key(string_t& val)
  6059. {
  6060. BasicJsonType k = BasicJsonType(val);
  6061. // check callback for key
  6062. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  6063. key_keep_stack.push_back(keep);
  6064. // add discarded value at given key and store the reference for later
  6065. if (keep && ref_stack.back())
  6066. {
  6067. object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
  6068. }
  6069. return true;
  6070. }
  6071. bool end_object()
  6072. {
  6073. if (ref_stack.back())
  6074. {
  6075. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  6076. {
  6077. // discard object
  6078. *ref_stack.back() = discarded;
  6079. }
  6080. else
  6081. {
  6082. ref_stack.back()->set_parents();
  6083. }
  6084. }
  6085. JSON_ASSERT(!ref_stack.empty());
  6086. JSON_ASSERT(!keep_stack.empty());
  6087. ref_stack.pop_back();
  6088. keep_stack.pop_back();
  6089. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  6090. {
  6091. // remove discarded value
  6092. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  6093. {
  6094. if (it->is_discarded())
  6095. {
  6096. ref_stack.back()->erase(it);
  6097. break;
  6098. }
  6099. }
  6100. }
  6101. return true;
  6102. }
  6103. bool start_array(std::size_t len)
  6104. {
  6105. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  6106. keep_stack.push_back(keep);
  6107. auto val = handle_value(BasicJsonType::value_t::array, true);
  6108. ref_stack.push_back(val.second);
  6109. // check array limit
  6110. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6111. {
  6112. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  6113. }
  6114. return true;
  6115. }
  6116. bool end_array()
  6117. {
  6118. bool keep = true;
  6119. if (ref_stack.back())
  6120. {
  6121. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  6122. if (keep)
  6123. {
  6124. ref_stack.back()->set_parents();
  6125. }
  6126. else
  6127. {
  6128. // discard array
  6129. *ref_stack.back() = discarded;
  6130. }
  6131. }
  6132. JSON_ASSERT(!ref_stack.empty());
  6133. JSON_ASSERT(!keep_stack.empty());
  6134. ref_stack.pop_back();
  6135. keep_stack.pop_back();
  6136. // remove discarded value
  6137. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  6138. {
  6139. ref_stack.back()->m_value.array->pop_back();
  6140. }
  6141. return true;
  6142. }
  6143. template<class Exception>
  6144. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  6145. const Exception& ex)
  6146. {
  6147. errored = true;
  6148. static_cast<void>(ex);
  6149. if (allow_exceptions)
  6150. {
  6151. JSON_THROW(ex);
  6152. }
  6153. return false;
  6154. }
  6155. constexpr bool is_errored() const
  6156. {
  6157. return errored;
  6158. }
  6159. private:
  6160. /*!
  6161. @param[in] v value to add to the JSON value we build during parsing
  6162. @param[in] skip_callback whether we should skip calling the callback
  6163. function; this is required after start_array() and
  6164. start_object() SAX events, because otherwise we would call the
  6165. callback function with an empty array or object, respectively.
  6166. @invariant If the ref stack is empty, then the passed value will be the new
  6167. root.
  6168. @invariant If the ref stack contains a value, then it is an array or an
  6169. object to which we can add elements
  6170. @return pair of boolean (whether value should be kept) and pointer (to the
  6171. passed value in the ref_stack hierarchy; nullptr if not kept)
  6172. */
  6173. template<typename Value>
  6174. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  6175. {
  6176. JSON_ASSERT(!keep_stack.empty());
  6177. // do not handle this value if we know it would be added to a discarded
  6178. // container
  6179. if (!keep_stack.back())
  6180. {
  6181. return {false, nullptr};
  6182. }
  6183. // create value
  6184. auto value = BasicJsonType(std::forward<Value>(v));
  6185. // check callback
  6186. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  6187. // do not handle this value if we just learnt it shall be discarded
  6188. if (!keep)
  6189. {
  6190. return {false, nullptr};
  6191. }
  6192. if (ref_stack.empty())
  6193. {
  6194. root = std::move(value);
  6195. return {true, &root};
  6196. }
  6197. // skip this value if we already decided to skip the parent
  6198. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  6199. if (!ref_stack.back())
  6200. {
  6201. return {false, nullptr};
  6202. }
  6203. // we now only expect arrays and objects
  6204. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6205. // array
  6206. if (ref_stack.back()->is_array())
  6207. {
  6208. ref_stack.back()->m_value.array->emplace_back(std::move(value));
  6209. return {true, &(ref_stack.back()->m_value.array->back())};
  6210. }
  6211. // object
  6212. JSON_ASSERT(ref_stack.back()->is_object());
  6213. // check if we should store an element for the current key
  6214. JSON_ASSERT(!key_keep_stack.empty());
  6215. const bool store_element = key_keep_stack.back();
  6216. key_keep_stack.pop_back();
  6217. if (!store_element)
  6218. {
  6219. return {false, nullptr};
  6220. }
  6221. JSON_ASSERT(object_element);
  6222. *object_element = std::move(value);
  6223. return {true, object_element};
  6224. }
  6225. /// the parsed JSON value
  6226. BasicJsonType& root;
  6227. /// stack to model hierarchy of values
  6228. std::vector<BasicJsonType*> ref_stack {};
  6229. /// stack to manage which values to keep
  6230. std::vector<bool> keep_stack {};
  6231. /// stack to manage which object keys to keep
  6232. std::vector<bool> key_keep_stack {};
  6233. /// helper to hold the reference for the next object element
  6234. BasicJsonType* object_element = nullptr;
  6235. /// whether a syntax error occurred
  6236. bool errored = false;
  6237. /// callback function
  6238. const parser_callback_t callback = nullptr;
  6239. /// whether to throw exceptions in case of errors
  6240. const bool allow_exceptions = true;
  6241. /// a discarded value for the callback
  6242. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  6243. };
  6244. template<typename BasicJsonType>
  6245. class json_sax_acceptor
  6246. {
  6247. public:
  6248. using number_integer_t = typename BasicJsonType::number_integer_t;
  6249. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6250. using number_float_t = typename BasicJsonType::number_float_t;
  6251. using string_t = typename BasicJsonType::string_t;
  6252. using binary_t = typename BasicJsonType::binary_t;
  6253. bool null()
  6254. {
  6255. return true;
  6256. }
  6257. bool boolean(bool /*unused*/)
  6258. {
  6259. return true;
  6260. }
  6261. bool number_integer(number_integer_t /*unused*/)
  6262. {
  6263. return true;
  6264. }
  6265. bool number_unsigned(number_unsigned_t /*unused*/)
  6266. {
  6267. return true;
  6268. }
  6269. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  6270. {
  6271. return true;
  6272. }
  6273. bool string(string_t& /*unused*/)
  6274. {
  6275. return true;
  6276. }
  6277. bool binary(binary_t& /*unused*/)
  6278. {
  6279. return true;
  6280. }
  6281. bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6282. {
  6283. return true;
  6284. }
  6285. bool key(string_t& /*unused*/)
  6286. {
  6287. return true;
  6288. }
  6289. bool end_object()
  6290. {
  6291. return true;
  6292. }
  6293. bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6294. {
  6295. return true;
  6296. }
  6297. bool end_array()
  6298. {
  6299. return true;
  6300. }
  6301. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  6302. {
  6303. return false;
  6304. }
  6305. };
  6306. } // namespace detail
  6307. NLOHMANN_JSON_NAMESPACE_END
  6308. // #include <nlohmann/detail/input/lexer.hpp>
  6309. // __ _____ _____ _____
  6310. // __| | __| | | | JSON for Modern C++
  6311. // | | |__ | | | | | | version 3.11.2
  6312. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6313. //
  6314. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  6315. // SPDX-License-Identifier: MIT
  6316. #include <array> // array
  6317. #include <clocale> // localeconv
  6318. #include <cstddef> // size_t
  6319. #include <cstdio> // snprintf
  6320. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6321. #include <initializer_list> // initializer_list
  6322. #include <string> // char_traits, string
  6323. #include <utility> // move
  6324. #include <vector> // vector
  6325. // #include <nlohmann/detail/input/input_adapters.hpp>
  6326. // #include <nlohmann/detail/input/position_t.hpp>
  6327. // #include <nlohmann/detail/macro_scope.hpp>
  6328. NLOHMANN_JSON_NAMESPACE_BEGIN
  6329. namespace detail
  6330. {
  6331. ///////////
  6332. // lexer //
  6333. ///////////
  6334. template<typename BasicJsonType>
  6335. class lexer_base
  6336. {
  6337. public:
  6338. /// token types for the parser
  6339. enum class token_type
  6340. {
  6341. uninitialized, ///< indicating the scanner is uninitialized
  6342. literal_true, ///< the `true` literal
  6343. literal_false, ///< the `false` literal
  6344. literal_null, ///< the `null` literal
  6345. value_string, ///< a string -- use get_string() for actual value
  6346. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6347. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6348. value_float, ///< an floating point number -- use get_number_float() for actual value
  6349. begin_array, ///< the character for array begin `[`
  6350. begin_object, ///< the character for object begin `{`
  6351. end_array, ///< the character for array end `]`
  6352. end_object, ///< the character for object end `}`
  6353. name_separator, ///< the name separator `:`
  6354. value_separator, ///< the value separator `,`
  6355. parse_error, ///< indicating a parse error
  6356. end_of_input, ///< indicating the end of the input buffer
  6357. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6358. };
  6359. /// return name of values of type token_type (only used for errors)
  6360. JSON_HEDLEY_RETURNS_NON_NULL
  6361. JSON_HEDLEY_CONST
  6362. static const char* token_type_name(const token_type t) noexcept
  6363. {
  6364. switch (t)
  6365. {
  6366. case token_type::uninitialized:
  6367. return "<uninitialized>";
  6368. case token_type::literal_true:
  6369. return "true literal";
  6370. case token_type::literal_false:
  6371. return "false literal";
  6372. case token_type::literal_null:
  6373. return "null literal";
  6374. case token_type::value_string:
  6375. return "string literal";
  6376. case token_type::value_unsigned:
  6377. case token_type::value_integer:
  6378. case token_type::value_float:
  6379. return "number literal";
  6380. case token_type::begin_array:
  6381. return "'['";
  6382. case token_type::begin_object:
  6383. return "'{'";
  6384. case token_type::end_array:
  6385. return "']'";
  6386. case token_type::end_object:
  6387. return "'}'";
  6388. case token_type::name_separator:
  6389. return "':'";
  6390. case token_type::value_separator:
  6391. return "','";
  6392. case token_type::parse_error:
  6393. return "<parse error>";
  6394. case token_type::end_of_input:
  6395. return "end of input";
  6396. case token_type::literal_or_value:
  6397. return "'[', '{', or a literal";
  6398. // LCOV_EXCL_START
  6399. default: // catch non-enum values
  6400. return "unknown token";
  6401. // LCOV_EXCL_STOP
  6402. }
  6403. }
  6404. };
  6405. /*!
  6406. @brief lexical analysis
  6407. This class organizes the lexical analysis during JSON deserialization.
  6408. */
  6409. template<typename BasicJsonType, typename InputAdapterType>
  6410. class lexer : public lexer_base<BasicJsonType>
  6411. {
  6412. using number_integer_t = typename BasicJsonType::number_integer_t;
  6413. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6414. using number_float_t = typename BasicJsonType::number_float_t;
  6415. using string_t = typename BasicJsonType::string_t;
  6416. using char_type = typename InputAdapterType::char_type;
  6417. using char_int_type = typename std::char_traits<char_type>::int_type;
  6418. public:
  6419. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6420. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6421. : ia(std::move(adapter))
  6422. , ignore_comments(ignore_comments_)
  6423. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6424. {}
  6425. // delete because of pointer members
  6426. lexer(const lexer&) = delete;
  6427. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6428. lexer& operator=(lexer&) = delete;
  6429. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6430. ~lexer() = default;
  6431. private:
  6432. /////////////////////
  6433. // locales
  6434. /////////////////////
  6435. /// return the locale-dependent decimal point
  6436. JSON_HEDLEY_PURE
  6437. static char get_decimal_point() noexcept
  6438. {
  6439. const auto* loc = localeconv();
  6440. JSON_ASSERT(loc != nullptr);
  6441. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6442. }
  6443. /////////////////////
  6444. // scan functions
  6445. /////////////////////
  6446. /*!
  6447. @brief get codepoint from 4 hex characters following `\u`
  6448. For input "\u c1 c2 c3 c4" the codepoint is:
  6449. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6450. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6451. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6452. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6453. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6454. between the ASCII value of the character and the desired integer value.
  6455. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6456. non-hex character)
  6457. */
  6458. int get_codepoint()
  6459. {
  6460. // this function only makes sense after reading `\u`
  6461. JSON_ASSERT(current == 'u');
  6462. int codepoint = 0;
  6463. const auto factors = { 12u, 8u, 4u, 0u };
  6464. for (const auto factor : factors)
  6465. {
  6466. get();
  6467. if (current >= '0' && current <= '9')
  6468. {
  6469. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6470. }
  6471. else if (current >= 'A' && current <= 'F')
  6472. {
  6473. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6474. }
  6475. else if (current >= 'a' && current <= 'f')
  6476. {
  6477. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6478. }
  6479. else
  6480. {
  6481. return -1;
  6482. }
  6483. }
  6484. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6485. return codepoint;
  6486. }
  6487. /*!
  6488. @brief check if the next byte(s) are inside a given range
  6489. Adds the current byte and, for each passed range, reads a new byte and
  6490. checks if it is inside the range. If a violation was detected, set up an
  6491. error message and return false. Otherwise, return true.
  6492. @param[in] ranges list of integers; interpreted as list of pairs of
  6493. inclusive lower and upper bound, respectively
  6494. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6495. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6496. @return true if and only if no range violation was detected
  6497. */
  6498. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6499. {
  6500. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6501. add(current);
  6502. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6503. {
  6504. get();
  6505. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range)))
  6506. {
  6507. add(current);
  6508. }
  6509. else
  6510. {
  6511. error_message = "invalid string: ill-formed UTF-8 byte";
  6512. return false;
  6513. }
  6514. }
  6515. return true;
  6516. }
  6517. /*!
  6518. @brief scan a string literal
  6519. This function scans a string according to Sect. 7 of RFC 8259. While
  6520. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6521. function returns successfully, token_buffer is *not* null-terminated (as it
  6522. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6523. string.
  6524. @return token_type::value_string if string could be successfully scanned,
  6525. token_type::parse_error otherwise
  6526. @note In case of errors, variable error_message contains a textual
  6527. description.
  6528. */
  6529. token_type scan_string()
  6530. {
  6531. // reset token_buffer (ignore opening quote)
  6532. reset();
  6533. // we entered the function by reading an open quote
  6534. JSON_ASSERT(current == '\"');
  6535. while (true)
  6536. {
  6537. // get next character
  6538. switch (get())
  6539. {
  6540. // end of file while parsing string
  6541. case std::char_traits<char_type>::eof():
  6542. {
  6543. error_message = "invalid string: missing closing quote";
  6544. return token_type::parse_error;
  6545. }
  6546. // closing quote
  6547. case '\"':
  6548. {
  6549. return token_type::value_string;
  6550. }
  6551. // escapes
  6552. case '\\':
  6553. {
  6554. switch (get())
  6555. {
  6556. // quotation mark
  6557. case '\"':
  6558. add('\"');
  6559. break;
  6560. // reverse solidus
  6561. case '\\':
  6562. add('\\');
  6563. break;
  6564. // solidus
  6565. case '/':
  6566. add('/');
  6567. break;
  6568. // backspace
  6569. case 'b':
  6570. add('\b');
  6571. break;
  6572. // form feed
  6573. case 'f':
  6574. add('\f');
  6575. break;
  6576. // line feed
  6577. case 'n':
  6578. add('\n');
  6579. break;
  6580. // carriage return
  6581. case 'r':
  6582. add('\r');
  6583. break;
  6584. // tab
  6585. case 't':
  6586. add('\t');
  6587. break;
  6588. // unicode escapes
  6589. case 'u':
  6590. {
  6591. const int codepoint1 = get_codepoint();
  6592. int codepoint = codepoint1; // start with codepoint1
  6593. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6594. {
  6595. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6596. return token_type::parse_error;
  6597. }
  6598. // check if code point is a high surrogate
  6599. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6600. {
  6601. // expect next \uxxxx entry
  6602. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6603. {
  6604. const int codepoint2 = get_codepoint();
  6605. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6606. {
  6607. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6608. return token_type::parse_error;
  6609. }
  6610. // check if codepoint2 is a low surrogate
  6611. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6612. {
  6613. // overwrite codepoint
  6614. codepoint = static_cast<int>(
  6615. // high surrogate occupies the most significant 22 bits
  6616. (static_cast<unsigned int>(codepoint1) << 10u)
  6617. // low surrogate occupies the least significant 15 bits
  6618. + static_cast<unsigned int>(codepoint2)
  6619. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6620. // in the result, so we have to subtract with:
  6621. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6622. - 0x35FDC00u);
  6623. }
  6624. else
  6625. {
  6626. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6627. return token_type::parse_error;
  6628. }
  6629. }
  6630. else
  6631. {
  6632. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6633. return token_type::parse_error;
  6634. }
  6635. }
  6636. else
  6637. {
  6638. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6639. {
  6640. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6641. return token_type::parse_error;
  6642. }
  6643. }
  6644. // result of the above calculation yields a proper codepoint
  6645. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6646. // translate codepoint into bytes
  6647. if (codepoint < 0x80)
  6648. {
  6649. // 1-byte characters: 0xxxxxxx (ASCII)
  6650. add(static_cast<char_int_type>(codepoint));
  6651. }
  6652. else if (codepoint <= 0x7FF)
  6653. {
  6654. // 2-byte characters: 110xxxxx 10xxxxxx
  6655. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6656. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6657. }
  6658. else if (codepoint <= 0xFFFF)
  6659. {
  6660. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6661. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6662. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6663. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6664. }
  6665. else
  6666. {
  6667. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6668. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6669. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6670. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6671. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6672. }
  6673. break;
  6674. }
  6675. // other characters after escape
  6676. default:
  6677. error_message = "invalid string: forbidden character after backslash";
  6678. return token_type::parse_error;
  6679. }
  6680. break;
  6681. }
  6682. // invalid control characters
  6683. case 0x00:
  6684. {
  6685. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6686. return token_type::parse_error;
  6687. }
  6688. case 0x01:
  6689. {
  6690. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6691. return token_type::parse_error;
  6692. }
  6693. case 0x02:
  6694. {
  6695. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6696. return token_type::parse_error;
  6697. }
  6698. case 0x03:
  6699. {
  6700. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6701. return token_type::parse_error;
  6702. }
  6703. case 0x04:
  6704. {
  6705. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6706. return token_type::parse_error;
  6707. }
  6708. case 0x05:
  6709. {
  6710. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6711. return token_type::parse_error;
  6712. }
  6713. case 0x06:
  6714. {
  6715. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6716. return token_type::parse_error;
  6717. }
  6718. case 0x07:
  6719. {
  6720. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6721. return token_type::parse_error;
  6722. }
  6723. case 0x08:
  6724. {
  6725. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6726. return token_type::parse_error;
  6727. }
  6728. case 0x09:
  6729. {
  6730. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6731. return token_type::parse_error;
  6732. }
  6733. case 0x0A:
  6734. {
  6735. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6736. return token_type::parse_error;
  6737. }
  6738. case 0x0B:
  6739. {
  6740. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6741. return token_type::parse_error;
  6742. }
  6743. case 0x0C:
  6744. {
  6745. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6746. return token_type::parse_error;
  6747. }
  6748. case 0x0D:
  6749. {
  6750. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6751. return token_type::parse_error;
  6752. }
  6753. case 0x0E:
  6754. {
  6755. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6756. return token_type::parse_error;
  6757. }
  6758. case 0x0F:
  6759. {
  6760. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6761. return token_type::parse_error;
  6762. }
  6763. case 0x10:
  6764. {
  6765. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6766. return token_type::parse_error;
  6767. }
  6768. case 0x11:
  6769. {
  6770. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6771. return token_type::parse_error;
  6772. }
  6773. case 0x12:
  6774. {
  6775. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6776. return token_type::parse_error;
  6777. }
  6778. case 0x13:
  6779. {
  6780. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6781. return token_type::parse_error;
  6782. }
  6783. case 0x14:
  6784. {
  6785. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6786. return token_type::parse_error;
  6787. }
  6788. case 0x15:
  6789. {
  6790. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6791. return token_type::parse_error;
  6792. }
  6793. case 0x16:
  6794. {
  6795. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6796. return token_type::parse_error;
  6797. }
  6798. case 0x17:
  6799. {
  6800. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6801. return token_type::parse_error;
  6802. }
  6803. case 0x18:
  6804. {
  6805. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6806. return token_type::parse_error;
  6807. }
  6808. case 0x19:
  6809. {
  6810. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6811. return token_type::parse_error;
  6812. }
  6813. case 0x1A:
  6814. {
  6815. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6816. return token_type::parse_error;
  6817. }
  6818. case 0x1B:
  6819. {
  6820. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6821. return token_type::parse_error;
  6822. }
  6823. case 0x1C:
  6824. {
  6825. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6826. return token_type::parse_error;
  6827. }
  6828. case 0x1D:
  6829. {
  6830. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6831. return token_type::parse_error;
  6832. }
  6833. case 0x1E:
  6834. {
  6835. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6836. return token_type::parse_error;
  6837. }
  6838. case 0x1F:
  6839. {
  6840. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6841. return token_type::parse_error;
  6842. }
  6843. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6844. case 0x20:
  6845. case 0x21:
  6846. case 0x23:
  6847. case 0x24:
  6848. case 0x25:
  6849. case 0x26:
  6850. case 0x27:
  6851. case 0x28:
  6852. case 0x29:
  6853. case 0x2A:
  6854. case 0x2B:
  6855. case 0x2C:
  6856. case 0x2D:
  6857. case 0x2E:
  6858. case 0x2F:
  6859. case 0x30:
  6860. case 0x31:
  6861. case 0x32:
  6862. case 0x33:
  6863. case 0x34:
  6864. case 0x35:
  6865. case 0x36:
  6866. case 0x37:
  6867. case 0x38:
  6868. case 0x39:
  6869. case 0x3A:
  6870. case 0x3B:
  6871. case 0x3C:
  6872. case 0x3D:
  6873. case 0x3E:
  6874. case 0x3F:
  6875. case 0x40:
  6876. case 0x41:
  6877. case 0x42:
  6878. case 0x43:
  6879. case 0x44:
  6880. case 0x45:
  6881. case 0x46:
  6882. case 0x47:
  6883. case 0x48:
  6884. case 0x49:
  6885. case 0x4A:
  6886. case 0x4B:
  6887. case 0x4C:
  6888. case 0x4D:
  6889. case 0x4E:
  6890. case 0x4F:
  6891. case 0x50:
  6892. case 0x51:
  6893. case 0x52:
  6894. case 0x53:
  6895. case 0x54:
  6896. case 0x55:
  6897. case 0x56:
  6898. case 0x57:
  6899. case 0x58:
  6900. case 0x59:
  6901. case 0x5A:
  6902. case 0x5B:
  6903. case 0x5D:
  6904. case 0x5E:
  6905. case 0x5F:
  6906. case 0x60:
  6907. case 0x61:
  6908. case 0x62:
  6909. case 0x63:
  6910. case 0x64:
  6911. case 0x65:
  6912. case 0x66:
  6913. case 0x67:
  6914. case 0x68:
  6915. case 0x69:
  6916. case 0x6A:
  6917. case 0x6B:
  6918. case 0x6C:
  6919. case 0x6D:
  6920. case 0x6E:
  6921. case 0x6F:
  6922. case 0x70:
  6923. case 0x71:
  6924. case 0x72:
  6925. case 0x73:
  6926. case 0x74:
  6927. case 0x75:
  6928. case 0x76:
  6929. case 0x77:
  6930. case 0x78:
  6931. case 0x79:
  6932. case 0x7A:
  6933. case 0x7B:
  6934. case 0x7C:
  6935. case 0x7D:
  6936. case 0x7E:
  6937. case 0x7F:
  6938. {
  6939. add(current);
  6940. break;
  6941. }
  6942. // U+0080..U+07FF: bytes C2..DF 80..BF
  6943. case 0xC2:
  6944. case 0xC3:
  6945. case 0xC4:
  6946. case 0xC5:
  6947. case 0xC6:
  6948. case 0xC7:
  6949. case 0xC8:
  6950. case 0xC9:
  6951. case 0xCA:
  6952. case 0xCB:
  6953. case 0xCC:
  6954. case 0xCD:
  6955. case 0xCE:
  6956. case 0xCF:
  6957. case 0xD0:
  6958. case 0xD1:
  6959. case 0xD2:
  6960. case 0xD3:
  6961. case 0xD4:
  6962. case 0xD5:
  6963. case 0xD6:
  6964. case 0xD7:
  6965. case 0xD8:
  6966. case 0xD9:
  6967. case 0xDA:
  6968. case 0xDB:
  6969. case 0xDC:
  6970. case 0xDD:
  6971. case 0xDE:
  6972. case 0xDF:
  6973. {
  6974. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6975. {
  6976. return token_type::parse_error;
  6977. }
  6978. break;
  6979. }
  6980. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6981. case 0xE0:
  6982. {
  6983. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6984. {
  6985. return token_type::parse_error;
  6986. }
  6987. break;
  6988. }
  6989. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6990. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6991. case 0xE1:
  6992. case 0xE2:
  6993. case 0xE3:
  6994. case 0xE4:
  6995. case 0xE5:
  6996. case 0xE6:
  6997. case 0xE7:
  6998. case 0xE8:
  6999. case 0xE9:
  7000. case 0xEA:
  7001. case 0xEB:
  7002. case 0xEC:
  7003. case 0xEE:
  7004. case 0xEF:
  7005. {
  7006. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  7007. {
  7008. return token_type::parse_error;
  7009. }
  7010. break;
  7011. }
  7012. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  7013. case 0xED:
  7014. {
  7015. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  7016. {
  7017. return token_type::parse_error;
  7018. }
  7019. break;
  7020. }
  7021. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  7022. case 0xF0:
  7023. {
  7024. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7025. {
  7026. return token_type::parse_error;
  7027. }
  7028. break;
  7029. }
  7030. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  7031. case 0xF1:
  7032. case 0xF2:
  7033. case 0xF3:
  7034. {
  7035. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7036. {
  7037. return token_type::parse_error;
  7038. }
  7039. break;
  7040. }
  7041. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  7042. case 0xF4:
  7043. {
  7044. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  7045. {
  7046. return token_type::parse_error;
  7047. }
  7048. break;
  7049. }
  7050. // remaining bytes (80..C1 and F5..FF) are ill-formed
  7051. default:
  7052. {
  7053. error_message = "invalid string: ill-formed UTF-8 byte";
  7054. return token_type::parse_error;
  7055. }
  7056. }
  7057. }
  7058. }
  7059. /*!
  7060. * @brief scan a comment
  7061. * @return whether comment could be scanned successfully
  7062. */
  7063. bool scan_comment()
  7064. {
  7065. switch (get())
  7066. {
  7067. // single-line comments skip input until a newline or EOF is read
  7068. case '/':
  7069. {
  7070. while (true)
  7071. {
  7072. switch (get())
  7073. {
  7074. case '\n':
  7075. case '\r':
  7076. case std::char_traits<char_type>::eof():
  7077. case '\0':
  7078. return true;
  7079. default:
  7080. break;
  7081. }
  7082. }
  7083. }
  7084. // multi-line comments skip input until */ is read
  7085. case '*':
  7086. {
  7087. while (true)
  7088. {
  7089. switch (get())
  7090. {
  7091. case std::char_traits<char_type>::eof():
  7092. case '\0':
  7093. {
  7094. error_message = "invalid comment; missing closing '*/'";
  7095. return false;
  7096. }
  7097. case '*':
  7098. {
  7099. switch (get())
  7100. {
  7101. case '/':
  7102. return true;
  7103. default:
  7104. {
  7105. unget();
  7106. continue;
  7107. }
  7108. }
  7109. }
  7110. default:
  7111. continue;
  7112. }
  7113. }
  7114. }
  7115. // unexpected character after reading '/'
  7116. default:
  7117. {
  7118. error_message = "invalid comment; expecting '/' or '*' after '/'";
  7119. return false;
  7120. }
  7121. }
  7122. }
  7123. JSON_HEDLEY_NON_NULL(2)
  7124. static void strtof(float& f, const char* str, char** endptr) noexcept
  7125. {
  7126. f = std::strtof(str, endptr);
  7127. }
  7128. JSON_HEDLEY_NON_NULL(2)
  7129. static void strtof(double& f, const char* str, char** endptr) noexcept
  7130. {
  7131. f = std::strtod(str, endptr);
  7132. }
  7133. JSON_HEDLEY_NON_NULL(2)
  7134. static void strtof(long double& f, const char* str, char** endptr) noexcept
  7135. {
  7136. f = std::strtold(str, endptr);
  7137. }
  7138. /*!
  7139. @brief scan a number literal
  7140. This function scans a string according to Sect. 6 of RFC 8259.
  7141. The function is realized with a deterministic finite state machine derived
  7142. from the grammar described in RFC 8259. Starting in state "init", the
  7143. input is read and used to determined the next state. Only state "done"
  7144. accepts the number. State "error" is a trap state to model errors. In the
  7145. table below, "anything" means any character but the ones listed before.
  7146. state | 0 | 1-9 | e E | + | - | . | anything
  7147. ---------|----------|----------|----------|---------|---------|----------|-----------
  7148. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  7149. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  7150. zero | done | done | exponent | done | done | decimal1 | done
  7151. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  7152. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  7153. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  7154. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  7155. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  7156. any2 | any2 | any2 | done | done | done | done | done
  7157. The state machine is realized with one label per state (prefixed with
  7158. "scan_number_") and `goto` statements between them. The state machine
  7159. contains cycles, but any cycle can be left when EOF is read. Therefore,
  7160. the function is guaranteed to terminate.
  7161. During scanning, the read bytes are stored in token_buffer. This string is
  7162. then converted to a signed integer, an unsigned integer, or a
  7163. floating-point number.
  7164. @return token_type::value_unsigned, token_type::value_integer, or
  7165. token_type::value_float if number could be successfully scanned,
  7166. token_type::parse_error otherwise
  7167. @note The scanner is independent of the current locale. Internally, the
  7168. locale's decimal point is used instead of `.` to work with the
  7169. locale-dependent converters.
  7170. */
  7171. token_type scan_number() // lgtm [cpp/use-of-goto]
  7172. {
  7173. // reset token_buffer to store the number's bytes
  7174. reset();
  7175. // the type of the parsed number; initially set to unsigned; will be
  7176. // changed if minus sign, decimal point or exponent is read
  7177. token_type number_type = token_type::value_unsigned;
  7178. // state (init): we just found out we need to scan a number
  7179. switch (current)
  7180. {
  7181. case '-':
  7182. {
  7183. add(current);
  7184. goto scan_number_minus;
  7185. }
  7186. case '0':
  7187. {
  7188. add(current);
  7189. goto scan_number_zero;
  7190. }
  7191. case '1':
  7192. case '2':
  7193. case '3':
  7194. case '4':
  7195. case '5':
  7196. case '6':
  7197. case '7':
  7198. case '8':
  7199. case '9':
  7200. {
  7201. add(current);
  7202. goto scan_number_any1;
  7203. }
  7204. // all other characters are rejected outside scan_number()
  7205. default: // LCOV_EXCL_LINE
  7206. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7207. }
  7208. scan_number_minus:
  7209. // state: we just parsed a leading minus sign
  7210. number_type = token_type::value_integer;
  7211. switch (get())
  7212. {
  7213. case '0':
  7214. {
  7215. add(current);
  7216. goto scan_number_zero;
  7217. }
  7218. case '1':
  7219. case '2':
  7220. case '3':
  7221. case '4':
  7222. case '5':
  7223. case '6':
  7224. case '7':
  7225. case '8':
  7226. case '9':
  7227. {
  7228. add(current);
  7229. goto scan_number_any1;
  7230. }
  7231. default:
  7232. {
  7233. error_message = "invalid number; expected digit after '-'";
  7234. return token_type::parse_error;
  7235. }
  7236. }
  7237. scan_number_zero:
  7238. // state: we just parse a zero (maybe with a leading minus sign)
  7239. switch (get())
  7240. {
  7241. case '.':
  7242. {
  7243. add(decimal_point_char);
  7244. goto scan_number_decimal1;
  7245. }
  7246. case 'e':
  7247. case 'E':
  7248. {
  7249. add(current);
  7250. goto scan_number_exponent;
  7251. }
  7252. default:
  7253. goto scan_number_done;
  7254. }
  7255. scan_number_any1:
  7256. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7257. switch (get())
  7258. {
  7259. case '0':
  7260. case '1':
  7261. case '2':
  7262. case '3':
  7263. case '4':
  7264. case '5':
  7265. case '6':
  7266. case '7':
  7267. case '8':
  7268. case '9':
  7269. {
  7270. add(current);
  7271. goto scan_number_any1;
  7272. }
  7273. case '.':
  7274. {
  7275. add(decimal_point_char);
  7276. goto scan_number_decimal1;
  7277. }
  7278. case 'e':
  7279. case 'E':
  7280. {
  7281. add(current);
  7282. goto scan_number_exponent;
  7283. }
  7284. default:
  7285. goto scan_number_done;
  7286. }
  7287. scan_number_decimal1:
  7288. // state: we just parsed a decimal point
  7289. number_type = token_type::value_float;
  7290. switch (get())
  7291. {
  7292. case '0':
  7293. case '1':
  7294. case '2':
  7295. case '3':
  7296. case '4':
  7297. case '5':
  7298. case '6':
  7299. case '7':
  7300. case '8':
  7301. case '9':
  7302. {
  7303. add(current);
  7304. goto scan_number_decimal2;
  7305. }
  7306. default:
  7307. {
  7308. error_message = "invalid number; expected digit after '.'";
  7309. return token_type::parse_error;
  7310. }
  7311. }
  7312. scan_number_decimal2:
  7313. // we just parsed at least one number after a decimal point
  7314. switch (get())
  7315. {
  7316. case '0':
  7317. case '1':
  7318. case '2':
  7319. case '3':
  7320. case '4':
  7321. case '5':
  7322. case '6':
  7323. case '7':
  7324. case '8':
  7325. case '9':
  7326. {
  7327. add(current);
  7328. goto scan_number_decimal2;
  7329. }
  7330. case 'e':
  7331. case 'E':
  7332. {
  7333. add(current);
  7334. goto scan_number_exponent;
  7335. }
  7336. default:
  7337. goto scan_number_done;
  7338. }
  7339. scan_number_exponent:
  7340. // we just parsed an exponent
  7341. number_type = token_type::value_float;
  7342. switch (get())
  7343. {
  7344. case '+':
  7345. case '-':
  7346. {
  7347. add(current);
  7348. goto scan_number_sign;
  7349. }
  7350. case '0':
  7351. case '1':
  7352. case '2':
  7353. case '3':
  7354. case '4':
  7355. case '5':
  7356. case '6':
  7357. case '7':
  7358. case '8':
  7359. case '9':
  7360. {
  7361. add(current);
  7362. goto scan_number_any2;
  7363. }
  7364. default:
  7365. {
  7366. error_message =
  7367. "invalid number; expected '+', '-', or digit after exponent";
  7368. return token_type::parse_error;
  7369. }
  7370. }
  7371. scan_number_sign:
  7372. // we just parsed an exponent sign
  7373. switch (get())
  7374. {
  7375. case '0':
  7376. case '1':
  7377. case '2':
  7378. case '3':
  7379. case '4':
  7380. case '5':
  7381. case '6':
  7382. case '7':
  7383. case '8':
  7384. case '9':
  7385. {
  7386. add(current);
  7387. goto scan_number_any2;
  7388. }
  7389. default:
  7390. {
  7391. error_message = "invalid number; expected digit after exponent sign";
  7392. return token_type::parse_error;
  7393. }
  7394. }
  7395. scan_number_any2:
  7396. // we just parsed a number after the exponent or exponent sign
  7397. switch (get())
  7398. {
  7399. case '0':
  7400. case '1':
  7401. case '2':
  7402. case '3':
  7403. case '4':
  7404. case '5':
  7405. case '6':
  7406. case '7':
  7407. case '8':
  7408. case '9':
  7409. {
  7410. add(current);
  7411. goto scan_number_any2;
  7412. }
  7413. default:
  7414. goto scan_number_done;
  7415. }
  7416. scan_number_done:
  7417. // unget the character after the number (we only read it to know that
  7418. // we are done scanning a number)
  7419. unget();
  7420. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7421. errno = 0;
  7422. // try to parse integers first and fall back to floats
  7423. if (number_type == token_type::value_unsigned)
  7424. {
  7425. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7426. // we checked the number format before
  7427. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7428. if (errno == 0)
  7429. {
  7430. value_unsigned = static_cast<number_unsigned_t>(x);
  7431. if (value_unsigned == x)
  7432. {
  7433. return token_type::value_unsigned;
  7434. }
  7435. }
  7436. }
  7437. else if (number_type == token_type::value_integer)
  7438. {
  7439. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7440. // we checked the number format before
  7441. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7442. if (errno == 0)
  7443. {
  7444. value_integer = static_cast<number_integer_t>(x);
  7445. if (value_integer == x)
  7446. {
  7447. return token_type::value_integer;
  7448. }
  7449. }
  7450. }
  7451. // this code is reached if we parse a floating-point number or if an
  7452. // integer conversion above failed
  7453. strtof(value_float, token_buffer.data(), &endptr);
  7454. // we checked the number format before
  7455. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7456. return token_type::value_float;
  7457. }
  7458. /*!
  7459. @param[in] literal_text the literal text to expect
  7460. @param[in] length the length of the passed literal text
  7461. @param[in] return_type the token type to return on success
  7462. */
  7463. JSON_HEDLEY_NON_NULL(2)
  7464. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7465. token_type return_type)
  7466. {
  7467. JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7468. for (std::size_t i = 1; i < length; ++i)
  7469. {
  7470. if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7471. {
  7472. error_message = "invalid literal";
  7473. return token_type::parse_error;
  7474. }
  7475. }
  7476. return return_type;
  7477. }
  7478. /////////////////////
  7479. // input management
  7480. /////////////////////
  7481. /// reset token_buffer; current character is beginning of token
  7482. void reset() noexcept
  7483. {
  7484. token_buffer.clear();
  7485. token_string.clear();
  7486. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7487. }
  7488. /*
  7489. @brief get next character from the input
  7490. This function provides the interface to the used input adapter. It does
  7491. not throw in case the input reached EOF, but returns a
  7492. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  7493. for use in error messages.
  7494. @return character read from the input
  7495. */
  7496. char_int_type get()
  7497. {
  7498. ++position.chars_read_total;
  7499. ++position.chars_read_current_line;
  7500. if (next_unget)
  7501. {
  7502. // just reset the next_unget variable and work with current
  7503. next_unget = false;
  7504. }
  7505. else
  7506. {
  7507. current = ia.get_character();
  7508. }
  7509. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7510. {
  7511. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7512. }
  7513. if (current == '\n')
  7514. {
  7515. ++position.lines_read;
  7516. position.chars_read_current_line = 0;
  7517. }
  7518. return current;
  7519. }
  7520. /*!
  7521. @brief unget current character (read it again on next get)
  7522. We implement unget by setting variable next_unget to true. The input is not
  7523. changed - we just simulate ungetting by modifying chars_read_total,
  7524. chars_read_current_line, and token_string. The next call to get() will
  7525. behave as if the unget character is read again.
  7526. */
  7527. void unget()
  7528. {
  7529. next_unget = true;
  7530. --position.chars_read_total;
  7531. // in case we "unget" a newline, we have to also decrement the lines_read
  7532. if (position.chars_read_current_line == 0)
  7533. {
  7534. if (position.lines_read > 0)
  7535. {
  7536. --position.lines_read;
  7537. }
  7538. }
  7539. else
  7540. {
  7541. --position.chars_read_current_line;
  7542. }
  7543. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7544. {
  7545. JSON_ASSERT(!token_string.empty());
  7546. token_string.pop_back();
  7547. }
  7548. }
  7549. /// add a character to token_buffer
  7550. void add(char_int_type c)
  7551. {
  7552. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7553. }
  7554. public:
  7555. /////////////////////
  7556. // value getters
  7557. /////////////////////
  7558. /// return integer value
  7559. constexpr number_integer_t get_number_integer() const noexcept
  7560. {
  7561. return value_integer;
  7562. }
  7563. /// return unsigned integer value
  7564. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7565. {
  7566. return value_unsigned;
  7567. }
  7568. /// return floating-point value
  7569. constexpr number_float_t get_number_float() const noexcept
  7570. {
  7571. return value_float;
  7572. }
  7573. /// return current string value (implicitly resets the token; useful only once)
  7574. string_t& get_string()
  7575. {
  7576. return token_buffer;
  7577. }
  7578. /////////////////////
  7579. // diagnostics
  7580. /////////////////////
  7581. /// return position of last read token
  7582. constexpr position_t get_position() const noexcept
  7583. {
  7584. return position;
  7585. }
  7586. /// return the last read token (for errors only). Will never contain EOF
  7587. /// (an arbitrary value that is not a valid char value, often -1), because
  7588. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7589. std::string get_token_string() const
  7590. {
  7591. // escape control characters
  7592. std::string result;
  7593. for (const auto c : token_string)
  7594. {
  7595. if (static_cast<unsigned char>(c) <= '\x1F')
  7596. {
  7597. // escape control characters
  7598. std::array<char, 9> cs{{}};
  7599. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7600. result += cs.data();
  7601. }
  7602. else
  7603. {
  7604. // add character as is
  7605. result.push_back(static_cast<std::string::value_type>(c));
  7606. }
  7607. }
  7608. return result;
  7609. }
  7610. /// return syntax error message
  7611. JSON_HEDLEY_RETURNS_NON_NULL
  7612. constexpr const char* get_error_message() const noexcept
  7613. {
  7614. return error_message;
  7615. }
  7616. /////////////////////
  7617. // actual scanner
  7618. /////////////////////
  7619. /*!
  7620. @brief skip the UTF-8 byte order mark
  7621. @return true iff there is no BOM or the correct BOM has been skipped
  7622. */
  7623. bool skip_bom()
  7624. {
  7625. if (get() == 0xEF)
  7626. {
  7627. // check if we completely parse the BOM
  7628. return get() == 0xBB && get() == 0xBF;
  7629. }
  7630. // the first character is not the beginning of the BOM; unget it to
  7631. // process is later
  7632. unget();
  7633. return true;
  7634. }
  7635. void skip_whitespace()
  7636. {
  7637. do
  7638. {
  7639. get();
  7640. }
  7641. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7642. }
  7643. token_type scan()
  7644. {
  7645. // initially, skip the BOM
  7646. if (position.chars_read_total == 0 && !skip_bom())
  7647. {
  7648. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7649. return token_type::parse_error;
  7650. }
  7651. // read next character and ignore whitespace
  7652. skip_whitespace();
  7653. // ignore comments
  7654. while (ignore_comments && current == '/')
  7655. {
  7656. if (!scan_comment())
  7657. {
  7658. return token_type::parse_error;
  7659. }
  7660. // skip following whitespace
  7661. skip_whitespace();
  7662. }
  7663. switch (current)
  7664. {
  7665. // structural characters
  7666. case '[':
  7667. return token_type::begin_array;
  7668. case ']':
  7669. return token_type::end_array;
  7670. case '{':
  7671. return token_type::begin_object;
  7672. case '}':
  7673. return token_type::end_object;
  7674. case ':':
  7675. return token_type::name_separator;
  7676. case ',':
  7677. return token_type::value_separator;
  7678. // literals
  7679. case 't':
  7680. {
  7681. 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')}};
  7682. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7683. }
  7684. case 'f':
  7685. {
  7686. 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')}};
  7687. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7688. }
  7689. case 'n':
  7690. {
  7691. 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')}};
  7692. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7693. }
  7694. // string
  7695. case '\"':
  7696. return scan_string();
  7697. // number
  7698. case '-':
  7699. case '0':
  7700. case '1':
  7701. case '2':
  7702. case '3':
  7703. case '4':
  7704. case '5':
  7705. case '6':
  7706. case '7':
  7707. case '8':
  7708. case '9':
  7709. return scan_number();
  7710. // end of input (the null byte is needed when parsing from
  7711. // string literals)
  7712. case '\0':
  7713. case std::char_traits<char_type>::eof():
  7714. return token_type::end_of_input;
  7715. // error
  7716. default:
  7717. error_message = "invalid literal";
  7718. return token_type::parse_error;
  7719. }
  7720. }
  7721. private:
  7722. /// input adapter
  7723. InputAdapterType ia;
  7724. /// whether comments should be ignored (true) or signaled as errors (false)
  7725. const bool ignore_comments = false;
  7726. /// the current character
  7727. char_int_type current = std::char_traits<char_type>::eof();
  7728. /// whether the next get() call should just return current
  7729. bool next_unget = false;
  7730. /// the start position of the current token
  7731. position_t position {};
  7732. /// raw input token string (for error messages)
  7733. std::vector<char_type> token_string {};
  7734. /// buffer for variable-length tokens (numbers, strings)
  7735. string_t token_buffer {};
  7736. /// a description of occurred lexer errors
  7737. const char* error_message = "";
  7738. // number values
  7739. number_integer_t value_integer = 0;
  7740. number_unsigned_t value_unsigned = 0;
  7741. number_float_t value_float = 0;
  7742. /// the decimal point
  7743. const char_int_type decimal_point_char = '.';
  7744. };
  7745. } // namespace detail
  7746. NLOHMANN_JSON_NAMESPACE_END
  7747. // #include <nlohmann/detail/macro_scope.hpp>
  7748. // #include <nlohmann/detail/meta/is_sax.hpp>
  7749. // __ _____ _____ _____
  7750. // __| | __| | | | JSON for Modern C++
  7751. // | | |__ | | | | | | version 3.11.2
  7752. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  7753. //
  7754. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7755. // SPDX-License-Identifier: MIT
  7756. #include <cstdint> // size_t
  7757. #include <utility> // declval
  7758. #include <string> // string
  7759. // #include <nlohmann/detail/abi_macros.hpp>
  7760. // #include <nlohmann/detail/meta/detected.hpp>
  7761. // #include <nlohmann/detail/meta/type_traits.hpp>
  7762. NLOHMANN_JSON_NAMESPACE_BEGIN
  7763. namespace detail
  7764. {
  7765. template<typename T>
  7766. using null_function_t = decltype(std::declval<T&>().null());
  7767. template<typename T>
  7768. using boolean_function_t =
  7769. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7770. template<typename T, typename Integer>
  7771. using number_integer_function_t =
  7772. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7773. template<typename T, typename Unsigned>
  7774. using number_unsigned_function_t =
  7775. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7776. template<typename T, typename Float, typename String>
  7777. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7778. std::declval<Float>(), std::declval<const String&>()));
  7779. template<typename T, typename String>
  7780. using string_function_t =
  7781. decltype(std::declval<T&>().string(std::declval<String&>()));
  7782. template<typename T, typename Binary>
  7783. using binary_function_t =
  7784. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7785. template<typename T>
  7786. using start_object_function_t =
  7787. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7788. template<typename T, typename String>
  7789. using key_function_t =
  7790. decltype(std::declval<T&>().key(std::declval<String&>()));
  7791. template<typename T>
  7792. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7793. template<typename T>
  7794. using start_array_function_t =
  7795. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7796. template<typename T>
  7797. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7798. template<typename T, typename Exception>
  7799. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7800. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7801. std::declval<const Exception&>()));
  7802. template<typename SAX, typename BasicJsonType>
  7803. struct is_sax
  7804. {
  7805. private:
  7806. static_assert(is_basic_json<BasicJsonType>::value,
  7807. "BasicJsonType must be of type basic_json<...>");
  7808. using number_integer_t = typename BasicJsonType::number_integer_t;
  7809. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7810. using number_float_t = typename BasicJsonType::number_float_t;
  7811. using string_t = typename BasicJsonType::string_t;
  7812. using binary_t = typename BasicJsonType::binary_t;
  7813. using exception_t = typename BasicJsonType::exception;
  7814. public:
  7815. static constexpr bool value =
  7816. is_detected_exact<bool, null_function_t, SAX>::value &&
  7817. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7818. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7819. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7820. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7821. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7822. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7823. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7824. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7825. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7826. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7827. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7828. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7829. };
  7830. template<typename SAX, typename BasicJsonType>
  7831. struct is_sax_static_asserts
  7832. {
  7833. private:
  7834. static_assert(is_basic_json<BasicJsonType>::value,
  7835. "BasicJsonType must be of type basic_json<...>");
  7836. using number_integer_t = typename BasicJsonType::number_integer_t;
  7837. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7838. using number_float_t = typename BasicJsonType::number_float_t;
  7839. using string_t = typename BasicJsonType::string_t;
  7840. using binary_t = typename BasicJsonType::binary_t;
  7841. using exception_t = typename BasicJsonType::exception;
  7842. public:
  7843. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7844. "Missing/invalid function: bool null()");
  7845. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7846. "Missing/invalid function: bool boolean(bool)");
  7847. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7848. "Missing/invalid function: bool boolean(bool)");
  7849. static_assert(
  7850. is_detected_exact<bool, number_integer_function_t, SAX,
  7851. number_integer_t>::value,
  7852. "Missing/invalid function: bool number_integer(number_integer_t)");
  7853. static_assert(
  7854. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7855. number_unsigned_t>::value,
  7856. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7857. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7858. number_float_t, string_t>::value,
  7859. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7860. static_assert(
  7861. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7862. "Missing/invalid function: bool string(string_t&)");
  7863. static_assert(
  7864. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7865. "Missing/invalid function: bool binary(binary_t&)");
  7866. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7867. "Missing/invalid function: bool start_object(std::size_t)");
  7868. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7869. "Missing/invalid function: bool key(string_t&)");
  7870. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7871. "Missing/invalid function: bool end_object()");
  7872. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7873. "Missing/invalid function: bool start_array(std::size_t)");
  7874. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7875. "Missing/invalid function: bool end_array()");
  7876. static_assert(
  7877. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7878. "Missing/invalid function: bool parse_error(std::size_t, const "
  7879. "std::string&, const exception&)");
  7880. };
  7881. } // namespace detail
  7882. NLOHMANN_JSON_NAMESPACE_END
  7883. // #include <nlohmann/detail/meta/type_traits.hpp>
  7884. // #include <nlohmann/detail/string_concat.hpp>
  7885. // #include <nlohmann/detail/value_t.hpp>
  7886. NLOHMANN_JSON_NAMESPACE_BEGIN
  7887. namespace detail
  7888. {
  7889. /// how to treat CBOR tags
  7890. enum class cbor_tag_handler_t
  7891. {
  7892. error, ///< throw a parse_error exception in case of a tag
  7893. ignore, ///< ignore tags
  7894. store ///< store tags as binary type
  7895. };
  7896. /*!
  7897. @brief determine system byte order
  7898. @return true if and only if system's byte order is little endian
  7899. @note from https://stackoverflow.com/a/1001328/266378
  7900. */
  7901. static inline bool little_endianness(int num = 1) noexcept
  7902. {
  7903. return *reinterpret_cast<char*>(&num) == 1;
  7904. }
  7905. ///////////////////
  7906. // binary reader //
  7907. ///////////////////
  7908. /*!
  7909. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7910. */
  7911. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7912. class binary_reader
  7913. {
  7914. using number_integer_t = typename BasicJsonType::number_integer_t;
  7915. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7916. using number_float_t = typename BasicJsonType::number_float_t;
  7917. using string_t = typename BasicJsonType::string_t;
  7918. using binary_t = typename BasicJsonType::binary_t;
  7919. using json_sax_t = SAX;
  7920. using char_type = typename InputAdapterType::char_type;
  7921. using char_int_type = typename std::char_traits<char_type>::int_type;
  7922. public:
  7923. /*!
  7924. @brief create a binary reader
  7925. @param[in] adapter input adapter to read from
  7926. */
  7927. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  7928. {
  7929. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7930. }
  7931. // make class move-only
  7932. binary_reader(const binary_reader&) = delete;
  7933. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7934. binary_reader& operator=(const binary_reader&) = delete;
  7935. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7936. ~binary_reader() = default;
  7937. /*!
  7938. @param[in] format the binary format to parse
  7939. @param[in] sax_ a SAX event processor
  7940. @param[in] strict whether to expect the input to be consumed completed
  7941. @param[in] tag_handler how to treat CBOR tags
  7942. @return whether parsing was successful
  7943. */
  7944. JSON_HEDLEY_NON_NULL(3)
  7945. bool sax_parse(const input_format_t format,
  7946. json_sax_t* sax_,
  7947. const bool strict = true,
  7948. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  7949. {
  7950. sax = sax_;
  7951. bool result = false;
  7952. switch (format)
  7953. {
  7954. case input_format_t::bson:
  7955. result = parse_bson_internal();
  7956. break;
  7957. case input_format_t::cbor:
  7958. result = parse_cbor_internal(true, tag_handler);
  7959. break;
  7960. case input_format_t::msgpack:
  7961. result = parse_msgpack_internal();
  7962. break;
  7963. case input_format_t::ubjson:
  7964. case input_format_t::bjdata:
  7965. result = parse_ubjson_internal();
  7966. break;
  7967. case input_format_t::json: // LCOV_EXCL_LINE
  7968. default: // LCOV_EXCL_LINE
  7969. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7970. }
  7971. // strict mode: next byte must be EOF
  7972. if (result && strict)
  7973. {
  7974. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  7975. {
  7976. get_ignore_noop();
  7977. }
  7978. else
  7979. {
  7980. get();
  7981. }
  7982. if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
  7983. {
  7984. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  7985. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  7986. }
  7987. }
  7988. return result;
  7989. }
  7990. private:
  7991. //////////
  7992. // BSON //
  7993. //////////
  7994. /*!
  7995. @brief Reads in a BSON-object and passes it to the SAX-parser.
  7996. @return whether a valid BSON-value was passed to the SAX parser
  7997. */
  7998. bool parse_bson_internal()
  7999. {
  8000. std::int32_t document_size{};
  8001. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8002. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  8003. {
  8004. return false;
  8005. }
  8006. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8007. {
  8008. return false;
  8009. }
  8010. return sax->end_object();
  8011. }
  8012. /*!
  8013. @brief Parses a C-style string from the BSON input.
  8014. @param[in,out] result A reference to the string variable where the read
  8015. string is to be stored.
  8016. @return `true` if the \x00-byte indicating the end of the string was
  8017. encountered before the EOF; false` indicates an unexpected EOF.
  8018. */
  8019. bool get_bson_cstr(string_t& result)
  8020. {
  8021. auto out = std::back_inserter(result);
  8022. while (true)
  8023. {
  8024. get();
  8025. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8026. {
  8027. return false;
  8028. }
  8029. if (current == 0x00)
  8030. {
  8031. return true;
  8032. }
  8033. *out++ = static_cast<typename string_t::value_type>(current);
  8034. }
  8035. }
  8036. /*!
  8037. @brief Parses a zero-terminated string of length @a len from the BSON
  8038. input.
  8039. @param[in] len The length (including the zero-byte at the end) of the
  8040. string to be read.
  8041. @param[in,out] result A reference to the string variable where the read
  8042. string is to be stored.
  8043. @tparam NumberType The type of the length @a len
  8044. @pre len >= 1
  8045. @return `true` if the string was successfully parsed
  8046. */
  8047. template<typename NumberType>
  8048. bool get_bson_string(const NumberType len, string_t& result)
  8049. {
  8050. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8051. {
  8052. auto last_token = get_token_string();
  8053. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8054. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8055. }
  8056. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
  8057. }
  8058. /*!
  8059. @brief Parses a byte array input of length @a len from the BSON input.
  8060. @param[in] len The length of the byte array to be read.
  8061. @param[in,out] result A reference to the binary variable where the read
  8062. array is to be stored.
  8063. @tparam NumberType The type of the length @a len
  8064. @pre len >= 0
  8065. @return `true` if the byte array was successfully parsed
  8066. */
  8067. template<typename NumberType>
  8068. bool get_bson_binary(const NumberType len, binary_t& result)
  8069. {
  8070. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8071. {
  8072. auto last_token = get_token_string();
  8073. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8074. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8075. }
  8076. // All BSON binary values have a subtype
  8077. std::uint8_t subtype{};
  8078. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8079. result.set_subtype(subtype);
  8080. return get_binary(input_format_t::bson, len, result);
  8081. }
  8082. /*!
  8083. @brief Read a BSON document element of the given @a element_type.
  8084. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8085. @param[in] element_type_parse_position The position in the input stream,
  8086. where the `element_type` was read.
  8087. @warning Not all BSON element types are supported yet. An unsupported
  8088. @a element_type will give rise to a parse_error.114:
  8089. Unsupported BSON record type 0x...
  8090. @return whether a valid BSON-object/array was passed to the SAX parser
  8091. */
  8092. bool parse_bson_element_internal(const char_int_type element_type,
  8093. const std::size_t element_type_parse_position)
  8094. {
  8095. switch (element_type)
  8096. {
  8097. case 0x01: // double
  8098. {
  8099. double number{};
  8100. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8101. }
  8102. case 0x02: // string
  8103. {
  8104. std::int32_t len{};
  8105. string_t value;
  8106. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8107. }
  8108. case 0x03: // object
  8109. {
  8110. return parse_bson_internal();
  8111. }
  8112. case 0x04: // array
  8113. {
  8114. return parse_bson_array();
  8115. }
  8116. case 0x05: // binary
  8117. {
  8118. std::int32_t len{};
  8119. binary_t value;
  8120. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8121. }
  8122. case 0x08: // boolean
  8123. {
  8124. return sax->boolean(get() != 0);
  8125. }
  8126. case 0x0A: // null
  8127. {
  8128. return sax->null();
  8129. }
  8130. case 0x10: // int32
  8131. {
  8132. std::int32_t value{};
  8133. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8134. }
  8135. case 0x12: // int64
  8136. {
  8137. std::int64_t value{};
  8138. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8139. }
  8140. default: // anything else not supported (yet)
  8141. {
  8142. std::array<char, 3> cr{{}};
  8143. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8144. const std::string cr_str{cr.data()};
  8145. return sax->parse_error(element_type_parse_position, cr_str,
  8146. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8147. }
  8148. }
  8149. }
  8150. /*!
  8151. @brief Read a BSON element list (as specified in the BSON-spec)
  8152. The same binary layout is used for objects and arrays, hence it must be
  8153. indicated with the argument @a is_array which one is expected
  8154. (true --> array, false --> object).
  8155. @param[in] is_array Determines if the element list being read is to be
  8156. treated as an object (@a is_array == false), or as an
  8157. array (@a is_array == true).
  8158. @return whether a valid BSON-object/array was passed to the SAX parser
  8159. */
  8160. bool parse_bson_element_list(const bool is_array)
  8161. {
  8162. string_t key;
  8163. while (auto element_type = get())
  8164. {
  8165. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8166. {
  8167. return false;
  8168. }
  8169. const std::size_t element_type_parse_position = chars_read;
  8170. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8171. {
  8172. return false;
  8173. }
  8174. if (!is_array && !sax->key(key))
  8175. {
  8176. return false;
  8177. }
  8178. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8179. {
  8180. return false;
  8181. }
  8182. // get_bson_cstr only appends
  8183. key.clear();
  8184. }
  8185. return true;
  8186. }
  8187. /*!
  8188. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8189. @return whether a valid BSON-array was passed to the SAX parser
  8190. */
  8191. bool parse_bson_array()
  8192. {
  8193. std::int32_t document_size{};
  8194. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8195. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  8196. {
  8197. return false;
  8198. }
  8199. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8200. {
  8201. return false;
  8202. }
  8203. return sax->end_array();
  8204. }
  8205. //////////
  8206. // CBOR //
  8207. //////////
  8208. /*!
  8209. @param[in] get_char whether a new character should be retrieved from the
  8210. input (true) or whether the last read character should
  8211. be considered instead (false)
  8212. @param[in] tag_handler how CBOR tags should be treated
  8213. @return whether a valid CBOR value was passed to the SAX parser
  8214. */
  8215. bool parse_cbor_internal(const bool get_char,
  8216. const cbor_tag_handler_t tag_handler)
  8217. {
  8218. switch (get_char ? get() : current)
  8219. {
  8220. // EOF
  8221. case std::char_traits<char_type>::eof():
  8222. return unexpect_eof(input_format_t::cbor, "value");
  8223. // Integer 0x00..0x17 (0..23)
  8224. case 0x00:
  8225. case 0x01:
  8226. case 0x02:
  8227. case 0x03:
  8228. case 0x04:
  8229. case 0x05:
  8230. case 0x06:
  8231. case 0x07:
  8232. case 0x08:
  8233. case 0x09:
  8234. case 0x0A:
  8235. case 0x0B:
  8236. case 0x0C:
  8237. case 0x0D:
  8238. case 0x0E:
  8239. case 0x0F:
  8240. case 0x10:
  8241. case 0x11:
  8242. case 0x12:
  8243. case 0x13:
  8244. case 0x14:
  8245. case 0x15:
  8246. case 0x16:
  8247. case 0x17:
  8248. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8249. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  8250. {
  8251. std::uint8_t number{};
  8252. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8253. }
  8254. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  8255. {
  8256. std::uint16_t number{};
  8257. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8258. }
  8259. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  8260. {
  8261. std::uint32_t number{};
  8262. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8263. }
  8264. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  8265. {
  8266. std::uint64_t number{};
  8267. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8268. }
  8269. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  8270. case 0x20:
  8271. case 0x21:
  8272. case 0x22:
  8273. case 0x23:
  8274. case 0x24:
  8275. case 0x25:
  8276. case 0x26:
  8277. case 0x27:
  8278. case 0x28:
  8279. case 0x29:
  8280. case 0x2A:
  8281. case 0x2B:
  8282. case 0x2C:
  8283. case 0x2D:
  8284. case 0x2E:
  8285. case 0x2F:
  8286. case 0x30:
  8287. case 0x31:
  8288. case 0x32:
  8289. case 0x33:
  8290. case 0x34:
  8291. case 0x35:
  8292. case 0x36:
  8293. case 0x37:
  8294. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8295. case 0x38: // Negative integer (one-byte uint8_t follows)
  8296. {
  8297. std::uint8_t number{};
  8298. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8299. }
  8300. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8301. {
  8302. std::uint16_t number{};
  8303. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8304. }
  8305. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8306. {
  8307. std::uint32_t number{};
  8308. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8309. }
  8310. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8311. {
  8312. std::uint64_t number{};
  8313. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8314. - static_cast<number_integer_t>(number));
  8315. }
  8316. // Binary data (0x00..0x17 bytes follow)
  8317. case 0x40:
  8318. case 0x41:
  8319. case 0x42:
  8320. case 0x43:
  8321. case 0x44:
  8322. case 0x45:
  8323. case 0x46:
  8324. case 0x47:
  8325. case 0x48:
  8326. case 0x49:
  8327. case 0x4A:
  8328. case 0x4B:
  8329. case 0x4C:
  8330. case 0x4D:
  8331. case 0x4E:
  8332. case 0x4F:
  8333. case 0x50:
  8334. case 0x51:
  8335. case 0x52:
  8336. case 0x53:
  8337. case 0x54:
  8338. case 0x55:
  8339. case 0x56:
  8340. case 0x57:
  8341. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8342. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8343. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8344. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8345. case 0x5F: // Binary data (indefinite length)
  8346. {
  8347. binary_t b;
  8348. return get_cbor_binary(b) && sax->binary(b);
  8349. }
  8350. // UTF-8 string (0x00..0x17 bytes follow)
  8351. case 0x60:
  8352. case 0x61:
  8353. case 0x62:
  8354. case 0x63:
  8355. case 0x64:
  8356. case 0x65:
  8357. case 0x66:
  8358. case 0x67:
  8359. case 0x68:
  8360. case 0x69:
  8361. case 0x6A:
  8362. case 0x6B:
  8363. case 0x6C:
  8364. case 0x6D:
  8365. case 0x6E:
  8366. case 0x6F:
  8367. case 0x70:
  8368. case 0x71:
  8369. case 0x72:
  8370. case 0x73:
  8371. case 0x74:
  8372. case 0x75:
  8373. case 0x76:
  8374. case 0x77:
  8375. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8376. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8377. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8378. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8379. case 0x7F: // UTF-8 string (indefinite length)
  8380. {
  8381. string_t s;
  8382. return get_cbor_string(s) && sax->string(s);
  8383. }
  8384. // array (0x00..0x17 data items follow)
  8385. case 0x80:
  8386. case 0x81:
  8387. case 0x82:
  8388. case 0x83:
  8389. case 0x84:
  8390. case 0x85:
  8391. case 0x86:
  8392. case 0x87:
  8393. case 0x88:
  8394. case 0x89:
  8395. case 0x8A:
  8396. case 0x8B:
  8397. case 0x8C:
  8398. case 0x8D:
  8399. case 0x8E:
  8400. case 0x8F:
  8401. case 0x90:
  8402. case 0x91:
  8403. case 0x92:
  8404. case 0x93:
  8405. case 0x94:
  8406. case 0x95:
  8407. case 0x96:
  8408. case 0x97:
  8409. return get_cbor_array(
  8410. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8411. case 0x98: // array (one-byte uint8_t for n follows)
  8412. {
  8413. std::uint8_t len{};
  8414. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8415. }
  8416. case 0x99: // array (two-byte uint16_t for n follow)
  8417. {
  8418. std::uint16_t len{};
  8419. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8420. }
  8421. case 0x9A: // array (four-byte uint32_t for n follow)
  8422. {
  8423. std::uint32_t len{};
  8424. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8425. }
  8426. case 0x9B: // array (eight-byte uint64_t for n follow)
  8427. {
  8428. std::uint64_t len{};
  8429. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8430. }
  8431. case 0x9F: // array (indefinite length)
  8432. return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
  8433. // map (0x00..0x17 pairs of data items follow)
  8434. case 0xA0:
  8435. case 0xA1:
  8436. case 0xA2:
  8437. case 0xA3:
  8438. case 0xA4:
  8439. case 0xA5:
  8440. case 0xA6:
  8441. case 0xA7:
  8442. case 0xA8:
  8443. case 0xA9:
  8444. case 0xAA:
  8445. case 0xAB:
  8446. case 0xAC:
  8447. case 0xAD:
  8448. case 0xAE:
  8449. case 0xAF:
  8450. case 0xB0:
  8451. case 0xB1:
  8452. case 0xB2:
  8453. case 0xB3:
  8454. case 0xB4:
  8455. case 0xB5:
  8456. case 0xB6:
  8457. case 0xB7:
  8458. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8459. case 0xB8: // map (one-byte uint8_t for n follows)
  8460. {
  8461. std::uint8_t len{};
  8462. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8463. }
  8464. case 0xB9: // map (two-byte uint16_t for n follow)
  8465. {
  8466. std::uint16_t len{};
  8467. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8468. }
  8469. case 0xBA: // map (four-byte uint32_t for n follow)
  8470. {
  8471. std::uint32_t len{};
  8472. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8473. }
  8474. case 0xBB: // map (eight-byte uint64_t for n follow)
  8475. {
  8476. std::uint64_t len{};
  8477. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8478. }
  8479. case 0xBF: // map (indefinite length)
  8480. return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
  8481. case 0xC6: // tagged item
  8482. case 0xC7:
  8483. case 0xC8:
  8484. case 0xC9:
  8485. case 0xCA:
  8486. case 0xCB:
  8487. case 0xCC:
  8488. case 0xCD:
  8489. case 0xCE:
  8490. case 0xCF:
  8491. case 0xD0:
  8492. case 0xD1:
  8493. case 0xD2:
  8494. case 0xD3:
  8495. case 0xD4:
  8496. case 0xD8: // tagged item (1 bytes follow)
  8497. case 0xD9: // tagged item (2 bytes follow)
  8498. case 0xDA: // tagged item (4 bytes follow)
  8499. case 0xDB: // tagged item (8 bytes follow)
  8500. {
  8501. switch (tag_handler)
  8502. {
  8503. case cbor_tag_handler_t::error:
  8504. {
  8505. auto last_token = get_token_string();
  8506. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8507. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8508. }
  8509. case cbor_tag_handler_t::ignore:
  8510. {
  8511. // ignore binary subtype
  8512. switch (current)
  8513. {
  8514. case 0xD8:
  8515. {
  8516. std::uint8_t subtype_to_ignore{};
  8517. get_number(input_format_t::cbor, subtype_to_ignore);
  8518. break;
  8519. }
  8520. case 0xD9:
  8521. {
  8522. std::uint16_t subtype_to_ignore{};
  8523. get_number(input_format_t::cbor, subtype_to_ignore);
  8524. break;
  8525. }
  8526. case 0xDA:
  8527. {
  8528. std::uint32_t subtype_to_ignore{};
  8529. get_number(input_format_t::cbor, subtype_to_ignore);
  8530. break;
  8531. }
  8532. case 0xDB:
  8533. {
  8534. std::uint64_t subtype_to_ignore{};
  8535. get_number(input_format_t::cbor, subtype_to_ignore);
  8536. break;
  8537. }
  8538. default:
  8539. break;
  8540. }
  8541. return parse_cbor_internal(true, tag_handler);
  8542. }
  8543. case cbor_tag_handler_t::store:
  8544. {
  8545. binary_t b;
  8546. // use binary subtype and store in binary container
  8547. switch (current)
  8548. {
  8549. case 0xD8:
  8550. {
  8551. std::uint8_t subtype{};
  8552. get_number(input_format_t::cbor, subtype);
  8553. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8554. break;
  8555. }
  8556. case 0xD9:
  8557. {
  8558. std::uint16_t subtype{};
  8559. get_number(input_format_t::cbor, subtype);
  8560. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8561. break;
  8562. }
  8563. case 0xDA:
  8564. {
  8565. std::uint32_t subtype{};
  8566. get_number(input_format_t::cbor, subtype);
  8567. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8568. break;
  8569. }
  8570. case 0xDB:
  8571. {
  8572. std::uint64_t subtype{};
  8573. get_number(input_format_t::cbor, subtype);
  8574. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8575. break;
  8576. }
  8577. default:
  8578. return parse_cbor_internal(true, tag_handler);
  8579. }
  8580. get();
  8581. return get_cbor_binary(b) && sax->binary(b);
  8582. }
  8583. default: // LCOV_EXCL_LINE
  8584. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8585. return false; // LCOV_EXCL_LINE
  8586. }
  8587. }
  8588. case 0xF4: // false
  8589. return sax->boolean(false);
  8590. case 0xF5: // true
  8591. return sax->boolean(true);
  8592. case 0xF6: // null
  8593. return sax->null();
  8594. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  8595. {
  8596. const auto byte1_raw = get();
  8597. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8598. {
  8599. return false;
  8600. }
  8601. const auto byte2_raw = get();
  8602. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8603. {
  8604. return false;
  8605. }
  8606. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  8607. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  8608. // code from RFC 7049, Appendix D, Figure 3:
  8609. // As half-precision floating-point numbers were only added
  8610. // to IEEE 754 in 2008, today's programming platforms often
  8611. // still only have limited support for them. It is very
  8612. // easy to include at least decoding support for them even
  8613. // without such support. An example of a small decoder for
  8614. // half-precision floating-point numbers in the C language
  8615. // is shown in Fig. 3.
  8616. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  8617. const double val = [&half]
  8618. {
  8619. const int exp = (half >> 10u) & 0x1Fu;
  8620. const unsigned int mant = half & 0x3FFu;
  8621. JSON_ASSERT(0 <= exp&& exp <= 32);
  8622. JSON_ASSERT(mant <= 1024);
  8623. switch (exp)
  8624. {
  8625. case 0:
  8626. return std::ldexp(mant, -24);
  8627. case 31:
  8628. return (mant == 0)
  8629. ? std::numeric_limits<double>::infinity()
  8630. : std::numeric_limits<double>::quiet_NaN();
  8631. default:
  8632. return std::ldexp(mant + 1024, exp - 25);
  8633. }
  8634. }();
  8635. return sax->number_float((half & 0x8000u) != 0
  8636. ? static_cast<number_float_t>(-val)
  8637. : static_cast<number_float_t>(val), "");
  8638. }
  8639. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  8640. {
  8641. float number{};
  8642. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8643. }
  8644. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8645. {
  8646. double number{};
  8647. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8648. }
  8649. default: // anything else (0xFF is handled inside the other types)
  8650. {
  8651. auto last_token = get_token_string();
  8652. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8653. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8654. }
  8655. }
  8656. }
  8657. /*!
  8658. @brief reads a CBOR string
  8659. This function first reads starting bytes to determine the expected
  8660. string length and then copies this number of bytes into a string.
  8661. Additionally, CBOR's strings with indefinite lengths are supported.
  8662. @param[out] result created string
  8663. @return whether string creation completed
  8664. */
  8665. bool get_cbor_string(string_t& result)
  8666. {
  8667. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8668. {
  8669. return false;
  8670. }
  8671. switch (current)
  8672. {
  8673. // UTF-8 string (0x00..0x17 bytes follow)
  8674. case 0x60:
  8675. case 0x61:
  8676. case 0x62:
  8677. case 0x63:
  8678. case 0x64:
  8679. case 0x65:
  8680. case 0x66:
  8681. case 0x67:
  8682. case 0x68:
  8683. case 0x69:
  8684. case 0x6A:
  8685. case 0x6B:
  8686. case 0x6C:
  8687. case 0x6D:
  8688. case 0x6E:
  8689. case 0x6F:
  8690. case 0x70:
  8691. case 0x71:
  8692. case 0x72:
  8693. case 0x73:
  8694. case 0x74:
  8695. case 0x75:
  8696. case 0x76:
  8697. case 0x77:
  8698. {
  8699. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8700. }
  8701. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8702. {
  8703. std::uint8_t len{};
  8704. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8705. }
  8706. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8707. {
  8708. std::uint16_t len{};
  8709. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8710. }
  8711. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8712. {
  8713. std::uint32_t len{};
  8714. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8715. }
  8716. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8717. {
  8718. std::uint64_t len{};
  8719. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8720. }
  8721. case 0x7F: // UTF-8 string (indefinite length)
  8722. {
  8723. while (get() != 0xFF)
  8724. {
  8725. string_t chunk;
  8726. if (!get_cbor_string(chunk))
  8727. {
  8728. return false;
  8729. }
  8730. result.append(chunk);
  8731. }
  8732. return true;
  8733. }
  8734. default:
  8735. {
  8736. auto last_token = get_token_string();
  8737. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8738. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  8739. }
  8740. }
  8741. }
  8742. /*!
  8743. @brief reads a CBOR byte array
  8744. This function first reads starting bytes to determine the expected
  8745. byte array length and then copies this number of bytes into the byte array.
  8746. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8747. @param[out] result created byte array
  8748. @return whether byte array creation completed
  8749. */
  8750. bool get_cbor_binary(binary_t& result)
  8751. {
  8752. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8753. {
  8754. return false;
  8755. }
  8756. switch (current)
  8757. {
  8758. // Binary data (0x00..0x17 bytes follow)
  8759. case 0x40:
  8760. case 0x41:
  8761. case 0x42:
  8762. case 0x43:
  8763. case 0x44:
  8764. case 0x45:
  8765. case 0x46:
  8766. case 0x47:
  8767. case 0x48:
  8768. case 0x49:
  8769. case 0x4A:
  8770. case 0x4B:
  8771. case 0x4C:
  8772. case 0x4D:
  8773. case 0x4E:
  8774. case 0x4F:
  8775. case 0x50:
  8776. case 0x51:
  8777. case 0x52:
  8778. case 0x53:
  8779. case 0x54:
  8780. case 0x55:
  8781. case 0x56:
  8782. case 0x57:
  8783. {
  8784. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8785. }
  8786. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8787. {
  8788. std::uint8_t len{};
  8789. return get_number(input_format_t::cbor, len) &&
  8790. get_binary(input_format_t::cbor, len, result);
  8791. }
  8792. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8793. {
  8794. std::uint16_t len{};
  8795. return get_number(input_format_t::cbor, len) &&
  8796. get_binary(input_format_t::cbor, len, result);
  8797. }
  8798. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8799. {
  8800. std::uint32_t len{};
  8801. return get_number(input_format_t::cbor, len) &&
  8802. get_binary(input_format_t::cbor, len, result);
  8803. }
  8804. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8805. {
  8806. std::uint64_t len{};
  8807. return get_number(input_format_t::cbor, len) &&
  8808. get_binary(input_format_t::cbor, len, result);
  8809. }
  8810. case 0x5F: // Binary data (indefinite length)
  8811. {
  8812. while (get() != 0xFF)
  8813. {
  8814. binary_t chunk;
  8815. if (!get_cbor_binary(chunk))
  8816. {
  8817. return false;
  8818. }
  8819. result.insert(result.end(), chunk.begin(), chunk.end());
  8820. }
  8821. return true;
  8822. }
  8823. default:
  8824. {
  8825. auto last_token = get_token_string();
  8826. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8827. 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));
  8828. }
  8829. }
  8830. }
  8831. /*!
  8832. @param[in] len the length of the array or static_cast<std::size_t>(-1) for an
  8833. array of indefinite size
  8834. @param[in] tag_handler how CBOR tags should be treated
  8835. @return whether array creation completed
  8836. */
  8837. bool get_cbor_array(const std::size_t len,
  8838. const cbor_tag_handler_t tag_handler)
  8839. {
  8840. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8841. {
  8842. return false;
  8843. }
  8844. if (len != static_cast<std::size_t>(-1))
  8845. {
  8846. for (std::size_t i = 0; i < len; ++i)
  8847. {
  8848. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8849. {
  8850. return false;
  8851. }
  8852. }
  8853. }
  8854. else
  8855. {
  8856. while (get() != 0xFF)
  8857. {
  8858. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8859. {
  8860. return false;
  8861. }
  8862. }
  8863. }
  8864. return sax->end_array();
  8865. }
  8866. /*!
  8867. @param[in] len the length of the object or static_cast<std::size_t>(-1) for an
  8868. object of indefinite size
  8869. @param[in] tag_handler how CBOR tags should be treated
  8870. @return whether object creation completed
  8871. */
  8872. bool get_cbor_object(const std::size_t len,
  8873. const cbor_tag_handler_t tag_handler)
  8874. {
  8875. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8876. {
  8877. return false;
  8878. }
  8879. if (len != 0)
  8880. {
  8881. string_t key;
  8882. if (len != static_cast<std::size_t>(-1))
  8883. {
  8884. for (std::size_t i = 0; i < len; ++i)
  8885. {
  8886. get();
  8887. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8888. {
  8889. return false;
  8890. }
  8891. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8892. {
  8893. return false;
  8894. }
  8895. key.clear();
  8896. }
  8897. }
  8898. else
  8899. {
  8900. while (get() != 0xFF)
  8901. {
  8902. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8903. {
  8904. return false;
  8905. }
  8906. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8907. {
  8908. return false;
  8909. }
  8910. key.clear();
  8911. }
  8912. }
  8913. }
  8914. return sax->end_object();
  8915. }
  8916. /////////////
  8917. // MsgPack //
  8918. /////////////
  8919. /*!
  8920. @return whether a valid MessagePack value was passed to the SAX parser
  8921. */
  8922. bool parse_msgpack_internal()
  8923. {
  8924. switch (get())
  8925. {
  8926. // EOF
  8927. case std::char_traits<char_type>::eof():
  8928. return unexpect_eof(input_format_t::msgpack, "value");
  8929. // positive fixint
  8930. case 0x00:
  8931. case 0x01:
  8932. case 0x02:
  8933. case 0x03:
  8934. case 0x04:
  8935. case 0x05:
  8936. case 0x06:
  8937. case 0x07:
  8938. case 0x08:
  8939. case 0x09:
  8940. case 0x0A:
  8941. case 0x0B:
  8942. case 0x0C:
  8943. case 0x0D:
  8944. case 0x0E:
  8945. case 0x0F:
  8946. case 0x10:
  8947. case 0x11:
  8948. case 0x12:
  8949. case 0x13:
  8950. case 0x14:
  8951. case 0x15:
  8952. case 0x16:
  8953. case 0x17:
  8954. case 0x18:
  8955. case 0x19:
  8956. case 0x1A:
  8957. case 0x1B:
  8958. case 0x1C:
  8959. case 0x1D:
  8960. case 0x1E:
  8961. case 0x1F:
  8962. case 0x20:
  8963. case 0x21:
  8964. case 0x22:
  8965. case 0x23:
  8966. case 0x24:
  8967. case 0x25:
  8968. case 0x26:
  8969. case 0x27:
  8970. case 0x28:
  8971. case 0x29:
  8972. case 0x2A:
  8973. case 0x2B:
  8974. case 0x2C:
  8975. case 0x2D:
  8976. case 0x2E:
  8977. case 0x2F:
  8978. case 0x30:
  8979. case 0x31:
  8980. case 0x32:
  8981. case 0x33:
  8982. case 0x34:
  8983. case 0x35:
  8984. case 0x36:
  8985. case 0x37:
  8986. case 0x38:
  8987. case 0x39:
  8988. case 0x3A:
  8989. case 0x3B:
  8990. case 0x3C:
  8991. case 0x3D:
  8992. case 0x3E:
  8993. case 0x3F:
  8994. case 0x40:
  8995. case 0x41:
  8996. case 0x42:
  8997. case 0x43:
  8998. case 0x44:
  8999. case 0x45:
  9000. case 0x46:
  9001. case 0x47:
  9002. case 0x48:
  9003. case 0x49:
  9004. case 0x4A:
  9005. case 0x4B:
  9006. case 0x4C:
  9007. case 0x4D:
  9008. case 0x4E:
  9009. case 0x4F:
  9010. case 0x50:
  9011. case 0x51:
  9012. case 0x52:
  9013. case 0x53:
  9014. case 0x54:
  9015. case 0x55:
  9016. case 0x56:
  9017. case 0x57:
  9018. case 0x58:
  9019. case 0x59:
  9020. case 0x5A:
  9021. case 0x5B:
  9022. case 0x5C:
  9023. case 0x5D:
  9024. case 0x5E:
  9025. case 0x5F:
  9026. case 0x60:
  9027. case 0x61:
  9028. case 0x62:
  9029. case 0x63:
  9030. case 0x64:
  9031. case 0x65:
  9032. case 0x66:
  9033. case 0x67:
  9034. case 0x68:
  9035. case 0x69:
  9036. case 0x6A:
  9037. case 0x6B:
  9038. case 0x6C:
  9039. case 0x6D:
  9040. case 0x6E:
  9041. case 0x6F:
  9042. case 0x70:
  9043. case 0x71:
  9044. case 0x72:
  9045. case 0x73:
  9046. case 0x74:
  9047. case 0x75:
  9048. case 0x76:
  9049. case 0x77:
  9050. case 0x78:
  9051. case 0x79:
  9052. case 0x7A:
  9053. case 0x7B:
  9054. case 0x7C:
  9055. case 0x7D:
  9056. case 0x7E:
  9057. case 0x7F:
  9058. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9059. // fixmap
  9060. case 0x80:
  9061. case 0x81:
  9062. case 0x82:
  9063. case 0x83:
  9064. case 0x84:
  9065. case 0x85:
  9066. case 0x86:
  9067. case 0x87:
  9068. case 0x88:
  9069. case 0x89:
  9070. case 0x8A:
  9071. case 0x8B:
  9072. case 0x8C:
  9073. case 0x8D:
  9074. case 0x8E:
  9075. case 0x8F:
  9076. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9077. // fixarray
  9078. case 0x90:
  9079. case 0x91:
  9080. case 0x92:
  9081. case 0x93:
  9082. case 0x94:
  9083. case 0x95:
  9084. case 0x96:
  9085. case 0x97:
  9086. case 0x98:
  9087. case 0x99:
  9088. case 0x9A:
  9089. case 0x9B:
  9090. case 0x9C:
  9091. case 0x9D:
  9092. case 0x9E:
  9093. case 0x9F:
  9094. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9095. // fixstr
  9096. case 0xA0:
  9097. case 0xA1:
  9098. case 0xA2:
  9099. case 0xA3:
  9100. case 0xA4:
  9101. case 0xA5:
  9102. case 0xA6:
  9103. case 0xA7:
  9104. case 0xA8:
  9105. case 0xA9:
  9106. case 0xAA:
  9107. case 0xAB:
  9108. case 0xAC:
  9109. case 0xAD:
  9110. case 0xAE:
  9111. case 0xAF:
  9112. case 0xB0:
  9113. case 0xB1:
  9114. case 0xB2:
  9115. case 0xB3:
  9116. case 0xB4:
  9117. case 0xB5:
  9118. case 0xB6:
  9119. case 0xB7:
  9120. case 0xB8:
  9121. case 0xB9:
  9122. case 0xBA:
  9123. case 0xBB:
  9124. case 0xBC:
  9125. case 0xBD:
  9126. case 0xBE:
  9127. case 0xBF:
  9128. case 0xD9: // str 8
  9129. case 0xDA: // str 16
  9130. case 0xDB: // str 32
  9131. {
  9132. string_t s;
  9133. return get_msgpack_string(s) && sax->string(s);
  9134. }
  9135. case 0xC0: // nil
  9136. return sax->null();
  9137. case 0xC2: // false
  9138. return sax->boolean(false);
  9139. case 0xC3: // true
  9140. return sax->boolean(true);
  9141. case 0xC4: // bin 8
  9142. case 0xC5: // bin 16
  9143. case 0xC6: // bin 32
  9144. case 0xC7: // ext 8
  9145. case 0xC8: // ext 16
  9146. case 0xC9: // ext 32
  9147. case 0xD4: // fixext 1
  9148. case 0xD5: // fixext 2
  9149. case 0xD6: // fixext 4
  9150. case 0xD7: // fixext 8
  9151. case 0xD8: // fixext 16
  9152. {
  9153. binary_t b;
  9154. return get_msgpack_binary(b) && sax->binary(b);
  9155. }
  9156. case 0xCA: // float 32
  9157. {
  9158. float number{};
  9159. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9160. }
  9161. case 0xCB: // float 64
  9162. {
  9163. double number{};
  9164. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9165. }
  9166. case 0xCC: // uint 8
  9167. {
  9168. std::uint8_t number{};
  9169. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9170. }
  9171. case 0xCD: // uint 16
  9172. {
  9173. std::uint16_t number{};
  9174. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9175. }
  9176. case 0xCE: // uint 32
  9177. {
  9178. std::uint32_t number{};
  9179. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9180. }
  9181. case 0xCF: // uint 64
  9182. {
  9183. std::uint64_t number{};
  9184. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9185. }
  9186. case 0xD0: // int 8
  9187. {
  9188. std::int8_t number{};
  9189. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9190. }
  9191. case 0xD1: // int 16
  9192. {
  9193. std::int16_t number{};
  9194. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9195. }
  9196. case 0xD2: // int 32
  9197. {
  9198. std::int32_t number{};
  9199. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9200. }
  9201. case 0xD3: // int 64
  9202. {
  9203. std::int64_t number{};
  9204. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9205. }
  9206. case 0xDC: // array 16
  9207. {
  9208. std::uint16_t len{};
  9209. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9210. }
  9211. case 0xDD: // array 32
  9212. {
  9213. std::uint32_t len{};
  9214. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9215. }
  9216. case 0xDE: // map 16
  9217. {
  9218. std::uint16_t len{};
  9219. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9220. }
  9221. case 0xDF: // map 32
  9222. {
  9223. std::uint32_t len{};
  9224. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9225. }
  9226. // negative fixint
  9227. case 0xE0:
  9228. case 0xE1:
  9229. case 0xE2:
  9230. case 0xE3:
  9231. case 0xE4:
  9232. case 0xE5:
  9233. case 0xE6:
  9234. case 0xE7:
  9235. case 0xE8:
  9236. case 0xE9:
  9237. case 0xEA:
  9238. case 0xEB:
  9239. case 0xEC:
  9240. case 0xED:
  9241. case 0xEE:
  9242. case 0xEF:
  9243. case 0xF0:
  9244. case 0xF1:
  9245. case 0xF2:
  9246. case 0xF3:
  9247. case 0xF4:
  9248. case 0xF5:
  9249. case 0xF6:
  9250. case 0xF7:
  9251. case 0xF8:
  9252. case 0xF9:
  9253. case 0xFA:
  9254. case 0xFB:
  9255. case 0xFC:
  9256. case 0xFD:
  9257. case 0xFE:
  9258. case 0xFF:
  9259. return sax->number_integer(static_cast<std::int8_t>(current));
  9260. default: // anything else
  9261. {
  9262. auto last_token = get_token_string();
  9263. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9264. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9265. }
  9266. }
  9267. }
  9268. /*!
  9269. @brief reads a MessagePack string
  9270. This function first reads starting bytes to determine the expected
  9271. string length and then copies this number of bytes into a string.
  9272. @param[out] result created string
  9273. @return whether string creation completed
  9274. */
  9275. bool get_msgpack_string(string_t& result)
  9276. {
  9277. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  9278. {
  9279. return false;
  9280. }
  9281. switch (current)
  9282. {
  9283. // fixstr
  9284. case 0xA0:
  9285. case 0xA1:
  9286. case 0xA2:
  9287. case 0xA3:
  9288. case 0xA4:
  9289. case 0xA5:
  9290. case 0xA6:
  9291. case 0xA7:
  9292. case 0xA8:
  9293. case 0xA9:
  9294. case 0xAA:
  9295. case 0xAB:
  9296. case 0xAC:
  9297. case 0xAD:
  9298. case 0xAE:
  9299. case 0xAF:
  9300. case 0xB0:
  9301. case 0xB1:
  9302. case 0xB2:
  9303. case 0xB3:
  9304. case 0xB4:
  9305. case 0xB5:
  9306. case 0xB6:
  9307. case 0xB7:
  9308. case 0xB8:
  9309. case 0xB9:
  9310. case 0xBA:
  9311. case 0xBB:
  9312. case 0xBC:
  9313. case 0xBD:
  9314. case 0xBE:
  9315. case 0xBF:
  9316. {
  9317. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9318. }
  9319. case 0xD9: // str 8
  9320. {
  9321. std::uint8_t len{};
  9322. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9323. }
  9324. case 0xDA: // str 16
  9325. {
  9326. std::uint16_t len{};
  9327. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9328. }
  9329. case 0xDB: // str 32
  9330. {
  9331. std::uint32_t len{};
  9332. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9333. }
  9334. default:
  9335. {
  9336. auto last_token = get_token_string();
  9337. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9338. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9339. }
  9340. }
  9341. }
  9342. /*!
  9343. @brief reads a MessagePack byte array
  9344. This function first reads starting bytes to determine the expected
  9345. byte array length and then copies this number of bytes into a byte array.
  9346. @param[out] result created byte array
  9347. @return whether byte array creation completed
  9348. */
  9349. bool get_msgpack_binary(binary_t& result)
  9350. {
  9351. // helper function to set the subtype
  9352. auto assign_and_return_true = [&result](std::int8_t subtype)
  9353. {
  9354. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9355. return true;
  9356. };
  9357. switch (current)
  9358. {
  9359. case 0xC4: // bin 8
  9360. {
  9361. std::uint8_t len{};
  9362. return get_number(input_format_t::msgpack, len) &&
  9363. get_binary(input_format_t::msgpack, len, result);
  9364. }
  9365. case 0xC5: // bin 16
  9366. {
  9367. std::uint16_t len{};
  9368. return get_number(input_format_t::msgpack, len) &&
  9369. get_binary(input_format_t::msgpack, len, result);
  9370. }
  9371. case 0xC6: // bin 32
  9372. {
  9373. std::uint32_t len{};
  9374. return get_number(input_format_t::msgpack, len) &&
  9375. get_binary(input_format_t::msgpack, len, result);
  9376. }
  9377. case 0xC7: // ext 8
  9378. {
  9379. std::uint8_t len{};
  9380. std::int8_t subtype{};
  9381. return get_number(input_format_t::msgpack, len) &&
  9382. get_number(input_format_t::msgpack, subtype) &&
  9383. get_binary(input_format_t::msgpack, len, result) &&
  9384. assign_and_return_true(subtype);
  9385. }
  9386. case 0xC8: // ext 16
  9387. {
  9388. std::uint16_t len{};
  9389. std::int8_t subtype{};
  9390. return get_number(input_format_t::msgpack, len) &&
  9391. get_number(input_format_t::msgpack, subtype) &&
  9392. get_binary(input_format_t::msgpack, len, result) &&
  9393. assign_and_return_true(subtype);
  9394. }
  9395. case 0xC9: // ext 32
  9396. {
  9397. std::uint32_t len{};
  9398. std::int8_t subtype{};
  9399. return get_number(input_format_t::msgpack, len) &&
  9400. get_number(input_format_t::msgpack, subtype) &&
  9401. get_binary(input_format_t::msgpack, len, result) &&
  9402. assign_and_return_true(subtype);
  9403. }
  9404. case 0xD4: // fixext 1
  9405. {
  9406. std::int8_t subtype{};
  9407. return get_number(input_format_t::msgpack, subtype) &&
  9408. get_binary(input_format_t::msgpack, 1, result) &&
  9409. assign_and_return_true(subtype);
  9410. }
  9411. case 0xD5: // fixext 2
  9412. {
  9413. std::int8_t subtype{};
  9414. return get_number(input_format_t::msgpack, subtype) &&
  9415. get_binary(input_format_t::msgpack, 2, result) &&
  9416. assign_and_return_true(subtype);
  9417. }
  9418. case 0xD6: // fixext 4
  9419. {
  9420. std::int8_t subtype{};
  9421. return get_number(input_format_t::msgpack, subtype) &&
  9422. get_binary(input_format_t::msgpack, 4, result) &&
  9423. assign_and_return_true(subtype);
  9424. }
  9425. case 0xD7: // fixext 8
  9426. {
  9427. std::int8_t subtype{};
  9428. return get_number(input_format_t::msgpack, subtype) &&
  9429. get_binary(input_format_t::msgpack, 8, result) &&
  9430. assign_and_return_true(subtype);
  9431. }
  9432. case 0xD8: // fixext 16
  9433. {
  9434. std::int8_t subtype{};
  9435. return get_number(input_format_t::msgpack, subtype) &&
  9436. get_binary(input_format_t::msgpack, 16, result) &&
  9437. assign_and_return_true(subtype);
  9438. }
  9439. default: // LCOV_EXCL_LINE
  9440. return false; // LCOV_EXCL_LINE
  9441. }
  9442. }
  9443. /*!
  9444. @param[in] len the length of the array
  9445. @return whether array creation completed
  9446. */
  9447. bool get_msgpack_array(const std::size_t len)
  9448. {
  9449. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9450. {
  9451. return false;
  9452. }
  9453. for (std::size_t i = 0; i < len; ++i)
  9454. {
  9455. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9456. {
  9457. return false;
  9458. }
  9459. }
  9460. return sax->end_array();
  9461. }
  9462. /*!
  9463. @param[in] len the length of the object
  9464. @return whether object creation completed
  9465. */
  9466. bool get_msgpack_object(const std::size_t len)
  9467. {
  9468. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9469. {
  9470. return false;
  9471. }
  9472. string_t key;
  9473. for (std::size_t i = 0; i < len; ++i)
  9474. {
  9475. get();
  9476. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  9477. {
  9478. return false;
  9479. }
  9480. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9481. {
  9482. return false;
  9483. }
  9484. key.clear();
  9485. }
  9486. return sax->end_object();
  9487. }
  9488. ////////////
  9489. // UBJSON //
  9490. ////////////
  9491. /*!
  9492. @param[in] get_char whether a new character should be retrieved from the
  9493. input (true, default) or whether the last read
  9494. character should be considered instead
  9495. @return whether a valid UBJSON value was passed to the SAX parser
  9496. */
  9497. bool parse_ubjson_internal(const bool get_char = true)
  9498. {
  9499. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  9500. }
  9501. /*!
  9502. @brief reads a UBJSON string
  9503. This function is either called after reading the 'S' byte explicitly
  9504. indicating a string, or in case of an object key where the 'S' byte can be
  9505. left out.
  9506. @param[out] result created string
  9507. @param[in] get_char whether a new character should be retrieved from the
  9508. input (true, default) or whether the last read
  9509. character should be considered instead
  9510. @return whether string creation completed
  9511. */
  9512. bool get_ubjson_string(string_t& result, const bool get_char = true)
  9513. {
  9514. if (get_char)
  9515. {
  9516. get(); // TODO(niels): may we ignore N here?
  9517. }
  9518. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9519. {
  9520. return false;
  9521. }
  9522. switch (current)
  9523. {
  9524. case 'U':
  9525. {
  9526. std::uint8_t len{};
  9527. return get_number(input_format, len) && get_string(input_format, len, result);
  9528. }
  9529. case 'i':
  9530. {
  9531. std::int8_t len{};
  9532. return get_number(input_format, len) && get_string(input_format, len, result);
  9533. }
  9534. case 'I':
  9535. {
  9536. std::int16_t len{};
  9537. return get_number(input_format, len) && get_string(input_format, len, result);
  9538. }
  9539. case 'l':
  9540. {
  9541. std::int32_t len{};
  9542. return get_number(input_format, len) && get_string(input_format, len, result);
  9543. }
  9544. case 'L':
  9545. {
  9546. std::int64_t len{};
  9547. return get_number(input_format, len) && get_string(input_format, len, result);
  9548. }
  9549. case 'u':
  9550. {
  9551. if (input_format != input_format_t::bjdata)
  9552. {
  9553. break;
  9554. }
  9555. std::uint16_t len{};
  9556. return get_number(input_format, len) && get_string(input_format, len, result);
  9557. }
  9558. case 'm':
  9559. {
  9560. if (input_format != input_format_t::bjdata)
  9561. {
  9562. break;
  9563. }
  9564. std::uint32_t len{};
  9565. return get_number(input_format, len) && get_string(input_format, len, result);
  9566. }
  9567. case 'M':
  9568. {
  9569. if (input_format != input_format_t::bjdata)
  9570. {
  9571. break;
  9572. }
  9573. std::uint64_t len{};
  9574. return get_number(input_format, len) && get_string(input_format, len, result);
  9575. }
  9576. default:
  9577. break;
  9578. }
  9579. auto last_token = get_token_string();
  9580. std::string message;
  9581. if (input_format != input_format_t::bjdata)
  9582. {
  9583. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  9584. }
  9585. else
  9586. {
  9587. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  9588. }
  9589. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  9590. }
  9591. /*!
  9592. @param[out] dim an integer vector storing the ND array dimensions
  9593. @return whether reading ND array size vector is successful
  9594. */
  9595. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  9596. {
  9597. std::pair<std::size_t, char_int_type> size_and_type;
  9598. size_t dimlen = 0;
  9599. bool no_ndarray = true;
  9600. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  9601. {
  9602. return false;
  9603. }
  9604. if (size_and_type.first != npos)
  9605. {
  9606. if (size_and_type.second != 0)
  9607. {
  9608. if (size_and_type.second != 'N')
  9609. {
  9610. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9611. {
  9612. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  9613. {
  9614. return false;
  9615. }
  9616. dim.push_back(dimlen);
  9617. }
  9618. }
  9619. }
  9620. else
  9621. {
  9622. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9623. {
  9624. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  9625. {
  9626. return false;
  9627. }
  9628. dim.push_back(dimlen);
  9629. }
  9630. }
  9631. }
  9632. else
  9633. {
  9634. while (current != ']')
  9635. {
  9636. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  9637. {
  9638. return false;
  9639. }
  9640. dim.push_back(dimlen);
  9641. get_ignore_noop();
  9642. }
  9643. }
  9644. return true;
  9645. }
  9646. /*!
  9647. @param[out] result determined size
  9648. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  9649. or ndarray dimension is not allowed; `false` means ndarray
  9650. is allowed; for output, `true` means an ndarray is found;
  9651. is_ndarray can only return `true` when its initial value
  9652. is `false`
  9653. @param[in] prefix type marker if already read, otherwise set to 0
  9654. @return whether size determination completed
  9655. */
  9656. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  9657. {
  9658. if (prefix == 0)
  9659. {
  9660. prefix = get_ignore_noop();
  9661. }
  9662. switch (prefix)
  9663. {
  9664. case 'U':
  9665. {
  9666. std::uint8_t number{};
  9667. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9668. {
  9669. return false;
  9670. }
  9671. result = static_cast<std::size_t>(number);
  9672. return true;
  9673. }
  9674. case 'i':
  9675. {
  9676. std::int8_t number{};
  9677. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9678. {
  9679. return false;
  9680. }
  9681. if (number < 0)
  9682. {
  9683. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9684. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9685. }
  9686. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  9687. return true;
  9688. }
  9689. case 'I':
  9690. {
  9691. std::int16_t number{};
  9692. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9693. {
  9694. return false;
  9695. }
  9696. if (number < 0)
  9697. {
  9698. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9699. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9700. }
  9701. result = static_cast<std::size_t>(number);
  9702. return true;
  9703. }
  9704. case 'l':
  9705. {
  9706. std::int32_t number{};
  9707. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9708. {
  9709. return false;
  9710. }
  9711. if (number < 0)
  9712. {
  9713. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9714. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9715. }
  9716. result = static_cast<std::size_t>(number);
  9717. return true;
  9718. }
  9719. case 'L':
  9720. {
  9721. std::int64_t number{};
  9722. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9723. {
  9724. return false;
  9725. }
  9726. if (number < 0)
  9727. {
  9728. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9729. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9730. }
  9731. if (!value_in_range_of<std::size_t>(number))
  9732. {
  9733. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9734. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9735. }
  9736. result = static_cast<std::size_t>(number);
  9737. return true;
  9738. }
  9739. case 'u':
  9740. {
  9741. if (input_format != input_format_t::bjdata)
  9742. {
  9743. break;
  9744. }
  9745. std::uint16_t number{};
  9746. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9747. {
  9748. return false;
  9749. }
  9750. result = static_cast<std::size_t>(number);
  9751. return true;
  9752. }
  9753. case 'm':
  9754. {
  9755. if (input_format != input_format_t::bjdata)
  9756. {
  9757. break;
  9758. }
  9759. std::uint32_t number{};
  9760. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9761. {
  9762. return false;
  9763. }
  9764. result = conditional_static_cast<std::size_t>(number);
  9765. return true;
  9766. }
  9767. case 'M':
  9768. {
  9769. if (input_format != input_format_t::bjdata)
  9770. {
  9771. break;
  9772. }
  9773. std::uint64_t number{};
  9774. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9775. {
  9776. return false;
  9777. }
  9778. if (!value_in_range_of<std::size_t>(number))
  9779. {
  9780. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9781. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9782. }
  9783. result = detail::conditional_static_cast<std::size_t>(number);
  9784. return true;
  9785. }
  9786. case '[':
  9787. {
  9788. if (input_format != input_format_t::bjdata)
  9789. {
  9790. break;
  9791. }
  9792. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  9793. {
  9794. 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));
  9795. }
  9796. std::vector<size_t> dim;
  9797. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  9798. {
  9799. return false;
  9800. }
  9801. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  9802. {
  9803. result = dim.at(dim.size() - 1);
  9804. return true;
  9805. }
  9806. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  9807. {
  9808. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  9809. {
  9810. if ( i == 0 )
  9811. {
  9812. result = 0;
  9813. return true;
  9814. }
  9815. }
  9816. string_t key = "_ArraySize_";
  9817. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  9818. {
  9819. return false;
  9820. }
  9821. result = 1;
  9822. for (auto i : dim)
  9823. {
  9824. result *= i;
  9825. 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()
  9826. {
  9827. 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));
  9828. }
  9829. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  9830. {
  9831. return false;
  9832. }
  9833. }
  9834. is_ndarray = true;
  9835. return sax->end_array();
  9836. }
  9837. result = 0;
  9838. return true;
  9839. }
  9840. default:
  9841. break;
  9842. }
  9843. auto last_token = get_token_string();
  9844. std::string message;
  9845. if (input_format != input_format_t::bjdata)
  9846. {
  9847. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  9848. }
  9849. else
  9850. {
  9851. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  9852. }
  9853. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  9854. }
  9855. /*!
  9856. @brief determine the type and size for a container
  9857. In the optimized UBJSON format, a type and a size can be provided to allow
  9858. for a more compact representation.
  9859. @param[out] result pair of the size and the type
  9860. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  9861. @return whether pair creation completed
  9862. */
  9863. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  9864. {
  9865. result.first = npos; // size
  9866. result.second = 0; // type
  9867. bool is_ndarray = false;
  9868. get_ignore_noop();
  9869. if (current == '$')
  9870. {
  9871. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  9872. if (input_format == input_format_t::bjdata
  9873. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  9874. {
  9875. auto last_token = get_token_string();
  9876. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9877. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  9878. }
  9879. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  9880. {
  9881. return false;
  9882. }
  9883. get_ignore_noop();
  9884. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  9885. {
  9886. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9887. {
  9888. return false;
  9889. }
  9890. auto last_token = get_token_string();
  9891. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9892. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  9893. }
  9894. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9895. if (input_format == input_format_t::bjdata && is_ndarray)
  9896. {
  9897. if (inside_ndarray)
  9898. {
  9899. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9900. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  9901. }
  9902. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  9903. }
  9904. return is_error;
  9905. }
  9906. if (current == '#')
  9907. {
  9908. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9909. if (input_format == input_format_t::bjdata && is_ndarray)
  9910. {
  9911. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9912. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  9913. }
  9914. return is_error;
  9915. }
  9916. return true;
  9917. }
  9918. /*!
  9919. @param prefix the previously read or set type prefix
  9920. @return whether value creation completed
  9921. */
  9922. bool get_ubjson_value(const char_int_type prefix)
  9923. {
  9924. switch (prefix)
  9925. {
  9926. case std::char_traits<char_type>::eof(): // EOF
  9927. return unexpect_eof(input_format, "value");
  9928. case 'T': // true
  9929. return sax->boolean(true);
  9930. case 'F': // false
  9931. return sax->boolean(false);
  9932. case 'Z': // null
  9933. return sax->null();
  9934. case 'U':
  9935. {
  9936. std::uint8_t number{};
  9937. return get_number(input_format, number) && sax->number_unsigned(number);
  9938. }
  9939. case 'i':
  9940. {
  9941. std::int8_t number{};
  9942. return get_number(input_format, number) && sax->number_integer(number);
  9943. }
  9944. case 'I':
  9945. {
  9946. std::int16_t number{};
  9947. return get_number(input_format, number) && sax->number_integer(number);
  9948. }
  9949. case 'l':
  9950. {
  9951. std::int32_t number{};
  9952. return get_number(input_format, number) && sax->number_integer(number);
  9953. }
  9954. case 'L':
  9955. {
  9956. std::int64_t number{};
  9957. return get_number(input_format, number) && sax->number_integer(number);
  9958. }
  9959. case 'u':
  9960. {
  9961. if (input_format != input_format_t::bjdata)
  9962. {
  9963. break;
  9964. }
  9965. std::uint16_t number{};
  9966. return get_number(input_format, number) && sax->number_unsigned(number);
  9967. }
  9968. case 'm':
  9969. {
  9970. if (input_format != input_format_t::bjdata)
  9971. {
  9972. break;
  9973. }
  9974. std::uint32_t number{};
  9975. return get_number(input_format, number) && sax->number_unsigned(number);
  9976. }
  9977. case 'M':
  9978. {
  9979. if (input_format != input_format_t::bjdata)
  9980. {
  9981. break;
  9982. }
  9983. std::uint64_t number{};
  9984. return get_number(input_format, number) && sax->number_unsigned(number);
  9985. }
  9986. case 'h':
  9987. {
  9988. if (input_format != input_format_t::bjdata)
  9989. {
  9990. break;
  9991. }
  9992. const auto byte1_raw = get();
  9993. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9994. {
  9995. return false;
  9996. }
  9997. const auto byte2_raw = get();
  9998. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9999. {
  10000. return false;
  10001. }
  10002. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  10003. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10004. // code from RFC 7049, Appendix D, Figure 3:
  10005. // As half-precision floating-point numbers were only added
  10006. // to IEEE 754 in 2008, today's programming platforms often
  10007. // still only have limited support for them. It is very
  10008. // easy to include at least decoding support for them even
  10009. // without such support. An example of a small decoder for
  10010. // half-precision floating-point numbers in the C language
  10011. // is shown in Fig. 3.
  10012. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10013. const double val = [&half]
  10014. {
  10015. const int exp = (half >> 10u) & 0x1Fu;
  10016. const unsigned int mant = half & 0x3FFu;
  10017. JSON_ASSERT(0 <= exp&& exp <= 32);
  10018. JSON_ASSERT(mant <= 1024);
  10019. switch (exp)
  10020. {
  10021. case 0:
  10022. return std::ldexp(mant, -24);
  10023. case 31:
  10024. return (mant == 0)
  10025. ? std::numeric_limits<double>::infinity()
  10026. : std::numeric_limits<double>::quiet_NaN();
  10027. default:
  10028. return std::ldexp(mant + 1024, exp - 25);
  10029. }
  10030. }();
  10031. return sax->number_float((half & 0x8000u) != 0
  10032. ? static_cast<number_float_t>(-val)
  10033. : static_cast<number_float_t>(val), "");
  10034. }
  10035. case 'd':
  10036. {
  10037. float number{};
  10038. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10039. }
  10040. case 'D':
  10041. {
  10042. double number{};
  10043. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10044. }
  10045. case 'H':
  10046. {
  10047. return get_ubjson_high_precision_number();
  10048. }
  10049. case 'C': // char
  10050. {
  10051. get();
  10052. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10053. {
  10054. return false;
  10055. }
  10056. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10057. {
  10058. auto last_token = get_token_string();
  10059. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10060. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10061. }
  10062. string_t s(1, static_cast<typename string_t::value_type>(current));
  10063. return sax->string(s);
  10064. }
  10065. case 'S': // string
  10066. {
  10067. string_t s;
  10068. return get_ubjson_string(s) && sax->string(s);
  10069. }
  10070. case '[': // array
  10071. return get_ubjson_array();
  10072. case '{': // object
  10073. return get_ubjson_object();
  10074. default: // anything else
  10075. break;
  10076. }
  10077. auto last_token = get_token_string();
  10078. 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));
  10079. }
  10080. /*!
  10081. @return whether array creation completed
  10082. */
  10083. bool get_ubjson_array()
  10084. {
  10085. std::pair<std::size_t, char_int_type> size_and_type;
  10086. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10087. {
  10088. return false;
  10089. }
  10090. // 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):
  10091. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10092. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10093. {
  10094. 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
  10095. 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)
  10096. {
  10097. return p.first < t;
  10098. });
  10099. string_t key = "_ArrayType_";
  10100. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10101. {
  10102. auto last_token = get_token_string();
  10103. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10104. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10105. }
  10106. string_t type = it->second; // sax->string() takes a reference
  10107. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10108. {
  10109. return false;
  10110. }
  10111. if (size_and_type.second == 'C')
  10112. {
  10113. size_and_type.second = 'U';
  10114. }
  10115. key = "_ArrayData_";
  10116. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10117. {
  10118. return false;
  10119. }
  10120. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10121. {
  10122. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10123. {
  10124. return false;
  10125. }
  10126. }
  10127. return (sax->end_array() && sax->end_object());
  10128. }
  10129. if (size_and_type.first != npos)
  10130. {
  10131. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10132. {
  10133. return false;
  10134. }
  10135. if (size_and_type.second != 0)
  10136. {
  10137. if (size_and_type.second != 'N')
  10138. {
  10139. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10140. {
  10141. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10142. {
  10143. return false;
  10144. }
  10145. }
  10146. }
  10147. }
  10148. else
  10149. {
  10150. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10151. {
  10152. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10153. {
  10154. return false;
  10155. }
  10156. }
  10157. }
  10158. }
  10159. else
  10160. {
  10161. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10162. {
  10163. return false;
  10164. }
  10165. while (current != ']')
  10166. {
  10167. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10168. {
  10169. return false;
  10170. }
  10171. get_ignore_noop();
  10172. }
  10173. }
  10174. return sax->end_array();
  10175. }
  10176. /*!
  10177. @return whether object creation completed
  10178. */
  10179. bool get_ubjson_object()
  10180. {
  10181. std::pair<std::size_t, char_int_type> size_and_type;
  10182. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10183. {
  10184. return false;
  10185. }
  10186. // do not accept ND-array size in objects in BJData
  10187. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10188. {
  10189. auto last_token = get_token_string();
  10190. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10191. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10192. }
  10193. string_t key;
  10194. if (size_and_type.first != npos)
  10195. {
  10196. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10197. {
  10198. return false;
  10199. }
  10200. if (size_and_type.second != 0)
  10201. {
  10202. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10203. {
  10204. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10205. {
  10206. return false;
  10207. }
  10208. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10209. {
  10210. return false;
  10211. }
  10212. key.clear();
  10213. }
  10214. }
  10215. else
  10216. {
  10217. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10218. {
  10219. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10220. {
  10221. return false;
  10222. }
  10223. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10224. {
  10225. return false;
  10226. }
  10227. key.clear();
  10228. }
  10229. }
  10230. }
  10231. else
  10232. {
  10233. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10234. {
  10235. return false;
  10236. }
  10237. while (current != '}')
  10238. {
  10239. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  10240. {
  10241. return false;
  10242. }
  10243. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10244. {
  10245. return false;
  10246. }
  10247. get_ignore_noop();
  10248. key.clear();
  10249. }
  10250. }
  10251. return sax->end_object();
  10252. }
  10253. // Note, no reader for UBJSON binary types is implemented because they do
  10254. // not exist
  10255. bool get_ubjson_high_precision_number()
  10256. {
  10257. // get size of following number string
  10258. std::size_t size{};
  10259. bool no_ndarray = true;
  10260. auto res = get_ubjson_size_value(size, no_ndarray);
  10261. if (JSON_HEDLEY_UNLIKELY(!res))
  10262. {
  10263. return res;
  10264. }
  10265. // get number string
  10266. std::vector<char> number_vector;
  10267. for (std::size_t i = 0; i < size; ++i)
  10268. {
  10269. get();
  10270. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10271. {
  10272. return false;
  10273. }
  10274. number_vector.push_back(static_cast<char>(current));
  10275. }
  10276. // parse number string
  10277. using ia_type = decltype(detail::input_adapter(number_vector));
  10278. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  10279. const auto result_number = number_lexer.scan();
  10280. const auto number_string = number_lexer.get_token_string();
  10281. const auto result_remainder = number_lexer.scan();
  10282. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10283. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10284. {
  10285. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10286. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10287. }
  10288. switch (result_number)
  10289. {
  10290. case token_type::value_integer:
  10291. return sax->number_integer(number_lexer.get_number_integer());
  10292. case token_type::value_unsigned:
  10293. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10294. case token_type::value_float:
  10295. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10296. case token_type::uninitialized:
  10297. case token_type::literal_true:
  10298. case token_type::literal_false:
  10299. case token_type::literal_null:
  10300. case token_type::value_string:
  10301. case token_type::begin_array:
  10302. case token_type::begin_object:
  10303. case token_type::end_array:
  10304. case token_type::end_object:
  10305. case token_type::name_separator:
  10306. case token_type::value_separator:
  10307. case token_type::parse_error:
  10308. case token_type::end_of_input:
  10309. case token_type::literal_or_value:
  10310. default:
  10311. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10312. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10313. }
  10314. }
  10315. ///////////////////////
  10316. // Utility functions //
  10317. ///////////////////////
  10318. /*!
  10319. @brief get next character from the input
  10320. This function provides the interface to the used input adapter. It does
  10321. not throw in case the input reached EOF, but returns a -'ve valued
  10322. `std::char_traits<char_type>::eof()` in that case.
  10323. @return character read from the input
  10324. */
  10325. char_int_type get()
  10326. {
  10327. ++chars_read;
  10328. return current = ia.get_character();
  10329. }
  10330. /*!
  10331. @return character read from the input after ignoring all 'N' entries
  10332. */
  10333. char_int_type get_ignore_noop()
  10334. {
  10335. do
  10336. {
  10337. get();
  10338. }
  10339. while (current == 'N');
  10340. return current;
  10341. }
  10342. /*
  10343. @brief read a number from the input
  10344. @tparam NumberType the type of the number
  10345. @param[in] format the current format (for diagnostics)
  10346. @param[out] result number of type @a NumberType
  10347. @return whether conversion completed
  10348. @note This function needs to respect the system's endianness, because
  10349. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  10350. (big endian) and therefore need reordering on little endian systems.
  10351. On the other hand, BSON and BJData use little endian and should reorder
  10352. on big endian systems.
  10353. */
  10354. template<typename NumberType, bool InputIsLittleEndian = false>
  10355. bool get_number(const input_format_t format, NumberType& result)
  10356. {
  10357. // step 1: read input into array with system's byte order
  10358. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  10359. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  10360. {
  10361. get();
  10362. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  10363. {
  10364. return false;
  10365. }
  10366. // reverse byte order prior to conversion if necessary
  10367. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  10368. {
  10369. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  10370. }
  10371. else
  10372. {
  10373. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  10374. }
  10375. }
  10376. // step 2: convert array into number of type T and return
  10377. std::memcpy(&result, vec.data(), sizeof(NumberType));
  10378. return true;
  10379. }
  10380. /*!
  10381. @brief create a string by reading characters from the input
  10382. @tparam NumberType the type of the number
  10383. @param[in] format the current format (for diagnostics)
  10384. @param[in] len number of characters to read
  10385. @param[out] result string created by reading @a len bytes
  10386. @return whether string creation completed
  10387. @note We can not reserve @a len bytes for the result, because @a len
  10388. may be too large. Usually, @ref unexpect_eof() detects the end of
  10389. the input before we run out of string memory.
  10390. */
  10391. template<typename NumberType>
  10392. bool get_string(const input_format_t format,
  10393. const NumberType len,
  10394. string_t& result)
  10395. {
  10396. bool success = true;
  10397. for (NumberType i = 0; i < len; i++)
  10398. {
  10399. get();
  10400. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  10401. {
  10402. success = false;
  10403. break;
  10404. }
  10405. result.push_back(static_cast<typename string_t::value_type>(current));
  10406. }
  10407. return success;
  10408. }
  10409. /*!
  10410. @brief create a byte array by reading bytes from the input
  10411. @tparam NumberType the type of the number
  10412. @param[in] format the current format (for diagnostics)
  10413. @param[in] len number of bytes to read
  10414. @param[out] result byte array created by reading @a len bytes
  10415. @return whether byte array creation completed
  10416. @note We can not reserve @a len bytes for the result, because @a len
  10417. may be too large. Usually, @ref unexpect_eof() detects the end of
  10418. the input before we run out of memory.
  10419. */
  10420. template<typename NumberType>
  10421. bool get_binary(const input_format_t format,
  10422. const NumberType len,
  10423. binary_t& result)
  10424. {
  10425. bool success = true;
  10426. for (NumberType i = 0; i < len; i++)
  10427. {
  10428. get();
  10429. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  10430. {
  10431. success = false;
  10432. break;
  10433. }
  10434. result.push_back(static_cast<std::uint8_t>(current));
  10435. }
  10436. return success;
  10437. }
  10438. /*!
  10439. @param[in] format the current format (for diagnostics)
  10440. @param[in] context further context information (for diagnostics)
  10441. @return whether the last read character is not EOF
  10442. */
  10443. JSON_HEDLEY_NON_NULL(3)
  10444. bool unexpect_eof(const input_format_t format, const char* context) const
  10445. {
  10446. if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
  10447. {
  10448. return sax->parse_error(chars_read, "<end of file>",
  10449. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10450. }
  10451. return true;
  10452. }
  10453. /*!
  10454. @return a string representation of the last read byte
  10455. */
  10456. std::string get_token_string() const
  10457. {
  10458. std::array<char, 3> cr{{}};
  10459. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  10460. return std::string{cr.data()};
  10461. }
  10462. /*!
  10463. @param[in] format the current format
  10464. @param[in] detail a detailed error message
  10465. @param[in] context further context information
  10466. @return a message string to use in the parse_error exceptions
  10467. */
  10468. std::string exception_message(const input_format_t format,
  10469. const std::string& detail,
  10470. const std::string& context) const
  10471. {
  10472. std::string error_msg = "syntax error while parsing ";
  10473. switch (format)
  10474. {
  10475. case input_format_t::cbor:
  10476. error_msg += "CBOR";
  10477. break;
  10478. case input_format_t::msgpack:
  10479. error_msg += "MessagePack";
  10480. break;
  10481. case input_format_t::ubjson:
  10482. error_msg += "UBJSON";
  10483. break;
  10484. case input_format_t::bson:
  10485. error_msg += "BSON";
  10486. break;
  10487. case input_format_t::bjdata:
  10488. error_msg += "BJData";
  10489. break;
  10490. case input_format_t::json: // LCOV_EXCL_LINE
  10491. default: // LCOV_EXCL_LINE
  10492. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  10493. }
  10494. return concat(error_msg, ' ', context, ": ", detail);
  10495. }
  10496. private:
  10497. static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
  10498. /// input adapter
  10499. InputAdapterType ia;
  10500. /// the current character
  10501. char_int_type current = std::char_traits<char_type>::eof();
  10502. /// the number of characters read
  10503. std::size_t chars_read = 0;
  10504. /// whether we can assume little endianness
  10505. const bool is_little_endian = little_endianness();
  10506. /// input format
  10507. const input_format_t input_format = input_format_t::json;
  10508. /// the SAX parser
  10509. json_sax_t* sax = nullptr;
  10510. // excluded markers in bjdata optimized type
  10511. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  10512. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  10513. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  10514. make_array<bjd_type>( \
  10515. bjd_type{'C', "char"}, \
  10516. bjd_type{'D', "double"}, \
  10517. bjd_type{'I', "int16"}, \
  10518. bjd_type{'L', "int64"}, \
  10519. bjd_type{'M', "uint64"}, \
  10520. bjd_type{'U', "uint8"}, \
  10521. bjd_type{'d', "single"}, \
  10522. bjd_type{'i', "int8"}, \
  10523. bjd_type{'l', "int32"}, \
  10524. bjd_type{'m', "uint32"}, \
  10525. bjd_type{'u', "uint16"})
  10526. JSON_PRIVATE_UNLESS_TESTED:
  10527. // lookup tables
  10528. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10529. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  10530. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  10531. using bjd_type = std::pair<char_int_type, string_t>;
  10532. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10533. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  10534. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  10535. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  10536. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  10537. };
  10538. #ifndef JSON_HAS_CPP_17
  10539. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  10540. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  10541. #endif
  10542. } // namespace detail
  10543. NLOHMANN_JSON_NAMESPACE_END
  10544. // #include <nlohmann/detail/input/input_adapters.hpp>
  10545. // #include <nlohmann/detail/input/lexer.hpp>
  10546. // #include <nlohmann/detail/input/parser.hpp>
  10547. // __ _____ _____ _____
  10548. // __| | __| | | | JSON for Modern C++
  10549. // | | |__ | | | | | | version 3.11.2
  10550. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10551. //
  10552. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10553. // SPDX-License-Identifier: MIT
  10554. #include <cmath> // isfinite
  10555. #include <cstdint> // uint8_t
  10556. #include <functional> // function
  10557. #include <string> // string
  10558. #include <utility> // move
  10559. #include <vector> // vector
  10560. // #include <nlohmann/detail/exceptions.hpp>
  10561. // #include <nlohmann/detail/input/input_adapters.hpp>
  10562. // #include <nlohmann/detail/input/json_sax.hpp>
  10563. // #include <nlohmann/detail/input/lexer.hpp>
  10564. // #include <nlohmann/detail/macro_scope.hpp>
  10565. // #include <nlohmann/detail/meta/is_sax.hpp>
  10566. // #include <nlohmann/detail/string_concat.hpp>
  10567. // #include <nlohmann/detail/value_t.hpp>
  10568. NLOHMANN_JSON_NAMESPACE_BEGIN
  10569. namespace detail
  10570. {
  10571. ////////////
  10572. // parser //
  10573. ////////////
  10574. enum class parse_event_t : std::uint8_t
  10575. {
  10576. /// the parser read `{` and started to process a JSON object
  10577. object_start,
  10578. /// the parser read `}` and finished processing a JSON object
  10579. object_end,
  10580. /// the parser read `[` and started to process a JSON array
  10581. array_start,
  10582. /// the parser read `]` and finished processing a JSON array
  10583. array_end,
  10584. /// the parser read a key of a value in an object
  10585. key,
  10586. /// the parser finished reading a JSON value
  10587. value
  10588. };
  10589. template<typename BasicJsonType>
  10590. using parser_callback_t =
  10591. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  10592. /*!
  10593. @brief syntax analysis
  10594. This class implements a recursive descent parser.
  10595. */
  10596. template<typename BasicJsonType, typename InputAdapterType>
  10597. class parser
  10598. {
  10599. using number_integer_t = typename BasicJsonType::number_integer_t;
  10600. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  10601. using number_float_t = typename BasicJsonType::number_float_t;
  10602. using string_t = typename BasicJsonType::string_t;
  10603. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  10604. using token_type = typename lexer_t::token_type;
  10605. public:
  10606. /// a parser reading from an input adapter
  10607. explicit parser(InputAdapterType&& adapter,
  10608. const parser_callback_t<BasicJsonType> cb = nullptr,
  10609. const bool allow_exceptions_ = true,
  10610. const bool skip_comments = false)
  10611. : callback(cb)
  10612. , m_lexer(std::move(adapter), skip_comments)
  10613. , allow_exceptions(allow_exceptions_)
  10614. {
  10615. // read first token
  10616. get_token();
  10617. }
  10618. /*!
  10619. @brief public parser interface
  10620. @param[in] strict whether to expect the last token to be EOF
  10621. @param[in,out] result parsed JSON value
  10622. @throw parse_error.101 in case of an unexpected token
  10623. @throw parse_error.102 if to_unicode fails or surrogate error
  10624. @throw parse_error.103 if to_unicode fails
  10625. */
  10626. void parse(const bool strict, BasicJsonType& result)
  10627. {
  10628. if (callback)
  10629. {
  10630. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  10631. sax_parse_internal(&sdp);
  10632. // in strict mode, input must be completely read
  10633. if (strict && (get_token() != token_type::end_of_input))
  10634. {
  10635. sdp.parse_error(m_lexer.get_position(),
  10636. m_lexer.get_token_string(),
  10637. parse_error::create(101, m_lexer.get_position(),
  10638. exception_message(token_type::end_of_input, "value"), nullptr));
  10639. }
  10640. // in case of an error, return discarded value
  10641. if (sdp.is_errored())
  10642. {
  10643. result = value_t::discarded;
  10644. return;
  10645. }
  10646. // set top-level value to null if it was discarded by the callback
  10647. // function
  10648. if (result.is_discarded())
  10649. {
  10650. result = nullptr;
  10651. }
  10652. }
  10653. else
  10654. {
  10655. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  10656. sax_parse_internal(&sdp);
  10657. // in strict mode, input must be completely read
  10658. if (strict && (get_token() != token_type::end_of_input))
  10659. {
  10660. sdp.parse_error(m_lexer.get_position(),
  10661. m_lexer.get_token_string(),
  10662. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10663. }
  10664. // in case of an error, return discarded value
  10665. if (sdp.is_errored())
  10666. {
  10667. result = value_t::discarded;
  10668. return;
  10669. }
  10670. }
  10671. result.assert_invariant();
  10672. }
  10673. /*!
  10674. @brief public accept interface
  10675. @param[in] strict whether to expect the last token to be EOF
  10676. @return whether the input is a proper JSON text
  10677. */
  10678. bool accept(const bool strict = true)
  10679. {
  10680. json_sax_acceptor<BasicJsonType> sax_acceptor;
  10681. return sax_parse(&sax_acceptor, strict);
  10682. }
  10683. template<typename SAX>
  10684. JSON_HEDLEY_NON_NULL(2)
  10685. bool sax_parse(SAX* sax, const bool strict = true)
  10686. {
  10687. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  10688. const bool result = sax_parse_internal(sax);
  10689. // strict mode: next byte must be EOF
  10690. if (result && strict && (get_token() != token_type::end_of_input))
  10691. {
  10692. return sax->parse_error(m_lexer.get_position(),
  10693. m_lexer.get_token_string(),
  10694. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10695. }
  10696. return result;
  10697. }
  10698. private:
  10699. template<typename SAX>
  10700. JSON_HEDLEY_NON_NULL(2)
  10701. bool sax_parse_internal(SAX* sax)
  10702. {
  10703. // stack to remember the hierarchy of structured values we are parsing
  10704. // true = array; false = object
  10705. std::vector<bool> states;
  10706. // value to avoid a goto (see comment where set to true)
  10707. bool skip_to_state_evaluation = false;
  10708. while (true)
  10709. {
  10710. if (!skip_to_state_evaluation)
  10711. {
  10712. // invariant: get_token() was called before each iteration
  10713. switch (last_token)
  10714. {
  10715. case token_type::begin_object:
  10716. {
  10717. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10718. {
  10719. return false;
  10720. }
  10721. // closing } -> we are done
  10722. if (get_token() == token_type::end_object)
  10723. {
  10724. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10725. {
  10726. return false;
  10727. }
  10728. break;
  10729. }
  10730. // parse key
  10731. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  10732. {
  10733. return sax->parse_error(m_lexer.get_position(),
  10734. m_lexer.get_token_string(),
  10735. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10736. }
  10737. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10738. {
  10739. return false;
  10740. }
  10741. // parse separator (:)
  10742. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10743. {
  10744. return sax->parse_error(m_lexer.get_position(),
  10745. m_lexer.get_token_string(),
  10746. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10747. }
  10748. // remember we are now inside an object
  10749. states.push_back(false);
  10750. // parse values
  10751. get_token();
  10752. continue;
  10753. }
  10754. case token_type::begin_array:
  10755. {
  10756. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10757. {
  10758. return false;
  10759. }
  10760. // closing ] -> we are done
  10761. if (get_token() == token_type::end_array)
  10762. {
  10763. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10764. {
  10765. return false;
  10766. }
  10767. break;
  10768. }
  10769. // remember we are now inside an array
  10770. states.push_back(true);
  10771. // parse values (no need to call get_token)
  10772. continue;
  10773. }
  10774. case token_type::value_float:
  10775. {
  10776. const auto res = m_lexer.get_number_float();
  10777. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  10778. {
  10779. return sax->parse_error(m_lexer.get_position(),
  10780. m_lexer.get_token_string(),
  10781. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  10782. }
  10783. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  10784. {
  10785. return false;
  10786. }
  10787. break;
  10788. }
  10789. case token_type::literal_false:
  10790. {
  10791. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  10792. {
  10793. return false;
  10794. }
  10795. break;
  10796. }
  10797. case token_type::literal_null:
  10798. {
  10799. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  10800. {
  10801. return false;
  10802. }
  10803. break;
  10804. }
  10805. case token_type::literal_true:
  10806. {
  10807. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  10808. {
  10809. return false;
  10810. }
  10811. break;
  10812. }
  10813. case token_type::value_integer:
  10814. {
  10815. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  10816. {
  10817. return false;
  10818. }
  10819. break;
  10820. }
  10821. case token_type::value_string:
  10822. {
  10823. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  10824. {
  10825. return false;
  10826. }
  10827. break;
  10828. }
  10829. case token_type::value_unsigned:
  10830. {
  10831. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  10832. {
  10833. return false;
  10834. }
  10835. break;
  10836. }
  10837. case token_type::parse_error:
  10838. {
  10839. // using "uninitialized" to avoid "expected" message
  10840. return sax->parse_error(m_lexer.get_position(),
  10841. m_lexer.get_token_string(),
  10842. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  10843. }
  10844. case token_type::uninitialized:
  10845. case token_type::end_array:
  10846. case token_type::end_object:
  10847. case token_type::name_separator:
  10848. case token_type::value_separator:
  10849. case token_type::end_of_input:
  10850. case token_type::literal_or_value:
  10851. default: // the last token was unexpected
  10852. {
  10853. return sax->parse_error(m_lexer.get_position(),
  10854. m_lexer.get_token_string(),
  10855. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10856. }
  10857. }
  10858. }
  10859. else
  10860. {
  10861. skip_to_state_evaluation = false;
  10862. }
  10863. // we reached this line after we successfully parsed a value
  10864. if (states.empty())
  10865. {
  10866. // empty stack: we reached the end of the hierarchy: done
  10867. return true;
  10868. }
  10869. if (states.back()) // array
  10870. {
  10871. // comma -> next value
  10872. if (get_token() == token_type::value_separator)
  10873. {
  10874. // parse a new value
  10875. get_token();
  10876. continue;
  10877. }
  10878. // closing ]
  10879. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  10880. {
  10881. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10882. {
  10883. return false;
  10884. }
  10885. // We are done with this array. Before we can parse a
  10886. // new value, we need to evaluate the new state first.
  10887. // By setting skip_to_state_evaluation to false, we
  10888. // are effectively jumping to the beginning of this if.
  10889. JSON_ASSERT(!states.empty());
  10890. states.pop_back();
  10891. skip_to_state_evaluation = true;
  10892. continue;
  10893. }
  10894. return sax->parse_error(m_lexer.get_position(),
  10895. m_lexer.get_token_string(),
  10896. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  10897. }
  10898. // states.back() is false -> object
  10899. // comma -> next value
  10900. if (get_token() == token_type::value_separator)
  10901. {
  10902. // parse key
  10903. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  10904. {
  10905. return sax->parse_error(m_lexer.get_position(),
  10906. m_lexer.get_token_string(),
  10907. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10908. }
  10909. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10910. {
  10911. return false;
  10912. }
  10913. // parse separator (:)
  10914. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10915. {
  10916. return sax->parse_error(m_lexer.get_position(),
  10917. m_lexer.get_token_string(),
  10918. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10919. }
  10920. // parse values
  10921. get_token();
  10922. continue;
  10923. }
  10924. // closing }
  10925. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  10926. {
  10927. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10928. {
  10929. return false;
  10930. }
  10931. // We are done with this object. Before we can parse a
  10932. // new value, we need to evaluate the new state first.
  10933. // By setting skip_to_state_evaluation to false, we
  10934. // are effectively jumping to the beginning of this if.
  10935. JSON_ASSERT(!states.empty());
  10936. states.pop_back();
  10937. skip_to_state_evaluation = true;
  10938. continue;
  10939. }
  10940. return sax->parse_error(m_lexer.get_position(),
  10941. m_lexer.get_token_string(),
  10942. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  10943. }
  10944. }
  10945. /// get next token from lexer
  10946. token_type get_token()
  10947. {
  10948. return last_token = m_lexer.scan();
  10949. }
  10950. std::string exception_message(const token_type expected, const std::string& context)
  10951. {
  10952. std::string error_msg = "syntax error ";
  10953. if (!context.empty())
  10954. {
  10955. error_msg += concat("while parsing ", context, ' ');
  10956. }
  10957. error_msg += "- ";
  10958. if (last_token == token_type::parse_error)
  10959. {
  10960. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  10961. m_lexer.get_token_string(), '\'');
  10962. }
  10963. else
  10964. {
  10965. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  10966. }
  10967. if (expected != token_type::uninitialized)
  10968. {
  10969. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  10970. }
  10971. return error_msg;
  10972. }
  10973. private:
  10974. /// callback function
  10975. const parser_callback_t<BasicJsonType> callback = nullptr;
  10976. /// the type of the last read token
  10977. token_type last_token = token_type::uninitialized;
  10978. /// the lexer
  10979. lexer_t m_lexer;
  10980. /// whether to throw exceptions in case of errors
  10981. const bool allow_exceptions = true;
  10982. };
  10983. } // namespace detail
  10984. NLOHMANN_JSON_NAMESPACE_END
  10985. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  10986. // __ _____ _____ _____
  10987. // __| | __| | | | JSON for Modern C++
  10988. // | | |__ | | | | | | version 3.11.2
  10989. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10990. //
  10991. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10992. // SPDX-License-Identifier: MIT
  10993. // #include <nlohmann/detail/abi_macros.hpp>
  10994. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  10995. // __ _____ _____ _____
  10996. // __| | __| | | | JSON for Modern C++
  10997. // | | |__ | | | | | | version 3.11.2
  10998. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10999. //
  11000. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11001. // SPDX-License-Identifier: MIT
  11002. #include <cstddef> // ptrdiff_t
  11003. #include <limits> // numeric_limits
  11004. // #include <nlohmann/detail/macro_scope.hpp>
  11005. NLOHMANN_JSON_NAMESPACE_BEGIN
  11006. namespace detail
  11007. {
  11008. /*
  11009. @brief an iterator for primitive JSON types
  11010. This class models an iterator for primitive JSON types (boolean, number,
  11011. string). It's only purpose is to allow the iterator/const_iterator classes
  11012. to "iterate" over primitive values. Internally, the iterator is modeled by
  11013. a `difference_type` variable. Value begin_value (`0`) models the begin,
  11014. end_value (`1`) models past the end.
  11015. */
  11016. class primitive_iterator_t
  11017. {
  11018. private:
  11019. using difference_type = std::ptrdiff_t;
  11020. static constexpr difference_type begin_value = 0;
  11021. static constexpr difference_type end_value = begin_value + 1;
  11022. JSON_PRIVATE_UNLESS_TESTED:
  11023. /// iterator as signed integer type
  11024. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11025. public:
  11026. constexpr difference_type get_value() const noexcept
  11027. {
  11028. return m_it;
  11029. }
  11030. /// set iterator to a defined beginning
  11031. void set_begin() noexcept
  11032. {
  11033. m_it = begin_value;
  11034. }
  11035. /// set iterator to a defined past the end
  11036. void set_end() noexcept
  11037. {
  11038. m_it = end_value;
  11039. }
  11040. /// return whether the iterator can be dereferenced
  11041. constexpr bool is_begin() const noexcept
  11042. {
  11043. return m_it == begin_value;
  11044. }
  11045. /// return whether the iterator is at end
  11046. constexpr bool is_end() const noexcept
  11047. {
  11048. return m_it == end_value;
  11049. }
  11050. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11051. {
  11052. return lhs.m_it == rhs.m_it;
  11053. }
  11054. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11055. {
  11056. return lhs.m_it < rhs.m_it;
  11057. }
  11058. primitive_iterator_t operator+(difference_type n) noexcept
  11059. {
  11060. auto result = *this;
  11061. result += n;
  11062. return result;
  11063. }
  11064. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11065. {
  11066. return lhs.m_it - rhs.m_it;
  11067. }
  11068. primitive_iterator_t& operator++() noexcept
  11069. {
  11070. ++m_it;
  11071. return *this;
  11072. }
  11073. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11074. {
  11075. auto result = *this;
  11076. ++m_it;
  11077. return result;
  11078. }
  11079. primitive_iterator_t& operator--() noexcept
  11080. {
  11081. --m_it;
  11082. return *this;
  11083. }
  11084. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11085. {
  11086. auto result = *this;
  11087. --m_it;
  11088. return result;
  11089. }
  11090. primitive_iterator_t& operator+=(difference_type n) noexcept
  11091. {
  11092. m_it += n;
  11093. return *this;
  11094. }
  11095. primitive_iterator_t& operator-=(difference_type n) noexcept
  11096. {
  11097. m_it -= n;
  11098. return *this;
  11099. }
  11100. };
  11101. } // namespace detail
  11102. NLOHMANN_JSON_NAMESPACE_END
  11103. NLOHMANN_JSON_NAMESPACE_BEGIN
  11104. namespace detail
  11105. {
  11106. /*!
  11107. @brief an iterator value
  11108. @note This structure could easily be a union, but MSVC currently does not allow
  11109. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11110. */
  11111. template<typename BasicJsonType> struct internal_iterator
  11112. {
  11113. /// iterator for JSON objects
  11114. typename BasicJsonType::object_t::iterator object_iterator {};
  11115. /// iterator for JSON arrays
  11116. typename BasicJsonType::array_t::iterator array_iterator {};
  11117. /// generic iterator for all other types
  11118. primitive_iterator_t primitive_iterator {};
  11119. };
  11120. } // namespace detail
  11121. NLOHMANN_JSON_NAMESPACE_END
  11122. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11123. // __ _____ _____ _____
  11124. // __| | __| | | | JSON for Modern C++
  11125. // | | |__ | | | | | | version 3.11.2
  11126. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11127. //
  11128. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11129. // SPDX-License-Identifier: MIT
  11130. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11131. #include <type_traits> // conditional, is_const, remove_const
  11132. // #include <nlohmann/detail/exceptions.hpp>
  11133. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11134. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11135. // #include <nlohmann/detail/macro_scope.hpp>
  11136. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11137. // #include <nlohmann/detail/meta/type_traits.hpp>
  11138. // #include <nlohmann/detail/value_t.hpp>
  11139. NLOHMANN_JSON_NAMESPACE_BEGIN
  11140. namespace detail
  11141. {
  11142. // forward declare, to be able to friend it later on
  11143. template<typename IteratorType> class iteration_proxy;
  11144. template<typename IteratorType> class iteration_proxy_value;
  11145. /*!
  11146. @brief a template for a bidirectional iterator for the @ref basic_json class
  11147. This class implements a both iterators (iterator and const_iterator) for the
  11148. @ref basic_json class.
  11149. @note An iterator is called *initialized* when a pointer to a JSON value has
  11150. been set (e.g., by a constructor or a copy assignment). If the iterator is
  11151. default-constructed, it is *uninitialized* and most methods are undefined.
  11152. **The library uses assertions to detect calls on uninitialized iterators.**
  11153. @requirement The class satisfies the following concept requirements:
  11154. -
  11155. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11156. The iterator that can be moved can be moved in both directions (i.e.
  11157. incremented and decremented).
  11158. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  11159. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  11160. */
  11161. template<typename BasicJsonType>
  11162. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  11163. {
  11164. /// the iterator with BasicJsonType of different const-ness
  11165. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  11166. /// allow basic_json to access private members
  11167. friend other_iter_impl;
  11168. friend BasicJsonType;
  11169. friend iteration_proxy<iter_impl>;
  11170. friend iteration_proxy_value<iter_impl>;
  11171. using object_t = typename BasicJsonType::object_t;
  11172. using array_t = typename BasicJsonType::array_t;
  11173. // make sure BasicJsonType is basic_json or const basic_json
  11174. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  11175. "iter_impl only accepts (const) basic_json");
  11176. // superficial check for the LegacyBidirectionalIterator named requirement
  11177. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  11178. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  11179. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  11180. public:
  11181. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  11182. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  11183. /// A user-defined iterator should provide publicly accessible typedefs named
  11184. /// iterator_category, value_type, difference_type, pointer, and reference.
  11185. /// Note that value_type is required to be non-const, even for constant iterators.
  11186. using iterator_category = std::bidirectional_iterator_tag;
  11187. /// the type of the values when the iterator is dereferenced
  11188. using value_type = typename BasicJsonType::value_type;
  11189. /// a type to represent differences between iterators
  11190. using difference_type = typename BasicJsonType::difference_type;
  11191. /// defines a pointer to the type iterated over (value_type)
  11192. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  11193. typename BasicJsonType::const_pointer,
  11194. typename BasicJsonType::pointer>::type;
  11195. /// defines a reference to the type iterated over (value_type)
  11196. using reference =
  11197. typename std::conditional<std::is_const<BasicJsonType>::value,
  11198. typename BasicJsonType::const_reference,
  11199. typename BasicJsonType::reference>::type;
  11200. iter_impl() = default;
  11201. ~iter_impl() = default;
  11202. iter_impl(iter_impl&&) noexcept = default;
  11203. iter_impl& operator=(iter_impl&&) noexcept = default;
  11204. /*!
  11205. @brief constructor for a given JSON instance
  11206. @param[in] object pointer to a JSON object for this iterator
  11207. @pre object != nullptr
  11208. @post The iterator is initialized; i.e. `m_object != nullptr`.
  11209. */
  11210. explicit iter_impl(pointer object) noexcept : m_object(object)
  11211. {
  11212. JSON_ASSERT(m_object != nullptr);
  11213. switch (m_object->m_type)
  11214. {
  11215. case value_t::object:
  11216. {
  11217. m_it.object_iterator = typename object_t::iterator();
  11218. break;
  11219. }
  11220. case value_t::array:
  11221. {
  11222. m_it.array_iterator = typename array_t::iterator();
  11223. break;
  11224. }
  11225. case value_t::null:
  11226. case value_t::string:
  11227. case value_t::boolean:
  11228. case value_t::number_integer:
  11229. case value_t::number_unsigned:
  11230. case value_t::number_float:
  11231. case value_t::binary:
  11232. case value_t::discarded:
  11233. default:
  11234. {
  11235. m_it.primitive_iterator = primitive_iterator_t();
  11236. break;
  11237. }
  11238. }
  11239. }
  11240. /*!
  11241. @note The conventional copy constructor and copy assignment are implicitly
  11242. defined. Combined with the following converting constructor and
  11243. assignment, they support: (1) copy from iterator to iterator, (2)
  11244. copy from const iterator to const iterator, and (3) conversion from
  11245. iterator to const iterator. However conversion from const iterator
  11246. to iterator is not defined.
  11247. */
  11248. /*!
  11249. @brief const copy constructor
  11250. @param[in] other const iterator to copy from
  11251. @note This copy constructor had to be defined explicitly to circumvent a bug
  11252. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  11253. information refer to: https://github.com/nlohmann/json/issues/1608
  11254. */
  11255. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  11256. : m_object(other.m_object), m_it(other.m_it)
  11257. {}
  11258. /*!
  11259. @brief converting assignment
  11260. @param[in] other const iterator to copy from
  11261. @return const/non-const iterator
  11262. @note It is not checked whether @a other is initialized.
  11263. */
  11264. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  11265. {
  11266. if (&other != this)
  11267. {
  11268. m_object = other.m_object;
  11269. m_it = other.m_it;
  11270. }
  11271. return *this;
  11272. }
  11273. /*!
  11274. @brief converting constructor
  11275. @param[in] other non-const iterator to copy from
  11276. @note It is not checked whether @a other is initialized.
  11277. */
  11278. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  11279. : m_object(other.m_object), m_it(other.m_it)
  11280. {}
  11281. /*!
  11282. @brief converting assignment
  11283. @param[in] other non-const iterator to copy from
  11284. @return const/non-const iterator
  11285. @note It is not checked whether @a other is initialized.
  11286. */
  11287. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  11288. {
  11289. m_object = other.m_object;
  11290. m_it = other.m_it;
  11291. return *this;
  11292. }
  11293. JSON_PRIVATE_UNLESS_TESTED:
  11294. /*!
  11295. @brief set the iterator to the first value
  11296. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11297. */
  11298. void set_begin() noexcept
  11299. {
  11300. JSON_ASSERT(m_object != nullptr);
  11301. switch (m_object->m_type)
  11302. {
  11303. case value_t::object:
  11304. {
  11305. m_it.object_iterator = m_object->m_value.object->begin();
  11306. break;
  11307. }
  11308. case value_t::array:
  11309. {
  11310. m_it.array_iterator = m_object->m_value.array->begin();
  11311. break;
  11312. }
  11313. case value_t::null:
  11314. {
  11315. // set to end so begin()==end() is true: null is empty
  11316. m_it.primitive_iterator.set_end();
  11317. break;
  11318. }
  11319. case value_t::string:
  11320. case value_t::boolean:
  11321. case value_t::number_integer:
  11322. case value_t::number_unsigned:
  11323. case value_t::number_float:
  11324. case value_t::binary:
  11325. case value_t::discarded:
  11326. default:
  11327. {
  11328. m_it.primitive_iterator.set_begin();
  11329. break;
  11330. }
  11331. }
  11332. }
  11333. /*!
  11334. @brief set the iterator past the last value
  11335. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11336. */
  11337. void set_end() noexcept
  11338. {
  11339. JSON_ASSERT(m_object != nullptr);
  11340. switch (m_object->m_type)
  11341. {
  11342. case value_t::object:
  11343. {
  11344. m_it.object_iterator = m_object->m_value.object->end();
  11345. break;
  11346. }
  11347. case value_t::array:
  11348. {
  11349. m_it.array_iterator = m_object->m_value.array->end();
  11350. break;
  11351. }
  11352. case value_t::null:
  11353. case value_t::string:
  11354. case value_t::boolean:
  11355. case value_t::number_integer:
  11356. case value_t::number_unsigned:
  11357. case value_t::number_float:
  11358. case value_t::binary:
  11359. case value_t::discarded:
  11360. default:
  11361. {
  11362. m_it.primitive_iterator.set_end();
  11363. break;
  11364. }
  11365. }
  11366. }
  11367. public:
  11368. /*!
  11369. @brief return a reference to the value pointed to by the iterator
  11370. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11371. */
  11372. reference operator*() const
  11373. {
  11374. JSON_ASSERT(m_object != nullptr);
  11375. switch (m_object->m_type)
  11376. {
  11377. case value_t::object:
  11378. {
  11379. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  11380. return m_it.object_iterator->second;
  11381. }
  11382. case value_t::array:
  11383. {
  11384. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  11385. return *m_it.array_iterator;
  11386. }
  11387. case value_t::null:
  11388. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11389. case value_t::string:
  11390. case value_t::boolean:
  11391. case value_t::number_integer:
  11392. case value_t::number_unsigned:
  11393. case value_t::number_float:
  11394. case value_t::binary:
  11395. case value_t::discarded:
  11396. default:
  11397. {
  11398. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11399. {
  11400. return *m_object;
  11401. }
  11402. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11403. }
  11404. }
  11405. }
  11406. /*!
  11407. @brief dereference the iterator
  11408. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11409. */
  11410. pointer operator->() const
  11411. {
  11412. JSON_ASSERT(m_object != nullptr);
  11413. switch (m_object->m_type)
  11414. {
  11415. case value_t::object:
  11416. {
  11417. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  11418. return &(m_it.object_iterator->second);
  11419. }
  11420. case value_t::array:
  11421. {
  11422. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  11423. return &*m_it.array_iterator;
  11424. }
  11425. case value_t::null:
  11426. case value_t::string:
  11427. case value_t::boolean:
  11428. case value_t::number_integer:
  11429. case value_t::number_unsigned:
  11430. case value_t::number_float:
  11431. case value_t::binary:
  11432. case value_t::discarded:
  11433. default:
  11434. {
  11435. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11436. {
  11437. return m_object;
  11438. }
  11439. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11440. }
  11441. }
  11442. }
  11443. /*!
  11444. @brief post-increment (it++)
  11445. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11446. */
  11447. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  11448. {
  11449. auto result = *this;
  11450. ++(*this);
  11451. return result;
  11452. }
  11453. /*!
  11454. @brief pre-increment (++it)
  11455. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11456. */
  11457. iter_impl& operator++()
  11458. {
  11459. JSON_ASSERT(m_object != nullptr);
  11460. switch (m_object->m_type)
  11461. {
  11462. case value_t::object:
  11463. {
  11464. std::advance(m_it.object_iterator, 1);
  11465. break;
  11466. }
  11467. case value_t::array:
  11468. {
  11469. std::advance(m_it.array_iterator, 1);
  11470. break;
  11471. }
  11472. case value_t::null:
  11473. case value_t::string:
  11474. case value_t::boolean:
  11475. case value_t::number_integer:
  11476. case value_t::number_unsigned:
  11477. case value_t::number_float:
  11478. case value_t::binary:
  11479. case value_t::discarded:
  11480. default:
  11481. {
  11482. ++m_it.primitive_iterator;
  11483. break;
  11484. }
  11485. }
  11486. return *this;
  11487. }
  11488. /*!
  11489. @brief post-decrement (it--)
  11490. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11491. */
  11492. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  11493. {
  11494. auto result = *this;
  11495. --(*this);
  11496. return result;
  11497. }
  11498. /*!
  11499. @brief pre-decrement (--it)
  11500. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11501. */
  11502. iter_impl& operator--()
  11503. {
  11504. JSON_ASSERT(m_object != nullptr);
  11505. switch (m_object->m_type)
  11506. {
  11507. case value_t::object:
  11508. {
  11509. std::advance(m_it.object_iterator, -1);
  11510. break;
  11511. }
  11512. case value_t::array:
  11513. {
  11514. std::advance(m_it.array_iterator, -1);
  11515. break;
  11516. }
  11517. case value_t::null:
  11518. case value_t::string:
  11519. case value_t::boolean:
  11520. case value_t::number_integer:
  11521. case value_t::number_unsigned:
  11522. case value_t::number_float:
  11523. case value_t::binary:
  11524. case value_t::discarded:
  11525. default:
  11526. {
  11527. --m_it.primitive_iterator;
  11528. break;
  11529. }
  11530. }
  11531. return *this;
  11532. }
  11533. /*!
  11534. @brief comparison: equal
  11535. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11536. */
  11537. 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 >
  11538. bool operator==(const IterImpl& other) const
  11539. {
  11540. // if objects are not the same, the comparison is undefined
  11541. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11542. {
  11543. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11544. }
  11545. JSON_ASSERT(m_object != nullptr);
  11546. switch (m_object->m_type)
  11547. {
  11548. case value_t::object:
  11549. return (m_it.object_iterator == other.m_it.object_iterator);
  11550. case value_t::array:
  11551. return (m_it.array_iterator == other.m_it.array_iterator);
  11552. case value_t::null:
  11553. case value_t::string:
  11554. case value_t::boolean:
  11555. case value_t::number_integer:
  11556. case value_t::number_unsigned:
  11557. case value_t::number_float:
  11558. case value_t::binary:
  11559. case value_t::discarded:
  11560. default:
  11561. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  11562. }
  11563. }
  11564. /*!
  11565. @brief comparison: not equal
  11566. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11567. */
  11568. 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 >
  11569. bool operator!=(const IterImpl& other) const
  11570. {
  11571. return !operator==(other);
  11572. }
  11573. /*!
  11574. @brief comparison: smaller
  11575. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11576. */
  11577. bool operator<(const iter_impl& other) const
  11578. {
  11579. // if objects are not the same, the comparison is undefined
  11580. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11581. {
  11582. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11583. }
  11584. JSON_ASSERT(m_object != nullptr);
  11585. switch (m_object->m_type)
  11586. {
  11587. case value_t::object:
  11588. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  11589. case value_t::array:
  11590. return (m_it.array_iterator < other.m_it.array_iterator);
  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. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  11601. }
  11602. }
  11603. /*!
  11604. @brief comparison: less than or equal
  11605. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11606. */
  11607. bool operator<=(const iter_impl& other) const
  11608. {
  11609. return !other.operator < (*this);
  11610. }
  11611. /*!
  11612. @brief comparison: greater than
  11613. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11614. */
  11615. bool operator>(const iter_impl& other) const
  11616. {
  11617. return !operator<=(other);
  11618. }
  11619. /*!
  11620. @brief comparison: greater than or equal
  11621. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11622. */
  11623. bool operator>=(const iter_impl& other) const
  11624. {
  11625. return !operator<(other);
  11626. }
  11627. /*!
  11628. @brief add to iterator
  11629. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11630. */
  11631. iter_impl& operator+=(difference_type i)
  11632. {
  11633. JSON_ASSERT(m_object != nullptr);
  11634. switch (m_object->m_type)
  11635. {
  11636. case value_t::object:
  11637. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11638. case value_t::array:
  11639. {
  11640. std::advance(m_it.array_iterator, i);
  11641. break;
  11642. }
  11643. case value_t::null:
  11644. case value_t::string:
  11645. case value_t::boolean:
  11646. case value_t::number_integer:
  11647. case value_t::number_unsigned:
  11648. case value_t::number_float:
  11649. case value_t::binary:
  11650. case value_t::discarded:
  11651. default:
  11652. {
  11653. m_it.primitive_iterator += i;
  11654. break;
  11655. }
  11656. }
  11657. return *this;
  11658. }
  11659. /*!
  11660. @brief subtract from iterator
  11661. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11662. */
  11663. iter_impl& operator-=(difference_type i)
  11664. {
  11665. return operator+=(-i);
  11666. }
  11667. /*!
  11668. @brief add to iterator
  11669. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11670. */
  11671. iter_impl operator+(difference_type i) const
  11672. {
  11673. auto result = *this;
  11674. result += i;
  11675. return result;
  11676. }
  11677. /*!
  11678. @brief addition of distance and iterator
  11679. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11680. */
  11681. friend iter_impl operator+(difference_type i, const iter_impl& it)
  11682. {
  11683. auto result = it;
  11684. result += i;
  11685. return result;
  11686. }
  11687. /*!
  11688. @brief subtract from iterator
  11689. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11690. */
  11691. iter_impl operator-(difference_type i) const
  11692. {
  11693. auto result = *this;
  11694. result -= i;
  11695. return result;
  11696. }
  11697. /*!
  11698. @brief return difference
  11699. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11700. */
  11701. difference_type operator-(const iter_impl& other) const
  11702. {
  11703. JSON_ASSERT(m_object != nullptr);
  11704. switch (m_object->m_type)
  11705. {
  11706. case value_t::object:
  11707. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11708. case value_t::array:
  11709. return m_it.array_iterator - other.m_it.array_iterator;
  11710. case value_t::null:
  11711. case value_t::string:
  11712. case value_t::boolean:
  11713. case value_t::number_integer:
  11714. case value_t::number_unsigned:
  11715. case value_t::number_float:
  11716. case value_t::binary:
  11717. case value_t::discarded:
  11718. default:
  11719. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  11720. }
  11721. }
  11722. /*!
  11723. @brief access to successor
  11724. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11725. */
  11726. reference operator[](difference_type n) const
  11727. {
  11728. JSON_ASSERT(m_object != nullptr);
  11729. switch (m_object->m_type)
  11730. {
  11731. case value_t::object:
  11732. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  11733. case value_t::array:
  11734. return *std::next(m_it.array_iterator, n);
  11735. case value_t::null:
  11736. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11737. case value_t::string:
  11738. case value_t::boolean:
  11739. case value_t::number_integer:
  11740. case value_t::number_unsigned:
  11741. case value_t::number_float:
  11742. case value_t::binary:
  11743. case value_t::discarded:
  11744. default:
  11745. {
  11746. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  11747. {
  11748. return *m_object;
  11749. }
  11750. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11751. }
  11752. }
  11753. }
  11754. /*!
  11755. @brief return the key of an object iterator
  11756. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11757. */
  11758. const typename object_t::key_type& key() const
  11759. {
  11760. JSON_ASSERT(m_object != nullptr);
  11761. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  11762. {
  11763. return m_it.object_iterator->first;
  11764. }
  11765. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  11766. }
  11767. /*!
  11768. @brief return the value of an iterator
  11769. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11770. */
  11771. reference value() const
  11772. {
  11773. return operator*();
  11774. }
  11775. JSON_PRIVATE_UNLESS_TESTED:
  11776. /// associated JSON instance
  11777. pointer m_object = nullptr;
  11778. /// the actual iterator of the associated instance
  11779. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  11780. };
  11781. } // namespace detail
  11782. NLOHMANN_JSON_NAMESPACE_END
  11783. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  11784. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  11785. // __ _____ _____ _____
  11786. // __| | __| | | | JSON for Modern C++
  11787. // | | |__ | | | | | | version 3.11.2
  11788. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11789. //
  11790. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11791. // SPDX-License-Identifier: MIT
  11792. #include <cstddef> // ptrdiff_t
  11793. #include <iterator> // reverse_iterator
  11794. #include <utility> // declval
  11795. // #include <nlohmann/detail/abi_macros.hpp>
  11796. NLOHMANN_JSON_NAMESPACE_BEGIN
  11797. namespace detail
  11798. {
  11799. //////////////////////
  11800. // reverse_iterator //
  11801. //////////////////////
  11802. /*!
  11803. @brief a template for a reverse iterator class
  11804. @tparam Base the base iterator type to reverse. Valid types are @ref
  11805. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  11806. create @ref const_reverse_iterator).
  11807. @requirement The class satisfies the following concept requirements:
  11808. -
  11809. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11810. The iterator that can be moved can be moved in both directions (i.e.
  11811. incremented and decremented).
  11812. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  11813. It is possible to write to the pointed-to element (only if @a Base is
  11814. @ref iterator).
  11815. @since version 1.0.0
  11816. */
  11817. template<typename Base>
  11818. class json_reverse_iterator : public std::reverse_iterator<Base>
  11819. {
  11820. public:
  11821. using difference_type = std::ptrdiff_t;
  11822. /// shortcut to the reverse iterator adapter
  11823. using base_iterator = std::reverse_iterator<Base>;
  11824. /// the reference type for the pointed-to element
  11825. using reference = typename Base::reference;
  11826. /// create reverse iterator from iterator
  11827. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  11828. : base_iterator(it) {}
  11829. /// create reverse iterator from base class
  11830. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  11831. /// post-increment (it++)
  11832. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  11833. {
  11834. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  11835. }
  11836. /// pre-increment (++it)
  11837. json_reverse_iterator& operator++()
  11838. {
  11839. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  11840. }
  11841. /// post-decrement (it--)
  11842. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  11843. {
  11844. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  11845. }
  11846. /// pre-decrement (--it)
  11847. json_reverse_iterator& operator--()
  11848. {
  11849. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  11850. }
  11851. /// add to iterator
  11852. json_reverse_iterator& operator+=(difference_type i)
  11853. {
  11854. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  11855. }
  11856. /// add to iterator
  11857. json_reverse_iterator operator+(difference_type i) const
  11858. {
  11859. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  11860. }
  11861. /// subtract from iterator
  11862. json_reverse_iterator operator-(difference_type i) const
  11863. {
  11864. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  11865. }
  11866. /// return difference
  11867. difference_type operator-(const json_reverse_iterator& other) const
  11868. {
  11869. return base_iterator(*this) - base_iterator(other);
  11870. }
  11871. /// access to successor
  11872. reference operator[](difference_type n) const
  11873. {
  11874. return *(this->operator+(n));
  11875. }
  11876. /// return the key of an object iterator
  11877. auto key() const -> decltype(std::declval<Base>().key())
  11878. {
  11879. auto it = --this->base();
  11880. return it.key();
  11881. }
  11882. /// return the value of an iterator
  11883. reference value() const
  11884. {
  11885. auto it = --this->base();
  11886. return it.operator * ();
  11887. }
  11888. };
  11889. } // namespace detail
  11890. NLOHMANN_JSON_NAMESPACE_END
  11891. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11892. // #include <nlohmann/detail/json_custom_base_class.hpp>
  11893. #include <type_traits> // conditional, is_same
  11894. // #include <nlohmann/detail/abi_macros.hpp>
  11895. NLOHMANN_JSON_NAMESPACE_BEGIN
  11896. namespace detail
  11897. {
  11898. /*!
  11899. @brief Default base class of the @ref basic_json class.
  11900. So that the correct implementations of the copy / move ctors / assign operators
  11901. of @ref basic_json do not require complex case distinctions
  11902. (no base class / custom base class used as customization point),
  11903. @ref basic_json always has a base class.
  11904. By default, this class is used because it is empty and thus has no effect
  11905. on the behavior of @ref basic_json.
  11906. */
  11907. struct json_default_base {};
  11908. template<class T>
  11909. using json_base_class = typename std::conditional <
  11910. std::is_same<T, void>::value,
  11911. json_default_base,
  11912. T
  11913. >::type;
  11914. } // namespace detail
  11915. NLOHMANN_JSON_NAMESPACE_END
  11916. // #include <nlohmann/detail/json_pointer.hpp>
  11917. // __ _____ _____ _____
  11918. // __| | __| | | | JSON for Modern C++
  11919. // | | |__ | | | | | | version 3.11.2
  11920. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11921. //
  11922. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11923. // SPDX-License-Identifier: MIT
  11924. #include <algorithm> // all_of
  11925. #include <cctype> // isdigit
  11926. #include <cerrno> // errno, ERANGE
  11927. #include <cstdlib> // strtoull
  11928. #ifndef JSON_NO_IO
  11929. #include <iosfwd> // ostream
  11930. #endif // JSON_NO_IO
  11931. #include <limits> // max
  11932. #include <numeric> // accumulate
  11933. #include <string> // string
  11934. #include <utility> // move
  11935. #include <vector> // vector
  11936. // #include <nlohmann/detail/exceptions.hpp>
  11937. // #include <nlohmann/detail/macro_scope.hpp>
  11938. // #include <nlohmann/detail/string_concat.hpp>
  11939. // #include <nlohmann/detail/string_escape.hpp>
  11940. // #include <nlohmann/detail/value_t.hpp>
  11941. NLOHMANN_JSON_NAMESPACE_BEGIN
  11942. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  11943. /// @sa https://json.nlohmann.me/api/json_pointer/
  11944. template<typename RefStringType>
  11945. class json_pointer
  11946. {
  11947. // allow basic_json to access private members
  11948. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11949. friend class basic_json;
  11950. template<typename>
  11951. friend class json_pointer;
  11952. template<typename T>
  11953. struct string_t_helper
  11954. {
  11955. using type = T;
  11956. };
  11957. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11958. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  11959. {
  11960. using type = StringType;
  11961. };
  11962. public:
  11963. // for backwards compatibility accept BasicJsonType
  11964. using string_t = typename string_t_helper<RefStringType>::type;
  11965. /// @brief create JSON pointer
  11966. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  11967. explicit json_pointer(const string_t& s = "")
  11968. : reference_tokens(split(s))
  11969. {}
  11970. /// @brief return a string representation of the JSON pointer
  11971. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  11972. string_t to_string() const
  11973. {
  11974. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  11975. string_t{},
  11976. [](const string_t& a, const string_t& b)
  11977. {
  11978. return detail::concat(a, '/', detail::escape(b));
  11979. });
  11980. }
  11981. /// @brief return a string representation of the JSON pointer
  11982. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  11983. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  11984. operator string_t() const
  11985. {
  11986. return to_string();
  11987. }
  11988. #ifndef JSON_NO_IO
  11989. /// @brief write string representation of the JSON pointer to stream
  11990. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  11991. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  11992. {
  11993. o << ptr.to_string();
  11994. return o;
  11995. }
  11996. #endif
  11997. /// @brief append another JSON pointer at the end of this JSON pointer
  11998. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11999. json_pointer& operator/=(const json_pointer& ptr)
  12000. {
  12001. reference_tokens.insert(reference_tokens.end(),
  12002. ptr.reference_tokens.begin(),
  12003. ptr.reference_tokens.end());
  12004. return *this;
  12005. }
  12006. /// @brief append an unescaped reference token at the end of this JSON pointer
  12007. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12008. json_pointer& operator/=(string_t token)
  12009. {
  12010. push_back(std::move(token));
  12011. return *this;
  12012. }
  12013. /// @brief append an array index at the end of this JSON pointer
  12014. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12015. json_pointer& operator/=(std::size_t array_idx)
  12016. {
  12017. return *this /= std::to_string(array_idx);
  12018. }
  12019. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  12020. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12021. friend json_pointer operator/(const json_pointer& lhs,
  12022. const json_pointer& rhs)
  12023. {
  12024. return json_pointer(lhs) /= rhs;
  12025. }
  12026. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  12027. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12028. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12029. {
  12030. return json_pointer(lhs) /= std::move(token);
  12031. }
  12032. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12033. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12034. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12035. {
  12036. return json_pointer(lhs) /= array_idx;
  12037. }
  12038. /// @brief returns the parent of this JSON pointer
  12039. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12040. json_pointer parent_pointer() const
  12041. {
  12042. if (empty())
  12043. {
  12044. return *this;
  12045. }
  12046. json_pointer res = *this;
  12047. res.pop_back();
  12048. return res;
  12049. }
  12050. /// @brief remove last reference token
  12051. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12052. void pop_back()
  12053. {
  12054. if (JSON_HEDLEY_UNLIKELY(empty()))
  12055. {
  12056. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12057. }
  12058. reference_tokens.pop_back();
  12059. }
  12060. /// @brief return last reference token
  12061. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12062. const string_t& back() const
  12063. {
  12064. if (JSON_HEDLEY_UNLIKELY(empty()))
  12065. {
  12066. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12067. }
  12068. return reference_tokens.back();
  12069. }
  12070. /// @brief append an unescaped token at the end of the reference pointer
  12071. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12072. void push_back(const string_t& token)
  12073. {
  12074. reference_tokens.push_back(token);
  12075. }
  12076. /// @brief append an unescaped token at the end of the reference pointer
  12077. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12078. void push_back(string_t&& token)
  12079. {
  12080. reference_tokens.push_back(std::move(token));
  12081. }
  12082. /// @brief return whether pointer points to the root document
  12083. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12084. bool empty() const noexcept
  12085. {
  12086. return reference_tokens.empty();
  12087. }
  12088. private:
  12089. /*!
  12090. @param[in] s reference token to be converted into an array index
  12091. @return integer representation of @a s
  12092. @throw parse_error.106 if an array index begins with '0'
  12093. @throw parse_error.109 if an array index begins not with a digit
  12094. @throw out_of_range.404 if string @a s could not be converted to an integer
  12095. @throw out_of_range.410 if an array index exceeds size_type
  12096. */
  12097. template<typename BasicJsonType>
  12098. static typename BasicJsonType::size_type array_index(const string_t& s)
  12099. {
  12100. using size_type = typename BasicJsonType::size_type;
  12101. // error condition (cf. RFC 6901, Sect. 4)
  12102. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12103. {
  12104. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12105. }
  12106. // error condition (cf. RFC 6901, Sect. 4)
  12107. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12108. {
  12109. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12110. }
  12111. const char* p = s.c_str();
  12112. char* p_end = nullptr;
  12113. errno = 0; // strtoull doesn't reset errno
  12114. const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12115. if (p == p_end // invalid input or empty string
  12116. || errno == ERANGE // out of range
  12117. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12118. {
  12119. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12120. }
  12121. // only triggered on special platforms (like 32bit), see also
  12122. // https://github.com/nlohmann/json/pull/2203
  12123. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12124. {
  12125. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12126. }
  12127. return static_cast<size_type>(res);
  12128. }
  12129. JSON_PRIVATE_UNLESS_TESTED:
  12130. json_pointer top() const
  12131. {
  12132. if (JSON_HEDLEY_UNLIKELY(empty()))
  12133. {
  12134. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12135. }
  12136. json_pointer result = *this;
  12137. result.reference_tokens = {reference_tokens[0]};
  12138. return result;
  12139. }
  12140. private:
  12141. /*!
  12142. @brief create and return a reference to the pointed to value
  12143. @complexity Linear in the number of reference tokens.
  12144. @throw parse_error.109 if array index is not a number
  12145. @throw type_error.313 if value cannot be unflattened
  12146. */
  12147. template<typename BasicJsonType>
  12148. BasicJsonType& get_and_create(BasicJsonType& j) const
  12149. {
  12150. auto* result = &j;
  12151. // in case no reference tokens exist, return a reference to the JSON value
  12152. // j which will be overwritten by a primitive value
  12153. for (const auto& reference_token : reference_tokens)
  12154. {
  12155. switch (result->type())
  12156. {
  12157. case detail::value_t::null:
  12158. {
  12159. if (reference_token == "0")
  12160. {
  12161. // start a new array if reference token is 0
  12162. result = &result->operator[](0);
  12163. }
  12164. else
  12165. {
  12166. // start a new object otherwise
  12167. result = &result->operator[](reference_token);
  12168. }
  12169. break;
  12170. }
  12171. case detail::value_t::object:
  12172. {
  12173. // create an entry in the object
  12174. result = &result->operator[](reference_token);
  12175. break;
  12176. }
  12177. case detail::value_t::array:
  12178. {
  12179. // create an entry in the array
  12180. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  12181. break;
  12182. }
  12183. /*
  12184. The following code is only reached if there exists a reference
  12185. token _and_ the current value is primitive. In this case, we have
  12186. an error situation, because primitive values may only occur as
  12187. single value; that is, with an empty list of reference tokens.
  12188. */
  12189. case detail::value_t::string:
  12190. case detail::value_t::boolean:
  12191. case detail::value_t::number_integer:
  12192. case detail::value_t::number_unsigned:
  12193. case detail::value_t::number_float:
  12194. case detail::value_t::binary:
  12195. case detail::value_t::discarded:
  12196. default:
  12197. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  12198. }
  12199. }
  12200. return *result;
  12201. }
  12202. /*!
  12203. @brief return a reference to the pointed to value
  12204. @note This version does not throw if a value is not present, but tries to
  12205. create nested values instead. For instance, calling this function
  12206. with pointer `"/this/that"` on a null value is equivalent to calling
  12207. `operator[]("this").operator[]("that")` on that value, effectively
  12208. changing the null value to an object.
  12209. @param[in] ptr a JSON value
  12210. @return reference to the JSON value pointed to by the JSON pointer
  12211. @complexity Linear in the length of the JSON pointer.
  12212. @throw parse_error.106 if an array index begins with '0'
  12213. @throw parse_error.109 if an array index was not a number
  12214. @throw out_of_range.404 if the JSON pointer can not be resolved
  12215. */
  12216. template<typename BasicJsonType>
  12217. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  12218. {
  12219. for (const auto& reference_token : reference_tokens)
  12220. {
  12221. // convert null values to arrays or objects before continuing
  12222. if (ptr->is_null())
  12223. {
  12224. // check if reference token is a number
  12225. const bool nums =
  12226. std::all_of(reference_token.begin(), reference_token.end(),
  12227. [](const unsigned char x)
  12228. {
  12229. return std::isdigit(x);
  12230. });
  12231. // change value to array for numbers or "-" or to object otherwise
  12232. *ptr = (nums || reference_token == "-")
  12233. ? detail::value_t::array
  12234. : detail::value_t::object;
  12235. }
  12236. switch (ptr->type())
  12237. {
  12238. case detail::value_t::object:
  12239. {
  12240. // use unchecked object access
  12241. ptr = &ptr->operator[](reference_token);
  12242. break;
  12243. }
  12244. case detail::value_t::array:
  12245. {
  12246. if (reference_token == "-")
  12247. {
  12248. // explicitly treat "-" as index beyond the end
  12249. ptr = &ptr->operator[](ptr->m_value.array->size());
  12250. }
  12251. else
  12252. {
  12253. // convert array index to number; unchecked access
  12254. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12255. }
  12256. break;
  12257. }
  12258. case detail::value_t::null:
  12259. case detail::value_t::string:
  12260. case detail::value_t::boolean:
  12261. case detail::value_t::number_integer:
  12262. case detail::value_t::number_unsigned:
  12263. case detail::value_t::number_float:
  12264. case detail::value_t::binary:
  12265. case detail::value_t::discarded:
  12266. default:
  12267. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12268. }
  12269. }
  12270. return *ptr;
  12271. }
  12272. /*!
  12273. @throw parse_error.106 if an array index begins with '0'
  12274. @throw parse_error.109 if an array index was not a number
  12275. @throw out_of_range.402 if the array index '-' is used
  12276. @throw out_of_range.404 if the JSON pointer can not be resolved
  12277. */
  12278. template<typename BasicJsonType>
  12279. BasicJsonType& get_checked(BasicJsonType* ptr) const
  12280. {
  12281. for (const auto& reference_token : reference_tokens)
  12282. {
  12283. switch (ptr->type())
  12284. {
  12285. case detail::value_t::object:
  12286. {
  12287. // note: at performs range check
  12288. ptr = &ptr->at(reference_token);
  12289. break;
  12290. }
  12291. case detail::value_t::array:
  12292. {
  12293. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12294. {
  12295. // "-" always fails the range check
  12296. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12297. "array index '-' (", std::to_string(ptr->m_value.array->size()),
  12298. ") is out of range"), ptr));
  12299. }
  12300. // note: at performs range check
  12301. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12302. break;
  12303. }
  12304. case detail::value_t::null:
  12305. case detail::value_t::string:
  12306. case detail::value_t::boolean:
  12307. case detail::value_t::number_integer:
  12308. case detail::value_t::number_unsigned:
  12309. case detail::value_t::number_float:
  12310. case detail::value_t::binary:
  12311. case detail::value_t::discarded:
  12312. default:
  12313. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12314. }
  12315. }
  12316. return *ptr;
  12317. }
  12318. /*!
  12319. @brief return a const reference to the pointed to value
  12320. @param[in] ptr a JSON value
  12321. @return const reference to the JSON value pointed to by the JSON
  12322. pointer
  12323. @throw parse_error.106 if an array index begins with '0'
  12324. @throw parse_error.109 if an array index was not a number
  12325. @throw out_of_range.402 if the array index '-' is used
  12326. @throw out_of_range.404 if the JSON pointer can not be resolved
  12327. */
  12328. template<typename BasicJsonType>
  12329. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  12330. {
  12331. for (const auto& reference_token : reference_tokens)
  12332. {
  12333. switch (ptr->type())
  12334. {
  12335. case detail::value_t::object:
  12336. {
  12337. // use unchecked object access
  12338. ptr = &ptr->operator[](reference_token);
  12339. break;
  12340. }
  12341. case detail::value_t::array:
  12342. {
  12343. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12344. {
  12345. // "-" cannot be used for const access
  12346. JSON_THROW(detail::out_of_range::create(402, detail::concat("array index '-' (", std::to_string(ptr->m_value.array->size()), ") is out of range"), ptr));
  12347. }
  12348. // use unchecked array access
  12349. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12350. break;
  12351. }
  12352. case detail::value_t::null:
  12353. case detail::value_t::string:
  12354. case detail::value_t::boolean:
  12355. case detail::value_t::number_integer:
  12356. case detail::value_t::number_unsigned:
  12357. case detail::value_t::number_float:
  12358. case detail::value_t::binary:
  12359. case detail::value_t::discarded:
  12360. default:
  12361. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12362. }
  12363. }
  12364. return *ptr;
  12365. }
  12366. /*!
  12367. @throw parse_error.106 if an array index begins with '0'
  12368. @throw parse_error.109 if an array index was not a number
  12369. @throw out_of_range.402 if the array index '-' is used
  12370. @throw out_of_range.404 if the JSON pointer can not be resolved
  12371. */
  12372. template<typename BasicJsonType>
  12373. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  12374. {
  12375. for (const auto& reference_token : reference_tokens)
  12376. {
  12377. switch (ptr->type())
  12378. {
  12379. case detail::value_t::object:
  12380. {
  12381. // note: at performs range check
  12382. ptr = &ptr->at(reference_token);
  12383. break;
  12384. }
  12385. case detail::value_t::array:
  12386. {
  12387. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12388. {
  12389. // "-" always fails the range check
  12390. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12391. "array index '-' (", std::to_string(ptr->m_value.array->size()),
  12392. ") is out of range"), ptr));
  12393. }
  12394. // note: at performs range check
  12395. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12396. break;
  12397. }
  12398. case detail::value_t::null:
  12399. case detail::value_t::string:
  12400. case detail::value_t::boolean:
  12401. case detail::value_t::number_integer:
  12402. case detail::value_t::number_unsigned:
  12403. case detail::value_t::number_float:
  12404. case detail::value_t::binary:
  12405. case detail::value_t::discarded:
  12406. default:
  12407. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12408. }
  12409. }
  12410. return *ptr;
  12411. }
  12412. /*!
  12413. @throw parse_error.106 if an array index begins with '0'
  12414. @throw parse_error.109 if an array index was not a number
  12415. */
  12416. template<typename BasicJsonType>
  12417. bool contains(const BasicJsonType* ptr) const
  12418. {
  12419. for (const auto& reference_token : reference_tokens)
  12420. {
  12421. switch (ptr->type())
  12422. {
  12423. case detail::value_t::object:
  12424. {
  12425. if (!ptr->contains(reference_token))
  12426. {
  12427. // we did not find the key in the object
  12428. return false;
  12429. }
  12430. ptr = &ptr->operator[](reference_token);
  12431. break;
  12432. }
  12433. case detail::value_t::array:
  12434. {
  12435. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12436. {
  12437. // "-" always fails the range check
  12438. return false;
  12439. }
  12440. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  12441. {
  12442. // invalid char
  12443. return false;
  12444. }
  12445. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  12446. {
  12447. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  12448. {
  12449. // first char should be between '1' and '9'
  12450. return false;
  12451. }
  12452. for (std::size_t i = 1; i < reference_token.size(); i++)
  12453. {
  12454. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  12455. {
  12456. // other char should be between '0' and '9'
  12457. return false;
  12458. }
  12459. }
  12460. }
  12461. const auto idx = array_index<BasicJsonType>(reference_token);
  12462. if (idx >= ptr->size())
  12463. {
  12464. // index out of range
  12465. return false;
  12466. }
  12467. ptr = &ptr->operator[](idx);
  12468. break;
  12469. }
  12470. case detail::value_t::null:
  12471. case detail::value_t::string:
  12472. case detail::value_t::boolean:
  12473. case detail::value_t::number_integer:
  12474. case detail::value_t::number_unsigned:
  12475. case detail::value_t::number_float:
  12476. case detail::value_t::binary:
  12477. case detail::value_t::discarded:
  12478. default:
  12479. {
  12480. // we do not expect primitive values if there is still a
  12481. // reference token to process
  12482. return false;
  12483. }
  12484. }
  12485. }
  12486. // no reference token left means we found a primitive value
  12487. return true;
  12488. }
  12489. /*!
  12490. @brief split the string input to reference tokens
  12491. @note This function is only called by the json_pointer constructor.
  12492. All exceptions below are documented there.
  12493. @throw parse_error.107 if the pointer is not empty or begins with '/'
  12494. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  12495. */
  12496. static std::vector<string_t> split(const string_t& reference_string)
  12497. {
  12498. std::vector<string_t> result;
  12499. // special case: empty reference string -> no reference tokens
  12500. if (reference_string.empty())
  12501. {
  12502. return result;
  12503. }
  12504. // check if nonempty reference string begins with slash
  12505. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  12506. {
  12507. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  12508. }
  12509. // extract the reference tokens:
  12510. // - slash: position of the last read slash (or end of string)
  12511. // - start: position after the previous slash
  12512. for (
  12513. // search for the first slash after the first character
  12514. std::size_t slash = reference_string.find_first_of('/', 1),
  12515. // set the beginning of the first reference token
  12516. start = 1;
  12517. // we can stop if start == 0 (if slash == string_t::npos)
  12518. start != 0;
  12519. // set the beginning of the next reference token
  12520. // (will eventually be 0 if slash == string_t::npos)
  12521. start = (slash == string_t::npos) ? 0 : slash + 1,
  12522. // find next slash
  12523. slash = reference_string.find_first_of('/', start))
  12524. {
  12525. // use the text between the beginning of the reference token
  12526. // (start) and the last slash (slash).
  12527. auto reference_token = reference_string.substr(start, slash - start);
  12528. // check reference tokens are properly escaped
  12529. for (std::size_t pos = reference_token.find_first_of('~');
  12530. pos != string_t::npos;
  12531. pos = reference_token.find_first_of('~', pos + 1))
  12532. {
  12533. JSON_ASSERT(reference_token[pos] == '~');
  12534. // ~ must be followed by 0 or 1
  12535. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  12536. (reference_token[pos + 1] != '0' &&
  12537. reference_token[pos + 1] != '1')))
  12538. {
  12539. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  12540. }
  12541. }
  12542. // finally, store the reference token
  12543. detail::unescape(reference_token);
  12544. result.push_back(reference_token);
  12545. }
  12546. return result;
  12547. }
  12548. private:
  12549. /*!
  12550. @param[in] reference_string the reference string to the current value
  12551. @param[in] value the value to consider
  12552. @param[in,out] result the result object to insert values to
  12553. @note Empty objects or arrays are flattened to `null`.
  12554. */
  12555. template<typename BasicJsonType>
  12556. static void flatten(const string_t& reference_string,
  12557. const BasicJsonType& value,
  12558. BasicJsonType& result)
  12559. {
  12560. switch (value.type())
  12561. {
  12562. case detail::value_t::array:
  12563. {
  12564. if (value.m_value.array->empty())
  12565. {
  12566. // flatten empty array as null
  12567. result[reference_string] = nullptr;
  12568. }
  12569. else
  12570. {
  12571. // iterate array and use index as reference string
  12572. for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
  12573. {
  12574. flatten(detail::concat(reference_string, '/', std::to_string(i)),
  12575. value.m_value.array->operator[](i), result);
  12576. }
  12577. }
  12578. break;
  12579. }
  12580. case detail::value_t::object:
  12581. {
  12582. if (value.m_value.object->empty())
  12583. {
  12584. // flatten empty object as null
  12585. result[reference_string] = nullptr;
  12586. }
  12587. else
  12588. {
  12589. // iterate object and use keys as reference string
  12590. for (const auto& element : *value.m_value.object)
  12591. {
  12592. flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result);
  12593. }
  12594. }
  12595. break;
  12596. }
  12597. case detail::value_t::null:
  12598. case detail::value_t::string:
  12599. case detail::value_t::boolean:
  12600. case detail::value_t::number_integer:
  12601. case detail::value_t::number_unsigned:
  12602. case detail::value_t::number_float:
  12603. case detail::value_t::binary:
  12604. case detail::value_t::discarded:
  12605. default:
  12606. {
  12607. // add primitive value with its reference string
  12608. result[reference_string] = value;
  12609. break;
  12610. }
  12611. }
  12612. }
  12613. /*!
  12614. @param[in] value flattened JSON
  12615. @return unflattened JSON
  12616. @throw parse_error.109 if array index is not a number
  12617. @throw type_error.314 if value is not an object
  12618. @throw type_error.315 if object values are not primitive
  12619. @throw type_error.313 if value cannot be unflattened
  12620. */
  12621. template<typename BasicJsonType>
  12622. static BasicJsonType
  12623. unflatten(const BasicJsonType& value)
  12624. {
  12625. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  12626. {
  12627. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  12628. }
  12629. BasicJsonType result;
  12630. // iterate the JSON object values
  12631. for (const auto& element : *value.m_value.object)
  12632. {
  12633. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  12634. {
  12635. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  12636. }
  12637. // assign value to reference pointed to by JSON pointer; Note that if
  12638. // the JSON pointer is "" (i.e., points to the whole value), function
  12639. // get_and_create returns a reference to result itself. An assignment
  12640. // will then create a primitive value.
  12641. json_pointer(element.first).get_and_create(result) = element.second;
  12642. }
  12643. return result;
  12644. }
  12645. // can't use conversion operator because of ambiguity
  12646. json_pointer<string_t> convert() const&
  12647. {
  12648. json_pointer<string_t> result;
  12649. result.reference_tokens = reference_tokens;
  12650. return result;
  12651. }
  12652. json_pointer<string_t> convert()&&
  12653. {
  12654. json_pointer<string_t> result;
  12655. result.reference_tokens = std::move(reference_tokens);
  12656. return result;
  12657. }
  12658. public:
  12659. #if JSON_HAS_THREE_WAY_COMPARISON
  12660. /// @brief compares two JSON pointers for equality
  12661. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12662. template<typename RefStringTypeRhs>
  12663. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  12664. {
  12665. return reference_tokens == rhs.reference_tokens;
  12666. }
  12667. /// @brief compares JSON pointer and string for equality
  12668. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12669. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  12670. bool operator==(const string_t& rhs) const
  12671. {
  12672. return *this == json_pointer(rhs);
  12673. }
  12674. /// @brief 3-way compares two JSON pointers
  12675. template<typename RefStringTypeRhs>
  12676. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  12677. {
  12678. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  12679. }
  12680. #else
  12681. /// @brief compares two JSON pointers for equality
  12682. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12683. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12684. // NOLINTNEXTLINE(readability-redundant-declaration)
  12685. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12686. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12687. /// @brief compares JSON pointer and string for equality
  12688. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12689. template<typename RefStringTypeLhs, typename StringType>
  12690. // NOLINTNEXTLINE(readability-redundant-declaration)
  12691. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12692. const StringType& rhs);
  12693. /// @brief compares string and JSON pointer for equality
  12694. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12695. template<typename RefStringTypeRhs, typename StringType>
  12696. // NOLINTNEXTLINE(readability-redundant-declaration)
  12697. friend bool operator==(const StringType& lhs,
  12698. const json_pointer<RefStringTypeRhs>& rhs);
  12699. /// @brief compares two JSON pointers for inequality
  12700. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12701. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12702. // NOLINTNEXTLINE(readability-redundant-declaration)
  12703. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12704. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12705. /// @brief compares JSON pointer and string for inequality
  12706. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12707. template<typename RefStringTypeLhs, typename StringType>
  12708. // NOLINTNEXTLINE(readability-redundant-declaration)
  12709. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12710. const StringType& rhs);
  12711. /// @brief compares string and JSON pointer for inequality
  12712. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12713. template<typename RefStringTypeRhs, typename StringType>
  12714. // NOLINTNEXTLINE(readability-redundant-declaration)
  12715. friend bool operator!=(const StringType& lhs,
  12716. const json_pointer<RefStringTypeRhs>& rhs);
  12717. /// @brief compares two JSON pointer for less-than
  12718. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12719. // NOLINTNEXTLINE(readability-redundant-declaration)
  12720. friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12721. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12722. #endif
  12723. private:
  12724. /// the reference tokens
  12725. std::vector<string_t> reference_tokens;
  12726. };
  12727. #if !JSON_HAS_THREE_WAY_COMPARISON
  12728. // functions cannot be defined inside class due to ODR violations
  12729. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12730. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12731. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12732. {
  12733. return lhs.reference_tokens == rhs.reference_tokens;
  12734. }
  12735. template<typename RefStringTypeLhs,
  12736. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12737. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12738. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12739. const StringType& rhs)
  12740. {
  12741. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  12742. }
  12743. template<typename RefStringTypeRhs,
  12744. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12745. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12746. inline bool operator==(const StringType& lhs,
  12747. const json_pointer<RefStringTypeRhs>& rhs)
  12748. {
  12749. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  12750. }
  12751. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12752. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12753. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12754. {
  12755. return !(lhs == rhs);
  12756. }
  12757. template<typename RefStringTypeLhs,
  12758. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12759. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12760. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12761. const StringType& rhs)
  12762. {
  12763. return !(lhs == rhs);
  12764. }
  12765. template<typename RefStringTypeRhs,
  12766. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12767. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12768. inline bool operator!=(const StringType& lhs,
  12769. const json_pointer<RefStringTypeRhs>& rhs)
  12770. {
  12771. return !(lhs == rhs);
  12772. }
  12773. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12774. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12775. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12776. {
  12777. return lhs.reference_tokens < rhs.reference_tokens;
  12778. }
  12779. #endif
  12780. NLOHMANN_JSON_NAMESPACE_END
  12781. // #include <nlohmann/detail/json_ref.hpp>
  12782. // __ _____ _____ _____
  12783. // __| | __| | | | JSON for Modern C++
  12784. // | | |__ | | | | | | version 3.11.2
  12785. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12786. //
  12787. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12788. // SPDX-License-Identifier: MIT
  12789. #include <initializer_list>
  12790. #include <utility>
  12791. // #include <nlohmann/detail/abi_macros.hpp>
  12792. // #include <nlohmann/detail/meta/type_traits.hpp>
  12793. NLOHMANN_JSON_NAMESPACE_BEGIN
  12794. namespace detail
  12795. {
  12796. template<typename BasicJsonType>
  12797. class json_ref
  12798. {
  12799. public:
  12800. using value_type = BasicJsonType;
  12801. json_ref(value_type&& value)
  12802. : owned_value(std::move(value))
  12803. {}
  12804. json_ref(const value_type& value)
  12805. : value_ref(&value)
  12806. {}
  12807. json_ref(std::initializer_list<json_ref> init)
  12808. : owned_value(init)
  12809. {}
  12810. template <
  12811. class... Args,
  12812. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  12813. json_ref(Args && ... args)
  12814. : owned_value(std::forward<Args>(args)...)
  12815. {}
  12816. // class should be movable only
  12817. json_ref(json_ref&&) noexcept = default;
  12818. json_ref(const json_ref&) = delete;
  12819. json_ref& operator=(const json_ref&) = delete;
  12820. json_ref& operator=(json_ref&&) = delete;
  12821. ~json_ref() = default;
  12822. value_type moved_or_copied() const
  12823. {
  12824. if (value_ref == nullptr)
  12825. {
  12826. return std::move(owned_value);
  12827. }
  12828. return *value_ref;
  12829. }
  12830. value_type const& operator*() const
  12831. {
  12832. return value_ref ? *value_ref : owned_value;
  12833. }
  12834. value_type const* operator->() const
  12835. {
  12836. return &** this;
  12837. }
  12838. private:
  12839. mutable value_type owned_value = nullptr;
  12840. value_type const* value_ref = nullptr;
  12841. };
  12842. } // namespace detail
  12843. NLOHMANN_JSON_NAMESPACE_END
  12844. // #include <nlohmann/detail/macro_scope.hpp>
  12845. // #include <nlohmann/detail/string_concat.hpp>
  12846. // #include <nlohmann/detail/string_escape.hpp>
  12847. // #include <nlohmann/detail/meta/cpp_future.hpp>
  12848. // #include <nlohmann/detail/meta/type_traits.hpp>
  12849. // #include <nlohmann/detail/output/binary_writer.hpp>
  12850. // __ _____ _____ _____
  12851. // __| | __| | | | JSON for Modern C++
  12852. // | | |__ | | | | | | version 3.11.2
  12853. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12854. //
  12855. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12856. // SPDX-License-Identifier: MIT
  12857. #include <algorithm> // reverse
  12858. #include <array> // array
  12859. #include <map> // map
  12860. #include <cmath> // isnan, isinf
  12861. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  12862. #include <cstring> // memcpy
  12863. #include <limits> // numeric_limits
  12864. #include <string> // string
  12865. #include <utility> // move
  12866. #include <vector> // vector
  12867. // #include <nlohmann/detail/input/binary_reader.hpp>
  12868. // #include <nlohmann/detail/macro_scope.hpp>
  12869. // #include <nlohmann/detail/output/output_adapters.hpp>
  12870. // __ _____ _____ _____
  12871. // __| | __| | | | JSON for Modern C++
  12872. // | | |__ | | | | | | version 3.11.2
  12873. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12874. //
  12875. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12876. // SPDX-License-Identifier: MIT
  12877. #include <algorithm> // copy
  12878. #include <cstddef> // size_t
  12879. #include <iterator> // back_inserter
  12880. #include <memory> // shared_ptr, make_shared
  12881. #include <string> // basic_string
  12882. #include <vector> // vector
  12883. #ifndef JSON_NO_IO
  12884. #include <ios> // streamsize
  12885. #include <ostream> // basic_ostream
  12886. #endif // JSON_NO_IO
  12887. // #include <nlohmann/detail/macro_scope.hpp>
  12888. NLOHMANN_JSON_NAMESPACE_BEGIN
  12889. namespace detail
  12890. {
  12891. /// abstract output adapter interface
  12892. template<typename CharType> struct output_adapter_protocol
  12893. {
  12894. virtual void write_character(CharType c) = 0;
  12895. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  12896. virtual ~output_adapter_protocol() = default;
  12897. output_adapter_protocol() = default;
  12898. output_adapter_protocol(const output_adapter_protocol&) = default;
  12899. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  12900. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  12901. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  12902. };
  12903. /// a type to simplify interfaces
  12904. template<typename CharType>
  12905. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  12906. /// output adapter for byte vectors
  12907. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  12908. class output_vector_adapter : public output_adapter_protocol<CharType>
  12909. {
  12910. public:
  12911. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  12912. : v(vec)
  12913. {}
  12914. void write_character(CharType c) override
  12915. {
  12916. v.push_back(c);
  12917. }
  12918. JSON_HEDLEY_NON_NULL(2)
  12919. void write_characters(const CharType* s, std::size_t length) override
  12920. {
  12921. v.insert(v.end(), s, s + length);
  12922. }
  12923. private:
  12924. std::vector<CharType, AllocatorType>& v;
  12925. };
  12926. #ifndef JSON_NO_IO
  12927. /// output adapter for output streams
  12928. template<typename CharType>
  12929. class output_stream_adapter : public output_adapter_protocol<CharType>
  12930. {
  12931. public:
  12932. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  12933. : stream(s)
  12934. {}
  12935. void write_character(CharType c) override
  12936. {
  12937. stream.put(c);
  12938. }
  12939. JSON_HEDLEY_NON_NULL(2)
  12940. void write_characters(const CharType* s, std::size_t length) override
  12941. {
  12942. stream.write(s, static_cast<std::streamsize>(length));
  12943. }
  12944. private:
  12945. std::basic_ostream<CharType>& stream;
  12946. };
  12947. #endif // JSON_NO_IO
  12948. /// output adapter for basic_string
  12949. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12950. class output_string_adapter : public output_adapter_protocol<CharType>
  12951. {
  12952. public:
  12953. explicit output_string_adapter(StringType& s) noexcept
  12954. : str(s)
  12955. {}
  12956. void write_character(CharType c) override
  12957. {
  12958. str.push_back(c);
  12959. }
  12960. JSON_HEDLEY_NON_NULL(2)
  12961. void write_characters(const CharType* s, std::size_t length) override
  12962. {
  12963. str.append(s, length);
  12964. }
  12965. private:
  12966. StringType& str;
  12967. };
  12968. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12969. class output_adapter
  12970. {
  12971. public:
  12972. template<typename AllocatorType = std::allocator<CharType>>
  12973. output_adapter(std::vector<CharType, AllocatorType>& vec)
  12974. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  12975. #ifndef JSON_NO_IO
  12976. output_adapter(std::basic_ostream<CharType>& s)
  12977. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  12978. #endif // JSON_NO_IO
  12979. output_adapter(StringType& s)
  12980. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  12981. operator output_adapter_t<CharType>()
  12982. {
  12983. return oa;
  12984. }
  12985. private:
  12986. output_adapter_t<CharType> oa = nullptr;
  12987. };
  12988. } // namespace detail
  12989. NLOHMANN_JSON_NAMESPACE_END
  12990. // #include <nlohmann/detail/string_concat.hpp>
  12991. NLOHMANN_JSON_NAMESPACE_BEGIN
  12992. namespace detail
  12993. {
  12994. ///////////////////
  12995. // binary writer //
  12996. ///////////////////
  12997. /*!
  12998. @brief serialization to CBOR and MessagePack values
  12999. */
  13000. template<typename BasicJsonType, typename CharType>
  13001. class binary_writer
  13002. {
  13003. using string_t = typename BasicJsonType::string_t;
  13004. using binary_t = typename BasicJsonType::binary_t;
  13005. using number_float_t = typename BasicJsonType::number_float_t;
  13006. public:
  13007. /*!
  13008. @brief create a binary writer
  13009. @param[in] adapter output adapter to write to
  13010. */
  13011. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  13012. {
  13013. JSON_ASSERT(oa);
  13014. }
  13015. /*!
  13016. @param[in] j JSON value to serialize
  13017. @pre j.type() == value_t::object
  13018. */
  13019. void write_bson(const BasicJsonType& j)
  13020. {
  13021. switch (j.type())
  13022. {
  13023. case value_t::object:
  13024. {
  13025. write_bson_object(*j.m_value.object);
  13026. break;
  13027. }
  13028. case value_t::null:
  13029. case value_t::array:
  13030. case value_t::string:
  13031. case value_t::boolean:
  13032. case value_t::number_integer:
  13033. case value_t::number_unsigned:
  13034. case value_t::number_float:
  13035. case value_t::binary:
  13036. case value_t::discarded:
  13037. default:
  13038. {
  13039. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13040. }
  13041. }
  13042. }
  13043. /*!
  13044. @param[in] j JSON value to serialize
  13045. */
  13046. void write_cbor(const BasicJsonType& j)
  13047. {
  13048. switch (j.type())
  13049. {
  13050. case value_t::null:
  13051. {
  13052. oa->write_character(to_char_type(0xF6));
  13053. break;
  13054. }
  13055. case value_t::boolean:
  13056. {
  13057. oa->write_character(j.m_value.boolean
  13058. ? to_char_type(0xF5)
  13059. : to_char_type(0xF4));
  13060. break;
  13061. }
  13062. case value_t::number_integer:
  13063. {
  13064. if (j.m_value.number_integer >= 0)
  13065. {
  13066. // CBOR does not differentiate between positive signed
  13067. // integers and unsigned integers. Therefore, we used the
  13068. // code from the value_t::number_unsigned case here.
  13069. if (j.m_value.number_integer <= 0x17)
  13070. {
  13071. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13072. }
  13073. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13074. {
  13075. oa->write_character(to_char_type(0x18));
  13076. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13077. }
  13078. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13079. {
  13080. oa->write_character(to_char_type(0x19));
  13081. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  13082. }
  13083. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13084. {
  13085. oa->write_character(to_char_type(0x1A));
  13086. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  13087. }
  13088. else
  13089. {
  13090. oa->write_character(to_char_type(0x1B));
  13091. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  13092. }
  13093. }
  13094. else
  13095. {
  13096. // The conversions below encode the sign in the first
  13097. // byte, and the value is converted to a positive number.
  13098. const auto positive_number = -1 - j.m_value.number_integer;
  13099. if (j.m_value.number_integer >= -24)
  13100. {
  13101. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13102. }
  13103. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13104. {
  13105. oa->write_character(to_char_type(0x38));
  13106. write_number(static_cast<std::uint8_t>(positive_number));
  13107. }
  13108. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13109. {
  13110. oa->write_character(to_char_type(0x39));
  13111. write_number(static_cast<std::uint16_t>(positive_number));
  13112. }
  13113. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13114. {
  13115. oa->write_character(to_char_type(0x3A));
  13116. write_number(static_cast<std::uint32_t>(positive_number));
  13117. }
  13118. else
  13119. {
  13120. oa->write_character(to_char_type(0x3B));
  13121. write_number(static_cast<std::uint64_t>(positive_number));
  13122. }
  13123. }
  13124. break;
  13125. }
  13126. case value_t::number_unsigned:
  13127. {
  13128. if (j.m_value.number_unsigned <= 0x17)
  13129. {
  13130. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  13131. }
  13132. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13133. {
  13134. oa->write_character(to_char_type(0x18));
  13135. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  13136. }
  13137. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13138. {
  13139. oa->write_character(to_char_type(0x19));
  13140. write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
  13141. }
  13142. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13143. {
  13144. oa->write_character(to_char_type(0x1A));
  13145. write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
  13146. }
  13147. else
  13148. {
  13149. oa->write_character(to_char_type(0x1B));
  13150. write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
  13151. }
  13152. break;
  13153. }
  13154. case value_t::number_float:
  13155. {
  13156. if (std::isnan(j.m_value.number_float))
  13157. {
  13158. // NaN is 0xf97e00 in CBOR
  13159. oa->write_character(to_char_type(0xF9));
  13160. oa->write_character(to_char_type(0x7E));
  13161. oa->write_character(to_char_type(0x00));
  13162. }
  13163. else if (std::isinf(j.m_value.number_float))
  13164. {
  13165. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  13166. oa->write_character(to_char_type(0xf9));
  13167. oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  13168. oa->write_character(to_char_type(0x00));
  13169. }
  13170. else
  13171. {
  13172. write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
  13173. }
  13174. break;
  13175. }
  13176. case value_t::string:
  13177. {
  13178. // step 1: write control byte and the string length
  13179. const auto N = j.m_value.string->size();
  13180. if (N <= 0x17)
  13181. {
  13182. write_number(static_cast<std::uint8_t>(0x60 + N));
  13183. }
  13184. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13185. {
  13186. oa->write_character(to_char_type(0x78));
  13187. write_number(static_cast<std::uint8_t>(N));
  13188. }
  13189. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13190. {
  13191. oa->write_character(to_char_type(0x79));
  13192. write_number(static_cast<std::uint16_t>(N));
  13193. }
  13194. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13195. {
  13196. oa->write_character(to_char_type(0x7A));
  13197. write_number(static_cast<std::uint32_t>(N));
  13198. }
  13199. // LCOV_EXCL_START
  13200. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13201. {
  13202. oa->write_character(to_char_type(0x7B));
  13203. write_number(static_cast<std::uint64_t>(N));
  13204. }
  13205. // LCOV_EXCL_STOP
  13206. // step 2: write the string
  13207. oa->write_characters(
  13208. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13209. j.m_value.string->size());
  13210. break;
  13211. }
  13212. case value_t::array:
  13213. {
  13214. // step 1: write control byte and the array size
  13215. const auto N = j.m_value.array->size();
  13216. if (N <= 0x17)
  13217. {
  13218. write_number(static_cast<std::uint8_t>(0x80 + N));
  13219. }
  13220. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13221. {
  13222. oa->write_character(to_char_type(0x98));
  13223. write_number(static_cast<std::uint8_t>(N));
  13224. }
  13225. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13226. {
  13227. oa->write_character(to_char_type(0x99));
  13228. write_number(static_cast<std::uint16_t>(N));
  13229. }
  13230. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13231. {
  13232. oa->write_character(to_char_type(0x9A));
  13233. write_number(static_cast<std::uint32_t>(N));
  13234. }
  13235. // LCOV_EXCL_START
  13236. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13237. {
  13238. oa->write_character(to_char_type(0x9B));
  13239. write_number(static_cast<std::uint64_t>(N));
  13240. }
  13241. // LCOV_EXCL_STOP
  13242. // step 2: write each element
  13243. for (const auto& el : *j.m_value.array)
  13244. {
  13245. write_cbor(el);
  13246. }
  13247. break;
  13248. }
  13249. case value_t::binary:
  13250. {
  13251. if (j.m_value.binary->has_subtype())
  13252. {
  13253. if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  13254. {
  13255. write_number(static_cast<std::uint8_t>(0xd8));
  13256. write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
  13257. }
  13258. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  13259. {
  13260. write_number(static_cast<std::uint8_t>(0xd9));
  13261. write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
  13262. }
  13263. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  13264. {
  13265. write_number(static_cast<std::uint8_t>(0xda));
  13266. write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
  13267. }
  13268. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  13269. {
  13270. write_number(static_cast<std::uint8_t>(0xdb));
  13271. write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
  13272. }
  13273. }
  13274. // step 1: write control byte and the binary array size
  13275. const auto N = j.m_value.binary->size();
  13276. if (N <= 0x17)
  13277. {
  13278. write_number(static_cast<std::uint8_t>(0x40 + N));
  13279. }
  13280. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13281. {
  13282. oa->write_character(to_char_type(0x58));
  13283. write_number(static_cast<std::uint8_t>(N));
  13284. }
  13285. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13286. {
  13287. oa->write_character(to_char_type(0x59));
  13288. write_number(static_cast<std::uint16_t>(N));
  13289. }
  13290. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13291. {
  13292. oa->write_character(to_char_type(0x5A));
  13293. write_number(static_cast<std::uint32_t>(N));
  13294. }
  13295. // LCOV_EXCL_START
  13296. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13297. {
  13298. oa->write_character(to_char_type(0x5B));
  13299. write_number(static_cast<std::uint64_t>(N));
  13300. }
  13301. // LCOV_EXCL_STOP
  13302. // step 2: write each element
  13303. oa->write_characters(
  13304. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13305. N);
  13306. break;
  13307. }
  13308. case value_t::object:
  13309. {
  13310. // step 1: write control byte and the object size
  13311. const auto N = j.m_value.object->size();
  13312. if (N <= 0x17)
  13313. {
  13314. write_number(static_cast<std::uint8_t>(0xA0 + N));
  13315. }
  13316. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13317. {
  13318. oa->write_character(to_char_type(0xB8));
  13319. write_number(static_cast<std::uint8_t>(N));
  13320. }
  13321. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13322. {
  13323. oa->write_character(to_char_type(0xB9));
  13324. write_number(static_cast<std::uint16_t>(N));
  13325. }
  13326. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13327. {
  13328. oa->write_character(to_char_type(0xBA));
  13329. write_number(static_cast<std::uint32_t>(N));
  13330. }
  13331. // LCOV_EXCL_START
  13332. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13333. {
  13334. oa->write_character(to_char_type(0xBB));
  13335. write_number(static_cast<std::uint64_t>(N));
  13336. }
  13337. // LCOV_EXCL_STOP
  13338. // step 2: write each element
  13339. for (const auto& el : *j.m_value.object)
  13340. {
  13341. write_cbor(el.first);
  13342. write_cbor(el.second);
  13343. }
  13344. break;
  13345. }
  13346. case value_t::discarded:
  13347. default:
  13348. break;
  13349. }
  13350. }
  13351. /*!
  13352. @param[in] j JSON value to serialize
  13353. */
  13354. void write_msgpack(const BasicJsonType& j)
  13355. {
  13356. switch (j.type())
  13357. {
  13358. case value_t::null: // nil
  13359. {
  13360. oa->write_character(to_char_type(0xC0));
  13361. break;
  13362. }
  13363. case value_t::boolean: // true and false
  13364. {
  13365. oa->write_character(j.m_value.boolean
  13366. ? to_char_type(0xC3)
  13367. : to_char_type(0xC2));
  13368. break;
  13369. }
  13370. case value_t::number_integer:
  13371. {
  13372. if (j.m_value.number_integer >= 0)
  13373. {
  13374. // MessagePack does not differentiate between positive
  13375. // signed integers and unsigned integers. Therefore, we used
  13376. // the code from the value_t::number_unsigned case here.
  13377. if (j.m_value.number_unsigned < 128)
  13378. {
  13379. // positive fixnum
  13380. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13381. }
  13382. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13383. {
  13384. // uint 8
  13385. oa->write_character(to_char_type(0xCC));
  13386. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13387. }
  13388. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13389. {
  13390. // uint 16
  13391. oa->write_character(to_char_type(0xCD));
  13392. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  13393. }
  13394. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13395. {
  13396. // uint 32
  13397. oa->write_character(to_char_type(0xCE));
  13398. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  13399. }
  13400. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13401. {
  13402. // uint 64
  13403. oa->write_character(to_char_type(0xCF));
  13404. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  13405. }
  13406. }
  13407. else
  13408. {
  13409. if (j.m_value.number_integer >= -32)
  13410. {
  13411. // negative fixnum
  13412. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  13413. }
  13414. else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  13415. j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  13416. {
  13417. // int 8
  13418. oa->write_character(to_char_type(0xD0));
  13419. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  13420. }
  13421. else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  13422. j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  13423. {
  13424. // int 16
  13425. oa->write_character(to_char_type(0xD1));
  13426. write_number(static_cast<std::int16_t>(j.m_value.number_integer));
  13427. }
  13428. else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  13429. j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  13430. {
  13431. // int 32
  13432. oa->write_character(to_char_type(0xD2));
  13433. write_number(static_cast<std::int32_t>(j.m_value.number_integer));
  13434. }
  13435. else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  13436. j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  13437. {
  13438. // int 64
  13439. oa->write_character(to_char_type(0xD3));
  13440. write_number(static_cast<std::int64_t>(j.m_value.number_integer));
  13441. }
  13442. }
  13443. break;
  13444. }
  13445. case value_t::number_unsigned:
  13446. {
  13447. if (j.m_value.number_unsigned < 128)
  13448. {
  13449. // positive fixnum
  13450. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13451. }
  13452. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13453. {
  13454. // uint 8
  13455. oa->write_character(to_char_type(0xCC));
  13456. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13457. }
  13458. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13459. {
  13460. // uint 16
  13461. oa->write_character(to_char_type(0xCD));
  13462. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  13463. }
  13464. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13465. {
  13466. // uint 32
  13467. oa->write_character(to_char_type(0xCE));
  13468. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  13469. }
  13470. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13471. {
  13472. // uint 64
  13473. oa->write_character(to_char_type(0xCF));
  13474. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  13475. }
  13476. break;
  13477. }
  13478. case value_t::number_float:
  13479. {
  13480. write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
  13481. break;
  13482. }
  13483. case value_t::string:
  13484. {
  13485. // step 1: write control byte and the string length
  13486. const auto N = j.m_value.string->size();
  13487. if (N <= 31)
  13488. {
  13489. // fixstr
  13490. write_number(static_cast<std::uint8_t>(0xA0 | N));
  13491. }
  13492. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13493. {
  13494. // str 8
  13495. oa->write_character(to_char_type(0xD9));
  13496. write_number(static_cast<std::uint8_t>(N));
  13497. }
  13498. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13499. {
  13500. // str 16
  13501. oa->write_character(to_char_type(0xDA));
  13502. write_number(static_cast<std::uint16_t>(N));
  13503. }
  13504. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13505. {
  13506. // str 32
  13507. oa->write_character(to_char_type(0xDB));
  13508. write_number(static_cast<std::uint32_t>(N));
  13509. }
  13510. // step 2: write the string
  13511. oa->write_characters(
  13512. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13513. j.m_value.string->size());
  13514. break;
  13515. }
  13516. case value_t::array:
  13517. {
  13518. // step 1: write control byte and the array size
  13519. const auto N = j.m_value.array->size();
  13520. if (N <= 15)
  13521. {
  13522. // fixarray
  13523. write_number(static_cast<std::uint8_t>(0x90 | N));
  13524. }
  13525. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13526. {
  13527. // array 16
  13528. oa->write_character(to_char_type(0xDC));
  13529. write_number(static_cast<std::uint16_t>(N));
  13530. }
  13531. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13532. {
  13533. // array 32
  13534. oa->write_character(to_char_type(0xDD));
  13535. write_number(static_cast<std::uint32_t>(N));
  13536. }
  13537. // step 2: write each element
  13538. for (const auto& el : *j.m_value.array)
  13539. {
  13540. write_msgpack(el);
  13541. }
  13542. break;
  13543. }
  13544. case value_t::binary:
  13545. {
  13546. // step 0: determine if the binary type has a set subtype to
  13547. // determine whether or not to use the ext or fixext types
  13548. const bool use_ext = j.m_value.binary->has_subtype();
  13549. // step 1: write control byte and the byte string length
  13550. const auto N = j.m_value.binary->size();
  13551. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13552. {
  13553. std::uint8_t output_type{};
  13554. bool fixed = true;
  13555. if (use_ext)
  13556. {
  13557. switch (N)
  13558. {
  13559. case 1:
  13560. output_type = 0xD4; // fixext 1
  13561. break;
  13562. case 2:
  13563. output_type = 0xD5; // fixext 2
  13564. break;
  13565. case 4:
  13566. output_type = 0xD6; // fixext 4
  13567. break;
  13568. case 8:
  13569. output_type = 0xD7; // fixext 8
  13570. break;
  13571. case 16:
  13572. output_type = 0xD8; // fixext 16
  13573. break;
  13574. default:
  13575. output_type = 0xC7; // ext 8
  13576. fixed = false;
  13577. break;
  13578. }
  13579. }
  13580. else
  13581. {
  13582. output_type = 0xC4; // bin 8
  13583. fixed = false;
  13584. }
  13585. oa->write_character(to_char_type(output_type));
  13586. if (!fixed)
  13587. {
  13588. write_number(static_cast<std::uint8_t>(N));
  13589. }
  13590. }
  13591. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13592. {
  13593. const std::uint8_t output_type = use_ext
  13594. ? 0xC8 // ext 16
  13595. : 0xC5; // bin 16
  13596. oa->write_character(to_char_type(output_type));
  13597. write_number(static_cast<std::uint16_t>(N));
  13598. }
  13599. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13600. {
  13601. const std::uint8_t output_type = use_ext
  13602. ? 0xC9 // ext 32
  13603. : 0xC6; // bin 32
  13604. oa->write_character(to_char_type(output_type));
  13605. write_number(static_cast<std::uint32_t>(N));
  13606. }
  13607. // step 1.5: if this is an ext type, write the subtype
  13608. if (use_ext)
  13609. {
  13610. write_number(static_cast<std::int8_t>(j.m_value.binary->subtype()));
  13611. }
  13612. // step 2: write the byte string
  13613. oa->write_characters(
  13614. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13615. N);
  13616. break;
  13617. }
  13618. case value_t::object:
  13619. {
  13620. // step 1: write control byte and the object size
  13621. const auto N = j.m_value.object->size();
  13622. if (N <= 15)
  13623. {
  13624. // fixmap
  13625. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  13626. }
  13627. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13628. {
  13629. // map 16
  13630. oa->write_character(to_char_type(0xDE));
  13631. write_number(static_cast<std::uint16_t>(N));
  13632. }
  13633. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13634. {
  13635. // map 32
  13636. oa->write_character(to_char_type(0xDF));
  13637. write_number(static_cast<std::uint32_t>(N));
  13638. }
  13639. // step 2: write each element
  13640. for (const auto& el : *j.m_value.object)
  13641. {
  13642. write_msgpack(el.first);
  13643. write_msgpack(el.second);
  13644. }
  13645. break;
  13646. }
  13647. case value_t::discarded:
  13648. default:
  13649. break;
  13650. }
  13651. }
  13652. /*!
  13653. @param[in] j JSON value to serialize
  13654. @param[in] use_count whether to use '#' prefixes (optimized format)
  13655. @param[in] use_type whether to use '$' prefixes (optimized format)
  13656. @param[in] add_prefix whether prefixes need to be used for this value
  13657. @param[in] use_bjdata whether write in BJData format, default is false
  13658. */
  13659. void write_ubjson(const BasicJsonType& j, const bool use_count,
  13660. const bool use_type, const bool add_prefix = true,
  13661. const bool use_bjdata = false)
  13662. {
  13663. switch (j.type())
  13664. {
  13665. case value_t::null:
  13666. {
  13667. if (add_prefix)
  13668. {
  13669. oa->write_character(to_char_type('Z'));
  13670. }
  13671. break;
  13672. }
  13673. case value_t::boolean:
  13674. {
  13675. if (add_prefix)
  13676. {
  13677. oa->write_character(j.m_value.boolean
  13678. ? to_char_type('T')
  13679. : to_char_type('F'));
  13680. }
  13681. break;
  13682. }
  13683. case value_t::number_integer:
  13684. {
  13685. write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix, use_bjdata);
  13686. break;
  13687. }
  13688. case value_t::number_unsigned:
  13689. {
  13690. write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix, use_bjdata);
  13691. break;
  13692. }
  13693. case value_t::number_float:
  13694. {
  13695. write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix, use_bjdata);
  13696. break;
  13697. }
  13698. case value_t::string:
  13699. {
  13700. if (add_prefix)
  13701. {
  13702. oa->write_character(to_char_type('S'));
  13703. }
  13704. write_number_with_ubjson_prefix(j.m_value.string->size(), true, use_bjdata);
  13705. oa->write_characters(
  13706. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13707. j.m_value.string->size());
  13708. break;
  13709. }
  13710. case value_t::array:
  13711. {
  13712. if (add_prefix)
  13713. {
  13714. oa->write_character(to_char_type('['));
  13715. }
  13716. bool prefix_required = true;
  13717. if (use_type && !j.m_value.array->empty())
  13718. {
  13719. JSON_ASSERT(use_count);
  13720. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13721. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  13722. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13723. {
  13724. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13725. });
  13726. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13727. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13728. {
  13729. prefix_required = false;
  13730. oa->write_character(to_char_type('$'));
  13731. oa->write_character(first_prefix);
  13732. }
  13733. }
  13734. if (use_count)
  13735. {
  13736. oa->write_character(to_char_type('#'));
  13737. write_number_with_ubjson_prefix(j.m_value.array->size(), true, use_bjdata);
  13738. }
  13739. for (const auto& el : *j.m_value.array)
  13740. {
  13741. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  13742. }
  13743. if (!use_count)
  13744. {
  13745. oa->write_character(to_char_type(']'));
  13746. }
  13747. break;
  13748. }
  13749. case value_t::binary:
  13750. {
  13751. if (add_prefix)
  13752. {
  13753. oa->write_character(to_char_type('['));
  13754. }
  13755. if (use_type && !j.m_value.binary->empty())
  13756. {
  13757. JSON_ASSERT(use_count);
  13758. oa->write_character(to_char_type('$'));
  13759. oa->write_character('U');
  13760. }
  13761. if (use_count)
  13762. {
  13763. oa->write_character(to_char_type('#'));
  13764. write_number_with_ubjson_prefix(j.m_value.binary->size(), true, use_bjdata);
  13765. }
  13766. if (use_type)
  13767. {
  13768. oa->write_characters(
  13769. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13770. j.m_value.binary->size());
  13771. }
  13772. else
  13773. {
  13774. for (size_t i = 0; i < j.m_value.binary->size(); ++i)
  13775. {
  13776. oa->write_character(to_char_type('U'));
  13777. oa->write_character(j.m_value.binary->data()[i]);
  13778. }
  13779. }
  13780. if (!use_count)
  13781. {
  13782. oa->write_character(to_char_type(']'));
  13783. }
  13784. break;
  13785. }
  13786. case value_t::object:
  13787. {
  13788. if (use_bjdata && j.m_value.object->size() == 3 && j.m_value.object->find("_ArrayType_") != j.m_value.object->end() && j.m_value.object->find("_ArraySize_") != j.m_value.object->end() && j.m_value.object->find("_ArrayData_") != j.m_value.object->end())
  13789. {
  13790. if (!write_bjdata_ndarray(*j.m_value.object, use_count, use_type)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  13791. {
  13792. break;
  13793. }
  13794. }
  13795. if (add_prefix)
  13796. {
  13797. oa->write_character(to_char_type('{'));
  13798. }
  13799. bool prefix_required = true;
  13800. if (use_type && !j.m_value.object->empty())
  13801. {
  13802. JSON_ASSERT(use_count);
  13803. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13804. const bool same_prefix = std::all_of(j.begin(), j.end(),
  13805. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13806. {
  13807. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13808. });
  13809. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13810. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13811. {
  13812. prefix_required = false;
  13813. oa->write_character(to_char_type('$'));
  13814. oa->write_character(first_prefix);
  13815. }
  13816. }
  13817. if (use_count)
  13818. {
  13819. oa->write_character(to_char_type('#'));
  13820. write_number_with_ubjson_prefix(j.m_value.object->size(), true, use_bjdata);
  13821. }
  13822. for (const auto& el : *j.m_value.object)
  13823. {
  13824. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  13825. oa->write_characters(
  13826. reinterpret_cast<const CharType*>(el.first.c_str()),
  13827. el.first.size());
  13828. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  13829. }
  13830. if (!use_count)
  13831. {
  13832. oa->write_character(to_char_type('}'));
  13833. }
  13834. break;
  13835. }
  13836. case value_t::discarded:
  13837. default:
  13838. break;
  13839. }
  13840. }
  13841. private:
  13842. //////////
  13843. // BSON //
  13844. //////////
  13845. /*!
  13846. @return The size of a BSON document entry header, including the id marker
  13847. and the entry name size (and its null-terminator).
  13848. */
  13849. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  13850. {
  13851. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  13852. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  13853. {
  13854. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  13855. static_cast<void>(j);
  13856. }
  13857. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  13858. }
  13859. /*!
  13860. @brief Writes the given @a element_type and @a name to the output adapter
  13861. */
  13862. void write_bson_entry_header(const string_t& name,
  13863. const std::uint8_t element_type)
  13864. {
  13865. oa->write_character(to_char_type(element_type)); // boolean
  13866. oa->write_characters(
  13867. reinterpret_cast<const CharType*>(name.c_str()),
  13868. name.size() + 1u);
  13869. }
  13870. /*!
  13871. @brief Writes a BSON element with key @a name and boolean value @a value
  13872. */
  13873. void write_bson_boolean(const string_t& name,
  13874. const bool value)
  13875. {
  13876. write_bson_entry_header(name, 0x08);
  13877. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  13878. }
  13879. /*!
  13880. @brief Writes a BSON element with key @a name and double value @a value
  13881. */
  13882. void write_bson_double(const string_t& name,
  13883. const double value)
  13884. {
  13885. write_bson_entry_header(name, 0x01);
  13886. write_number<double>(value, true);
  13887. }
  13888. /*!
  13889. @return The size of the BSON-encoded string in @a value
  13890. */
  13891. static std::size_t calc_bson_string_size(const string_t& value)
  13892. {
  13893. return sizeof(std::int32_t) + value.size() + 1ul;
  13894. }
  13895. /*!
  13896. @brief Writes a BSON element with key @a name and string value @a value
  13897. */
  13898. void write_bson_string(const string_t& name,
  13899. const string_t& value)
  13900. {
  13901. write_bson_entry_header(name, 0x02);
  13902. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  13903. oa->write_characters(
  13904. reinterpret_cast<const CharType*>(value.c_str()),
  13905. value.size() + 1);
  13906. }
  13907. /*!
  13908. @brief Writes a BSON element with key @a name and null value
  13909. */
  13910. void write_bson_null(const string_t& name)
  13911. {
  13912. write_bson_entry_header(name, 0x0A);
  13913. }
  13914. /*!
  13915. @return The size of the BSON-encoded integer @a value
  13916. */
  13917. static std::size_t calc_bson_integer_size(const std::int64_t value)
  13918. {
  13919. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  13920. ? sizeof(std::int32_t)
  13921. : sizeof(std::int64_t);
  13922. }
  13923. /*!
  13924. @brief Writes a BSON element with key @a name and integer @a value
  13925. */
  13926. void write_bson_integer(const string_t& name,
  13927. const std::int64_t value)
  13928. {
  13929. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  13930. {
  13931. write_bson_entry_header(name, 0x10); // int32
  13932. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  13933. }
  13934. else
  13935. {
  13936. write_bson_entry_header(name, 0x12); // int64
  13937. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  13938. }
  13939. }
  13940. /*!
  13941. @return The size of the BSON-encoded unsigned integer in @a j
  13942. */
  13943. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  13944. {
  13945. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13946. ? sizeof(std::int32_t)
  13947. : sizeof(std::int64_t);
  13948. }
  13949. /*!
  13950. @brief Writes a BSON element with key @a name and unsigned @a value
  13951. */
  13952. void write_bson_unsigned(const string_t& name,
  13953. const BasicJsonType& j)
  13954. {
  13955. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13956. {
  13957. write_bson_entry_header(name, 0x10 /* int32 */);
  13958. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_value.number_unsigned), true);
  13959. }
  13960. else if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13961. {
  13962. write_bson_entry_header(name, 0x12 /* int64 */);
  13963. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_value.number_unsigned), true);
  13964. }
  13965. else
  13966. {
  13967. JSON_THROW(out_of_range::create(407, concat("integer number ", std::to_string(j.m_value.number_unsigned), " cannot be represented by BSON as it does not fit int64"), &j));
  13968. }
  13969. }
  13970. /*!
  13971. @brief Writes a BSON element with key @a name and object @a value
  13972. */
  13973. void write_bson_object_entry(const string_t& name,
  13974. const typename BasicJsonType::object_t& value)
  13975. {
  13976. write_bson_entry_header(name, 0x03); // object
  13977. write_bson_object(value);
  13978. }
  13979. /*!
  13980. @return The size of the BSON-encoded array @a value
  13981. */
  13982. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  13983. {
  13984. std::size_t array_index = 0ul;
  13985. 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)
  13986. {
  13987. return result + calc_bson_element_size(std::to_string(array_index++), el);
  13988. });
  13989. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  13990. }
  13991. /*!
  13992. @return The size of the BSON-encoded binary array @a value
  13993. */
  13994. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  13995. {
  13996. return sizeof(std::int32_t) + value.size() + 1ul;
  13997. }
  13998. /*!
  13999. @brief Writes a BSON element with key @a name and array @a value
  14000. */
  14001. void write_bson_array(const string_t& name,
  14002. const typename BasicJsonType::array_t& value)
  14003. {
  14004. write_bson_entry_header(name, 0x04); // array
  14005. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  14006. std::size_t array_index = 0ul;
  14007. for (const auto& el : value)
  14008. {
  14009. write_bson_element(std::to_string(array_index++), el);
  14010. }
  14011. oa->write_character(to_char_type(0x00));
  14012. }
  14013. /*!
  14014. @brief Writes a BSON element with key @a name and binary value @a value
  14015. */
  14016. void write_bson_binary(const string_t& name,
  14017. const binary_t& value)
  14018. {
  14019. write_bson_entry_header(name, 0x05);
  14020. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  14021. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  14022. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  14023. }
  14024. /*!
  14025. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  14026. @return The calculated size for the BSON document entry for @a j with the given @a name.
  14027. */
  14028. static std::size_t calc_bson_element_size(const string_t& name,
  14029. const BasicJsonType& j)
  14030. {
  14031. const auto header_size = calc_bson_entry_header_size(name, j);
  14032. switch (j.type())
  14033. {
  14034. case value_t::object:
  14035. return header_size + calc_bson_object_size(*j.m_value.object);
  14036. case value_t::array:
  14037. return header_size + calc_bson_array_size(*j.m_value.array);
  14038. case value_t::binary:
  14039. return header_size + calc_bson_binary_size(*j.m_value.binary);
  14040. case value_t::boolean:
  14041. return header_size + 1ul;
  14042. case value_t::number_float:
  14043. return header_size + 8ul;
  14044. case value_t::number_integer:
  14045. return header_size + calc_bson_integer_size(j.m_value.number_integer);
  14046. case value_t::number_unsigned:
  14047. return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
  14048. case value_t::string:
  14049. return header_size + calc_bson_string_size(*j.m_value.string);
  14050. case value_t::null:
  14051. return header_size + 0ul;
  14052. // LCOV_EXCL_START
  14053. case value_t::discarded:
  14054. default:
  14055. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14056. return 0ul;
  14057. // LCOV_EXCL_STOP
  14058. }
  14059. }
  14060. /*!
  14061. @brief Serializes the JSON value @a j to BSON and associates it with the
  14062. key @a name.
  14063. @param name The name to associate with the JSON entity @a j within the
  14064. current BSON document
  14065. */
  14066. void write_bson_element(const string_t& name,
  14067. const BasicJsonType& j)
  14068. {
  14069. switch (j.type())
  14070. {
  14071. case value_t::object:
  14072. return write_bson_object_entry(name, *j.m_value.object);
  14073. case value_t::array:
  14074. return write_bson_array(name, *j.m_value.array);
  14075. case value_t::binary:
  14076. return write_bson_binary(name, *j.m_value.binary);
  14077. case value_t::boolean:
  14078. return write_bson_boolean(name, j.m_value.boolean);
  14079. case value_t::number_float:
  14080. return write_bson_double(name, j.m_value.number_float);
  14081. case value_t::number_integer:
  14082. return write_bson_integer(name, j.m_value.number_integer);
  14083. case value_t::number_unsigned:
  14084. return write_bson_unsigned(name, j);
  14085. case value_t::string:
  14086. return write_bson_string(name, *j.m_value.string);
  14087. case value_t::null:
  14088. return write_bson_null(name);
  14089. // LCOV_EXCL_START
  14090. case value_t::discarded:
  14091. default:
  14092. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14093. return;
  14094. // LCOV_EXCL_STOP
  14095. }
  14096. }
  14097. /*!
  14098. @brief Calculates the size of the BSON serialization of the given
  14099. JSON-object @a j.
  14100. @param[in] value JSON value to serialize
  14101. @pre value.type() == value_t::object
  14102. */
  14103. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14104. {
  14105. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14106. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14107. {
  14108. return result += calc_bson_element_size(el.first, el.second);
  14109. });
  14110. return sizeof(std::int32_t) + document_size + 1ul;
  14111. }
  14112. /*!
  14113. @param[in] value JSON value to serialize
  14114. @pre value.type() == value_t::object
  14115. */
  14116. void write_bson_object(const typename BasicJsonType::object_t& value)
  14117. {
  14118. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  14119. for (const auto& el : value)
  14120. {
  14121. write_bson_element(el.first, el.second);
  14122. }
  14123. oa->write_character(to_char_type(0x00));
  14124. }
  14125. //////////
  14126. // CBOR //
  14127. //////////
  14128. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  14129. {
  14130. return to_char_type(0xFA); // Single-Precision Float
  14131. }
  14132. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  14133. {
  14134. return to_char_type(0xFB); // Double-Precision Float
  14135. }
  14136. /////////////
  14137. // MsgPack //
  14138. /////////////
  14139. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  14140. {
  14141. return to_char_type(0xCA); // float 32
  14142. }
  14143. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  14144. {
  14145. return to_char_type(0xCB); // float 64
  14146. }
  14147. ////////////
  14148. // UBJSON //
  14149. ////////////
  14150. // UBJSON: write number (floating point)
  14151. template<typename NumberType, typename std::enable_if<
  14152. std::is_floating_point<NumberType>::value, int>::type = 0>
  14153. void write_number_with_ubjson_prefix(const NumberType n,
  14154. const bool add_prefix,
  14155. const bool use_bjdata)
  14156. {
  14157. if (add_prefix)
  14158. {
  14159. oa->write_character(get_ubjson_float_prefix(n));
  14160. }
  14161. write_number(n, use_bjdata);
  14162. }
  14163. // UBJSON: write number (unsigned integer)
  14164. template<typename NumberType, typename std::enable_if<
  14165. std::is_unsigned<NumberType>::value, int>::type = 0>
  14166. void write_number_with_ubjson_prefix(const NumberType n,
  14167. const bool add_prefix,
  14168. const bool use_bjdata)
  14169. {
  14170. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14171. {
  14172. if (add_prefix)
  14173. {
  14174. oa->write_character(to_char_type('i')); // int8
  14175. }
  14176. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14177. }
  14178. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  14179. {
  14180. if (add_prefix)
  14181. {
  14182. oa->write_character(to_char_type('U')); // uint8
  14183. }
  14184. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14185. }
  14186. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14187. {
  14188. if (add_prefix)
  14189. {
  14190. oa->write_character(to_char_type('I')); // int16
  14191. }
  14192. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14193. }
  14194. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  14195. {
  14196. if (add_prefix)
  14197. {
  14198. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14199. }
  14200. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  14201. }
  14202. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14203. {
  14204. if (add_prefix)
  14205. {
  14206. oa->write_character(to_char_type('l')); // int32
  14207. }
  14208. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14209. }
  14210. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  14211. {
  14212. if (add_prefix)
  14213. {
  14214. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14215. }
  14216. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  14217. }
  14218. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14219. {
  14220. if (add_prefix)
  14221. {
  14222. oa->write_character(to_char_type('L')); // int64
  14223. }
  14224. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14225. }
  14226. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  14227. {
  14228. if (add_prefix)
  14229. {
  14230. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  14231. }
  14232. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  14233. }
  14234. else
  14235. {
  14236. if (add_prefix)
  14237. {
  14238. oa->write_character(to_char_type('H')); // high-precision number
  14239. }
  14240. const auto number = BasicJsonType(n).dump();
  14241. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14242. for (std::size_t i = 0; i < number.size(); ++i)
  14243. {
  14244. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14245. }
  14246. }
  14247. }
  14248. // UBJSON: write number (signed integer)
  14249. template < typename NumberType, typename std::enable_if <
  14250. std::is_signed<NumberType>::value&&
  14251. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  14252. void write_number_with_ubjson_prefix(const NumberType n,
  14253. const bool add_prefix,
  14254. const bool use_bjdata)
  14255. {
  14256. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  14257. {
  14258. if (add_prefix)
  14259. {
  14260. oa->write_character(to_char_type('i')); // int8
  14261. }
  14262. write_number(static_cast<std::int8_t>(n), use_bjdata);
  14263. }
  14264. 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)()))
  14265. {
  14266. if (add_prefix)
  14267. {
  14268. oa->write_character(to_char_type('U')); // uint8
  14269. }
  14270. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14271. }
  14272. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  14273. {
  14274. if (add_prefix)
  14275. {
  14276. oa->write_character(to_char_type('I')); // int16
  14277. }
  14278. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14279. }
  14280. 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)())))
  14281. {
  14282. if (add_prefix)
  14283. {
  14284. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14285. }
  14286. write_number(static_cast<uint16_t>(n), use_bjdata);
  14287. }
  14288. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  14289. {
  14290. if (add_prefix)
  14291. {
  14292. oa->write_character(to_char_type('l')); // int32
  14293. }
  14294. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14295. }
  14296. 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)())))
  14297. {
  14298. if (add_prefix)
  14299. {
  14300. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14301. }
  14302. write_number(static_cast<uint32_t>(n), use_bjdata);
  14303. }
  14304. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  14305. {
  14306. if (add_prefix)
  14307. {
  14308. oa->write_character(to_char_type('L')); // int64
  14309. }
  14310. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14311. }
  14312. // LCOV_EXCL_START
  14313. else
  14314. {
  14315. if (add_prefix)
  14316. {
  14317. oa->write_character(to_char_type('H')); // high-precision number
  14318. }
  14319. const auto number = BasicJsonType(n).dump();
  14320. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14321. for (std::size_t i = 0; i < number.size(); ++i)
  14322. {
  14323. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14324. }
  14325. }
  14326. // LCOV_EXCL_STOP
  14327. }
  14328. /*!
  14329. @brief determine the type prefix of container values
  14330. */
  14331. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  14332. {
  14333. switch (j.type())
  14334. {
  14335. case value_t::null:
  14336. return 'Z';
  14337. case value_t::boolean:
  14338. return j.m_value.boolean ? 'T' : 'F';
  14339. case value_t::number_integer:
  14340. {
  14341. if ((std::numeric_limits<std::int8_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  14342. {
  14343. return 'i';
  14344. }
  14345. if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  14346. {
  14347. return 'U';
  14348. }
  14349. if ((std::numeric_limits<std::int16_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  14350. {
  14351. return 'I';
  14352. }
  14353. if (use_bjdata && ((std::numeric_limits<std::uint16_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)()))
  14354. {
  14355. return 'u';
  14356. }
  14357. if ((std::numeric_limits<std::int32_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  14358. {
  14359. return 'l';
  14360. }
  14361. if (use_bjdata && ((std::numeric_limits<std::uint32_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)()))
  14362. {
  14363. return 'm';
  14364. }
  14365. if ((std::numeric_limits<std::int64_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  14366. {
  14367. return 'L';
  14368. }
  14369. // anything else is treated as high-precision number
  14370. return 'H'; // LCOV_EXCL_LINE
  14371. }
  14372. case value_t::number_unsigned:
  14373. {
  14374. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14375. {
  14376. return 'i';
  14377. }
  14378. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  14379. {
  14380. return 'U';
  14381. }
  14382. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14383. {
  14384. return 'I';
  14385. }
  14386. if (use_bjdata && j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  14387. {
  14388. return 'u';
  14389. }
  14390. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14391. {
  14392. return 'l';
  14393. }
  14394. if (use_bjdata && j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  14395. {
  14396. return 'm';
  14397. }
  14398. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14399. {
  14400. return 'L';
  14401. }
  14402. if (use_bjdata && j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14403. {
  14404. return 'M';
  14405. }
  14406. // anything else is treated as high-precision number
  14407. return 'H'; // LCOV_EXCL_LINE
  14408. }
  14409. case value_t::number_float:
  14410. return get_ubjson_float_prefix(j.m_value.number_float);
  14411. case value_t::string:
  14412. return 'S';
  14413. case value_t::array: // fallthrough
  14414. case value_t::binary:
  14415. return '[';
  14416. case value_t::object:
  14417. return '{';
  14418. case value_t::discarded:
  14419. default: // discarded values
  14420. return 'N';
  14421. }
  14422. }
  14423. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  14424. {
  14425. return 'd'; // float 32
  14426. }
  14427. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  14428. {
  14429. return 'D'; // float 64
  14430. }
  14431. /*!
  14432. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  14433. */
  14434. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  14435. {
  14436. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  14437. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  14438. };
  14439. string_t key = "_ArrayType_";
  14440. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  14441. if (it == bjdtype.end())
  14442. {
  14443. return true;
  14444. }
  14445. CharType dtype = it->second;
  14446. key = "_ArraySize_";
  14447. std::size_t len = (value.at(key).empty() ? 0 : 1);
  14448. for (const auto& el : value.at(key))
  14449. {
  14450. len *= static_cast<std::size_t>(el.m_value.number_unsigned);
  14451. }
  14452. key = "_ArrayData_";
  14453. if (value.at(key).size() != len)
  14454. {
  14455. return true;
  14456. }
  14457. oa->write_character('[');
  14458. oa->write_character('$');
  14459. oa->write_character(dtype);
  14460. oa->write_character('#');
  14461. key = "_ArraySize_";
  14462. write_ubjson(value.at(key), use_count, use_type, true, true);
  14463. key = "_ArrayData_";
  14464. if (dtype == 'U' || dtype == 'C')
  14465. {
  14466. for (const auto& el : value.at(key))
  14467. {
  14468. write_number(static_cast<std::uint8_t>(el.m_value.number_unsigned), true);
  14469. }
  14470. }
  14471. else if (dtype == 'i')
  14472. {
  14473. for (const auto& el : value.at(key))
  14474. {
  14475. write_number(static_cast<std::int8_t>(el.m_value.number_integer), true);
  14476. }
  14477. }
  14478. else if (dtype == 'u')
  14479. {
  14480. for (const auto& el : value.at(key))
  14481. {
  14482. write_number(static_cast<std::uint16_t>(el.m_value.number_unsigned), true);
  14483. }
  14484. }
  14485. else if (dtype == 'I')
  14486. {
  14487. for (const auto& el : value.at(key))
  14488. {
  14489. write_number(static_cast<std::int16_t>(el.m_value.number_integer), true);
  14490. }
  14491. }
  14492. else if (dtype == 'm')
  14493. {
  14494. for (const auto& el : value.at(key))
  14495. {
  14496. write_number(static_cast<std::uint32_t>(el.m_value.number_unsigned), true);
  14497. }
  14498. }
  14499. else if (dtype == 'l')
  14500. {
  14501. for (const auto& el : value.at(key))
  14502. {
  14503. write_number(static_cast<std::int32_t>(el.m_value.number_integer), true);
  14504. }
  14505. }
  14506. else if (dtype == 'M')
  14507. {
  14508. for (const auto& el : value.at(key))
  14509. {
  14510. write_number(static_cast<std::uint64_t>(el.m_value.number_unsigned), true);
  14511. }
  14512. }
  14513. else if (dtype == 'L')
  14514. {
  14515. for (const auto& el : value.at(key))
  14516. {
  14517. write_number(static_cast<std::int64_t>(el.m_value.number_integer), true);
  14518. }
  14519. }
  14520. else if (dtype == 'd')
  14521. {
  14522. for (const auto& el : value.at(key))
  14523. {
  14524. write_number(static_cast<float>(el.m_value.number_float), true);
  14525. }
  14526. }
  14527. else if (dtype == 'D')
  14528. {
  14529. for (const auto& el : value.at(key))
  14530. {
  14531. write_number(static_cast<double>(el.m_value.number_float), true);
  14532. }
  14533. }
  14534. return false;
  14535. }
  14536. ///////////////////////
  14537. // Utility functions //
  14538. ///////////////////////
  14539. /*
  14540. @brief write a number to output input
  14541. @param[in] n number of type @a NumberType
  14542. @param[in] OutputIsLittleEndian Set to true if output data is
  14543. required to be little endian
  14544. @tparam NumberType the type of the number
  14545. @note This function needs to respect the system's endianness, because bytes
  14546. in CBOR, MessagePack, and UBJSON are stored in network order (big
  14547. endian) and therefore need reordering on little endian systems.
  14548. On the other hand, BSON and BJData use little endian and should reorder
  14549. on big endian systems.
  14550. */
  14551. template<typename NumberType>
  14552. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  14553. {
  14554. // step 1: write number to array of length NumberType
  14555. std::array<CharType, sizeof(NumberType)> vec{};
  14556. std::memcpy(vec.data(), &n, sizeof(NumberType));
  14557. // step 2: write array to output (with possible reordering)
  14558. if (is_little_endian != OutputIsLittleEndian)
  14559. {
  14560. // reverse byte order prior to conversion if necessary
  14561. std::reverse(vec.begin(), vec.end());
  14562. }
  14563. oa->write_characters(vec.data(), sizeof(NumberType));
  14564. }
  14565. void write_compact_float(const number_float_t n, detail::input_format_t format)
  14566. {
  14567. #ifdef __GNUC__
  14568. #pragma GCC diagnostic push
  14569. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14570. #endif
  14571. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  14572. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  14573. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  14574. {
  14575. oa->write_character(format == detail::input_format_t::cbor
  14576. ? get_cbor_float_prefix(static_cast<float>(n))
  14577. : get_msgpack_float_prefix(static_cast<float>(n)));
  14578. write_number(static_cast<float>(n));
  14579. }
  14580. else
  14581. {
  14582. oa->write_character(format == detail::input_format_t::cbor
  14583. ? get_cbor_float_prefix(n)
  14584. : get_msgpack_float_prefix(n));
  14585. write_number(n);
  14586. }
  14587. #ifdef __GNUC__
  14588. #pragma GCC diagnostic pop
  14589. #endif
  14590. }
  14591. public:
  14592. // The following to_char_type functions are implement the conversion
  14593. // between uint8_t and CharType. In case CharType is not unsigned,
  14594. // such a conversion is required to allow values greater than 128.
  14595. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  14596. template < typename C = CharType,
  14597. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  14598. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14599. {
  14600. return *reinterpret_cast<char*>(&x);
  14601. }
  14602. template < typename C = CharType,
  14603. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  14604. static CharType to_char_type(std::uint8_t x) noexcept
  14605. {
  14606. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  14607. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  14608. CharType result;
  14609. std::memcpy(&result, &x, sizeof(x));
  14610. return result;
  14611. }
  14612. template<typename C = CharType,
  14613. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  14614. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14615. {
  14616. return x;
  14617. }
  14618. template < typename InputCharType, typename C = CharType,
  14619. enable_if_t <
  14620. std::is_signed<C>::value &&
  14621. std::is_signed<char>::value &&
  14622. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  14623. > * = nullptr >
  14624. static constexpr CharType to_char_type(InputCharType x) noexcept
  14625. {
  14626. return x;
  14627. }
  14628. private:
  14629. /// whether we can assume little endianness
  14630. const bool is_little_endian = little_endianness();
  14631. /// the output
  14632. output_adapter_t<CharType> oa = nullptr;
  14633. };
  14634. } // namespace detail
  14635. NLOHMANN_JSON_NAMESPACE_END
  14636. // #include <nlohmann/detail/output/output_adapters.hpp>
  14637. // #include <nlohmann/detail/output/serializer.hpp>
  14638. // __ _____ _____ _____
  14639. // __| | __| | | | JSON for Modern C++
  14640. // | | |__ | | | | | | version 3.11.2
  14641. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14642. //
  14643. // SPDX-FileCopyrightText: 2008-2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  14644. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  14645. // SPDX-License-Identifier: MIT
  14646. #include <algorithm> // reverse, remove, fill, find, none_of
  14647. #include <array> // array
  14648. #include <clocale> // localeconv, lconv
  14649. #include <cmath> // labs, isfinite, isnan, signbit
  14650. #include <cstddef> // size_t, ptrdiff_t
  14651. #include <cstdint> // uint8_t
  14652. #include <cstdio> // snprintf
  14653. #include <limits> // numeric_limits
  14654. #include <string> // string, char_traits
  14655. #include <iomanip> // setfill, setw
  14656. #include <type_traits> // is_same
  14657. #include <utility> // move
  14658. // #include <nlohmann/detail/conversions/to_chars.hpp>
  14659. // __ _____ _____ _____
  14660. // __| | __| | | | JSON for Modern C++
  14661. // | | |__ | | | | | | version 3.11.2
  14662. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14663. //
  14664. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  14665. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  14666. // SPDX-License-Identifier: MIT
  14667. #include <array> // array
  14668. #include <cmath> // signbit, isfinite
  14669. #include <cstdint> // intN_t, uintN_t
  14670. #include <cstring> // memcpy, memmove
  14671. #include <limits> // numeric_limits
  14672. #include <type_traits> // conditional
  14673. // #include <nlohmann/detail/macro_scope.hpp>
  14674. NLOHMANN_JSON_NAMESPACE_BEGIN
  14675. namespace detail
  14676. {
  14677. /*!
  14678. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  14679. conversion.
  14680. This implementation is a slightly modified version of the reference
  14681. implementation which may be obtained from
  14682. http://florian.loitsch.com/publications (bench.tar.gz).
  14683. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  14684. For a detailed description of the algorithm see:
  14685. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  14686. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  14687. Language Design and Implementation, PLDI 2010
  14688. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  14689. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  14690. Design and Implementation, PLDI 1996
  14691. */
  14692. namespace dtoa_impl
  14693. {
  14694. template<typename Target, typename Source>
  14695. Target reinterpret_bits(const Source source)
  14696. {
  14697. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  14698. Target target;
  14699. std::memcpy(&target, &source, sizeof(Source));
  14700. return target;
  14701. }
  14702. struct diyfp // f * 2^e
  14703. {
  14704. static constexpr int kPrecision = 64; // = q
  14705. std::uint64_t f = 0;
  14706. int e = 0;
  14707. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  14708. /*!
  14709. @brief returns x - y
  14710. @pre x.e == y.e and x.f >= y.f
  14711. */
  14712. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  14713. {
  14714. JSON_ASSERT(x.e == y.e);
  14715. JSON_ASSERT(x.f >= y.f);
  14716. return {x.f - y.f, x.e};
  14717. }
  14718. /*!
  14719. @brief returns x * y
  14720. @note The result is rounded. (Only the upper q bits are returned.)
  14721. */
  14722. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  14723. {
  14724. static_assert(kPrecision == 64, "internal error");
  14725. // Computes:
  14726. // f = round((x.f * y.f) / 2^q)
  14727. // e = x.e + y.e + q
  14728. // Emulate the 64-bit * 64-bit multiplication:
  14729. //
  14730. // p = u * v
  14731. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  14732. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  14733. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  14734. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  14735. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  14736. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  14737. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  14738. //
  14739. // (Since Q might be larger than 2^32 - 1)
  14740. //
  14741. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  14742. //
  14743. // (Q_hi + H does not overflow a 64-bit int)
  14744. //
  14745. // = p_lo + 2^64 p_hi
  14746. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  14747. const std::uint64_t u_hi = x.f >> 32u;
  14748. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  14749. const std::uint64_t v_hi = y.f >> 32u;
  14750. const std::uint64_t p0 = u_lo * v_lo;
  14751. const std::uint64_t p1 = u_lo * v_hi;
  14752. const std::uint64_t p2 = u_hi * v_lo;
  14753. const std::uint64_t p3 = u_hi * v_hi;
  14754. const std::uint64_t p0_hi = p0 >> 32u;
  14755. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  14756. const std::uint64_t p1_hi = p1 >> 32u;
  14757. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  14758. const std::uint64_t p2_hi = p2 >> 32u;
  14759. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  14760. // The full product might now be computed as
  14761. //
  14762. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  14763. // p_lo = p0_lo + (Q << 32)
  14764. //
  14765. // But in this particular case here, the full p_lo is not required.
  14766. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  14767. // Q_hi + 1 does not overflow).
  14768. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  14769. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  14770. return {h, x.e + y.e + 64};
  14771. }
  14772. /*!
  14773. @brief normalize x such that the significand is >= 2^(q-1)
  14774. @pre x.f != 0
  14775. */
  14776. static diyfp normalize(diyfp x) noexcept
  14777. {
  14778. JSON_ASSERT(x.f != 0);
  14779. while ((x.f >> 63u) == 0)
  14780. {
  14781. x.f <<= 1u;
  14782. x.e--;
  14783. }
  14784. return x;
  14785. }
  14786. /*!
  14787. @brief normalize x such that the result has the exponent E
  14788. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  14789. */
  14790. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  14791. {
  14792. const int delta = x.e - target_exponent;
  14793. JSON_ASSERT(delta >= 0);
  14794. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  14795. return {x.f << delta, target_exponent};
  14796. }
  14797. };
  14798. struct boundaries
  14799. {
  14800. diyfp w;
  14801. diyfp minus;
  14802. diyfp plus;
  14803. };
  14804. /*!
  14805. Compute the (normalized) diyfp representing the input number 'value' and its
  14806. boundaries.
  14807. @pre value must be finite and positive
  14808. */
  14809. template<typename FloatType>
  14810. boundaries compute_boundaries(FloatType value)
  14811. {
  14812. JSON_ASSERT(std::isfinite(value));
  14813. JSON_ASSERT(value > 0);
  14814. // Convert the IEEE representation into a diyfp.
  14815. //
  14816. // If v is denormal:
  14817. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  14818. // If v is normalized:
  14819. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  14820. static_assert(std::numeric_limits<FloatType>::is_iec559,
  14821. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  14822. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  14823. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  14824. constexpr int kMinExp = 1 - kBias;
  14825. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  14826. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  14827. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  14828. const std::uint64_t E = bits >> (kPrecision - 1);
  14829. const std::uint64_t F = bits & (kHiddenBit - 1);
  14830. const bool is_denormal = E == 0;
  14831. const diyfp v = is_denormal
  14832. ? diyfp(F, kMinExp)
  14833. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  14834. // Compute the boundaries m- and m+ of the floating-point value
  14835. // v = f * 2^e.
  14836. //
  14837. // Determine v- and v+, the floating-point predecessor and successor if v,
  14838. // respectively.
  14839. //
  14840. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  14841. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  14842. //
  14843. // v+ = v + 2^e
  14844. //
  14845. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  14846. // between m- and m+ round to v, regardless of how the input rounding
  14847. // algorithm breaks ties.
  14848. //
  14849. // ---+-------------+-------------+-------------+-------------+--- (A)
  14850. // v- m- v m+ v+
  14851. //
  14852. // -----------------+------+------+-------------+-------------+--- (B)
  14853. // v- m- v m+ v+
  14854. const bool lower_boundary_is_closer = F == 0 && E > 1;
  14855. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  14856. const diyfp m_minus = lower_boundary_is_closer
  14857. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  14858. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  14859. // Determine the normalized w+ = m+.
  14860. const diyfp w_plus = diyfp::normalize(m_plus);
  14861. // Determine w- = m- such that e_(w-) = e_(w+).
  14862. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  14863. return {diyfp::normalize(v), w_minus, w_plus};
  14864. }
  14865. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  14866. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  14867. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  14868. //
  14869. // alpha <= e = e_c + e_w + q <= gamma
  14870. //
  14871. // or
  14872. //
  14873. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  14874. // <= f_c * f_w * 2^gamma
  14875. //
  14876. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  14877. //
  14878. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  14879. //
  14880. // or
  14881. //
  14882. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  14883. //
  14884. // The choice of (alpha,gamma) determines the size of the table and the form of
  14885. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  14886. // in practice:
  14887. //
  14888. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  14889. // processed independently: An integral part p1, and a fractional part p2:
  14890. //
  14891. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  14892. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  14893. // = p1 + p2 * 2^e
  14894. //
  14895. // The conversion of p1 into decimal form requires a series of divisions and
  14896. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  14897. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  14898. // achieved by choosing
  14899. //
  14900. // -e >= 32 or e <= -32 := gamma
  14901. //
  14902. // In order to convert the fractional part
  14903. //
  14904. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14905. //
  14906. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  14907. // d[-i] are extracted in order:
  14908. //
  14909. // (10 * p2) div 2^-e = d[-1]
  14910. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  14911. //
  14912. // The multiplication by 10 must not overflow. It is sufficient to choose
  14913. //
  14914. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  14915. //
  14916. // Since p2 = f mod 2^-e < 2^-e,
  14917. //
  14918. // -e <= 60 or e >= -60 := alpha
  14919. constexpr int kAlpha = -60;
  14920. constexpr int kGamma = -32;
  14921. struct cached_power // c = f * 2^e ~= 10^k
  14922. {
  14923. std::uint64_t f;
  14924. int e;
  14925. int k;
  14926. };
  14927. /*!
  14928. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  14929. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  14930. satisfies (Definition 3.2 from [1])
  14931. alpha <= e_c + e + q <= gamma.
  14932. */
  14933. inline cached_power get_cached_power_for_binary_exponent(int e)
  14934. {
  14935. // Now
  14936. //
  14937. // alpha <= e_c + e + q <= gamma (1)
  14938. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  14939. //
  14940. // and since the c's are normalized, 2^(q-1) <= f_c,
  14941. //
  14942. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  14943. // ==> 2^(alpha - e - 1) <= c
  14944. //
  14945. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  14946. //
  14947. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  14948. // = ceil( (alpha - e - 1) * log_10(2) )
  14949. //
  14950. // From the paper:
  14951. // "In theory the result of the procedure could be wrong since c is rounded,
  14952. // and the computation itself is approximated [...]. In practice, however,
  14953. // this simple function is sufficient."
  14954. //
  14955. // For IEEE double precision floating-point numbers converted into
  14956. // normalized diyfp's w = f * 2^e, with q = 64,
  14957. //
  14958. // e >= -1022 (min IEEE exponent)
  14959. // -52 (p - 1)
  14960. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  14961. // -11 (normalize the diyfp)
  14962. // = -1137
  14963. //
  14964. // and
  14965. //
  14966. // e <= +1023 (max IEEE exponent)
  14967. // -52 (p - 1)
  14968. // -11 (normalize the diyfp)
  14969. // = 960
  14970. //
  14971. // This binary exponent range [-1137,960] results in a decimal exponent
  14972. // range [-307,324]. One does not need to store a cached power for each
  14973. // k in this range. For each such k it suffices to find a cached power
  14974. // such that the exponent of the product lies in [alpha,gamma].
  14975. // This implies that the difference of the decimal exponents of adjacent
  14976. // table entries must be less than or equal to
  14977. //
  14978. // floor( (gamma - alpha) * log_10(2) ) = 8.
  14979. //
  14980. // (A smaller distance gamma-alpha would require a larger table.)
  14981. // NB:
  14982. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  14983. constexpr int kCachedPowersMinDecExp = -300;
  14984. constexpr int kCachedPowersDecStep = 8;
  14985. static constexpr std::array<cached_power, 79> kCachedPowers =
  14986. {
  14987. {
  14988. { 0xAB70FE17C79AC6CA, -1060, -300 },
  14989. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  14990. { 0xBE5691EF416BD60C, -1007, -284 },
  14991. { 0x8DD01FAD907FFC3C, -980, -276 },
  14992. { 0xD3515C2831559A83, -954, -268 },
  14993. { 0x9D71AC8FADA6C9B5, -927, -260 },
  14994. { 0xEA9C227723EE8BCB, -901, -252 },
  14995. { 0xAECC49914078536D, -874, -244 },
  14996. { 0x823C12795DB6CE57, -847, -236 },
  14997. { 0xC21094364DFB5637, -821, -228 },
  14998. { 0x9096EA6F3848984F, -794, -220 },
  14999. { 0xD77485CB25823AC7, -768, -212 },
  15000. { 0xA086CFCD97BF97F4, -741, -204 },
  15001. { 0xEF340A98172AACE5, -715, -196 },
  15002. { 0xB23867FB2A35B28E, -688, -188 },
  15003. { 0x84C8D4DFD2C63F3B, -661, -180 },
  15004. { 0xC5DD44271AD3CDBA, -635, -172 },
  15005. { 0x936B9FCEBB25C996, -608, -164 },
  15006. { 0xDBAC6C247D62A584, -582, -156 },
  15007. { 0xA3AB66580D5FDAF6, -555, -148 },
  15008. { 0xF3E2F893DEC3F126, -529, -140 },
  15009. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  15010. { 0x87625F056C7C4A8B, -475, -124 },
  15011. { 0xC9BCFF6034C13053, -449, -116 },
  15012. { 0x964E858C91BA2655, -422, -108 },
  15013. { 0xDFF9772470297EBD, -396, -100 },
  15014. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  15015. { 0xF8A95FCF88747D94, -343, -84 },
  15016. { 0xB94470938FA89BCF, -316, -76 },
  15017. { 0x8A08F0F8BF0F156B, -289, -68 },
  15018. { 0xCDB02555653131B6, -263, -60 },
  15019. { 0x993FE2C6D07B7FAC, -236, -52 },
  15020. { 0xE45C10C42A2B3B06, -210, -44 },
  15021. { 0xAA242499697392D3, -183, -36 },
  15022. { 0xFD87B5F28300CA0E, -157, -28 },
  15023. { 0xBCE5086492111AEB, -130, -20 },
  15024. { 0x8CBCCC096F5088CC, -103, -12 },
  15025. { 0xD1B71758E219652C, -77, -4 },
  15026. { 0x9C40000000000000, -50, 4 },
  15027. { 0xE8D4A51000000000, -24, 12 },
  15028. { 0xAD78EBC5AC620000, 3, 20 },
  15029. { 0x813F3978F8940984, 30, 28 },
  15030. { 0xC097CE7BC90715B3, 56, 36 },
  15031. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15032. { 0xD5D238A4ABE98068, 109, 52 },
  15033. { 0x9F4F2726179A2245, 136, 60 },
  15034. { 0xED63A231D4C4FB27, 162, 68 },
  15035. { 0xB0DE65388CC8ADA8, 189, 76 },
  15036. { 0x83C7088E1AAB65DB, 216, 84 },
  15037. { 0xC45D1DF942711D9A, 242, 92 },
  15038. { 0x924D692CA61BE758, 269, 100 },
  15039. { 0xDA01EE641A708DEA, 295, 108 },
  15040. { 0xA26DA3999AEF774A, 322, 116 },
  15041. { 0xF209787BB47D6B85, 348, 124 },
  15042. { 0xB454E4A179DD1877, 375, 132 },
  15043. { 0x865B86925B9BC5C2, 402, 140 },
  15044. { 0xC83553C5C8965D3D, 428, 148 },
  15045. { 0x952AB45CFA97A0B3, 455, 156 },
  15046. { 0xDE469FBD99A05FE3, 481, 164 },
  15047. { 0xA59BC234DB398C25, 508, 172 },
  15048. { 0xF6C69A72A3989F5C, 534, 180 },
  15049. { 0xB7DCBF5354E9BECE, 561, 188 },
  15050. { 0x88FCF317F22241E2, 588, 196 },
  15051. { 0xCC20CE9BD35C78A5, 614, 204 },
  15052. { 0x98165AF37B2153DF, 641, 212 },
  15053. { 0xE2A0B5DC971F303A, 667, 220 },
  15054. { 0xA8D9D1535CE3B396, 694, 228 },
  15055. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15056. { 0xBB764C4CA7A44410, 747, 244 },
  15057. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15058. { 0xD01FEF10A657842C, 800, 260 },
  15059. { 0x9B10A4E5E9913129, 827, 268 },
  15060. { 0xE7109BFBA19C0C9D, 853, 276 },
  15061. { 0xAC2820D9623BF429, 880, 284 },
  15062. { 0x80444B5E7AA7CF85, 907, 292 },
  15063. { 0xBF21E44003ACDD2D, 933, 300 },
  15064. { 0x8E679C2F5E44FF8F, 960, 308 },
  15065. { 0xD433179D9C8CB841, 986, 316 },
  15066. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15067. }
  15068. };
  15069. // This computation gives exactly the same results for k as
  15070. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15071. // for |e| <= 1500, but doesn't require floating-point operations.
  15072. // NB: log_10(2) ~= 78913 / 2^18
  15073. JSON_ASSERT(e >= -1500);
  15074. JSON_ASSERT(e <= 1500);
  15075. const int f = kAlpha - e - 1;
  15076. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  15077. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15078. JSON_ASSERT(index >= 0);
  15079. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15080. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15081. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15082. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15083. return cached;
  15084. }
  15085. /*!
  15086. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15087. For n == 0, returns 1 and sets pow10 := 1.
  15088. */
  15089. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15090. {
  15091. // LCOV_EXCL_START
  15092. if (n >= 1000000000)
  15093. {
  15094. pow10 = 1000000000;
  15095. return 10;
  15096. }
  15097. // LCOV_EXCL_STOP
  15098. if (n >= 100000000)
  15099. {
  15100. pow10 = 100000000;
  15101. return 9;
  15102. }
  15103. if (n >= 10000000)
  15104. {
  15105. pow10 = 10000000;
  15106. return 8;
  15107. }
  15108. if (n >= 1000000)
  15109. {
  15110. pow10 = 1000000;
  15111. return 7;
  15112. }
  15113. if (n >= 100000)
  15114. {
  15115. pow10 = 100000;
  15116. return 6;
  15117. }
  15118. if (n >= 10000)
  15119. {
  15120. pow10 = 10000;
  15121. return 5;
  15122. }
  15123. if (n >= 1000)
  15124. {
  15125. pow10 = 1000;
  15126. return 4;
  15127. }
  15128. if (n >= 100)
  15129. {
  15130. pow10 = 100;
  15131. return 3;
  15132. }
  15133. if (n >= 10)
  15134. {
  15135. pow10 = 10;
  15136. return 2;
  15137. }
  15138. pow10 = 1;
  15139. return 1;
  15140. }
  15141. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  15142. std::uint64_t rest, std::uint64_t ten_k)
  15143. {
  15144. JSON_ASSERT(len >= 1);
  15145. JSON_ASSERT(dist <= delta);
  15146. JSON_ASSERT(rest <= delta);
  15147. JSON_ASSERT(ten_k > 0);
  15148. // <--------------------------- delta ---->
  15149. // <---- dist --------->
  15150. // --------------[------------------+-------------------]--------------
  15151. // M- w M+
  15152. //
  15153. // ten_k
  15154. // <------>
  15155. // <---- rest ---->
  15156. // --------------[------------------+----+--------------]--------------
  15157. // w V
  15158. // = buf * 10^k
  15159. //
  15160. // ten_k represents a unit-in-the-last-place in the decimal representation
  15161. // stored in buf.
  15162. // Decrement buf by ten_k while this takes buf closer to w.
  15163. // The tests are written in this order to avoid overflow in unsigned
  15164. // integer arithmetic.
  15165. while (rest < dist
  15166. && delta - rest >= ten_k
  15167. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  15168. {
  15169. JSON_ASSERT(buf[len - 1] != '0');
  15170. buf[len - 1]--;
  15171. rest += ten_k;
  15172. }
  15173. }
  15174. /*!
  15175. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  15176. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  15177. */
  15178. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  15179. diyfp M_minus, diyfp w, diyfp M_plus)
  15180. {
  15181. static_assert(kAlpha >= -60, "internal error");
  15182. static_assert(kGamma <= -32, "internal error");
  15183. // Generates the digits (and the exponent) of a decimal floating-point
  15184. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  15185. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  15186. //
  15187. // <--------------------------- delta ---->
  15188. // <---- dist --------->
  15189. // --------------[------------------+-------------------]--------------
  15190. // M- w M+
  15191. //
  15192. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  15193. // V is in [M-,M+].
  15194. JSON_ASSERT(M_plus.e >= kAlpha);
  15195. JSON_ASSERT(M_plus.e <= kGamma);
  15196. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  15197. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  15198. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  15199. //
  15200. // M+ = f * 2^e
  15201. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  15202. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  15203. // = p1 + p2 * 2^e
  15204. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  15205. 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.)
  15206. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  15207. // 1)
  15208. //
  15209. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  15210. JSON_ASSERT(p1 > 0);
  15211. std::uint32_t pow10{};
  15212. const int k = find_largest_pow10(p1, pow10);
  15213. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  15214. //
  15215. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  15216. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  15217. //
  15218. // M+ = p1 + p2 * 2^e
  15219. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  15220. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  15221. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  15222. //
  15223. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  15224. //
  15225. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  15226. //
  15227. // but stop as soon as
  15228. //
  15229. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  15230. int n = k;
  15231. while (n > 0)
  15232. {
  15233. // Invariants:
  15234. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  15235. // pow10 = 10^(n-1) <= p1 < 10^n
  15236. //
  15237. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  15238. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  15239. //
  15240. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  15241. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  15242. //
  15243. JSON_ASSERT(d <= 9);
  15244. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15245. //
  15246. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  15247. //
  15248. p1 = r;
  15249. n--;
  15250. //
  15251. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  15252. // pow10 = 10^n
  15253. //
  15254. // Now check if enough digits have been generated.
  15255. // Compute
  15256. //
  15257. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  15258. //
  15259. // Note:
  15260. // Since rest and delta share the same exponent e, it suffices to
  15261. // compare the significands.
  15262. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  15263. if (rest <= delta)
  15264. {
  15265. // V = buffer * 10^n, with M- <= V <= M+.
  15266. decimal_exponent += n;
  15267. // We may now just stop. But instead look if the buffer could be
  15268. // decremented to bring V closer to w.
  15269. //
  15270. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  15271. // The rounding procedure works with diyfp's with an implicit
  15272. // exponent of e.
  15273. //
  15274. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  15275. //
  15276. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  15277. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  15278. return;
  15279. }
  15280. pow10 /= 10;
  15281. //
  15282. // pow10 = 10^(n-1) <= p1 < 10^n
  15283. // Invariants restored.
  15284. }
  15285. // 2)
  15286. //
  15287. // The digits of the integral part have been generated:
  15288. //
  15289. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  15290. // = buffer + p2 * 2^e
  15291. //
  15292. // Now generate the digits of the fractional part p2 * 2^e.
  15293. //
  15294. // Note:
  15295. // No decimal point is generated: the exponent is adjusted instead.
  15296. //
  15297. // p2 actually represents the fraction
  15298. //
  15299. // p2 * 2^e
  15300. // = p2 / 2^-e
  15301. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15302. //
  15303. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  15304. //
  15305. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  15306. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  15307. //
  15308. // using
  15309. //
  15310. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  15311. // = ( d) * 2^-e + ( r)
  15312. //
  15313. // or
  15314. // 10^m * p2 * 2^e = d + r * 2^e
  15315. //
  15316. // i.e.
  15317. //
  15318. // M+ = buffer + p2 * 2^e
  15319. // = buffer + 10^-m * (d + r * 2^e)
  15320. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  15321. //
  15322. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  15323. JSON_ASSERT(p2 > delta);
  15324. int m = 0;
  15325. for (;;)
  15326. {
  15327. // Invariant:
  15328. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  15329. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  15330. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  15331. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  15332. //
  15333. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  15334. p2 *= 10;
  15335. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  15336. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  15337. //
  15338. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  15339. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  15340. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15341. //
  15342. JSON_ASSERT(d <= 9);
  15343. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15344. //
  15345. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15346. //
  15347. p2 = r;
  15348. m++;
  15349. //
  15350. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  15351. // Invariant restored.
  15352. // Check if enough digits have been generated.
  15353. //
  15354. // 10^-m * p2 * 2^e <= delta * 2^e
  15355. // p2 * 2^e <= 10^m * delta * 2^e
  15356. // p2 <= 10^m * delta
  15357. delta *= 10;
  15358. dist *= 10;
  15359. if (p2 <= delta)
  15360. {
  15361. break;
  15362. }
  15363. }
  15364. // V = buffer * 10^-m, with M- <= V <= M+.
  15365. decimal_exponent -= m;
  15366. // 1 ulp in the decimal representation is now 10^-m.
  15367. // Since delta and dist are now scaled by 10^m, we need to do the
  15368. // same with ulp in order to keep the units in sync.
  15369. //
  15370. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  15371. //
  15372. const std::uint64_t ten_m = one.f;
  15373. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  15374. // By construction this algorithm generates the shortest possible decimal
  15375. // number (Loitsch, Theorem 6.2) which rounds back to w.
  15376. // For an input number of precision p, at least
  15377. //
  15378. // N = 1 + ceil(p * log_10(2))
  15379. //
  15380. // decimal digits are sufficient to identify all binary floating-point
  15381. // numbers (Matula, "In-and-Out conversions").
  15382. // This implies that the algorithm does not produce more than N decimal
  15383. // digits.
  15384. //
  15385. // N = 17 for p = 53 (IEEE double precision)
  15386. // N = 9 for p = 24 (IEEE single precision)
  15387. }
  15388. /*!
  15389. v = buf * 10^decimal_exponent
  15390. len is the length of the buffer (number of decimal digits)
  15391. The buffer must be large enough, i.e. >= max_digits10.
  15392. */
  15393. JSON_HEDLEY_NON_NULL(1)
  15394. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  15395. diyfp m_minus, diyfp v, diyfp m_plus)
  15396. {
  15397. JSON_ASSERT(m_plus.e == m_minus.e);
  15398. JSON_ASSERT(m_plus.e == v.e);
  15399. // --------(-----------------------+-----------------------)-------- (A)
  15400. // m- v m+
  15401. //
  15402. // --------------------(-----------+-----------------------)-------- (B)
  15403. // m- v m+
  15404. //
  15405. // First scale v (and m- and m+) such that the exponent is in the range
  15406. // [alpha, gamma].
  15407. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  15408. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  15409. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  15410. const diyfp w = diyfp::mul(v, c_minus_k);
  15411. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  15412. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  15413. // ----(---+---)---------------(---+---)---------------(---+---)----
  15414. // w- w w+
  15415. // = c*m- = c*v = c*m+
  15416. //
  15417. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  15418. // w+ are now off by a small amount.
  15419. // In fact:
  15420. //
  15421. // w - v * 10^k < 1 ulp
  15422. //
  15423. // To account for this inaccuracy, add resp. subtract 1 ulp.
  15424. //
  15425. // --------+---[---------------(---+---)---------------]---+--------
  15426. // w- M- w M+ w+
  15427. //
  15428. // Now any number in [M-, M+] (bounds included) will round to w when input,
  15429. // regardless of how the input rounding algorithm breaks ties.
  15430. //
  15431. // And digit_gen generates the shortest possible such number in [M-, M+].
  15432. // Note that this does not mean that Grisu2 always generates the shortest
  15433. // possible number in the interval (m-, m+).
  15434. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  15435. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  15436. decimal_exponent = -cached.k; // = -(-k) = k
  15437. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  15438. }
  15439. /*!
  15440. v = buf * 10^decimal_exponent
  15441. len is the length of the buffer (number of decimal digits)
  15442. The buffer must be large enough, i.e. >= max_digits10.
  15443. */
  15444. template<typename FloatType>
  15445. JSON_HEDLEY_NON_NULL(1)
  15446. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  15447. {
  15448. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  15449. "internal error: not enough precision");
  15450. JSON_ASSERT(std::isfinite(value));
  15451. JSON_ASSERT(value > 0);
  15452. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  15453. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  15454. // decimal representations are not exactly "short".
  15455. //
  15456. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  15457. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  15458. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  15459. // does.
  15460. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  15461. // representation using the corresponding std::from_chars function recovers value exactly". That
  15462. // indicates that single precision floating-point numbers should be recovered using
  15463. // 'std::strtof'.
  15464. //
  15465. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  15466. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  15467. // value is off by 1 ulp.
  15468. #if 0
  15469. const boundaries w = compute_boundaries(static_cast<double>(value));
  15470. #else
  15471. const boundaries w = compute_boundaries(value);
  15472. #endif
  15473. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  15474. }
  15475. /*!
  15476. @brief appends a decimal representation of e to buf
  15477. @return a pointer to the element following the exponent.
  15478. @pre -1000 < e < 1000
  15479. */
  15480. JSON_HEDLEY_NON_NULL(1)
  15481. JSON_HEDLEY_RETURNS_NON_NULL
  15482. inline char* append_exponent(char* buf, int e)
  15483. {
  15484. JSON_ASSERT(e > -1000);
  15485. JSON_ASSERT(e < 1000);
  15486. if (e < 0)
  15487. {
  15488. e = -e;
  15489. *buf++ = '-';
  15490. }
  15491. else
  15492. {
  15493. *buf++ = '+';
  15494. }
  15495. auto k = static_cast<std::uint32_t>(e);
  15496. if (k < 10)
  15497. {
  15498. // Always print at least two digits in the exponent.
  15499. // This is for compatibility with printf("%g").
  15500. *buf++ = '0';
  15501. *buf++ = static_cast<char>('0' + k);
  15502. }
  15503. else if (k < 100)
  15504. {
  15505. *buf++ = static_cast<char>('0' + k / 10);
  15506. k %= 10;
  15507. *buf++ = static_cast<char>('0' + k);
  15508. }
  15509. else
  15510. {
  15511. *buf++ = static_cast<char>('0' + k / 100);
  15512. k %= 100;
  15513. *buf++ = static_cast<char>('0' + k / 10);
  15514. k %= 10;
  15515. *buf++ = static_cast<char>('0' + k);
  15516. }
  15517. return buf;
  15518. }
  15519. /*!
  15520. @brief prettify v = buf * 10^decimal_exponent
  15521. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  15522. notation. Otherwise it will be printed in exponential notation.
  15523. @pre min_exp < 0
  15524. @pre max_exp > 0
  15525. */
  15526. JSON_HEDLEY_NON_NULL(1)
  15527. JSON_HEDLEY_RETURNS_NON_NULL
  15528. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  15529. int min_exp, int max_exp)
  15530. {
  15531. JSON_ASSERT(min_exp < 0);
  15532. JSON_ASSERT(max_exp > 0);
  15533. const int k = len;
  15534. const int n = len + decimal_exponent;
  15535. // v = buf * 10^(n-k)
  15536. // k is the length of the buffer (number of decimal digits)
  15537. // n is the position of the decimal point relative to the start of the buffer.
  15538. if (k <= n && n <= max_exp)
  15539. {
  15540. // digits[000]
  15541. // len <= max_exp + 2
  15542. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  15543. // Make it look like a floating-point number (#362, #378)
  15544. buf[n + 0] = '.';
  15545. buf[n + 1] = '0';
  15546. return buf + (static_cast<size_t>(n) + 2);
  15547. }
  15548. if (0 < n && n <= max_exp)
  15549. {
  15550. // dig.its
  15551. // len <= max_digits10 + 1
  15552. JSON_ASSERT(k > n);
  15553. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  15554. buf[n] = '.';
  15555. return buf + (static_cast<size_t>(k) + 1U);
  15556. }
  15557. if (min_exp < n && n <= 0)
  15558. {
  15559. // 0.[000]digits
  15560. // len <= 2 + (-min_exp - 1) + max_digits10
  15561. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  15562. buf[0] = '0';
  15563. buf[1] = '.';
  15564. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  15565. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  15566. }
  15567. if (k == 1)
  15568. {
  15569. // dE+123
  15570. // len <= 1 + 5
  15571. buf += 1;
  15572. }
  15573. else
  15574. {
  15575. // d.igitsE+123
  15576. // len <= max_digits10 + 1 + 5
  15577. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  15578. buf[1] = '.';
  15579. buf += 1 + static_cast<size_t>(k);
  15580. }
  15581. *buf++ = 'e';
  15582. return append_exponent(buf, n - 1);
  15583. }
  15584. } // namespace dtoa_impl
  15585. /*!
  15586. @brief generates a decimal representation of the floating-point number value in [first, last).
  15587. The format of the resulting decimal representation is similar to printf's %g
  15588. format. Returns an iterator pointing past-the-end of the decimal representation.
  15589. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  15590. @note The buffer must be large enough.
  15591. @note The result is NOT null-terminated.
  15592. */
  15593. template<typename FloatType>
  15594. JSON_HEDLEY_NON_NULL(1, 2)
  15595. JSON_HEDLEY_RETURNS_NON_NULL
  15596. char* to_chars(char* first, const char* last, FloatType value)
  15597. {
  15598. static_cast<void>(last); // maybe unused - fix warning
  15599. JSON_ASSERT(std::isfinite(value));
  15600. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  15601. if (std::signbit(value))
  15602. {
  15603. value = -value;
  15604. *first++ = '-';
  15605. }
  15606. #ifdef __GNUC__
  15607. #pragma GCC diagnostic push
  15608. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15609. #endif
  15610. if (value == 0) // +-0
  15611. {
  15612. *first++ = '0';
  15613. // Make it look like a floating-point number (#362, #378)
  15614. *first++ = '.';
  15615. *first++ = '0';
  15616. return first;
  15617. }
  15618. #ifdef __GNUC__
  15619. #pragma GCC diagnostic pop
  15620. #endif
  15621. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  15622. // Compute v = buffer * 10^decimal_exponent.
  15623. // The decimal digits are stored in the buffer, which needs to be interpreted
  15624. // as an unsigned decimal integer.
  15625. // len is the length of the buffer, i.e. the number of decimal digits.
  15626. int len = 0;
  15627. int decimal_exponent = 0;
  15628. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  15629. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  15630. // Format the buffer like printf("%.*g", prec, value)
  15631. constexpr int kMinExp = -4;
  15632. // Use digits10 here to increase compatibility with version 2.
  15633. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  15634. JSON_ASSERT(last - first >= kMaxExp + 2);
  15635. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  15636. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  15637. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  15638. }
  15639. } // namespace detail
  15640. NLOHMANN_JSON_NAMESPACE_END
  15641. // #include <nlohmann/detail/exceptions.hpp>
  15642. // #include <nlohmann/detail/macro_scope.hpp>
  15643. // #include <nlohmann/detail/meta/cpp_future.hpp>
  15644. // #include <nlohmann/detail/output/binary_writer.hpp>
  15645. // #include <nlohmann/detail/output/output_adapters.hpp>
  15646. // #include <nlohmann/detail/string_concat.hpp>
  15647. // #include <nlohmann/detail/value_t.hpp>
  15648. NLOHMANN_JSON_NAMESPACE_BEGIN
  15649. namespace detail
  15650. {
  15651. ///////////////////
  15652. // serialization //
  15653. ///////////////////
  15654. /// how to treat decoding errors
  15655. enum class error_handler_t
  15656. {
  15657. strict, ///< throw a type_error exception in case of invalid UTF-8
  15658. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  15659. ignore ///< ignore invalid UTF-8 sequences
  15660. };
  15661. template<typename BasicJsonType>
  15662. class serializer
  15663. {
  15664. using string_t = typename BasicJsonType::string_t;
  15665. using number_float_t = typename BasicJsonType::number_float_t;
  15666. using number_integer_t = typename BasicJsonType::number_integer_t;
  15667. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  15668. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  15669. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  15670. static constexpr std::uint8_t UTF8_REJECT = 1;
  15671. public:
  15672. /*!
  15673. @param[in] s output stream to serialize to
  15674. @param[in] ichar indentation character to use
  15675. @param[in] error_handler_ how to react on decoding errors
  15676. */
  15677. serializer(output_adapter_t<char> s, const char ichar,
  15678. error_handler_t error_handler_ = error_handler_t::strict)
  15679. : o(std::move(s))
  15680. , loc(std::localeconv())
  15681. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  15682. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  15683. , indent_char(ichar)
  15684. , indent_string(512, indent_char)
  15685. , error_handler(error_handler_)
  15686. {}
  15687. // delete because of pointer members
  15688. serializer(const serializer&) = delete;
  15689. serializer& operator=(const serializer&) = delete;
  15690. serializer(serializer&&) = delete;
  15691. serializer& operator=(serializer&&) = delete;
  15692. ~serializer() = default;
  15693. /*!
  15694. @brief internal implementation of the serialization function
  15695. This function is called by the public member function dump and organizes
  15696. the serialization internally. The indentation level is propagated as
  15697. additional parameter. In case of arrays and objects, the function is
  15698. called recursively.
  15699. - strings and object keys are escaped using `escape_string()`
  15700. - integer numbers are converted implicitly via `operator<<`
  15701. - floating-point numbers are converted to a string using `"%g"` format
  15702. - binary values are serialized as objects containing the subtype and the
  15703. byte array
  15704. @param[in] val value to serialize
  15705. @param[in] pretty_print whether the output shall be pretty-printed
  15706. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  15707. in the output are escaped with `\uXXXX` sequences, and the result consists
  15708. of ASCII characters only.
  15709. @param[in] indent_step the indent level
  15710. @param[in] current_indent the current indent level (only used internally)
  15711. */
  15712. void dump(const BasicJsonType& val,
  15713. const bool pretty_print,
  15714. const bool ensure_ascii,
  15715. const unsigned int indent_step,
  15716. const unsigned int current_indent = 0)
  15717. {
  15718. switch (val.m_type)
  15719. {
  15720. case value_t::object:
  15721. {
  15722. if (val.m_value.object->empty())
  15723. {
  15724. o->write_characters("{}", 2);
  15725. return;
  15726. }
  15727. if (pretty_print)
  15728. {
  15729. o->write_characters("{\n", 2);
  15730. // variable to hold indentation for recursive calls
  15731. const auto new_indent = current_indent + indent_step;
  15732. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15733. {
  15734. indent_string.resize(indent_string.size() * 2, ' ');
  15735. }
  15736. // first n-1 elements
  15737. auto i = val.m_value.object->cbegin();
  15738. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  15739. {
  15740. o->write_characters(indent_string.c_str(), new_indent);
  15741. o->write_character('\"');
  15742. dump_escaped(i->first, ensure_ascii);
  15743. o->write_characters("\": ", 3);
  15744. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15745. o->write_characters(",\n", 2);
  15746. }
  15747. // last element
  15748. JSON_ASSERT(i != val.m_value.object->cend());
  15749. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  15750. o->write_characters(indent_string.c_str(), new_indent);
  15751. o->write_character('\"');
  15752. dump_escaped(i->first, ensure_ascii);
  15753. o->write_characters("\": ", 3);
  15754. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15755. o->write_character('\n');
  15756. o->write_characters(indent_string.c_str(), current_indent);
  15757. o->write_character('}');
  15758. }
  15759. else
  15760. {
  15761. o->write_character('{');
  15762. // first n-1 elements
  15763. auto i = val.m_value.object->cbegin();
  15764. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  15765. {
  15766. o->write_character('\"');
  15767. dump_escaped(i->first, ensure_ascii);
  15768. o->write_characters("\":", 2);
  15769. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15770. o->write_character(',');
  15771. }
  15772. // last element
  15773. JSON_ASSERT(i != val.m_value.object->cend());
  15774. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  15775. o->write_character('\"');
  15776. dump_escaped(i->first, ensure_ascii);
  15777. o->write_characters("\":", 2);
  15778. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15779. o->write_character('}');
  15780. }
  15781. return;
  15782. }
  15783. case value_t::array:
  15784. {
  15785. if (val.m_value.array->empty())
  15786. {
  15787. o->write_characters("[]", 2);
  15788. return;
  15789. }
  15790. if (pretty_print)
  15791. {
  15792. o->write_characters("[\n", 2);
  15793. // variable to hold indentation for recursive calls
  15794. const auto new_indent = current_indent + indent_step;
  15795. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15796. {
  15797. indent_string.resize(indent_string.size() * 2, ' ');
  15798. }
  15799. // first n-1 elements
  15800. for (auto i = val.m_value.array->cbegin();
  15801. i != val.m_value.array->cend() - 1; ++i)
  15802. {
  15803. o->write_characters(indent_string.c_str(), new_indent);
  15804. dump(*i, true, ensure_ascii, indent_step, new_indent);
  15805. o->write_characters(",\n", 2);
  15806. }
  15807. // last element
  15808. JSON_ASSERT(!val.m_value.array->empty());
  15809. o->write_characters(indent_string.c_str(), new_indent);
  15810. dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  15811. o->write_character('\n');
  15812. o->write_characters(indent_string.c_str(), current_indent);
  15813. o->write_character(']');
  15814. }
  15815. else
  15816. {
  15817. o->write_character('[');
  15818. // first n-1 elements
  15819. for (auto i = val.m_value.array->cbegin();
  15820. i != val.m_value.array->cend() - 1; ++i)
  15821. {
  15822. dump(*i, false, ensure_ascii, indent_step, current_indent);
  15823. o->write_character(',');
  15824. }
  15825. // last element
  15826. JSON_ASSERT(!val.m_value.array->empty());
  15827. dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  15828. o->write_character(']');
  15829. }
  15830. return;
  15831. }
  15832. case value_t::string:
  15833. {
  15834. o->write_character('\"');
  15835. dump_escaped(*val.m_value.string, ensure_ascii);
  15836. o->write_character('\"');
  15837. return;
  15838. }
  15839. case value_t::binary:
  15840. {
  15841. if (pretty_print)
  15842. {
  15843. o->write_characters("{\n", 2);
  15844. // variable to hold indentation for recursive calls
  15845. const auto new_indent = current_indent + indent_step;
  15846. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15847. {
  15848. indent_string.resize(indent_string.size() * 2, ' ');
  15849. }
  15850. o->write_characters(indent_string.c_str(), new_indent);
  15851. o->write_characters("\"bytes\": [", 10);
  15852. if (!val.m_value.binary->empty())
  15853. {
  15854. for (auto i = val.m_value.binary->cbegin();
  15855. i != val.m_value.binary->cend() - 1; ++i)
  15856. {
  15857. dump_integer(*i);
  15858. o->write_characters(", ", 2);
  15859. }
  15860. dump_integer(val.m_value.binary->back());
  15861. }
  15862. o->write_characters("],\n", 3);
  15863. o->write_characters(indent_string.c_str(), new_indent);
  15864. o->write_characters("\"subtype\": ", 11);
  15865. if (val.m_value.binary->has_subtype())
  15866. {
  15867. dump_integer(val.m_value.binary->subtype());
  15868. }
  15869. else
  15870. {
  15871. o->write_characters("null", 4);
  15872. }
  15873. o->write_character('\n');
  15874. o->write_characters(indent_string.c_str(), current_indent);
  15875. o->write_character('}');
  15876. }
  15877. else
  15878. {
  15879. o->write_characters("{\"bytes\":[", 10);
  15880. if (!val.m_value.binary->empty())
  15881. {
  15882. for (auto i = val.m_value.binary->cbegin();
  15883. i != val.m_value.binary->cend() - 1; ++i)
  15884. {
  15885. dump_integer(*i);
  15886. o->write_character(',');
  15887. }
  15888. dump_integer(val.m_value.binary->back());
  15889. }
  15890. o->write_characters("],\"subtype\":", 12);
  15891. if (val.m_value.binary->has_subtype())
  15892. {
  15893. dump_integer(val.m_value.binary->subtype());
  15894. o->write_character('}');
  15895. }
  15896. else
  15897. {
  15898. o->write_characters("null}", 5);
  15899. }
  15900. }
  15901. return;
  15902. }
  15903. case value_t::boolean:
  15904. {
  15905. if (val.m_value.boolean)
  15906. {
  15907. o->write_characters("true", 4);
  15908. }
  15909. else
  15910. {
  15911. o->write_characters("false", 5);
  15912. }
  15913. return;
  15914. }
  15915. case value_t::number_integer:
  15916. {
  15917. dump_integer(val.m_value.number_integer);
  15918. return;
  15919. }
  15920. case value_t::number_unsigned:
  15921. {
  15922. dump_integer(val.m_value.number_unsigned);
  15923. return;
  15924. }
  15925. case value_t::number_float:
  15926. {
  15927. dump_float(val.m_value.number_float);
  15928. return;
  15929. }
  15930. case value_t::discarded:
  15931. {
  15932. o->write_characters("<discarded>", 11);
  15933. return;
  15934. }
  15935. case value_t::null:
  15936. {
  15937. o->write_characters("null", 4);
  15938. return;
  15939. }
  15940. default: // LCOV_EXCL_LINE
  15941. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15942. }
  15943. }
  15944. JSON_PRIVATE_UNLESS_TESTED:
  15945. /*!
  15946. @brief dump escaped string
  15947. Escape a string by replacing certain special characters by a sequence of an
  15948. escape character (backslash) and another character and other control
  15949. characters by a sequence of "\u" followed by a four-digit hex
  15950. representation. The escaped string is written to output stream @a o.
  15951. @param[in] s the string to escape
  15952. @param[in] ensure_ascii whether to escape non-ASCII characters with
  15953. \uXXXX sequences
  15954. @complexity Linear in the length of string @a s.
  15955. */
  15956. void dump_escaped(const string_t& s, const bool ensure_ascii)
  15957. {
  15958. std::uint32_t codepoint{};
  15959. std::uint8_t state = UTF8_ACCEPT;
  15960. std::size_t bytes = 0; // number of bytes written to string_buffer
  15961. // number of bytes written at the point of the last valid byte
  15962. std::size_t bytes_after_last_accept = 0;
  15963. std::size_t undumped_chars = 0;
  15964. for (std::size_t i = 0; i < s.size(); ++i)
  15965. {
  15966. const auto byte = static_cast<std::uint8_t>(s[i]);
  15967. switch (decode(state, codepoint, byte))
  15968. {
  15969. case UTF8_ACCEPT: // decode found a new code point
  15970. {
  15971. switch (codepoint)
  15972. {
  15973. case 0x08: // backspace
  15974. {
  15975. string_buffer[bytes++] = '\\';
  15976. string_buffer[bytes++] = 'b';
  15977. break;
  15978. }
  15979. case 0x09: // horizontal tab
  15980. {
  15981. string_buffer[bytes++] = '\\';
  15982. string_buffer[bytes++] = 't';
  15983. break;
  15984. }
  15985. case 0x0A: // newline
  15986. {
  15987. string_buffer[bytes++] = '\\';
  15988. string_buffer[bytes++] = 'n';
  15989. break;
  15990. }
  15991. case 0x0C: // formfeed
  15992. {
  15993. string_buffer[bytes++] = '\\';
  15994. string_buffer[bytes++] = 'f';
  15995. break;
  15996. }
  15997. case 0x0D: // carriage return
  15998. {
  15999. string_buffer[bytes++] = '\\';
  16000. string_buffer[bytes++] = 'r';
  16001. break;
  16002. }
  16003. case 0x22: // quotation mark
  16004. {
  16005. string_buffer[bytes++] = '\\';
  16006. string_buffer[bytes++] = '\"';
  16007. break;
  16008. }
  16009. case 0x5C: // reverse solidus
  16010. {
  16011. string_buffer[bytes++] = '\\';
  16012. string_buffer[bytes++] = '\\';
  16013. break;
  16014. }
  16015. default:
  16016. {
  16017. // escape control characters (0x00..0x1F) or, if
  16018. // ensure_ascii parameter is used, non-ASCII characters
  16019. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  16020. {
  16021. if (codepoint <= 0xFFFF)
  16022. {
  16023. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16024. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  16025. static_cast<std::uint16_t>(codepoint)));
  16026. bytes += 6;
  16027. }
  16028. else
  16029. {
  16030. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16031. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16032. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16033. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16034. bytes += 12;
  16035. }
  16036. }
  16037. else
  16038. {
  16039. // copy byte to buffer (all previous bytes
  16040. // been copied have in default case above)
  16041. string_buffer[bytes++] = s[i];
  16042. }
  16043. break;
  16044. }
  16045. }
  16046. // write buffer and reset index; there must be 13 bytes
  16047. // left, as this is the maximal number of bytes to be
  16048. // written ("\uxxxx\uxxxx\0") for one code point
  16049. if (string_buffer.size() - bytes < 13)
  16050. {
  16051. o->write_characters(string_buffer.data(), bytes);
  16052. bytes = 0;
  16053. }
  16054. // remember the byte position of this accept
  16055. bytes_after_last_accept = bytes;
  16056. undumped_chars = 0;
  16057. break;
  16058. }
  16059. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16060. {
  16061. switch (error_handler)
  16062. {
  16063. case error_handler_t::strict:
  16064. {
  16065. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16066. }
  16067. case error_handler_t::ignore:
  16068. case error_handler_t::replace:
  16069. {
  16070. // in case we saw this character the first time, we
  16071. // would like to read it again, because the byte
  16072. // may be OK for itself, but just not OK for the
  16073. // previous sequence
  16074. if (undumped_chars > 0)
  16075. {
  16076. --i;
  16077. }
  16078. // reset length buffer to the last accepted index;
  16079. // thus removing/ignoring the invalid characters
  16080. bytes = bytes_after_last_accept;
  16081. if (error_handler == error_handler_t::replace)
  16082. {
  16083. // add a replacement character
  16084. if (ensure_ascii)
  16085. {
  16086. string_buffer[bytes++] = '\\';
  16087. string_buffer[bytes++] = 'u';
  16088. string_buffer[bytes++] = 'f';
  16089. string_buffer[bytes++] = 'f';
  16090. string_buffer[bytes++] = 'f';
  16091. string_buffer[bytes++] = 'd';
  16092. }
  16093. else
  16094. {
  16095. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16096. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16097. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16098. }
  16099. // write buffer and reset index; there must be 13 bytes
  16100. // left, as this is the maximal number of bytes to be
  16101. // written ("\uxxxx\uxxxx\0") for one code point
  16102. if (string_buffer.size() - bytes < 13)
  16103. {
  16104. o->write_characters(string_buffer.data(), bytes);
  16105. bytes = 0;
  16106. }
  16107. bytes_after_last_accept = bytes;
  16108. }
  16109. undumped_chars = 0;
  16110. // continue processing the string
  16111. state = UTF8_ACCEPT;
  16112. break;
  16113. }
  16114. default: // LCOV_EXCL_LINE
  16115. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16116. }
  16117. break;
  16118. }
  16119. default: // decode found yet incomplete multi-byte code point
  16120. {
  16121. if (!ensure_ascii)
  16122. {
  16123. // code point will not be escaped - copy byte to buffer
  16124. string_buffer[bytes++] = s[i];
  16125. }
  16126. ++undumped_chars;
  16127. break;
  16128. }
  16129. }
  16130. }
  16131. // we finished processing the string
  16132. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  16133. {
  16134. // write buffer
  16135. if (bytes > 0)
  16136. {
  16137. o->write_characters(string_buffer.data(), bytes);
  16138. }
  16139. }
  16140. else
  16141. {
  16142. // we finish reading, but do not accept: string was incomplete
  16143. switch (error_handler)
  16144. {
  16145. case error_handler_t::strict:
  16146. {
  16147. 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));
  16148. }
  16149. case error_handler_t::ignore:
  16150. {
  16151. // write all accepted bytes
  16152. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16153. break;
  16154. }
  16155. case error_handler_t::replace:
  16156. {
  16157. // write all accepted bytes
  16158. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16159. // add a replacement character
  16160. if (ensure_ascii)
  16161. {
  16162. o->write_characters("\\ufffd", 6);
  16163. }
  16164. else
  16165. {
  16166. o->write_characters("\xEF\xBF\xBD", 3);
  16167. }
  16168. break;
  16169. }
  16170. default: // LCOV_EXCL_LINE
  16171. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16172. }
  16173. }
  16174. }
  16175. private:
  16176. /*!
  16177. @brief count digits
  16178. Count the number of decimal (base 10) digits for an input unsigned integer.
  16179. @param[in] x unsigned integer number to count its digits
  16180. @return number of decimal digits
  16181. */
  16182. inline unsigned int count_digits(number_unsigned_t x) noexcept
  16183. {
  16184. unsigned int n_digits = 1;
  16185. for (;;)
  16186. {
  16187. if (x < 10)
  16188. {
  16189. return n_digits;
  16190. }
  16191. if (x < 100)
  16192. {
  16193. return n_digits + 1;
  16194. }
  16195. if (x < 1000)
  16196. {
  16197. return n_digits + 2;
  16198. }
  16199. if (x < 10000)
  16200. {
  16201. return n_digits + 3;
  16202. }
  16203. x = x / 10000u;
  16204. n_digits += 4;
  16205. }
  16206. }
  16207. /*!
  16208. * @brief convert a byte to a uppercase hex representation
  16209. * @param[in] byte byte to represent
  16210. * @return representation ("00".."FF")
  16211. */
  16212. static std::string hex_bytes(std::uint8_t byte)
  16213. {
  16214. std::string result = "FF";
  16215. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  16216. result[0] = nibble_to_hex[byte / 16];
  16217. result[1] = nibble_to_hex[byte % 16];
  16218. return result;
  16219. }
  16220. // templates to avoid warnings about useless casts
  16221. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  16222. bool is_negative_number(NumberType x)
  16223. {
  16224. return x < 0;
  16225. }
  16226. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  16227. bool is_negative_number(NumberType /*unused*/)
  16228. {
  16229. return false;
  16230. }
  16231. /*!
  16232. @brief dump an integer
  16233. Dump a given integer to output stream @a o. Works internally with
  16234. @a number_buffer.
  16235. @param[in] x integer number (signed or unsigned) to dump
  16236. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  16237. */
  16238. template < typename NumberType, detail::enable_if_t <
  16239. std::is_integral<NumberType>::value ||
  16240. std::is_same<NumberType, number_unsigned_t>::value ||
  16241. std::is_same<NumberType, number_integer_t>::value ||
  16242. std::is_same<NumberType, binary_char_t>::value,
  16243. int > = 0 >
  16244. void dump_integer(NumberType x)
  16245. {
  16246. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  16247. {
  16248. {
  16249. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  16250. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  16251. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  16252. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  16253. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  16254. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  16255. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  16256. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  16257. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  16258. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  16259. }
  16260. };
  16261. // special case for "0"
  16262. if (x == 0)
  16263. {
  16264. o->write_character('0');
  16265. return;
  16266. }
  16267. // use a pointer to fill the buffer
  16268. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16269. number_unsigned_t abs_value;
  16270. unsigned int n_chars{};
  16271. if (is_negative_number(x))
  16272. {
  16273. *buffer_ptr = '-';
  16274. abs_value = remove_sign(static_cast<number_integer_t>(x));
  16275. // account one more byte for the minus sign
  16276. n_chars = 1 + count_digits(abs_value);
  16277. }
  16278. else
  16279. {
  16280. abs_value = static_cast<number_unsigned_t>(x);
  16281. n_chars = count_digits(abs_value);
  16282. }
  16283. // spare 1 byte for '\0'
  16284. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  16285. // jump to the end to generate the string from backward,
  16286. // so we later avoid reversing the result
  16287. buffer_ptr += n_chars;
  16288. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  16289. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  16290. while (abs_value >= 100)
  16291. {
  16292. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  16293. abs_value /= 100;
  16294. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16295. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16296. }
  16297. if (abs_value >= 10)
  16298. {
  16299. const auto digits_index = static_cast<unsigned>(abs_value);
  16300. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16301. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16302. }
  16303. else
  16304. {
  16305. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  16306. }
  16307. o->write_characters(number_buffer.data(), n_chars);
  16308. }
  16309. /*!
  16310. @brief dump a floating-point number
  16311. Dump a given floating-point number to output stream @a o. Works internally
  16312. with @a number_buffer.
  16313. @param[in] x floating-point number to dump
  16314. */
  16315. void dump_float(number_float_t x)
  16316. {
  16317. // NaN / inf
  16318. if (!std::isfinite(x))
  16319. {
  16320. o->write_characters("null", 4);
  16321. return;
  16322. }
  16323. // If number_float_t is an IEEE-754 single or double precision number,
  16324. // use the Grisu2 algorithm to produce short numbers which are
  16325. // guaranteed to round-trip, using strtof and strtod, resp.
  16326. //
  16327. // NB: The test below works if <long double> == <double>.
  16328. static constexpr bool is_ieee_single_or_double
  16329. = (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) ||
  16330. (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);
  16331. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  16332. }
  16333. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  16334. {
  16335. auto* begin = number_buffer.data();
  16336. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  16337. o->write_characters(begin, static_cast<size_t>(end - begin));
  16338. }
  16339. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  16340. {
  16341. // get number of digits for a float -> text -> float round-trip
  16342. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  16343. // the actual conversion
  16344. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16345. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  16346. // negative value indicates an error
  16347. JSON_ASSERT(len > 0);
  16348. // check if buffer was large enough
  16349. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  16350. // erase thousands separator
  16351. if (thousands_sep != '\0')
  16352. {
  16353. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16354. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  16355. std::fill(end, number_buffer.end(), '\0');
  16356. JSON_ASSERT((end - number_buffer.begin()) <= len);
  16357. len = (end - number_buffer.begin());
  16358. }
  16359. // convert decimal point to '.'
  16360. if (decimal_point != '\0' && decimal_point != '.')
  16361. {
  16362. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16363. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  16364. if (dec_pos != number_buffer.end())
  16365. {
  16366. *dec_pos = '.';
  16367. }
  16368. }
  16369. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  16370. // determine if we need to append ".0"
  16371. const bool value_is_int_like =
  16372. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  16373. [](char c)
  16374. {
  16375. return c == '.' || c == 'e';
  16376. });
  16377. if (value_is_int_like)
  16378. {
  16379. o->write_characters(".0", 2);
  16380. }
  16381. }
  16382. /*!
  16383. @brief check whether a string is UTF-8 encoded
  16384. The function checks each byte of a string whether it is UTF-8 encoded. The
  16385. result of the check is stored in the @a state parameter. The function must
  16386. be called initially with state 0 (accept). State 1 means the string must
  16387. be rejected, because the current byte is not allowed. If the string is
  16388. completely processed, but the state is non-zero, the string ended
  16389. prematurely; that is, the last byte indicated more bytes should have
  16390. followed.
  16391. @param[in,out] state the state of the decoding
  16392. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  16393. @param[in] byte next byte to decode
  16394. @return new state
  16395. @note The function has been edited: a std::array is used.
  16396. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  16397. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  16398. */
  16399. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  16400. {
  16401. static const std::array<std::uint8_t, 400> utf8d =
  16402. {
  16403. {
  16404. 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
  16405. 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
  16406. 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
  16407. 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
  16408. 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
  16409. 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
  16410. 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
  16411. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  16412. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  16413. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  16414. 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
  16415. 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
  16416. 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
  16417. 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
  16418. }
  16419. };
  16420. JSON_ASSERT(byte < utf8d.size());
  16421. const std::uint8_t type = utf8d[byte];
  16422. codep = (state != UTF8_ACCEPT)
  16423. ? (byte & 0x3fu) | (codep << 6u)
  16424. : (0xFFu >> type) & (byte);
  16425. const std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  16426. JSON_ASSERT(index < 400);
  16427. state = utf8d[index];
  16428. return state;
  16429. }
  16430. /*
  16431. * Overload to make the compiler happy while it is instantiating
  16432. * dump_integer for number_unsigned_t.
  16433. * Must never be called.
  16434. */
  16435. number_unsigned_t remove_sign(number_unsigned_t x)
  16436. {
  16437. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16438. return x; // LCOV_EXCL_LINE
  16439. }
  16440. /*
  16441. * Helper function for dump_integer
  16442. *
  16443. * This function takes a negative signed integer and returns its absolute
  16444. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  16445. * not directly remove the sign of an arbitrary signed integer as the
  16446. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  16447. * #1708 for details.
  16448. */
  16449. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  16450. {
  16451. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  16452. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  16453. }
  16454. private:
  16455. /// the output of the serializer
  16456. output_adapter_t<char> o = nullptr;
  16457. /// a (hopefully) large enough character buffer
  16458. std::array<char, 64> number_buffer{{}};
  16459. /// the locale
  16460. const std::lconv* loc = nullptr;
  16461. /// the locale's thousand separator character
  16462. const char thousands_sep = '\0';
  16463. /// the locale's decimal point character
  16464. const char decimal_point = '\0';
  16465. /// string buffer
  16466. std::array<char, 512> string_buffer{{}};
  16467. /// the indentation character
  16468. const char indent_char;
  16469. /// the indentation string
  16470. string_t indent_string;
  16471. /// error_handler how to react on decoding errors
  16472. const error_handler_t error_handler;
  16473. };
  16474. } // namespace detail
  16475. NLOHMANN_JSON_NAMESPACE_END
  16476. // #include <nlohmann/detail/value_t.hpp>
  16477. // #include <nlohmann/json_fwd.hpp>
  16478. // #include <nlohmann/ordered_map.hpp>
  16479. // __ _____ _____ _____
  16480. // __| | __| | | | JSON for Modern C++
  16481. // | | |__ | | | | | | version 3.11.2
  16482. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  16483. //
  16484. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  16485. // SPDX-License-Identifier: MIT
  16486. #include <functional> // equal_to, less
  16487. #include <initializer_list> // initializer_list
  16488. #include <iterator> // input_iterator_tag, iterator_traits
  16489. #include <memory> // allocator
  16490. #include <stdexcept> // for out_of_range
  16491. #include <type_traits> // enable_if, is_convertible
  16492. #include <utility> // pair
  16493. #include <vector> // vector
  16494. // #include <nlohmann/detail/macro_scope.hpp>
  16495. // #include <nlohmann/detail/meta/type_traits.hpp>
  16496. NLOHMANN_JSON_NAMESPACE_BEGIN
  16497. /// ordered_map: a minimal map-like container that preserves insertion order
  16498. /// for use within nlohmann::basic_json<ordered_map>
  16499. template <class Key, class T, class IgnoredLess = std::less<Key>,
  16500. class Allocator = std::allocator<std::pair<const Key, T>>>
  16501. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  16502. {
  16503. using key_type = Key;
  16504. using mapped_type = T;
  16505. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  16506. using iterator = typename Container::iterator;
  16507. using const_iterator = typename Container::const_iterator;
  16508. using size_type = typename Container::size_type;
  16509. using value_type = typename Container::value_type;
  16510. #ifdef JSON_HAS_CPP_14
  16511. using key_compare = std::equal_to<>;
  16512. #else
  16513. using key_compare = std::equal_to<Key>;
  16514. #endif
  16515. // Explicit constructors instead of `using Container::Container`
  16516. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  16517. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  16518. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  16519. template <class It>
  16520. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  16521. : Container{first, last, alloc} {}
  16522. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  16523. : Container{init, alloc} {}
  16524. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  16525. {
  16526. for (auto it = this->begin(); it != this->end(); ++it)
  16527. {
  16528. if (m_compare(it->first, key))
  16529. {
  16530. return {it, false};
  16531. }
  16532. }
  16533. Container::emplace_back(key, std::forward<T>(t));
  16534. return {std::prev(this->end()), true};
  16535. }
  16536. template<class KeyType, detail::enable_if_t<
  16537. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16538. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  16539. {
  16540. for (auto it = this->begin(); it != this->end(); ++it)
  16541. {
  16542. if (m_compare(it->first, key))
  16543. {
  16544. return {it, false};
  16545. }
  16546. }
  16547. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  16548. return {std::prev(this->end()), true};
  16549. }
  16550. T& operator[](const key_type& key)
  16551. {
  16552. return emplace(key, T{}).first->second;
  16553. }
  16554. template<class KeyType, detail::enable_if_t<
  16555. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16556. T & operator[](KeyType && key)
  16557. {
  16558. return emplace(std::forward<KeyType>(key), T{}).first->second;
  16559. }
  16560. const T& operator[](const key_type& key) const
  16561. {
  16562. return at(key);
  16563. }
  16564. template<class KeyType, detail::enable_if_t<
  16565. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16566. const T & operator[](KeyType && key) const
  16567. {
  16568. return at(std::forward<KeyType>(key));
  16569. }
  16570. T& at(const key_type& key)
  16571. {
  16572. for (auto it = this->begin(); it != this->end(); ++it)
  16573. {
  16574. if (m_compare(it->first, key))
  16575. {
  16576. return it->second;
  16577. }
  16578. }
  16579. JSON_THROW(std::out_of_range("key not found"));
  16580. }
  16581. template<class KeyType, detail::enable_if_t<
  16582. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16583. T & at(KeyType && key)
  16584. {
  16585. for (auto it = this->begin(); it != this->end(); ++it)
  16586. {
  16587. if (m_compare(it->first, key))
  16588. {
  16589. return it->second;
  16590. }
  16591. }
  16592. JSON_THROW(std::out_of_range("key not found"));
  16593. }
  16594. const T& at(const key_type& key) const
  16595. {
  16596. for (auto it = this->begin(); it != this->end(); ++it)
  16597. {
  16598. if (m_compare(it->first, key))
  16599. {
  16600. return it->second;
  16601. }
  16602. }
  16603. JSON_THROW(std::out_of_range("key not found"));
  16604. }
  16605. template<class KeyType, detail::enable_if_t<
  16606. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16607. const T & at(KeyType && key) const
  16608. {
  16609. for (auto it = this->begin(); it != this->end(); ++it)
  16610. {
  16611. if (m_compare(it->first, key))
  16612. {
  16613. return it->second;
  16614. }
  16615. }
  16616. JSON_THROW(std::out_of_range("key not found"));
  16617. }
  16618. size_type erase(const key_type& key)
  16619. {
  16620. for (auto it = this->begin(); it != this->end(); ++it)
  16621. {
  16622. if (m_compare(it->first, key))
  16623. {
  16624. // Since we cannot move const Keys, re-construct them in place
  16625. for (auto next = it; ++next != this->end(); ++it)
  16626. {
  16627. it->~value_type(); // Destroy but keep allocation
  16628. new (&*it) value_type{std::move(*next)};
  16629. }
  16630. Container::pop_back();
  16631. return 1;
  16632. }
  16633. }
  16634. return 0;
  16635. }
  16636. template<class KeyType, detail::enable_if_t<
  16637. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16638. size_type erase(KeyType && key)
  16639. {
  16640. for (auto it = this->begin(); it != this->end(); ++it)
  16641. {
  16642. if (m_compare(it->first, key))
  16643. {
  16644. // Since we cannot move const Keys, re-construct them in place
  16645. for (auto next = it; ++next != this->end(); ++it)
  16646. {
  16647. it->~value_type(); // Destroy but keep allocation
  16648. new (&*it) value_type{std::move(*next)};
  16649. }
  16650. Container::pop_back();
  16651. return 1;
  16652. }
  16653. }
  16654. return 0;
  16655. }
  16656. iterator erase(iterator pos)
  16657. {
  16658. return erase(pos, std::next(pos));
  16659. }
  16660. iterator erase(iterator first, iterator last)
  16661. {
  16662. if (first == last)
  16663. {
  16664. return first;
  16665. }
  16666. const auto elements_affected = std::distance(first, last);
  16667. const auto offset = std::distance(Container::begin(), first);
  16668. // This is the start situation. We need to delete elements_affected
  16669. // elements (3 in this example: e, f, g), and need to return an
  16670. // iterator past the last deleted element (h in this example).
  16671. // Note that offset is the distance from the start of the vector
  16672. // to first. We will need this later.
  16673. // [ a, b, c, d, e, f, g, h, i, j ]
  16674. // ^ ^
  16675. // first last
  16676. // Since we cannot move const Keys, we re-construct them in place.
  16677. // We start at first and re-construct (viz. copy) the elements from
  16678. // the back of the vector. Example for first iteration:
  16679. // ,--------.
  16680. // v | destroy e and re-construct with h
  16681. // [ a, b, c, d, e, f, g, h, i, j ]
  16682. // ^ ^
  16683. // it it + elements_affected
  16684. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  16685. {
  16686. it->~value_type(); // destroy but keep allocation
  16687. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  16688. }
  16689. // [ a, b, c, d, h, i, j, h, i, j ]
  16690. // ^ ^
  16691. // first last
  16692. // remove the unneeded elements at the end of the vector
  16693. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  16694. // [ a, b, c, d, h, i, j ]
  16695. // ^ ^
  16696. // first last
  16697. // first is now pointing past the last deleted element, but we cannot
  16698. // use this iterator, because it may have been invalidated by the
  16699. // resize call. Instead, we can return begin() + offset.
  16700. return Container::begin() + offset;
  16701. }
  16702. size_type count(const key_type& key) const
  16703. {
  16704. for (auto it = this->begin(); it != this->end(); ++it)
  16705. {
  16706. if (m_compare(it->first, key))
  16707. {
  16708. return 1;
  16709. }
  16710. }
  16711. return 0;
  16712. }
  16713. template<class KeyType, detail::enable_if_t<
  16714. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16715. size_type count(KeyType && key) const
  16716. {
  16717. for (auto it = this->begin(); it != this->end(); ++it)
  16718. {
  16719. if (m_compare(it->first, key))
  16720. {
  16721. return 1;
  16722. }
  16723. }
  16724. return 0;
  16725. }
  16726. iterator find(const key_type& key)
  16727. {
  16728. for (auto it = this->begin(); it != this->end(); ++it)
  16729. {
  16730. if (m_compare(it->first, key))
  16731. {
  16732. return it;
  16733. }
  16734. }
  16735. return Container::end();
  16736. }
  16737. template<class KeyType, detail::enable_if_t<
  16738. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16739. iterator find(KeyType && key)
  16740. {
  16741. for (auto it = this->begin(); it != this->end(); ++it)
  16742. {
  16743. if (m_compare(it->first, key))
  16744. {
  16745. return it;
  16746. }
  16747. }
  16748. return Container::end();
  16749. }
  16750. const_iterator find(const key_type& key) const
  16751. {
  16752. for (auto it = this->begin(); it != this->end(); ++it)
  16753. {
  16754. if (m_compare(it->first, key))
  16755. {
  16756. return it;
  16757. }
  16758. }
  16759. return Container::end();
  16760. }
  16761. std::pair<iterator, bool> insert( value_type&& value )
  16762. {
  16763. return emplace(value.first, std::move(value.second));
  16764. }
  16765. std::pair<iterator, bool> insert( const value_type& value )
  16766. {
  16767. for (auto it = this->begin(); it != this->end(); ++it)
  16768. {
  16769. if (m_compare(it->first, value.first))
  16770. {
  16771. return {it, false};
  16772. }
  16773. }
  16774. Container::push_back(value);
  16775. return {--this->end(), true};
  16776. }
  16777. template<typename InputIt>
  16778. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  16779. std::input_iterator_tag>::value>::type;
  16780. template<typename InputIt, typename = require_input_iter<InputIt>>
  16781. void insert(InputIt first, InputIt last)
  16782. {
  16783. for (auto it = first; it != last; ++it)
  16784. {
  16785. insert(*it);
  16786. }
  16787. }
  16788. private:
  16789. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  16790. };
  16791. NLOHMANN_JSON_NAMESPACE_END
  16792. #if defined(JSON_HAS_CPP_17)
  16793. #include <any>
  16794. #include <string_view>
  16795. #endif
  16796. /*!
  16797. @brief namespace for Niels Lohmann
  16798. @see https://github.com/nlohmann
  16799. @since version 1.0.0
  16800. */
  16801. NLOHMANN_JSON_NAMESPACE_BEGIN
  16802. /*!
  16803. @brief a class to store JSON values
  16804. @internal
  16805. @invariant The member variables @a m_value and @a m_type have the following
  16806. relationship:
  16807. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  16808. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  16809. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  16810. The invariants are checked by member function assert_invariant().
  16811. @note ObjectType trick from https://stackoverflow.com/a/9860911
  16812. @endinternal
  16813. @since version 1.0.0
  16814. @nosubgrouping
  16815. */
  16816. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  16817. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  16818. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  16819. {
  16820. private:
  16821. template<detail::value_t> friend struct detail::external_constructor;
  16822. template<typename>
  16823. friend class ::nlohmann::json_pointer;
  16824. // can be restored when json_pointer backwards compatibility is removed
  16825. // friend ::nlohmann::json_pointer<StringType>;
  16826. template<typename BasicJsonType, typename InputType>
  16827. friend class ::nlohmann::detail::parser;
  16828. friend ::nlohmann::detail::serializer<basic_json>;
  16829. template<typename BasicJsonType>
  16830. friend class ::nlohmann::detail::iter_impl;
  16831. template<typename BasicJsonType, typename CharType>
  16832. friend class ::nlohmann::detail::binary_writer;
  16833. template<typename BasicJsonType, typename InputType, typename SAX>
  16834. friend class ::nlohmann::detail::binary_reader;
  16835. template<typename BasicJsonType>
  16836. friend class ::nlohmann::detail::json_sax_dom_parser;
  16837. template<typename BasicJsonType>
  16838. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  16839. friend class ::nlohmann::detail::exception;
  16840. /// workaround type for MSVC
  16841. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  16842. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  16843. JSON_PRIVATE_UNLESS_TESTED:
  16844. // convenience aliases for types residing in namespace detail;
  16845. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  16846. template<typename InputAdapterType>
  16847. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  16848. InputAdapterType adapter,
  16849. detail::parser_callback_t<basic_json>cb = nullptr,
  16850. const bool allow_exceptions = true,
  16851. const bool ignore_comments = false
  16852. )
  16853. {
  16854. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  16855. std::move(cb), allow_exceptions, ignore_comments);
  16856. }
  16857. private:
  16858. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  16859. template<typename BasicJsonType>
  16860. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  16861. template<typename BasicJsonType>
  16862. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  16863. template<typename Iterator>
  16864. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  16865. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  16866. template<typename CharType>
  16867. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  16868. template<typename InputType>
  16869. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  16870. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  16871. JSON_PRIVATE_UNLESS_TESTED:
  16872. using serializer = ::nlohmann::detail::serializer<basic_json>;
  16873. public:
  16874. using value_t = detail::value_t;
  16875. /// JSON Pointer, see @ref nlohmann::json_pointer
  16876. using json_pointer = ::nlohmann::json_pointer<StringType>;
  16877. template<typename T, typename SFINAE>
  16878. using json_serializer = JSONSerializer<T, SFINAE>;
  16879. /// how to treat decoding errors
  16880. using error_handler_t = detail::error_handler_t;
  16881. /// how to treat CBOR tags
  16882. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  16883. /// helper type for initializer lists of basic_json values
  16884. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  16885. using input_format_t = detail::input_format_t;
  16886. /// SAX interface type, see @ref nlohmann::json_sax
  16887. using json_sax_t = json_sax<basic_json>;
  16888. ////////////////
  16889. // exceptions //
  16890. ////////////////
  16891. /// @name exceptions
  16892. /// Classes to implement user-defined exceptions.
  16893. /// @{
  16894. using exception = detail::exception;
  16895. using parse_error = detail::parse_error;
  16896. using invalid_iterator = detail::invalid_iterator;
  16897. using type_error = detail::type_error;
  16898. using out_of_range = detail::out_of_range;
  16899. using other_error = detail::other_error;
  16900. /// @}
  16901. /////////////////////
  16902. // container types //
  16903. /////////////////////
  16904. /// @name container types
  16905. /// The canonic container types to use @ref basic_json like any other STL
  16906. /// container.
  16907. /// @{
  16908. /// the type of elements in a basic_json container
  16909. using value_type = basic_json;
  16910. /// the type of an element reference
  16911. using reference = value_type&;
  16912. /// the type of an element const reference
  16913. using const_reference = const value_type&;
  16914. /// a type to represent differences between iterators
  16915. using difference_type = std::ptrdiff_t;
  16916. /// a type to represent container sizes
  16917. using size_type = std::size_t;
  16918. /// the allocator type
  16919. using allocator_type = AllocatorType<basic_json>;
  16920. /// the type of an element pointer
  16921. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  16922. /// the type of an element const pointer
  16923. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  16924. /// an iterator for a basic_json container
  16925. using iterator = iter_impl<basic_json>;
  16926. /// a const iterator for a basic_json container
  16927. using const_iterator = iter_impl<const basic_json>;
  16928. /// a reverse iterator for a basic_json container
  16929. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  16930. /// a const reverse iterator for a basic_json container
  16931. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  16932. /// @}
  16933. /// @brief returns the allocator associated with the container
  16934. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  16935. static allocator_type get_allocator()
  16936. {
  16937. return allocator_type();
  16938. }
  16939. /// @brief returns version information on the library
  16940. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  16941. JSON_HEDLEY_WARN_UNUSED_RESULT
  16942. static basic_json meta()
  16943. {
  16944. basic_json result;
  16945. result["copyright"] = "(C) 2013-2022 Niels Lohmann";
  16946. result["name"] = "JSON for Modern C++";
  16947. result["url"] = "https://github.com/nlohmann/json";
  16948. result["version"]["string"] =
  16949. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  16950. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  16951. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  16952. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  16953. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  16954. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  16955. #ifdef _WIN32
  16956. result["platform"] = "win32";
  16957. #elif defined __linux__
  16958. result["platform"] = "linux";
  16959. #elif defined __APPLE__
  16960. result["platform"] = "apple";
  16961. #elif defined __unix__
  16962. result["platform"] = "unix";
  16963. #else
  16964. result["platform"] = "unknown";
  16965. #endif
  16966. #if defined(__ICC) || defined(__INTEL_COMPILER)
  16967. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  16968. #elif defined(__clang__)
  16969. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  16970. #elif defined(__GNUC__) || defined(__GNUG__)
  16971. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  16972. std::to_string(__GNUC__), '.',
  16973. std::to_string(__GNUC_MINOR__), '.',
  16974. std::to_string(__GNUC_PATCHLEVEL__))
  16975. }
  16976. };
  16977. #elif defined(__HP_cc) || defined(__HP_aCC)
  16978. result["compiler"] = "hp"
  16979. #elif defined(__IBMCPP__)
  16980. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  16981. #elif defined(_MSC_VER)
  16982. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  16983. #elif defined(__PGI)
  16984. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  16985. #elif defined(__SUNPRO_CC)
  16986. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  16987. #else
  16988. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  16989. #endif
  16990. #if defined(_MSVC_LANG)
  16991. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  16992. #elif defined(__cplusplus)
  16993. result["compiler"]["c++"] = std::to_string(__cplusplus);
  16994. #else
  16995. result["compiler"]["c++"] = "unknown";
  16996. #endif
  16997. return result;
  16998. }
  16999. ///////////////////////////
  17000. // JSON value data types //
  17001. ///////////////////////////
  17002. /// @name JSON value data types
  17003. /// The data types to store a JSON value. These types are derived from
  17004. /// the template arguments passed to class @ref basic_json.
  17005. /// @{
  17006. /// @brief default object key comparator type
  17007. /// The actual object key comparator type (@ref object_comparator_t) may be
  17008. /// different.
  17009. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  17010. #if defined(JSON_HAS_CPP_14)
  17011. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  17012. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  17013. using default_object_comparator_t = std::less<>;
  17014. #else
  17015. using default_object_comparator_t = std::less<StringType>;
  17016. #endif
  17017. /// @brief a type for an object
  17018. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  17019. using object_t = ObjectType<StringType,
  17020. basic_json,
  17021. default_object_comparator_t,
  17022. AllocatorType<std::pair<const StringType,
  17023. basic_json>>>;
  17024. /// @brief a type for an array
  17025. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  17026. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  17027. /// @brief a type for a string
  17028. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  17029. using string_t = StringType;
  17030. /// @brief a type for a boolean
  17031. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17032. using boolean_t = BooleanType;
  17033. /// @brief a type for a number (integer)
  17034. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17035. using number_integer_t = NumberIntegerType;
  17036. /// @brief a type for a number (unsigned)
  17037. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17038. using number_unsigned_t = NumberUnsignedType;
  17039. /// @brief a type for a number (floating-point)
  17040. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17041. using number_float_t = NumberFloatType;
  17042. /// @brief a type for a packed binary type
  17043. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17044. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17045. /// @brief object key comparator type
  17046. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17047. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17048. /// @}
  17049. private:
  17050. /// helper for exception-safe object creation
  17051. template<typename T, typename... Args>
  17052. JSON_HEDLEY_RETURNS_NON_NULL
  17053. static T* create(Args&& ... args)
  17054. {
  17055. AllocatorType<T> alloc;
  17056. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17057. auto deleter = [&](T * obj)
  17058. {
  17059. AllocatorTraits::deallocate(alloc, obj, 1);
  17060. };
  17061. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17062. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17063. JSON_ASSERT(obj != nullptr);
  17064. return obj.release();
  17065. }
  17066. ////////////////////////
  17067. // JSON value storage //
  17068. ////////////////////////
  17069. JSON_PRIVATE_UNLESS_TESTED:
  17070. /*!
  17071. @brief a JSON value
  17072. The actual storage for a JSON value of the @ref basic_json class. This
  17073. union combines the different storage types for the JSON value types
  17074. defined in @ref value_t.
  17075. JSON type | value_t type | used type
  17076. --------- | --------------- | ------------------------
  17077. object | object | pointer to @ref object_t
  17078. array | array | pointer to @ref array_t
  17079. string | string | pointer to @ref string_t
  17080. boolean | boolean | @ref boolean_t
  17081. number | number_integer | @ref number_integer_t
  17082. number | number_unsigned | @ref number_unsigned_t
  17083. number | number_float | @ref number_float_t
  17084. binary | binary | pointer to @ref binary_t
  17085. null | null | *no value is stored*
  17086. @note Variable-length types (objects, arrays, and strings) are stored as
  17087. pointers. The size of the union should not exceed 64 bits if the default
  17088. value types are used.
  17089. @since version 1.0.0
  17090. */
  17091. union json_value
  17092. {
  17093. /// object (stored with pointer to save storage)
  17094. object_t* object;
  17095. /// array (stored with pointer to save storage)
  17096. array_t* array;
  17097. /// string (stored with pointer to save storage)
  17098. string_t* string;
  17099. /// binary (stored with pointer to save storage)
  17100. binary_t* binary;
  17101. /// boolean
  17102. boolean_t boolean;
  17103. /// number (integer)
  17104. number_integer_t number_integer;
  17105. /// number (unsigned integer)
  17106. number_unsigned_t number_unsigned;
  17107. /// number (floating-point)
  17108. number_float_t number_float;
  17109. /// default constructor (for null values)
  17110. json_value() = default;
  17111. /// constructor for booleans
  17112. json_value(boolean_t v) noexcept : boolean(v) {}
  17113. /// constructor for numbers (integer)
  17114. json_value(number_integer_t v) noexcept : number_integer(v) {}
  17115. /// constructor for numbers (unsigned)
  17116. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  17117. /// constructor for numbers (floating-point)
  17118. json_value(number_float_t v) noexcept : number_float(v) {}
  17119. /// constructor for empty values of a given type
  17120. json_value(value_t t)
  17121. {
  17122. switch (t)
  17123. {
  17124. case value_t::object:
  17125. {
  17126. object = create<object_t>();
  17127. break;
  17128. }
  17129. case value_t::array:
  17130. {
  17131. array = create<array_t>();
  17132. break;
  17133. }
  17134. case value_t::string:
  17135. {
  17136. string = create<string_t>("");
  17137. break;
  17138. }
  17139. case value_t::binary:
  17140. {
  17141. binary = create<binary_t>();
  17142. break;
  17143. }
  17144. case value_t::boolean:
  17145. {
  17146. boolean = static_cast<boolean_t>(false);
  17147. break;
  17148. }
  17149. case value_t::number_integer:
  17150. {
  17151. number_integer = static_cast<number_integer_t>(0);
  17152. break;
  17153. }
  17154. case value_t::number_unsigned:
  17155. {
  17156. number_unsigned = static_cast<number_unsigned_t>(0);
  17157. break;
  17158. }
  17159. case value_t::number_float:
  17160. {
  17161. number_float = static_cast<number_float_t>(0.0);
  17162. break;
  17163. }
  17164. case value_t::null:
  17165. {
  17166. object = nullptr; // silence warning, see #821
  17167. break;
  17168. }
  17169. case value_t::discarded:
  17170. default:
  17171. {
  17172. object = nullptr; // silence warning, see #821
  17173. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  17174. {
  17175. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.11.2", nullptr)); // LCOV_EXCL_LINE
  17176. }
  17177. break;
  17178. }
  17179. }
  17180. }
  17181. /// constructor for strings
  17182. json_value(const string_t& value) : string(create<string_t>(value)) {}
  17183. /// constructor for rvalue strings
  17184. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  17185. /// constructor for objects
  17186. json_value(const object_t& value) : object(create<object_t>(value)) {}
  17187. /// constructor for rvalue objects
  17188. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  17189. /// constructor for arrays
  17190. json_value(const array_t& value) : array(create<array_t>(value)) {}
  17191. /// constructor for rvalue arrays
  17192. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  17193. /// constructor for binary arrays
  17194. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  17195. /// constructor for rvalue binary arrays
  17196. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  17197. /// constructor for binary arrays (internal type)
  17198. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  17199. /// constructor for rvalue binary arrays (internal type)
  17200. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  17201. void destroy(value_t t)
  17202. {
  17203. if (t == value_t::array || t == value_t::object)
  17204. {
  17205. // flatten the current json_value to a heap-allocated stack
  17206. std::vector<basic_json> stack;
  17207. // move the top-level items to stack
  17208. if (t == value_t::array)
  17209. {
  17210. stack.reserve(array->size());
  17211. std::move(array->begin(), array->end(), std::back_inserter(stack));
  17212. }
  17213. else
  17214. {
  17215. stack.reserve(object->size());
  17216. for (auto&& it : *object)
  17217. {
  17218. stack.push_back(std::move(it.second));
  17219. }
  17220. }
  17221. while (!stack.empty())
  17222. {
  17223. // move the last item to local variable to be processed
  17224. basic_json current_item(std::move(stack.back()));
  17225. stack.pop_back();
  17226. // if current_item is array/object, move
  17227. // its children to the stack to be processed later
  17228. if (current_item.is_array())
  17229. {
  17230. std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
  17231. current_item.m_value.array->clear();
  17232. }
  17233. else if (current_item.is_object())
  17234. {
  17235. for (auto&& it : *current_item.m_value.object)
  17236. {
  17237. stack.push_back(std::move(it.second));
  17238. }
  17239. current_item.m_value.object->clear();
  17240. }
  17241. // it's now safe that current_item get destructed
  17242. // since it doesn't have any children
  17243. }
  17244. }
  17245. switch (t)
  17246. {
  17247. case value_t::object:
  17248. {
  17249. AllocatorType<object_t> alloc;
  17250. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  17251. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  17252. break;
  17253. }
  17254. case value_t::array:
  17255. {
  17256. AllocatorType<array_t> alloc;
  17257. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  17258. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  17259. break;
  17260. }
  17261. case value_t::string:
  17262. {
  17263. AllocatorType<string_t> alloc;
  17264. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  17265. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  17266. break;
  17267. }
  17268. case value_t::binary:
  17269. {
  17270. AllocatorType<binary_t> alloc;
  17271. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  17272. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  17273. break;
  17274. }
  17275. case value_t::null:
  17276. case value_t::boolean:
  17277. case value_t::number_integer:
  17278. case value_t::number_unsigned:
  17279. case value_t::number_float:
  17280. case value_t::discarded:
  17281. default:
  17282. {
  17283. break;
  17284. }
  17285. }
  17286. }
  17287. };
  17288. private:
  17289. /*!
  17290. @brief checks the class invariants
  17291. This function asserts the class invariants. It needs to be called at the
  17292. end of every constructor to make sure that created objects respect the
  17293. invariant. Furthermore, it has to be called each time the type of a JSON
  17294. value is changed, because the invariant expresses a relationship between
  17295. @a m_type and @a m_value.
  17296. Furthermore, the parent relation is checked for arrays and objects: If
  17297. @a check_parents true and the value is an array or object, then the
  17298. container's elements must have the current value as parent.
  17299. @param[in] check_parents whether the parent relation should be checked.
  17300. The value is true by default and should only be set to false
  17301. during destruction of objects when the invariant does not
  17302. need to hold.
  17303. */
  17304. void assert_invariant(bool check_parents = true) const noexcept
  17305. {
  17306. JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
  17307. JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
  17308. JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
  17309. JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
  17310. #if JSON_DIAGNOSTICS
  17311. JSON_TRY
  17312. {
  17313. // cppcheck-suppress assertWithSideEffect
  17314. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  17315. {
  17316. return j.m_parent == this;
  17317. }));
  17318. }
  17319. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  17320. #endif
  17321. static_cast<void>(check_parents);
  17322. }
  17323. void set_parents()
  17324. {
  17325. #if JSON_DIAGNOSTICS
  17326. switch (m_type)
  17327. {
  17328. case value_t::array:
  17329. {
  17330. for (auto& element : *m_value.array)
  17331. {
  17332. element.m_parent = this;
  17333. }
  17334. break;
  17335. }
  17336. case value_t::object:
  17337. {
  17338. for (auto& element : *m_value.object)
  17339. {
  17340. element.second.m_parent = this;
  17341. }
  17342. break;
  17343. }
  17344. case value_t::null:
  17345. case value_t::string:
  17346. case value_t::boolean:
  17347. case value_t::number_integer:
  17348. case value_t::number_unsigned:
  17349. case value_t::number_float:
  17350. case value_t::binary:
  17351. case value_t::discarded:
  17352. default:
  17353. break;
  17354. }
  17355. #endif
  17356. }
  17357. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  17358. {
  17359. #if JSON_DIAGNOSTICS
  17360. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  17361. {
  17362. (it + i)->m_parent = this;
  17363. }
  17364. #else
  17365. static_cast<void>(count_set_parents);
  17366. #endif
  17367. return it;
  17368. }
  17369. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  17370. {
  17371. #if JSON_DIAGNOSTICS
  17372. if (old_capacity != static_cast<std::size_t>(-1))
  17373. {
  17374. // see https://github.com/nlohmann/json/issues/2838
  17375. JSON_ASSERT(type() == value_t::array);
  17376. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  17377. {
  17378. // capacity has changed: update all parents
  17379. set_parents();
  17380. return j;
  17381. }
  17382. }
  17383. // ordered_json uses a vector internally, so pointers could have
  17384. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  17385. #ifdef JSON_HEDLEY_MSVC_VERSION
  17386. #pragma warning(push )
  17387. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  17388. #endif
  17389. if (detail::is_ordered_map<object_t>::value)
  17390. {
  17391. set_parents();
  17392. return j;
  17393. }
  17394. #ifdef JSON_HEDLEY_MSVC_VERSION
  17395. #pragma warning( pop )
  17396. #endif
  17397. j.m_parent = this;
  17398. #else
  17399. static_cast<void>(j);
  17400. static_cast<void>(old_capacity);
  17401. #endif
  17402. return j;
  17403. }
  17404. public:
  17405. //////////////////////////
  17406. // JSON parser callback //
  17407. //////////////////////////
  17408. /// @brief parser event types
  17409. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  17410. using parse_event_t = detail::parse_event_t;
  17411. /// @brief per-element parser callback type
  17412. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  17413. using parser_callback_t = detail::parser_callback_t<basic_json>;
  17414. //////////////////
  17415. // constructors //
  17416. //////////////////
  17417. /// @name constructors and destructors
  17418. /// Constructors of class @ref basic_json, copy/move constructor, copy
  17419. /// assignment, static functions creating objects, and the destructor.
  17420. /// @{
  17421. /// @brief create an empty value with a given type
  17422. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17423. basic_json(const value_t v)
  17424. : m_type(v), m_value(v)
  17425. {
  17426. assert_invariant();
  17427. }
  17428. /// @brief create a null object
  17429. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17430. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  17431. : basic_json(value_t::null)
  17432. {
  17433. assert_invariant();
  17434. }
  17435. /// @brief create a JSON value from compatible types
  17436. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17437. template < typename CompatibleType,
  17438. typename U = detail::uncvref_t<CompatibleType>,
  17439. detail::enable_if_t <
  17440. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  17441. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  17442. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  17443. std::forward<CompatibleType>(val))))
  17444. {
  17445. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  17446. set_parents();
  17447. assert_invariant();
  17448. }
  17449. /// @brief create a JSON value from an existing one
  17450. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17451. template < typename BasicJsonType,
  17452. detail::enable_if_t <
  17453. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  17454. basic_json(const BasicJsonType& val)
  17455. {
  17456. using other_boolean_t = typename BasicJsonType::boolean_t;
  17457. using other_number_float_t = typename BasicJsonType::number_float_t;
  17458. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  17459. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  17460. using other_string_t = typename BasicJsonType::string_t;
  17461. using other_object_t = typename BasicJsonType::object_t;
  17462. using other_array_t = typename BasicJsonType::array_t;
  17463. using other_binary_t = typename BasicJsonType::binary_t;
  17464. switch (val.type())
  17465. {
  17466. case value_t::boolean:
  17467. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  17468. break;
  17469. case value_t::number_float:
  17470. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  17471. break;
  17472. case value_t::number_integer:
  17473. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  17474. break;
  17475. case value_t::number_unsigned:
  17476. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  17477. break;
  17478. case value_t::string:
  17479. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  17480. break;
  17481. case value_t::object:
  17482. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  17483. break;
  17484. case value_t::array:
  17485. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  17486. break;
  17487. case value_t::binary:
  17488. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  17489. break;
  17490. case value_t::null:
  17491. *this = nullptr;
  17492. break;
  17493. case value_t::discarded:
  17494. m_type = value_t::discarded;
  17495. break;
  17496. default: // LCOV_EXCL_LINE
  17497. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17498. }
  17499. JSON_ASSERT(m_type == val.type());
  17500. set_parents();
  17501. assert_invariant();
  17502. }
  17503. /// @brief create a container (array or object) from an initializer list
  17504. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17505. basic_json(initializer_list_t init,
  17506. bool type_deduction = true,
  17507. value_t manual_type = value_t::array)
  17508. {
  17509. // check if each element is an array with two elements whose first
  17510. // element is a string
  17511. bool is_an_object = std::all_of(init.begin(), init.end(),
  17512. [](const detail::json_ref<basic_json>& element_ref)
  17513. {
  17514. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
  17515. });
  17516. // adjust type if type deduction is not wanted
  17517. if (!type_deduction)
  17518. {
  17519. // if array is wanted, do not create an object though possible
  17520. if (manual_type == value_t::array)
  17521. {
  17522. is_an_object = false;
  17523. }
  17524. // if object is wanted but impossible, throw an exception
  17525. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  17526. {
  17527. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  17528. }
  17529. }
  17530. if (is_an_object)
  17531. {
  17532. // the initializer list is a list of pairs -> create object
  17533. m_type = value_t::object;
  17534. m_value = value_t::object;
  17535. for (auto& element_ref : init)
  17536. {
  17537. auto element = element_ref.moved_or_copied();
  17538. m_value.object->emplace(
  17539. std::move(*((*element.m_value.array)[0].m_value.string)),
  17540. std::move((*element.m_value.array)[1]));
  17541. }
  17542. }
  17543. else
  17544. {
  17545. // the initializer list describes an array -> create array
  17546. m_type = value_t::array;
  17547. m_value.array = create<array_t>(init.begin(), init.end());
  17548. }
  17549. set_parents();
  17550. assert_invariant();
  17551. }
  17552. /// @brief explicitly create a binary array (without subtype)
  17553. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17554. JSON_HEDLEY_WARN_UNUSED_RESULT
  17555. static basic_json binary(const typename binary_t::container_type& init)
  17556. {
  17557. auto res = basic_json();
  17558. res.m_type = value_t::binary;
  17559. res.m_value = init;
  17560. return res;
  17561. }
  17562. /// @brief explicitly create a binary array (with subtype)
  17563. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17564. JSON_HEDLEY_WARN_UNUSED_RESULT
  17565. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  17566. {
  17567. auto res = basic_json();
  17568. res.m_type = value_t::binary;
  17569. res.m_value = binary_t(init, subtype);
  17570. return res;
  17571. }
  17572. /// @brief explicitly create a binary array
  17573. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17574. JSON_HEDLEY_WARN_UNUSED_RESULT
  17575. static basic_json binary(typename binary_t::container_type&& init)
  17576. {
  17577. auto res = basic_json();
  17578. res.m_type = value_t::binary;
  17579. res.m_value = std::move(init);
  17580. return res;
  17581. }
  17582. /// @brief explicitly create a binary array (with subtype)
  17583. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17584. JSON_HEDLEY_WARN_UNUSED_RESULT
  17585. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  17586. {
  17587. auto res = basic_json();
  17588. res.m_type = value_t::binary;
  17589. res.m_value = binary_t(std::move(init), subtype);
  17590. return res;
  17591. }
  17592. /// @brief explicitly create an array from an initializer list
  17593. /// @sa https://json.nlohmann.me/api/basic_json/array/
  17594. JSON_HEDLEY_WARN_UNUSED_RESULT
  17595. static basic_json array(initializer_list_t init = {})
  17596. {
  17597. return basic_json(init, false, value_t::array);
  17598. }
  17599. /// @brief explicitly create an object from an initializer list
  17600. /// @sa https://json.nlohmann.me/api/basic_json/object/
  17601. JSON_HEDLEY_WARN_UNUSED_RESULT
  17602. static basic_json object(initializer_list_t init = {})
  17603. {
  17604. return basic_json(init, false, value_t::object);
  17605. }
  17606. /// @brief construct an array with count copies of given value
  17607. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17608. basic_json(size_type cnt, const basic_json& val)
  17609. : m_type(value_t::array)
  17610. {
  17611. m_value.array = create<array_t>(cnt, val);
  17612. set_parents();
  17613. assert_invariant();
  17614. }
  17615. /// @brief construct a JSON container given an iterator range
  17616. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17617. template < class InputIT, typename std::enable_if <
  17618. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  17619. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  17620. basic_json(InputIT first, InputIT last)
  17621. {
  17622. JSON_ASSERT(first.m_object != nullptr);
  17623. JSON_ASSERT(last.m_object != nullptr);
  17624. // make sure iterator fits the current value
  17625. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  17626. {
  17627. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  17628. }
  17629. // copy type from first iterator
  17630. m_type = first.m_object->m_type;
  17631. // check if iterator range is complete for primitive values
  17632. switch (m_type)
  17633. {
  17634. case value_t::boolean:
  17635. case value_t::number_float:
  17636. case value_t::number_integer:
  17637. case value_t::number_unsigned:
  17638. case value_t::string:
  17639. {
  17640. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  17641. || !last.m_it.primitive_iterator.is_end()))
  17642. {
  17643. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  17644. }
  17645. break;
  17646. }
  17647. case value_t::null:
  17648. case value_t::object:
  17649. case value_t::array:
  17650. case value_t::binary:
  17651. case value_t::discarded:
  17652. default:
  17653. break;
  17654. }
  17655. switch (m_type)
  17656. {
  17657. case value_t::number_integer:
  17658. {
  17659. m_value.number_integer = first.m_object->m_value.number_integer;
  17660. break;
  17661. }
  17662. case value_t::number_unsigned:
  17663. {
  17664. m_value.number_unsigned = first.m_object->m_value.number_unsigned;
  17665. break;
  17666. }
  17667. case value_t::number_float:
  17668. {
  17669. m_value.number_float = first.m_object->m_value.number_float;
  17670. break;
  17671. }
  17672. case value_t::boolean:
  17673. {
  17674. m_value.boolean = first.m_object->m_value.boolean;
  17675. break;
  17676. }
  17677. case value_t::string:
  17678. {
  17679. m_value = *first.m_object->m_value.string;
  17680. break;
  17681. }
  17682. case value_t::object:
  17683. {
  17684. m_value.object = create<object_t>(first.m_it.object_iterator,
  17685. last.m_it.object_iterator);
  17686. break;
  17687. }
  17688. case value_t::array:
  17689. {
  17690. m_value.array = create<array_t>(first.m_it.array_iterator,
  17691. last.m_it.array_iterator);
  17692. break;
  17693. }
  17694. case value_t::binary:
  17695. {
  17696. m_value = *first.m_object->m_value.binary;
  17697. break;
  17698. }
  17699. case value_t::null:
  17700. case value_t::discarded:
  17701. default:
  17702. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  17703. }
  17704. set_parents();
  17705. assert_invariant();
  17706. }
  17707. ///////////////////////////////////////
  17708. // other constructors and destructor //
  17709. ///////////////////////////////////////
  17710. template<typename JsonRef,
  17711. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  17712. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  17713. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  17714. /// @brief copy constructor
  17715. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17716. basic_json(const basic_json& other)
  17717. : json_base_class_t(other),
  17718. m_type(other.m_type)
  17719. {
  17720. // check of passed value is valid
  17721. other.assert_invariant();
  17722. switch (m_type)
  17723. {
  17724. case value_t::object:
  17725. {
  17726. m_value = *other.m_value.object;
  17727. break;
  17728. }
  17729. case value_t::array:
  17730. {
  17731. m_value = *other.m_value.array;
  17732. break;
  17733. }
  17734. case value_t::string:
  17735. {
  17736. m_value = *other.m_value.string;
  17737. break;
  17738. }
  17739. case value_t::boolean:
  17740. {
  17741. m_value = other.m_value.boolean;
  17742. break;
  17743. }
  17744. case value_t::number_integer:
  17745. {
  17746. m_value = other.m_value.number_integer;
  17747. break;
  17748. }
  17749. case value_t::number_unsigned:
  17750. {
  17751. m_value = other.m_value.number_unsigned;
  17752. break;
  17753. }
  17754. case value_t::number_float:
  17755. {
  17756. m_value = other.m_value.number_float;
  17757. break;
  17758. }
  17759. case value_t::binary:
  17760. {
  17761. m_value = *other.m_value.binary;
  17762. break;
  17763. }
  17764. case value_t::null:
  17765. case value_t::discarded:
  17766. default:
  17767. break;
  17768. }
  17769. set_parents();
  17770. assert_invariant();
  17771. }
  17772. /// @brief move constructor
  17773. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17774. basic_json(basic_json&& other) noexcept
  17775. : json_base_class_t(std::move(other)),
  17776. m_type(std::move(other.m_type)),
  17777. m_value(std::move(other.m_value))
  17778. {
  17779. // check that passed value is valid
  17780. other.assert_invariant(false); // NOLINT(bugprone-use-after-move,hicpp-invalid-access-moved)
  17781. // invalidate payload
  17782. other.m_type = value_t::null;
  17783. other.m_value = {};
  17784. set_parents();
  17785. assert_invariant();
  17786. }
  17787. /// @brief copy assignment
  17788. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  17789. basic_json& operator=(basic_json other) noexcept (
  17790. std::is_nothrow_move_constructible<value_t>::value&&
  17791. std::is_nothrow_move_assignable<value_t>::value&&
  17792. std::is_nothrow_move_constructible<json_value>::value&&
  17793. std::is_nothrow_move_assignable<json_value>::value&&
  17794. std::is_nothrow_move_assignable<json_base_class_t>::value
  17795. )
  17796. {
  17797. // check that passed value is valid
  17798. other.assert_invariant();
  17799. using std::swap;
  17800. swap(m_type, other.m_type);
  17801. swap(m_value, other.m_value);
  17802. json_base_class_t::operator=(std::move(other));
  17803. set_parents();
  17804. assert_invariant();
  17805. return *this;
  17806. }
  17807. /// @brief destructor
  17808. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  17809. ~basic_json() noexcept
  17810. {
  17811. assert_invariant(false);
  17812. m_value.destroy(m_type);
  17813. }
  17814. /// @}
  17815. public:
  17816. ///////////////////////
  17817. // object inspection //
  17818. ///////////////////////
  17819. /// @name object inspection
  17820. /// Functions to inspect the type of a JSON value.
  17821. /// @{
  17822. /// @brief serialization
  17823. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  17824. string_t dump(const int indent = -1,
  17825. const char indent_char = ' ',
  17826. const bool ensure_ascii = false,
  17827. const error_handler_t error_handler = error_handler_t::strict) const
  17828. {
  17829. string_t result;
  17830. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17831. if (indent >= 0)
  17832. {
  17833. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17834. }
  17835. else
  17836. {
  17837. s.dump(*this, false, ensure_ascii, 0);
  17838. }
  17839. return result;
  17840. }
  17841. /// @brief return the type of the JSON value (explicit)
  17842. /// @sa https://json.nlohmann.me/api/basic_json/type/
  17843. constexpr value_t type() const noexcept
  17844. {
  17845. return m_type;
  17846. }
  17847. /// @brief return whether type is primitive
  17848. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  17849. constexpr bool is_primitive() const noexcept
  17850. {
  17851. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17852. }
  17853. /// @brief return whether type is structured
  17854. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  17855. constexpr bool is_structured() const noexcept
  17856. {
  17857. return is_array() || is_object();
  17858. }
  17859. /// @brief return whether value is null
  17860. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  17861. constexpr bool is_null() const noexcept
  17862. {
  17863. return m_type == value_t::null;
  17864. }
  17865. /// @brief return whether value is a boolean
  17866. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  17867. constexpr bool is_boolean() const noexcept
  17868. {
  17869. return m_type == value_t::boolean;
  17870. }
  17871. /// @brief return whether value is a number
  17872. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  17873. constexpr bool is_number() const noexcept
  17874. {
  17875. return is_number_integer() || is_number_float();
  17876. }
  17877. /// @brief return whether value is an integer number
  17878. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  17879. constexpr bool is_number_integer() const noexcept
  17880. {
  17881. return m_type == value_t::number_integer || m_type == value_t::number_unsigned;
  17882. }
  17883. /// @brief return whether value is an unsigned integer number
  17884. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  17885. constexpr bool is_number_unsigned() const noexcept
  17886. {
  17887. return m_type == value_t::number_unsigned;
  17888. }
  17889. /// @brief return whether value is a floating-point number
  17890. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  17891. constexpr bool is_number_float() const noexcept
  17892. {
  17893. return m_type == value_t::number_float;
  17894. }
  17895. /// @brief return whether value is an object
  17896. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  17897. constexpr bool is_object() const noexcept
  17898. {
  17899. return m_type == value_t::object;
  17900. }
  17901. /// @brief return whether value is an array
  17902. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  17903. constexpr bool is_array() const noexcept
  17904. {
  17905. return m_type == value_t::array;
  17906. }
  17907. /// @brief return whether value is a string
  17908. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  17909. constexpr bool is_string() const noexcept
  17910. {
  17911. return m_type == value_t::string;
  17912. }
  17913. /// @brief return whether value is a binary array
  17914. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  17915. constexpr bool is_binary() const noexcept
  17916. {
  17917. return m_type == value_t::binary;
  17918. }
  17919. /// @brief return whether value is discarded
  17920. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  17921. constexpr bool is_discarded() const noexcept
  17922. {
  17923. return m_type == value_t::discarded;
  17924. }
  17925. /// @brief return the type of the JSON value (implicit)
  17926. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  17927. constexpr operator value_t() const noexcept
  17928. {
  17929. return m_type;
  17930. }
  17931. /// @}
  17932. private:
  17933. //////////////////
  17934. // value access //
  17935. //////////////////
  17936. /// get a boolean (explicit)
  17937. boolean_t get_impl(boolean_t* /*unused*/) const
  17938. {
  17939. if (JSON_HEDLEY_LIKELY(is_boolean()))
  17940. {
  17941. return m_value.boolean;
  17942. }
  17943. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  17944. }
  17945. /// get a pointer to the value (object)
  17946. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  17947. {
  17948. return is_object() ? m_value.object : nullptr;
  17949. }
  17950. /// get a pointer to the value (object)
  17951. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  17952. {
  17953. return is_object() ? m_value.object : nullptr;
  17954. }
  17955. /// get a pointer to the value (array)
  17956. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  17957. {
  17958. return is_array() ? m_value.array : nullptr;
  17959. }
  17960. /// get a pointer to the value (array)
  17961. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  17962. {
  17963. return is_array() ? m_value.array : nullptr;
  17964. }
  17965. /// get a pointer to the value (string)
  17966. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  17967. {
  17968. return is_string() ? m_value.string : nullptr;
  17969. }
  17970. /// get a pointer to the value (string)
  17971. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  17972. {
  17973. return is_string() ? m_value.string : nullptr;
  17974. }
  17975. /// get a pointer to the value (boolean)
  17976. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  17977. {
  17978. return is_boolean() ? &m_value.boolean : nullptr;
  17979. }
  17980. /// get a pointer to the value (boolean)
  17981. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  17982. {
  17983. return is_boolean() ? &m_value.boolean : nullptr;
  17984. }
  17985. /// get a pointer to the value (integer number)
  17986. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  17987. {
  17988. return is_number_integer() ? &m_value.number_integer : nullptr;
  17989. }
  17990. /// get a pointer to the value (integer number)
  17991. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  17992. {
  17993. return is_number_integer() ? &m_value.number_integer : nullptr;
  17994. }
  17995. /// get a pointer to the value (unsigned number)
  17996. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  17997. {
  17998. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17999. }
  18000. /// get a pointer to the value (unsigned number)
  18001. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  18002. {
  18003. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  18004. }
  18005. /// get a pointer to the value (floating-point number)
  18006. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  18007. {
  18008. return is_number_float() ? &m_value.number_float : nullptr;
  18009. }
  18010. /// get a pointer to the value (floating-point number)
  18011. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  18012. {
  18013. return is_number_float() ? &m_value.number_float : nullptr;
  18014. }
  18015. /// get a pointer to the value (binary)
  18016. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  18017. {
  18018. return is_binary() ? m_value.binary : nullptr;
  18019. }
  18020. /// get a pointer to the value (binary)
  18021. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  18022. {
  18023. return is_binary() ? m_value.binary : nullptr;
  18024. }
  18025. /*!
  18026. @brief helper function to implement get_ref()
  18027. This function helps to implement get_ref() without code duplication for
  18028. const and non-const overloads
  18029. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  18030. @throw type_error.303 if ReferenceType does not match underlying value
  18031. type of the current JSON
  18032. */
  18033. template<typename ReferenceType, typename ThisType>
  18034. static ReferenceType get_ref_impl(ThisType& obj)
  18035. {
  18036. // delegate the call to get_ptr<>()
  18037. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18038. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18039. {
  18040. return *ptr;
  18041. }
  18042. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18043. }
  18044. public:
  18045. /// @name value access
  18046. /// Direct access to the stored value of a JSON value.
  18047. /// @{
  18048. /// @brief get a pointer value (implicit)
  18049. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18050. template<typename PointerType, typename std::enable_if<
  18051. std::is_pointer<PointerType>::value, int>::type = 0>
  18052. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18053. {
  18054. // delegate the call to get_impl_ptr<>()
  18055. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18056. }
  18057. /// @brief get a pointer value (implicit)
  18058. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18059. template < typename PointerType, typename std::enable_if <
  18060. std::is_pointer<PointerType>::value&&
  18061. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18062. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18063. {
  18064. // delegate the call to get_impl_ptr<>() const
  18065. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18066. }
  18067. private:
  18068. /*!
  18069. @brief get a value (explicit)
  18070. Explicit type conversion between the JSON value and a compatible value
  18071. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18072. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18073. The value is converted by calling the @ref json_serializer<ValueType>
  18074. `from_json()` method.
  18075. The function is equivalent to executing
  18076. @code {.cpp}
  18077. ValueType ret;
  18078. JSONSerializer<ValueType>::from_json(*this, ret);
  18079. return ret;
  18080. @endcode
  18081. This overloads is chosen if:
  18082. - @a ValueType is not @ref basic_json,
  18083. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18084. `void from_json(const basic_json&, ValueType&)`, and
  18085. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  18086. the form `ValueType from_json(const basic_json&)`
  18087. @tparam ValueType the returned value type
  18088. @return copy of the JSON value, converted to @a ValueType
  18089. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18090. @liveexample{The example below shows several conversions from JSON values
  18091. to other types. There a few things to note: (1) Floating-point numbers can
  18092. be converted to integers\, (2) A JSON array can be converted to a standard
  18093. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18094. associative containers such as `std::unordered_map<std::string\,
  18095. json>`.,get__ValueType_const}
  18096. @since version 2.1.0
  18097. */
  18098. template < typename ValueType,
  18099. detail::enable_if_t <
  18100. detail::is_default_constructible<ValueType>::value&&
  18101. detail::has_from_json<basic_json_t, ValueType>::value,
  18102. int > = 0 >
  18103. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  18104. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  18105. {
  18106. auto ret = ValueType();
  18107. JSONSerializer<ValueType>::from_json(*this, ret);
  18108. return ret;
  18109. }
  18110. /*!
  18111. @brief get a value (explicit); special case
  18112. Explicit type conversion between the JSON value and a compatible value
  18113. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18114. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18115. The value is converted by calling the @ref json_serializer<ValueType>
  18116. `from_json()` method.
  18117. The function is equivalent to executing
  18118. @code {.cpp}
  18119. return JSONSerializer<ValueType>::from_json(*this);
  18120. @endcode
  18121. This overloads is chosen if:
  18122. - @a ValueType is not @ref basic_json and
  18123. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18124. `ValueType from_json(const basic_json&)`
  18125. @note If @ref json_serializer<ValueType> has both overloads of
  18126. `from_json()`, this one is chosen.
  18127. @tparam ValueType the returned value type
  18128. @return copy of the JSON value, converted to @a ValueType
  18129. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18130. @since version 2.1.0
  18131. */
  18132. template < typename ValueType,
  18133. detail::enable_if_t <
  18134. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  18135. int > = 0 >
  18136. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  18137. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  18138. {
  18139. return JSONSerializer<ValueType>::from_json(*this);
  18140. }
  18141. /*!
  18142. @brief get special-case overload
  18143. This overloads converts the current @ref basic_json in a different
  18144. @ref basic_json type
  18145. @tparam BasicJsonType == @ref basic_json
  18146. @return a copy of *this, converted into @a BasicJsonType
  18147. @complexity Depending on the implementation of the called `from_json()`
  18148. method.
  18149. @since version 3.2.0
  18150. */
  18151. template < typename BasicJsonType,
  18152. detail::enable_if_t <
  18153. detail::is_basic_json<BasicJsonType>::value,
  18154. int > = 0 >
  18155. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  18156. {
  18157. return *this;
  18158. }
  18159. /*!
  18160. @brief get special-case overload
  18161. This overloads avoids a lot of template boilerplate, it can be seen as the
  18162. identity method
  18163. @tparam BasicJsonType == @ref basic_json
  18164. @return a copy of *this
  18165. @complexity Constant.
  18166. @since version 2.1.0
  18167. */
  18168. template<typename BasicJsonType,
  18169. detail::enable_if_t<
  18170. std::is_same<BasicJsonType, basic_json_t>::value,
  18171. int> = 0>
  18172. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  18173. {
  18174. return *this;
  18175. }
  18176. /*!
  18177. @brief get a pointer value (explicit)
  18178. @copydoc get()
  18179. */
  18180. template<typename PointerType,
  18181. detail::enable_if_t<
  18182. std::is_pointer<PointerType>::value,
  18183. int> = 0>
  18184. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  18185. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  18186. {
  18187. // delegate the call to get_ptr
  18188. return get_ptr<PointerType>();
  18189. }
  18190. public:
  18191. /*!
  18192. @brief get a (pointer) value (explicit)
  18193. Performs explicit type conversion between the JSON value and a compatible value if required.
  18194. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  18195. No copies are made.
  18196. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  18197. from the current @ref basic_json.
  18198. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  18199. method.
  18200. @tparam ValueTypeCV the provided value type
  18201. @tparam ValueType the returned value type
  18202. @return copy of the JSON value, converted to @tparam ValueType if necessary
  18203. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  18204. @since version 2.1.0
  18205. */
  18206. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  18207. #if defined(JSON_HAS_CPP_14)
  18208. constexpr
  18209. #endif
  18210. auto get() const noexcept(
  18211. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  18212. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  18213. {
  18214. // we cannot static_assert on ValueTypeCV being non-const, because
  18215. // there is support for get<const basic_json_t>(), which is why we
  18216. // still need the uncvref
  18217. static_assert(!std::is_reference<ValueTypeCV>::value,
  18218. "get() cannot be used with reference types, you might want to use get_ref()");
  18219. return get_impl<ValueType>(detail::priority_tag<4> {});
  18220. }
  18221. /*!
  18222. @brief get a pointer value (explicit)
  18223. Explicit pointer access to the internally stored JSON value. No copies are
  18224. made.
  18225. @warning The pointer becomes invalid if the underlying JSON object
  18226. changes.
  18227. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  18228. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  18229. @ref number_unsigned_t, or @ref number_float_t.
  18230. @return pointer to the internally stored JSON value if the requested
  18231. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  18232. @complexity Constant.
  18233. @liveexample{The example below shows how pointers to internal values of a
  18234. JSON value can be requested. Note that no type conversions are made and a
  18235. `nullptr` is returned if the value and the requested pointer type does not
  18236. match.,get__PointerType}
  18237. @sa see @ref get_ptr() for explicit pointer-member access
  18238. @since version 1.0.0
  18239. */
  18240. template<typename PointerType, typename std::enable_if<
  18241. std::is_pointer<PointerType>::value, int>::type = 0>
  18242. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  18243. {
  18244. // delegate the call to get_ptr
  18245. return get_ptr<PointerType>();
  18246. }
  18247. /// @brief get a value (explicit)
  18248. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  18249. template < typename ValueType,
  18250. detail::enable_if_t <
  18251. !detail::is_basic_json<ValueType>::value&&
  18252. detail::has_from_json<basic_json_t, ValueType>::value,
  18253. int > = 0 >
  18254. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  18255. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  18256. {
  18257. JSONSerializer<ValueType>::from_json(*this, v);
  18258. return v;
  18259. }
  18260. // specialization to allow calling get_to with a basic_json value
  18261. // see https://github.com/nlohmann/json/issues/2175
  18262. template<typename ValueType,
  18263. detail::enable_if_t <
  18264. detail::is_basic_json<ValueType>::value,
  18265. int> = 0>
  18266. ValueType & get_to(ValueType& v) const
  18267. {
  18268. v = *this;
  18269. return v;
  18270. }
  18271. template <
  18272. typename T, std::size_t N,
  18273. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18274. detail::enable_if_t <
  18275. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  18276. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18277. noexcept(noexcept(JSONSerializer<Array>::from_json(
  18278. std::declval<const basic_json_t&>(), v)))
  18279. {
  18280. JSONSerializer<Array>::from_json(*this, v);
  18281. return v;
  18282. }
  18283. /// @brief get a reference value (implicit)
  18284. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18285. template<typename ReferenceType, typename std::enable_if<
  18286. std::is_reference<ReferenceType>::value, int>::type = 0>
  18287. ReferenceType get_ref()
  18288. {
  18289. // delegate call to get_ref_impl
  18290. return get_ref_impl<ReferenceType>(*this);
  18291. }
  18292. /// @brief get a reference value (implicit)
  18293. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18294. template < typename ReferenceType, typename std::enable_if <
  18295. std::is_reference<ReferenceType>::value&&
  18296. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  18297. ReferenceType get_ref() const
  18298. {
  18299. // delegate call to get_ref_impl
  18300. return get_ref_impl<ReferenceType>(*this);
  18301. }
  18302. /*!
  18303. @brief get a value (implicit)
  18304. Implicit type conversion between the JSON value and a compatible value.
  18305. The call is realized by calling @ref get() const.
  18306. @tparam ValueType non-pointer type compatible to the JSON value, for
  18307. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  18308. `std::vector` types for JSON arrays. The character type of @ref string_t
  18309. as well as an initializer list of this type is excluded to avoid
  18310. ambiguities as these types implicitly convert to `std::string`.
  18311. @return copy of the JSON value, converted to type @a ValueType
  18312. @throw type_error.302 in case passed type @a ValueType is incompatible
  18313. to the JSON value type (e.g., the JSON value is of type boolean, but a
  18314. string is requested); see example below
  18315. @complexity Linear in the size of the JSON value.
  18316. @liveexample{The example below shows several conversions from JSON values
  18317. to other types. There a few things to note: (1) Floating-point numbers can
  18318. be converted to integers\, (2) A JSON array can be converted to a standard
  18319. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18320. associative containers such as `std::unordered_map<std::string\,
  18321. json>`.,operator__ValueType}
  18322. @since version 1.0.0
  18323. */
  18324. template < typename ValueType, typename std::enable_if <
  18325. detail::conjunction <
  18326. detail::negation<std::is_pointer<ValueType>>,
  18327. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  18328. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  18329. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  18330. detail::negation<detail::is_basic_json<ValueType>>,
  18331. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  18332. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  18333. detail::negation<std::is_same<ValueType, std::string_view>>,
  18334. #endif
  18335. #if defined(JSON_HAS_CPP_17)
  18336. detail::negation<std::is_same<ValueType, std::any>>,
  18337. #endif
  18338. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  18339. >::value, int >::type = 0 >
  18340. JSON_EXPLICIT operator ValueType() const
  18341. {
  18342. // delegate the call to get<>() const
  18343. return get<ValueType>();
  18344. }
  18345. /// @brief get a binary value
  18346. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18347. binary_t& get_binary()
  18348. {
  18349. if (!is_binary())
  18350. {
  18351. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18352. }
  18353. return *get_ptr<binary_t*>();
  18354. }
  18355. /// @brief get a binary value
  18356. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18357. const binary_t& get_binary() const
  18358. {
  18359. if (!is_binary())
  18360. {
  18361. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18362. }
  18363. return *get_ptr<const binary_t*>();
  18364. }
  18365. /// @}
  18366. ////////////////////
  18367. // element access //
  18368. ////////////////////
  18369. /// @name element access
  18370. /// Access to the JSON value.
  18371. /// @{
  18372. /// @brief access specified array element with bounds checking
  18373. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18374. reference at(size_type idx)
  18375. {
  18376. // at only works for arrays
  18377. if (JSON_HEDLEY_LIKELY(is_array()))
  18378. {
  18379. JSON_TRY
  18380. {
  18381. return set_parent(m_value.array->at(idx));
  18382. }
  18383. JSON_CATCH (std::out_of_range&)
  18384. {
  18385. // create better exception explanation
  18386. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18387. }
  18388. }
  18389. else
  18390. {
  18391. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18392. }
  18393. }
  18394. /// @brief access specified array element with bounds checking
  18395. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18396. const_reference at(size_type idx) const
  18397. {
  18398. // at only works for arrays
  18399. if (JSON_HEDLEY_LIKELY(is_array()))
  18400. {
  18401. JSON_TRY
  18402. {
  18403. return m_value.array->at(idx);
  18404. }
  18405. JSON_CATCH (std::out_of_range&)
  18406. {
  18407. // create better exception explanation
  18408. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18409. }
  18410. }
  18411. else
  18412. {
  18413. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18414. }
  18415. }
  18416. /// @brief access specified object element with bounds checking
  18417. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18418. reference at(const typename object_t::key_type& key)
  18419. {
  18420. // at only works for objects
  18421. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18422. {
  18423. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18424. }
  18425. auto it = m_value.object->find(key);
  18426. if (it == m_value.object->end())
  18427. {
  18428. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18429. }
  18430. return set_parent(it->second);
  18431. }
  18432. /// @brief access specified object element with bounds checking
  18433. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18434. template<class KeyType, detail::enable_if_t<
  18435. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18436. reference at(KeyType && key)
  18437. {
  18438. // at only works for objects
  18439. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18440. {
  18441. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18442. }
  18443. auto it = m_value.object->find(std::forward<KeyType>(key));
  18444. if (it == m_value.object->end())
  18445. {
  18446. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18447. }
  18448. return set_parent(it->second);
  18449. }
  18450. /// @brief access specified object element with bounds checking
  18451. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18452. const_reference at(const typename object_t::key_type& key) const
  18453. {
  18454. // at only works for objects
  18455. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18456. {
  18457. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18458. }
  18459. auto it = m_value.object->find(key);
  18460. if (it == m_value.object->end())
  18461. {
  18462. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18463. }
  18464. return it->second;
  18465. }
  18466. /// @brief access specified object element with bounds checking
  18467. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18468. template<class KeyType, detail::enable_if_t<
  18469. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18470. const_reference at(KeyType && key) const
  18471. {
  18472. // at only works for objects
  18473. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18474. {
  18475. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18476. }
  18477. auto it = m_value.object->find(std::forward<KeyType>(key));
  18478. if (it == m_value.object->end())
  18479. {
  18480. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18481. }
  18482. return it->second;
  18483. }
  18484. /// @brief access specified array element
  18485. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18486. reference operator[](size_type idx)
  18487. {
  18488. // implicitly convert null value to an empty array
  18489. if (is_null())
  18490. {
  18491. m_type = value_t::array;
  18492. m_value.array = create<array_t>();
  18493. assert_invariant();
  18494. }
  18495. // operator[] only works for arrays
  18496. if (JSON_HEDLEY_LIKELY(is_array()))
  18497. {
  18498. // fill up array with null values if given idx is outside range
  18499. if (idx >= m_value.array->size())
  18500. {
  18501. #if JSON_DIAGNOSTICS
  18502. // remember array size & capacity before resizing
  18503. const auto old_size = m_value.array->size();
  18504. const auto old_capacity = m_value.array->capacity();
  18505. #endif
  18506. m_value.array->resize(idx + 1);
  18507. #if JSON_DIAGNOSTICS
  18508. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  18509. {
  18510. // capacity has changed: update all parents
  18511. set_parents();
  18512. }
  18513. else
  18514. {
  18515. // set parent for values added above
  18516. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  18517. }
  18518. #endif
  18519. assert_invariant();
  18520. }
  18521. return m_value.array->operator[](idx);
  18522. }
  18523. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18524. }
  18525. /// @brief access specified array element
  18526. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18527. const_reference operator[](size_type idx) const
  18528. {
  18529. // const operator[] only works for arrays
  18530. if (JSON_HEDLEY_LIKELY(is_array()))
  18531. {
  18532. return m_value.array->operator[](idx);
  18533. }
  18534. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18535. }
  18536. /// @brief access specified object element
  18537. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18538. reference operator[](typename object_t::key_type key)
  18539. {
  18540. // implicitly convert null value to an empty object
  18541. if (is_null())
  18542. {
  18543. m_type = value_t::object;
  18544. m_value.object = create<object_t>();
  18545. assert_invariant();
  18546. }
  18547. // operator[] only works for objects
  18548. if (JSON_HEDLEY_LIKELY(is_object()))
  18549. {
  18550. auto result = m_value.object->emplace(std::move(key), nullptr);
  18551. return set_parent(result.first->second);
  18552. }
  18553. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18554. }
  18555. /// @brief access specified object element
  18556. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18557. const_reference operator[](const typename object_t::key_type& key) const
  18558. {
  18559. // const operator[] only works for objects
  18560. if (JSON_HEDLEY_LIKELY(is_object()))
  18561. {
  18562. auto it = m_value.object->find(key);
  18563. JSON_ASSERT(it != m_value.object->end());
  18564. return it->second;
  18565. }
  18566. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18567. }
  18568. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  18569. // (they seemingly cannot be constrained to resolve the ambiguity)
  18570. template<typename T>
  18571. reference operator[](T* key)
  18572. {
  18573. return operator[](typename object_t::key_type(key));
  18574. }
  18575. template<typename T>
  18576. const_reference operator[](T* key) const
  18577. {
  18578. return operator[](typename object_t::key_type(key));
  18579. }
  18580. /// @brief access specified object element
  18581. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18582. template<class KeyType, detail::enable_if_t<
  18583. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18584. reference operator[](KeyType && key)
  18585. {
  18586. // implicitly convert null value to an empty object
  18587. if (is_null())
  18588. {
  18589. m_type = value_t::object;
  18590. m_value.object = create<object_t>();
  18591. assert_invariant();
  18592. }
  18593. // operator[] only works for objects
  18594. if (JSON_HEDLEY_LIKELY(is_object()))
  18595. {
  18596. auto result = m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  18597. return set_parent(result.first->second);
  18598. }
  18599. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18600. }
  18601. /// @brief access specified object element
  18602. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18603. template<class KeyType, detail::enable_if_t<
  18604. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18605. const_reference operator[](KeyType && key) const
  18606. {
  18607. // const operator[] only works for objects
  18608. if (JSON_HEDLEY_LIKELY(is_object()))
  18609. {
  18610. auto it = m_value.object->find(std::forward<KeyType>(key));
  18611. JSON_ASSERT(it != m_value.object->end());
  18612. return it->second;
  18613. }
  18614. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18615. }
  18616. private:
  18617. template<typename KeyType>
  18618. using is_comparable_with_object_key = detail::is_comparable <
  18619. object_comparator_t, const typename object_t::key_type&, KeyType >;
  18620. template<typename ValueType>
  18621. using value_return_type = std::conditional <
  18622. detail::is_c_string_uncvref<ValueType>::value,
  18623. string_t, typename std::decay<ValueType>::type >;
  18624. public:
  18625. /// @brief access specified object element with default value
  18626. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18627. template < class ValueType, detail::enable_if_t <
  18628. !detail::is_transparent<object_comparator_t>::value
  18629. && detail::is_getable<basic_json_t, ValueType>::value
  18630. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18631. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  18632. {
  18633. // value only works for objects
  18634. if (JSON_HEDLEY_LIKELY(is_object()))
  18635. {
  18636. // if key is found, return value and given default value otherwise
  18637. const auto it = find(key);
  18638. if (it != end())
  18639. {
  18640. return it->template get<ValueType>();
  18641. }
  18642. return default_value;
  18643. }
  18644. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18645. }
  18646. /// @brief access specified object element with default value
  18647. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18648. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18649. detail::enable_if_t <
  18650. !detail::is_transparent<object_comparator_t>::value
  18651. && detail::is_getable<basic_json_t, ReturnType>::value
  18652. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18653. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  18654. {
  18655. // value only works for objects
  18656. if (JSON_HEDLEY_LIKELY(is_object()))
  18657. {
  18658. // if key is found, return value and given default value otherwise
  18659. const auto it = find(key);
  18660. if (it != end())
  18661. {
  18662. return it->template get<ReturnType>();
  18663. }
  18664. return std::forward<ValueType>(default_value);
  18665. }
  18666. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18667. }
  18668. /// @brief access specified object element with default value
  18669. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18670. template < class ValueType, class KeyType, detail::enable_if_t <
  18671. detail::is_transparent<object_comparator_t>::value
  18672. && !detail::is_json_pointer<KeyType>::value
  18673. && is_comparable_with_object_key<KeyType>::value
  18674. && detail::is_getable<basic_json_t, ValueType>::value
  18675. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18676. ValueType value(KeyType && key, const ValueType& default_value) const
  18677. {
  18678. // value only works for objects
  18679. if (JSON_HEDLEY_LIKELY(is_object()))
  18680. {
  18681. // if key is found, return value and given default value otherwise
  18682. const auto it = find(std::forward<KeyType>(key));
  18683. if (it != end())
  18684. {
  18685. return it->template get<ValueType>();
  18686. }
  18687. return default_value;
  18688. }
  18689. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18690. }
  18691. /// @brief access specified object element via JSON Pointer with default value
  18692. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18693. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  18694. detail::enable_if_t <
  18695. detail::is_transparent<object_comparator_t>::value
  18696. && !detail::is_json_pointer<KeyType>::value
  18697. && is_comparable_with_object_key<KeyType>::value
  18698. && detail::is_getable<basic_json_t, ReturnType>::value
  18699. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18700. ReturnType value(KeyType && key, ValueType && default_value) const
  18701. {
  18702. // value only works for objects
  18703. if (JSON_HEDLEY_LIKELY(is_object()))
  18704. {
  18705. // if key is found, return value and given default value otherwise
  18706. const auto it = find(std::forward<KeyType>(key));
  18707. if (it != end())
  18708. {
  18709. return it->template get<ReturnType>();
  18710. }
  18711. return std::forward<ValueType>(default_value);
  18712. }
  18713. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18714. }
  18715. /// @brief access specified object element via JSON Pointer with default value
  18716. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18717. template < class ValueType, detail::enable_if_t <
  18718. detail::is_getable<basic_json_t, ValueType>::value
  18719. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18720. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18721. {
  18722. // value only works for objects
  18723. if (JSON_HEDLEY_LIKELY(is_object()))
  18724. {
  18725. // if pointer resolves a value, return it or use default value
  18726. JSON_TRY
  18727. {
  18728. return ptr.get_checked(this).template get<ValueType>();
  18729. }
  18730. JSON_INTERNAL_CATCH (out_of_range&)
  18731. {
  18732. return default_value;
  18733. }
  18734. }
  18735. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18736. }
  18737. /// @brief access specified object element via JSON Pointer with default value
  18738. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18739. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18740. detail::enable_if_t <
  18741. detail::is_getable<basic_json_t, ReturnType>::value
  18742. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18743. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  18744. {
  18745. // value only works for objects
  18746. if (JSON_HEDLEY_LIKELY(is_object()))
  18747. {
  18748. // if pointer resolves a value, return it or use default value
  18749. JSON_TRY
  18750. {
  18751. return ptr.get_checked(this).template get<ReturnType>();
  18752. }
  18753. JSON_INTERNAL_CATCH (out_of_range&)
  18754. {
  18755. return std::forward<ValueType>(default_value);
  18756. }
  18757. }
  18758. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18759. }
  18760. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  18761. detail::is_basic_json<BasicJsonType>::value
  18762. && detail::is_getable<basic_json_t, ValueType>::value
  18763. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18764. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18765. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  18766. {
  18767. return value(ptr.convert(), default_value);
  18768. }
  18769. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  18770. detail::enable_if_t <
  18771. detail::is_basic_json<BasicJsonType>::value
  18772. && detail::is_getable<basic_json_t, ReturnType>::value
  18773. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18774. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18775. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  18776. {
  18777. return value(ptr.convert(), std::forward<ValueType>(default_value));
  18778. }
  18779. /// @brief access the first element
  18780. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18781. reference front()
  18782. {
  18783. return *begin();
  18784. }
  18785. /// @brief access the first element
  18786. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18787. const_reference front() const
  18788. {
  18789. return *cbegin();
  18790. }
  18791. /// @brief access the last element
  18792. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18793. reference back()
  18794. {
  18795. auto tmp = end();
  18796. --tmp;
  18797. return *tmp;
  18798. }
  18799. /// @brief access the last element
  18800. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18801. const_reference back() const
  18802. {
  18803. auto tmp = cend();
  18804. --tmp;
  18805. return *tmp;
  18806. }
  18807. /// @brief remove element given an iterator
  18808. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18809. template < class IteratorType, detail::enable_if_t <
  18810. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18811. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18812. IteratorType erase(IteratorType pos)
  18813. {
  18814. // make sure iterator fits the current value
  18815. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18816. {
  18817. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18818. }
  18819. IteratorType result = end();
  18820. switch (m_type)
  18821. {
  18822. case value_t::boolean:
  18823. case value_t::number_float:
  18824. case value_t::number_integer:
  18825. case value_t::number_unsigned:
  18826. case value_t::string:
  18827. case value_t::binary:
  18828. {
  18829. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18830. {
  18831. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  18832. }
  18833. if (is_string())
  18834. {
  18835. AllocatorType<string_t> alloc;
  18836. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18837. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18838. m_value.string = nullptr;
  18839. }
  18840. else if (is_binary())
  18841. {
  18842. AllocatorType<binary_t> alloc;
  18843. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18844. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18845. m_value.binary = nullptr;
  18846. }
  18847. m_type = value_t::null;
  18848. assert_invariant();
  18849. break;
  18850. }
  18851. case value_t::object:
  18852. {
  18853. result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
  18854. break;
  18855. }
  18856. case value_t::array:
  18857. {
  18858. result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
  18859. break;
  18860. }
  18861. case value_t::null:
  18862. case value_t::discarded:
  18863. default:
  18864. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18865. }
  18866. return result;
  18867. }
  18868. /// @brief remove elements given an iterator range
  18869. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18870. template < class IteratorType, detail::enable_if_t <
  18871. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18872. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18873. IteratorType erase(IteratorType first, IteratorType last)
  18874. {
  18875. // make sure iterator fits the current value
  18876. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18877. {
  18878. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  18879. }
  18880. IteratorType result = end();
  18881. switch (m_type)
  18882. {
  18883. case value_t::boolean:
  18884. case value_t::number_float:
  18885. case value_t::number_integer:
  18886. case value_t::number_unsigned:
  18887. case value_t::string:
  18888. case value_t::binary:
  18889. {
  18890. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18891. || !last.m_it.primitive_iterator.is_end()))
  18892. {
  18893. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  18894. }
  18895. if (is_string())
  18896. {
  18897. AllocatorType<string_t> alloc;
  18898. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18899. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18900. m_value.string = nullptr;
  18901. }
  18902. else if (is_binary())
  18903. {
  18904. AllocatorType<binary_t> alloc;
  18905. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18906. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18907. m_value.binary = nullptr;
  18908. }
  18909. m_type = value_t::null;
  18910. assert_invariant();
  18911. break;
  18912. }
  18913. case value_t::object:
  18914. {
  18915. result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
  18916. last.m_it.object_iterator);
  18917. break;
  18918. }
  18919. case value_t::array:
  18920. {
  18921. result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
  18922. last.m_it.array_iterator);
  18923. break;
  18924. }
  18925. case value_t::null:
  18926. case value_t::discarded:
  18927. default:
  18928. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18929. }
  18930. return result;
  18931. }
  18932. private:
  18933. template < typename KeyType, detail::enable_if_t <
  18934. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18935. size_type erase_internal(KeyType && key)
  18936. {
  18937. // this erase only works for objects
  18938. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18939. {
  18940. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18941. }
  18942. return m_value.object->erase(std::forward<KeyType>(key));
  18943. }
  18944. template < typename KeyType, detail::enable_if_t <
  18945. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18946. size_type erase_internal(KeyType && key)
  18947. {
  18948. // this erase only works for objects
  18949. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18950. {
  18951. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18952. }
  18953. const auto it = m_value.object->find(std::forward<KeyType>(key));
  18954. if (it != m_value.object->end())
  18955. {
  18956. m_value.object->erase(it);
  18957. return 1;
  18958. }
  18959. return 0;
  18960. }
  18961. public:
  18962. /// @brief remove element from a JSON object given a key
  18963. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18964. size_type erase(const typename object_t::key_type& key)
  18965. {
  18966. // the indirection via erase_internal() is added to avoid making this
  18967. // function a template and thus de-rank it during overload resolution
  18968. return erase_internal(key);
  18969. }
  18970. /// @brief remove element from a JSON object given a key
  18971. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18972. template<class KeyType, detail::enable_if_t<
  18973. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18974. size_type erase(KeyType && key)
  18975. {
  18976. return erase_internal(std::forward<KeyType>(key));
  18977. }
  18978. /// @brief remove element from a JSON array given an index
  18979. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18980. void erase(const size_type idx)
  18981. {
  18982. // this erase only works for arrays
  18983. if (JSON_HEDLEY_LIKELY(is_array()))
  18984. {
  18985. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  18986. {
  18987. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18988. }
  18989. m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
  18990. }
  18991. else
  18992. {
  18993. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18994. }
  18995. }
  18996. /// @}
  18997. ////////////
  18998. // lookup //
  18999. ////////////
  19000. /// @name lookup
  19001. /// @{
  19002. /// @brief find an element in a JSON object
  19003. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19004. iterator find(const typename object_t::key_type& key)
  19005. {
  19006. auto result = end();
  19007. if (is_object())
  19008. {
  19009. result.m_it.object_iterator = m_value.object->find(key);
  19010. }
  19011. return result;
  19012. }
  19013. /// @brief find an element in a JSON object
  19014. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19015. const_iterator find(const typename object_t::key_type& key) const
  19016. {
  19017. auto result = cend();
  19018. if (is_object())
  19019. {
  19020. result.m_it.object_iterator = m_value.object->find(key);
  19021. }
  19022. return result;
  19023. }
  19024. /// @brief find an element in a JSON object
  19025. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19026. template<class KeyType, detail::enable_if_t<
  19027. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19028. iterator find(KeyType && key)
  19029. {
  19030. auto result = end();
  19031. if (is_object())
  19032. {
  19033. result.m_it.object_iterator = m_value.object->find(std::forward<KeyType>(key));
  19034. }
  19035. return result;
  19036. }
  19037. /// @brief find an element in a JSON object
  19038. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19039. template<class KeyType, detail::enable_if_t<
  19040. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19041. const_iterator find(KeyType && key) const
  19042. {
  19043. auto result = cend();
  19044. if (is_object())
  19045. {
  19046. result.m_it.object_iterator = m_value.object->find(std::forward<KeyType>(key));
  19047. }
  19048. return result;
  19049. }
  19050. /// @brief returns the number of occurrences of a key in a JSON object
  19051. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19052. size_type count(const typename object_t::key_type& key) const
  19053. {
  19054. // return 0 for all nonobject types
  19055. return is_object() ? m_value.object->count(key) : 0;
  19056. }
  19057. /// @brief returns the number of occurrences of a key in a JSON object
  19058. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19059. template<class KeyType, detail::enable_if_t<
  19060. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19061. size_type count(KeyType && key) const
  19062. {
  19063. // return 0 for all nonobject types
  19064. return is_object() ? m_value.object->count(std::forward<KeyType>(key)) : 0;
  19065. }
  19066. /// @brief check the existence of an element in a JSON object
  19067. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19068. bool contains(const typename object_t::key_type& key) const
  19069. {
  19070. return is_object() && m_value.object->find(key) != m_value.object->end();
  19071. }
  19072. /// @brief check the existence of an element in a JSON object
  19073. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19074. template<class KeyType, detail::enable_if_t<
  19075. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19076. bool contains(KeyType && key) const
  19077. {
  19078. return is_object() && m_value.object->find(std::forward<KeyType>(key)) != m_value.object->end();
  19079. }
  19080. /// @brief check the existence of an element in a JSON object given a JSON pointer
  19081. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19082. bool contains(const json_pointer& ptr) const
  19083. {
  19084. return ptr.contains(this);
  19085. }
  19086. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  19087. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19088. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  19089. {
  19090. return ptr.contains(this);
  19091. }
  19092. /// @}
  19093. ///////////////
  19094. // iterators //
  19095. ///////////////
  19096. /// @name iterators
  19097. /// @{
  19098. /// @brief returns an iterator to the first element
  19099. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19100. iterator begin() noexcept
  19101. {
  19102. iterator result(this);
  19103. result.set_begin();
  19104. return result;
  19105. }
  19106. /// @brief returns an iterator to the first element
  19107. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19108. const_iterator begin() const noexcept
  19109. {
  19110. return cbegin();
  19111. }
  19112. /// @brief returns a const iterator to the first element
  19113. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  19114. const_iterator cbegin() const noexcept
  19115. {
  19116. const_iterator result(this);
  19117. result.set_begin();
  19118. return result;
  19119. }
  19120. /// @brief returns an iterator to one past the last element
  19121. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19122. iterator end() noexcept
  19123. {
  19124. iterator result(this);
  19125. result.set_end();
  19126. return result;
  19127. }
  19128. /// @brief returns an iterator to one past the last element
  19129. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19130. const_iterator end() const noexcept
  19131. {
  19132. return cend();
  19133. }
  19134. /// @brief returns an iterator to one past the last element
  19135. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  19136. const_iterator cend() const noexcept
  19137. {
  19138. const_iterator result(this);
  19139. result.set_end();
  19140. return result;
  19141. }
  19142. /// @brief returns an iterator to the reverse-beginning
  19143. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19144. reverse_iterator rbegin() noexcept
  19145. {
  19146. return reverse_iterator(end());
  19147. }
  19148. /// @brief returns an iterator to the reverse-beginning
  19149. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19150. const_reverse_iterator rbegin() const noexcept
  19151. {
  19152. return crbegin();
  19153. }
  19154. /// @brief returns an iterator to the reverse-end
  19155. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19156. reverse_iterator rend() noexcept
  19157. {
  19158. return reverse_iterator(begin());
  19159. }
  19160. /// @brief returns an iterator to the reverse-end
  19161. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19162. const_reverse_iterator rend() const noexcept
  19163. {
  19164. return crend();
  19165. }
  19166. /// @brief returns a const reverse iterator to the last element
  19167. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  19168. const_reverse_iterator crbegin() const noexcept
  19169. {
  19170. return const_reverse_iterator(cend());
  19171. }
  19172. /// @brief returns a const reverse iterator to one before the first
  19173. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  19174. const_reverse_iterator crend() const noexcept
  19175. {
  19176. return const_reverse_iterator(cbegin());
  19177. }
  19178. public:
  19179. /// @brief wrapper to access iterator member functions in range-based for
  19180. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19181. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19182. /// version 4.0.0 of the library. Please use @ref items() instead;
  19183. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19184. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19185. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19186. {
  19187. return ref.items();
  19188. }
  19189. /// @brief wrapper to access iterator member functions in range-based for
  19190. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19191. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19192. /// version 4.0.0 of the library. Please use @ref items() instead;
  19193. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19194. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19195. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19196. {
  19197. return ref.items();
  19198. }
  19199. /// @brief helper to access iterator member functions in range-based for
  19200. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19201. iteration_proxy<iterator> items() noexcept
  19202. {
  19203. return iteration_proxy<iterator>(*this);
  19204. }
  19205. /// @brief helper to access iterator member functions in range-based for
  19206. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19207. iteration_proxy<const_iterator> items() const noexcept
  19208. {
  19209. return iteration_proxy<const_iterator>(*this);
  19210. }
  19211. /// @}
  19212. //////////////
  19213. // capacity //
  19214. //////////////
  19215. /// @name capacity
  19216. /// @{
  19217. /// @brief checks whether the container is empty.
  19218. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  19219. bool empty() const noexcept
  19220. {
  19221. switch (m_type)
  19222. {
  19223. case value_t::null:
  19224. {
  19225. // null values are empty
  19226. return true;
  19227. }
  19228. case value_t::array:
  19229. {
  19230. // delegate call to array_t::empty()
  19231. return m_value.array->empty();
  19232. }
  19233. case value_t::object:
  19234. {
  19235. // delegate call to object_t::empty()
  19236. return m_value.object->empty();
  19237. }
  19238. case value_t::string:
  19239. case value_t::boolean:
  19240. case value_t::number_integer:
  19241. case value_t::number_unsigned:
  19242. case value_t::number_float:
  19243. case value_t::binary:
  19244. case value_t::discarded:
  19245. default:
  19246. {
  19247. // all other types are nonempty
  19248. return false;
  19249. }
  19250. }
  19251. }
  19252. /// @brief returns the number of elements
  19253. /// @sa https://json.nlohmann.me/api/basic_json/size/
  19254. size_type size() const noexcept
  19255. {
  19256. switch (m_type)
  19257. {
  19258. case value_t::null:
  19259. {
  19260. // null values are empty
  19261. return 0;
  19262. }
  19263. case value_t::array:
  19264. {
  19265. // delegate call to array_t::size()
  19266. return m_value.array->size();
  19267. }
  19268. case value_t::object:
  19269. {
  19270. // delegate call to object_t::size()
  19271. return m_value.object->size();
  19272. }
  19273. case value_t::string:
  19274. case value_t::boolean:
  19275. case value_t::number_integer:
  19276. case value_t::number_unsigned:
  19277. case value_t::number_float:
  19278. case value_t::binary:
  19279. case value_t::discarded:
  19280. default:
  19281. {
  19282. // all other types have size 1
  19283. return 1;
  19284. }
  19285. }
  19286. }
  19287. /// @brief returns the maximum possible number of elements
  19288. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  19289. size_type max_size() const noexcept
  19290. {
  19291. switch (m_type)
  19292. {
  19293. case value_t::array:
  19294. {
  19295. // delegate call to array_t::max_size()
  19296. return m_value.array->max_size();
  19297. }
  19298. case value_t::object:
  19299. {
  19300. // delegate call to object_t::max_size()
  19301. return m_value.object->max_size();
  19302. }
  19303. case value_t::null:
  19304. case value_t::string:
  19305. case value_t::boolean:
  19306. case value_t::number_integer:
  19307. case value_t::number_unsigned:
  19308. case value_t::number_float:
  19309. case value_t::binary:
  19310. case value_t::discarded:
  19311. default:
  19312. {
  19313. // all other types have max_size() == size()
  19314. return size();
  19315. }
  19316. }
  19317. }
  19318. /// @}
  19319. ///////////////
  19320. // modifiers //
  19321. ///////////////
  19322. /// @name modifiers
  19323. /// @{
  19324. /// @brief clears the contents
  19325. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  19326. void clear() noexcept
  19327. {
  19328. switch (m_type)
  19329. {
  19330. case value_t::number_integer:
  19331. {
  19332. m_value.number_integer = 0;
  19333. break;
  19334. }
  19335. case value_t::number_unsigned:
  19336. {
  19337. m_value.number_unsigned = 0;
  19338. break;
  19339. }
  19340. case value_t::number_float:
  19341. {
  19342. m_value.number_float = 0.0;
  19343. break;
  19344. }
  19345. case value_t::boolean:
  19346. {
  19347. m_value.boolean = false;
  19348. break;
  19349. }
  19350. case value_t::string:
  19351. {
  19352. m_value.string->clear();
  19353. break;
  19354. }
  19355. case value_t::binary:
  19356. {
  19357. m_value.binary->clear();
  19358. break;
  19359. }
  19360. case value_t::array:
  19361. {
  19362. m_value.array->clear();
  19363. break;
  19364. }
  19365. case value_t::object:
  19366. {
  19367. m_value.object->clear();
  19368. break;
  19369. }
  19370. case value_t::null:
  19371. case value_t::discarded:
  19372. default:
  19373. break;
  19374. }
  19375. }
  19376. /// @brief add an object to an array
  19377. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19378. void push_back(basic_json&& val)
  19379. {
  19380. // push_back only works for null objects or arrays
  19381. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19382. {
  19383. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19384. }
  19385. // transform null object into an array
  19386. if (is_null())
  19387. {
  19388. m_type = value_t::array;
  19389. m_value = value_t::array;
  19390. assert_invariant();
  19391. }
  19392. // add element to array (move semantics)
  19393. const auto old_capacity = m_value.array->capacity();
  19394. m_value.array->push_back(std::move(val));
  19395. set_parent(m_value.array->back(), old_capacity);
  19396. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  19397. }
  19398. /// @brief add an object to an array
  19399. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19400. reference operator+=(basic_json&& val)
  19401. {
  19402. push_back(std::move(val));
  19403. return *this;
  19404. }
  19405. /// @brief add an object to an array
  19406. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19407. void push_back(const basic_json& val)
  19408. {
  19409. // push_back only works for null objects or arrays
  19410. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19411. {
  19412. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19413. }
  19414. // transform null object into an array
  19415. if (is_null())
  19416. {
  19417. m_type = value_t::array;
  19418. m_value = value_t::array;
  19419. assert_invariant();
  19420. }
  19421. // add element to array
  19422. const auto old_capacity = m_value.array->capacity();
  19423. m_value.array->push_back(val);
  19424. set_parent(m_value.array->back(), old_capacity);
  19425. }
  19426. /// @brief add an object to an array
  19427. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19428. reference operator+=(const basic_json& val)
  19429. {
  19430. push_back(val);
  19431. return *this;
  19432. }
  19433. /// @brief add an object to an object
  19434. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19435. void push_back(const typename object_t::value_type& val)
  19436. {
  19437. // push_back only works for null objects or objects
  19438. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19439. {
  19440. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19441. }
  19442. // transform null object into an object
  19443. if (is_null())
  19444. {
  19445. m_type = value_t::object;
  19446. m_value = value_t::object;
  19447. assert_invariant();
  19448. }
  19449. // add element to object
  19450. auto res = m_value.object->insert(val);
  19451. set_parent(res.first->second);
  19452. }
  19453. /// @brief add an object to an object
  19454. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19455. reference operator+=(const typename object_t::value_type& val)
  19456. {
  19457. push_back(val);
  19458. return *this;
  19459. }
  19460. /// @brief add an object to an object
  19461. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19462. void push_back(initializer_list_t init)
  19463. {
  19464. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  19465. {
  19466. basic_json&& key = init.begin()->moved_or_copied();
  19467. push_back(typename object_t::value_type(
  19468. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  19469. }
  19470. else
  19471. {
  19472. push_back(basic_json(init));
  19473. }
  19474. }
  19475. /// @brief add an object to an object
  19476. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19477. reference operator+=(initializer_list_t init)
  19478. {
  19479. push_back(init);
  19480. return *this;
  19481. }
  19482. /// @brief add an object to an array
  19483. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  19484. template<class... Args>
  19485. reference emplace_back(Args&& ... args)
  19486. {
  19487. // emplace_back only works for null objects or arrays
  19488. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19489. {
  19490. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  19491. }
  19492. // transform null object into an array
  19493. if (is_null())
  19494. {
  19495. m_type = value_t::array;
  19496. m_value = value_t::array;
  19497. assert_invariant();
  19498. }
  19499. // add element to array (perfect forwarding)
  19500. const auto old_capacity = m_value.array->capacity();
  19501. m_value.array->emplace_back(std::forward<Args>(args)...);
  19502. return set_parent(m_value.array->back(), old_capacity);
  19503. }
  19504. /// @brief add an object to an object if key does not exist
  19505. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  19506. template<class... Args>
  19507. std::pair<iterator, bool> emplace(Args&& ... args)
  19508. {
  19509. // emplace only works for null objects or arrays
  19510. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19511. {
  19512. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  19513. }
  19514. // transform null object into an object
  19515. if (is_null())
  19516. {
  19517. m_type = value_t::object;
  19518. m_value = value_t::object;
  19519. assert_invariant();
  19520. }
  19521. // add element to array (perfect forwarding)
  19522. auto res = m_value.object->emplace(std::forward<Args>(args)...);
  19523. set_parent(res.first->second);
  19524. // create result iterator and set iterator to the result of emplace
  19525. auto it = begin();
  19526. it.m_it.object_iterator = res.first;
  19527. // return pair of iterator and boolean
  19528. return {it, res.second};
  19529. }
  19530. /// Helper for insertion of an iterator
  19531. /// @note: This uses std::distance to support GCC 4.8,
  19532. /// see https://github.com/nlohmann/json/pull/1257
  19533. template<typename... Args>
  19534. iterator insert_iterator(const_iterator pos, Args&& ... args)
  19535. {
  19536. iterator result(this);
  19537. JSON_ASSERT(m_value.array != nullptr);
  19538. auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
  19539. m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  19540. result.m_it.array_iterator = m_value.array->begin() + insert_pos;
  19541. // This could have been written as:
  19542. // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  19543. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  19544. set_parents();
  19545. return result;
  19546. }
  19547. /// @brief inserts element into array
  19548. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19549. iterator insert(const_iterator pos, const basic_json& val)
  19550. {
  19551. // insert only works for arrays
  19552. if (JSON_HEDLEY_LIKELY(is_array()))
  19553. {
  19554. // check if iterator pos fits to this JSON value
  19555. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19556. {
  19557. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19558. }
  19559. // insert to array and return iterator
  19560. return insert_iterator(pos, val);
  19561. }
  19562. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19563. }
  19564. /// @brief inserts element into array
  19565. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19566. iterator insert(const_iterator pos, basic_json&& val)
  19567. {
  19568. return insert(pos, val);
  19569. }
  19570. /// @brief inserts copies of element into array
  19571. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19572. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  19573. {
  19574. // insert only works for arrays
  19575. if (JSON_HEDLEY_LIKELY(is_array()))
  19576. {
  19577. // check if iterator pos fits to this JSON value
  19578. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19579. {
  19580. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19581. }
  19582. // insert to array and return iterator
  19583. return insert_iterator(pos, cnt, val);
  19584. }
  19585. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19586. }
  19587. /// @brief inserts range of elements into array
  19588. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19589. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  19590. {
  19591. // insert only works for arrays
  19592. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19593. {
  19594. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19595. }
  19596. // check if iterator pos fits to this JSON value
  19597. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19598. {
  19599. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19600. }
  19601. // check if range iterators belong to the same JSON object
  19602. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19603. {
  19604. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19605. }
  19606. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  19607. {
  19608. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  19609. }
  19610. // insert to array and return iterator
  19611. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  19612. }
  19613. /// @brief inserts elements from initializer list into array
  19614. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19615. iterator insert(const_iterator pos, initializer_list_t ilist)
  19616. {
  19617. // insert only works for arrays
  19618. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19619. {
  19620. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19621. }
  19622. // check if iterator pos fits to this JSON value
  19623. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19624. {
  19625. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19626. }
  19627. // insert to array and return iterator
  19628. return insert_iterator(pos, ilist.begin(), ilist.end());
  19629. }
  19630. /// @brief inserts range of elements into object
  19631. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19632. void insert(const_iterator first, const_iterator last)
  19633. {
  19634. // insert only works for objects
  19635. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19636. {
  19637. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19638. }
  19639. // check if range iterators belong to the same JSON object
  19640. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19641. {
  19642. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19643. }
  19644. // passed iterators must belong to objects
  19645. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19646. {
  19647. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  19648. }
  19649. m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  19650. }
  19651. /// @brief updates a JSON object from another object, overwriting existing keys
  19652. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19653. void update(const_reference j, bool merge_objects = false)
  19654. {
  19655. update(j.begin(), j.end(), merge_objects);
  19656. }
  19657. /// @brief updates a JSON object from another object, overwriting existing keys
  19658. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19659. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  19660. {
  19661. // implicitly convert null value to an empty object
  19662. if (is_null())
  19663. {
  19664. m_type = value_t::object;
  19665. m_value.object = create<object_t>();
  19666. assert_invariant();
  19667. }
  19668. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19669. {
  19670. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  19671. }
  19672. // check if range iterators belong to the same JSON object
  19673. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19674. {
  19675. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19676. }
  19677. // passed iterators must belong to objects
  19678. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19679. {
  19680. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  19681. }
  19682. for (auto it = first; it != last; ++it)
  19683. {
  19684. if (merge_objects && it.value().is_object())
  19685. {
  19686. auto it2 = m_value.object->find(it.key());
  19687. if (it2 != m_value.object->end())
  19688. {
  19689. it2->second.update(it.value(), true);
  19690. continue;
  19691. }
  19692. }
  19693. m_value.object->operator[](it.key()) = it.value();
  19694. #if JSON_DIAGNOSTICS
  19695. m_value.object->operator[](it.key()).m_parent = this;
  19696. #endif
  19697. }
  19698. }
  19699. /// @brief exchanges the values
  19700. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19701. void swap(reference other) noexcept (
  19702. std::is_nothrow_move_constructible<value_t>::value&&
  19703. std::is_nothrow_move_assignable<value_t>::value&&
  19704. std::is_nothrow_move_constructible<json_value>::value&&
  19705. std::is_nothrow_move_assignable<json_value>::value
  19706. )
  19707. {
  19708. std::swap(m_type, other.m_type);
  19709. std::swap(m_value, other.m_value);
  19710. set_parents();
  19711. other.set_parents();
  19712. assert_invariant();
  19713. }
  19714. /// @brief exchanges the values
  19715. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19716. friend void swap(reference left, reference right) noexcept (
  19717. std::is_nothrow_move_constructible<value_t>::value&&
  19718. std::is_nothrow_move_assignable<value_t>::value&&
  19719. std::is_nothrow_move_constructible<json_value>::value&&
  19720. std::is_nothrow_move_assignable<json_value>::value
  19721. )
  19722. {
  19723. left.swap(right);
  19724. }
  19725. /// @brief exchanges the values
  19726. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19727. void swap(array_t& other) // NOLINT(bugprone-exception-escape)
  19728. {
  19729. // swap only works for arrays
  19730. if (JSON_HEDLEY_LIKELY(is_array()))
  19731. {
  19732. using std::swap;
  19733. swap(*(m_value.array), other);
  19734. }
  19735. else
  19736. {
  19737. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  19738. }
  19739. }
  19740. /// @brief exchanges the values
  19741. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19742. void swap(object_t& other) // NOLINT(bugprone-exception-escape)
  19743. {
  19744. // swap only works for objects
  19745. if (JSON_HEDLEY_LIKELY(is_object()))
  19746. {
  19747. using std::swap;
  19748. swap(*(m_value.object), other);
  19749. }
  19750. else
  19751. {
  19752. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  19753. }
  19754. }
  19755. /// @brief exchanges the values
  19756. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19757. void swap(string_t& other) // NOLINT(bugprone-exception-escape)
  19758. {
  19759. // swap only works for strings
  19760. if (JSON_HEDLEY_LIKELY(is_string()))
  19761. {
  19762. using std::swap;
  19763. swap(*(m_value.string), other);
  19764. }
  19765. else
  19766. {
  19767. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  19768. }
  19769. }
  19770. /// @brief exchanges the values
  19771. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19772. void swap(binary_t& other) // NOLINT(bugprone-exception-escape)
  19773. {
  19774. // swap only works for strings
  19775. if (JSON_HEDLEY_LIKELY(is_binary()))
  19776. {
  19777. using std::swap;
  19778. swap(*(m_value.binary), other);
  19779. }
  19780. else
  19781. {
  19782. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  19783. }
  19784. }
  19785. /// @brief exchanges the values
  19786. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19787. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  19788. {
  19789. // swap only works for strings
  19790. if (JSON_HEDLEY_LIKELY(is_binary()))
  19791. {
  19792. using std::swap;
  19793. swap(*(m_value.binary), other);
  19794. }
  19795. else
  19796. {
  19797. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  19798. }
  19799. }
  19800. /// @}
  19801. //////////////////////////////////////////
  19802. // lexicographical comparison operators //
  19803. //////////////////////////////////////////
  19804. /// @name lexicographical comparison operators
  19805. /// @{
  19806. // note parentheses around operands are necessary; see
  19807. // https://github.com/nlohmann/json/issues/1530
  19808. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  19809. const auto lhs_type = lhs.type(); \
  19810. const auto rhs_type = rhs.type(); \
  19811. \
  19812. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  19813. { \
  19814. switch (lhs_type) \
  19815. { \
  19816. case value_t::array: \
  19817. return (*lhs.m_value.array) op (*rhs.m_value.array); \
  19818. \
  19819. case value_t::object: \
  19820. return (*lhs.m_value.object) op (*rhs.m_value.object); \
  19821. \
  19822. case value_t::null: \
  19823. return (null_result); \
  19824. \
  19825. case value_t::string: \
  19826. return (*lhs.m_value.string) op (*rhs.m_value.string); \
  19827. \
  19828. case value_t::boolean: \
  19829. return (lhs.m_value.boolean) op (rhs.m_value.boolean); \
  19830. \
  19831. case value_t::number_integer: \
  19832. return (lhs.m_value.number_integer) op (rhs.m_value.number_integer); \
  19833. \
  19834. case value_t::number_unsigned: \
  19835. return (lhs.m_value.number_unsigned) op (rhs.m_value.number_unsigned); \
  19836. \
  19837. case value_t::number_float: \
  19838. return (lhs.m_value.number_float) op (rhs.m_value.number_float); \
  19839. \
  19840. case value_t::binary: \
  19841. return (*lhs.m_value.binary) op (*rhs.m_value.binary); \
  19842. \
  19843. case value_t::discarded: \
  19844. default: \
  19845. return (unordered_result); \
  19846. } \
  19847. } \
  19848. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  19849. { \
  19850. return static_cast<number_float_t>(lhs.m_value.number_integer) op rhs.m_value.number_float; \
  19851. } \
  19852. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  19853. { \
  19854. return lhs.m_value.number_float op static_cast<number_float_t>(rhs.m_value.number_integer); \
  19855. } \
  19856. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  19857. { \
  19858. return static_cast<number_float_t>(lhs.m_value.number_unsigned) op rhs.m_value.number_float; \
  19859. } \
  19860. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  19861. { \
  19862. return lhs.m_value.number_float op static_cast<number_float_t>(rhs.m_value.number_unsigned); \
  19863. } \
  19864. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  19865. { \
  19866. return static_cast<number_integer_t>(lhs.m_value.number_unsigned) op rhs.m_value.number_integer; \
  19867. } \
  19868. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  19869. { \
  19870. return lhs.m_value.number_integer op static_cast<number_integer_t>(rhs.m_value.number_unsigned); \
  19871. } \
  19872. else if(compares_unordered(lhs, rhs))\
  19873. {\
  19874. return (unordered_result);\
  19875. }\
  19876. \
  19877. return (default_result);
  19878. JSON_PRIVATE_UNLESS_TESTED:
  19879. // returns true if:
  19880. // - any operand is NaN and the other operand is of number type
  19881. // - any operand is discarded
  19882. // in legacy mode, discarded values are considered ordered if
  19883. // an operation is computed as an odd number of inverses of others
  19884. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  19885. {
  19886. if ((lhs.is_number_float() && std::isnan(lhs.m_value.number_float) && rhs.is_number())
  19887. || (rhs.is_number_float() && std::isnan(rhs.m_value.number_float) && lhs.is_number()))
  19888. {
  19889. return true;
  19890. }
  19891. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19892. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  19893. #else
  19894. static_cast<void>(inverse);
  19895. return lhs.is_discarded() || rhs.is_discarded();
  19896. #endif
  19897. }
  19898. private:
  19899. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  19900. {
  19901. return compares_unordered(*this, rhs, inverse);
  19902. }
  19903. public:
  19904. #if JSON_HAS_THREE_WAY_COMPARISON
  19905. /// @brief comparison: equal
  19906. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19907. bool operator==(const_reference rhs) const noexcept
  19908. {
  19909. #ifdef __GNUC__
  19910. #pragma GCC diagnostic push
  19911. #pragma GCC diagnostic ignored "-Wfloat-equal"
  19912. #endif
  19913. const_reference lhs = *this;
  19914. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  19915. #ifdef __GNUC__
  19916. #pragma GCC diagnostic pop
  19917. #endif
  19918. }
  19919. /// @brief comparison: equal
  19920. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19921. template<typename ScalarType>
  19922. requires std::is_scalar_v<ScalarType>
  19923. bool operator==(ScalarType rhs) const noexcept
  19924. {
  19925. return *this == basic_json(rhs);
  19926. }
  19927. /// @brief comparison: not equal
  19928. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19929. bool operator!=(const_reference rhs) const noexcept
  19930. {
  19931. if (compares_unordered(rhs, true))
  19932. {
  19933. return false;
  19934. }
  19935. return !operator==(rhs);
  19936. }
  19937. /// @brief comparison: 3-way
  19938. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19939. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  19940. {
  19941. const_reference lhs = *this;
  19942. // default_result is used if we cannot compare values. In that case,
  19943. // we compare types.
  19944. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  19945. std::partial_ordering::equivalent,
  19946. std::partial_ordering::unordered,
  19947. lhs_type <=> rhs_type) // *NOPAD*
  19948. }
  19949. /// @brief comparison: 3-way
  19950. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19951. template<typename ScalarType>
  19952. requires std::is_scalar_v<ScalarType>
  19953. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  19954. {
  19955. return *this <=> basic_json(rhs); // *NOPAD*
  19956. }
  19957. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19958. // all operators that are computed as an odd number of inverses of others
  19959. // need to be overloaded to emulate the legacy comparison behavior
  19960. /// @brief comparison: less than or equal
  19961. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19962. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19963. bool operator<=(const_reference rhs) const noexcept
  19964. {
  19965. if (compares_unordered(rhs, true))
  19966. {
  19967. return false;
  19968. }
  19969. return !(rhs < *this);
  19970. }
  19971. /// @brief comparison: less than or equal
  19972. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19973. template<typename ScalarType>
  19974. requires std::is_scalar_v<ScalarType>
  19975. bool operator<=(ScalarType rhs) const noexcept
  19976. {
  19977. return *this <= basic_json(rhs);
  19978. }
  19979. /// @brief comparison: greater than or equal
  19980. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19981. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19982. bool operator>=(const_reference rhs) const noexcept
  19983. {
  19984. if (compares_unordered(rhs, true))
  19985. {
  19986. return false;
  19987. }
  19988. return !(*this < rhs);
  19989. }
  19990. /// @brief comparison: greater than or equal
  19991. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19992. template<typename ScalarType>
  19993. requires std::is_scalar_v<ScalarType>
  19994. bool operator>=(ScalarType rhs) const noexcept
  19995. {
  19996. return *this >= basic_json(rhs);
  19997. }
  19998. #endif
  19999. #else
  20000. /// @brief comparison: equal
  20001. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20002. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20003. {
  20004. #ifdef __GNUC__
  20005. #pragma GCC diagnostic push
  20006. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20007. #endif
  20008. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20009. #ifdef __GNUC__
  20010. #pragma GCC diagnostic pop
  20011. #endif
  20012. }
  20013. /// @brief comparison: equal
  20014. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20015. template<typename ScalarType, typename std::enable_if<
  20016. std::is_scalar<ScalarType>::value, int>::type = 0>
  20017. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20018. {
  20019. return lhs == basic_json(rhs);
  20020. }
  20021. /// @brief comparison: equal
  20022. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20023. template<typename ScalarType, typename std::enable_if<
  20024. std::is_scalar<ScalarType>::value, int>::type = 0>
  20025. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20026. {
  20027. return basic_json(lhs) == rhs;
  20028. }
  20029. /// @brief comparison: not equal
  20030. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20031. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20032. {
  20033. if (compares_unordered(lhs, rhs, true))
  20034. {
  20035. return false;
  20036. }
  20037. return !(lhs == rhs);
  20038. }
  20039. /// @brief comparison: not equal
  20040. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20041. template<typename ScalarType, typename std::enable_if<
  20042. std::is_scalar<ScalarType>::value, int>::type = 0>
  20043. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20044. {
  20045. return lhs != basic_json(rhs);
  20046. }
  20047. /// @brief comparison: not equal
  20048. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20049. template<typename ScalarType, typename std::enable_if<
  20050. std::is_scalar<ScalarType>::value, int>::type = 0>
  20051. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20052. {
  20053. return basic_json(lhs) != rhs;
  20054. }
  20055. /// @brief comparison: less than
  20056. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20057. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20058. {
  20059. // default_result is used if we cannot compare values. In that case,
  20060. // we compare types. Note we have to call the operator explicitly,
  20061. // because MSVC has problems otherwise.
  20062. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20063. }
  20064. /// @brief comparison: less than
  20065. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20066. template<typename ScalarType, typename std::enable_if<
  20067. std::is_scalar<ScalarType>::value, int>::type = 0>
  20068. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20069. {
  20070. return lhs < basic_json(rhs);
  20071. }
  20072. /// @brief comparison: less than
  20073. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20074. template<typename ScalarType, typename std::enable_if<
  20075. std::is_scalar<ScalarType>::value, int>::type = 0>
  20076. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20077. {
  20078. return basic_json(lhs) < rhs;
  20079. }
  20080. /// @brief comparison: less than or equal
  20081. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20082. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20083. {
  20084. if (compares_unordered(lhs, rhs, true))
  20085. {
  20086. return false;
  20087. }
  20088. return !(rhs < lhs);
  20089. }
  20090. /// @brief comparison: less than or equal
  20091. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20092. template<typename ScalarType, typename std::enable_if<
  20093. std::is_scalar<ScalarType>::value, int>::type = 0>
  20094. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20095. {
  20096. return lhs <= basic_json(rhs);
  20097. }
  20098. /// @brief comparison: less than or equal
  20099. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20100. template<typename ScalarType, typename std::enable_if<
  20101. std::is_scalar<ScalarType>::value, int>::type = 0>
  20102. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20103. {
  20104. return basic_json(lhs) <= rhs;
  20105. }
  20106. /// @brief comparison: greater than
  20107. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20108. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20109. {
  20110. // double inverse
  20111. if (compares_unordered(lhs, rhs))
  20112. {
  20113. return false;
  20114. }
  20115. return !(lhs <= rhs);
  20116. }
  20117. /// @brief comparison: greater than
  20118. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20119. template<typename ScalarType, typename std::enable_if<
  20120. std::is_scalar<ScalarType>::value, int>::type = 0>
  20121. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  20122. {
  20123. return lhs > basic_json(rhs);
  20124. }
  20125. /// @brief comparison: greater than
  20126. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20127. template<typename ScalarType, typename std::enable_if<
  20128. std::is_scalar<ScalarType>::value, int>::type = 0>
  20129. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  20130. {
  20131. return basic_json(lhs) > rhs;
  20132. }
  20133. /// @brief comparison: greater than or equal
  20134. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20135. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  20136. {
  20137. if (compares_unordered(lhs, rhs, true))
  20138. {
  20139. return false;
  20140. }
  20141. return !(lhs < rhs);
  20142. }
  20143. /// @brief comparison: greater than or equal
  20144. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20145. template<typename ScalarType, typename std::enable_if<
  20146. std::is_scalar<ScalarType>::value, int>::type = 0>
  20147. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  20148. {
  20149. return lhs >= basic_json(rhs);
  20150. }
  20151. /// @brief comparison: greater than or equal
  20152. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20153. template<typename ScalarType, typename std::enable_if<
  20154. std::is_scalar<ScalarType>::value, int>::type = 0>
  20155. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  20156. {
  20157. return basic_json(lhs) >= rhs;
  20158. }
  20159. #endif
  20160. #undef JSON_IMPLEMENT_OPERATOR
  20161. /// @}
  20162. ///////////////////
  20163. // serialization //
  20164. ///////////////////
  20165. /// @name serialization
  20166. /// @{
  20167. #ifndef JSON_NO_IO
  20168. /// @brief serialize to stream
  20169. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20170. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20171. {
  20172. // read width member and use it as indentation parameter if nonzero
  20173. const bool pretty_print = o.width() > 0;
  20174. const auto indentation = pretty_print ? o.width() : 0;
  20175. // reset width to 0 for subsequent calls to this stream
  20176. o.width(0);
  20177. // do the actual serialization
  20178. serializer s(detail::output_adapter<char>(o), o.fill());
  20179. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20180. return o;
  20181. }
  20182. /// @brief serialize to stream
  20183. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20184. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  20185. /// version 4.0.0 of the library. Please use
  20186. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  20187. /// replace calls like `j >> o;` with `o << j;`.
  20188. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20189. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20190. {
  20191. return o << j;
  20192. }
  20193. #endif // JSON_NO_IO
  20194. /// @}
  20195. /////////////////////
  20196. // deserialization //
  20197. /////////////////////
  20198. /// @name deserialization
  20199. /// @{
  20200. /// @brief deserialize from a compatible input
  20201. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20202. template<typename InputType>
  20203. JSON_HEDLEY_WARN_UNUSED_RESULT
  20204. static basic_json parse(InputType&& i,
  20205. const parser_callback_t cb = nullptr,
  20206. const bool allow_exceptions = true,
  20207. const bool ignore_comments = false)
  20208. {
  20209. basic_json result;
  20210. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20211. return result;
  20212. }
  20213. /// @brief deserialize from a pair of character iterators
  20214. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20215. template<typename IteratorType>
  20216. JSON_HEDLEY_WARN_UNUSED_RESULT
  20217. static basic_json parse(IteratorType first,
  20218. IteratorType last,
  20219. const parser_callback_t cb = nullptr,
  20220. const bool allow_exceptions = true,
  20221. const bool ignore_comments = false)
  20222. {
  20223. basic_json result;
  20224. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20225. return result;
  20226. }
  20227. JSON_HEDLEY_WARN_UNUSED_RESULT
  20228. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20229. static basic_json parse(detail::span_input_adapter&& i,
  20230. const parser_callback_t cb = nullptr,
  20231. const bool allow_exceptions = true,
  20232. const bool ignore_comments = false)
  20233. {
  20234. basic_json result;
  20235. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  20236. return result;
  20237. }
  20238. /// @brief check if the input is valid JSON
  20239. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20240. template<typename InputType>
  20241. static bool accept(InputType&& i,
  20242. const bool ignore_comments = false)
  20243. {
  20244. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20245. }
  20246. /// @brief check if the input is valid JSON
  20247. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20248. template<typename IteratorType>
  20249. static bool accept(IteratorType first, IteratorType last,
  20250. const bool ignore_comments = false)
  20251. {
  20252. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  20253. }
  20254. JSON_HEDLEY_WARN_UNUSED_RESULT
  20255. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  20256. static bool accept(detail::span_input_adapter&& i,
  20257. const bool ignore_comments = false)
  20258. {
  20259. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  20260. }
  20261. /// @brief generate SAX events
  20262. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20263. template <typename InputType, typename SAX>
  20264. JSON_HEDLEY_NON_NULL(2)
  20265. static bool sax_parse(InputType&& i, SAX* sax,
  20266. input_format_t format = input_format_t::json,
  20267. const bool strict = true,
  20268. const bool ignore_comments = false)
  20269. {
  20270. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20271. return format == input_format_t::json
  20272. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20273. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20274. }
  20275. /// @brief generate SAX events
  20276. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20277. template<class IteratorType, class SAX>
  20278. JSON_HEDLEY_NON_NULL(3)
  20279. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  20280. input_format_t format = input_format_t::json,
  20281. const bool strict = true,
  20282. const bool ignore_comments = false)
  20283. {
  20284. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20285. return format == input_format_t::json
  20286. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20287. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20288. }
  20289. /// @brief generate SAX events
  20290. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20291. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  20292. /// version 4.0.0 of the library. Please use
  20293. /// sax_parse(ptr, ptr + len) instead.
  20294. template <typename SAX>
  20295. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  20296. JSON_HEDLEY_NON_NULL(2)
  20297. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  20298. input_format_t format = input_format_t::json,
  20299. const bool strict = true,
  20300. const bool ignore_comments = false)
  20301. {
  20302. auto ia = i.get();
  20303. return format == input_format_t::json
  20304. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20305. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20306. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20307. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20308. }
  20309. #ifndef JSON_NO_IO
  20310. /// @brief deserialize from stream
  20311. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20312. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  20313. /// version 4.0.0 of the library. Please use
  20314. /// operator>>(std::istream&, basic_json&) instead; that is,
  20315. /// replace calls like `j << i;` with `i >> j;`.
  20316. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  20317. friend std::istream& operator<<(basic_json& j, std::istream& i)
  20318. {
  20319. return operator>>(i, j);
  20320. }
  20321. /// @brief deserialize from stream
  20322. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20323. friend std::istream& operator>>(std::istream& i, basic_json& j)
  20324. {
  20325. parser(detail::input_adapter(i)).parse(false, j);
  20326. return i;
  20327. }
  20328. #endif // JSON_NO_IO
  20329. /// @}
  20330. ///////////////////////////
  20331. // convenience functions //
  20332. ///////////////////////////
  20333. /// @brief return the type as string
  20334. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  20335. JSON_HEDLEY_RETURNS_NON_NULL
  20336. const char* type_name() const noexcept
  20337. {
  20338. switch (m_type)
  20339. {
  20340. case value_t::null:
  20341. return "null";
  20342. case value_t::object:
  20343. return "object";
  20344. case value_t::array:
  20345. return "array";
  20346. case value_t::string:
  20347. return "string";
  20348. case value_t::boolean:
  20349. return "boolean";
  20350. case value_t::binary:
  20351. return "binary";
  20352. case value_t::discarded:
  20353. return "discarded";
  20354. case value_t::number_integer:
  20355. case value_t::number_unsigned:
  20356. case value_t::number_float:
  20357. default:
  20358. return "number";
  20359. }
  20360. }
  20361. JSON_PRIVATE_UNLESS_TESTED:
  20362. //////////////////////
  20363. // member variables //
  20364. //////////////////////
  20365. /// the type of the current element
  20366. value_t m_type = value_t::null;
  20367. /// the value of the current element
  20368. json_value m_value = {};
  20369. #if JSON_DIAGNOSTICS
  20370. /// a pointer to a parent value (for debugging purposes)
  20371. basic_json* m_parent = nullptr;
  20372. #endif
  20373. //////////////////////////////////////////
  20374. // binary serialization/deserialization //
  20375. //////////////////////////////////////////
  20376. /// @name binary serialization/deserialization support
  20377. /// @{
  20378. public:
  20379. /// @brief create a CBOR serialization of a given JSON value
  20380. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20381. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  20382. {
  20383. std::vector<std::uint8_t> result;
  20384. to_cbor(j, result);
  20385. return result;
  20386. }
  20387. /// @brief create a CBOR serialization of a given JSON value
  20388. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20389. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20390. {
  20391. binary_writer<std::uint8_t>(o).write_cbor(j);
  20392. }
  20393. /// @brief create a CBOR serialization of a given JSON value
  20394. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20395. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  20396. {
  20397. binary_writer<char>(o).write_cbor(j);
  20398. }
  20399. /// @brief create a MessagePack serialization of a given JSON value
  20400. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20401. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  20402. {
  20403. std::vector<std::uint8_t> result;
  20404. to_msgpack(j, result);
  20405. return result;
  20406. }
  20407. /// @brief create a MessagePack serialization of a given JSON value
  20408. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20409. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20410. {
  20411. binary_writer<std::uint8_t>(o).write_msgpack(j);
  20412. }
  20413. /// @brief create a MessagePack serialization of a given JSON value
  20414. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20415. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  20416. {
  20417. binary_writer<char>(o).write_msgpack(j);
  20418. }
  20419. /// @brief create a UBJSON serialization of a given JSON value
  20420. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20421. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  20422. const bool use_size = false,
  20423. const bool use_type = false)
  20424. {
  20425. std::vector<std::uint8_t> result;
  20426. to_ubjson(j, result, use_size, use_type);
  20427. return result;
  20428. }
  20429. /// @brief create a UBJSON serialization of a given JSON value
  20430. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20431. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20432. const bool use_size = false, const bool use_type = false)
  20433. {
  20434. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  20435. }
  20436. /// @brief create a UBJSON serialization of a given JSON value
  20437. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20438. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  20439. const bool use_size = false, const bool use_type = false)
  20440. {
  20441. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  20442. }
  20443. /// @brief create a BJData serialization of a given JSON value
  20444. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20445. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  20446. const bool use_size = false,
  20447. const bool use_type = false)
  20448. {
  20449. std::vector<std::uint8_t> result;
  20450. to_bjdata(j, result, use_size, use_type);
  20451. return result;
  20452. }
  20453. /// @brief create a BJData serialization of a given JSON value
  20454. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20455. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20456. const bool use_size = false, const bool use_type = false)
  20457. {
  20458. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  20459. }
  20460. /// @brief create a BJData serialization of a given JSON value
  20461. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20462. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  20463. const bool use_size = false, const bool use_type = false)
  20464. {
  20465. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  20466. }
  20467. /// @brief create a BSON serialization of a given JSON value
  20468. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20469. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  20470. {
  20471. std::vector<std::uint8_t> result;
  20472. to_bson(j, result);
  20473. return result;
  20474. }
  20475. /// @brief create a BSON serialization of a given JSON value
  20476. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20477. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20478. {
  20479. binary_writer<std::uint8_t>(o).write_bson(j);
  20480. }
  20481. /// @brief create a BSON serialization of a given JSON value
  20482. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20483. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  20484. {
  20485. binary_writer<char>(o).write_bson(j);
  20486. }
  20487. /// @brief create a JSON value from an input in CBOR format
  20488. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20489. template<typename InputType>
  20490. JSON_HEDLEY_WARN_UNUSED_RESULT
  20491. static basic_json from_cbor(InputType&& i,
  20492. const bool strict = true,
  20493. const bool allow_exceptions = true,
  20494. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20495. {
  20496. basic_json result;
  20497. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20498. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20499. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20500. return res ? result : basic_json(value_t::discarded);
  20501. }
  20502. /// @brief create a JSON value from an input in CBOR format
  20503. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20504. template<typename IteratorType>
  20505. JSON_HEDLEY_WARN_UNUSED_RESULT
  20506. static basic_json from_cbor(IteratorType first, IteratorType last,
  20507. const bool strict = true,
  20508. const bool allow_exceptions = true,
  20509. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20510. {
  20511. basic_json result;
  20512. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20513. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20514. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20515. return res ? result : basic_json(value_t::discarded);
  20516. }
  20517. template<typename T>
  20518. JSON_HEDLEY_WARN_UNUSED_RESULT
  20519. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20520. static basic_json from_cbor(const T* ptr, std::size_t len,
  20521. const bool strict = true,
  20522. const bool allow_exceptions = true,
  20523. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20524. {
  20525. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  20526. }
  20527. JSON_HEDLEY_WARN_UNUSED_RESULT
  20528. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20529. static basic_json from_cbor(detail::span_input_adapter&& i,
  20530. const bool strict = true,
  20531. const bool allow_exceptions = true,
  20532. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20533. {
  20534. basic_json result;
  20535. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20536. auto ia = i.get();
  20537. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20538. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20539. return res ? result : basic_json(value_t::discarded);
  20540. }
  20541. /// @brief create a JSON value from an input in MessagePack format
  20542. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20543. template<typename InputType>
  20544. JSON_HEDLEY_WARN_UNUSED_RESULT
  20545. static basic_json from_msgpack(InputType&& i,
  20546. const bool strict = true,
  20547. const bool allow_exceptions = true)
  20548. {
  20549. basic_json result;
  20550. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20551. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20552. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20553. return res ? result : basic_json(value_t::discarded);
  20554. }
  20555. /// @brief create a JSON value from an input in MessagePack format
  20556. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20557. template<typename IteratorType>
  20558. JSON_HEDLEY_WARN_UNUSED_RESULT
  20559. static basic_json from_msgpack(IteratorType first, IteratorType last,
  20560. const bool strict = true,
  20561. const bool allow_exceptions = true)
  20562. {
  20563. basic_json result;
  20564. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20565. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20566. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20567. return res ? result : basic_json(value_t::discarded);
  20568. }
  20569. template<typename T>
  20570. JSON_HEDLEY_WARN_UNUSED_RESULT
  20571. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20572. static basic_json from_msgpack(const T* ptr, std::size_t len,
  20573. const bool strict = true,
  20574. const bool allow_exceptions = true)
  20575. {
  20576. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  20577. }
  20578. JSON_HEDLEY_WARN_UNUSED_RESULT
  20579. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20580. static basic_json from_msgpack(detail::span_input_adapter&& i,
  20581. const bool strict = true,
  20582. const bool allow_exceptions = true)
  20583. {
  20584. basic_json result;
  20585. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20586. auto ia = i.get();
  20587. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20588. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20589. return res ? result : basic_json(value_t::discarded);
  20590. }
  20591. /// @brief create a JSON value from an input in UBJSON format
  20592. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20593. template<typename InputType>
  20594. JSON_HEDLEY_WARN_UNUSED_RESULT
  20595. static basic_json from_ubjson(InputType&& i,
  20596. const bool strict = true,
  20597. const bool allow_exceptions = true)
  20598. {
  20599. basic_json result;
  20600. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20601. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20602. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20603. return res ? result : basic_json(value_t::discarded);
  20604. }
  20605. /// @brief create a JSON value from an input in UBJSON format
  20606. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20607. template<typename IteratorType>
  20608. JSON_HEDLEY_WARN_UNUSED_RESULT
  20609. static basic_json from_ubjson(IteratorType first, IteratorType last,
  20610. const bool strict = true,
  20611. const bool allow_exceptions = true)
  20612. {
  20613. basic_json result;
  20614. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20615. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20616. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20617. return res ? result : basic_json(value_t::discarded);
  20618. }
  20619. template<typename T>
  20620. JSON_HEDLEY_WARN_UNUSED_RESULT
  20621. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20622. static basic_json from_ubjson(const T* ptr, std::size_t len,
  20623. const bool strict = true,
  20624. const bool allow_exceptions = true)
  20625. {
  20626. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  20627. }
  20628. JSON_HEDLEY_WARN_UNUSED_RESULT
  20629. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20630. static basic_json from_ubjson(detail::span_input_adapter&& i,
  20631. const bool strict = true,
  20632. const bool allow_exceptions = true)
  20633. {
  20634. basic_json result;
  20635. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20636. auto ia = i.get();
  20637. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20638. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20639. return res ? result : basic_json(value_t::discarded);
  20640. }
  20641. /// @brief create a JSON value from an input in BJData format
  20642. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20643. template<typename InputType>
  20644. JSON_HEDLEY_WARN_UNUSED_RESULT
  20645. static basic_json from_bjdata(InputType&& i,
  20646. const bool strict = true,
  20647. const bool allow_exceptions = true)
  20648. {
  20649. basic_json result;
  20650. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20651. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20652. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20653. return res ? result : basic_json(value_t::discarded);
  20654. }
  20655. /// @brief create a JSON value from an input in BJData format
  20656. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20657. template<typename IteratorType>
  20658. JSON_HEDLEY_WARN_UNUSED_RESULT
  20659. static basic_json from_bjdata(IteratorType first, IteratorType last,
  20660. const bool strict = true,
  20661. const bool allow_exceptions = true)
  20662. {
  20663. basic_json result;
  20664. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20665. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20666. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20667. return res ? result : basic_json(value_t::discarded);
  20668. }
  20669. /// @brief create a JSON value from an input in BSON format
  20670. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20671. template<typename InputType>
  20672. JSON_HEDLEY_WARN_UNUSED_RESULT
  20673. static basic_json from_bson(InputType&& i,
  20674. const bool strict = true,
  20675. const bool allow_exceptions = true)
  20676. {
  20677. basic_json result;
  20678. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20679. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20680. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20681. return res ? result : basic_json(value_t::discarded);
  20682. }
  20683. /// @brief create a JSON value from an input in BSON format
  20684. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20685. template<typename IteratorType>
  20686. JSON_HEDLEY_WARN_UNUSED_RESULT
  20687. static basic_json from_bson(IteratorType first, IteratorType last,
  20688. const bool strict = true,
  20689. const bool allow_exceptions = true)
  20690. {
  20691. basic_json result;
  20692. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20693. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20694. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20695. return res ? result : basic_json(value_t::discarded);
  20696. }
  20697. template<typename T>
  20698. JSON_HEDLEY_WARN_UNUSED_RESULT
  20699. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20700. static basic_json from_bson(const T* ptr, std::size_t len,
  20701. const bool strict = true,
  20702. const bool allow_exceptions = true)
  20703. {
  20704. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  20705. }
  20706. JSON_HEDLEY_WARN_UNUSED_RESULT
  20707. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20708. static basic_json from_bson(detail::span_input_adapter&& i,
  20709. const bool strict = true,
  20710. const bool allow_exceptions = true)
  20711. {
  20712. basic_json result;
  20713. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20714. auto ia = i.get();
  20715. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20716. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20717. return res ? result : basic_json(value_t::discarded);
  20718. }
  20719. /// @}
  20720. //////////////////////////
  20721. // JSON Pointer support //
  20722. //////////////////////////
  20723. /// @name JSON Pointer functions
  20724. /// @{
  20725. /// @brief access specified element via JSON Pointer
  20726. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20727. reference operator[](const json_pointer& ptr)
  20728. {
  20729. return ptr.get_unchecked(this);
  20730. }
  20731. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20732. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20733. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20734. {
  20735. return ptr.get_unchecked(this);
  20736. }
  20737. /// @brief access specified element via JSON Pointer
  20738. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20739. const_reference operator[](const json_pointer& ptr) const
  20740. {
  20741. return ptr.get_unchecked(this);
  20742. }
  20743. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20744. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20745. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20746. {
  20747. return ptr.get_unchecked(this);
  20748. }
  20749. /// @brief access specified element via JSON Pointer
  20750. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20751. reference at(const json_pointer& ptr)
  20752. {
  20753. return ptr.get_checked(this);
  20754. }
  20755. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20756. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20757. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20758. {
  20759. return ptr.get_checked(this);
  20760. }
  20761. /// @brief access specified element via JSON Pointer
  20762. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20763. const_reference at(const json_pointer& ptr) const
  20764. {
  20765. return ptr.get_checked(this);
  20766. }
  20767. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20768. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20769. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20770. {
  20771. return ptr.get_checked(this);
  20772. }
  20773. /// @brief return flattened JSON value
  20774. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  20775. basic_json flatten() const
  20776. {
  20777. basic_json result(value_t::object);
  20778. json_pointer::flatten("", *this, result);
  20779. return result;
  20780. }
  20781. /// @brief unflatten a previously flattened JSON value
  20782. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  20783. basic_json unflatten() const
  20784. {
  20785. return json_pointer::unflatten(*this);
  20786. }
  20787. /// @}
  20788. //////////////////////////
  20789. // JSON Patch functions //
  20790. //////////////////////////
  20791. /// @name JSON Patch functions
  20792. /// @{
  20793. /// @brief applies a JSON patch in-place without copying the object
  20794. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20795. void patch_inplace(const basic_json& json_patch)
  20796. {
  20797. basic_json& result = *this;
  20798. // the valid JSON Patch operations
  20799. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  20800. const auto get_op = [](const std::string & op)
  20801. {
  20802. if (op == "add")
  20803. {
  20804. return patch_operations::add;
  20805. }
  20806. if (op == "remove")
  20807. {
  20808. return patch_operations::remove;
  20809. }
  20810. if (op == "replace")
  20811. {
  20812. return patch_operations::replace;
  20813. }
  20814. if (op == "move")
  20815. {
  20816. return patch_operations::move;
  20817. }
  20818. if (op == "copy")
  20819. {
  20820. return patch_operations::copy;
  20821. }
  20822. if (op == "test")
  20823. {
  20824. return patch_operations::test;
  20825. }
  20826. return patch_operations::invalid;
  20827. };
  20828. // wrapper for "add" operation; add value at ptr
  20829. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  20830. {
  20831. // adding to the root of the target document means replacing it
  20832. if (ptr.empty())
  20833. {
  20834. result = val;
  20835. return;
  20836. }
  20837. // make sure the top element of the pointer exists
  20838. json_pointer const top_pointer = ptr.top();
  20839. if (top_pointer != ptr)
  20840. {
  20841. result.at(top_pointer);
  20842. }
  20843. // get reference to parent of JSON pointer ptr
  20844. const auto last_path = ptr.back();
  20845. ptr.pop_back();
  20846. // parent must exist when performing patch add per RFC6902 specs
  20847. basic_json& parent = result.at(ptr);
  20848. switch (parent.m_type)
  20849. {
  20850. case value_t::null:
  20851. case value_t::object:
  20852. {
  20853. // use operator[] to add value
  20854. parent[last_path] = val;
  20855. break;
  20856. }
  20857. case value_t::array:
  20858. {
  20859. if (last_path == "-")
  20860. {
  20861. // special case: append to back
  20862. parent.push_back(val);
  20863. }
  20864. else
  20865. {
  20866. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  20867. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  20868. {
  20869. // avoid undefined behavior
  20870. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  20871. }
  20872. // default case: insert add offset
  20873. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  20874. }
  20875. break;
  20876. }
  20877. // if there exists a parent it cannot be primitive
  20878. case value_t::string: // LCOV_EXCL_LINE
  20879. case value_t::boolean: // LCOV_EXCL_LINE
  20880. case value_t::number_integer: // LCOV_EXCL_LINE
  20881. case value_t::number_unsigned: // LCOV_EXCL_LINE
  20882. case value_t::number_float: // LCOV_EXCL_LINE
  20883. case value_t::binary: // LCOV_EXCL_LINE
  20884. case value_t::discarded: // LCOV_EXCL_LINE
  20885. default: // LCOV_EXCL_LINE
  20886. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  20887. }
  20888. };
  20889. // wrapper for "remove" operation; remove value at ptr
  20890. const auto operation_remove = [this, &result](json_pointer & ptr)
  20891. {
  20892. // get reference to parent of JSON pointer ptr
  20893. const auto last_path = ptr.back();
  20894. ptr.pop_back();
  20895. basic_json& parent = result.at(ptr);
  20896. // remove child
  20897. if (parent.is_object())
  20898. {
  20899. // perform range check
  20900. auto it = parent.find(last_path);
  20901. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  20902. {
  20903. parent.erase(it);
  20904. }
  20905. else
  20906. {
  20907. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  20908. }
  20909. }
  20910. else if (parent.is_array())
  20911. {
  20912. // note erase performs range check
  20913. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  20914. }
  20915. };
  20916. // type check: top level value must be an array
  20917. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  20918. {
  20919. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  20920. }
  20921. // iterate and apply the operations
  20922. for (const auto& val : json_patch)
  20923. {
  20924. // wrapper to get a value for an operation
  20925. const auto get_value = [&val](const std::string & op,
  20926. const std::string & member,
  20927. bool string_type) -> basic_json &
  20928. {
  20929. // find value
  20930. auto it = val.m_value.object->find(member);
  20931. // context-sensitive error message
  20932. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\'');
  20933. // check if desired value is present
  20934. if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
  20935. {
  20936. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20937. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  20938. }
  20939. // check if result is of type string
  20940. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  20941. {
  20942. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20943. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  20944. }
  20945. // no error: return value
  20946. return it->second;
  20947. };
  20948. // type check: every element of the array must be an object
  20949. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  20950. {
  20951. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  20952. }
  20953. // collect mandatory members
  20954. const auto op = get_value("op", "op", true).template get<std::string>();
  20955. const auto path = get_value(op, "path", true).template get<std::string>();
  20956. json_pointer ptr(path);
  20957. switch (get_op(op))
  20958. {
  20959. case patch_operations::add:
  20960. {
  20961. operation_add(ptr, get_value("add", "value", false));
  20962. break;
  20963. }
  20964. case patch_operations::remove:
  20965. {
  20966. operation_remove(ptr);
  20967. break;
  20968. }
  20969. case patch_operations::replace:
  20970. {
  20971. // the "path" location must exist - use at()
  20972. result.at(ptr) = get_value("replace", "value", false);
  20973. break;
  20974. }
  20975. case patch_operations::move:
  20976. {
  20977. const auto from_path = get_value("move", "from", true).template get<std::string>();
  20978. json_pointer from_ptr(from_path);
  20979. // the "from" location must exist - use at()
  20980. basic_json const v = result.at(from_ptr);
  20981. // The move operation is functionally identical to a
  20982. // "remove" operation on the "from" location, followed
  20983. // immediately by an "add" operation at the target
  20984. // location with the value that was just removed.
  20985. operation_remove(from_ptr);
  20986. operation_add(ptr, v);
  20987. break;
  20988. }
  20989. case patch_operations::copy:
  20990. {
  20991. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  20992. const json_pointer from_ptr(from_path);
  20993. // the "from" location must exist - use at()
  20994. basic_json const v = result.at(from_ptr);
  20995. // The copy is functionally identical to an "add"
  20996. // operation at the target location using the value
  20997. // specified in the "from" member.
  20998. operation_add(ptr, v);
  20999. break;
  21000. }
  21001. case patch_operations::test:
  21002. {
  21003. bool success = false;
  21004. JSON_TRY
  21005. {
  21006. // check if "value" matches the one at "path"
  21007. // the "path" location must exist - use at()
  21008. success = (result.at(ptr) == get_value("test", "value", false));
  21009. }
  21010. JSON_INTERNAL_CATCH (out_of_range&)
  21011. {
  21012. // ignore out of range errors: success remains false
  21013. }
  21014. // throw an exception if test fails
  21015. if (JSON_HEDLEY_UNLIKELY(!success))
  21016. {
  21017. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  21018. }
  21019. break;
  21020. }
  21021. case patch_operations::invalid:
  21022. default:
  21023. {
  21024. // op must be "add", "remove", "replace", "move", "copy", or
  21025. // "test"
  21026. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  21027. }
  21028. }
  21029. }
  21030. }
  21031. /// @brief applies a JSON patch to a copy of the current object
  21032. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21033. basic_json patch(const basic_json& json_patch) const
  21034. {
  21035. basic_json result = *this;
  21036. result.patch_inplace(json_patch);
  21037. return result;
  21038. }
  21039. /// @brief creates a diff as a JSON patch
  21040. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  21041. JSON_HEDLEY_WARN_UNUSED_RESULT
  21042. static basic_json diff(const basic_json& source, const basic_json& target,
  21043. const std::string& path = "")
  21044. {
  21045. // the patch
  21046. basic_json result(value_t::array);
  21047. // if the values are the same, return empty patch
  21048. if (source == target)
  21049. {
  21050. return result;
  21051. }
  21052. if (source.type() != target.type())
  21053. {
  21054. // different types: replace value
  21055. result.push_back(
  21056. {
  21057. {"op", "replace"}, {"path", path}, {"value", target}
  21058. });
  21059. return result;
  21060. }
  21061. switch (source.type())
  21062. {
  21063. case value_t::array:
  21064. {
  21065. // first pass: traverse common elements
  21066. std::size_t i = 0;
  21067. while (i < source.size() && i < target.size())
  21068. {
  21069. // recursive call to compare array values at index i
  21070. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  21071. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21072. ++i;
  21073. }
  21074. // We now reached the end of at least one array
  21075. // in a second pass, traverse the remaining elements
  21076. // remove my remaining elements
  21077. const auto end_index = static_cast<difference_type>(result.size());
  21078. while (i < source.size())
  21079. {
  21080. // add operations in reverse order to avoid invalid
  21081. // indices
  21082. result.insert(result.begin() + end_index, object(
  21083. {
  21084. {"op", "remove"},
  21085. {"path", detail::concat(path, '/', std::to_string(i))}
  21086. }));
  21087. ++i;
  21088. }
  21089. // add other remaining elements
  21090. while (i < target.size())
  21091. {
  21092. result.push_back(
  21093. {
  21094. {"op", "add"},
  21095. {"path", detail::concat(path, "/-")},
  21096. {"value", target[i]}
  21097. });
  21098. ++i;
  21099. }
  21100. break;
  21101. }
  21102. case value_t::object:
  21103. {
  21104. // first pass: traverse this object's elements
  21105. for (auto it = source.cbegin(); it != source.cend(); ++it)
  21106. {
  21107. // escape the key name to be used in a JSON patch
  21108. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21109. if (target.find(it.key()) != target.end())
  21110. {
  21111. // recursive call to compare object values at key it
  21112. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  21113. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21114. }
  21115. else
  21116. {
  21117. // found a key that is not in o -> remove it
  21118. result.push_back(object(
  21119. {
  21120. {"op", "remove"}, {"path", path_key}
  21121. }));
  21122. }
  21123. }
  21124. // second pass: traverse other object's elements
  21125. for (auto it = target.cbegin(); it != target.cend(); ++it)
  21126. {
  21127. if (source.find(it.key()) == source.end())
  21128. {
  21129. // found a key that is not in this -> add it
  21130. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21131. result.push_back(
  21132. {
  21133. {"op", "add"}, {"path", path_key},
  21134. {"value", it.value()}
  21135. });
  21136. }
  21137. }
  21138. break;
  21139. }
  21140. case value_t::null:
  21141. case value_t::string:
  21142. case value_t::boolean:
  21143. case value_t::number_integer:
  21144. case value_t::number_unsigned:
  21145. case value_t::number_float:
  21146. case value_t::binary:
  21147. case value_t::discarded:
  21148. default:
  21149. {
  21150. // both primitive type: replace value
  21151. result.push_back(
  21152. {
  21153. {"op", "replace"}, {"path", path}, {"value", target}
  21154. });
  21155. break;
  21156. }
  21157. }
  21158. return result;
  21159. }
  21160. /// @}
  21161. ////////////////////////////////
  21162. // JSON Merge Patch functions //
  21163. ////////////////////////////////
  21164. /// @name JSON Merge Patch functions
  21165. /// @{
  21166. /// @brief applies a JSON Merge Patch
  21167. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  21168. void merge_patch(const basic_json& apply_patch)
  21169. {
  21170. if (apply_patch.is_object())
  21171. {
  21172. if (!is_object())
  21173. {
  21174. *this = object();
  21175. }
  21176. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  21177. {
  21178. if (it.value().is_null())
  21179. {
  21180. erase(it.key());
  21181. }
  21182. else
  21183. {
  21184. operator[](it.key()).merge_patch(it.value());
  21185. }
  21186. }
  21187. }
  21188. else
  21189. {
  21190. *this = apply_patch;
  21191. }
  21192. }
  21193. /// @}
  21194. };
  21195. /// @brief user-defined to_string function for JSON values
  21196. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  21197. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21198. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  21199. {
  21200. return j.dump();
  21201. }
  21202. inline namespace literals
  21203. {
  21204. inline namespace json_literals
  21205. {
  21206. /// @brief user-defined string literal for JSON values
  21207. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  21208. JSON_HEDLEY_NON_NULL(1)
  21209. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  21210. {
  21211. return nlohmann::json::parse(s, s + n);
  21212. }
  21213. /// @brief user-defined string literal for JSON pointer
  21214. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  21215. JSON_HEDLEY_NON_NULL(1)
  21216. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  21217. {
  21218. return nlohmann::json::json_pointer(std::string(s, n));
  21219. }
  21220. } // namespace json_literals
  21221. } // namespace literals
  21222. NLOHMANN_JSON_NAMESPACE_END
  21223. ///////////////////////
  21224. // nonmember support //
  21225. ///////////////////////
  21226. namespace std // NOLINT(cert-dcl58-cpp)
  21227. {
  21228. /// @brief hash value for JSON objects
  21229. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  21230. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21231. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  21232. {
  21233. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  21234. {
  21235. return nlohmann::detail::hash(j);
  21236. }
  21237. };
  21238. // specialization for std::less<value_t>
  21239. template<>
  21240. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  21241. {
  21242. /*!
  21243. @brief compare two value_t enum values
  21244. @since version 3.0.0
  21245. */
  21246. bool operator()(::nlohmann::detail::value_t lhs,
  21247. ::nlohmann::detail::value_t rhs) const noexcept
  21248. {
  21249. #if JSON_HAS_THREE_WAY_COMPARISON
  21250. return std::is_lt(lhs <=> rhs); // *NOPAD*
  21251. #else
  21252. return ::nlohmann::detail::operator<(lhs, rhs);
  21253. #endif
  21254. }
  21255. };
  21256. // C++20 prohibit function specialization in the std namespace.
  21257. #ifndef JSON_HAS_CPP_20
  21258. /// @brief exchanges the values of two JSON objects
  21259. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  21260. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21261. 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)
  21262. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression)
  21263. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  21264. {
  21265. j1.swap(j2);
  21266. }
  21267. #endif
  21268. } // namespace std
  21269. #if JSON_USE_GLOBAL_UDLS
  21270. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21271. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21272. #endif
  21273. // #include <nlohmann/detail/macro_unscope.hpp>
  21274. // __ _____ _____ _____
  21275. // __| | __| | | | JSON for Modern C++
  21276. // | | |__ | | | | | | version 3.11.2
  21277. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21278. //
  21279. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  21280. // SPDX-License-Identifier: MIT
  21281. // restore clang diagnostic settings
  21282. #if defined(__clang__)
  21283. #pragma clang diagnostic pop
  21284. #endif
  21285. // clean up
  21286. #undef JSON_ASSERT
  21287. #undef JSON_INTERNAL_CATCH
  21288. #undef JSON_THROW
  21289. #undef JSON_PRIVATE_UNLESS_TESTED
  21290. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21291. #undef NLOHMANN_BASIC_JSON_TPL
  21292. #undef JSON_EXPLICIT
  21293. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  21294. #undef JSON_INLINE_VARIABLE
  21295. #undef JSON_NO_UNIQUE_ADDRESS
  21296. #undef JSON_DISABLE_ENUM_SERIALIZATION
  21297. #undef JSON_USE_GLOBAL_UDLS
  21298. #ifndef JSON_TEST_KEEP_MACROS
  21299. #undef JSON_CATCH
  21300. #undef JSON_TRY
  21301. #undef JSON_HAS_CPP_11
  21302. #undef JSON_HAS_CPP_14
  21303. #undef JSON_HAS_CPP_17
  21304. #undef JSON_HAS_CPP_20
  21305. #undef JSON_HAS_FILESYSTEM
  21306. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  21307. #undef JSON_HAS_THREE_WAY_COMPARISON
  21308. #undef JSON_HAS_RANGES
  21309. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  21310. #endif
  21311. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  21312. // __ _____ _____ _____
  21313. // __| | __| | | | JSON for Modern C++
  21314. // | | |__ | | | | | | version 3.11.2
  21315. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21316. //
  21317. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  21318. // SPDX-License-Identifier: MIT
  21319. #undef JSON_HEDLEY_ALWAYS_INLINE
  21320. #undef JSON_HEDLEY_ARM_VERSION
  21321. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  21322. #undef JSON_HEDLEY_ARRAY_PARAM
  21323. #undef JSON_HEDLEY_ASSUME
  21324. #undef JSON_HEDLEY_BEGIN_C_DECLS
  21325. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  21326. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  21327. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  21328. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  21329. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  21330. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  21331. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  21332. #undef JSON_HEDLEY_COMPCERT_VERSION
  21333. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  21334. #undef JSON_HEDLEY_CONCAT
  21335. #undef JSON_HEDLEY_CONCAT3
  21336. #undef JSON_HEDLEY_CONCAT3_EX
  21337. #undef JSON_HEDLEY_CONCAT_EX
  21338. #undef JSON_HEDLEY_CONST
  21339. #undef JSON_HEDLEY_CONSTEXPR
  21340. #undef JSON_HEDLEY_CONST_CAST
  21341. #undef JSON_HEDLEY_CPP_CAST
  21342. #undef JSON_HEDLEY_CRAY_VERSION
  21343. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  21344. #undef JSON_HEDLEY_C_DECL
  21345. #undef JSON_HEDLEY_DEPRECATED
  21346. #undef JSON_HEDLEY_DEPRECATED_FOR
  21347. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  21348. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  21349. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  21350. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  21351. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  21352. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  21353. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  21354. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  21355. #undef JSON_HEDLEY_DMC_VERSION
  21356. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  21357. #undef JSON_HEDLEY_EMPTY_BASES
  21358. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  21359. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  21360. #undef JSON_HEDLEY_END_C_DECLS
  21361. #undef JSON_HEDLEY_FLAGS
  21362. #undef JSON_HEDLEY_FLAGS_CAST
  21363. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  21364. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  21365. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  21366. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  21367. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  21368. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  21369. #undef JSON_HEDLEY_GCC_HAS_WARNING
  21370. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  21371. #undef JSON_HEDLEY_GCC_VERSION
  21372. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  21373. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  21374. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  21375. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  21376. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  21377. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  21378. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  21379. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  21380. #undef JSON_HEDLEY_GNUC_VERSION
  21381. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  21382. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  21383. #undef JSON_HEDLEY_HAS_BUILTIN
  21384. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  21385. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  21386. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  21387. #undef JSON_HEDLEY_HAS_EXTENSION
  21388. #undef JSON_HEDLEY_HAS_FEATURE
  21389. #undef JSON_HEDLEY_HAS_WARNING
  21390. #undef JSON_HEDLEY_IAR_VERSION
  21391. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  21392. #undef JSON_HEDLEY_IBM_VERSION
  21393. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  21394. #undef JSON_HEDLEY_IMPORT
  21395. #undef JSON_HEDLEY_INLINE
  21396. #undef JSON_HEDLEY_INTEL_CL_VERSION
  21397. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  21398. #undef JSON_HEDLEY_INTEL_VERSION
  21399. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  21400. #undef JSON_HEDLEY_IS_CONSTANT
  21401. #undef JSON_HEDLEY_IS_CONSTEXPR_
  21402. #undef JSON_HEDLEY_LIKELY
  21403. #undef JSON_HEDLEY_MALLOC
  21404. #undef JSON_HEDLEY_MCST_LCC_VERSION
  21405. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  21406. #undef JSON_HEDLEY_MESSAGE
  21407. #undef JSON_HEDLEY_MSVC_VERSION
  21408. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  21409. #undef JSON_HEDLEY_NEVER_INLINE
  21410. #undef JSON_HEDLEY_NON_NULL
  21411. #undef JSON_HEDLEY_NO_ESCAPE
  21412. #undef JSON_HEDLEY_NO_RETURN
  21413. #undef JSON_HEDLEY_NO_THROW
  21414. #undef JSON_HEDLEY_NULL
  21415. #undef JSON_HEDLEY_PELLES_VERSION
  21416. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  21417. #undef JSON_HEDLEY_PGI_VERSION
  21418. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  21419. #undef JSON_HEDLEY_PREDICT
  21420. #undef JSON_HEDLEY_PRINTF_FORMAT
  21421. #undef JSON_HEDLEY_PRIVATE
  21422. #undef JSON_HEDLEY_PUBLIC
  21423. #undef JSON_HEDLEY_PURE
  21424. #undef JSON_HEDLEY_REINTERPRET_CAST
  21425. #undef JSON_HEDLEY_REQUIRE
  21426. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  21427. #undef JSON_HEDLEY_REQUIRE_MSG
  21428. #undef JSON_HEDLEY_RESTRICT
  21429. #undef JSON_HEDLEY_RETURNS_NON_NULL
  21430. #undef JSON_HEDLEY_SENTINEL
  21431. #undef JSON_HEDLEY_STATIC_ASSERT
  21432. #undef JSON_HEDLEY_STATIC_CAST
  21433. #undef JSON_HEDLEY_STRINGIFY
  21434. #undef JSON_HEDLEY_STRINGIFY_EX
  21435. #undef JSON_HEDLEY_SUNPRO_VERSION
  21436. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  21437. #undef JSON_HEDLEY_TINYC_VERSION
  21438. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  21439. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  21440. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  21441. #undef JSON_HEDLEY_TI_CL2000_VERSION
  21442. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  21443. #undef JSON_HEDLEY_TI_CL430_VERSION
  21444. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  21445. #undef JSON_HEDLEY_TI_CL6X_VERSION
  21446. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  21447. #undef JSON_HEDLEY_TI_CL7X_VERSION
  21448. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  21449. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  21450. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  21451. #undef JSON_HEDLEY_TI_VERSION
  21452. #undef JSON_HEDLEY_TI_VERSION_CHECK
  21453. #undef JSON_HEDLEY_UNAVAILABLE
  21454. #undef JSON_HEDLEY_UNLIKELY
  21455. #undef JSON_HEDLEY_UNPREDICTABLE
  21456. #undef JSON_HEDLEY_UNREACHABLE
  21457. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  21458. #undef JSON_HEDLEY_VERSION
  21459. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  21460. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  21461. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  21462. #undef JSON_HEDLEY_VERSION_ENCODE
  21463. #undef JSON_HEDLEY_WARNING
  21464. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  21465. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  21466. #undef JSON_HEDLEY_FALL_THROUGH
  21467. #endif // INCLUDE_NLOHMANN_JSON_HPP_