json.hpp 898 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. /****************************************************************************\
  9. * Note on documentation: The source files contain links to the online *
  10. * documentation of the public API at https://json.nlohmann.me. This URL *
  11. * contains the most recent documentation and should also be applicable to *
  12. * previous versions; documentation for deprecated functions is not *
  13. * removed, but marked deprecated. See "Generate documentation" section in *
  14. * file docs/README.md. *
  15. \****************************************************************************/
  16. #ifndef INCLUDE_NLOHMANN_JSON_HPP_
  17. #define INCLUDE_NLOHMANN_JSON_HPP_
  18. #include <algorithm> // all_of, find, for_each
  19. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  20. #include <functional> // hash, less
  21. #include <initializer_list> // initializer_list
  22. #ifndef JSON_NO_IO
  23. #include <iosfwd> // istream, ostream
  24. #endif // JSON_NO_IO
  25. #include <iterator> // random_access_iterator_tag
  26. #include <memory> // unique_ptr
  27. #include <string> // string, stoi, to_string
  28. #include <utility> // declval, forward, move, pair, swap
  29. #include <vector> // vector
  30. // #include <nlohmann/adl_serializer.hpp>
  31. // __ _____ _____ _____
  32. // __| | __| | | | JSON for Modern C++
  33. // | | |__ | | | | | | version 3.11.3
  34. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  35. //
  36. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  37. // SPDX-License-Identifier: MIT
  38. #include <utility>
  39. // #include <nlohmann/detail/abi_macros.hpp>
  40. // __ _____ _____ _____
  41. // __| | __| | | | JSON for Modern C++
  42. // | | |__ | | | | | | version 3.11.3
  43. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  44. //
  45. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  46. // SPDX-License-Identifier: MIT
  47. // This file contains all macro definitions affecting or depending on the ABI
  48. #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
  49. #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
  50. #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 3
  51. #warning "Already included a different version of the library!"
  52. #endif
  53. #endif
  54. #endif
  55. #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
  56. #define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum)
  57. #define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum)
  58. #ifndef JSON_DIAGNOSTICS
  59. #define JSON_DIAGNOSTICS 0
  60. #endif
  61. #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  62. #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
  63. #endif
  64. #if JSON_DIAGNOSTICS
  65. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
  66. #else
  67. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
  68. #endif
  69. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  70. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
  71. #else
  72. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
  73. #endif
  74. #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
  75. #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
  76. #endif
  77. // Construct the namespace ABI tags component
  78. #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
  79. #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
  80. NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
  81. #define NLOHMANN_JSON_ABI_TAGS \
  82. NLOHMANN_JSON_ABI_TAGS_CONCAT( \
  83. NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
  84. NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
  85. // Construct the namespace version component
  86. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
  87. _v ## major ## _ ## minor ## _ ## patch
  88. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
  89. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
  90. #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
  91. #define NLOHMANN_JSON_NAMESPACE_VERSION
  92. #else
  93. #define NLOHMANN_JSON_NAMESPACE_VERSION \
  94. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
  95. NLOHMANN_JSON_VERSION_MINOR, \
  96. NLOHMANN_JSON_VERSION_PATCH)
  97. #endif
  98. // Combine namespace components
  99. #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
  100. #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
  101. NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
  102. #ifndef NLOHMANN_JSON_NAMESPACE
  103. #define NLOHMANN_JSON_NAMESPACE \
  104. nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
  105. NLOHMANN_JSON_ABI_TAGS, \
  106. NLOHMANN_JSON_NAMESPACE_VERSION)
  107. #endif
  108. #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
  109. #define NLOHMANN_JSON_NAMESPACE_BEGIN \
  110. namespace nlohmann \
  111. { \
  112. inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
  113. NLOHMANN_JSON_ABI_TAGS, \
  114. NLOHMANN_JSON_NAMESPACE_VERSION) \
  115. {
  116. #endif
  117. #ifndef NLOHMANN_JSON_NAMESPACE_END
  118. #define NLOHMANN_JSON_NAMESPACE_END \
  119. } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
  120. } // namespace nlohmann
  121. #endif
  122. // #include <nlohmann/detail/conversions/from_json.hpp>
  123. // __ _____ _____ _____
  124. // __| | __| | | | JSON for Modern C++
  125. // | | |__ | | | | | | version 3.11.3
  126. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  127. //
  128. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  129. // SPDX-License-Identifier: MIT
  130. #include <algorithm> // transform
  131. #include <array> // array
  132. #include <forward_list> // forward_list
  133. #include <iterator> // inserter, front_inserter, end
  134. #include <map> // map
  135. #include <string> // string
  136. #include <tuple> // tuple, make_tuple
  137. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  138. #include <unordered_map> // unordered_map
  139. #include <utility> // pair, declval
  140. #include <valarray> // valarray
  141. // #include <nlohmann/detail/exceptions.hpp>
  142. // __ _____ _____ _____
  143. // __| | __| | | | JSON for Modern C++
  144. // | | |__ | | | | | | version 3.11.3
  145. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  146. //
  147. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  148. // SPDX-License-Identifier: MIT
  149. #include <cstddef> // nullptr_t
  150. #include <exception> // exception
  151. #if JSON_DIAGNOSTICS
  152. #include <numeric> // accumulate
  153. #endif
  154. #include <stdexcept> // runtime_error
  155. #include <string> // to_string
  156. #include <vector> // vector
  157. // #include <nlohmann/detail/value_t.hpp>
  158. // __ _____ _____ _____
  159. // __| | __| | | | JSON for Modern C++
  160. // | | |__ | | | | | | version 3.11.3
  161. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  162. //
  163. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  164. // SPDX-License-Identifier: MIT
  165. #include <array> // array
  166. #include <cstddef> // size_t
  167. #include <cstdint> // uint8_t
  168. #include <string> // string
  169. // #include <nlohmann/detail/macro_scope.hpp>
  170. // __ _____ _____ _____
  171. // __| | __| | | | JSON for Modern C++
  172. // | | |__ | | | | | | version 3.11.3
  173. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  174. //
  175. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  176. // SPDX-License-Identifier: MIT
  177. #include <utility> // declval, pair
  178. // #include <nlohmann/detail/meta/detected.hpp>
  179. // __ _____ _____ _____
  180. // __| | __| | | | JSON for Modern C++
  181. // | | |__ | | | | | | version 3.11.3
  182. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  183. //
  184. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  185. // SPDX-License-Identifier: MIT
  186. #include <type_traits>
  187. // #include <nlohmann/detail/meta/void_t.hpp>
  188. // __ _____ _____ _____
  189. // __| | __| | | | JSON for Modern C++
  190. // | | |__ | | | | | | version 3.11.3
  191. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  192. //
  193. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  194. // SPDX-License-Identifier: MIT
  195. // #include <nlohmann/detail/abi_macros.hpp>
  196. NLOHMANN_JSON_NAMESPACE_BEGIN
  197. namespace detail
  198. {
  199. template<typename ...Ts> struct make_void
  200. {
  201. using type = void;
  202. };
  203. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  204. } // namespace detail
  205. NLOHMANN_JSON_NAMESPACE_END
  206. NLOHMANN_JSON_NAMESPACE_BEGIN
  207. namespace detail
  208. {
  209. // https://en.cppreference.com/w/cpp/experimental/is_detected
  210. struct nonesuch
  211. {
  212. nonesuch() = delete;
  213. ~nonesuch() = delete;
  214. nonesuch(nonesuch const&) = delete;
  215. nonesuch(nonesuch const&&) = delete;
  216. void operator=(nonesuch const&) = delete;
  217. void operator=(nonesuch&&) = delete;
  218. };
  219. template<class Default,
  220. class AlwaysVoid,
  221. template<class...> class Op,
  222. class... Args>
  223. struct detector
  224. {
  225. using value_t = std::false_type;
  226. using type = Default;
  227. };
  228. template<class Default, template<class...> class Op, class... Args>
  229. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  230. {
  231. using value_t = std::true_type;
  232. using type = Op<Args...>;
  233. };
  234. template<template<class...> class Op, class... Args>
  235. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  236. template<template<class...> class Op, class... Args>
  237. struct is_detected_lazy : is_detected<Op, Args...> { };
  238. template<template<class...> class Op, class... Args>
  239. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  240. template<class Default, template<class...> class Op, class... Args>
  241. using detected_or = detector<Default, void, Op, Args...>;
  242. template<class Default, template<class...> class Op, class... Args>
  243. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  244. template<class Expected, template<class...> class Op, class... Args>
  245. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  246. template<class To, template<class...> class Op, class... Args>
  247. using is_detected_convertible =
  248. std::is_convertible<detected_t<Op, Args...>, To>;
  249. } // namespace detail
  250. NLOHMANN_JSON_NAMESPACE_END
  251. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  252. // __ _____ _____ _____
  253. // __| | __| | | | JSON for Modern C++
  254. // | | |__ | | | | | | version 3.11.3
  255. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  256. //
  257. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  258. // SPDX-FileCopyrightText: 2016-2021 Evan Nemerson <evan@nemerson.com>
  259. // SPDX-License-Identifier: MIT
  260. /* Hedley - https://nemequ.github.io/hedley
  261. * Created by Evan Nemerson <evan@nemerson.com>
  262. */
  263. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  264. #if defined(JSON_HEDLEY_VERSION)
  265. #undef JSON_HEDLEY_VERSION
  266. #endif
  267. #define JSON_HEDLEY_VERSION 15
  268. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  269. #undef JSON_HEDLEY_STRINGIFY_EX
  270. #endif
  271. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  272. #if defined(JSON_HEDLEY_STRINGIFY)
  273. #undef JSON_HEDLEY_STRINGIFY
  274. #endif
  275. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  276. #if defined(JSON_HEDLEY_CONCAT_EX)
  277. #undef JSON_HEDLEY_CONCAT_EX
  278. #endif
  279. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  280. #if defined(JSON_HEDLEY_CONCAT)
  281. #undef JSON_HEDLEY_CONCAT
  282. #endif
  283. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  284. #if defined(JSON_HEDLEY_CONCAT3_EX)
  285. #undef JSON_HEDLEY_CONCAT3_EX
  286. #endif
  287. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  288. #if defined(JSON_HEDLEY_CONCAT3)
  289. #undef JSON_HEDLEY_CONCAT3
  290. #endif
  291. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  292. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  293. #undef JSON_HEDLEY_VERSION_ENCODE
  294. #endif
  295. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  296. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  297. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  298. #endif
  299. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  300. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  301. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  302. #endif
  303. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  304. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  305. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  306. #endif
  307. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  308. #if defined(JSON_HEDLEY_GNUC_VERSION)
  309. #undef JSON_HEDLEY_GNUC_VERSION
  310. #endif
  311. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  312. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  313. #elif defined(__GNUC__)
  314. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  315. #endif
  316. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  317. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  318. #endif
  319. #if defined(JSON_HEDLEY_GNUC_VERSION)
  320. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  321. #else
  322. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  323. #endif
  324. #if defined(JSON_HEDLEY_MSVC_VERSION)
  325. #undef JSON_HEDLEY_MSVC_VERSION
  326. #endif
  327. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  328. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  329. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  330. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  331. #elif defined(_MSC_VER) && !defined(__ICL)
  332. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  333. #endif
  334. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  335. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  336. #endif
  337. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  338. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  339. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  340. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  341. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  342. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  343. #else
  344. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  345. #endif
  346. #if defined(JSON_HEDLEY_INTEL_VERSION)
  347. #undef JSON_HEDLEY_INTEL_VERSION
  348. #endif
  349. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  350. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  351. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  352. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  353. #endif
  354. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  355. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  356. #endif
  357. #if defined(JSON_HEDLEY_INTEL_VERSION)
  358. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  359. #else
  360. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  361. #endif
  362. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  363. #undef JSON_HEDLEY_INTEL_CL_VERSION
  364. #endif
  365. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  366. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  367. #endif
  368. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  369. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  370. #endif
  371. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  372. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  373. #else
  374. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  375. #endif
  376. #if defined(JSON_HEDLEY_PGI_VERSION)
  377. #undef JSON_HEDLEY_PGI_VERSION
  378. #endif
  379. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  380. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  381. #endif
  382. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  383. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  384. #endif
  385. #if defined(JSON_HEDLEY_PGI_VERSION)
  386. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  387. #else
  388. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  389. #endif
  390. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  391. #undef JSON_HEDLEY_SUNPRO_VERSION
  392. #endif
  393. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  394. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10)
  395. #elif defined(__SUNPRO_C)
  396. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  397. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  398. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10)
  399. #elif defined(__SUNPRO_CC)
  400. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  401. #endif
  402. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  403. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  404. #endif
  405. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  406. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  407. #else
  408. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  409. #endif
  410. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  411. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  412. #endif
  413. #if defined(__EMSCRIPTEN__)
  414. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  415. #endif
  416. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  417. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  418. #endif
  419. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  420. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  421. #else
  422. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  423. #endif
  424. #if defined(JSON_HEDLEY_ARM_VERSION)
  425. #undef JSON_HEDLEY_ARM_VERSION
  426. #endif
  427. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  428. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  429. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  430. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  431. #endif
  432. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  433. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  434. #endif
  435. #if defined(JSON_HEDLEY_ARM_VERSION)
  436. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  437. #else
  438. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  439. #endif
  440. #if defined(JSON_HEDLEY_IBM_VERSION)
  441. #undef JSON_HEDLEY_IBM_VERSION
  442. #endif
  443. #if defined(__ibmxl__)
  444. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  445. #elif defined(__xlC__) && defined(__xlC_ver__)
  446. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  447. #elif defined(__xlC__)
  448. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  449. #endif
  450. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  451. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  452. #endif
  453. #if defined(JSON_HEDLEY_IBM_VERSION)
  454. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  455. #else
  456. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  457. #endif
  458. #if defined(JSON_HEDLEY_TI_VERSION)
  459. #undef JSON_HEDLEY_TI_VERSION
  460. #endif
  461. #if \
  462. defined(__TI_COMPILER_VERSION__) && \
  463. ( \
  464. defined(__TMS470__) || defined(__TI_ARM__) || \
  465. defined(__MSP430__) || \
  466. defined(__TMS320C2000__) \
  467. )
  468. #if (__TI_COMPILER_VERSION__ >= 16000000)
  469. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  470. #endif
  471. #endif
  472. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  473. #undef JSON_HEDLEY_TI_VERSION_CHECK
  474. #endif
  475. #if defined(JSON_HEDLEY_TI_VERSION)
  476. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  477. #else
  478. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  479. #endif
  480. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  481. #undef JSON_HEDLEY_TI_CL2000_VERSION
  482. #endif
  483. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  484. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  485. #endif
  486. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  487. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  488. #endif
  489. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  490. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  491. #else
  492. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  493. #endif
  494. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  495. #undef JSON_HEDLEY_TI_CL430_VERSION
  496. #endif
  497. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  498. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  499. #endif
  500. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  501. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  502. #endif
  503. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  504. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  505. #else
  506. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  507. #endif
  508. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  509. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  510. #endif
  511. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  512. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  513. #endif
  514. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  515. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  516. #endif
  517. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  518. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  519. #else
  520. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  521. #endif
  522. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  523. #undef JSON_HEDLEY_TI_CL6X_VERSION
  524. #endif
  525. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  526. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  527. #endif
  528. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  529. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  530. #endif
  531. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  532. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  533. #else
  534. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  535. #endif
  536. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  537. #undef JSON_HEDLEY_TI_CL7X_VERSION
  538. #endif
  539. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  540. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  541. #endif
  542. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  543. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  544. #endif
  545. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  546. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  547. #else
  548. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  549. #endif
  550. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  551. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  552. #endif
  553. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  554. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  555. #endif
  556. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  557. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  558. #endif
  559. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  560. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  561. #else
  562. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  563. #endif
  564. #if defined(JSON_HEDLEY_CRAY_VERSION)
  565. #undef JSON_HEDLEY_CRAY_VERSION
  566. #endif
  567. #if defined(_CRAYC)
  568. #if defined(_RELEASE_PATCHLEVEL)
  569. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  570. #else
  571. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  572. #endif
  573. #endif
  574. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  575. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  576. #endif
  577. #if defined(JSON_HEDLEY_CRAY_VERSION)
  578. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  579. #else
  580. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  581. #endif
  582. #if defined(JSON_HEDLEY_IAR_VERSION)
  583. #undef JSON_HEDLEY_IAR_VERSION
  584. #endif
  585. #if defined(__IAR_SYSTEMS_ICC__)
  586. #if __VER__ > 1000
  587. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  588. #else
  589. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  590. #endif
  591. #endif
  592. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  593. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  594. #endif
  595. #if defined(JSON_HEDLEY_IAR_VERSION)
  596. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  597. #else
  598. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  599. #endif
  600. #if defined(JSON_HEDLEY_TINYC_VERSION)
  601. #undef JSON_HEDLEY_TINYC_VERSION
  602. #endif
  603. #if defined(__TINYC__)
  604. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  605. #endif
  606. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  607. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  608. #endif
  609. #if defined(JSON_HEDLEY_TINYC_VERSION)
  610. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  611. #else
  612. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  613. #endif
  614. #if defined(JSON_HEDLEY_DMC_VERSION)
  615. #undef JSON_HEDLEY_DMC_VERSION
  616. #endif
  617. #if defined(__DMC__)
  618. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  619. #endif
  620. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  621. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  622. #endif
  623. #if defined(JSON_HEDLEY_DMC_VERSION)
  624. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  625. #else
  626. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  627. #endif
  628. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  629. #undef JSON_HEDLEY_COMPCERT_VERSION
  630. #endif
  631. #if defined(__COMPCERT_VERSION__)
  632. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  633. #endif
  634. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  635. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  636. #endif
  637. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  638. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  639. #else
  640. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  641. #endif
  642. #if defined(JSON_HEDLEY_PELLES_VERSION)
  643. #undef JSON_HEDLEY_PELLES_VERSION
  644. #endif
  645. #if defined(__POCC__)
  646. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  647. #endif
  648. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  649. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  650. #endif
  651. #if defined(JSON_HEDLEY_PELLES_VERSION)
  652. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  653. #else
  654. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  655. #endif
  656. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  657. #undef JSON_HEDLEY_MCST_LCC_VERSION
  658. #endif
  659. #if defined(__LCC__) && defined(__LCC_MINOR__)
  660. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  661. #endif
  662. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  663. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  664. #endif
  665. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  666. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  667. #else
  668. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  669. #endif
  670. #if defined(JSON_HEDLEY_GCC_VERSION)
  671. #undef JSON_HEDLEY_GCC_VERSION
  672. #endif
  673. #if \
  674. defined(JSON_HEDLEY_GNUC_VERSION) && \
  675. !defined(__clang__) && \
  676. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  677. !defined(JSON_HEDLEY_PGI_VERSION) && \
  678. !defined(JSON_HEDLEY_ARM_VERSION) && \
  679. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  680. !defined(JSON_HEDLEY_TI_VERSION) && \
  681. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  682. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  683. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  684. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  685. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  686. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  687. !defined(__COMPCERT__) && \
  688. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  689. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  690. #endif
  691. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  692. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  693. #endif
  694. #if defined(JSON_HEDLEY_GCC_VERSION)
  695. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  696. #else
  697. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  698. #endif
  699. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  700. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  701. #endif
  702. #if \
  703. defined(__has_attribute) && \
  704. ( \
  705. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  706. )
  707. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  708. #else
  709. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  710. #endif
  711. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  712. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  713. #endif
  714. #if defined(__has_attribute)
  715. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  716. #else
  717. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  718. #endif
  719. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  720. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  721. #endif
  722. #if defined(__has_attribute)
  723. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  724. #else
  725. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  726. #endif
  727. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  728. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  729. #endif
  730. #if \
  731. defined(__has_cpp_attribute) && \
  732. defined(__cplusplus) && \
  733. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  734. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  735. #else
  736. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  737. #endif
  738. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  739. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  740. #endif
  741. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  742. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  743. #elif \
  744. !defined(JSON_HEDLEY_PGI_VERSION) && \
  745. !defined(JSON_HEDLEY_IAR_VERSION) && \
  746. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  747. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  748. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  749. #else
  750. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  751. #endif
  752. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  753. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  754. #endif
  755. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  756. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  757. #else
  758. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  759. #endif
  760. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  761. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  762. #endif
  763. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  764. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  765. #else
  766. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  767. #endif
  768. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  769. #undef JSON_HEDLEY_HAS_BUILTIN
  770. #endif
  771. #if defined(__has_builtin)
  772. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  773. #else
  774. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  775. #endif
  776. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  777. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  778. #endif
  779. #if defined(__has_builtin)
  780. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  781. #else
  782. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  783. #endif
  784. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  785. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  786. #endif
  787. #if defined(__has_builtin)
  788. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  789. #else
  790. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  791. #endif
  792. #if defined(JSON_HEDLEY_HAS_FEATURE)
  793. #undef JSON_HEDLEY_HAS_FEATURE
  794. #endif
  795. #if defined(__has_feature)
  796. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  797. #else
  798. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  799. #endif
  800. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  801. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  802. #endif
  803. #if defined(__has_feature)
  804. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  805. #else
  806. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  807. #endif
  808. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  809. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  810. #endif
  811. #if defined(__has_feature)
  812. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  813. #else
  814. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  815. #endif
  816. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  817. #undef JSON_HEDLEY_HAS_EXTENSION
  818. #endif
  819. #if defined(__has_extension)
  820. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  821. #else
  822. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  823. #endif
  824. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  825. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  826. #endif
  827. #if defined(__has_extension)
  828. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  829. #else
  830. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  831. #endif
  832. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  833. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  834. #endif
  835. #if defined(__has_extension)
  836. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  837. #else
  838. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  839. #endif
  840. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  841. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  842. #endif
  843. #if defined(__has_declspec_attribute)
  844. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  845. #else
  846. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  847. #endif
  848. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  849. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  850. #endif
  851. #if defined(__has_declspec_attribute)
  852. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  853. #else
  854. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  855. #endif
  856. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  857. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  858. #endif
  859. #if defined(__has_declspec_attribute)
  860. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  861. #else
  862. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  863. #endif
  864. #if defined(JSON_HEDLEY_HAS_WARNING)
  865. #undef JSON_HEDLEY_HAS_WARNING
  866. #endif
  867. #if defined(__has_warning)
  868. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  869. #else
  870. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  871. #endif
  872. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  873. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  874. #endif
  875. #if defined(__has_warning)
  876. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  877. #else
  878. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  879. #endif
  880. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  881. #undef JSON_HEDLEY_GCC_HAS_WARNING
  882. #endif
  883. #if defined(__has_warning)
  884. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  885. #else
  886. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  887. #endif
  888. #if \
  889. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  890. defined(__clang__) || \
  891. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  892. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  893. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  894. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  895. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  896. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  897. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  898. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  899. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  900. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  901. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  902. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  903. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  904. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  905. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  906. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  907. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  908. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  909. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  910. #else
  911. #define JSON_HEDLEY_PRAGMA(value)
  912. #endif
  913. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  914. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  915. #endif
  916. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  917. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  918. #endif
  919. #if defined(__clang__)
  920. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  921. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  922. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  923. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  924. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  925. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  926. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  927. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  928. #elif \
  929. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  930. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  931. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  932. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  933. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  934. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  935. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  936. #elif \
  937. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  938. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  939. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  940. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  941. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  942. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  943. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  944. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  945. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  946. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  947. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  948. #else
  949. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  950. #define JSON_HEDLEY_DIAGNOSTIC_POP
  951. #endif
  952. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  953. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  954. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  955. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  956. #endif
  957. #if defined(__cplusplus)
  958. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  959. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  960. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  961. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  962. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  963. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  964. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  965. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  966. xpr \
  967. JSON_HEDLEY_DIAGNOSTIC_POP
  968. # else
  969. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  970. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  971. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  972. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  973. xpr \
  974. JSON_HEDLEY_DIAGNOSTIC_POP
  975. # endif
  976. # else
  977. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  978. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  979. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  980. xpr \
  981. JSON_HEDLEY_DIAGNOSTIC_POP
  982. # endif
  983. # endif
  984. #endif
  985. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  986. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  987. #endif
  988. #if defined(JSON_HEDLEY_CONST_CAST)
  989. #undef JSON_HEDLEY_CONST_CAST
  990. #endif
  991. #if defined(__cplusplus)
  992. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  993. #elif \
  994. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  995. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  996. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  997. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  998. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  999. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  1000. ((T) (expr)); \
  1001. JSON_HEDLEY_DIAGNOSTIC_POP \
  1002. }))
  1003. #else
  1004. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  1005. #endif
  1006. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  1007. #undef JSON_HEDLEY_REINTERPRET_CAST
  1008. #endif
  1009. #if defined(__cplusplus)
  1010. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  1011. #else
  1012. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  1013. #endif
  1014. #if defined(JSON_HEDLEY_STATIC_CAST)
  1015. #undef JSON_HEDLEY_STATIC_CAST
  1016. #endif
  1017. #if defined(__cplusplus)
  1018. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  1019. #else
  1020. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  1021. #endif
  1022. #if defined(JSON_HEDLEY_CPP_CAST)
  1023. #undef JSON_HEDLEY_CPP_CAST
  1024. #endif
  1025. #if defined(__cplusplus)
  1026. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  1027. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1028. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1029. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  1030. ((T) (expr)) \
  1031. JSON_HEDLEY_DIAGNOSTIC_POP
  1032. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  1033. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1034. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1035. _Pragma("diag_suppress=Pe137") \
  1036. JSON_HEDLEY_DIAGNOSTIC_POP
  1037. # else
  1038. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  1039. # endif
  1040. #else
  1041. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  1042. #endif
  1043. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  1044. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1045. #endif
  1046. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  1047. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  1048. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1049. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  1050. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1051. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  1052. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1053. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  1054. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1055. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1056. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1057. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1058. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1059. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  1060. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1061. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1062. #elif \
  1063. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1064. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1065. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1066. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1067. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1068. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1069. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1070. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1071. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1072. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1073. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  1074. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  1075. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  1076. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  1077. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  1078. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  1079. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1080. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  1081. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  1082. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  1083. #else
  1084. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1085. #endif
  1086. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  1087. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1088. #endif
  1089. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1090. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  1091. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1092. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  1093. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1094. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  1095. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1096. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  1097. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1098. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  1099. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1100. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  1101. #elif \
  1102. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  1103. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1104. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1105. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  1106. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1107. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  1108. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1109. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1110. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  1111. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1112. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  1113. #else
  1114. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1115. #endif
  1116. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1117. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1118. #endif
  1119. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1120. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1121. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1122. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1123. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1124. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1125. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1126. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1127. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1128. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1129. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1130. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1131. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1132. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1133. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1134. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1135. #elif \
  1136. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1137. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1138. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1139. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1140. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1141. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1142. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1143. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1144. #else
  1145. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1146. #endif
  1147. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1148. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1149. #endif
  1150. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1151. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1152. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1153. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1154. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1155. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1156. #else
  1157. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1158. #endif
  1159. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1160. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1161. #endif
  1162. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1163. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1164. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1165. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1166. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1167. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1168. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1169. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1170. #else
  1171. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1172. #endif
  1173. #if defined(JSON_HEDLEY_DEPRECATED)
  1174. #undef JSON_HEDLEY_DEPRECATED
  1175. #endif
  1176. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1177. #undef JSON_HEDLEY_DEPRECATED_FOR
  1178. #endif
  1179. #if \
  1180. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1181. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1182. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1183. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1184. #elif \
  1185. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1186. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1187. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1188. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1189. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1190. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1191. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1192. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1193. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1194. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1195. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1196. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1197. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1198. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1199. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1200. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1201. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1202. #elif \
  1203. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1204. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1205. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1206. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1207. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1208. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1209. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1210. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1211. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1212. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1213. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1214. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1215. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1216. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1217. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1218. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1219. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1220. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1221. #elif \
  1222. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1223. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1224. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1225. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1226. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1227. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1228. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1229. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1230. #else
  1231. #define JSON_HEDLEY_DEPRECATED(since)
  1232. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1233. #endif
  1234. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1235. #undef JSON_HEDLEY_UNAVAILABLE
  1236. #endif
  1237. #if \
  1238. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1239. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1240. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1241. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1242. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1243. #else
  1244. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1245. #endif
  1246. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1247. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1248. #endif
  1249. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1250. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1251. #endif
  1252. #if \
  1253. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1254. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1255. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1256. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1257. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1258. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1259. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1260. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1261. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1262. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1263. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1264. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1265. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1266. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1267. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1268. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1269. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1270. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1271. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1272. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1273. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1274. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1275. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1276. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1277. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1278. #elif defined(_Check_return_) /* SAL */
  1279. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1280. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1281. #else
  1282. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1283. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1284. #endif
  1285. #if defined(JSON_HEDLEY_SENTINEL)
  1286. #undef JSON_HEDLEY_SENTINEL
  1287. #endif
  1288. #if \
  1289. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1290. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1291. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1292. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1293. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1294. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1295. #else
  1296. #define JSON_HEDLEY_SENTINEL(position)
  1297. #endif
  1298. #if defined(JSON_HEDLEY_NO_RETURN)
  1299. #undef JSON_HEDLEY_NO_RETURN
  1300. #endif
  1301. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1302. #define JSON_HEDLEY_NO_RETURN __noreturn
  1303. #elif \
  1304. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1305. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1306. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1307. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1308. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1309. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1310. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1311. #elif \
  1312. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1313. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1314. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1315. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1316. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1317. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1318. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1319. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1320. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1321. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1322. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1323. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1324. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1325. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1326. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1327. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1328. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1329. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1330. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1331. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1332. #elif \
  1333. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1334. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1335. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1336. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1337. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1338. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1339. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1340. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1341. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1342. #else
  1343. #define JSON_HEDLEY_NO_RETURN
  1344. #endif
  1345. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1346. #undef JSON_HEDLEY_NO_ESCAPE
  1347. #endif
  1348. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1349. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1350. #else
  1351. #define JSON_HEDLEY_NO_ESCAPE
  1352. #endif
  1353. #if defined(JSON_HEDLEY_UNREACHABLE)
  1354. #undef JSON_HEDLEY_UNREACHABLE
  1355. #endif
  1356. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1357. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1358. #endif
  1359. #if defined(JSON_HEDLEY_ASSUME)
  1360. #undef JSON_HEDLEY_ASSUME
  1361. #endif
  1362. #if \
  1363. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1364. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1365. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1366. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1367. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1368. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1369. #elif \
  1370. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1371. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1372. #if defined(__cplusplus)
  1373. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1374. #else
  1375. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1376. #endif
  1377. #endif
  1378. #if \
  1379. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1380. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1381. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1382. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1383. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1384. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1385. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1386. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1387. #elif defined(JSON_HEDLEY_ASSUME)
  1388. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1389. #endif
  1390. #if !defined(JSON_HEDLEY_ASSUME)
  1391. #if defined(JSON_HEDLEY_UNREACHABLE)
  1392. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1393. #else
  1394. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1395. #endif
  1396. #endif
  1397. #if defined(JSON_HEDLEY_UNREACHABLE)
  1398. #if \
  1399. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1400. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1401. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1402. #else
  1403. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1404. #endif
  1405. #else
  1406. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1407. #endif
  1408. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1409. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1410. #endif
  1411. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1412. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1413. #pragma clang diagnostic ignored "-Wpedantic"
  1414. #endif
  1415. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1416. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1417. #endif
  1418. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1419. #if defined(__clang__)
  1420. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1421. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1422. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1423. #endif
  1424. #endif
  1425. #if defined(JSON_HEDLEY_NON_NULL)
  1426. #undef JSON_HEDLEY_NON_NULL
  1427. #endif
  1428. #if \
  1429. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1430. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1431. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1432. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1433. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1434. #else
  1435. #define JSON_HEDLEY_NON_NULL(...)
  1436. #endif
  1437. JSON_HEDLEY_DIAGNOSTIC_POP
  1438. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1439. #undef JSON_HEDLEY_PRINTF_FORMAT
  1440. #endif
  1441. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1442. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1443. #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
  1444. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
  1445. #elif \
  1446. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1447. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1448. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1449. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1450. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1451. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1452. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1453. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1454. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1455. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1456. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1457. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1458. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1459. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1460. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1461. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1462. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1463. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1464. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1465. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1466. #else
  1467. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1468. #endif
  1469. #if defined(JSON_HEDLEY_CONSTEXPR)
  1470. #undef JSON_HEDLEY_CONSTEXPR
  1471. #endif
  1472. #if defined(__cplusplus)
  1473. #if __cplusplus >= 201103L
  1474. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1475. #endif
  1476. #endif
  1477. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1478. #define JSON_HEDLEY_CONSTEXPR
  1479. #endif
  1480. #if defined(JSON_HEDLEY_PREDICT)
  1481. #undef JSON_HEDLEY_PREDICT
  1482. #endif
  1483. #if defined(JSON_HEDLEY_LIKELY)
  1484. #undef JSON_HEDLEY_LIKELY
  1485. #endif
  1486. #if defined(JSON_HEDLEY_UNLIKELY)
  1487. #undef JSON_HEDLEY_UNLIKELY
  1488. #endif
  1489. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1490. #undef JSON_HEDLEY_UNPREDICTABLE
  1491. #endif
  1492. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1493. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1494. #endif
  1495. #if \
  1496. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1497. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1498. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1499. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1500. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1501. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1502. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1503. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1504. #elif \
  1505. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1506. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1507. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1508. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1509. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1510. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1511. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1512. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1513. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1514. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1515. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1516. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1517. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1518. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1519. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1520. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1521. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1522. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1523. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1524. (__extension__ ({ \
  1525. double hedley_probability_ = (probability); \
  1526. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1527. }))
  1528. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1529. (__extension__ ({ \
  1530. double hedley_probability_ = (probability); \
  1531. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1532. }))
  1533. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1534. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1535. #else
  1536. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1537. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1538. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1539. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1540. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1541. #endif
  1542. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1543. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1544. #endif
  1545. #if defined(JSON_HEDLEY_MALLOC)
  1546. #undef JSON_HEDLEY_MALLOC
  1547. #endif
  1548. #if \
  1549. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1550. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1551. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1552. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1553. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1554. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1555. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1556. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1557. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1558. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1559. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1560. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1561. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1562. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1563. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1564. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1565. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1566. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1567. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1568. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1569. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1570. #elif \
  1571. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1572. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1573. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1574. #else
  1575. #define JSON_HEDLEY_MALLOC
  1576. #endif
  1577. #if defined(JSON_HEDLEY_PURE)
  1578. #undef JSON_HEDLEY_PURE
  1579. #endif
  1580. #if \
  1581. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1582. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1583. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1584. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1585. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1586. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1587. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1588. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1589. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1590. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1591. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1592. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1593. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1594. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1595. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1596. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1597. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1598. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1599. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1600. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1601. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1602. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1603. #elif defined(__cplusplus) && \
  1604. ( \
  1605. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1606. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1607. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1608. )
  1609. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1610. #else
  1611. # define JSON_HEDLEY_PURE
  1612. #endif
  1613. #if defined(JSON_HEDLEY_CONST)
  1614. #undef JSON_HEDLEY_CONST
  1615. #endif
  1616. #if \
  1617. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1618. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1619. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1620. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1621. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1622. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1623. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1624. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1625. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1626. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1627. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1628. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1629. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1630. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1631. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1632. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1633. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1634. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1635. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1636. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1637. #elif \
  1638. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1639. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1640. #else
  1641. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1642. #endif
  1643. #if defined(JSON_HEDLEY_RESTRICT)
  1644. #undef JSON_HEDLEY_RESTRICT
  1645. #endif
  1646. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1647. #define JSON_HEDLEY_RESTRICT restrict
  1648. #elif \
  1649. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1650. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1651. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1652. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1653. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1654. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1655. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1656. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1657. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1658. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1659. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1660. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1661. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1662. defined(__clang__) || \
  1663. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1664. #define JSON_HEDLEY_RESTRICT __restrict
  1665. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1666. #define JSON_HEDLEY_RESTRICT _Restrict
  1667. #else
  1668. #define JSON_HEDLEY_RESTRICT
  1669. #endif
  1670. #if defined(JSON_HEDLEY_INLINE)
  1671. #undef JSON_HEDLEY_INLINE
  1672. #endif
  1673. #if \
  1674. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1675. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1676. #define JSON_HEDLEY_INLINE inline
  1677. #elif \
  1678. defined(JSON_HEDLEY_GCC_VERSION) || \
  1679. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1680. #define JSON_HEDLEY_INLINE __inline__
  1681. #elif \
  1682. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1683. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1684. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1685. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1686. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1687. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1688. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1689. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1690. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1691. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1692. #define JSON_HEDLEY_INLINE __inline
  1693. #else
  1694. #define JSON_HEDLEY_INLINE
  1695. #endif
  1696. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1697. #undef JSON_HEDLEY_ALWAYS_INLINE
  1698. #endif
  1699. #if \
  1700. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1701. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1702. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1703. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1704. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1705. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1706. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1707. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1708. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1709. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1710. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1711. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1712. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1713. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1714. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1715. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1716. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1717. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1718. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1719. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1720. #elif \
  1721. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1722. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1723. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1724. #elif defined(__cplusplus) && \
  1725. ( \
  1726. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1727. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1728. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1729. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1730. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1731. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1732. )
  1733. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1734. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1735. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1736. #else
  1737. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1738. #endif
  1739. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1740. #undef JSON_HEDLEY_NEVER_INLINE
  1741. #endif
  1742. #if \
  1743. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1744. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1745. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1746. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1747. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1748. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1749. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1750. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1751. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1752. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1753. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1754. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1755. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1756. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1757. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1758. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1759. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1760. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1761. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1762. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1763. #elif \
  1764. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1765. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1766. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1767. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1768. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1769. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1770. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1771. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1772. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1773. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1774. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1775. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1776. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1777. #else
  1778. #define JSON_HEDLEY_NEVER_INLINE
  1779. #endif
  1780. #if defined(JSON_HEDLEY_PRIVATE)
  1781. #undef JSON_HEDLEY_PRIVATE
  1782. #endif
  1783. #if defined(JSON_HEDLEY_PUBLIC)
  1784. #undef JSON_HEDLEY_PUBLIC
  1785. #endif
  1786. #if defined(JSON_HEDLEY_IMPORT)
  1787. #undef JSON_HEDLEY_IMPORT
  1788. #endif
  1789. #if defined(_WIN32) || defined(__CYGWIN__)
  1790. # define JSON_HEDLEY_PRIVATE
  1791. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1792. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1793. #else
  1794. # if \
  1795. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1796. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1797. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1798. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1799. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1800. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1801. ( \
  1802. defined(__TI_EABI__) && \
  1803. ( \
  1804. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1805. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1806. ) \
  1807. ) || \
  1808. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1809. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1810. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1811. # else
  1812. # define JSON_HEDLEY_PRIVATE
  1813. # define JSON_HEDLEY_PUBLIC
  1814. # endif
  1815. # define JSON_HEDLEY_IMPORT extern
  1816. #endif
  1817. #if defined(JSON_HEDLEY_NO_THROW)
  1818. #undef JSON_HEDLEY_NO_THROW
  1819. #endif
  1820. #if \
  1821. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1822. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1823. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1824. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1825. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1826. #elif \
  1827. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1828. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1829. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1830. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1831. #else
  1832. #define JSON_HEDLEY_NO_THROW
  1833. #endif
  1834. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1835. #undef JSON_HEDLEY_FALL_THROUGH
  1836. #endif
  1837. #if \
  1838. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1839. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1840. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1841. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1842. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1843. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1844. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1845. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1846. #elif defined(__fallthrough) /* SAL */
  1847. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1848. #else
  1849. #define JSON_HEDLEY_FALL_THROUGH
  1850. #endif
  1851. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1852. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1853. #endif
  1854. #if \
  1855. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1856. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1857. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1858. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1859. #elif defined(_Ret_notnull_) /* SAL */
  1860. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1861. #else
  1862. #define JSON_HEDLEY_RETURNS_NON_NULL
  1863. #endif
  1864. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1865. #undef JSON_HEDLEY_ARRAY_PARAM
  1866. #endif
  1867. #if \
  1868. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1869. !defined(__STDC_NO_VLA__) && \
  1870. !defined(__cplusplus) && \
  1871. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1872. !defined(JSON_HEDLEY_TINYC_VERSION)
  1873. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1874. #else
  1875. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1876. #endif
  1877. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1878. #undef JSON_HEDLEY_IS_CONSTANT
  1879. #endif
  1880. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1881. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1882. #endif
  1883. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1884. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1885. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1886. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1887. #endif
  1888. #if \
  1889. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1890. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1891. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1892. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1893. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1894. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1895. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1896. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1897. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1898. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1899. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1900. #endif
  1901. #if !defined(__cplusplus)
  1902. # if \
  1903. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1904. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1905. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1906. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1907. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1908. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1909. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1910. #if defined(__INTPTR_TYPE__)
  1911. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1912. #else
  1913. #include <stdint.h>
  1914. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1915. #endif
  1916. # elif \
  1917. ( \
  1918. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1919. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1920. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1921. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1922. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1923. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1924. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1925. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1926. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1927. #if defined(__INTPTR_TYPE__)
  1928. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1929. #else
  1930. #include <stdint.h>
  1931. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1932. #endif
  1933. # elif \
  1934. defined(JSON_HEDLEY_GCC_VERSION) || \
  1935. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1936. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1937. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1938. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1939. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1940. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1941. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1942. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1943. defined(__clang__)
  1944. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1945. sizeof(void) != \
  1946. sizeof(*( \
  1947. 1 ? \
  1948. ((void*) ((expr) * 0L) ) : \
  1949. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1950. ) \
  1951. ) \
  1952. )
  1953. # endif
  1954. #endif
  1955. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1956. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1957. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1958. #endif
  1959. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1960. #else
  1961. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1962. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1963. #endif
  1964. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1965. #endif
  1966. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1967. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1968. #endif
  1969. #if defined(JSON_HEDLEY_END_C_DECLS)
  1970. #undef JSON_HEDLEY_END_C_DECLS
  1971. #endif
  1972. #if defined(JSON_HEDLEY_C_DECL)
  1973. #undef JSON_HEDLEY_C_DECL
  1974. #endif
  1975. #if defined(__cplusplus)
  1976. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1977. #define JSON_HEDLEY_END_C_DECLS }
  1978. #define JSON_HEDLEY_C_DECL extern "C"
  1979. #else
  1980. #define JSON_HEDLEY_BEGIN_C_DECLS
  1981. #define JSON_HEDLEY_END_C_DECLS
  1982. #define JSON_HEDLEY_C_DECL
  1983. #endif
  1984. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1985. #undef JSON_HEDLEY_STATIC_ASSERT
  1986. #endif
  1987. #if \
  1988. !defined(__cplusplus) && ( \
  1989. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  1990. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1991. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  1992. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1993. defined(_Static_assert) \
  1994. )
  1995. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  1996. #elif \
  1997. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  1998. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  1999. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2000. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  2001. #else
  2002. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  2003. #endif
  2004. #if defined(JSON_HEDLEY_NULL)
  2005. #undef JSON_HEDLEY_NULL
  2006. #endif
  2007. #if defined(__cplusplus)
  2008. #if __cplusplus >= 201103L
  2009. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  2010. #elif defined(NULL)
  2011. #define JSON_HEDLEY_NULL NULL
  2012. #else
  2013. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  2014. #endif
  2015. #elif defined(NULL)
  2016. #define JSON_HEDLEY_NULL NULL
  2017. #else
  2018. #define JSON_HEDLEY_NULL ((void*) 0)
  2019. #endif
  2020. #if defined(JSON_HEDLEY_MESSAGE)
  2021. #undef JSON_HEDLEY_MESSAGE
  2022. #endif
  2023. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2024. # define JSON_HEDLEY_MESSAGE(msg) \
  2025. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2026. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2027. JSON_HEDLEY_PRAGMA(message msg) \
  2028. JSON_HEDLEY_DIAGNOSTIC_POP
  2029. #elif \
  2030. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  2031. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2032. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  2033. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  2034. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  2035. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  2036. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2037. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  2038. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2039. #else
  2040. # define JSON_HEDLEY_MESSAGE(msg)
  2041. #endif
  2042. #if defined(JSON_HEDLEY_WARNING)
  2043. #undef JSON_HEDLEY_WARNING
  2044. #endif
  2045. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2046. # define JSON_HEDLEY_WARNING(msg) \
  2047. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2048. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2049. JSON_HEDLEY_PRAGMA(clang warning msg) \
  2050. JSON_HEDLEY_DIAGNOSTIC_POP
  2051. #elif \
  2052. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  2053. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  2054. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2055. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  2056. #elif \
  2057. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  2058. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2059. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2060. #else
  2061. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  2062. #endif
  2063. #if defined(JSON_HEDLEY_REQUIRE)
  2064. #undef JSON_HEDLEY_REQUIRE
  2065. #endif
  2066. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  2067. #undef JSON_HEDLEY_REQUIRE_MSG
  2068. #endif
  2069. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  2070. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  2071. # define JSON_HEDLEY_REQUIRE(expr) \
  2072. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2073. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2074. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  2075. JSON_HEDLEY_DIAGNOSTIC_POP
  2076. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  2077. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2078. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2079. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  2080. JSON_HEDLEY_DIAGNOSTIC_POP
  2081. # else
  2082. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  2083. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  2084. # endif
  2085. #else
  2086. # define JSON_HEDLEY_REQUIRE(expr)
  2087. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  2088. #endif
  2089. #if defined(JSON_HEDLEY_FLAGS)
  2090. #undef JSON_HEDLEY_FLAGS
  2091. #endif
  2092. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  2093. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  2094. #else
  2095. #define JSON_HEDLEY_FLAGS
  2096. #endif
  2097. #if defined(JSON_HEDLEY_FLAGS_CAST)
  2098. #undef JSON_HEDLEY_FLAGS_CAST
  2099. #endif
  2100. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  2101. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  2102. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2103. _Pragma("warning(disable:188)") \
  2104. ((T) (expr)); \
  2105. JSON_HEDLEY_DIAGNOSTIC_POP \
  2106. }))
  2107. #else
  2108. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  2109. #endif
  2110. #if defined(JSON_HEDLEY_EMPTY_BASES)
  2111. #undef JSON_HEDLEY_EMPTY_BASES
  2112. #endif
  2113. #if \
  2114. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2115. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2116. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2117. #else
  2118. #define JSON_HEDLEY_EMPTY_BASES
  2119. #endif
  2120. /* Remaining macros are deprecated. */
  2121. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2122. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2123. #endif
  2124. #if defined(__clang__)
  2125. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2126. #else
  2127. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2128. #endif
  2129. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2130. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2131. #endif
  2132. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2133. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2134. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2135. #endif
  2136. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2137. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2138. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2139. #endif
  2140. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2141. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2142. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2143. #endif
  2144. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2145. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2146. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2147. #endif
  2148. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2149. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2150. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2151. #endif
  2152. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2153. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2154. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2155. #endif
  2156. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2157. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2158. // This file contains all internal macro definitions (except those affecting ABI)
  2159. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2160. // #include <nlohmann/detail/abi_macros.hpp>
  2161. // exclude unsupported compilers
  2162. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2163. #if defined(__clang__)
  2164. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2165. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2166. #endif
  2167. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2168. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2169. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2170. #endif
  2171. #endif
  2172. #endif
  2173. // C++ language standard detection
  2174. // if the user manually specified the used c++ version this is skipped
  2175. #if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2176. #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
  2177. #define JSON_HAS_CPP_20
  2178. #define JSON_HAS_CPP_17
  2179. #define JSON_HAS_CPP_14
  2180. #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2181. #define JSON_HAS_CPP_17
  2182. #define JSON_HAS_CPP_14
  2183. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2184. #define JSON_HAS_CPP_14
  2185. #endif
  2186. // the cpp 11 flag is always specified because it is the minimal required version
  2187. #define JSON_HAS_CPP_11
  2188. #endif
  2189. #ifdef __has_include
  2190. #if __has_include(<version>)
  2191. #include <version>
  2192. #endif
  2193. #endif
  2194. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2195. #ifdef JSON_HAS_CPP_17
  2196. #if defined(__cpp_lib_filesystem)
  2197. #define JSON_HAS_FILESYSTEM 1
  2198. #elif defined(__cpp_lib_experimental_filesystem)
  2199. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2200. #elif !defined(__has_include)
  2201. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2202. #elif __has_include(<filesystem>)
  2203. #define JSON_HAS_FILESYSTEM 1
  2204. #elif __has_include(<experimental/filesystem>)
  2205. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2206. #endif
  2207. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2208. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2209. #undef JSON_HAS_FILESYSTEM
  2210. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2211. #endif
  2212. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2213. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2214. #undef JSON_HAS_FILESYSTEM
  2215. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2216. #endif
  2217. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2218. #if defined(__clang_major__) && __clang_major__ < 7
  2219. #undef JSON_HAS_FILESYSTEM
  2220. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2221. #endif
  2222. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2223. #if defined(_MSC_VER) && _MSC_VER < 1914
  2224. #undef JSON_HAS_FILESYSTEM
  2225. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2226. #endif
  2227. // no filesystem support before iOS 13
  2228. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2229. #undef JSON_HAS_FILESYSTEM
  2230. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2231. #endif
  2232. // no filesystem support before macOS Catalina
  2233. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2234. #undef JSON_HAS_FILESYSTEM
  2235. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2236. #endif
  2237. #endif
  2238. #endif
  2239. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2240. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2241. #endif
  2242. #ifndef JSON_HAS_FILESYSTEM
  2243. #define JSON_HAS_FILESYSTEM 0
  2244. #endif
  2245. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2246. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2247. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2248. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2249. #else
  2250. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2251. #endif
  2252. #endif
  2253. #ifndef JSON_HAS_RANGES
  2254. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
  2255. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2256. #define JSON_HAS_RANGES 0
  2257. #elif defined(__cpp_lib_ranges)
  2258. #define JSON_HAS_RANGES 1
  2259. #else
  2260. #define JSON_HAS_RANGES 0
  2261. #endif
  2262. #endif
  2263. #ifndef JSON_HAS_STATIC_RTTI
  2264. #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
  2265. #define JSON_HAS_STATIC_RTTI 1
  2266. #else
  2267. #define JSON_HAS_STATIC_RTTI 0
  2268. #endif
  2269. #endif
  2270. #ifdef JSON_HAS_CPP_17
  2271. #define JSON_INLINE_VARIABLE inline
  2272. #else
  2273. #define JSON_INLINE_VARIABLE
  2274. #endif
  2275. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2276. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2277. #else
  2278. #define JSON_NO_UNIQUE_ADDRESS
  2279. #endif
  2280. // disable documentation warnings on clang
  2281. #if defined(__clang__)
  2282. #pragma clang diagnostic push
  2283. #pragma clang diagnostic ignored "-Wdocumentation"
  2284. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2285. #endif
  2286. // allow disabling exceptions
  2287. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2288. #define JSON_THROW(exception) throw exception
  2289. #define JSON_TRY try
  2290. #define JSON_CATCH(exception) catch(exception)
  2291. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2292. #else
  2293. #include <cstdlib>
  2294. #define JSON_THROW(exception) std::abort()
  2295. #define JSON_TRY if(true)
  2296. #define JSON_CATCH(exception) if(false)
  2297. #define JSON_INTERNAL_CATCH(exception) if(false)
  2298. #endif
  2299. // override exception macros
  2300. #if defined(JSON_THROW_USER)
  2301. #undef JSON_THROW
  2302. #define JSON_THROW JSON_THROW_USER
  2303. #endif
  2304. #if defined(JSON_TRY_USER)
  2305. #undef JSON_TRY
  2306. #define JSON_TRY JSON_TRY_USER
  2307. #endif
  2308. #if defined(JSON_CATCH_USER)
  2309. #undef JSON_CATCH
  2310. #define JSON_CATCH JSON_CATCH_USER
  2311. #undef JSON_INTERNAL_CATCH
  2312. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2313. #endif
  2314. #if defined(JSON_INTERNAL_CATCH_USER)
  2315. #undef JSON_INTERNAL_CATCH
  2316. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2317. #endif
  2318. // allow overriding assert
  2319. #if !defined(JSON_ASSERT)
  2320. #include <cassert> // assert
  2321. #define JSON_ASSERT(x) assert(x)
  2322. #endif
  2323. // allow to access some private functions (needed by the test suite)
  2324. #if defined(JSON_TESTS_PRIVATE)
  2325. #define JSON_PRIVATE_UNLESS_TESTED public
  2326. #else
  2327. #define JSON_PRIVATE_UNLESS_TESTED private
  2328. #endif
  2329. /*!
  2330. @brief macro to briefly define a mapping between an enum and JSON
  2331. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2332. @since version 3.4.0
  2333. */
  2334. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2335. template<typename BasicJsonType> \
  2336. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2337. { \
  2338. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2339. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2340. auto it = std::find_if(std::begin(m), std::end(m), \
  2341. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2342. { \
  2343. return ej_pair.first == e; \
  2344. }); \
  2345. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2346. } \
  2347. template<typename BasicJsonType> \
  2348. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2349. { \
  2350. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2351. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2352. auto it = std::find_if(std::begin(m), std::end(m), \
  2353. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2354. { \
  2355. return ej_pair.second == j; \
  2356. }); \
  2357. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2358. }
  2359. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2360. // may be removed in the future once the class is split.
  2361. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2362. template<template<typename, typename, typename...> class ObjectType, \
  2363. template<typename, typename...> class ArrayType, \
  2364. class StringType, class BooleanType, class NumberIntegerType, \
  2365. class NumberUnsignedType, class NumberFloatType, \
  2366. template<typename> class AllocatorType, \
  2367. template<typename, typename = void> class JSONSerializer, \
  2368. class BinaryType, \
  2369. class CustomBaseClass>
  2370. #define NLOHMANN_BASIC_JSON_TPL \
  2371. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2372. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2373. AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
  2374. // Macros to simplify conversion from/to types
  2375. #define NLOHMANN_JSON_EXPAND( x ) x
  2376. #define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME
  2377. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2378. NLOHMANN_JSON_PASTE64, \
  2379. NLOHMANN_JSON_PASTE63, \
  2380. NLOHMANN_JSON_PASTE62, \
  2381. NLOHMANN_JSON_PASTE61, \
  2382. NLOHMANN_JSON_PASTE60, \
  2383. NLOHMANN_JSON_PASTE59, \
  2384. NLOHMANN_JSON_PASTE58, \
  2385. NLOHMANN_JSON_PASTE57, \
  2386. NLOHMANN_JSON_PASTE56, \
  2387. NLOHMANN_JSON_PASTE55, \
  2388. NLOHMANN_JSON_PASTE54, \
  2389. NLOHMANN_JSON_PASTE53, \
  2390. NLOHMANN_JSON_PASTE52, \
  2391. NLOHMANN_JSON_PASTE51, \
  2392. NLOHMANN_JSON_PASTE50, \
  2393. NLOHMANN_JSON_PASTE49, \
  2394. NLOHMANN_JSON_PASTE48, \
  2395. NLOHMANN_JSON_PASTE47, \
  2396. NLOHMANN_JSON_PASTE46, \
  2397. NLOHMANN_JSON_PASTE45, \
  2398. NLOHMANN_JSON_PASTE44, \
  2399. NLOHMANN_JSON_PASTE43, \
  2400. NLOHMANN_JSON_PASTE42, \
  2401. NLOHMANN_JSON_PASTE41, \
  2402. NLOHMANN_JSON_PASTE40, \
  2403. NLOHMANN_JSON_PASTE39, \
  2404. NLOHMANN_JSON_PASTE38, \
  2405. NLOHMANN_JSON_PASTE37, \
  2406. NLOHMANN_JSON_PASTE36, \
  2407. NLOHMANN_JSON_PASTE35, \
  2408. NLOHMANN_JSON_PASTE34, \
  2409. NLOHMANN_JSON_PASTE33, \
  2410. NLOHMANN_JSON_PASTE32, \
  2411. NLOHMANN_JSON_PASTE31, \
  2412. NLOHMANN_JSON_PASTE30, \
  2413. NLOHMANN_JSON_PASTE29, \
  2414. NLOHMANN_JSON_PASTE28, \
  2415. NLOHMANN_JSON_PASTE27, \
  2416. NLOHMANN_JSON_PASTE26, \
  2417. NLOHMANN_JSON_PASTE25, \
  2418. NLOHMANN_JSON_PASTE24, \
  2419. NLOHMANN_JSON_PASTE23, \
  2420. NLOHMANN_JSON_PASTE22, \
  2421. NLOHMANN_JSON_PASTE21, \
  2422. NLOHMANN_JSON_PASTE20, \
  2423. NLOHMANN_JSON_PASTE19, \
  2424. NLOHMANN_JSON_PASTE18, \
  2425. NLOHMANN_JSON_PASTE17, \
  2426. NLOHMANN_JSON_PASTE16, \
  2427. NLOHMANN_JSON_PASTE15, \
  2428. NLOHMANN_JSON_PASTE14, \
  2429. NLOHMANN_JSON_PASTE13, \
  2430. NLOHMANN_JSON_PASTE12, \
  2431. NLOHMANN_JSON_PASTE11, \
  2432. NLOHMANN_JSON_PASTE10, \
  2433. NLOHMANN_JSON_PASTE9, \
  2434. NLOHMANN_JSON_PASTE8, \
  2435. NLOHMANN_JSON_PASTE7, \
  2436. NLOHMANN_JSON_PASTE6, \
  2437. NLOHMANN_JSON_PASTE5, \
  2438. NLOHMANN_JSON_PASTE4, \
  2439. NLOHMANN_JSON_PASTE3, \
  2440. NLOHMANN_JSON_PASTE2, \
  2441. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2442. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2443. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2444. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2445. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2446. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2447. #define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6)
  2448. #define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7)
  2449. #define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8)
  2450. #define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9)
  2451. #define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10)
  2452. #define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11)
  2453. #define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12)
  2454. #define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13)
  2455. #define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14)
  2456. #define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15)
  2457. #define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16)
  2458. #define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17)
  2459. #define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18)
  2460. #define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19)
  2461. #define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20)
  2462. #define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21)
  2463. #define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)
  2464. #define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23)
  2465. #define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24)
  2466. #define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25)
  2467. #define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26)
  2468. #define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27)
  2469. #define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28)
  2470. #define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29)
  2471. #define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30)
  2472. #define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31)
  2473. #define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32)
  2474. #define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33)
  2475. #define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34)
  2476. #define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35)
  2477. #define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36)
  2478. #define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37)
  2479. #define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38)
  2480. #define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39)
  2481. #define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40)
  2482. #define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41)
  2483. #define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42)
  2484. #define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43)
  2485. #define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44)
  2486. #define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45)
  2487. #define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46)
  2488. #define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47)
  2489. #define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48)
  2490. #define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49)
  2491. #define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50)
  2492. #define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51)
  2493. #define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52)
  2494. #define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53)
  2495. #define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54)
  2496. #define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55)
  2497. #define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56)
  2498. #define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57)
  2499. #define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58)
  2500. #define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59)
  2501. #define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60)
  2502. #define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61)
  2503. #define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62)
  2504. #define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63)
  2505. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2506. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2507. #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
  2508. /*!
  2509. @brief macro
  2510. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2511. @since version 3.9.0
  2512. */
  2513. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2514. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2515. friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2516. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2517. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2518. friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2519. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2520. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2521. /*!
  2522. @brief macro
  2523. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2524. @since version 3.9.0
  2525. */
  2526. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2527. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2528. inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
  2529. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
  2530. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
  2531. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2532. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2533. inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2534. // inspired from https://stackoverflow.com/a/26745591
  2535. // allows to call any std function as if (e.g. with begin):
  2536. // using std::begin; begin(x);
  2537. //
  2538. // it allows using the detected idiom to retrieve the return type
  2539. // of such an expression
  2540. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2541. namespace detail { \
  2542. using std::std_name; \
  2543. \
  2544. template<typename... T> \
  2545. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2546. } \
  2547. \
  2548. namespace detail2 { \
  2549. struct std_name##_tag \
  2550. { \
  2551. }; \
  2552. \
  2553. template<typename... T> \
  2554. std_name##_tag std_name(T&&...); \
  2555. \
  2556. template<typename... T> \
  2557. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2558. \
  2559. template<typename... T> \
  2560. struct would_call_std_##std_name \
  2561. { \
  2562. static constexpr auto const value = ::nlohmann::detail:: \
  2563. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2564. }; \
  2565. } /* namespace detail2 */ \
  2566. \
  2567. template<typename... T> \
  2568. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2569. { \
  2570. }
  2571. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2572. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2573. #endif
  2574. #if JSON_USE_IMPLICIT_CONVERSIONS
  2575. #define JSON_EXPLICIT
  2576. #else
  2577. #define JSON_EXPLICIT explicit
  2578. #endif
  2579. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2580. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2581. #endif
  2582. #ifndef JSON_USE_GLOBAL_UDLS
  2583. #define JSON_USE_GLOBAL_UDLS 1
  2584. #endif
  2585. #if JSON_HAS_THREE_WAY_COMPARISON
  2586. #include <compare> // partial_ordering
  2587. #endif
  2588. NLOHMANN_JSON_NAMESPACE_BEGIN
  2589. namespace detail
  2590. {
  2591. ///////////////////////////
  2592. // JSON type enumeration //
  2593. ///////////////////////////
  2594. /*!
  2595. @brief the JSON type enumeration
  2596. This enumeration collects the different JSON types. It is internally used to
  2597. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2598. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2599. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2600. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2601. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2602. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2603. @ref basic_json::is_structured() rely on it.
  2604. @note There are three enumeration entries (number_integer, number_unsigned, and
  2605. number_float), because the library distinguishes these three types for numbers:
  2606. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2607. @ref basic_json::number_integer_t is used for signed integers, and
  2608. @ref basic_json::number_float_t is used for floating-point numbers or to
  2609. approximate integers which do not fit in the limits of their respective type.
  2610. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2611. value with the default value for a given type
  2612. @since version 1.0.0
  2613. */
  2614. enum class value_t : std::uint8_t
  2615. {
  2616. null, ///< null value
  2617. object, ///< object (unordered set of name/value pairs)
  2618. array, ///< array (ordered collection of values)
  2619. string, ///< string value
  2620. boolean, ///< boolean value
  2621. number_integer, ///< number value (signed integer)
  2622. number_unsigned, ///< number value (unsigned integer)
  2623. number_float, ///< number value (floating-point)
  2624. binary, ///< binary array (ordered collection of bytes)
  2625. discarded ///< discarded by the parser callback function
  2626. };
  2627. /*!
  2628. @brief comparison operator for JSON types
  2629. Returns an ordering that is similar to Python:
  2630. - order: null < boolean < number < object < array < string < binary
  2631. - furthermore, each type is not smaller than itself
  2632. - discarded values are not comparable
  2633. - binary is represented as a b"" string in python and directly comparable to a
  2634. string; however, making a binary array directly comparable with a string would
  2635. be surprising behavior in a JSON file.
  2636. @since version 1.0.0
  2637. */
  2638. #if JSON_HAS_THREE_WAY_COMPARISON
  2639. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2640. #else
  2641. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2642. #endif
  2643. {
  2644. static constexpr std::array<std::uint8_t, 9> order = {{
  2645. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2646. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2647. 6 /* binary */
  2648. }
  2649. };
  2650. const auto l_index = static_cast<std::size_t>(lhs);
  2651. const auto r_index = static_cast<std::size_t>(rhs);
  2652. #if JSON_HAS_THREE_WAY_COMPARISON
  2653. if (l_index < order.size() && r_index < order.size())
  2654. {
  2655. return order[l_index] <=> order[r_index]; // *NOPAD*
  2656. }
  2657. return std::partial_ordering::unordered;
  2658. #else
  2659. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2660. #endif
  2661. }
  2662. // GCC selects the built-in operator< over an operator rewritten from
  2663. // a user-defined spaceship operator
  2664. // Clang, MSVC, and ICC select the rewritten candidate
  2665. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2666. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2667. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2668. {
  2669. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2670. }
  2671. #endif
  2672. } // namespace detail
  2673. NLOHMANN_JSON_NAMESPACE_END
  2674. // #include <nlohmann/detail/string_escape.hpp>
  2675. // __ _____ _____ _____
  2676. // __| | __| | | | JSON for Modern C++
  2677. // | | |__ | | | | | | version 3.11.3
  2678. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2679. //
  2680. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2681. // SPDX-License-Identifier: MIT
  2682. // #include <nlohmann/detail/abi_macros.hpp>
  2683. NLOHMANN_JSON_NAMESPACE_BEGIN
  2684. namespace detail
  2685. {
  2686. /*!
  2687. @brief replace all occurrences of a substring by another string
  2688. @param[in,out] s the string to manipulate; changed so that all
  2689. occurrences of @a f are replaced with @a t
  2690. @param[in] f the substring to replace with @a t
  2691. @param[in] t the string to replace @a f
  2692. @pre The search string @a f must not be empty. **This precondition is
  2693. enforced with an assertion.**
  2694. @since version 2.0.0
  2695. */
  2696. template<typename StringType>
  2697. inline void replace_substring(StringType& s, const StringType& f,
  2698. const StringType& t)
  2699. {
  2700. JSON_ASSERT(!f.empty());
  2701. for (auto pos = s.find(f); // find first occurrence of f
  2702. pos != StringType::npos; // make sure f was found
  2703. s.replace(pos, f.size(), t), // replace with t, and
  2704. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2705. {}
  2706. }
  2707. /*!
  2708. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2709. * @param[in] s string to escape
  2710. * @return escaped string
  2711. *
  2712. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2713. */
  2714. template<typename StringType>
  2715. inline StringType escape(StringType s)
  2716. {
  2717. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2718. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2719. return s;
  2720. }
  2721. /*!
  2722. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2723. * @param[in] s string to unescape
  2724. * @return unescaped string
  2725. *
  2726. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2727. */
  2728. template<typename StringType>
  2729. static void unescape(StringType& s)
  2730. {
  2731. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2732. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2733. }
  2734. } // namespace detail
  2735. NLOHMANN_JSON_NAMESPACE_END
  2736. // #include <nlohmann/detail/input/position_t.hpp>
  2737. // __ _____ _____ _____
  2738. // __| | __| | | | JSON for Modern C++
  2739. // | | |__ | | | | | | version 3.11.3
  2740. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2741. //
  2742. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2743. // SPDX-License-Identifier: MIT
  2744. #include <cstddef> // size_t
  2745. // #include <nlohmann/detail/abi_macros.hpp>
  2746. NLOHMANN_JSON_NAMESPACE_BEGIN
  2747. namespace detail
  2748. {
  2749. /// struct to capture the start position of the current token
  2750. struct position_t
  2751. {
  2752. /// the total number of characters read
  2753. std::size_t chars_read_total = 0;
  2754. /// the number of characters read in the current line
  2755. std::size_t chars_read_current_line = 0;
  2756. /// the number of lines read
  2757. std::size_t lines_read = 0;
  2758. /// conversion to size_t to preserve SAX interface
  2759. constexpr operator size_t() const
  2760. {
  2761. return chars_read_total;
  2762. }
  2763. };
  2764. } // namespace detail
  2765. NLOHMANN_JSON_NAMESPACE_END
  2766. // #include <nlohmann/detail/macro_scope.hpp>
  2767. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2768. // __ _____ _____ _____
  2769. // __| | __| | | | JSON for Modern C++
  2770. // | | |__ | | | | | | version 3.11.3
  2771. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2772. //
  2773. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2774. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2775. // SPDX-License-Identifier: MIT
  2776. #include <array> // array
  2777. #include <cstddef> // size_t
  2778. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2779. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2780. // #include <nlohmann/detail/macro_scope.hpp>
  2781. NLOHMANN_JSON_NAMESPACE_BEGIN
  2782. namespace detail
  2783. {
  2784. template<typename T>
  2785. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2786. #ifdef JSON_HAS_CPP_14
  2787. // the following utilities are natively available in C++14
  2788. using std::enable_if_t;
  2789. using std::index_sequence;
  2790. using std::make_index_sequence;
  2791. using std::index_sequence_for;
  2792. #else
  2793. // alias templates to reduce boilerplate
  2794. template<bool B, typename T = void>
  2795. using enable_if_t = typename std::enable_if<B, T>::type;
  2796. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2797. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2798. //// START OF CODE FROM GOOGLE ABSEIL
  2799. // integer_sequence
  2800. //
  2801. // Class template representing a compile-time integer sequence. An instantiation
  2802. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2803. // type through its template arguments (which is a common need when
  2804. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2805. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2806. //
  2807. // Example:
  2808. //
  2809. // template< class T, T... Ints >
  2810. // void user_function(integer_sequence<T, Ints...>);
  2811. //
  2812. // int main()
  2813. // {
  2814. // // user_function's `T` will be deduced to `int` and `Ints...`
  2815. // // will be deduced to `0, 1, 2, 3, 4`.
  2816. // user_function(make_integer_sequence<int, 5>());
  2817. // }
  2818. template <typename T, T... Ints>
  2819. struct integer_sequence
  2820. {
  2821. using value_type = T;
  2822. static constexpr std::size_t size() noexcept
  2823. {
  2824. return sizeof...(Ints);
  2825. }
  2826. };
  2827. // index_sequence
  2828. //
  2829. // A helper template for an `integer_sequence` of `size_t`,
  2830. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2831. // `std::index_sequence`.
  2832. template <size_t... Ints>
  2833. using index_sequence = integer_sequence<size_t, Ints...>;
  2834. namespace utility_internal
  2835. {
  2836. template <typename Seq, size_t SeqSize, size_t Rem>
  2837. struct Extend;
  2838. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2839. template <typename T, T... Ints, size_t SeqSize>
  2840. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2841. {
  2842. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2843. };
  2844. template <typename T, T... Ints, size_t SeqSize>
  2845. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2846. {
  2847. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2848. };
  2849. // Recursion helper for 'make_integer_sequence<T, N>'.
  2850. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2851. template <typename T, size_t N>
  2852. struct Gen
  2853. {
  2854. using type =
  2855. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2856. };
  2857. template <typename T>
  2858. struct Gen<T, 0>
  2859. {
  2860. using type = integer_sequence<T>;
  2861. };
  2862. } // namespace utility_internal
  2863. // Compile-time sequences of integers
  2864. // make_integer_sequence
  2865. //
  2866. // This template alias is equivalent to
  2867. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2868. // replacement for C++14's `std::make_integer_sequence`.
  2869. template <typename T, T N>
  2870. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2871. // make_index_sequence
  2872. //
  2873. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2874. // and is designed to be a drop-in replacement for C++14's
  2875. // `std::make_index_sequence`.
  2876. template <size_t N>
  2877. using make_index_sequence = make_integer_sequence<size_t, N>;
  2878. // index_sequence_for
  2879. //
  2880. // Converts a typename pack into an index sequence of the same length, and
  2881. // is designed to be a drop-in replacement for C++14's
  2882. // `std::index_sequence_for()`
  2883. template <typename... Ts>
  2884. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2885. //// END OF CODE FROM GOOGLE ABSEIL
  2886. #endif
  2887. // dispatch utility (taken from ranges-v3)
  2888. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2889. template<> struct priority_tag<0> {};
  2890. // taken from ranges-v3
  2891. template<typename T>
  2892. struct static_const
  2893. {
  2894. static JSON_INLINE_VARIABLE constexpr T value{};
  2895. };
  2896. #ifndef JSON_HAS_CPP_17
  2897. template<typename T>
  2898. constexpr T static_const<T>::value;
  2899. #endif
  2900. template<typename T, typename... Args>
  2901. inline constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  2902. {
  2903. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  2904. }
  2905. } // namespace detail
  2906. NLOHMANN_JSON_NAMESPACE_END
  2907. // #include <nlohmann/detail/meta/type_traits.hpp>
  2908. // __ _____ _____ _____
  2909. // __| | __| | | | JSON for Modern C++
  2910. // | | |__ | | | | | | version 3.11.3
  2911. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2912. //
  2913. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2914. // SPDX-License-Identifier: MIT
  2915. #include <limits> // numeric_limits
  2916. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2917. #include <utility> // declval
  2918. #include <tuple> // tuple
  2919. #include <string> // char_traits
  2920. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2921. // __ _____ _____ _____
  2922. // __| | __| | | | JSON for Modern C++
  2923. // | | |__ | | | | | | version 3.11.3
  2924. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2925. //
  2926. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2927. // SPDX-License-Identifier: MIT
  2928. #include <iterator> // random_access_iterator_tag
  2929. // #include <nlohmann/detail/abi_macros.hpp>
  2930. // #include <nlohmann/detail/meta/void_t.hpp>
  2931. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2932. NLOHMANN_JSON_NAMESPACE_BEGIN
  2933. namespace detail
  2934. {
  2935. template<typename It, typename = void>
  2936. struct iterator_types {};
  2937. template<typename It>
  2938. struct iterator_types <
  2939. It,
  2940. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2941. typename It::reference, typename It::iterator_category >>
  2942. {
  2943. using difference_type = typename It::difference_type;
  2944. using value_type = typename It::value_type;
  2945. using pointer = typename It::pointer;
  2946. using reference = typename It::reference;
  2947. using iterator_category = typename It::iterator_category;
  2948. };
  2949. // This is required as some compilers implement std::iterator_traits in a way that
  2950. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2951. template<typename T, typename = void>
  2952. struct iterator_traits
  2953. {
  2954. };
  2955. template<typename T>
  2956. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2957. : iterator_types<T>
  2958. {
  2959. };
  2960. template<typename T>
  2961. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2962. {
  2963. using iterator_category = std::random_access_iterator_tag;
  2964. using value_type = T;
  2965. using difference_type = ptrdiff_t;
  2966. using pointer = T*;
  2967. using reference = T&;
  2968. };
  2969. } // namespace detail
  2970. NLOHMANN_JSON_NAMESPACE_END
  2971. // #include <nlohmann/detail/macro_scope.hpp>
  2972. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  2973. // __ _____ _____ _____
  2974. // __| | __| | | | JSON for Modern C++
  2975. // | | |__ | | | | | | version 3.11.3
  2976. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2977. //
  2978. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2979. // SPDX-License-Identifier: MIT
  2980. // #include <nlohmann/detail/macro_scope.hpp>
  2981. NLOHMANN_JSON_NAMESPACE_BEGIN
  2982. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  2983. NLOHMANN_JSON_NAMESPACE_END
  2984. // #include <nlohmann/detail/meta/call_std/end.hpp>
  2985. // __ _____ _____ _____
  2986. // __| | __| | | | JSON for Modern C++
  2987. // | | |__ | | | | | | version 3.11.3
  2988. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2989. //
  2990. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  2991. // SPDX-License-Identifier: MIT
  2992. // #include <nlohmann/detail/macro_scope.hpp>
  2993. NLOHMANN_JSON_NAMESPACE_BEGIN
  2994. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  2995. NLOHMANN_JSON_NAMESPACE_END
  2996. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2997. // #include <nlohmann/detail/meta/detected.hpp>
  2998. // #include <nlohmann/json_fwd.hpp>
  2999. // __ _____ _____ _____
  3000. // __| | __| | | | JSON for Modern C++
  3001. // | | |__ | | | | | | version 3.11.3
  3002. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3003. //
  3004. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  3005. // SPDX-License-Identifier: MIT
  3006. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3007. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3008. #include <cstdint> // int64_t, uint64_t
  3009. #include <map> // map
  3010. #include <memory> // allocator
  3011. #include <string> // string
  3012. #include <vector> // vector
  3013. // #include <nlohmann/detail/abi_macros.hpp>
  3014. /*!
  3015. @brief namespace for Niels Lohmann
  3016. @see https://github.com/nlohmann
  3017. @since version 1.0.0
  3018. */
  3019. NLOHMANN_JSON_NAMESPACE_BEGIN
  3020. /*!
  3021. @brief default JSONSerializer template argument
  3022. This serializer ignores the template arguments and uses ADL
  3023. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3024. for serialization.
  3025. */
  3026. template<typename T = void, typename SFINAE = void>
  3027. struct adl_serializer;
  3028. /// a class to store JSON values
  3029. /// @sa https://json.nlohmann.me/api/basic_json/
  3030. template<template<typename U, typename V, typename... Args> class ObjectType =
  3031. std::map,
  3032. template<typename U, typename... Args> class ArrayType = std::vector,
  3033. class StringType = std::string, class BooleanType = bool,
  3034. class NumberIntegerType = std::int64_t,
  3035. class NumberUnsignedType = std::uint64_t,
  3036. class NumberFloatType = double,
  3037. template<typename U> class AllocatorType = std::allocator,
  3038. template<typename T, typename SFINAE = void> class JSONSerializer =
  3039. adl_serializer,
  3040. class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
  3041. class CustomBaseClass = void>
  3042. class basic_json;
  3043. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3044. /// @sa https://json.nlohmann.me/api/json_pointer/
  3045. template<typename RefStringType>
  3046. class json_pointer;
  3047. /*!
  3048. @brief default specialization
  3049. @sa https://json.nlohmann.me/api/json/
  3050. */
  3051. using json = basic_json<>;
  3052. /// @brief a minimal map-like container that preserves insertion order
  3053. /// @sa https://json.nlohmann.me/api/ordered_map/
  3054. template<class Key, class T, class IgnoredLess, class Allocator>
  3055. struct ordered_map;
  3056. /// @brief specialization that maintains the insertion order of object keys
  3057. /// @sa https://json.nlohmann.me/api/ordered_json/
  3058. using ordered_json = basic_json<nlohmann::ordered_map>;
  3059. NLOHMANN_JSON_NAMESPACE_END
  3060. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3061. NLOHMANN_JSON_NAMESPACE_BEGIN
  3062. /*!
  3063. @brief detail namespace with internal helper functions
  3064. This namespace collects functions that should not be exposed,
  3065. implementations of some @ref basic_json methods, and meta-programming helpers.
  3066. @since version 2.1.0
  3067. */
  3068. namespace detail
  3069. {
  3070. /////////////
  3071. // helpers //
  3072. /////////////
  3073. // Note to maintainers:
  3074. //
  3075. // Every trait in this file expects a non CV-qualified type.
  3076. // The only exceptions are in the 'aliases for detected' section
  3077. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3078. //
  3079. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3080. // (e.g. to_json(BasicJsonType&, const T&))
  3081. template<typename> struct is_basic_json : std::false_type {};
  3082. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3083. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3084. // used by exceptions create() member functions
  3085. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  3086. // false_type otherwise
  3087. template<typename BasicJsonContext>
  3088. struct is_basic_json_context :
  3089. std::integral_constant < bool,
  3090. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3091. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3092. {};
  3093. //////////////////////
  3094. // json_ref helpers //
  3095. //////////////////////
  3096. template<typename>
  3097. class json_ref;
  3098. template<typename>
  3099. struct is_json_ref : std::false_type {};
  3100. template<typename T>
  3101. struct is_json_ref<json_ref<T>> : std::true_type {};
  3102. //////////////////////////
  3103. // aliases for detected //
  3104. //////////////////////////
  3105. template<typename T>
  3106. using mapped_type_t = typename T::mapped_type;
  3107. template<typename T>
  3108. using key_type_t = typename T::key_type;
  3109. template<typename T>
  3110. using value_type_t = typename T::value_type;
  3111. template<typename T>
  3112. using difference_type_t = typename T::difference_type;
  3113. template<typename T>
  3114. using pointer_t = typename T::pointer;
  3115. template<typename T>
  3116. using reference_t = typename T::reference;
  3117. template<typename T>
  3118. using iterator_category_t = typename T::iterator_category;
  3119. template<typename T, typename... Args>
  3120. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3121. template<typename T, typename... Args>
  3122. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3123. template<typename T, typename U>
  3124. using get_template_function = decltype(std::declval<T>().template get<U>());
  3125. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3126. template<typename BasicJsonType, typename T, typename = void>
  3127. struct has_from_json : std::false_type {};
  3128. // trait checking if j.get<T> is valid
  3129. // use this trait instead of std::is_constructible or std::is_convertible,
  3130. // both rely on, or make use of implicit conversions, and thus fail when T
  3131. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3132. template <typename BasicJsonType, typename T>
  3133. struct is_getable
  3134. {
  3135. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3136. };
  3137. template<typename BasicJsonType, typename T>
  3138. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3139. {
  3140. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3141. static constexpr bool value =
  3142. is_detected_exact<void, from_json_function, serializer,
  3143. const BasicJsonType&, T&>::value;
  3144. };
  3145. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3146. // this overload is used for non-default-constructible user-defined-types
  3147. template<typename BasicJsonType, typename T, typename = void>
  3148. struct has_non_default_from_json : std::false_type {};
  3149. template<typename BasicJsonType, typename T>
  3150. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3151. {
  3152. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3153. static constexpr bool value =
  3154. is_detected_exact<T, from_json_function, serializer,
  3155. const BasicJsonType&>::value;
  3156. };
  3157. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3158. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3159. template<typename BasicJsonType, typename T, typename = void>
  3160. struct has_to_json : std::false_type {};
  3161. template<typename BasicJsonType, typename T>
  3162. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3163. {
  3164. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3165. static constexpr bool value =
  3166. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3167. T>::value;
  3168. };
  3169. template<typename T>
  3170. using detect_key_compare = typename T::key_compare;
  3171. template<typename T>
  3172. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3173. // obtains the actual object key comparator
  3174. template<typename BasicJsonType>
  3175. struct actual_object_comparator
  3176. {
  3177. using object_t = typename BasicJsonType::object_t;
  3178. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3179. using type = typename std::conditional < has_key_compare<object_t>::value,
  3180. typename object_t::key_compare, object_comparator_t>::type;
  3181. };
  3182. template<typename BasicJsonType>
  3183. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3184. /////////////////
  3185. // char_traits //
  3186. /////////////////
  3187. // Primary template of char_traits calls std char_traits
  3188. template<typename T>
  3189. struct char_traits : std::char_traits<T>
  3190. {};
  3191. // Explicitly define char traits for unsigned char since it is not standard
  3192. template<>
  3193. struct char_traits<unsigned char> : std::char_traits<char>
  3194. {
  3195. using char_type = unsigned char;
  3196. using int_type = uint64_t;
  3197. // Redefine to_int_type function
  3198. static int_type to_int_type(char_type c) noexcept
  3199. {
  3200. return static_cast<int_type>(c);
  3201. }
  3202. static char_type to_char_type(int_type i) noexcept
  3203. {
  3204. return static_cast<char_type>(i);
  3205. }
  3206. static constexpr int_type eof() noexcept
  3207. {
  3208. return static_cast<int_type>(EOF);
  3209. }
  3210. };
  3211. // Explicitly define char traits for signed char since it is not standard
  3212. template<>
  3213. struct char_traits<signed char> : std::char_traits<char>
  3214. {
  3215. using char_type = signed char;
  3216. using int_type = uint64_t;
  3217. // Redefine to_int_type function
  3218. static int_type to_int_type(char_type c) noexcept
  3219. {
  3220. return static_cast<int_type>(c);
  3221. }
  3222. static char_type to_char_type(int_type i) noexcept
  3223. {
  3224. return static_cast<char_type>(i);
  3225. }
  3226. static constexpr int_type eof() noexcept
  3227. {
  3228. return static_cast<int_type>(EOF);
  3229. }
  3230. };
  3231. ///////////////////
  3232. // is_ functions //
  3233. ///////////////////
  3234. // https://en.cppreference.com/w/cpp/types/conjunction
  3235. template<class...> struct conjunction : std::true_type { };
  3236. template<class B> struct conjunction<B> : B { };
  3237. template<class B, class... Bn>
  3238. struct conjunction<B, Bn...>
  3239. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3240. // https://en.cppreference.com/w/cpp/types/negation
  3241. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3242. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3243. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3244. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3245. template <typename T>
  3246. struct is_default_constructible : std::is_default_constructible<T> {};
  3247. template <typename T1, typename T2>
  3248. struct is_default_constructible<std::pair<T1, T2>>
  3249. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3250. template <typename T1, typename T2>
  3251. struct is_default_constructible<const std::pair<T1, T2>>
  3252. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3253. template <typename... Ts>
  3254. struct is_default_constructible<std::tuple<Ts...>>
  3255. : conjunction<is_default_constructible<Ts>...> {};
  3256. template <typename... Ts>
  3257. struct is_default_constructible<const std::tuple<Ts...>>
  3258. : conjunction<is_default_constructible<Ts>...> {};
  3259. template <typename T, typename... Args>
  3260. struct is_constructible : std::is_constructible<T, Args...> {};
  3261. template <typename T1, typename T2>
  3262. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3263. template <typename T1, typename T2>
  3264. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3265. template <typename... Ts>
  3266. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3267. template <typename... Ts>
  3268. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3269. template<typename T, typename = void>
  3270. struct is_iterator_traits : std::false_type {};
  3271. template<typename T>
  3272. struct is_iterator_traits<iterator_traits<T>>
  3273. {
  3274. private:
  3275. using traits = iterator_traits<T>;
  3276. public:
  3277. static constexpr auto value =
  3278. is_detected<value_type_t, traits>::value &&
  3279. is_detected<difference_type_t, traits>::value &&
  3280. is_detected<pointer_t, traits>::value &&
  3281. is_detected<iterator_category_t, traits>::value &&
  3282. is_detected<reference_t, traits>::value;
  3283. };
  3284. template<typename T>
  3285. struct is_range
  3286. {
  3287. private:
  3288. using t_ref = typename std::add_lvalue_reference<T>::type;
  3289. using iterator = detected_t<result_of_begin, t_ref>;
  3290. using sentinel = detected_t<result_of_end, t_ref>;
  3291. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3292. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3293. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3294. static constexpr auto is_iterator_begin =
  3295. is_iterator_traits<iterator_traits<iterator>>::value;
  3296. public:
  3297. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3298. };
  3299. template<typename R>
  3300. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3301. template<typename T>
  3302. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3303. // The following implementation of is_complete_type is taken from
  3304. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3305. // and is written by Xiang Fan who agreed to using it in this library.
  3306. template<typename T, typename = void>
  3307. struct is_complete_type : std::false_type {};
  3308. template<typename T>
  3309. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3310. template<typename BasicJsonType, typename CompatibleObjectType,
  3311. typename = void>
  3312. struct is_compatible_object_type_impl : std::false_type {};
  3313. template<typename BasicJsonType, typename CompatibleObjectType>
  3314. struct is_compatible_object_type_impl <
  3315. BasicJsonType, CompatibleObjectType,
  3316. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3317. is_detected<key_type_t, CompatibleObjectType>::value >>
  3318. {
  3319. using object_t = typename BasicJsonType::object_t;
  3320. // macOS's is_constructible does not play well with nonesuch...
  3321. static constexpr bool value =
  3322. is_constructible<typename object_t::key_type,
  3323. typename CompatibleObjectType::key_type>::value &&
  3324. is_constructible<typename object_t::mapped_type,
  3325. typename CompatibleObjectType::mapped_type>::value;
  3326. };
  3327. template<typename BasicJsonType, typename CompatibleObjectType>
  3328. struct is_compatible_object_type
  3329. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3330. template<typename BasicJsonType, typename ConstructibleObjectType,
  3331. typename = void>
  3332. struct is_constructible_object_type_impl : std::false_type {};
  3333. template<typename BasicJsonType, typename ConstructibleObjectType>
  3334. struct is_constructible_object_type_impl <
  3335. BasicJsonType, ConstructibleObjectType,
  3336. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3337. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3338. {
  3339. using object_t = typename BasicJsonType::object_t;
  3340. static constexpr bool value =
  3341. (is_default_constructible<ConstructibleObjectType>::value &&
  3342. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3343. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3344. (is_constructible<typename ConstructibleObjectType::key_type,
  3345. typename object_t::key_type>::value &&
  3346. std::is_same <
  3347. typename object_t::mapped_type,
  3348. typename ConstructibleObjectType::mapped_type >::value)) ||
  3349. (has_from_json<BasicJsonType,
  3350. typename ConstructibleObjectType::mapped_type>::value ||
  3351. has_non_default_from_json <
  3352. BasicJsonType,
  3353. typename ConstructibleObjectType::mapped_type >::value);
  3354. };
  3355. template<typename BasicJsonType, typename ConstructibleObjectType>
  3356. struct is_constructible_object_type
  3357. : is_constructible_object_type_impl<BasicJsonType,
  3358. ConstructibleObjectType> {};
  3359. template<typename BasicJsonType, typename CompatibleStringType>
  3360. struct is_compatible_string_type
  3361. {
  3362. static constexpr auto value =
  3363. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3364. };
  3365. template<typename BasicJsonType, typename ConstructibleStringType>
  3366. struct is_constructible_string_type
  3367. {
  3368. // launder type through decltype() to fix compilation failure on ICPC
  3369. #ifdef __INTEL_COMPILER
  3370. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3371. #else
  3372. using laundered_type = ConstructibleStringType;
  3373. #endif
  3374. static constexpr auto value =
  3375. conjunction <
  3376. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3377. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3378. value_type_t, laundered_type >>::value;
  3379. };
  3380. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3381. struct is_compatible_array_type_impl : std::false_type {};
  3382. template<typename BasicJsonType, typename CompatibleArrayType>
  3383. struct is_compatible_array_type_impl <
  3384. BasicJsonType, CompatibleArrayType,
  3385. enable_if_t <
  3386. is_detected<iterator_t, CompatibleArrayType>::value&&
  3387. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3388. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3389. // c.f. https://github.com/nlohmann/json/pull/3073
  3390. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3391. {
  3392. static constexpr bool value =
  3393. is_constructible<BasicJsonType,
  3394. range_value_t<CompatibleArrayType>>::value;
  3395. };
  3396. template<typename BasicJsonType, typename CompatibleArrayType>
  3397. struct is_compatible_array_type
  3398. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3399. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3400. struct is_constructible_array_type_impl : std::false_type {};
  3401. template<typename BasicJsonType, typename ConstructibleArrayType>
  3402. struct is_constructible_array_type_impl <
  3403. BasicJsonType, ConstructibleArrayType,
  3404. enable_if_t<std::is_same<ConstructibleArrayType,
  3405. typename BasicJsonType::value_type>::value >>
  3406. : std::true_type {};
  3407. template<typename BasicJsonType, typename ConstructibleArrayType>
  3408. struct is_constructible_array_type_impl <
  3409. BasicJsonType, ConstructibleArrayType,
  3410. enable_if_t < !std::is_same<ConstructibleArrayType,
  3411. typename BasicJsonType::value_type>::value&&
  3412. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3413. is_default_constructible<ConstructibleArrayType>::value&&
  3414. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3415. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3416. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3417. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3418. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3419. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3420. // c.f. https://github.com/nlohmann/json/pull/3073
  3421. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3422. is_complete_type <
  3423. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3424. {
  3425. using value_type = range_value_t<ConstructibleArrayType>;
  3426. static constexpr bool value =
  3427. std::is_same<value_type,
  3428. typename BasicJsonType::array_t::value_type>::value ||
  3429. has_from_json<BasicJsonType,
  3430. value_type>::value ||
  3431. has_non_default_from_json <
  3432. BasicJsonType,
  3433. value_type >::value;
  3434. };
  3435. template<typename BasicJsonType, typename ConstructibleArrayType>
  3436. struct is_constructible_array_type
  3437. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3438. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3439. typename = void>
  3440. struct is_compatible_integer_type_impl : std::false_type {};
  3441. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3442. struct is_compatible_integer_type_impl <
  3443. RealIntegerType, CompatibleNumberIntegerType,
  3444. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3445. std::is_integral<CompatibleNumberIntegerType>::value&&
  3446. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3447. {
  3448. // is there an assert somewhere on overflows?
  3449. using RealLimits = std::numeric_limits<RealIntegerType>;
  3450. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3451. static constexpr auto value =
  3452. is_constructible<RealIntegerType,
  3453. CompatibleNumberIntegerType>::value &&
  3454. CompatibleLimits::is_integer &&
  3455. RealLimits::is_signed == CompatibleLimits::is_signed;
  3456. };
  3457. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3458. struct is_compatible_integer_type
  3459. : is_compatible_integer_type_impl<RealIntegerType,
  3460. CompatibleNumberIntegerType> {};
  3461. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3462. struct is_compatible_type_impl: std::false_type {};
  3463. template<typename BasicJsonType, typename CompatibleType>
  3464. struct is_compatible_type_impl <
  3465. BasicJsonType, CompatibleType,
  3466. enable_if_t<is_complete_type<CompatibleType>::value >>
  3467. {
  3468. static constexpr bool value =
  3469. has_to_json<BasicJsonType, CompatibleType>::value;
  3470. };
  3471. template<typename BasicJsonType, typename CompatibleType>
  3472. struct is_compatible_type
  3473. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3474. template<typename T1, typename T2>
  3475. struct is_constructible_tuple : std::false_type {};
  3476. template<typename T1, typename... Args>
  3477. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3478. template<typename BasicJsonType, typename T>
  3479. struct is_json_iterator_of : std::false_type {};
  3480. template<typename BasicJsonType>
  3481. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3482. template<typename BasicJsonType>
  3483. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3484. {};
  3485. // checks if a given type T is a template specialization of Primary
  3486. template<template <typename...> class Primary, typename T>
  3487. struct is_specialization_of : std::false_type {};
  3488. template<template <typename...> class Primary, typename... Args>
  3489. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3490. template<typename T>
  3491. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3492. // checks if A and B are comparable using Compare functor
  3493. template<typename Compare, typename A, typename B, typename = void>
  3494. struct is_comparable : std::false_type {};
  3495. template<typename Compare, typename A, typename B>
  3496. struct is_comparable<Compare, A, B, void_t<
  3497. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3498. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3499. >> : std::true_type {};
  3500. template<typename T>
  3501. using detect_is_transparent = typename T::is_transparent;
  3502. // type trait to check if KeyType can be used as object key (without a BasicJsonType)
  3503. // see is_usable_as_basic_json_key_type below
  3504. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3505. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3506. using is_usable_as_key_type = typename std::conditional <
  3507. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3508. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3509. ObjectKeyType>::value)
  3510. && (!RequireTransparentComparator
  3511. || is_detected <detect_is_transparent, Comparator>::value)
  3512. && !is_json_pointer<KeyType>::value,
  3513. std::true_type,
  3514. std::false_type >::type;
  3515. // type trait to check if KeyType can be used as object key
  3516. // true if:
  3517. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3518. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3519. // - the comparator is transparent or RequireTransparentComparator is false
  3520. // - KeyType is not a JSON iterator or json_pointer
  3521. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3522. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3523. using is_usable_as_basic_json_key_type = typename std::conditional <
  3524. is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3525. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3526. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3527. && !is_json_iterator_of<BasicJsonType, KeyType>::value,
  3528. std::true_type,
  3529. std::false_type >::type;
  3530. template<typename ObjectType, typename KeyType>
  3531. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3532. // type trait to check if object_t has an erase() member functions accepting KeyType
  3533. template<typename BasicJsonType, typename KeyType>
  3534. using has_erase_with_key_type = typename std::conditional <
  3535. is_detected <
  3536. detect_erase_with_key_type,
  3537. typename BasicJsonType::object_t, KeyType >::value,
  3538. std::true_type,
  3539. std::false_type >::type;
  3540. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3541. // ordered_map inherits capacity() from std::vector)
  3542. template <typename T>
  3543. struct is_ordered_map
  3544. {
  3545. using one = char;
  3546. struct two
  3547. {
  3548. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3549. };
  3550. template <typename C> static one test( decltype(&C::capacity) ) ;
  3551. template <typename C> static two test(...);
  3552. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3553. };
  3554. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3555. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3556. T conditional_static_cast(U value)
  3557. {
  3558. return static_cast<T>(value);
  3559. }
  3560. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3561. T conditional_static_cast(U value)
  3562. {
  3563. return value;
  3564. }
  3565. template<typename... Types>
  3566. using all_integral = conjunction<std::is_integral<Types>...>;
  3567. template<typename... Types>
  3568. using all_signed = conjunction<std::is_signed<Types>...>;
  3569. template<typename... Types>
  3570. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3571. // there's a disjunction trait in another PR; replace when merged
  3572. template<typename... Types>
  3573. using same_sign = std::integral_constant < bool,
  3574. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3575. template<typename OfType, typename T>
  3576. using never_out_of_range = std::integral_constant < bool,
  3577. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3578. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3579. template<typename OfType, typename T,
  3580. bool OfTypeSigned = std::is_signed<OfType>::value,
  3581. bool TSigned = std::is_signed<T>::value>
  3582. struct value_in_range_of_impl2;
  3583. template<typename OfType, typename T>
  3584. struct value_in_range_of_impl2<OfType, T, false, false>
  3585. {
  3586. static constexpr bool test(T val)
  3587. {
  3588. using CommonType = typename std::common_type<OfType, T>::type;
  3589. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3590. }
  3591. };
  3592. template<typename OfType, typename T>
  3593. struct value_in_range_of_impl2<OfType, T, true, false>
  3594. {
  3595. static constexpr bool test(T val)
  3596. {
  3597. using CommonType = typename std::common_type<OfType, T>::type;
  3598. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3599. }
  3600. };
  3601. template<typename OfType, typename T>
  3602. struct value_in_range_of_impl2<OfType, T, false, true>
  3603. {
  3604. static constexpr bool test(T val)
  3605. {
  3606. using CommonType = typename std::common_type<OfType, T>::type;
  3607. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3608. }
  3609. };
  3610. template<typename OfType, typename T>
  3611. struct value_in_range_of_impl2<OfType, T, true, true>
  3612. {
  3613. static constexpr bool test(T val)
  3614. {
  3615. using CommonType = typename std::common_type<OfType, T>::type;
  3616. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3617. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3618. }
  3619. };
  3620. template<typename OfType, typename T,
  3621. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3622. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3623. struct value_in_range_of_impl1;
  3624. template<typename OfType, typename T>
  3625. struct value_in_range_of_impl1<OfType, T, false>
  3626. {
  3627. static constexpr bool test(T val)
  3628. {
  3629. return value_in_range_of_impl2<OfType, T>::test(val);
  3630. }
  3631. };
  3632. template<typename OfType, typename T>
  3633. struct value_in_range_of_impl1<OfType, T, true>
  3634. {
  3635. static constexpr bool test(T /*val*/)
  3636. {
  3637. return true;
  3638. }
  3639. };
  3640. template<typename OfType, typename T>
  3641. inline constexpr bool value_in_range_of(T val)
  3642. {
  3643. return value_in_range_of_impl1<OfType, T>::test(val);
  3644. }
  3645. template<bool Value>
  3646. using bool_constant = std::integral_constant<bool, Value>;
  3647. ///////////////////////////////////////////////////////////////////////////////
  3648. // is_c_string
  3649. ///////////////////////////////////////////////////////////////////////////////
  3650. namespace impl
  3651. {
  3652. template<typename T>
  3653. inline constexpr bool is_c_string()
  3654. {
  3655. using TUnExt = typename std::remove_extent<T>::type;
  3656. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3657. using TUnPtr = typename std::remove_pointer<T>::type;
  3658. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3659. return
  3660. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3661. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3662. }
  3663. } // namespace impl
  3664. // checks whether T is a [cv] char */[cv] char[] C string
  3665. template<typename T>
  3666. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3667. template<typename T>
  3668. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3669. ///////////////////////////////////////////////////////////////////////////////
  3670. // is_transparent
  3671. ///////////////////////////////////////////////////////////////////////////////
  3672. namespace impl
  3673. {
  3674. template<typename T>
  3675. inline constexpr bool is_transparent()
  3676. {
  3677. return is_detected<detect_is_transparent, T>::value;
  3678. }
  3679. } // namespace impl
  3680. // checks whether T has a member named is_transparent
  3681. template<typename T>
  3682. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3683. ///////////////////////////////////////////////////////////////////////////////
  3684. } // namespace detail
  3685. NLOHMANN_JSON_NAMESPACE_END
  3686. // #include <nlohmann/detail/string_concat.hpp>
  3687. // __ _____ _____ _____
  3688. // __| | __| | | | JSON for Modern C++
  3689. // | | |__ | | | | | | version 3.11.3
  3690. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3691. //
  3692. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  3693. // SPDX-License-Identifier: MIT
  3694. #include <cstring> // strlen
  3695. #include <string> // string
  3696. #include <utility> // forward
  3697. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3698. // #include <nlohmann/detail/meta/detected.hpp>
  3699. NLOHMANN_JSON_NAMESPACE_BEGIN
  3700. namespace detail
  3701. {
  3702. inline std::size_t concat_length()
  3703. {
  3704. return 0;
  3705. }
  3706. template<typename... Args>
  3707. inline std::size_t concat_length(const char* cstr, const Args& ... rest);
  3708. template<typename StringType, typename... Args>
  3709. inline std::size_t concat_length(const StringType& str, const Args& ... rest);
  3710. template<typename... Args>
  3711. inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
  3712. {
  3713. return 1 + concat_length(rest...);
  3714. }
  3715. template<typename... Args>
  3716. inline std::size_t concat_length(const char* cstr, const Args& ... rest)
  3717. {
  3718. // cppcheck-suppress ignoredReturnValue
  3719. return ::strlen(cstr) + concat_length(rest...);
  3720. }
  3721. template<typename StringType, typename... Args>
  3722. inline std::size_t concat_length(const StringType& str, const Args& ... rest)
  3723. {
  3724. return str.size() + concat_length(rest...);
  3725. }
  3726. template<typename OutStringType>
  3727. inline void concat_into(OutStringType& /*out*/)
  3728. {}
  3729. template<typename StringType, typename Arg>
  3730. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3731. template<typename StringType, typename Arg>
  3732. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3733. template<typename StringType, typename Arg>
  3734. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3735. template<typename StringType, typename Arg>
  3736. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3737. template<typename StringType, typename Arg>
  3738. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3739. template<typename StringType, typename Arg>
  3740. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3741. template<typename StringType, typename Arg>
  3742. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3743. template<typename StringType, typename Arg>
  3744. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3745. template < typename OutStringType, typename Arg, typename... Args,
  3746. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3747. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3748. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3749. template < typename OutStringType, typename Arg, typename... Args,
  3750. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3751. && !detect_string_can_append_op<OutStringType, Arg>::value
  3752. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3753. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3754. template < typename OutStringType, typename Arg, typename... Args,
  3755. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3756. && !detect_string_can_append_op<OutStringType, Arg>::value
  3757. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3758. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3759. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3760. template<typename OutStringType, typename Arg, typename... Args,
  3761. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3762. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3763. {
  3764. out.append(std::forward<Arg>(arg));
  3765. concat_into(out, std::forward<Args>(rest)...);
  3766. }
  3767. template < typename OutStringType, typename Arg, typename... Args,
  3768. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3769. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3770. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3771. {
  3772. out += std::forward<Arg>(arg);
  3773. concat_into(out, std::forward<Args>(rest)...);
  3774. }
  3775. template < typename OutStringType, typename Arg, typename... Args,
  3776. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3777. && !detect_string_can_append_op<OutStringType, Arg>::value
  3778. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3779. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3780. {
  3781. out.append(arg.begin(), arg.end());
  3782. concat_into(out, std::forward<Args>(rest)...);
  3783. }
  3784. template < typename OutStringType, typename Arg, typename... Args,
  3785. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3786. && !detect_string_can_append_op<OutStringType, Arg>::value
  3787. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3788. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3789. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3790. {
  3791. out.append(arg.data(), arg.size());
  3792. concat_into(out, std::forward<Args>(rest)...);
  3793. }
  3794. template<typename OutStringType = std::string, typename... Args>
  3795. inline OutStringType concat(Args && ... args)
  3796. {
  3797. OutStringType str;
  3798. str.reserve(concat_length(args...));
  3799. concat_into(str, std::forward<Args>(args)...);
  3800. return str;
  3801. }
  3802. } // namespace detail
  3803. NLOHMANN_JSON_NAMESPACE_END
  3804. NLOHMANN_JSON_NAMESPACE_BEGIN
  3805. namespace detail
  3806. {
  3807. ////////////////
  3808. // exceptions //
  3809. ////////////////
  3810. /// @brief general exception of the @ref basic_json class
  3811. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3812. class exception : public std::exception
  3813. {
  3814. public:
  3815. /// returns the explanatory string
  3816. const char* what() const noexcept override
  3817. {
  3818. return m.what();
  3819. }
  3820. /// the id of the exception
  3821. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3822. protected:
  3823. JSON_HEDLEY_NON_NULL(3)
  3824. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3825. static std::string name(const std::string& ename, int id_)
  3826. {
  3827. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3828. }
  3829. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3830. {
  3831. return "";
  3832. }
  3833. template<typename BasicJsonType>
  3834. static std::string diagnostics(const BasicJsonType* leaf_element)
  3835. {
  3836. #if JSON_DIAGNOSTICS
  3837. std::vector<std::string> tokens;
  3838. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3839. {
  3840. switch (current->m_parent->type())
  3841. {
  3842. case value_t::array:
  3843. {
  3844. for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
  3845. {
  3846. if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
  3847. {
  3848. tokens.emplace_back(std::to_string(i));
  3849. break;
  3850. }
  3851. }
  3852. break;
  3853. }
  3854. case value_t::object:
  3855. {
  3856. for (const auto& element : *current->m_parent->m_data.m_value.object)
  3857. {
  3858. if (&element.second == current)
  3859. {
  3860. tokens.emplace_back(element.first.c_str());
  3861. break;
  3862. }
  3863. }
  3864. break;
  3865. }
  3866. case value_t::null: // LCOV_EXCL_LINE
  3867. case value_t::string: // LCOV_EXCL_LINE
  3868. case value_t::boolean: // LCOV_EXCL_LINE
  3869. case value_t::number_integer: // LCOV_EXCL_LINE
  3870. case value_t::number_unsigned: // LCOV_EXCL_LINE
  3871. case value_t::number_float: // LCOV_EXCL_LINE
  3872. case value_t::binary: // LCOV_EXCL_LINE
  3873. case value_t::discarded: // LCOV_EXCL_LINE
  3874. default: // LCOV_EXCL_LINE
  3875. break; // LCOV_EXCL_LINE
  3876. }
  3877. }
  3878. if (tokens.empty())
  3879. {
  3880. return "";
  3881. }
  3882. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  3883. [](const std::string & a, const std::string & b)
  3884. {
  3885. return concat(a, '/', detail::escape(b));
  3886. });
  3887. return concat('(', str, ") ");
  3888. #else
  3889. static_cast<void>(leaf_element);
  3890. return "";
  3891. #endif
  3892. }
  3893. private:
  3894. /// an exception object as storage for error messages
  3895. std::runtime_error m;
  3896. };
  3897. /// @brief exception indicating a parse error
  3898. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  3899. class parse_error : public exception
  3900. {
  3901. public:
  3902. /*!
  3903. @brief create a parse error exception
  3904. @param[in] id_ the id of the exception
  3905. @param[in] pos the position where the error occurred (or with
  3906. chars_read_total=0 if the position cannot be
  3907. determined)
  3908. @param[in] what_arg the explanatory string
  3909. @return parse_error object
  3910. */
  3911. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3912. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  3913. {
  3914. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3915. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  3916. return {id_, pos.chars_read_total, w.c_str()};
  3917. }
  3918. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3919. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  3920. {
  3921. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3922. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  3923. ": ", exception::diagnostics(context), what_arg);
  3924. return {id_, byte_, w.c_str()};
  3925. }
  3926. /*!
  3927. @brief byte index of the parse error
  3928. The byte index of the last read character in the input file.
  3929. @note For an input with n bytes, 1 is the index of the first character and
  3930. n+1 is the index of the terminating null byte or the end of file.
  3931. This also holds true when reading a byte vector (CBOR or MessagePack).
  3932. */
  3933. const std::size_t byte;
  3934. private:
  3935. parse_error(int id_, std::size_t byte_, const char* what_arg)
  3936. : exception(id_, what_arg), byte(byte_) {}
  3937. static std::string position_string(const position_t& pos)
  3938. {
  3939. return concat(" at line ", std::to_string(pos.lines_read + 1),
  3940. ", column ", std::to_string(pos.chars_read_current_line));
  3941. }
  3942. };
  3943. /// @brief exception indicating errors with iterators
  3944. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  3945. class invalid_iterator : public exception
  3946. {
  3947. public:
  3948. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3949. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  3950. {
  3951. const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  3952. return {id_, w.c_str()};
  3953. }
  3954. private:
  3955. JSON_HEDLEY_NON_NULL(3)
  3956. invalid_iterator(int id_, const char* what_arg)
  3957. : exception(id_, what_arg) {}
  3958. };
  3959. /// @brief exception indicating executing a member function with a wrong type
  3960. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  3961. class type_error : public exception
  3962. {
  3963. public:
  3964. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3965. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3966. {
  3967. const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  3968. return {id_, w.c_str()};
  3969. }
  3970. private:
  3971. JSON_HEDLEY_NON_NULL(3)
  3972. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3973. };
  3974. /// @brief exception indicating access out of the defined range
  3975. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  3976. class out_of_range : public exception
  3977. {
  3978. public:
  3979. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3980. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  3981. {
  3982. const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  3983. return {id_, w.c_str()};
  3984. }
  3985. private:
  3986. JSON_HEDLEY_NON_NULL(3)
  3987. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3988. };
  3989. /// @brief exception indicating other library errors
  3990. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  3991. class other_error : public exception
  3992. {
  3993. public:
  3994. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3995. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3996. {
  3997. const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  3998. return {id_, w.c_str()};
  3999. }
  4000. private:
  4001. JSON_HEDLEY_NON_NULL(3)
  4002. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  4003. };
  4004. } // namespace detail
  4005. NLOHMANN_JSON_NAMESPACE_END
  4006. // #include <nlohmann/detail/macro_scope.hpp>
  4007. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4008. // #include <nlohmann/detail/meta/identity_tag.hpp>
  4009. // __ _____ _____ _____
  4010. // __| | __| | | | JSON for Modern C++
  4011. // | | |__ | | | | | | version 3.11.3
  4012. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4013. //
  4014. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4015. // SPDX-License-Identifier: MIT
  4016. // #include <nlohmann/detail/abi_macros.hpp>
  4017. NLOHMANN_JSON_NAMESPACE_BEGIN
  4018. namespace detail
  4019. {
  4020. // dispatching helper struct
  4021. template <class T> struct identity_tag {};
  4022. } // namespace detail
  4023. NLOHMANN_JSON_NAMESPACE_END
  4024. // #include <nlohmann/detail/meta/std_fs.hpp>
  4025. // __ _____ _____ _____
  4026. // __| | __| | | | JSON for Modern C++
  4027. // | | |__ | | | | | | version 3.11.3
  4028. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4029. //
  4030. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4031. // SPDX-License-Identifier: MIT
  4032. // #include <nlohmann/detail/macro_scope.hpp>
  4033. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4034. #include <experimental/filesystem>
  4035. NLOHMANN_JSON_NAMESPACE_BEGIN
  4036. namespace detail
  4037. {
  4038. namespace std_fs = std::experimental::filesystem;
  4039. } // namespace detail
  4040. NLOHMANN_JSON_NAMESPACE_END
  4041. #elif JSON_HAS_FILESYSTEM
  4042. #include <filesystem>
  4043. NLOHMANN_JSON_NAMESPACE_BEGIN
  4044. namespace detail
  4045. {
  4046. namespace std_fs = std::filesystem;
  4047. } // namespace detail
  4048. NLOHMANN_JSON_NAMESPACE_END
  4049. #endif
  4050. // #include <nlohmann/detail/meta/type_traits.hpp>
  4051. // #include <nlohmann/detail/string_concat.hpp>
  4052. // #include <nlohmann/detail/value_t.hpp>
  4053. NLOHMANN_JSON_NAMESPACE_BEGIN
  4054. namespace detail
  4055. {
  4056. template<typename BasicJsonType>
  4057. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  4058. {
  4059. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  4060. {
  4061. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  4062. }
  4063. n = nullptr;
  4064. }
  4065. // overloads for basic_json template parameters
  4066. template < typename BasicJsonType, typename ArithmeticType,
  4067. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4068. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4069. int > = 0 >
  4070. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4071. {
  4072. switch (static_cast<value_t>(j))
  4073. {
  4074. case value_t::number_unsigned:
  4075. {
  4076. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4077. break;
  4078. }
  4079. case value_t::number_integer:
  4080. {
  4081. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4082. break;
  4083. }
  4084. case value_t::number_float:
  4085. {
  4086. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4087. break;
  4088. }
  4089. case value_t::null:
  4090. case value_t::object:
  4091. case value_t::array:
  4092. case value_t::string:
  4093. case value_t::boolean:
  4094. case value_t::binary:
  4095. case value_t::discarded:
  4096. default:
  4097. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4098. }
  4099. }
  4100. template<typename BasicJsonType>
  4101. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4102. {
  4103. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4104. {
  4105. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4106. }
  4107. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4108. }
  4109. template<typename BasicJsonType>
  4110. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4111. {
  4112. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4113. {
  4114. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4115. }
  4116. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4117. }
  4118. template <
  4119. typename BasicJsonType, typename StringType,
  4120. enable_if_t <
  4121. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4122. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4123. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4124. && !is_json_ref<StringType>::value, int > = 0 >
  4125. inline void from_json(const BasicJsonType& j, StringType& s)
  4126. {
  4127. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4128. {
  4129. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4130. }
  4131. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4132. }
  4133. template<typename BasicJsonType>
  4134. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4135. {
  4136. get_arithmetic_value(j, val);
  4137. }
  4138. template<typename BasicJsonType>
  4139. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4140. {
  4141. get_arithmetic_value(j, val);
  4142. }
  4143. template<typename BasicJsonType>
  4144. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4145. {
  4146. get_arithmetic_value(j, val);
  4147. }
  4148. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4149. template<typename BasicJsonType, typename EnumType,
  4150. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4151. inline void from_json(const BasicJsonType& j, EnumType& e)
  4152. {
  4153. typename std::underlying_type<EnumType>::type val;
  4154. get_arithmetic_value(j, val);
  4155. e = static_cast<EnumType>(val);
  4156. }
  4157. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4158. // forward_list doesn't have an insert method
  4159. template<typename BasicJsonType, typename T, typename Allocator,
  4160. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4161. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4162. {
  4163. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4164. {
  4165. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4166. }
  4167. l.clear();
  4168. std::transform(j.rbegin(), j.rend(),
  4169. std::front_inserter(l), [](const BasicJsonType & i)
  4170. {
  4171. return i.template get<T>();
  4172. });
  4173. }
  4174. // valarray doesn't have an insert method
  4175. template<typename BasicJsonType, typename T,
  4176. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4177. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4178. {
  4179. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4180. {
  4181. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4182. }
  4183. l.resize(j.size());
  4184. std::transform(j.begin(), j.end(), std::begin(l),
  4185. [](const BasicJsonType & elem)
  4186. {
  4187. return elem.template get<T>();
  4188. });
  4189. }
  4190. template<typename BasicJsonType, typename T, std::size_t N>
  4191. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4192. -> decltype(j.template get<T>(), void())
  4193. {
  4194. for (std::size_t i = 0; i < N; ++i)
  4195. {
  4196. arr[i] = j.at(i).template get<T>();
  4197. }
  4198. }
  4199. template<typename BasicJsonType>
  4200. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4201. {
  4202. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4203. }
  4204. template<typename BasicJsonType, typename T, std::size_t N>
  4205. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4206. priority_tag<2> /*unused*/)
  4207. -> decltype(j.template get<T>(), void())
  4208. {
  4209. for (std::size_t i = 0; i < N; ++i)
  4210. {
  4211. arr[i] = j.at(i).template get<T>();
  4212. }
  4213. }
  4214. template<typename BasicJsonType, typename ConstructibleArrayType,
  4215. enable_if_t<
  4216. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4217. int> = 0>
  4218. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4219. -> decltype(
  4220. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4221. j.template get<typename ConstructibleArrayType::value_type>(),
  4222. void())
  4223. {
  4224. using std::end;
  4225. ConstructibleArrayType ret;
  4226. ret.reserve(j.size());
  4227. std::transform(j.begin(), j.end(),
  4228. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4229. {
  4230. // get<BasicJsonType>() returns *this, this won't call a from_json
  4231. // method when value_type is BasicJsonType
  4232. return i.template get<typename ConstructibleArrayType::value_type>();
  4233. });
  4234. arr = std::move(ret);
  4235. }
  4236. template<typename BasicJsonType, typename ConstructibleArrayType,
  4237. enable_if_t<
  4238. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4239. int> = 0>
  4240. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4241. priority_tag<0> /*unused*/)
  4242. {
  4243. using std::end;
  4244. ConstructibleArrayType ret;
  4245. std::transform(
  4246. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4247. [](const BasicJsonType & i)
  4248. {
  4249. // get<BasicJsonType>() returns *this, this won't call a from_json
  4250. // method when value_type is BasicJsonType
  4251. return i.template get<typename ConstructibleArrayType::value_type>();
  4252. });
  4253. arr = std::move(ret);
  4254. }
  4255. template < typename BasicJsonType, typename ConstructibleArrayType,
  4256. enable_if_t <
  4257. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4258. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4259. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4260. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4261. !is_basic_json<ConstructibleArrayType>::value,
  4262. int > = 0 >
  4263. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4264. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4265. j.template get<typename ConstructibleArrayType::value_type>(),
  4266. void())
  4267. {
  4268. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4269. {
  4270. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4271. }
  4272. from_json_array_impl(j, arr, priority_tag<3> {});
  4273. }
  4274. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4275. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4276. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4277. {
  4278. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4279. }
  4280. template < typename BasicJsonType, typename T, std::size_t N >
  4281. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4282. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4283. {
  4284. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4285. {
  4286. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4287. }
  4288. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4289. }
  4290. template<typename BasicJsonType>
  4291. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4292. {
  4293. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4294. {
  4295. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4296. }
  4297. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4298. }
  4299. template<typename BasicJsonType, typename ConstructibleObjectType,
  4300. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4301. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4302. {
  4303. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4304. {
  4305. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4306. }
  4307. ConstructibleObjectType ret;
  4308. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4309. using value_type = typename ConstructibleObjectType::value_type;
  4310. std::transform(
  4311. inner_object->begin(), inner_object->end(),
  4312. std::inserter(ret, ret.begin()),
  4313. [](typename BasicJsonType::object_t::value_type const & p)
  4314. {
  4315. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4316. });
  4317. obj = std::move(ret);
  4318. }
  4319. // overload for arithmetic types, not chosen for basic_json template arguments
  4320. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4321. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4322. // an arithmetic type?
  4323. template < typename BasicJsonType, typename ArithmeticType,
  4324. enable_if_t <
  4325. std::is_arithmetic<ArithmeticType>::value&&
  4326. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4327. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4328. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4329. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4330. int > = 0 >
  4331. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4332. {
  4333. switch (static_cast<value_t>(j))
  4334. {
  4335. case value_t::number_unsigned:
  4336. {
  4337. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4338. break;
  4339. }
  4340. case value_t::number_integer:
  4341. {
  4342. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4343. break;
  4344. }
  4345. case value_t::number_float:
  4346. {
  4347. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4348. break;
  4349. }
  4350. case value_t::boolean:
  4351. {
  4352. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4353. break;
  4354. }
  4355. case value_t::null:
  4356. case value_t::object:
  4357. case value_t::array:
  4358. case value_t::string:
  4359. case value_t::binary:
  4360. case value_t::discarded:
  4361. default:
  4362. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4363. }
  4364. }
  4365. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4366. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4367. {
  4368. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4369. }
  4370. template < typename BasicJsonType, class A1, class A2 >
  4371. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4372. {
  4373. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4374. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4375. }
  4376. template<typename BasicJsonType, typename A1, typename A2>
  4377. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4378. {
  4379. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4380. }
  4381. template<typename BasicJsonType, typename... Args>
  4382. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4383. {
  4384. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4385. }
  4386. template<typename BasicJsonType, typename... Args>
  4387. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4388. {
  4389. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4390. }
  4391. template<typename BasicJsonType, typename TupleRelated>
  4392. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4393. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4394. {
  4395. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4396. {
  4397. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4398. }
  4399. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4400. }
  4401. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4402. typename = enable_if_t < !std::is_constructible <
  4403. typename BasicJsonType::string_t, Key >::value >>
  4404. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  4405. {
  4406. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4407. {
  4408. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4409. }
  4410. m.clear();
  4411. for (const auto& p : j)
  4412. {
  4413. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4414. {
  4415. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4416. }
  4417. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4418. }
  4419. }
  4420. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4421. typename = enable_if_t < !std::is_constructible <
  4422. typename BasicJsonType::string_t, Key >::value >>
  4423. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4424. {
  4425. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4426. {
  4427. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4428. }
  4429. m.clear();
  4430. for (const auto& p : j)
  4431. {
  4432. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4433. {
  4434. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4435. }
  4436. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4437. }
  4438. }
  4439. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4440. template<typename BasicJsonType>
  4441. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4442. {
  4443. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4444. {
  4445. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4446. }
  4447. p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4448. }
  4449. #endif
  4450. struct from_json_fn
  4451. {
  4452. template<typename BasicJsonType, typename T>
  4453. auto operator()(const BasicJsonType& j, T&& val) const
  4454. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4455. -> decltype(from_json(j, std::forward<T>(val)))
  4456. {
  4457. return from_json(j, std::forward<T>(val));
  4458. }
  4459. };
  4460. } // namespace detail
  4461. #ifndef JSON_HAS_CPP_17
  4462. /// namespace to hold default `from_json` function
  4463. /// to see why this is required:
  4464. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4465. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4466. {
  4467. #endif
  4468. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4469. detail::static_const<detail::from_json_fn>::value;
  4470. #ifndef JSON_HAS_CPP_17
  4471. } // namespace
  4472. #endif
  4473. NLOHMANN_JSON_NAMESPACE_END
  4474. // #include <nlohmann/detail/conversions/to_json.hpp>
  4475. // __ _____ _____ _____
  4476. // __| | __| | | | JSON for Modern C++
  4477. // | | |__ | | | | | | version 3.11.3
  4478. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4479. //
  4480. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4481. // SPDX-License-Identifier: MIT
  4482. #include <algorithm> // copy
  4483. #include <iterator> // begin, end
  4484. #include <string> // string
  4485. #include <tuple> // tuple, get
  4486. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4487. #include <utility> // move, forward, declval, pair
  4488. #include <valarray> // valarray
  4489. #include <vector> // vector
  4490. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4491. // __ _____ _____ _____
  4492. // __| | __| | | | JSON for Modern C++
  4493. // | | |__ | | | | | | version 3.11.3
  4494. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4495. //
  4496. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  4497. // SPDX-License-Identifier: MIT
  4498. #include <cstddef> // size_t
  4499. #include <iterator> // input_iterator_tag
  4500. #include <string> // string, to_string
  4501. #include <tuple> // tuple_size, get, tuple_element
  4502. #include <utility> // move
  4503. #if JSON_HAS_RANGES
  4504. #include <ranges> // enable_borrowed_range
  4505. #endif
  4506. // #include <nlohmann/detail/abi_macros.hpp>
  4507. // #include <nlohmann/detail/meta/type_traits.hpp>
  4508. // #include <nlohmann/detail/value_t.hpp>
  4509. NLOHMANN_JSON_NAMESPACE_BEGIN
  4510. namespace detail
  4511. {
  4512. template<typename string_type>
  4513. void int_to_string( string_type& target, std::size_t value )
  4514. {
  4515. // For ADL
  4516. using std::to_string;
  4517. target = to_string(value);
  4518. }
  4519. template<typename IteratorType> class iteration_proxy_value
  4520. {
  4521. public:
  4522. using difference_type = std::ptrdiff_t;
  4523. using value_type = iteration_proxy_value;
  4524. using pointer = value_type *;
  4525. using reference = value_type &;
  4526. using iterator_category = std::input_iterator_tag;
  4527. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4528. private:
  4529. /// the iterator
  4530. IteratorType anchor{};
  4531. /// an index for arrays (used to create key names)
  4532. std::size_t array_index = 0;
  4533. /// last stringified array index
  4534. mutable std::size_t array_index_last = 0;
  4535. /// a string representation of the array index
  4536. mutable string_type array_index_str = "0";
  4537. /// an empty string (to return a reference for primitive values)
  4538. string_type empty_str{};
  4539. public:
  4540. explicit iteration_proxy_value() = default;
  4541. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4542. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4543. && std::is_nothrow_default_constructible<string_type>::value)
  4544. : anchor(std::move(it))
  4545. , array_index(array_index_)
  4546. {}
  4547. iteration_proxy_value(iteration_proxy_value const&) = default;
  4548. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4549. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4550. iteration_proxy_value(iteration_proxy_value&&)
  4551. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4552. && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4553. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4554. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4555. && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4556. ~iteration_proxy_value() = default;
  4557. /// dereference operator (needed for range-based for)
  4558. const iteration_proxy_value& operator*() const
  4559. {
  4560. return *this;
  4561. }
  4562. /// increment operator (needed for range-based for)
  4563. iteration_proxy_value& operator++()
  4564. {
  4565. ++anchor;
  4566. ++array_index;
  4567. return *this;
  4568. }
  4569. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4570. {
  4571. auto tmp = iteration_proxy_value(anchor, array_index);
  4572. ++anchor;
  4573. ++array_index;
  4574. return tmp;
  4575. }
  4576. /// equality operator (needed for InputIterator)
  4577. bool operator==(const iteration_proxy_value& o) const
  4578. {
  4579. return anchor == o.anchor;
  4580. }
  4581. /// inequality operator (needed for range-based for)
  4582. bool operator!=(const iteration_proxy_value& o) const
  4583. {
  4584. return anchor != o.anchor;
  4585. }
  4586. /// return key of the iterator
  4587. const string_type& key() const
  4588. {
  4589. JSON_ASSERT(anchor.m_object != nullptr);
  4590. switch (anchor.m_object->type())
  4591. {
  4592. // use integer array index as key
  4593. case value_t::array:
  4594. {
  4595. if (array_index != array_index_last)
  4596. {
  4597. int_to_string( array_index_str, array_index );
  4598. array_index_last = array_index;
  4599. }
  4600. return array_index_str;
  4601. }
  4602. // use key from the object
  4603. case value_t::object:
  4604. return anchor.key();
  4605. // use an empty key for all primitive types
  4606. case value_t::null:
  4607. case value_t::string:
  4608. case value_t::boolean:
  4609. case value_t::number_integer:
  4610. case value_t::number_unsigned:
  4611. case value_t::number_float:
  4612. case value_t::binary:
  4613. case value_t::discarded:
  4614. default:
  4615. return empty_str;
  4616. }
  4617. }
  4618. /// return value of the iterator
  4619. typename IteratorType::reference value() const
  4620. {
  4621. return anchor.value();
  4622. }
  4623. };
  4624. /// proxy class for the items() function
  4625. template<typename IteratorType> class iteration_proxy
  4626. {
  4627. private:
  4628. /// the container to iterate
  4629. typename IteratorType::pointer container = nullptr;
  4630. public:
  4631. explicit iteration_proxy() = default;
  4632. /// construct iteration proxy from a container
  4633. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4634. : container(&cont) {}
  4635. iteration_proxy(iteration_proxy const&) = default;
  4636. iteration_proxy& operator=(iteration_proxy const&) = default;
  4637. iteration_proxy(iteration_proxy&&) noexcept = default;
  4638. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4639. ~iteration_proxy() = default;
  4640. /// return iterator begin (needed for range-based for)
  4641. iteration_proxy_value<IteratorType> begin() const noexcept
  4642. {
  4643. return iteration_proxy_value<IteratorType>(container->begin());
  4644. }
  4645. /// return iterator end (needed for range-based for)
  4646. iteration_proxy_value<IteratorType> end() const noexcept
  4647. {
  4648. return iteration_proxy_value<IteratorType>(container->end());
  4649. }
  4650. };
  4651. // Structured Bindings Support
  4652. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4653. // And see https://github.com/nlohmann/json/pull/1391
  4654. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4655. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4656. {
  4657. return i.key();
  4658. }
  4659. // Structured Bindings Support
  4660. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4661. // And see https://github.com/nlohmann/json/pull/1391
  4662. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4663. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4664. {
  4665. return i.value();
  4666. }
  4667. } // namespace detail
  4668. NLOHMANN_JSON_NAMESPACE_END
  4669. // The Addition to the STD Namespace is required to add
  4670. // Structured Bindings Support to the iteration_proxy_value class
  4671. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4672. // And see https://github.com/nlohmann/json/pull/1391
  4673. namespace std
  4674. {
  4675. #if defined(__clang__)
  4676. // Fix: https://github.com/nlohmann/json/issues/1401
  4677. #pragma clang diagnostic push
  4678. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4679. #endif
  4680. template<typename IteratorType>
  4681. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  4682. : public std::integral_constant<std::size_t, 2> {};
  4683. template<std::size_t N, typename IteratorType>
  4684. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  4685. {
  4686. public:
  4687. using type = decltype(
  4688. get<N>(std::declval <
  4689. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4690. };
  4691. #if defined(__clang__)
  4692. #pragma clang diagnostic pop
  4693. #endif
  4694. } // namespace std
  4695. #if JSON_HAS_RANGES
  4696. template <typename IteratorType>
  4697. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4698. #endif
  4699. // #include <nlohmann/detail/macro_scope.hpp>
  4700. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4701. // #include <nlohmann/detail/meta/std_fs.hpp>
  4702. // #include <nlohmann/detail/meta/type_traits.hpp>
  4703. // #include <nlohmann/detail/value_t.hpp>
  4704. NLOHMANN_JSON_NAMESPACE_BEGIN
  4705. namespace detail
  4706. {
  4707. //////////////////
  4708. // constructors //
  4709. //////////////////
  4710. /*
  4711. * Note all external_constructor<>::construct functions need to call
  4712. * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
  4713. * allocated value (e.g., a string). See bug issue
  4714. * https://github.com/nlohmann/json/issues/2865 for more information.
  4715. */
  4716. template<value_t> struct external_constructor;
  4717. template<>
  4718. struct external_constructor<value_t::boolean>
  4719. {
  4720. template<typename BasicJsonType>
  4721. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4722. {
  4723. j.m_data.m_value.destroy(j.m_data.m_type);
  4724. j.m_data.m_type = value_t::boolean;
  4725. j.m_data.m_value = b;
  4726. j.assert_invariant();
  4727. }
  4728. };
  4729. template<>
  4730. struct external_constructor<value_t::string>
  4731. {
  4732. template<typename BasicJsonType>
  4733. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4734. {
  4735. j.m_data.m_value.destroy(j.m_data.m_type);
  4736. j.m_data.m_type = value_t::string;
  4737. j.m_data.m_value = s;
  4738. j.assert_invariant();
  4739. }
  4740. template<typename BasicJsonType>
  4741. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4742. {
  4743. j.m_data.m_value.destroy(j.m_data.m_type);
  4744. j.m_data.m_type = value_t::string;
  4745. j.m_data.m_value = std::move(s);
  4746. j.assert_invariant();
  4747. }
  4748. template < typename BasicJsonType, typename CompatibleStringType,
  4749. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4750. int > = 0 >
  4751. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4752. {
  4753. j.m_data.m_value.destroy(j.m_data.m_type);
  4754. j.m_data.m_type = value_t::string;
  4755. j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4756. j.assert_invariant();
  4757. }
  4758. };
  4759. template<>
  4760. struct external_constructor<value_t::binary>
  4761. {
  4762. template<typename BasicJsonType>
  4763. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4764. {
  4765. j.m_data.m_value.destroy(j.m_data.m_type);
  4766. j.m_data.m_type = value_t::binary;
  4767. j.m_data.m_value = typename BasicJsonType::binary_t(b);
  4768. j.assert_invariant();
  4769. }
  4770. template<typename BasicJsonType>
  4771. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4772. {
  4773. j.m_data.m_value.destroy(j.m_data.m_type);
  4774. j.m_data.m_type = value_t::binary;
  4775. j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
  4776. j.assert_invariant();
  4777. }
  4778. };
  4779. template<>
  4780. struct external_constructor<value_t::number_float>
  4781. {
  4782. template<typename BasicJsonType>
  4783. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4784. {
  4785. j.m_data.m_value.destroy(j.m_data.m_type);
  4786. j.m_data.m_type = value_t::number_float;
  4787. j.m_data.m_value = val;
  4788. j.assert_invariant();
  4789. }
  4790. };
  4791. template<>
  4792. struct external_constructor<value_t::number_unsigned>
  4793. {
  4794. template<typename BasicJsonType>
  4795. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4796. {
  4797. j.m_data.m_value.destroy(j.m_data.m_type);
  4798. j.m_data.m_type = value_t::number_unsigned;
  4799. j.m_data.m_value = val;
  4800. j.assert_invariant();
  4801. }
  4802. };
  4803. template<>
  4804. struct external_constructor<value_t::number_integer>
  4805. {
  4806. template<typename BasicJsonType>
  4807. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4808. {
  4809. j.m_data.m_value.destroy(j.m_data.m_type);
  4810. j.m_data.m_type = value_t::number_integer;
  4811. j.m_data.m_value = val;
  4812. j.assert_invariant();
  4813. }
  4814. };
  4815. template<>
  4816. struct external_constructor<value_t::array>
  4817. {
  4818. template<typename BasicJsonType>
  4819. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4820. {
  4821. j.m_data.m_value.destroy(j.m_data.m_type);
  4822. j.m_data.m_type = value_t::array;
  4823. j.m_data.m_value = arr;
  4824. j.set_parents();
  4825. j.assert_invariant();
  4826. }
  4827. template<typename BasicJsonType>
  4828. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4829. {
  4830. j.m_data.m_value.destroy(j.m_data.m_type);
  4831. j.m_data.m_type = value_t::array;
  4832. j.m_data.m_value = std::move(arr);
  4833. j.set_parents();
  4834. j.assert_invariant();
  4835. }
  4836. template < typename BasicJsonType, typename CompatibleArrayType,
  4837. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4838. int > = 0 >
  4839. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4840. {
  4841. using std::begin;
  4842. using std::end;
  4843. j.m_data.m_value.destroy(j.m_data.m_type);
  4844. j.m_data.m_type = value_t::array;
  4845. j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4846. j.set_parents();
  4847. j.assert_invariant();
  4848. }
  4849. template<typename BasicJsonType>
  4850. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4851. {
  4852. j.m_data.m_value.destroy(j.m_data.m_type);
  4853. j.m_data.m_type = value_t::array;
  4854. j.m_data.m_value = value_t::array;
  4855. j.m_data.m_value.array->reserve(arr.size());
  4856. for (const bool x : arr)
  4857. {
  4858. j.m_data.m_value.array->push_back(x);
  4859. j.set_parent(j.m_data.m_value.array->back());
  4860. }
  4861. j.assert_invariant();
  4862. }
  4863. template<typename BasicJsonType, typename T,
  4864. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4865. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4866. {
  4867. j.m_data.m_value.destroy(j.m_data.m_type);
  4868. j.m_data.m_type = value_t::array;
  4869. j.m_data.m_value = value_t::array;
  4870. j.m_data.m_value.array->resize(arr.size());
  4871. if (arr.size() > 0)
  4872. {
  4873. std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
  4874. }
  4875. j.set_parents();
  4876. j.assert_invariant();
  4877. }
  4878. };
  4879. template<>
  4880. struct external_constructor<value_t::object>
  4881. {
  4882. template<typename BasicJsonType>
  4883. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4884. {
  4885. j.m_data.m_value.destroy(j.m_data.m_type);
  4886. j.m_data.m_type = value_t::object;
  4887. j.m_data.m_value = obj;
  4888. j.set_parents();
  4889. j.assert_invariant();
  4890. }
  4891. template<typename BasicJsonType>
  4892. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4893. {
  4894. j.m_data.m_value.destroy(j.m_data.m_type);
  4895. j.m_data.m_type = value_t::object;
  4896. j.m_data.m_value = std::move(obj);
  4897. j.set_parents();
  4898. j.assert_invariant();
  4899. }
  4900. template < typename BasicJsonType, typename CompatibleObjectType,
  4901. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4902. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4903. {
  4904. using std::begin;
  4905. using std::end;
  4906. j.m_data.m_value.destroy(j.m_data.m_type);
  4907. j.m_data.m_type = value_t::object;
  4908. j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4909. j.set_parents();
  4910. j.assert_invariant();
  4911. }
  4912. };
  4913. /////////////
  4914. // to_json //
  4915. /////////////
  4916. template<typename BasicJsonType, typename T,
  4917. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4918. inline void to_json(BasicJsonType& j, T b) noexcept
  4919. {
  4920. external_constructor<value_t::boolean>::construct(j, b);
  4921. }
  4922. template < typename BasicJsonType, typename BoolRef,
  4923. enable_if_t <
  4924. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  4925. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  4926. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  4927. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  4928. typename BasicJsonType::boolean_t >::value))
  4929. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  4930. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  4931. {
  4932. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  4933. }
  4934. template<typename BasicJsonType, typename CompatibleString,
  4935. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4936. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  4937. {
  4938. external_constructor<value_t::string>::construct(j, s);
  4939. }
  4940. template<typename BasicJsonType>
  4941. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4942. {
  4943. external_constructor<value_t::string>::construct(j, std::move(s));
  4944. }
  4945. template<typename BasicJsonType, typename FloatType,
  4946. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4947. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  4948. {
  4949. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4950. }
  4951. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4952. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4953. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4954. {
  4955. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4956. }
  4957. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4958. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4959. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4960. {
  4961. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4962. }
  4963. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4964. template<typename BasicJsonType, typename EnumType,
  4965. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4966. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  4967. {
  4968. using underlying_type = typename std::underlying_type<EnumType>::type;
  4969. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  4970. }
  4971. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4972. template<typename BasicJsonType>
  4973. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4974. {
  4975. external_constructor<value_t::array>::construct(j, e);
  4976. }
  4977. template < typename BasicJsonType, typename CompatibleArrayType,
  4978. enable_if_t < is_compatible_array_type<BasicJsonType,
  4979. CompatibleArrayType>::value&&
  4980. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4981. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4982. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4983. !is_basic_json<CompatibleArrayType>::value,
  4984. int > = 0 >
  4985. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4986. {
  4987. external_constructor<value_t::array>::construct(j, arr);
  4988. }
  4989. template<typename BasicJsonType>
  4990. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4991. {
  4992. external_constructor<value_t::binary>::construct(j, bin);
  4993. }
  4994. template<typename BasicJsonType, typename T,
  4995. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4996. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4997. {
  4998. external_constructor<value_t::array>::construct(j, std::move(arr));
  4999. }
  5000. template<typename BasicJsonType>
  5001. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  5002. {
  5003. external_constructor<value_t::array>::construct(j, std::move(arr));
  5004. }
  5005. template < typename BasicJsonType, typename CompatibleObjectType,
  5006. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  5007. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  5008. {
  5009. external_constructor<value_t::object>::construct(j, obj);
  5010. }
  5011. template<typename BasicJsonType>
  5012. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  5013. {
  5014. external_constructor<value_t::object>::construct(j, std::move(obj));
  5015. }
  5016. template <
  5017. typename BasicJsonType, typename T, std::size_t N,
  5018. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  5019. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5020. int > = 0 >
  5021. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  5022. {
  5023. external_constructor<value_t::array>::construct(j, arr);
  5024. }
  5025. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  5026. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  5027. {
  5028. j = { p.first, p.second };
  5029. }
  5030. // for https://github.com/nlohmann/json/pull/1134
  5031. template<typename BasicJsonType, typename T,
  5032. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  5033. inline void to_json(BasicJsonType& j, const T& b)
  5034. {
  5035. j = { {b.key(), b.value()} };
  5036. }
  5037. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  5038. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  5039. {
  5040. j = { std::get<Idx>(t)... };
  5041. }
  5042. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  5043. inline void to_json(BasicJsonType& j, const T& t)
  5044. {
  5045. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  5046. }
  5047. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  5048. template<typename BasicJsonType>
  5049. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  5050. {
  5051. j = p.string();
  5052. }
  5053. #endif
  5054. struct to_json_fn
  5055. {
  5056. template<typename BasicJsonType, typename T>
  5057. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  5058. -> decltype(to_json(j, std::forward<T>(val)), void())
  5059. {
  5060. return to_json(j, std::forward<T>(val));
  5061. }
  5062. };
  5063. } // namespace detail
  5064. #ifndef JSON_HAS_CPP_17
  5065. /// namespace to hold default `to_json` function
  5066. /// to see why this is required:
  5067. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5068. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5069. {
  5070. #endif
  5071. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5072. detail::static_const<detail::to_json_fn>::value;
  5073. #ifndef JSON_HAS_CPP_17
  5074. } // namespace
  5075. #endif
  5076. NLOHMANN_JSON_NAMESPACE_END
  5077. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5078. NLOHMANN_JSON_NAMESPACE_BEGIN
  5079. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5080. template<typename ValueType, typename>
  5081. struct adl_serializer
  5082. {
  5083. /// @brief convert a JSON value to any value type
  5084. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5085. template<typename BasicJsonType, typename TargetType = ValueType>
  5086. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5087. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5088. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5089. {
  5090. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5091. }
  5092. /// @brief convert a JSON value to any value type
  5093. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5094. template<typename BasicJsonType, typename TargetType = ValueType>
  5095. static auto from_json(BasicJsonType && j) noexcept(
  5096. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5097. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5098. {
  5099. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5100. }
  5101. /// @brief convert any value type to a JSON value
  5102. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5103. template<typename BasicJsonType, typename TargetType = ValueType>
  5104. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5105. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5106. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5107. {
  5108. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5109. }
  5110. };
  5111. NLOHMANN_JSON_NAMESPACE_END
  5112. // #include <nlohmann/byte_container_with_subtype.hpp>
  5113. // __ _____ _____ _____
  5114. // __| | __| | | | JSON for Modern C++
  5115. // | | |__ | | | | | | version 3.11.3
  5116. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5117. //
  5118. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5119. // SPDX-License-Identifier: MIT
  5120. #include <cstdint> // uint8_t, uint64_t
  5121. #include <tuple> // tie
  5122. #include <utility> // move
  5123. // #include <nlohmann/detail/abi_macros.hpp>
  5124. NLOHMANN_JSON_NAMESPACE_BEGIN
  5125. /// @brief an internal type for a backed binary type
  5126. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5127. template<typename BinaryType>
  5128. class byte_container_with_subtype : public BinaryType
  5129. {
  5130. public:
  5131. using container_type = BinaryType;
  5132. using subtype_type = std::uint64_t;
  5133. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5134. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5135. : container_type()
  5136. {}
  5137. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5138. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5139. : container_type(b)
  5140. {}
  5141. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5142. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5143. : container_type(std::move(b))
  5144. {}
  5145. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5146. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5147. : container_type(b)
  5148. , m_subtype(subtype_)
  5149. , m_has_subtype(true)
  5150. {}
  5151. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5152. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5153. : container_type(std::move(b))
  5154. , m_subtype(subtype_)
  5155. , m_has_subtype(true)
  5156. {}
  5157. bool operator==(const byte_container_with_subtype& rhs) const
  5158. {
  5159. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5160. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5161. }
  5162. bool operator!=(const byte_container_with_subtype& rhs) const
  5163. {
  5164. return !(rhs == *this);
  5165. }
  5166. /// @brief sets the binary subtype
  5167. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5168. void set_subtype(subtype_type subtype_) noexcept
  5169. {
  5170. m_subtype = subtype_;
  5171. m_has_subtype = true;
  5172. }
  5173. /// @brief return the binary subtype
  5174. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5175. constexpr subtype_type subtype() const noexcept
  5176. {
  5177. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5178. }
  5179. /// @brief return whether the value has a subtype
  5180. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5181. constexpr bool has_subtype() const noexcept
  5182. {
  5183. return m_has_subtype;
  5184. }
  5185. /// @brief clears the binary subtype
  5186. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5187. void clear_subtype() noexcept
  5188. {
  5189. m_subtype = 0;
  5190. m_has_subtype = false;
  5191. }
  5192. private:
  5193. subtype_type m_subtype = 0;
  5194. bool m_has_subtype = false;
  5195. };
  5196. NLOHMANN_JSON_NAMESPACE_END
  5197. // #include <nlohmann/detail/conversions/from_json.hpp>
  5198. // #include <nlohmann/detail/conversions/to_json.hpp>
  5199. // #include <nlohmann/detail/exceptions.hpp>
  5200. // #include <nlohmann/detail/hash.hpp>
  5201. // __ _____ _____ _____
  5202. // __| | __| | | | JSON for Modern C++
  5203. // | | |__ | | | | | | version 3.11.3
  5204. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5205. //
  5206. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5207. // SPDX-License-Identifier: MIT
  5208. #include <cstdint> // uint8_t
  5209. #include <cstddef> // size_t
  5210. #include <functional> // hash
  5211. // #include <nlohmann/detail/abi_macros.hpp>
  5212. // #include <nlohmann/detail/value_t.hpp>
  5213. NLOHMANN_JSON_NAMESPACE_BEGIN
  5214. namespace detail
  5215. {
  5216. // boost::hash_combine
  5217. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5218. {
  5219. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5220. return seed;
  5221. }
  5222. /*!
  5223. @brief hash a JSON value
  5224. The hash function tries to rely on std::hash where possible. Furthermore, the
  5225. type of the JSON value is taken into account to have different hash values for
  5226. null, 0, 0U, and false, etc.
  5227. @tparam BasicJsonType basic_json specialization
  5228. @param j JSON value to hash
  5229. @return hash value of j
  5230. */
  5231. template<typename BasicJsonType>
  5232. std::size_t hash(const BasicJsonType& j)
  5233. {
  5234. using string_t = typename BasicJsonType::string_t;
  5235. using number_integer_t = typename BasicJsonType::number_integer_t;
  5236. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5237. using number_float_t = typename BasicJsonType::number_float_t;
  5238. const auto type = static_cast<std::size_t>(j.type());
  5239. switch (j.type())
  5240. {
  5241. case BasicJsonType::value_t::null:
  5242. case BasicJsonType::value_t::discarded:
  5243. {
  5244. return combine(type, 0);
  5245. }
  5246. case BasicJsonType::value_t::object:
  5247. {
  5248. auto seed = combine(type, j.size());
  5249. for (const auto& element : j.items())
  5250. {
  5251. const auto h = std::hash<string_t> {}(element.key());
  5252. seed = combine(seed, h);
  5253. seed = combine(seed, hash(element.value()));
  5254. }
  5255. return seed;
  5256. }
  5257. case BasicJsonType::value_t::array:
  5258. {
  5259. auto seed = combine(type, j.size());
  5260. for (const auto& element : j)
  5261. {
  5262. seed = combine(seed, hash(element));
  5263. }
  5264. return seed;
  5265. }
  5266. case BasicJsonType::value_t::string:
  5267. {
  5268. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5269. return combine(type, h);
  5270. }
  5271. case BasicJsonType::value_t::boolean:
  5272. {
  5273. const auto h = std::hash<bool> {}(j.template get<bool>());
  5274. return combine(type, h);
  5275. }
  5276. case BasicJsonType::value_t::number_integer:
  5277. {
  5278. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5279. return combine(type, h);
  5280. }
  5281. case BasicJsonType::value_t::number_unsigned:
  5282. {
  5283. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5284. return combine(type, h);
  5285. }
  5286. case BasicJsonType::value_t::number_float:
  5287. {
  5288. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5289. return combine(type, h);
  5290. }
  5291. case BasicJsonType::value_t::binary:
  5292. {
  5293. auto seed = combine(type, j.get_binary().size());
  5294. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5295. seed = combine(seed, h);
  5296. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5297. for (const auto byte : j.get_binary())
  5298. {
  5299. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5300. }
  5301. return seed;
  5302. }
  5303. default: // LCOV_EXCL_LINE
  5304. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5305. return 0; // LCOV_EXCL_LINE
  5306. }
  5307. }
  5308. } // namespace detail
  5309. NLOHMANN_JSON_NAMESPACE_END
  5310. // #include <nlohmann/detail/input/binary_reader.hpp>
  5311. // __ _____ _____ _____
  5312. // __| | __| | | | JSON for Modern C++
  5313. // | | |__ | | | | | | version 3.11.3
  5314. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5315. //
  5316. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5317. // SPDX-License-Identifier: MIT
  5318. #include <algorithm> // generate_n
  5319. #include <array> // array
  5320. #include <cmath> // ldexp
  5321. #include <cstddef> // size_t
  5322. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5323. #include <cstdio> // snprintf
  5324. #include <cstring> // memcpy
  5325. #include <iterator> // back_inserter
  5326. #include <limits> // numeric_limits
  5327. #include <string> // char_traits, string
  5328. #include <utility> // make_pair, move
  5329. #include <vector> // vector
  5330. // #include <nlohmann/detail/exceptions.hpp>
  5331. // #include <nlohmann/detail/input/input_adapters.hpp>
  5332. // __ _____ _____ _____
  5333. // __| | __| | | | JSON for Modern C++
  5334. // | | |__ | | | | | | version 3.11.3
  5335. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5336. //
  5337. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5338. // SPDX-License-Identifier: MIT
  5339. #include <array> // array
  5340. #include <cstddef> // size_t
  5341. #include <cstring> // strlen
  5342. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5343. #include <memory> // shared_ptr, make_shared, addressof
  5344. #include <numeric> // accumulate
  5345. #include <string> // string, char_traits
  5346. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5347. #include <utility> // pair, declval
  5348. #ifndef JSON_NO_IO
  5349. #include <cstdio> // FILE *
  5350. #include <istream> // istream
  5351. #endif // JSON_NO_IO
  5352. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5353. // #include <nlohmann/detail/macro_scope.hpp>
  5354. // #include <nlohmann/detail/meta/type_traits.hpp>
  5355. NLOHMANN_JSON_NAMESPACE_BEGIN
  5356. namespace detail
  5357. {
  5358. /// the supported input formats
  5359. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5360. ////////////////////
  5361. // input adapters //
  5362. ////////////////////
  5363. #ifndef JSON_NO_IO
  5364. /*!
  5365. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5366. buffer. This adapter is a very low level adapter.
  5367. */
  5368. class file_input_adapter
  5369. {
  5370. public:
  5371. using char_type = char;
  5372. JSON_HEDLEY_NON_NULL(2)
  5373. explicit file_input_adapter(std::FILE* f) noexcept
  5374. : m_file(f)
  5375. {
  5376. JSON_ASSERT(m_file != nullptr);
  5377. }
  5378. // make class move-only
  5379. file_input_adapter(const file_input_adapter&) = delete;
  5380. file_input_adapter(file_input_adapter&&) noexcept = default;
  5381. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5382. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5383. ~file_input_adapter() = default;
  5384. std::char_traits<char>::int_type get_character() noexcept
  5385. {
  5386. return std::fgetc(m_file);
  5387. }
  5388. private:
  5389. /// the file pointer to read from
  5390. std::FILE* m_file;
  5391. };
  5392. /*!
  5393. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5394. beginning of input. Does not support changing the underlying std::streambuf
  5395. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5396. subsequent use of standard std::istream operations to process any input
  5397. characters following those used in parsing the JSON input. Clears the
  5398. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5399. subsequent call for input from the std::istream.
  5400. */
  5401. class input_stream_adapter
  5402. {
  5403. public:
  5404. using char_type = char;
  5405. ~input_stream_adapter()
  5406. {
  5407. // clear stream flags; we use underlying streambuf I/O, do not
  5408. // maintain ifstream flags, except eof
  5409. if (is != nullptr)
  5410. {
  5411. is->clear(is->rdstate() & std::ios::eofbit);
  5412. }
  5413. }
  5414. explicit input_stream_adapter(std::istream& i)
  5415. : is(&i), sb(i.rdbuf())
  5416. {}
  5417. // delete because of pointer members
  5418. input_stream_adapter(const input_stream_adapter&) = delete;
  5419. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5420. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5421. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5422. : is(rhs.is), sb(rhs.sb)
  5423. {
  5424. rhs.is = nullptr;
  5425. rhs.sb = nullptr;
  5426. }
  5427. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5428. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5429. // end up as the same value, e.g. 0xFFFFFFFF.
  5430. std::char_traits<char>::int_type get_character()
  5431. {
  5432. auto res = sb->sbumpc();
  5433. // set eof manually, as we don't use the istream interface.
  5434. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5435. {
  5436. is->clear(is->rdstate() | std::ios::eofbit);
  5437. }
  5438. return res;
  5439. }
  5440. private:
  5441. /// the associated input stream
  5442. std::istream* is = nullptr;
  5443. std::streambuf* sb = nullptr;
  5444. };
  5445. #endif // JSON_NO_IO
  5446. // General-purpose iterator-based adapter. It might not be as fast as
  5447. // theoretically possible for some containers, but it is extremely versatile.
  5448. template<typename IteratorType>
  5449. class iterator_input_adapter
  5450. {
  5451. public:
  5452. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5453. iterator_input_adapter(IteratorType first, IteratorType last)
  5454. : current(std::move(first)), end(std::move(last))
  5455. {}
  5456. typename char_traits<char_type>::int_type get_character()
  5457. {
  5458. if (JSON_HEDLEY_LIKELY(current != end))
  5459. {
  5460. auto result = char_traits<char_type>::to_int_type(*current);
  5461. std::advance(current, 1);
  5462. return result;
  5463. }
  5464. return char_traits<char_type>::eof();
  5465. }
  5466. private:
  5467. IteratorType current;
  5468. IteratorType end;
  5469. template<typename BaseInputAdapter, size_t T>
  5470. friend struct wide_string_input_helper;
  5471. bool empty() const
  5472. {
  5473. return current == end;
  5474. }
  5475. };
  5476. template<typename BaseInputAdapter, size_t T>
  5477. struct wide_string_input_helper;
  5478. template<typename BaseInputAdapter>
  5479. struct wide_string_input_helper<BaseInputAdapter, 4>
  5480. {
  5481. // UTF-32
  5482. static void fill_buffer(BaseInputAdapter& input,
  5483. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5484. size_t& utf8_bytes_index,
  5485. size_t& utf8_bytes_filled)
  5486. {
  5487. utf8_bytes_index = 0;
  5488. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5489. {
  5490. utf8_bytes[0] = std::char_traits<char>::eof();
  5491. utf8_bytes_filled = 1;
  5492. }
  5493. else
  5494. {
  5495. // get the current character
  5496. const auto wc = input.get_character();
  5497. // UTF-32 to UTF-8 encoding
  5498. if (wc < 0x80)
  5499. {
  5500. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5501. utf8_bytes_filled = 1;
  5502. }
  5503. else if (wc <= 0x7FF)
  5504. {
  5505. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5506. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5507. utf8_bytes_filled = 2;
  5508. }
  5509. else if (wc <= 0xFFFF)
  5510. {
  5511. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5512. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5513. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5514. utf8_bytes_filled = 3;
  5515. }
  5516. else if (wc <= 0x10FFFF)
  5517. {
  5518. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5519. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5520. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5521. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5522. utf8_bytes_filled = 4;
  5523. }
  5524. else
  5525. {
  5526. // unknown character
  5527. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5528. utf8_bytes_filled = 1;
  5529. }
  5530. }
  5531. }
  5532. };
  5533. template<typename BaseInputAdapter>
  5534. struct wide_string_input_helper<BaseInputAdapter, 2>
  5535. {
  5536. // UTF-16
  5537. static void fill_buffer(BaseInputAdapter& input,
  5538. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5539. size_t& utf8_bytes_index,
  5540. size_t& utf8_bytes_filled)
  5541. {
  5542. utf8_bytes_index = 0;
  5543. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5544. {
  5545. utf8_bytes[0] = std::char_traits<char>::eof();
  5546. utf8_bytes_filled = 1;
  5547. }
  5548. else
  5549. {
  5550. // get the current character
  5551. const auto wc = input.get_character();
  5552. // UTF-16 to UTF-8 encoding
  5553. if (wc < 0x80)
  5554. {
  5555. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5556. utf8_bytes_filled = 1;
  5557. }
  5558. else if (wc <= 0x7FF)
  5559. {
  5560. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5561. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5562. utf8_bytes_filled = 2;
  5563. }
  5564. else if (0xD800 > wc || wc >= 0xE000)
  5565. {
  5566. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5567. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5568. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5569. utf8_bytes_filled = 3;
  5570. }
  5571. else
  5572. {
  5573. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5574. {
  5575. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5576. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5577. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5578. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5579. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5580. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5581. utf8_bytes_filled = 4;
  5582. }
  5583. else
  5584. {
  5585. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5586. utf8_bytes_filled = 1;
  5587. }
  5588. }
  5589. }
  5590. }
  5591. };
  5592. // Wraps another input adapter to convert wide character types into individual bytes.
  5593. template<typename BaseInputAdapter, typename WideCharType>
  5594. class wide_string_input_adapter
  5595. {
  5596. public:
  5597. using char_type = char;
  5598. wide_string_input_adapter(BaseInputAdapter base)
  5599. : base_adapter(base) {}
  5600. typename std::char_traits<char>::int_type get_character() noexcept
  5601. {
  5602. // check if buffer needs to be filled
  5603. if (utf8_bytes_index == utf8_bytes_filled)
  5604. {
  5605. fill_buffer<sizeof(WideCharType)>();
  5606. JSON_ASSERT(utf8_bytes_filled > 0);
  5607. JSON_ASSERT(utf8_bytes_index == 0);
  5608. }
  5609. // use buffer
  5610. JSON_ASSERT(utf8_bytes_filled > 0);
  5611. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5612. return utf8_bytes[utf8_bytes_index++];
  5613. }
  5614. private:
  5615. BaseInputAdapter base_adapter;
  5616. template<size_t T>
  5617. void fill_buffer()
  5618. {
  5619. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5620. }
  5621. /// a buffer for UTF-8 bytes
  5622. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5623. /// index to the utf8_codes array for the next valid byte
  5624. std::size_t utf8_bytes_index = 0;
  5625. /// number of valid bytes in the utf8_codes array
  5626. std::size_t utf8_bytes_filled = 0;
  5627. };
  5628. template<typename IteratorType, typename Enable = void>
  5629. struct iterator_input_adapter_factory
  5630. {
  5631. using iterator_type = IteratorType;
  5632. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5633. using adapter_type = iterator_input_adapter<iterator_type>;
  5634. static adapter_type create(IteratorType first, IteratorType last)
  5635. {
  5636. return adapter_type(std::move(first), std::move(last));
  5637. }
  5638. };
  5639. template<typename T>
  5640. struct is_iterator_of_multibyte
  5641. {
  5642. using value_type = typename std::iterator_traits<T>::value_type;
  5643. enum
  5644. {
  5645. value = sizeof(value_type) > 1
  5646. };
  5647. };
  5648. template<typename IteratorType>
  5649. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5650. {
  5651. using iterator_type = IteratorType;
  5652. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5653. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5654. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5655. static adapter_type create(IteratorType first, IteratorType last)
  5656. {
  5657. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5658. }
  5659. };
  5660. // General purpose iterator-based input
  5661. template<typename IteratorType>
  5662. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5663. {
  5664. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5665. return factory_type::create(first, last);
  5666. }
  5667. // Convenience shorthand from container to iterator
  5668. // Enables ADL on begin(container) and end(container)
  5669. // Encloses the using declarations in namespace for not to leak them to outside scope
  5670. namespace container_input_adapter_factory_impl
  5671. {
  5672. using std::begin;
  5673. using std::end;
  5674. template<typename ContainerType, typename Enable = void>
  5675. struct container_input_adapter_factory {};
  5676. template<typename ContainerType>
  5677. struct container_input_adapter_factory< ContainerType,
  5678. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5679. {
  5680. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5681. static adapter_type create(const ContainerType& container)
  5682. {
  5683. return input_adapter(begin(container), end(container));
  5684. }
  5685. };
  5686. } // namespace container_input_adapter_factory_impl
  5687. template<typename ContainerType>
  5688. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5689. {
  5690. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5691. }
  5692. #ifndef JSON_NO_IO
  5693. // Special cases with fast paths
  5694. inline file_input_adapter input_adapter(std::FILE* file)
  5695. {
  5696. return file_input_adapter(file);
  5697. }
  5698. inline input_stream_adapter input_adapter(std::istream& stream)
  5699. {
  5700. return input_stream_adapter(stream);
  5701. }
  5702. inline input_stream_adapter input_adapter(std::istream&& stream)
  5703. {
  5704. return input_stream_adapter(stream);
  5705. }
  5706. #endif // JSON_NO_IO
  5707. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5708. // Null-delimited strings, and the like.
  5709. template < typename CharT,
  5710. typename std::enable_if <
  5711. std::is_pointer<CharT>::value&&
  5712. !std::is_array<CharT>::value&&
  5713. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5714. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5715. int >::type = 0 >
  5716. contiguous_bytes_input_adapter input_adapter(CharT b)
  5717. {
  5718. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5719. const auto* ptr = reinterpret_cast<const char*>(b);
  5720. return input_adapter(ptr, ptr + length);
  5721. }
  5722. template<typename T, std::size_t N>
  5723. 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)
  5724. {
  5725. return input_adapter(array, array + N);
  5726. }
  5727. // This class only handles inputs of input_buffer_adapter type.
  5728. // It's required so that expressions like {ptr, len} can be implicitly cast
  5729. // to the correct adapter.
  5730. class span_input_adapter
  5731. {
  5732. public:
  5733. template < typename CharT,
  5734. typename std::enable_if <
  5735. std::is_pointer<CharT>::value&&
  5736. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5737. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5738. int >::type = 0 >
  5739. span_input_adapter(CharT b, std::size_t l)
  5740. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5741. template<class IteratorType,
  5742. typename std::enable_if<
  5743. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5744. int>::type = 0>
  5745. span_input_adapter(IteratorType first, IteratorType last)
  5746. : ia(input_adapter(first, last)) {}
  5747. contiguous_bytes_input_adapter&& get()
  5748. {
  5749. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5750. }
  5751. private:
  5752. contiguous_bytes_input_adapter ia;
  5753. };
  5754. } // namespace detail
  5755. NLOHMANN_JSON_NAMESPACE_END
  5756. // #include <nlohmann/detail/input/json_sax.hpp>
  5757. // __ _____ _____ _____
  5758. // __| | __| | | | JSON for Modern C++
  5759. // | | |__ | | | | | | version 3.11.3
  5760. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5761. //
  5762. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  5763. // SPDX-License-Identifier: MIT
  5764. #include <cstddef>
  5765. #include <string> // string
  5766. #include <utility> // move
  5767. #include <vector> // vector
  5768. // #include <nlohmann/detail/exceptions.hpp>
  5769. // #include <nlohmann/detail/macro_scope.hpp>
  5770. // #include <nlohmann/detail/string_concat.hpp>
  5771. NLOHMANN_JSON_NAMESPACE_BEGIN
  5772. /*!
  5773. @brief SAX interface
  5774. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5775. Each function is called in different situations while the input is parsed. The
  5776. boolean return value informs the parser whether to continue processing the
  5777. input.
  5778. */
  5779. template<typename BasicJsonType>
  5780. struct json_sax
  5781. {
  5782. using number_integer_t = typename BasicJsonType::number_integer_t;
  5783. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5784. using number_float_t = typename BasicJsonType::number_float_t;
  5785. using string_t = typename BasicJsonType::string_t;
  5786. using binary_t = typename BasicJsonType::binary_t;
  5787. /*!
  5788. @brief a null value was read
  5789. @return whether parsing should proceed
  5790. */
  5791. virtual bool null() = 0;
  5792. /*!
  5793. @brief a boolean value was read
  5794. @param[in] val boolean value
  5795. @return whether parsing should proceed
  5796. */
  5797. virtual bool boolean(bool val) = 0;
  5798. /*!
  5799. @brief an integer number was read
  5800. @param[in] val integer value
  5801. @return whether parsing should proceed
  5802. */
  5803. virtual bool number_integer(number_integer_t val) = 0;
  5804. /*!
  5805. @brief an unsigned integer number was read
  5806. @param[in] val unsigned integer value
  5807. @return whether parsing should proceed
  5808. */
  5809. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5810. /*!
  5811. @brief a floating-point number was read
  5812. @param[in] val floating-point value
  5813. @param[in] s raw token value
  5814. @return whether parsing should proceed
  5815. */
  5816. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5817. /*!
  5818. @brief a string value was read
  5819. @param[in] val string value
  5820. @return whether parsing should proceed
  5821. @note It is safe to move the passed string value.
  5822. */
  5823. virtual bool string(string_t& val) = 0;
  5824. /*!
  5825. @brief a binary value was read
  5826. @param[in] val binary value
  5827. @return whether parsing should proceed
  5828. @note It is safe to move the passed binary value.
  5829. */
  5830. virtual bool binary(binary_t& val) = 0;
  5831. /*!
  5832. @brief the beginning of an object was read
  5833. @param[in] elements number of object elements or -1 if unknown
  5834. @return whether parsing should proceed
  5835. @note binary formats may report the number of elements
  5836. */
  5837. virtual bool start_object(std::size_t elements) = 0;
  5838. /*!
  5839. @brief an object key was read
  5840. @param[in] val object key
  5841. @return whether parsing should proceed
  5842. @note It is safe to move the passed string.
  5843. */
  5844. virtual bool key(string_t& val) = 0;
  5845. /*!
  5846. @brief the end of an object was read
  5847. @return whether parsing should proceed
  5848. */
  5849. virtual bool end_object() = 0;
  5850. /*!
  5851. @brief the beginning of an array was read
  5852. @param[in] elements number of array elements or -1 if unknown
  5853. @return whether parsing should proceed
  5854. @note binary formats may report the number of elements
  5855. */
  5856. virtual bool start_array(std::size_t elements) = 0;
  5857. /*!
  5858. @brief the end of an array was read
  5859. @return whether parsing should proceed
  5860. */
  5861. virtual bool end_array() = 0;
  5862. /*!
  5863. @brief a parse error occurred
  5864. @param[in] position the position in the input where the error occurs
  5865. @param[in] last_token the last read token
  5866. @param[in] ex an exception object describing the error
  5867. @return whether parsing should proceed (must return false)
  5868. */
  5869. virtual bool parse_error(std::size_t position,
  5870. const std::string& last_token,
  5871. const detail::exception& ex) = 0;
  5872. json_sax() = default;
  5873. json_sax(const json_sax&) = default;
  5874. json_sax(json_sax&&) noexcept = default;
  5875. json_sax& operator=(const json_sax&) = default;
  5876. json_sax& operator=(json_sax&&) noexcept = default;
  5877. virtual ~json_sax() = default;
  5878. };
  5879. namespace detail
  5880. {
  5881. /*!
  5882. @brief SAX implementation to create a JSON value from SAX events
  5883. This class implements the @ref json_sax interface and processes the SAX events
  5884. to create a JSON value which makes it basically a DOM parser. The structure or
  5885. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5886. a pointer to the respective array or object for each recursion depth.
  5887. After successful parsing, the value that is passed by reference to the
  5888. constructor contains the parsed value.
  5889. @tparam BasicJsonType the JSON type
  5890. */
  5891. template<typename BasicJsonType>
  5892. class json_sax_dom_parser
  5893. {
  5894. public:
  5895. using number_integer_t = typename BasicJsonType::number_integer_t;
  5896. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5897. using number_float_t = typename BasicJsonType::number_float_t;
  5898. using string_t = typename BasicJsonType::string_t;
  5899. using binary_t = typename BasicJsonType::binary_t;
  5900. /*!
  5901. @param[in,out] r reference to a JSON value that is manipulated while
  5902. parsing
  5903. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5904. */
  5905. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5906. : root(r), allow_exceptions(allow_exceptions_)
  5907. {}
  5908. // make class move-only
  5909. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5910. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5911. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5912. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5913. ~json_sax_dom_parser() = default;
  5914. bool null()
  5915. {
  5916. handle_value(nullptr);
  5917. return true;
  5918. }
  5919. bool boolean(bool val)
  5920. {
  5921. handle_value(val);
  5922. return true;
  5923. }
  5924. bool number_integer(number_integer_t val)
  5925. {
  5926. handle_value(val);
  5927. return true;
  5928. }
  5929. bool number_unsigned(number_unsigned_t val)
  5930. {
  5931. handle_value(val);
  5932. return true;
  5933. }
  5934. bool number_float(number_float_t val, const string_t& /*unused*/)
  5935. {
  5936. handle_value(val);
  5937. return true;
  5938. }
  5939. bool string(string_t& val)
  5940. {
  5941. handle_value(val);
  5942. return true;
  5943. }
  5944. bool binary(binary_t& val)
  5945. {
  5946. handle_value(std::move(val));
  5947. return true;
  5948. }
  5949. bool start_object(std::size_t len)
  5950. {
  5951. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5952. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5953. {
  5954. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5955. }
  5956. return true;
  5957. }
  5958. bool key(string_t& val)
  5959. {
  5960. JSON_ASSERT(!ref_stack.empty());
  5961. JSON_ASSERT(ref_stack.back()->is_object());
  5962. // add null at given key and store the reference for later
  5963. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
  5964. return true;
  5965. }
  5966. bool end_object()
  5967. {
  5968. JSON_ASSERT(!ref_stack.empty());
  5969. JSON_ASSERT(ref_stack.back()->is_object());
  5970. ref_stack.back()->set_parents();
  5971. ref_stack.pop_back();
  5972. return true;
  5973. }
  5974. bool start_array(std::size_t len)
  5975. {
  5976. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5977. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5978. {
  5979. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5980. }
  5981. return true;
  5982. }
  5983. bool end_array()
  5984. {
  5985. JSON_ASSERT(!ref_stack.empty());
  5986. JSON_ASSERT(ref_stack.back()->is_array());
  5987. ref_stack.back()->set_parents();
  5988. ref_stack.pop_back();
  5989. return true;
  5990. }
  5991. template<class Exception>
  5992. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5993. const Exception& ex)
  5994. {
  5995. errored = true;
  5996. static_cast<void>(ex);
  5997. if (allow_exceptions)
  5998. {
  5999. JSON_THROW(ex);
  6000. }
  6001. return false;
  6002. }
  6003. constexpr bool is_errored() const
  6004. {
  6005. return errored;
  6006. }
  6007. private:
  6008. /*!
  6009. @invariant If the ref stack is empty, then the passed value will be the new
  6010. root.
  6011. @invariant If the ref stack contains a value, then it is an array or an
  6012. object to which we can add elements
  6013. */
  6014. template<typename Value>
  6015. JSON_HEDLEY_RETURNS_NON_NULL
  6016. BasicJsonType* handle_value(Value&& v)
  6017. {
  6018. if (ref_stack.empty())
  6019. {
  6020. root = BasicJsonType(std::forward<Value>(v));
  6021. return &root;
  6022. }
  6023. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6024. if (ref_stack.back()->is_array())
  6025. {
  6026. ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
  6027. return &(ref_stack.back()->m_data.m_value.array->back());
  6028. }
  6029. JSON_ASSERT(ref_stack.back()->is_object());
  6030. JSON_ASSERT(object_element);
  6031. *object_element = BasicJsonType(std::forward<Value>(v));
  6032. return object_element;
  6033. }
  6034. /// the parsed JSON value
  6035. BasicJsonType& root;
  6036. /// stack to model hierarchy of values
  6037. std::vector<BasicJsonType*> ref_stack {};
  6038. /// helper to hold the reference for the next object element
  6039. BasicJsonType* object_element = nullptr;
  6040. /// whether a syntax error occurred
  6041. bool errored = false;
  6042. /// whether to throw exceptions in case of errors
  6043. const bool allow_exceptions = true;
  6044. };
  6045. template<typename BasicJsonType>
  6046. class json_sax_dom_callback_parser
  6047. {
  6048. public:
  6049. using number_integer_t = typename BasicJsonType::number_integer_t;
  6050. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6051. using number_float_t = typename BasicJsonType::number_float_t;
  6052. using string_t = typename BasicJsonType::string_t;
  6053. using binary_t = typename BasicJsonType::binary_t;
  6054. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  6055. using parse_event_t = typename BasicJsonType::parse_event_t;
  6056. json_sax_dom_callback_parser(BasicJsonType& r,
  6057. const parser_callback_t cb,
  6058. const bool allow_exceptions_ = true)
  6059. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  6060. {
  6061. keep_stack.push_back(true);
  6062. }
  6063. // make class move-only
  6064. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  6065. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6066. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  6067. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6068. ~json_sax_dom_callback_parser() = default;
  6069. bool null()
  6070. {
  6071. handle_value(nullptr);
  6072. return true;
  6073. }
  6074. bool boolean(bool val)
  6075. {
  6076. handle_value(val);
  6077. return true;
  6078. }
  6079. bool number_integer(number_integer_t val)
  6080. {
  6081. handle_value(val);
  6082. return true;
  6083. }
  6084. bool number_unsigned(number_unsigned_t val)
  6085. {
  6086. handle_value(val);
  6087. return true;
  6088. }
  6089. bool number_float(number_float_t val, const string_t& /*unused*/)
  6090. {
  6091. handle_value(val);
  6092. return true;
  6093. }
  6094. bool string(string_t& val)
  6095. {
  6096. handle_value(val);
  6097. return true;
  6098. }
  6099. bool binary(binary_t& val)
  6100. {
  6101. handle_value(std::move(val));
  6102. return true;
  6103. }
  6104. bool start_object(std::size_t len)
  6105. {
  6106. // check callback for object start
  6107. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  6108. keep_stack.push_back(keep);
  6109. auto val = handle_value(BasicJsonType::value_t::object, true);
  6110. ref_stack.push_back(val.second);
  6111. // check object limit
  6112. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6113. {
  6114. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  6115. }
  6116. return true;
  6117. }
  6118. bool key(string_t& val)
  6119. {
  6120. BasicJsonType k = BasicJsonType(val);
  6121. // check callback for key
  6122. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  6123. key_keep_stack.push_back(keep);
  6124. // add discarded value at given key and store the reference for later
  6125. if (keep && ref_stack.back())
  6126. {
  6127. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
  6128. }
  6129. return true;
  6130. }
  6131. bool end_object()
  6132. {
  6133. if (ref_stack.back())
  6134. {
  6135. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  6136. {
  6137. // discard object
  6138. *ref_stack.back() = discarded;
  6139. }
  6140. else
  6141. {
  6142. ref_stack.back()->set_parents();
  6143. }
  6144. }
  6145. JSON_ASSERT(!ref_stack.empty());
  6146. JSON_ASSERT(!keep_stack.empty());
  6147. ref_stack.pop_back();
  6148. keep_stack.pop_back();
  6149. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  6150. {
  6151. // remove discarded value
  6152. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  6153. {
  6154. if (it->is_discarded())
  6155. {
  6156. ref_stack.back()->erase(it);
  6157. break;
  6158. }
  6159. }
  6160. }
  6161. return true;
  6162. }
  6163. bool start_array(std::size_t len)
  6164. {
  6165. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  6166. keep_stack.push_back(keep);
  6167. auto val = handle_value(BasicJsonType::value_t::array, true);
  6168. ref_stack.push_back(val.second);
  6169. // check array limit
  6170. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6171. {
  6172. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  6173. }
  6174. return true;
  6175. }
  6176. bool end_array()
  6177. {
  6178. bool keep = true;
  6179. if (ref_stack.back())
  6180. {
  6181. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  6182. if (keep)
  6183. {
  6184. ref_stack.back()->set_parents();
  6185. }
  6186. else
  6187. {
  6188. // discard array
  6189. *ref_stack.back() = discarded;
  6190. }
  6191. }
  6192. JSON_ASSERT(!ref_stack.empty());
  6193. JSON_ASSERT(!keep_stack.empty());
  6194. ref_stack.pop_back();
  6195. keep_stack.pop_back();
  6196. // remove discarded value
  6197. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  6198. {
  6199. ref_stack.back()->m_data.m_value.array->pop_back();
  6200. }
  6201. return true;
  6202. }
  6203. template<class Exception>
  6204. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  6205. const Exception& ex)
  6206. {
  6207. errored = true;
  6208. static_cast<void>(ex);
  6209. if (allow_exceptions)
  6210. {
  6211. JSON_THROW(ex);
  6212. }
  6213. return false;
  6214. }
  6215. constexpr bool is_errored() const
  6216. {
  6217. return errored;
  6218. }
  6219. private:
  6220. /*!
  6221. @param[in] v value to add to the JSON value we build during parsing
  6222. @param[in] skip_callback whether we should skip calling the callback
  6223. function; this is required after start_array() and
  6224. start_object() SAX events, because otherwise we would call the
  6225. callback function with an empty array or object, respectively.
  6226. @invariant If the ref stack is empty, then the passed value will be the new
  6227. root.
  6228. @invariant If the ref stack contains a value, then it is an array or an
  6229. object to which we can add elements
  6230. @return pair of boolean (whether value should be kept) and pointer (to the
  6231. passed value in the ref_stack hierarchy; nullptr if not kept)
  6232. */
  6233. template<typename Value>
  6234. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  6235. {
  6236. JSON_ASSERT(!keep_stack.empty());
  6237. // do not handle this value if we know it would be added to a discarded
  6238. // container
  6239. if (!keep_stack.back())
  6240. {
  6241. return {false, nullptr};
  6242. }
  6243. // create value
  6244. auto value = BasicJsonType(std::forward<Value>(v));
  6245. // check callback
  6246. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  6247. // do not handle this value if we just learnt it shall be discarded
  6248. if (!keep)
  6249. {
  6250. return {false, nullptr};
  6251. }
  6252. if (ref_stack.empty())
  6253. {
  6254. root = std::move(value);
  6255. return {true, & root};
  6256. }
  6257. // skip this value if we already decided to skip the parent
  6258. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  6259. if (!ref_stack.back())
  6260. {
  6261. return {false, nullptr};
  6262. }
  6263. // we now only expect arrays and objects
  6264. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6265. // array
  6266. if (ref_stack.back()->is_array())
  6267. {
  6268. ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
  6269. return {true, & (ref_stack.back()->m_data.m_value.array->back())};
  6270. }
  6271. // object
  6272. JSON_ASSERT(ref_stack.back()->is_object());
  6273. // check if we should store an element for the current key
  6274. JSON_ASSERT(!key_keep_stack.empty());
  6275. const bool store_element = key_keep_stack.back();
  6276. key_keep_stack.pop_back();
  6277. if (!store_element)
  6278. {
  6279. return {false, nullptr};
  6280. }
  6281. JSON_ASSERT(object_element);
  6282. *object_element = std::move(value);
  6283. return {true, object_element};
  6284. }
  6285. /// the parsed JSON value
  6286. BasicJsonType& root;
  6287. /// stack to model hierarchy of values
  6288. std::vector<BasicJsonType*> ref_stack {};
  6289. /// stack to manage which values to keep
  6290. std::vector<bool> keep_stack {};
  6291. /// stack to manage which object keys to keep
  6292. std::vector<bool> key_keep_stack {};
  6293. /// helper to hold the reference for the next object element
  6294. BasicJsonType* object_element = nullptr;
  6295. /// whether a syntax error occurred
  6296. bool errored = false;
  6297. /// callback function
  6298. const parser_callback_t callback = nullptr;
  6299. /// whether to throw exceptions in case of errors
  6300. const bool allow_exceptions = true;
  6301. /// a discarded value for the callback
  6302. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  6303. };
  6304. template<typename BasicJsonType>
  6305. class json_sax_acceptor
  6306. {
  6307. public:
  6308. using number_integer_t = typename BasicJsonType::number_integer_t;
  6309. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6310. using number_float_t = typename BasicJsonType::number_float_t;
  6311. using string_t = typename BasicJsonType::string_t;
  6312. using binary_t = typename BasicJsonType::binary_t;
  6313. bool null()
  6314. {
  6315. return true;
  6316. }
  6317. bool boolean(bool /*unused*/)
  6318. {
  6319. return true;
  6320. }
  6321. bool number_integer(number_integer_t /*unused*/)
  6322. {
  6323. return true;
  6324. }
  6325. bool number_unsigned(number_unsigned_t /*unused*/)
  6326. {
  6327. return true;
  6328. }
  6329. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  6330. {
  6331. return true;
  6332. }
  6333. bool string(string_t& /*unused*/)
  6334. {
  6335. return true;
  6336. }
  6337. bool binary(binary_t& /*unused*/)
  6338. {
  6339. return true;
  6340. }
  6341. bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6342. {
  6343. return true;
  6344. }
  6345. bool key(string_t& /*unused*/)
  6346. {
  6347. return true;
  6348. }
  6349. bool end_object()
  6350. {
  6351. return true;
  6352. }
  6353. bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6354. {
  6355. return true;
  6356. }
  6357. bool end_array()
  6358. {
  6359. return true;
  6360. }
  6361. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  6362. {
  6363. return false;
  6364. }
  6365. };
  6366. } // namespace detail
  6367. NLOHMANN_JSON_NAMESPACE_END
  6368. // #include <nlohmann/detail/input/lexer.hpp>
  6369. // __ _____ _____ _____
  6370. // __| | __| | | | JSON for Modern C++
  6371. // | | |__ | | | | | | version 3.11.3
  6372. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6373. //
  6374. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  6375. // SPDX-License-Identifier: MIT
  6376. #include <array> // array
  6377. #include <clocale> // localeconv
  6378. #include <cstddef> // size_t
  6379. #include <cstdio> // snprintf
  6380. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6381. #include <initializer_list> // initializer_list
  6382. #include <string> // char_traits, string
  6383. #include <utility> // move
  6384. #include <vector> // vector
  6385. // #include <nlohmann/detail/input/input_adapters.hpp>
  6386. // #include <nlohmann/detail/input/position_t.hpp>
  6387. // #include <nlohmann/detail/macro_scope.hpp>
  6388. // #include <nlohmann/detail/meta/type_traits.hpp>
  6389. NLOHMANN_JSON_NAMESPACE_BEGIN
  6390. namespace detail
  6391. {
  6392. ///////////
  6393. // lexer //
  6394. ///////////
  6395. template<typename BasicJsonType>
  6396. class lexer_base
  6397. {
  6398. public:
  6399. /// token types for the parser
  6400. enum class token_type
  6401. {
  6402. uninitialized, ///< indicating the scanner is uninitialized
  6403. literal_true, ///< the `true` literal
  6404. literal_false, ///< the `false` literal
  6405. literal_null, ///< the `null` literal
  6406. value_string, ///< a string -- use get_string() for actual value
  6407. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6408. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6409. value_float, ///< an floating point number -- use get_number_float() for actual value
  6410. begin_array, ///< the character for array begin `[`
  6411. begin_object, ///< the character for object begin `{`
  6412. end_array, ///< the character for array end `]`
  6413. end_object, ///< the character for object end `}`
  6414. name_separator, ///< the name separator `:`
  6415. value_separator, ///< the value separator `,`
  6416. parse_error, ///< indicating a parse error
  6417. end_of_input, ///< indicating the end of the input buffer
  6418. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6419. };
  6420. /// return name of values of type token_type (only used for errors)
  6421. JSON_HEDLEY_RETURNS_NON_NULL
  6422. JSON_HEDLEY_CONST
  6423. static const char* token_type_name(const token_type t) noexcept
  6424. {
  6425. switch (t)
  6426. {
  6427. case token_type::uninitialized:
  6428. return "<uninitialized>";
  6429. case token_type::literal_true:
  6430. return "true literal";
  6431. case token_type::literal_false:
  6432. return "false literal";
  6433. case token_type::literal_null:
  6434. return "null literal";
  6435. case token_type::value_string:
  6436. return "string literal";
  6437. case token_type::value_unsigned:
  6438. case token_type::value_integer:
  6439. case token_type::value_float:
  6440. return "number literal";
  6441. case token_type::begin_array:
  6442. return "'['";
  6443. case token_type::begin_object:
  6444. return "'{'";
  6445. case token_type::end_array:
  6446. return "']'";
  6447. case token_type::end_object:
  6448. return "'}'";
  6449. case token_type::name_separator:
  6450. return "':'";
  6451. case token_type::value_separator:
  6452. return "','";
  6453. case token_type::parse_error:
  6454. return "<parse error>";
  6455. case token_type::end_of_input:
  6456. return "end of input";
  6457. case token_type::literal_or_value:
  6458. return "'[', '{', or a literal";
  6459. // LCOV_EXCL_START
  6460. default: // catch non-enum values
  6461. return "unknown token";
  6462. // LCOV_EXCL_STOP
  6463. }
  6464. }
  6465. };
  6466. /*!
  6467. @brief lexical analysis
  6468. This class organizes the lexical analysis during JSON deserialization.
  6469. */
  6470. template<typename BasicJsonType, typename InputAdapterType>
  6471. class lexer : public lexer_base<BasicJsonType>
  6472. {
  6473. using number_integer_t = typename BasicJsonType::number_integer_t;
  6474. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6475. using number_float_t = typename BasicJsonType::number_float_t;
  6476. using string_t = typename BasicJsonType::string_t;
  6477. using char_type = typename InputAdapterType::char_type;
  6478. using char_int_type = typename char_traits<char_type>::int_type;
  6479. public:
  6480. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6481. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6482. : ia(std::move(adapter))
  6483. , ignore_comments(ignore_comments_)
  6484. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6485. {}
  6486. // delete because of pointer members
  6487. lexer(const lexer&) = delete;
  6488. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6489. lexer& operator=(lexer&) = delete;
  6490. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6491. ~lexer() = default;
  6492. private:
  6493. /////////////////////
  6494. // locales
  6495. /////////////////////
  6496. /// return the locale-dependent decimal point
  6497. JSON_HEDLEY_PURE
  6498. static char get_decimal_point() noexcept
  6499. {
  6500. const auto* loc = localeconv();
  6501. JSON_ASSERT(loc != nullptr);
  6502. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6503. }
  6504. /////////////////////
  6505. // scan functions
  6506. /////////////////////
  6507. /*!
  6508. @brief get codepoint from 4 hex characters following `\u`
  6509. For input "\u c1 c2 c3 c4" the codepoint is:
  6510. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6511. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6512. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6513. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6514. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6515. between the ASCII value of the character and the desired integer value.
  6516. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6517. non-hex character)
  6518. */
  6519. int get_codepoint()
  6520. {
  6521. // this function only makes sense after reading `\u`
  6522. JSON_ASSERT(current == 'u');
  6523. int codepoint = 0;
  6524. const auto factors = { 12u, 8u, 4u, 0u };
  6525. for (const auto factor : factors)
  6526. {
  6527. get();
  6528. if (current >= '0' && current <= '9')
  6529. {
  6530. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6531. }
  6532. else if (current >= 'A' && current <= 'F')
  6533. {
  6534. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6535. }
  6536. else if (current >= 'a' && current <= 'f')
  6537. {
  6538. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6539. }
  6540. else
  6541. {
  6542. return -1;
  6543. }
  6544. }
  6545. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6546. return codepoint;
  6547. }
  6548. /*!
  6549. @brief check if the next byte(s) are inside a given range
  6550. Adds the current byte and, for each passed range, reads a new byte and
  6551. checks if it is inside the range. If a violation was detected, set up an
  6552. error message and return false. Otherwise, return true.
  6553. @param[in] ranges list of integers; interpreted as list of pairs of
  6554. inclusive lower and upper bound, respectively
  6555. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6556. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6557. @return true if and only if no range violation was detected
  6558. */
  6559. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6560. {
  6561. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6562. add(current);
  6563. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6564. {
  6565. get();
  6566. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
  6567. {
  6568. add(current);
  6569. }
  6570. else
  6571. {
  6572. error_message = "invalid string: ill-formed UTF-8 byte";
  6573. return false;
  6574. }
  6575. }
  6576. return true;
  6577. }
  6578. /*!
  6579. @brief scan a string literal
  6580. This function scans a string according to Sect. 7 of RFC 8259. While
  6581. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6582. function returns successfully, token_buffer is *not* null-terminated (as it
  6583. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6584. string.
  6585. @return token_type::value_string if string could be successfully scanned,
  6586. token_type::parse_error otherwise
  6587. @note In case of errors, variable error_message contains a textual
  6588. description.
  6589. */
  6590. token_type scan_string()
  6591. {
  6592. // reset token_buffer (ignore opening quote)
  6593. reset();
  6594. // we entered the function by reading an open quote
  6595. JSON_ASSERT(current == '\"');
  6596. while (true)
  6597. {
  6598. // get next character
  6599. switch (get())
  6600. {
  6601. // end of file while parsing string
  6602. case char_traits<char_type>::eof():
  6603. {
  6604. error_message = "invalid string: missing closing quote";
  6605. return token_type::parse_error;
  6606. }
  6607. // closing quote
  6608. case '\"':
  6609. {
  6610. return token_type::value_string;
  6611. }
  6612. // escapes
  6613. case '\\':
  6614. {
  6615. switch (get())
  6616. {
  6617. // quotation mark
  6618. case '\"':
  6619. add('\"');
  6620. break;
  6621. // reverse solidus
  6622. case '\\':
  6623. add('\\');
  6624. break;
  6625. // solidus
  6626. case '/':
  6627. add('/');
  6628. break;
  6629. // backspace
  6630. case 'b':
  6631. add('\b');
  6632. break;
  6633. // form feed
  6634. case 'f':
  6635. add('\f');
  6636. break;
  6637. // line feed
  6638. case 'n':
  6639. add('\n');
  6640. break;
  6641. // carriage return
  6642. case 'r':
  6643. add('\r');
  6644. break;
  6645. // tab
  6646. case 't':
  6647. add('\t');
  6648. break;
  6649. // unicode escapes
  6650. case 'u':
  6651. {
  6652. const int codepoint1 = get_codepoint();
  6653. int codepoint = codepoint1; // start with codepoint1
  6654. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6655. {
  6656. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6657. return token_type::parse_error;
  6658. }
  6659. // check if code point is a high surrogate
  6660. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6661. {
  6662. // expect next \uxxxx entry
  6663. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6664. {
  6665. const int codepoint2 = get_codepoint();
  6666. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6667. {
  6668. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6669. return token_type::parse_error;
  6670. }
  6671. // check if codepoint2 is a low surrogate
  6672. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6673. {
  6674. // overwrite codepoint
  6675. codepoint = static_cast<int>(
  6676. // high surrogate occupies the most significant 22 bits
  6677. (static_cast<unsigned int>(codepoint1) << 10u)
  6678. // low surrogate occupies the least significant 15 bits
  6679. + static_cast<unsigned int>(codepoint2)
  6680. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6681. // in the result, so we have to subtract with:
  6682. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6683. - 0x35FDC00u);
  6684. }
  6685. else
  6686. {
  6687. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6688. return token_type::parse_error;
  6689. }
  6690. }
  6691. else
  6692. {
  6693. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6694. return token_type::parse_error;
  6695. }
  6696. }
  6697. else
  6698. {
  6699. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6700. {
  6701. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6702. return token_type::parse_error;
  6703. }
  6704. }
  6705. // result of the above calculation yields a proper codepoint
  6706. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6707. // translate codepoint into bytes
  6708. if (codepoint < 0x80)
  6709. {
  6710. // 1-byte characters: 0xxxxxxx (ASCII)
  6711. add(static_cast<char_int_type>(codepoint));
  6712. }
  6713. else if (codepoint <= 0x7FF)
  6714. {
  6715. // 2-byte characters: 110xxxxx 10xxxxxx
  6716. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6717. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6718. }
  6719. else if (codepoint <= 0xFFFF)
  6720. {
  6721. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6722. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6723. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6724. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6725. }
  6726. else
  6727. {
  6728. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6729. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6730. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6731. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6732. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6733. }
  6734. break;
  6735. }
  6736. // other characters after escape
  6737. default:
  6738. error_message = "invalid string: forbidden character after backslash";
  6739. return token_type::parse_error;
  6740. }
  6741. break;
  6742. }
  6743. // invalid control characters
  6744. case 0x00:
  6745. {
  6746. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6747. return token_type::parse_error;
  6748. }
  6749. case 0x01:
  6750. {
  6751. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6752. return token_type::parse_error;
  6753. }
  6754. case 0x02:
  6755. {
  6756. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6757. return token_type::parse_error;
  6758. }
  6759. case 0x03:
  6760. {
  6761. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6762. return token_type::parse_error;
  6763. }
  6764. case 0x04:
  6765. {
  6766. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6767. return token_type::parse_error;
  6768. }
  6769. case 0x05:
  6770. {
  6771. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6772. return token_type::parse_error;
  6773. }
  6774. case 0x06:
  6775. {
  6776. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6777. return token_type::parse_error;
  6778. }
  6779. case 0x07:
  6780. {
  6781. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6782. return token_type::parse_error;
  6783. }
  6784. case 0x08:
  6785. {
  6786. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6787. return token_type::parse_error;
  6788. }
  6789. case 0x09:
  6790. {
  6791. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6792. return token_type::parse_error;
  6793. }
  6794. case 0x0A:
  6795. {
  6796. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6797. return token_type::parse_error;
  6798. }
  6799. case 0x0B:
  6800. {
  6801. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6802. return token_type::parse_error;
  6803. }
  6804. case 0x0C:
  6805. {
  6806. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6807. return token_type::parse_error;
  6808. }
  6809. case 0x0D:
  6810. {
  6811. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6812. return token_type::parse_error;
  6813. }
  6814. case 0x0E:
  6815. {
  6816. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6817. return token_type::parse_error;
  6818. }
  6819. case 0x0F:
  6820. {
  6821. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6822. return token_type::parse_error;
  6823. }
  6824. case 0x10:
  6825. {
  6826. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6827. return token_type::parse_error;
  6828. }
  6829. case 0x11:
  6830. {
  6831. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6832. return token_type::parse_error;
  6833. }
  6834. case 0x12:
  6835. {
  6836. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6837. return token_type::parse_error;
  6838. }
  6839. case 0x13:
  6840. {
  6841. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6842. return token_type::parse_error;
  6843. }
  6844. case 0x14:
  6845. {
  6846. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6847. return token_type::parse_error;
  6848. }
  6849. case 0x15:
  6850. {
  6851. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6852. return token_type::parse_error;
  6853. }
  6854. case 0x16:
  6855. {
  6856. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6857. return token_type::parse_error;
  6858. }
  6859. case 0x17:
  6860. {
  6861. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6862. return token_type::parse_error;
  6863. }
  6864. case 0x18:
  6865. {
  6866. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6867. return token_type::parse_error;
  6868. }
  6869. case 0x19:
  6870. {
  6871. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6872. return token_type::parse_error;
  6873. }
  6874. case 0x1A:
  6875. {
  6876. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6877. return token_type::parse_error;
  6878. }
  6879. case 0x1B:
  6880. {
  6881. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6882. return token_type::parse_error;
  6883. }
  6884. case 0x1C:
  6885. {
  6886. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6887. return token_type::parse_error;
  6888. }
  6889. case 0x1D:
  6890. {
  6891. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6892. return token_type::parse_error;
  6893. }
  6894. case 0x1E:
  6895. {
  6896. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6897. return token_type::parse_error;
  6898. }
  6899. case 0x1F:
  6900. {
  6901. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6902. return token_type::parse_error;
  6903. }
  6904. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6905. case 0x20:
  6906. case 0x21:
  6907. case 0x23:
  6908. case 0x24:
  6909. case 0x25:
  6910. case 0x26:
  6911. case 0x27:
  6912. case 0x28:
  6913. case 0x29:
  6914. case 0x2A:
  6915. case 0x2B:
  6916. case 0x2C:
  6917. case 0x2D:
  6918. case 0x2E:
  6919. case 0x2F:
  6920. case 0x30:
  6921. case 0x31:
  6922. case 0x32:
  6923. case 0x33:
  6924. case 0x34:
  6925. case 0x35:
  6926. case 0x36:
  6927. case 0x37:
  6928. case 0x38:
  6929. case 0x39:
  6930. case 0x3A:
  6931. case 0x3B:
  6932. case 0x3C:
  6933. case 0x3D:
  6934. case 0x3E:
  6935. case 0x3F:
  6936. case 0x40:
  6937. case 0x41:
  6938. case 0x42:
  6939. case 0x43:
  6940. case 0x44:
  6941. case 0x45:
  6942. case 0x46:
  6943. case 0x47:
  6944. case 0x48:
  6945. case 0x49:
  6946. case 0x4A:
  6947. case 0x4B:
  6948. case 0x4C:
  6949. case 0x4D:
  6950. case 0x4E:
  6951. case 0x4F:
  6952. case 0x50:
  6953. case 0x51:
  6954. case 0x52:
  6955. case 0x53:
  6956. case 0x54:
  6957. case 0x55:
  6958. case 0x56:
  6959. case 0x57:
  6960. case 0x58:
  6961. case 0x59:
  6962. case 0x5A:
  6963. case 0x5B:
  6964. case 0x5D:
  6965. case 0x5E:
  6966. case 0x5F:
  6967. case 0x60:
  6968. case 0x61:
  6969. case 0x62:
  6970. case 0x63:
  6971. case 0x64:
  6972. case 0x65:
  6973. case 0x66:
  6974. case 0x67:
  6975. case 0x68:
  6976. case 0x69:
  6977. case 0x6A:
  6978. case 0x6B:
  6979. case 0x6C:
  6980. case 0x6D:
  6981. case 0x6E:
  6982. case 0x6F:
  6983. case 0x70:
  6984. case 0x71:
  6985. case 0x72:
  6986. case 0x73:
  6987. case 0x74:
  6988. case 0x75:
  6989. case 0x76:
  6990. case 0x77:
  6991. case 0x78:
  6992. case 0x79:
  6993. case 0x7A:
  6994. case 0x7B:
  6995. case 0x7C:
  6996. case 0x7D:
  6997. case 0x7E:
  6998. case 0x7F:
  6999. {
  7000. add(current);
  7001. break;
  7002. }
  7003. // U+0080..U+07FF: bytes C2..DF 80..BF
  7004. case 0xC2:
  7005. case 0xC3:
  7006. case 0xC4:
  7007. case 0xC5:
  7008. case 0xC6:
  7009. case 0xC7:
  7010. case 0xC8:
  7011. case 0xC9:
  7012. case 0xCA:
  7013. case 0xCB:
  7014. case 0xCC:
  7015. case 0xCD:
  7016. case 0xCE:
  7017. case 0xCF:
  7018. case 0xD0:
  7019. case 0xD1:
  7020. case 0xD2:
  7021. case 0xD3:
  7022. case 0xD4:
  7023. case 0xD5:
  7024. case 0xD6:
  7025. case 0xD7:
  7026. case 0xD8:
  7027. case 0xD9:
  7028. case 0xDA:
  7029. case 0xDB:
  7030. case 0xDC:
  7031. case 0xDD:
  7032. case 0xDE:
  7033. case 0xDF:
  7034. {
  7035. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  7036. {
  7037. return token_type::parse_error;
  7038. }
  7039. break;
  7040. }
  7041. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  7042. case 0xE0:
  7043. {
  7044. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  7045. {
  7046. return token_type::parse_error;
  7047. }
  7048. break;
  7049. }
  7050. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  7051. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  7052. case 0xE1:
  7053. case 0xE2:
  7054. case 0xE3:
  7055. case 0xE4:
  7056. case 0xE5:
  7057. case 0xE6:
  7058. case 0xE7:
  7059. case 0xE8:
  7060. case 0xE9:
  7061. case 0xEA:
  7062. case 0xEB:
  7063. case 0xEC:
  7064. case 0xEE:
  7065. case 0xEF:
  7066. {
  7067. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  7068. {
  7069. return token_type::parse_error;
  7070. }
  7071. break;
  7072. }
  7073. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  7074. case 0xED:
  7075. {
  7076. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  7077. {
  7078. return token_type::parse_error;
  7079. }
  7080. break;
  7081. }
  7082. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  7083. case 0xF0:
  7084. {
  7085. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7086. {
  7087. return token_type::parse_error;
  7088. }
  7089. break;
  7090. }
  7091. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  7092. case 0xF1:
  7093. case 0xF2:
  7094. case 0xF3:
  7095. {
  7096. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7097. {
  7098. return token_type::parse_error;
  7099. }
  7100. break;
  7101. }
  7102. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  7103. case 0xF4:
  7104. {
  7105. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  7106. {
  7107. return token_type::parse_error;
  7108. }
  7109. break;
  7110. }
  7111. // remaining bytes (80..C1 and F5..FF) are ill-formed
  7112. default:
  7113. {
  7114. error_message = "invalid string: ill-formed UTF-8 byte";
  7115. return token_type::parse_error;
  7116. }
  7117. }
  7118. }
  7119. }
  7120. /*!
  7121. * @brief scan a comment
  7122. * @return whether comment could be scanned successfully
  7123. */
  7124. bool scan_comment()
  7125. {
  7126. switch (get())
  7127. {
  7128. // single-line comments skip input until a newline or EOF is read
  7129. case '/':
  7130. {
  7131. while (true)
  7132. {
  7133. switch (get())
  7134. {
  7135. case '\n':
  7136. case '\r':
  7137. case char_traits<char_type>::eof():
  7138. case '\0':
  7139. return true;
  7140. default:
  7141. break;
  7142. }
  7143. }
  7144. }
  7145. // multi-line comments skip input until */ is read
  7146. case '*':
  7147. {
  7148. while (true)
  7149. {
  7150. switch (get())
  7151. {
  7152. case char_traits<char_type>::eof():
  7153. case '\0':
  7154. {
  7155. error_message = "invalid comment; missing closing '*/'";
  7156. return false;
  7157. }
  7158. case '*':
  7159. {
  7160. switch (get())
  7161. {
  7162. case '/':
  7163. return true;
  7164. default:
  7165. {
  7166. unget();
  7167. continue;
  7168. }
  7169. }
  7170. }
  7171. default:
  7172. continue;
  7173. }
  7174. }
  7175. }
  7176. // unexpected character after reading '/'
  7177. default:
  7178. {
  7179. error_message = "invalid comment; expecting '/' or '*' after '/'";
  7180. return false;
  7181. }
  7182. }
  7183. }
  7184. JSON_HEDLEY_NON_NULL(2)
  7185. static void strtof(float& f, const char* str, char** endptr) noexcept
  7186. {
  7187. f = std::strtof(str, endptr);
  7188. }
  7189. JSON_HEDLEY_NON_NULL(2)
  7190. static void strtof(double& f, const char* str, char** endptr) noexcept
  7191. {
  7192. f = std::strtod(str, endptr);
  7193. }
  7194. JSON_HEDLEY_NON_NULL(2)
  7195. static void strtof(long double& f, const char* str, char** endptr) noexcept
  7196. {
  7197. f = std::strtold(str, endptr);
  7198. }
  7199. /*!
  7200. @brief scan a number literal
  7201. This function scans a string according to Sect. 6 of RFC 8259.
  7202. The function is realized with a deterministic finite state machine derived
  7203. from the grammar described in RFC 8259. Starting in state "init", the
  7204. input is read and used to determined the next state. Only state "done"
  7205. accepts the number. State "error" is a trap state to model errors. In the
  7206. table below, "anything" means any character but the ones listed before.
  7207. state | 0 | 1-9 | e E | + | - | . | anything
  7208. ---------|----------|----------|----------|---------|---------|----------|-----------
  7209. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  7210. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  7211. zero | done | done | exponent | done | done | decimal1 | done
  7212. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  7213. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  7214. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  7215. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  7216. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  7217. any2 | any2 | any2 | done | done | done | done | done
  7218. The state machine is realized with one label per state (prefixed with
  7219. "scan_number_") and `goto` statements between them. The state machine
  7220. contains cycles, but any cycle can be left when EOF is read. Therefore,
  7221. the function is guaranteed to terminate.
  7222. During scanning, the read bytes are stored in token_buffer. This string is
  7223. then converted to a signed integer, an unsigned integer, or a
  7224. floating-point number.
  7225. @return token_type::value_unsigned, token_type::value_integer, or
  7226. token_type::value_float if number could be successfully scanned,
  7227. token_type::parse_error otherwise
  7228. @note The scanner is independent of the current locale. Internally, the
  7229. locale's decimal point is used instead of `.` to work with the
  7230. locale-dependent converters.
  7231. */
  7232. token_type scan_number() // lgtm [cpp/use-of-goto]
  7233. {
  7234. // reset token_buffer to store the number's bytes
  7235. reset();
  7236. // the type of the parsed number; initially set to unsigned; will be
  7237. // changed if minus sign, decimal point or exponent is read
  7238. token_type number_type = token_type::value_unsigned;
  7239. // state (init): we just found out we need to scan a number
  7240. switch (current)
  7241. {
  7242. case '-':
  7243. {
  7244. add(current);
  7245. goto scan_number_minus;
  7246. }
  7247. case '0':
  7248. {
  7249. add(current);
  7250. goto scan_number_zero;
  7251. }
  7252. case '1':
  7253. case '2':
  7254. case '3':
  7255. case '4':
  7256. case '5':
  7257. case '6':
  7258. case '7':
  7259. case '8':
  7260. case '9':
  7261. {
  7262. add(current);
  7263. goto scan_number_any1;
  7264. }
  7265. // all other characters are rejected outside scan_number()
  7266. default: // LCOV_EXCL_LINE
  7267. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7268. }
  7269. scan_number_minus:
  7270. // state: we just parsed a leading minus sign
  7271. number_type = token_type::value_integer;
  7272. switch (get())
  7273. {
  7274. case '0':
  7275. {
  7276. add(current);
  7277. goto scan_number_zero;
  7278. }
  7279. case '1':
  7280. case '2':
  7281. case '3':
  7282. case '4':
  7283. case '5':
  7284. case '6':
  7285. case '7':
  7286. case '8':
  7287. case '9':
  7288. {
  7289. add(current);
  7290. goto scan_number_any1;
  7291. }
  7292. default:
  7293. {
  7294. error_message = "invalid number; expected digit after '-'";
  7295. return token_type::parse_error;
  7296. }
  7297. }
  7298. scan_number_zero:
  7299. // state: we just parse a zero (maybe with a leading minus sign)
  7300. switch (get())
  7301. {
  7302. case '.':
  7303. {
  7304. add(decimal_point_char);
  7305. goto scan_number_decimal1;
  7306. }
  7307. case 'e':
  7308. case 'E':
  7309. {
  7310. add(current);
  7311. goto scan_number_exponent;
  7312. }
  7313. default:
  7314. goto scan_number_done;
  7315. }
  7316. scan_number_any1:
  7317. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7318. switch (get())
  7319. {
  7320. case '0':
  7321. case '1':
  7322. case '2':
  7323. case '3':
  7324. case '4':
  7325. case '5':
  7326. case '6':
  7327. case '7':
  7328. case '8':
  7329. case '9':
  7330. {
  7331. add(current);
  7332. goto scan_number_any1;
  7333. }
  7334. case '.':
  7335. {
  7336. add(decimal_point_char);
  7337. goto scan_number_decimal1;
  7338. }
  7339. case 'e':
  7340. case 'E':
  7341. {
  7342. add(current);
  7343. goto scan_number_exponent;
  7344. }
  7345. default:
  7346. goto scan_number_done;
  7347. }
  7348. scan_number_decimal1:
  7349. // state: we just parsed a decimal point
  7350. number_type = token_type::value_float;
  7351. switch (get())
  7352. {
  7353. case '0':
  7354. case '1':
  7355. case '2':
  7356. case '3':
  7357. case '4':
  7358. case '5':
  7359. case '6':
  7360. case '7':
  7361. case '8':
  7362. case '9':
  7363. {
  7364. add(current);
  7365. goto scan_number_decimal2;
  7366. }
  7367. default:
  7368. {
  7369. error_message = "invalid number; expected digit after '.'";
  7370. return token_type::parse_error;
  7371. }
  7372. }
  7373. scan_number_decimal2:
  7374. // we just parsed at least one number after a decimal point
  7375. switch (get())
  7376. {
  7377. case '0':
  7378. case '1':
  7379. case '2':
  7380. case '3':
  7381. case '4':
  7382. case '5':
  7383. case '6':
  7384. case '7':
  7385. case '8':
  7386. case '9':
  7387. {
  7388. add(current);
  7389. goto scan_number_decimal2;
  7390. }
  7391. case 'e':
  7392. case 'E':
  7393. {
  7394. add(current);
  7395. goto scan_number_exponent;
  7396. }
  7397. default:
  7398. goto scan_number_done;
  7399. }
  7400. scan_number_exponent:
  7401. // we just parsed an exponent
  7402. number_type = token_type::value_float;
  7403. switch (get())
  7404. {
  7405. case '+':
  7406. case '-':
  7407. {
  7408. add(current);
  7409. goto scan_number_sign;
  7410. }
  7411. case '0':
  7412. case '1':
  7413. case '2':
  7414. case '3':
  7415. case '4':
  7416. case '5':
  7417. case '6':
  7418. case '7':
  7419. case '8':
  7420. case '9':
  7421. {
  7422. add(current);
  7423. goto scan_number_any2;
  7424. }
  7425. default:
  7426. {
  7427. error_message =
  7428. "invalid number; expected '+', '-', or digit after exponent";
  7429. return token_type::parse_error;
  7430. }
  7431. }
  7432. scan_number_sign:
  7433. // we just parsed an exponent sign
  7434. switch (get())
  7435. {
  7436. case '0':
  7437. case '1':
  7438. case '2':
  7439. case '3':
  7440. case '4':
  7441. case '5':
  7442. case '6':
  7443. case '7':
  7444. case '8':
  7445. case '9':
  7446. {
  7447. add(current);
  7448. goto scan_number_any2;
  7449. }
  7450. default:
  7451. {
  7452. error_message = "invalid number; expected digit after exponent sign";
  7453. return token_type::parse_error;
  7454. }
  7455. }
  7456. scan_number_any2:
  7457. // we just parsed a number after the exponent or exponent sign
  7458. switch (get())
  7459. {
  7460. case '0':
  7461. case '1':
  7462. case '2':
  7463. case '3':
  7464. case '4':
  7465. case '5':
  7466. case '6':
  7467. case '7':
  7468. case '8':
  7469. case '9':
  7470. {
  7471. add(current);
  7472. goto scan_number_any2;
  7473. }
  7474. default:
  7475. goto scan_number_done;
  7476. }
  7477. scan_number_done:
  7478. // unget the character after the number (we only read it to know that
  7479. // we are done scanning a number)
  7480. unget();
  7481. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7482. errno = 0;
  7483. // try to parse integers first and fall back to floats
  7484. if (number_type == token_type::value_unsigned)
  7485. {
  7486. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7487. // we checked the number format before
  7488. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7489. if (errno == 0)
  7490. {
  7491. value_unsigned = static_cast<number_unsigned_t>(x);
  7492. if (value_unsigned == x)
  7493. {
  7494. return token_type::value_unsigned;
  7495. }
  7496. }
  7497. }
  7498. else if (number_type == token_type::value_integer)
  7499. {
  7500. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7501. // we checked the number format before
  7502. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7503. if (errno == 0)
  7504. {
  7505. value_integer = static_cast<number_integer_t>(x);
  7506. if (value_integer == x)
  7507. {
  7508. return token_type::value_integer;
  7509. }
  7510. }
  7511. }
  7512. // this code is reached if we parse a floating-point number or if an
  7513. // integer conversion above failed
  7514. strtof(value_float, token_buffer.data(), &endptr);
  7515. // we checked the number format before
  7516. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7517. return token_type::value_float;
  7518. }
  7519. /*!
  7520. @param[in] literal_text the literal text to expect
  7521. @param[in] length the length of the passed literal text
  7522. @param[in] return_type the token type to return on success
  7523. */
  7524. JSON_HEDLEY_NON_NULL(2)
  7525. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7526. token_type return_type)
  7527. {
  7528. JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7529. for (std::size_t i = 1; i < length; ++i)
  7530. {
  7531. if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7532. {
  7533. error_message = "invalid literal";
  7534. return token_type::parse_error;
  7535. }
  7536. }
  7537. return return_type;
  7538. }
  7539. /////////////////////
  7540. // input management
  7541. /////////////////////
  7542. /// reset token_buffer; current character is beginning of token
  7543. void reset() noexcept
  7544. {
  7545. token_buffer.clear();
  7546. token_string.clear();
  7547. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7548. }
  7549. /*
  7550. @brief get next character from the input
  7551. This function provides the interface to the used input adapter. It does
  7552. not throw in case the input reached EOF, but returns a
  7553. `char_traits<char>::eof()` in that case. Stores the scanned characters
  7554. for use in error messages.
  7555. @return character read from the input
  7556. */
  7557. char_int_type get()
  7558. {
  7559. ++position.chars_read_total;
  7560. ++position.chars_read_current_line;
  7561. if (next_unget)
  7562. {
  7563. // just reset the next_unget variable and work with current
  7564. next_unget = false;
  7565. }
  7566. else
  7567. {
  7568. current = ia.get_character();
  7569. }
  7570. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7571. {
  7572. token_string.push_back(char_traits<char_type>::to_char_type(current));
  7573. }
  7574. if (current == '\n')
  7575. {
  7576. ++position.lines_read;
  7577. position.chars_read_current_line = 0;
  7578. }
  7579. return current;
  7580. }
  7581. /*!
  7582. @brief unget current character (read it again on next get)
  7583. We implement unget by setting variable next_unget to true. The input is not
  7584. changed - we just simulate ungetting by modifying chars_read_total,
  7585. chars_read_current_line, and token_string. The next call to get() will
  7586. behave as if the unget character is read again.
  7587. */
  7588. void unget()
  7589. {
  7590. next_unget = true;
  7591. --position.chars_read_total;
  7592. // in case we "unget" a newline, we have to also decrement the lines_read
  7593. if (position.chars_read_current_line == 0)
  7594. {
  7595. if (position.lines_read > 0)
  7596. {
  7597. --position.lines_read;
  7598. }
  7599. }
  7600. else
  7601. {
  7602. --position.chars_read_current_line;
  7603. }
  7604. if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
  7605. {
  7606. JSON_ASSERT(!token_string.empty());
  7607. token_string.pop_back();
  7608. }
  7609. }
  7610. /// add a character to token_buffer
  7611. void add(char_int_type c)
  7612. {
  7613. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7614. }
  7615. public:
  7616. /////////////////////
  7617. // value getters
  7618. /////////////////////
  7619. /// return integer value
  7620. constexpr number_integer_t get_number_integer() const noexcept
  7621. {
  7622. return value_integer;
  7623. }
  7624. /// return unsigned integer value
  7625. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7626. {
  7627. return value_unsigned;
  7628. }
  7629. /// return floating-point value
  7630. constexpr number_float_t get_number_float() const noexcept
  7631. {
  7632. return value_float;
  7633. }
  7634. /// return current string value (implicitly resets the token; useful only once)
  7635. string_t& get_string()
  7636. {
  7637. return token_buffer;
  7638. }
  7639. /////////////////////
  7640. // diagnostics
  7641. /////////////////////
  7642. /// return position of last read token
  7643. constexpr position_t get_position() const noexcept
  7644. {
  7645. return position;
  7646. }
  7647. /// return the last read token (for errors only). Will never contain EOF
  7648. /// (an arbitrary value that is not a valid char value, often -1), because
  7649. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7650. std::string get_token_string() const
  7651. {
  7652. // escape control characters
  7653. std::string result;
  7654. for (const auto c : token_string)
  7655. {
  7656. if (static_cast<unsigned char>(c) <= '\x1F')
  7657. {
  7658. // escape control characters
  7659. std::array<char, 9> cs{{}};
  7660. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7661. result += cs.data();
  7662. }
  7663. else
  7664. {
  7665. // add character as is
  7666. result.push_back(static_cast<std::string::value_type>(c));
  7667. }
  7668. }
  7669. return result;
  7670. }
  7671. /// return syntax error message
  7672. JSON_HEDLEY_RETURNS_NON_NULL
  7673. constexpr const char* get_error_message() const noexcept
  7674. {
  7675. return error_message;
  7676. }
  7677. /////////////////////
  7678. // actual scanner
  7679. /////////////////////
  7680. /*!
  7681. @brief skip the UTF-8 byte order mark
  7682. @return true iff there is no BOM or the correct BOM has been skipped
  7683. */
  7684. bool skip_bom()
  7685. {
  7686. if (get() == 0xEF)
  7687. {
  7688. // check if we completely parse the BOM
  7689. return get() == 0xBB && get() == 0xBF;
  7690. }
  7691. // the first character is not the beginning of the BOM; unget it to
  7692. // process is later
  7693. unget();
  7694. return true;
  7695. }
  7696. void skip_whitespace()
  7697. {
  7698. do
  7699. {
  7700. get();
  7701. }
  7702. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7703. }
  7704. token_type scan()
  7705. {
  7706. // initially, skip the BOM
  7707. if (position.chars_read_total == 0 && !skip_bom())
  7708. {
  7709. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7710. return token_type::parse_error;
  7711. }
  7712. // read next character and ignore whitespace
  7713. skip_whitespace();
  7714. // ignore comments
  7715. while (ignore_comments && current == '/')
  7716. {
  7717. if (!scan_comment())
  7718. {
  7719. return token_type::parse_error;
  7720. }
  7721. // skip following whitespace
  7722. skip_whitespace();
  7723. }
  7724. switch (current)
  7725. {
  7726. // structural characters
  7727. case '[':
  7728. return token_type::begin_array;
  7729. case ']':
  7730. return token_type::end_array;
  7731. case '{':
  7732. return token_type::begin_object;
  7733. case '}':
  7734. return token_type::end_object;
  7735. case ':':
  7736. return token_type::name_separator;
  7737. case ',':
  7738. return token_type::value_separator;
  7739. // literals
  7740. case 't':
  7741. {
  7742. 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')}};
  7743. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7744. }
  7745. case 'f':
  7746. {
  7747. 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')}};
  7748. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7749. }
  7750. case 'n':
  7751. {
  7752. 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')}};
  7753. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7754. }
  7755. // string
  7756. case '\"':
  7757. return scan_string();
  7758. // number
  7759. case '-':
  7760. case '0':
  7761. case '1':
  7762. case '2':
  7763. case '3':
  7764. case '4':
  7765. case '5':
  7766. case '6':
  7767. case '7':
  7768. case '8':
  7769. case '9':
  7770. return scan_number();
  7771. // end of input (the null byte is needed when parsing from
  7772. // string literals)
  7773. case '\0':
  7774. case char_traits<char_type>::eof():
  7775. return token_type::end_of_input;
  7776. // error
  7777. default:
  7778. error_message = "invalid literal";
  7779. return token_type::parse_error;
  7780. }
  7781. }
  7782. private:
  7783. /// input adapter
  7784. InputAdapterType ia;
  7785. /// whether comments should be ignored (true) or signaled as errors (false)
  7786. const bool ignore_comments = false;
  7787. /// the current character
  7788. char_int_type current = char_traits<char_type>::eof();
  7789. /// whether the next get() call should just return current
  7790. bool next_unget = false;
  7791. /// the start position of the current token
  7792. position_t position {};
  7793. /// raw input token string (for error messages)
  7794. std::vector<char_type> token_string {};
  7795. /// buffer for variable-length tokens (numbers, strings)
  7796. string_t token_buffer {};
  7797. /// a description of occurred lexer errors
  7798. const char* error_message = "";
  7799. // number values
  7800. number_integer_t value_integer = 0;
  7801. number_unsigned_t value_unsigned = 0;
  7802. number_float_t value_float = 0;
  7803. /// the decimal point
  7804. const char_int_type decimal_point_char = '.';
  7805. };
  7806. } // namespace detail
  7807. NLOHMANN_JSON_NAMESPACE_END
  7808. // #include <nlohmann/detail/macro_scope.hpp>
  7809. // #include <nlohmann/detail/meta/is_sax.hpp>
  7810. // __ _____ _____ _____
  7811. // __| | __| | | | JSON for Modern C++
  7812. // | | |__ | | | | | | version 3.11.3
  7813. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  7814. //
  7815. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7816. // SPDX-License-Identifier: MIT
  7817. #include <cstdint> // size_t
  7818. #include <utility> // declval
  7819. #include <string> // string
  7820. // #include <nlohmann/detail/abi_macros.hpp>
  7821. // #include <nlohmann/detail/meta/detected.hpp>
  7822. // #include <nlohmann/detail/meta/type_traits.hpp>
  7823. NLOHMANN_JSON_NAMESPACE_BEGIN
  7824. namespace detail
  7825. {
  7826. template<typename T>
  7827. using null_function_t = decltype(std::declval<T&>().null());
  7828. template<typename T>
  7829. using boolean_function_t =
  7830. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7831. template<typename T, typename Integer>
  7832. using number_integer_function_t =
  7833. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7834. template<typename T, typename Unsigned>
  7835. using number_unsigned_function_t =
  7836. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7837. template<typename T, typename Float, typename String>
  7838. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7839. std::declval<Float>(), std::declval<const String&>()));
  7840. template<typename T, typename String>
  7841. using string_function_t =
  7842. decltype(std::declval<T&>().string(std::declval<String&>()));
  7843. template<typename T, typename Binary>
  7844. using binary_function_t =
  7845. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7846. template<typename T>
  7847. using start_object_function_t =
  7848. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7849. template<typename T, typename String>
  7850. using key_function_t =
  7851. decltype(std::declval<T&>().key(std::declval<String&>()));
  7852. template<typename T>
  7853. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7854. template<typename T>
  7855. using start_array_function_t =
  7856. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7857. template<typename T>
  7858. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7859. template<typename T, typename Exception>
  7860. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7861. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7862. std::declval<const Exception&>()));
  7863. template<typename SAX, typename BasicJsonType>
  7864. struct is_sax
  7865. {
  7866. private:
  7867. static_assert(is_basic_json<BasicJsonType>::value,
  7868. "BasicJsonType must be of type basic_json<...>");
  7869. using number_integer_t = typename BasicJsonType::number_integer_t;
  7870. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7871. using number_float_t = typename BasicJsonType::number_float_t;
  7872. using string_t = typename BasicJsonType::string_t;
  7873. using binary_t = typename BasicJsonType::binary_t;
  7874. using exception_t = typename BasicJsonType::exception;
  7875. public:
  7876. static constexpr bool value =
  7877. is_detected_exact<bool, null_function_t, SAX>::value &&
  7878. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7879. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7880. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7881. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7882. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7883. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7884. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7885. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7886. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7887. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7888. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7889. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7890. };
  7891. template<typename SAX, typename BasicJsonType>
  7892. struct is_sax_static_asserts
  7893. {
  7894. private:
  7895. static_assert(is_basic_json<BasicJsonType>::value,
  7896. "BasicJsonType must be of type basic_json<...>");
  7897. using number_integer_t = typename BasicJsonType::number_integer_t;
  7898. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7899. using number_float_t = typename BasicJsonType::number_float_t;
  7900. using string_t = typename BasicJsonType::string_t;
  7901. using binary_t = typename BasicJsonType::binary_t;
  7902. using exception_t = typename BasicJsonType::exception;
  7903. public:
  7904. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7905. "Missing/invalid function: bool null()");
  7906. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7907. "Missing/invalid function: bool boolean(bool)");
  7908. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7909. "Missing/invalid function: bool boolean(bool)");
  7910. static_assert(
  7911. is_detected_exact<bool, number_integer_function_t, SAX,
  7912. number_integer_t>::value,
  7913. "Missing/invalid function: bool number_integer(number_integer_t)");
  7914. static_assert(
  7915. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7916. number_unsigned_t>::value,
  7917. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7918. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7919. number_float_t, string_t>::value,
  7920. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7921. static_assert(
  7922. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7923. "Missing/invalid function: bool string(string_t&)");
  7924. static_assert(
  7925. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7926. "Missing/invalid function: bool binary(binary_t&)");
  7927. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7928. "Missing/invalid function: bool start_object(std::size_t)");
  7929. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7930. "Missing/invalid function: bool key(string_t&)");
  7931. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7932. "Missing/invalid function: bool end_object()");
  7933. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7934. "Missing/invalid function: bool start_array(std::size_t)");
  7935. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7936. "Missing/invalid function: bool end_array()");
  7937. static_assert(
  7938. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7939. "Missing/invalid function: bool parse_error(std::size_t, const "
  7940. "std::string&, const exception&)");
  7941. };
  7942. } // namespace detail
  7943. NLOHMANN_JSON_NAMESPACE_END
  7944. // #include <nlohmann/detail/meta/type_traits.hpp>
  7945. // #include <nlohmann/detail/string_concat.hpp>
  7946. // #include <nlohmann/detail/value_t.hpp>
  7947. NLOHMANN_JSON_NAMESPACE_BEGIN
  7948. namespace detail
  7949. {
  7950. /// how to treat CBOR tags
  7951. enum class cbor_tag_handler_t
  7952. {
  7953. error, ///< throw a parse_error exception in case of a tag
  7954. ignore, ///< ignore tags
  7955. store ///< store tags as binary type
  7956. };
  7957. /*!
  7958. @brief determine system byte order
  7959. @return true if and only if system's byte order is little endian
  7960. @note from https://stackoverflow.com/a/1001328/266378
  7961. */
  7962. static inline bool little_endianness(int num = 1) noexcept
  7963. {
  7964. return *reinterpret_cast<char*>(&num) == 1;
  7965. }
  7966. ///////////////////
  7967. // binary reader //
  7968. ///////////////////
  7969. /*!
  7970. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7971. */
  7972. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7973. class binary_reader
  7974. {
  7975. using number_integer_t = typename BasicJsonType::number_integer_t;
  7976. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7977. using number_float_t = typename BasicJsonType::number_float_t;
  7978. using string_t = typename BasicJsonType::string_t;
  7979. using binary_t = typename BasicJsonType::binary_t;
  7980. using json_sax_t = SAX;
  7981. using char_type = typename InputAdapterType::char_type;
  7982. using char_int_type = typename char_traits<char_type>::int_type;
  7983. public:
  7984. /*!
  7985. @brief create a binary reader
  7986. @param[in] adapter input adapter to read from
  7987. */
  7988. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  7989. {
  7990. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7991. }
  7992. // make class move-only
  7993. binary_reader(const binary_reader&) = delete;
  7994. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7995. binary_reader& operator=(const binary_reader&) = delete;
  7996. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7997. ~binary_reader() = default;
  7998. /*!
  7999. @param[in] format the binary format to parse
  8000. @param[in] sax_ a SAX event processor
  8001. @param[in] strict whether to expect the input to be consumed completed
  8002. @param[in] tag_handler how to treat CBOR tags
  8003. @return whether parsing was successful
  8004. */
  8005. JSON_HEDLEY_NON_NULL(3)
  8006. bool sax_parse(const input_format_t format,
  8007. json_sax_t* sax_,
  8008. const bool strict = true,
  8009. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  8010. {
  8011. sax = sax_;
  8012. bool result = false;
  8013. switch (format)
  8014. {
  8015. case input_format_t::bson:
  8016. result = parse_bson_internal();
  8017. break;
  8018. case input_format_t::cbor:
  8019. result = parse_cbor_internal(true, tag_handler);
  8020. break;
  8021. case input_format_t::msgpack:
  8022. result = parse_msgpack_internal();
  8023. break;
  8024. case input_format_t::ubjson:
  8025. case input_format_t::bjdata:
  8026. result = parse_ubjson_internal();
  8027. break;
  8028. case input_format_t::json: // LCOV_EXCL_LINE
  8029. default: // LCOV_EXCL_LINE
  8030. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8031. }
  8032. // strict mode: next byte must be EOF
  8033. if (result && strict)
  8034. {
  8035. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  8036. {
  8037. get_ignore_noop();
  8038. }
  8039. else
  8040. {
  8041. get();
  8042. }
  8043. if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
  8044. {
  8045. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  8046. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  8047. }
  8048. }
  8049. return result;
  8050. }
  8051. private:
  8052. //////////
  8053. // BSON //
  8054. //////////
  8055. /*!
  8056. @brief Reads in a BSON-object and passes it to the SAX-parser.
  8057. @return whether a valid BSON-value was passed to the SAX parser
  8058. */
  8059. bool parse_bson_internal()
  8060. {
  8061. std::int32_t document_size{};
  8062. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8063. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  8064. {
  8065. return false;
  8066. }
  8067. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8068. {
  8069. return false;
  8070. }
  8071. return sax->end_object();
  8072. }
  8073. /*!
  8074. @brief Parses a C-style string from the BSON input.
  8075. @param[in,out] result A reference to the string variable where the read
  8076. string is to be stored.
  8077. @return `true` if the \x00-byte indicating the end of the string was
  8078. encountered before the EOF; false` indicates an unexpected EOF.
  8079. */
  8080. bool get_bson_cstr(string_t& result)
  8081. {
  8082. auto out = std::back_inserter(result);
  8083. while (true)
  8084. {
  8085. get();
  8086. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8087. {
  8088. return false;
  8089. }
  8090. if (current == 0x00)
  8091. {
  8092. return true;
  8093. }
  8094. *out++ = static_cast<typename string_t::value_type>(current);
  8095. }
  8096. }
  8097. /*!
  8098. @brief Parses a zero-terminated string of length @a len from the BSON
  8099. input.
  8100. @param[in] len The length (including the zero-byte at the end) of the
  8101. string to be read.
  8102. @param[in,out] result A reference to the string variable where the read
  8103. string is to be stored.
  8104. @tparam NumberType The type of the length @a len
  8105. @pre len >= 1
  8106. @return `true` if the string was successfully parsed
  8107. */
  8108. template<typename NumberType>
  8109. bool get_bson_string(const NumberType len, string_t& result)
  8110. {
  8111. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8112. {
  8113. auto last_token = get_token_string();
  8114. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8115. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8116. }
  8117. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
  8118. }
  8119. /*!
  8120. @brief Parses a byte array input of length @a len from the BSON input.
  8121. @param[in] len The length of the byte array to be read.
  8122. @param[in,out] result A reference to the binary variable where the read
  8123. array is to be stored.
  8124. @tparam NumberType The type of the length @a len
  8125. @pre len >= 0
  8126. @return `true` if the byte array was successfully parsed
  8127. */
  8128. template<typename NumberType>
  8129. bool get_bson_binary(const NumberType len, binary_t& result)
  8130. {
  8131. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8132. {
  8133. auto last_token = get_token_string();
  8134. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8135. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8136. }
  8137. // All BSON binary values have a subtype
  8138. std::uint8_t subtype{};
  8139. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8140. result.set_subtype(subtype);
  8141. return get_binary(input_format_t::bson, len, result);
  8142. }
  8143. /*!
  8144. @brief Read a BSON document element of the given @a element_type.
  8145. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8146. @param[in] element_type_parse_position The position in the input stream,
  8147. where the `element_type` was read.
  8148. @warning Not all BSON element types are supported yet. An unsupported
  8149. @a element_type will give rise to a parse_error.114:
  8150. Unsupported BSON record type 0x...
  8151. @return whether a valid BSON-object/array was passed to the SAX parser
  8152. */
  8153. bool parse_bson_element_internal(const char_int_type element_type,
  8154. const std::size_t element_type_parse_position)
  8155. {
  8156. switch (element_type)
  8157. {
  8158. case 0x01: // double
  8159. {
  8160. double number{};
  8161. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8162. }
  8163. case 0x02: // string
  8164. {
  8165. std::int32_t len{};
  8166. string_t value;
  8167. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8168. }
  8169. case 0x03: // object
  8170. {
  8171. return parse_bson_internal();
  8172. }
  8173. case 0x04: // array
  8174. {
  8175. return parse_bson_array();
  8176. }
  8177. case 0x05: // binary
  8178. {
  8179. std::int32_t len{};
  8180. binary_t value;
  8181. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8182. }
  8183. case 0x08: // boolean
  8184. {
  8185. return sax->boolean(get() != 0);
  8186. }
  8187. case 0x0A: // null
  8188. {
  8189. return sax->null();
  8190. }
  8191. case 0x10: // int32
  8192. {
  8193. std::int32_t value{};
  8194. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8195. }
  8196. case 0x12: // int64
  8197. {
  8198. std::int64_t value{};
  8199. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8200. }
  8201. default: // anything else not supported (yet)
  8202. {
  8203. std::array<char, 3> cr{{}};
  8204. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8205. const std::string cr_str{cr.data()};
  8206. return sax->parse_error(element_type_parse_position, cr_str,
  8207. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8208. }
  8209. }
  8210. }
  8211. /*!
  8212. @brief Read a BSON element list (as specified in the BSON-spec)
  8213. The same binary layout is used for objects and arrays, hence it must be
  8214. indicated with the argument @a is_array which one is expected
  8215. (true --> array, false --> object).
  8216. @param[in] is_array Determines if the element list being read is to be
  8217. treated as an object (@a is_array == false), or as an
  8218. array (@a is_array == true).
  8219. @return whether a valid BSON-object/array was passed to the SAX parser
  8220. */
  8221. bool parse_bson_element_list(const bool is_array)
  8222. {
  8223. string_t key;
  8224. while (auto element_type = get())
  8225. {
  8226. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8227. {
  8228. return false;
  8229. }
  8230. const std::size_t element_type_parse_position = chars_read;
  8231. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8232. {
  8233. return false;
  8234. }
  8235. if (!is_array && !sax->key(key))
  8236. {
  8237. return false;
  8238. }
  8239. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8240. {
  8241. return false;
  8242. }
  8243. // get_bson_cstr only appends
  8244. key.clear();
  8245. }
  8246. return true;
  8247. }
  8248. /*!
  8249. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8250. @return whether a valid BSON-array was passed to the SAX parser
  8251. */
  8252. bool parse_bson_array()
  8253. {
  8254. std::int32_t document_size{};
  8255. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8256. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  8257. {
  8258. return false;
  8259. }
  8260. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8261. {
  8262. return false;
  8263. }
  8264. return sax->end_array();
  8265. }
  8266. //////////
  8267. // CBOR //
  8268. //////////
  8269. /*!
  8270. @param[in] get_char whether a new character should be retrieved from the
  8271. input (true) or whether the last read character should
  8272. be considered instead (false)
  8273. @param[in] tag_handler how CBOR tags should be treated
  8274. @return whether a valid CBOR value was passed to the SAX parser
  8275. */
  8276. bool parse_cbor_internal(const bool get_char,
  8277. const cbor_tag_handler_t tag_handler)
  8278. {
  8279. switch (get_char ? get() : current)
  8280. {
  8281. // EOF
  8282. case char_traits<char_type>::eof():
  8283. return unexpect_eof(input_format_t::cbor, "value");
  8284. // Integer 0x00..0x17 (0..23)
  8285. case 0x00:
  8286. case 0x01:
  8287. case 0x02:
  8288. case 0x03:
  8289. case 0x04:
  8290. case 0x05:
  8291. case 0x06:
  8292. case 0x07:
  8293. case 0x08:
  8294. case 0x09:
  8295. case 0x0A:
  8296. case 0x0B:
  8297. case 0x0C:
  8298. case 0x0D:
  8299. case 0x0E:
  8300. case 0x0F:
  8301. case 0x10:
  8302. case 0x11:
  8303. case 0x12:
  8304. case 0x13:
  8305. case 0x14:
  8306. case 0x15:
  8307. case 0x16:
  8308. case 0x17:
  8309. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8310. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  8311. {
  8312. std::uint8_t number{};
  8313. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8314. }
  8315. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  8316. {
  8317. std::uint16_t number{};
  8318. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8319. }
  8320. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  8321. {
  8322. std::uint32_t number{};
  8323. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8324. }
  8325. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  8326. {
  8327. std::uint64_t number{};
  8328. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8329. }
  8330. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  8331. case 0x20:
  8332. case 0x21:
  8333. case 0x22:
  8334. case 0x23:
  8335. case 0x24:
  8336. case 0x25:
  8337. case 0x26:
  8338. case 0x27:
  8339. case 0x28:
  8340. case 0x29:
  8341. case 0x2A:
  8342. case 0x2B:
  8343. case 0x2C:
  8344. case 0x2D:
  8345. case 0x2E:
  8346. case 0x2F:
  8347. case 0x30:
  8348. case 0x31:
  8349. case 0x32:
  8350. case 0x33:
  8351. case 0x34:
  8352. case 0x35:
  8353. case 0x36:
  8354. case 0x37:
  8355. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8356. case 0x38: // Negative integer (one-byte uint8_t follows)
  8357. {
  8358. std::uint8_t number{};
  8359. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8360. }
  8361. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8362. {
  8363. std::uint16_t number{};
  8364. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8365. }
  8366. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8367. {
  8368. std::uint32_t number{};
  8369. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8370. }
  8371. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8372. {
  8373. std::uint64_t number{};
  8374. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8375. - static_cast<number_integer_t>(number));
  8376. }
  8377. // Binary data (0x00..0x17 bytes follow)
  8378. case 0x40:
  8379. case 0x41:
  8380. case 0x42:
  8381. case 0x43:
  8382. case 0x44:
  8383. case 0x45:
  8384. case 0x46:
  8385. case 0x47:
  8386. case 0x48:
  8387. case 0x49:
  8388. case 0x4A:
  8389. case 0x4B:
  8390. case 0x4C:
  8391. case 0x4D:
  8392. case 0x4E:
  8393. case 0x4F:
  8394. case 0x50:
  8395. case 0x51:
  8396. case 0x52:
  8397. case 0x53:
  8398. case 0x54:
  8399. case 0x55:
  8400. case 0x56:
  8401. case 0x57:
  8402. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8403. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8404. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8405. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8406. case 0x5F: // Binary data (indefinite length)
  8407. {
  8408. binary_t b;
  8409. return get_cbor_binary(b) && sax->binary(b);
  8410. }
  8411. // UTF-8 string (0x00..0x17 bytes follow)
  8412. case 0x60:
  8413. case 0x61:
  8414. case 0x62:
  8415. case 0x63:
  8416. case 0x64:
  8417. case 0x65:
  8418. case 0x66:
  8419. case 0x67:
  8420. case 0x68:
  8421. case 0x69:
  8422. case 0x6A:
  8423. case 0x6B:
  8424. case 0x6C:
  8425. case 0x6D:
  8426. case 0x6E:
  8427. case 0x6F:
  8428. case 0x70:
  8429. case 0x71:
  8430. case 0x72:
  8431. case 0x73:
  8432. case 0x74:
  8433. case 0x75:
  8434. case 0x76:
  8435. case 0x77:
  8436. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8437. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8438. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8439. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8440. case 0x7F: // UTF-8 string (indefinite length)
  8441. {
  8442. string_t s;
  8443. return get_cbor_string(s) && sax->string(s);
  8444. }
  8445. // array (0x00..0x17 data items follow)
  8446. case 0x80:
  8447. case 0x81:
  8448. case 0x82:
  8449. case 0x83:
  8450. case 0x84:
  8451. case 0x85:
  8452. case 0x86:
  8453. case 0x87:
  8454. case 0x88:
  8455. case 0x89:
  8456. case 0x8A:
  8457. case 0x8B:
  8458. case 0x8C:
  8459. case 0x8D:
  8460. case 0x8E:
  8461. case 0x8F:
  8462. case 0x90:
  8463. case 0x91:
  8464. case 0x92:
  8465. case 0x93:
  8466. case 0x94:
  8467. case 0x95:
  8468. case 0x96:
  8469. case 0x97:
  8470. return get_cbor_array(
  8471. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8472. case 0x98: // array (one-byte uint8_t for n follows)
  8473. {
  8474. std::uint8_t len{};
  8475. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8476. }
  8477. case 0x99: // array (two-byte uint16_t for n follow)
  8478. {
  8479. std::uint16_t len{};
  8480. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8481. }
  8482. case 0x9A: // array (four-byte uint32_t for n follow)
  8483. {
  8484. std::uint32_t len{};
  8485. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8486. }
  8487. case 0x9B: // array (eight-byte uint64_t for n follow)
  8488. {
  8489. std::uint64_t len{};
  8490. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8491. }
  8492. case 0x9F: // array (indefinite length)
  8493. return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
  8494. // map (0x00..0x17 pairs of data items follow)
  8495. case 0xA0:
  8496. case 0xA1:
  8497. case 0xA2:
  8498. case 0xA3:
  8499. case 0xA4:
  8500. case 0xA5:
  8501. case 0xA6:
  8502. case 0xA7:
  8503. case 0xA8:
  8504. case 0xA9:
  8505. case 0xAA:
  8506. case 0xAB:
  8507. case 0xAC:
  8508. case 0xAD:
  8509. case 0xAE:
  8510. case 0xAF:
  8511. case 0xB0:
  8512. case 0xB1:
  8513. case 0xB2:
  8514. case 0xB3:
  8515. case 0xB4:
  8516. case 0xB5:
  8517. case 0xB6:
  8518. case 0xB7:
  8519. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8520. case 0xB8: // map (one-byte uint8_t for n follows)
  8521. {
  8522. std::uint8_t len{};
  8523. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8524. }
  8525. case 0xB9: // map (two-byte uint16_t for n follow)
  8526. {
  8527. std::uint16_t len{};
  8528. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8529. }
  8530. case 0xBA: // map (four-byte uint32_t for n follow)
  8531. {
  8532. std::uint32_t len{};
  8533. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8534. }
  8535. case 0xBB: // map (eight-byte uint64_t for n follow)
  8536. {
  8537. std::uint64_t len{};
  8538. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8539. }
  8540. case 0xBF: // map (indefinite length)
  8541. return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
  8542. case 0xC6: // tagged item
  8543. case 0xC7:
  8544. case 0xC8:
  8545. case 0xC9:
  8546. case 0xCA:
  8547. case 0xCB:
  8548. case 0xCC:
  8549. case 0xCD:
  8550. case 0xCE:
  8551. case 0xCF:
  8552. case 0xD0:
  8553. case 0xD1:
  8554. case 0xD2:
  8555. case 0xD3:
  8556. case 0xD4:
  8557. case 0xD8: // tagged item (1 bytes follow)
  8558. case 0xD9: // tagged item (2 bytes follow)
  8559. case 0xDA: // tagged item (4 bytes follow)
  8560. case 0xDB: // tagged item (8 bytes follow)
  8561. {
  8562. switch (tag_handler)
  8563. {
  8564. case cbor_tag_handler_t::error:
  8565. {
  8566. auto last_token = get_token_string();
  8567. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8568. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8569. }
  8570. case cbor_tag_handler_t::ignore:
  8571. {
  8572. // ignore binary subtype
  8573. switch (current)
  8574. {
  8575. case 0xD8:
  8576. {
  8577. std::uint8_t subtype_to_ignore{};
  8578. get_number(input_format_t::cbor, subtype_to_ignore);
  8579. break;
  8580. }
  8581. case 0xD9:
  8582. {
  8583. std::uint16_t subtype_to_ignore{};
  8584. get_number(input_format_t::cbor, subtype_to_ignore);
  8585. break;
  8586. }
  8587. case 0xDA:
  8588. {
  8589. std::uint32_t subtype_to_ignore{};
  8590. get_number(input_format_t::cbor, subtype_to_ignore);
  8591. break;
  8592. }
  8593. case 0xDB:
  8594. {
  8595. std::uint64_t subtype_to_ignore{};
  8596. get_number(input_format_t::cbor, subtype_to_ignore);
  8597. break;
  8598. }
  8599. default:
  8600. break;
  8601. }
  8602. return parse_cbor_internal(true, tag_handler);
  8603. }
  8604. case cbor_tag_handler_t::store:
  8605. {
  8606. binary_t b;
  8607. // use binary subtype and store in binary container
  8608. switch (current)
  8609. {
  8610. case 0xD8:
  8611. {
  8612. std::uint8_t subtype{};
  8613. get_number(input_format_t::cbor, subtype);
  8614. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8615. break;
  8616. }
  8617. case 0xD9:
  8618. {
  8619. std::uint16_t subtype{};
  8620. get_number(input_format_t::cbor, subtype);
  8621. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8622. break;
  8623. }
  8624. case 0xDA:
  8625. {
  8626. std::uint32_t subtype{};
  8627. get_number(input_format_t::cbor, subtype);
  8628. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8629. break;
  8630. }
  8631. case 0xDB:
  8632. {
  8633. std::uint64_t subtype{};
  8634. get_number(input_format_t::cbor, subtype);
  8635. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8636. break;
  8637. }
  8638. default:
  8639. return parse_cbor_internal(true, tag_handler);
  8640. }
  8641. get();
  8642. return get_cbor_binary(b) && sax->binary(b);
  8643. }
  8644. default: // LCOV_EXCL_LINE
  8645. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8646. return false; // LCOV_EXCL_LINE
  8647. }
  8648. }
  8649. case 0xF4: // false
  8650. return sax->boolean(false);
  8651. case 0xF5: // true
  8652. return sax->boolean(true);
  8653. case 0xF6: // null
  8654. return sax->null();
  8655. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  8656. {
  8657. const auto byte1_raw = get();
  8658. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8659. {
  8660. return false;
  8661. }
  8662. const auto byte2_raw = get();
  8663. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8664. {
  8665. return false;
  8666. }
  8667. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  8668. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  8669. // code from RFC 7049, Appendix D, Figure 3:
  8670. // As half-precision floating-point numbers were only added
  8671. // to IEEE 754 in 2008, today's programming platforms often
  8672. // still only have limited support for them. It is very
  8673. // easy to include at least decoding support for them even
  8674. // without such support. An example of a small decoder for
  8675. // half-precision floating-point numbers in the C language
  8676. // is shown in Fig. 3.
  8677. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  8678. const double val = [&half]
  8679. {
  8680. const int exp = (half >> 10u) & 0x1Fu;
  8681. const unsigned int mant = half & 0x3FFu;
  8682. JSON_ASSERT(0 <= exp&& exp <= 32);
  8683. JSON_ASSERT(mant <= 1024);
  8684. switch (exp)
  8685. {
  8686. case 0:
  8687. return std::ldexp(mant, -24);
  8688. case 31:
  8689. return (mant == 0)
  8690. ? std::numeric_limits<double>::infinity()
  8691. : std::numeric_limits<double>::quiet_NaN();
  8692. default:
  8693. return std::ldexp(mant + 1024, exp - 25);
  8694. }
  8695. }();
  8696. return sax->number_float((half & 0x8000u) != 0
  8697. ? static_cast<number_float_t>(-val)
  8698. : static_cast<number_float_t>(val), "");
  8699. }
  8700. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  8701. {
  8702. float number{};
  8703. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8704. }
  8705. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8706. {
  8707. double number{};
  8708. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8709. }
  8710. default: // anything else (0xFF is handled inside the other types)
  8711. {
  8712. auto last_token = get_token_string();
  8713. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8714. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8715. }
  8716. }
  8717. }
  8718. /*!
  8719. @brief reads a CBOR string
  8720. This function first reads starting bytes to determine the expected
  8721. string length and then copies this number of bytes into a string.
  8722. Additionally, CBOR's strings with indefinite lengths are supported.
  8723. @param[out] result created string
  8724. @return whether string creation completed
  8725. */
  8726. bool get_cbor_string(string_t& result)
  8727. {
  8728. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8729. {
  8730. return false;
  8731. }
  8732. switch (current)
  8733. {
  8734. // UTF-8 string (0x00..0x17 bytes follow)
  8735. case 0x60:
  8736. case 0x61:
  8737. case 0x62:
  8738. case 0x63:
  8739. case 0x64:
  8740. case 0x65:
  8741. case 0x66:
  8742. case 0x67:
  8743. case 0x68:
  8744. case 0x69:
  8745. case 0x6A:
  8746. case 0x6B:
  8747. case 0x6C:
  8748. case 0x6D:
  8749. case 0x6E:
  8750. case 0x6F:
  8751. case 0x70:
  8752. case 0x71:
  8753. case 0x72:
  8754. case 0x73:
  8755. case 0x74:
  8756. case 0x75:
  8757. case 0x76:
  8758. case 0x77:
  8759. {
  8760. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8761. }
  8762. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8763. {
  8764. std::uint8_t len{};
  8765. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8766. }
  8767. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8768. {
  8769. std::uint16_t len{};
  8770. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8771. }
  8772. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8773. {
  8774. std::uint32_t len{};
  8775. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8776. }
  8777. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8778. {
  8779. std::uint64_t len{};
  8780. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8781. }
  8782. case 0x7F: // UTF-8 string (indefinite length)
  8783. {
  8784. while (get() != 0xFF)
  8785. {
  8786. string_t chunk;
  8787. if (!get_cbor_string(chunk))
  8788. {
  8789. return false;
  8790. }
  8791. result.append(chunk);
  8792. }
  8793. return true;
  8794. }
  8795. default:
  8796. {
  8797. auto last_token = get_token_string();
  8798. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8799. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  8800. }
  8801. }
  8802. }
  8803. /*!
  8804. @brief reads a CBOR byte array
  8805. This function first reads starting bytes to determine the expected
  8806. byte array length and then copies this number of bytes into the byte array.
  8807. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8808. @param[out] result created byte array
  8809. @return whether byte array creation completed
  8810. */
  8811. bool get_cbor_binary(binary_t& result)
  8812. {
  8813. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8814. {
  8815. return false;
  8816. }
  8817. switch (current)
  8818. {
  8819. // Binary data (0x00..0x17 bytes follow)
  8820. case 0x40:
  8821. case 0x41:
  8822. case 0x42:
  8823. case 0x43:
  8824. case 0x44:
  8825. case 0x45:
  8826. case 0x46:
  8827. case 0x47:
  8828. case 0x48:
  8829. case 0x49:
  8830. case 0x4A:
  8831. case 0x4B:
  8832. case 0x4C:
  8833. case 0x4D:
  8834. case 0x4E:
  8835. case 0x4F:
  8836. case 0x50:
  8837. case 0x51:
  8838. case 0x52:
  8839. case 0x53:
  8840. case 0x54:
  8841. case 0x55:
  8842. case 0x56:
  8843. case 0x57:
  8844. {
  8845. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8846. }
  8847. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8848. {
  8849. std::uint8_t len{};
  8850. return get_number(input_format_t::cbor, len) &&
  8851. get_binary(input_format_t::cbor, len, result);
  8852. }
  8853. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8854. {
  8855. std::uint16_t len{};
  8856. return get_number(input_format_t::cbor, len) &&
  8857. get_binary(input_format_t::cbor, len, result);
  8858. }
  8859. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8860. {
  8861. std::uint32_t len{};
  8862. return get_number(input_format_t::cbor, len) &&
  8863. get_binary(input_format_t::cbor, len, result);
  8864. }
  8865. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8866. {
  8867. std::uint64_t len{};
  8868. return get_number(input_format_t::cbor, len) &&
  8869. get_binary(input_format_t::cbor, len, result);
  8870. }
  8871. case 0x5F: // Binary data (indefinite length)
  8872. {
  8873. while (get() != 0xFF)
  8874. {
  8875. binary_t chunk;
  8876. if (!get_cbor_binary(chunk))
  8877. {
  8878. return false;
  8879. }
  8880. result.insert(result.end(), chunk.begin(), chunk.end());
  8881. }
  8882. return true;
  8883. }
  8884. default:
  8885. {
  8886. auto last_token = get_token_string();
  8887. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8888. 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));
  8889. }
  8890. }
  8891. }
  8892. /*!
  8893. @param[in] len the length of the array or static_cast<std::size_t>(-1) for an
  8894. array of indefinite size
  8895. @param[in] tag_handler how CBOR tags should be treated
  8896. @return whether array creation completed
  8897. */
  8898. bool get_cbor_array(const std::size_t len,
  8899. const cbor_tag_handler_t tag_handler)
  8900. {
  8901. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8902. {
  8903. return false;
  8904. }
  8905. if (len != static_cast<std::size_t>(-1))
  8906. {
  8907. for (std::size_t i = 0; i < len; ++i)
  8908. {
  8909. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8910. {
  8911. return false;
  8912. }
  8913. }
  8914. }
  8915. else
  8916. {
  8917. while (get() != 0xFF)
  8918. {
  8919. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8920. {
  8921. return false;
  8922. }
  8923. }
  8924. }
  8925. return sax->end_array();
  8926. }
  8927. /*!
  8928. @param[in] len the length of the object or static_cast<std::size_t>(-1) for an
  8929. object of indefinite size
  8930. @param[in] tag_handler how CBOR tags should be treated
  8931. @return whether object creation completed
  8932. */
  8933. bool get_cbor_object(const std::size_t len,
  8934. const cbor_tag_handler_t tag_handler)
  8935. {
  8936. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8937. {
  8938. return false;
  8939. }
  8940. if (len != 0)
  8941. {
  8942. string_t key;
  8943. if (len != static_cast<std::size_t>(-1))
  8944. {
  8945. for (std::size_t i = 0; i < len; ++i)
  8946. {
  8947. get();
  8948. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8949. {
  8950. return false;
  8951. }
  8952. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8953. {
  8954. return false;
  8955. }
  8956. key.clear();
  8957. }
  8958. }
  8959. else
  8960. {
  8961. while (get() != 0xFF)
  8962. {
  8963. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8964. {
  8965. return false;
  8966. }
  8967. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8968. {
  8969. return false;
  8970. }
  8971. key.clear();
  8972. }
  8973. }
  8974. }
  8975. return sax->end_object();
  8976. }
  8977. /////////////
  8978. // MsgPack //
  8979. /////////////
  8980. /*!
  8981. @return whether a valid MessagePack value was passed to the SAX parser
  8982. */
  8983. bool parse_msgpack_internal()
  8984. {
  8985. switch (get())
  8986. {
  8987. // EOF
  8988. case char_traits<char_type>::eof():
  8989. return unexpect_eof(input_format_t::msgpack, "value");
  8990. // positive fixint
  8991. case 0x00:
  8992. case 0x01:
  8993. case 0x02:
  8994. case 0x03:
  8995. case 0x04:
  8996. case 0x05:
  8997. case 0x06:
  8998. case 0x07:
  8999. case 0x08:
  9000. case 0x09:
  9001. case 0x0A:
  9002. case 0x0B:
  9003. case 0x0C:
  9004. case 0x0D:
  9005. case 0x0E:
  9006. case 0x0F:
  9007. case 0x10:
  9008. case 0x11:
  9009. case 0x12:
  9010. case 0x13:
  9011. case 0x14:
  9012. case 0x15:
  9013. case 0x16:
  9014. case 0x17:
  9015. case 0x18:
  9016. case 0x19:
  9017. case 0x1A:
  9018. case 0x1B:
  9019. case 0x1C:
  9020. case 0x1D:
  9021. case 0x1E:
  9022. case 0x1F:
  9023. case 0x20:
  9024. case 0x21:
  9025. case 0x22:
  9026. case 0x23:
  9027. case 0x24:
  9028. case 0x25:
  9029. case 0x26:
  9030. case 0x27:
  9031. case 0x28:
  9032. case 0x29:
  9033. case 0x2A:
  9034. case 0x2B:
  9035. case 0x2C:
  9036. case 0x2D:
  9037. case 0x2E:
  9038. case 0x2F:
  9039. case 0x30:
  9040. case 0x31:
  9041. case 0x32:
  9042. case 0x33:
  9043. case 0x34:
  9044. case 0x35:
  9045. case 0x36:
  9046. case 0x37:
  9047. case 0x38:
  9048. case 0x39:
  9049. case 0x3A:
  9050. case 0x3B:
  9051. case 0x3C:
  9052. case 0x3D:
  9053. case 0x3E:
  9054. case 0x3F:
  9055. case 0x40:
  9056. case 0x41:
  9057. case 0x42:
  9058. case 0x43:
  9059. case 0x44:
  9060. case 0x45:
  9061. case 0x46:
  9062. case 0x47:
  9063. case 0x48:
  9064. case 0x49:
  9065. case 0x4A:
  9066. case 0x4B:
  9067. case 0x4C:
  9068. case 0x4D:
  9069. case 0x4E:
  9070. case 0x4F:
  9071. case 0x50:
  9072. case 0x51:
  9073. case 0x52:
  9074. case 0x53:
  9075. case 0x54:
  9076. case 0x55:
  9077. case 0x56:
  9078. case 0x57:
  9079. case 0x58:
  9080. case 0x59:
  9081. case 0x5A:
  9082. case 0x5B:
  9083. case 0x5C:
  9084. case 0x5D:
  9085. case 0x5E:
  9086. case 0x5F:
  9087. case 0x60:
  9088. case 0x61:
  9089. case 0x62:
  9090. case 0x63:
  9091. case 0x64:
  9092. case 0x65:
  9093. case 0x66:
  9094. case 0x67:
  9095. case 0x68:
  9096. case 0x69:
  9097. case 0x6A:
  9098. case 0x6B:
  9099. case 0x6C:
  9100. case 0x6D:
  9101. case 0x6E:
  9102. case 0x6F:
  9103. case 0x70:
  9104. case 0x71:
  9105. case 0x72:
  9106. case 0x73:
  9107. case 0x74:
  9108. case 0x75:
  9109. case 0x76:
  9110. case 0x77:
  9111. case 0x78:
  9112. case 0x79:
  9113. case 0x7A:
  9114. case 0x7B:
  9115. case 0x7C:
  9116. case 0x7D:
  9117. case 0x7E:
  9118. case 0x7F:
  9119. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9120. // fixmap
  9121. case 0x80:
  9122. case 0x81:
  9123. case 0x82:
  9124. case 0x83:
  9125. case 0x84:
  9126. case 0x85:
  9127. case 0x86:
  9128. case 0x87:
  9129. case 0x88:
  9130. case 0x89:
  9131. case 0x8A:
  9132. case 0x8B:
  9133. case 0x8C:
  9134. case 0x8D:
  9135. case 0x8E:
  9136. case 0x8F:
  9137. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9138. // fixarray
  9139. case 0x90:
  9140. case 0x91:
  9141. case 0x92:
  9142. case 0x93:
  9143. case 0x94:
  9144. case 0x95:
  9145. case 0x96:
  9146. case 0x97:
  9147. case 0x98:
  9148. case 0x99:
  9149. case 0x9A:
  9150. case 0x9B:
  9151. case 0x9C:
  9152. case 0x9D:
  9153. case 0x9E:
  9154. case 0x9F:
  9155. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9156. // fixstr
  9157. case 0xA0:
  9158. case 0xA1:
  9159. case 0xA2:
  9160. case 0xA3:
  9161. case 0xA4:
  9162. case 0xA5:
  9163. case 0xA6:
  9164. case 0xA7:
  9165. case 0xA8:
  9166. case 0xA9:
  9167. case 0xAA:
  9168. case 0xAB:
  9169. case 0xAC:
  9170. case 0xAD:
  9171. case 0xAE:
  9172. case 0xAF:
  9173. case 0xB0:
  9174. case 0xB1:
  9175. case 0xB2:
  9176. case 0xB3:
  9177. case 0xB4:
  9178. case 0xB5:
  9179. case 0xB6:
  9180. case 0xB7:
  9181. case 0xB8:
  9182. case 0xB9:
  9183. case 0xBA:
  9184. case 0xBB:
  9185. case 0xBC:
  9186. case 0xBD:
  9187. case 0xBE:
  9188. case 0xBF:
  9189. case 0xD9: // str 8
  9190. case 0xDA: // str 16
  9191. case 0xDB: // str 32
  9192. {
  9193. string_t s;
  9194. return get_msgpack_string(s) && sax->string(s);
  9195. }
  9196. case 0xC0: // nil
  9197. return sax->null();
  9198. case 0xC2: // false
  9199. return sax->boolean(false);
  9200. case 0xC3: // true
  9201. return sax->boolean(true);
  9202. case 0xC4: // bin 8
  9203. case 0xC5: // bin 16
  9204. case 0xC6: // bin 32
  9205. case 0xC7: // ext 8
  9206. case 0xC8: // ext 16
  9207. case 0xC9: // ext 32
  9208. case 0xD4: // fixext 1
  9209. case 0xD5: // fixext 2
  9210. case 0xD6: // fixext 4
  9211. case 0xD7: // fixext 8
  9212. case 0xD8: // fixext 16
  9213. {
  9214. binary_t b;
  9215. return get_msgpack_binary(b) && sax->binary(b);
  9216. }
  9217. case 0xCA: // float 32
  9218. {
  9219. float number{};
  9220. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9221. }
  9222. case 0xCB: // float 64
  9223. {
  9224. double number{};
  9225. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9226. }
  9227. case 0xCC: // uint 8
  9228. {
  9229. std::uint8_t number{};
  9230. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9231. }
  9232. case 0xCD: // uint 16
  9233. {
  9234. std::uint16_t number{};
  9235. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9236. }
  9237. case 0xCE: // uint 32
  9238. {
  9239. std::uint32_t number{};
  9240. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9241. }
  9242. case 0xCF: // uint 64
  9243. {
  9244. std::uint64_t number{};
  9245. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9246. }
  9247. case 0xD0: // int 8
  9248. {
  9249. std::int8_t number{};
  9250. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9251. }
  9252. case 0xD1: // int 16
  9253. {
  9254. std::int16_t number{};
  9255. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9256. }
  9257. case 0xD2: // int 32
  9258. {
  9259. std::int32_t number{};
  9260. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9261. }
  9262. case 0xD3: // int 64
  9263. {
  9264. std::int64_t number{};
  9265. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9266. }
  9267. case 0xDC: // array 16
  9268. {
  9269. std::uint16_t len{};
  9270. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9271. }
  9272. case 0xDD: // array 32
  9273. {
  9274. std::uint32_t len{};
  9275. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9276. }
  9277. case 0xDE: // map 16
  9278. {
  9279. std::uint16_t len{};
  9280. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9281. }
  9282. case 0xDF: // map 32
  9283. {
  9284. std::uint32_t len{};
  9285. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9286. }
  9287. // negative fixint
  9288. case 0xE0:
  9289. case 0xE1:
  9290. case 0xE2:
  9291. case 0xE3:
  9292. case 0xE4:
  9293. case 0xE5:
  9294. case 0xE6:
  9295. case 0xE7:
  9296. case 0xE8:
  9297. case 0xE9:
  9298. case 0xEA:
  9299. case 0xEB:
  9300. case 0xEC:
  9301. case 0xED:
  9302. case 0xEE:
  9303. case 0xEF:
  9304. case 0xF0:
  9305. case 0xF1:
  9306. case 0xF2:
  9307. case 0xF3:
  9308. case 0xF4:
  9309. case 0xF5:
  9310. case 0xF6:
  9311. case 0xF7:
  9312. case 0xF8:
  9313. case 0xF9:
  9314. case 0xFA:
  9315. case 0xFB:
  9316. case 0xFC:
  9317. case 0xFD:
  9318. case 0xFE:
  9319. case 0xFF:
  9320. return sax->number_integer(static_cast<std::int8_t>(current));
  9321. default: // anything else
  9322. {
  9323. auto last_token = get_token_string();
  9324. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9325. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9326. }
  9327. }
  9328. }
  9329. /*!
  9330. @brief reads a MessagePack string
  9331. This function first reads starting bytes to determine the expected
  9332. string length and then copies this number of bytes into a string.
  9333. @param[out] result created string
  9334. @return whether string creation completed
  9335. */
  9336. bool get_msgpack_string(string_t& result)
  9337. {
  9338. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  9339. {
  9340. return false;
  9341. }
  9342. switch (current)
  9343. {
  9344. // fixstr
  9345. case 0xA0:
  9346. case 0xA1:
  9347. case 0xA2:
  9348. case 0xA3:
  9349. case 0xA4:
  9350. case 0xA5:
  9351. case 0xA6:
  9352. case 0xA7:
  9353. case 0xA8:
  9354. case 0xA9:
  9355. case 0xAA:
  9356. case 0xAB:
  9357. case 0xAC:
  9358. case 0xAD:
  9359. case 0xAE:
  9360. case 0xAF:
  9361. case 0xB0:
  9362. case 0xB1:
  9363. case 0xB2:
  9364. case 0xB3:
  9365. case 0xB4:
  9366. case 0xB5:
  9367. case 0xB6:
  9368. case 0xB7:
  9369. case 0xB8:
  9370. case 0xB9:
  9371. case 0xBA:
  9372. case 0xBB:
  9373. case 0xBC:
  9374. case 0xBD:
  9375. case 0xBE:
  9376. case 0xBF:
  9377. {
  9378. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9379. }
  9380. case 0xD9: // str 8
  9381. {
  9382. std::uint8_t len{};
  9383. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9384. }
  9385. case 0xDA: // str 16
  9386. {
  9387. std::uint16_t len{};
  9388. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9389. }
  9390. case 0xDB: // str 32
  9391. {
  9392. std::uint32_t len{};
  9393. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9394. }
  9395. default:
  9396. {
  9397. auto last_token = get_token_string();
  9398. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9399. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9400. }
  9401. }
  9402. }
  9403. /*!
  9404. @brief reads a MessagePack byte array
  9405. This function first reads starting bytes to determine the expected
  9406. byte array length and then copies this number of bytes into a byte array.
  9407. @param[out] result created byte array
  9408. @return whether byte array creation completed
  9409. */
  9410. bool get_msgpack_binary(binary_t& result)
  9411. {
  9412. // helper function to set the subtype
  9413. auto assign_and_return_true = [&result](std::int8_t subtype)
  9414. {
  9415. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9416. return true;
  9417. };
  9418. switch (current)
  9419. {
  9420. case 0xC4: // bin 8
  9421. {
  9422. std::uint8_t len{};
  9423. return get_number(input_format_t::msgpack, len) &&
  9424. get_binary(input_format_t::msgpack, len, result);
  9425. }
  9426. case 0xC5: // bin 16
  9427. {
  9428. std::uint16_t len{};
  9429. return get_number(input_format_t::msgpack, len) &&
  9430. get_binary(input_format_t::msgpack, len, result);
  9431. }
  9432. case 0xC6: // bin 32
  9433. {
  9434. std::uint32_t len{};
  9435. return get_number(input_format_t::msgpack, len) &&
  9436. get_binary(input_format_t::msgpack, len, result);
  9437. }
  9438. case 0xC7: // ext 8
  9439. {
  9440. std::uint8_t len{};
  9441. std::int8_t subtype{};
  9442. return get_number(input_format_t::msgpack, len) &&
  9443. get_number(input_format_t::msgpack, subtype) &&
  9444. get_binary(input_format_t::msgpack, len, result) &&
  9445. assign_and_return_true(subtype);
  9446. }
  9447. case 0xC8: // ext 16
  9448. {
  9449. std::uint16_t len{};
  9450. std::int8_t subtype{};
  9451. return get_number(input_format_t::msgpack, len) &&
  9452. get_number(input_format_t::msgpack, subtype) &&
  9453. get_binary(input_format_t::msgpack, len, result) &&
  9454. assign_and_return_true(subtype);
  9455. }
  9456. case 0xC9: // ext 32
  9457. {
  9458. std::uint32_t len{};
  9459. std::int8_t subtype{};
  9460. return get_number(input_format_t::msgpack, len) &&
  9461. get_number(input_format_t::msgpack, subtype) &&
  9462. get_binary(input_format_t::msgpack, len, result) &&
  9463. assign_and_return_true(subtype);
  9464. }
  9465. case 0xD4: // fixext 1
  9466. {
  9467. std::int8_t subtype{};
  9468. return get_number(input_format_t::msgpack, subtype) &&
  9469. get_binary(input_format_t::msgpack, 1, result) &&
  9470. assign_and_return_true(subtype);
  9471. }
  9472. case 0xD5: // fixext 2
  9473. {
  9474. std::int8_t subtype{};
  9475. return get_number(input_format_t::msgpack, subtype) &&
  9476. get_binary(input_format_t::msgpack, 2, result) &&
  9477. assign_and_return_true(subtype);
  9478. }
  9479. case 0xD6: // fixext 4
  9480. {
  9481. std::int8_t subtype{};
  9482. return get_number(input_format_t::msgpack, subtype) &&
  9483. get_binary(input_format_t::msgpack, 4, result) &&
  9484. assign_and_return_true(subtype);
  9485. }
  9486. case 0xD7: // fixext 8
  9487. {
  9488. std::int8_t subtype{};
  9489. return get_number(input_format_t::msgpack, subtype) &&
  9490. get_binary(input_format_t::msgpack, 8, result) &&
  9491. assign_and_return_true(subtype);
  9492. }
  9493. case 0xD8: // fixext 16
  9494. {
  9495. std::int8_t subtype{};
  9496. return get_number(input_format_t::msgpack, subtype) &&
  9497. get_binary(input_format_t::msgpack, 16, result) &&
  9498. assign_and_return_true(subtype);
  9499. }
  9500. default: // LCOV_EXCL_LINE
  9501. return false; // LCOV_EXCL_LINE
  9502. }
  9503. }
  9504. /*!
  9505. @param[in] len the length of the array
  9506. @return whether array creation completed
  9507. */
  9508. bool get_msgpack_array(const std::size_t len)
  9509. {
  9510. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9511. {
  9512. return false;
  9513. }
  9514. for (std::size_t i = 0; i < len; ++i)
  9515. {
  9516. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9517. {
  9518. return false;
  9519. }
  9520. }
  9521. return sax->end_array();
  9522. }
  9523. /*!
  9524. @param[in] len the length of the object
  9525. @return whether object creation completed
  9526. */
  9527. bool get_msgpack_object(const std::size_t len)
  9528. {
  9529. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9530. {
  9531. return false;
  9532. }
  9533. string_t key;
  9534. for (std::size_t i = 0; i < len; ++i)
  9535. {
  9536. get();
  9537. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  9538. {
  9539. return false;
  9540. }
  9541. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9542. {
  9543. return false;
  9544. }
  9545. key.clear();
  9546. }
  9547. return sax->end_object();
  9548. }
  9549. ////////////
  9550. // UBJSON //
  9551. ////////////
  9552. /*!
  9553. @param[in] get_char whether a new character should be retrieved from the
  9554. input (true, default) or whether the last read
  9555. character should be considered instead
  9556. @return whether a valid UBJSON value was passed to the SAX parser
  9557. */
  9558. bool parse_ubjson_internal(const bool get_char = true)
  9559. {
  9560. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  9561. }
  9562. /*!
  9563. @brief reads a UBJSON string
  9564. This function is either called after reading the 'S' byte explicitly
  9565. indicating a string, or in case of an object key where the 'S' byte can be
  9566. left out.
  9567. @param[out] result created string
  9568. @param[in] get_char whether a new character should be retrieved from the
  9569. input (true, default) or whether the last read
  9570. character should be considered instead
  9571. @return whether string creation completed
  9572. */
  9573. bool get_ubjson_string(string_t& result, const bool get_char = true)
  9574. {
  9575. if (get_char)
  9576. {
  9577. get(); // TODO(niels): may we ignore N here?
  9578. }
  9579. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9580. {
  9581. return false;
  9582. }
  9583. switch (current)
  9584. {
  9585. case 'U':
  9586. {
  9587. std::uint8_t len{};
  9588. return get_number(input_format, len) && get_string(input_format, len, result);
  9589. }
  9590. case 'i':
  9591. {
  9592. std::int8_t len{};
  9593. return get_number(input_format, len) && get_string(input_format, len, result);
  9594. }
  9595. case 'I':
  9596. {
  9597. std::int16_t len{};
  9598. return get_number(input_format, len) && get_string(input_format, len, result);
  9599. }
  9600. case 'l':
  9601. {
  9602. std::int32_t len{};
  9603. return get_number(input_format, len) && get_string(input_format, len, result);
  9604. }
  9605. case 'L':
  9606. {
  9607. std::int64_t len{};
  9608. return get_number(input_format, len) && get_string(input_format, len, result);
  9609. }
  9610. case 'u':
  9611. {
  9612. if (input_format != input_format_t::bjdata)
  9613. {
  9614. break;
  9615. }
  9616. std::uint16_t len{};
  9617. return get_number(input_format, len) && get_string(input_format, len, result);
  9618. }
  9619. case 'm':
  9620. {
  9621. if (input_format != input_format_t::bjdata)
  9622. {
  9623. break;
  9624. }
  9625. std::uint32_t len{};
  9626. return get_number(input_format, len) && get_string(input_format, len, result);
  9627. }
  9628. case 'M':
  9629. {
  9630. if (input_format != input_format_t::bjdata)
  9631. {
  9632. break;
  9633. }
  9634. std::uint64_t len{};
  9635. return get_number(input_format, len) && get_string(input_format, len, result);
  9636. }
  9637. default:
  9638. break;
  9639. }
  9640. auto last_token = get_token_string();
  9641. std::string message;
  9642. if (input_format != input_format_t::bjdata)
  9643. {
  9644. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  9645. }
  9646. else
  9647. {
  9648. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  9649. }
  9650. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  9651. }
  9652. /*!
  9653. @param[out] dim an integer vector storing the ND array dimensions
  9654. @return whether reading ND array size vector is successful
  9655. */
  9656. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  9657. {
  9658. std::pair<std::size_t, char_int_type> size_and_type;
  9659. size_t dimlen = 0;
  9660. bool no_ndarray = true;
  9661. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  9662. {
  9663. return false;
  9664. }
  9665. if (size_and_type.first != npos)
  9666. {
  9667. if (size_and_type.second != 0)
  9668. {
  9669. if (size_and_type.second != 'N')
  9670. {
  9671. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9672. {
  9673. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  9674. {
  9675. return false;
  9676. }
  9677. dim.push_back(dimlen);
  9678. }
  9679. }
  9680. }
  9681. else
  9682. {
  9683. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9684. {
  9685. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  9686. {
  9687. return false;
  9688. }
  9689. dim.push_back(dimlen);
  9690. }
  9691. }
  9692. }
  9693. else
  9694. {
  9695. while (current != ']')
  9696. {
  9697. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  9698. {
  9699. return false;
  9700. }
  9701. dim.push_back(dimlen);
  9702. get_ignore_noop();
  9703. }
  9704. }
  9705. return true;
  9706. }
  9707. /*!
  9708. @param[out] result determined size
  9709. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  9710. or ndarray dimension is not allowed; `false` means ndarray
  9711. is allowed; for output, `true` means an ndarray is found;
  9712. is_ndarray can only return `true` when its initial value
  9713. is `false`
  9714. @param[in] prefix type marker if already read, otherwise set to 0
  9715. @return whether size determination completed
  9716. */
  9717. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  9718. {
  9719. if (prefix == 0)
  9720. {
  9721. prefix = get_ignore_noop();
  9722. }
  9723. switch (prefix)
  9724. {
  9725. case 'U':
  9726. {
  9727. std::uint8_t number{};
  9728. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9729. {
  9730. return false;
  9731. }
  9732. result = static_cast<std::size_t>(number);
  9733. return true;
  9734. }
  9735. case 'i':
  9736. {
  9737. std::int8_t number{};
  9738. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9739. {
  9740. return false;
  9741. }
  9742. if (number < 0)
  9743. {
  9744. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9745. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9746. }
  9747. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  9748. return true;
  9749. }
  9750. case 'I':
  9751. {
  9752. std::int16_t number{};
  9753. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9754. {
  9755. return false;
  9756. }
  9757. if (number < 0)
  9758. {
  9759. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9760. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9761. }
  9762. result = static_cast<std::size_t>(number);
  9763. return true;
  9764. }
  9765. case 'l':
  9766. {
  9767. std::int32_t number{};
  9768. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9769. {
  9770. return false;
  9771. }
  9772. if (number < 0)
  9773. {
  9774. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9775. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9776. }
  9777. result = static_cast<std::size_t>(number);
  9778. return true;
  9779. }
  9780. case 'L':
  9781. {
  9782. std::int64_t number{};
  9783. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9784. {
  9785. return false;
  9786. }
  9787. if (number < 0)
  9788. {
  9789. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9790. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9791. }
  9792. if (!value_in_range_of<std::size_t>(number))
  9793. {
  9794. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9795. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9796. }
  9797. result = static_cast<std::size_t>(number);
  9798. return true;
  9799. }
  9800. case 'u':
  9801. {
  9802. if (input_format != input_format_t::bjdata)
  9803. {
  9804. break;
  9805. }
  9806. std::uint16_t number{};
  9807. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9808. {
  9809. return false;
  9810. }
  9811. result = static_cast<std::size_t>(number);
  9812. return true;
  9813. }
  9814. case 'm':
  9815. {
  9816. if (input_format != input_format_t::bjdata)
  9817. {
  9818. break;
  9819. }
  9820. std::uint32_t number{};
  9821. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9822. {
  9823. return false;
  9824. }
  9825. result = conditional_static_cast<std::size_t>(number);
  9826. return true;
  9827. }
  9828. case 'M':
  9829. {
  9830. if (input_format != input_format_t::bjdata)
  9831. {
  9832. break;
  9833. }
  9834. std::uint64_t number{};
  9835. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9836. {
  9837. return false;
  9838. }
  9839. if (!value_in_range_of<std::size_t>(number))
  9840. {
  9841. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9842. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9843. }
  9844. result = detail::conditional_static_cast<std::size_t>(number);
  9845. return true;
  9846. }
  9847. case '[':
  9848. {
  9849. if (input_format != input_format_t::bjdata)
  9850. {
  9851. break;
  9852. }
  9853. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  9854. {
  9855. 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));
  9856. }
  9857. std::vector<size_t> dim;
  9858. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  9859. {
  9860. return false;
  9861. }
  9862. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  9863. {
  9864. result = dim.at(dim.size() - 1);
  9865. return true;
  9866. }
  9867. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  9868. {
  9869. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  9870. {
  9871. if ( i == 0 )
  9872. {
  9873. result = 0;
  9874. return true;
  9875. }
  9876. }
  9877. string_t key = "_ArraySize_";
  9878. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  9879. {
  9880. return false;
  9881. }
  9882. result = 1;
  9883. for (auto i : dim)
  9884. {
  9885. result *= i;
  9886. 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()
  9887. {
  9888. 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));
  9889. }
  9890. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  9891. {
  9892. return false;
  9893. }
  9894. }
  9895. is_ndarray = true;
  9896. return sax->end_array();
  9897. }
  9898. result = 0;
  9899. return true;
  9900. }
  9901. default:
  9902. break;
  9903. }
  9904. auto last_token = get_token_string();
  9905. std::string message;
  9906. if (input_format != input_format_t::bjdata)
  9907. {
  9908. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  9909. }
  9910. else
  9911. {
  9912. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  9913. }
  9914. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  9915. }
  9916. /*!
  9917. @brief determine the type and size for a container
  9918. In the optimized UBJSON format, a type and a size can be provided to allow
  9919. for a more compact representation.
  9920. @param[out] result pair of the size and the type
  9921. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  9922. @return whether pair creation completed
  9923. */
  9924. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  9925. {
  9926. result.first = npos; // size
  9927. result.second = 0; // type
  9928. bool is_ndarray = false;
  9929. get_ignore_noop();
  9930. if (current == '$')
  9931. {
  9932. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  9933. if (input_format == input_format_t::bjdata
  9934. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  9935. {
  9936. auto last_token = get_token_string();
  9937. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9938. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  9939. }
  9940. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  9941. {
  9942. return false;
  9943. }
  9944. get_ignore_noop();
  9945. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  9946. {
  9947. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9948. {
  9949. return false;
  9950. }
  9951. auto last_token = get_token_string();
  9952. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9953. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  9954. }
  9955. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9956. if (input_format == input_format_t::bjdata && is_ndarray)
  9957. {
  9958. if (inside_ndarray)
  9959. {
  9960. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9961. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  9962. }
  9963. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  9964. }
  9965. return is_error;
  9966. }
  9967. if (current == '#')
  9968. {
  9969. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9970. if (input_format == input_format_t::bjdata && is_ndarray)
  9971. {
  9972. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9973. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  9974. }
  9975. return is_error;
  9976. }
  9977. return true;
  9978. }
  9979. /*!
  9980. @param prefix the previously read or set type prefix
  9981. @return whether value creation completed
  9982. */
  9983. bool get_ubjson_value(const char_int_type prefix)
  9984. {
  9985. switch (prefix)
  9986. {
  9987. case char_traits<char_type>::eof(): // EOF
  9988. return unexpect_eof(input_format, "value");
  9989. case 'T': // true
  9990. return sax->boolean(true);
  9991. case 'F': // false
  9992. return sax->boolean(false);
  9993. case 'Z': // null
  9994. return sax->null();
  9995. case 'U':
  9996. {
  9997. std::uint8_t number{};
  9998. return get_number(input_format, number) && sax->number_unsigned(number);
  9999. }
  10000. case 'i':
  10001. {
  10002. std::int8_t number{};
  10003. return get_number(input_format, number) && sax->number_integer(number);
  10004. }
  10005. case 'I':
  10006. {
  10007. std::int16_t number{};
  10008. return get_number(input_format, number) && sax->number_integer(number);
  10009. }
  10010. case 'l':
  10011. {
  10012. std::int32_t number{};
  10013. return get_number(input_format, number) && sax->number_integer(number);
  10014. }
  10015. case 'L':
  10016. {
  10017. std::int64_t number{};
  10018. return get_number(input_format, number) && sax->number_integer(number);
  10019. }
  10020. case 'u':
  10021. {
  10022. if (input_format != input_format_t::bjdata)
  10023. {
  10024. break;
  10025. }
  10026. std::uint16_t number{};
  10027. return get_number(input_format, number) && sax->number_unsigned(number);
  10028. }
  10029. case 'm':
  10030. {
  10031. if (input_format != input_format_t::bjdata)
  10032. {
  10033. break;
  10034. }
  10035. std::uint32_t number{};
  10036. return get_number(input_format, number) && sax->number_unsigned(number);
  10037. }
  10038. case 'M':
  10039. {
  10040. if (input_format != input_format_t::bjdata)
  10041. {
  10042. break;
  10043. }
  10044. std::uint64_t number{};
  10045. return get_number(input_format, number) && sax->number_unsigned(number);
  10046. }
  10047. case 'h':
  10048. {
  10049. if (input_format != input_format_t::bjdata)
  10050. {
  10051. break;
  10052. }
  10053. const auto byte1_raw = get();
  10054. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10055. {
  10056. return false;
  10057. }
  10058. const auto byte2_raw = get();
  10059. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10060. {
  10061. return false;
  10062. }
  10063. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  10064. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10065. // code from RFC 7049, Appendix D, Figure 3:
  10066. // As half-precision floating-point numbers were only added
  10067. // to IEEE 754 in 2008, today's programming platforms often
  10068. // still only have limited support for them. It is very
  10069. // easy to include at least decoding support for them even
  10070. // without such support. An example of a small decoder for
  10071. // half-precision floating-point numbers in the C language
  10072. // is shown in Fig. 3.
  10073. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10074. const double val = [&half]
  10075. {
  10076. const int exp = (half >> 10u) & 0x1Fu;
  10077. const unsigned int mant = half & 0x3FFu;
  10078. JSON_ASSERT(0 <= exp&& exp <= 32);
  10079. JSON_ASSERT(mant <= 1024);
  10080. switch (exp)
  10081. {
  10082. case 0:
  10083. return std::ldexp(mant, -24);
  10084. case 31:
  10085. return (mant == 0)
  10086. ? std::numeric_limits<double>::infinity()
  10087. : std::numeric_limits<double>::quiet_NaN();
  10088. default:
  10089. return std::ldexp(mant + 1024, exp - 25);
  10090. }
  10091. }();
  10092. return sax->number_float((half & 0x8000u) != 0
  10093. ? static_cast<number_float_t>(-val)
  10094. : static_cast<number_float_t>(val), "");
  10095. }
  10096. case 'd':
  10097. {
  10098. float number{};
  10099. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10100. }
  10101. case 'D':
  10102. {
  10103. double number{};
  10104. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10105. }
  10106. case 'H':
  10107. {
  10108. return get_ubjson_high_precision_number();
  10109. }
  10110. case 'C': // char
  10111. {
  10112. get();
  10113. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10114. {
  10115. return false;
  10116. }
  10117. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10118. {
  10119. auto last_token = get_token_string();
  10120. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10121. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10122. }
  10123. string_t s(1, static_cast<typename string_t::value_type>(current));
  10124. return sax->string(s);
  10125. }
  10126. case 'S': // string
  10127. {
  10128. string_t s;
  10129. return get_ubjson_string(s) && sax->string(s);
  10130. }
  10131. case '[': // array
  10132. return get_ubjson_array();
  10133. case '{': // object
  10134. return get_ubjson_object();
  10135. default: // anything else
  10136. break;
  10137. }
  10138. auto last_token = get_token_string();
  10139. 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));
  10140. }
  10141. /*!
  10142. @return whether array creation completed
  10143. */
  10144. bool get_ubjson_array()
  10145. {
  10146. std::pair<std::size_t, char_int_type> size_and_type;
  10147. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10148. {
  10149. return false;
  10150. }
  10151. // 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):
  10152. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10153. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10154. {
  10155. 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
  10156. 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)
  10157. {
  10158. return p.first < t;
  10159. });
  10160. string_t key = "_ArrayType_";
  10161. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10162. {
  10163. auto last_token = get_token_string();
  10164. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10165. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10166. }
  10167. string_t type = it->second; // sax->string() takes a reference
  10168. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10169. {
  10170. return false;
  10171. }
  10172. if (size_and_type.second == 'C')
  10173. {
  10174. size_and_type.second = 'U';
  10175. }
  10176. key = "_ArrayData_";
  10177. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10178. {
  10179. return false;
  10180. }
  10181. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10182. {
  10183. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10184. {
  10185. return false;
  10186. }
  10187. }
  10188. return (sax->end_array() && sax->end_object());
  10189. }
  10190. if (size_and_type.first != npos)
  10191. {
  10192. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10193. {
  10194. return false;
  10195. }
  10196. if (size_and_type.second != 0)
  10197. {
  10198. if (size_and_type.second != 'N')
  10199. {
  10200. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10201. {
  10202. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10203. {
  10204. return false;
  10205. }
  10206. }
  10207. }
  10208. }
  10209. else
  10210. {
  10211. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10212. {
  10213. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10214. {
  10215. return false;
  10216. }
  10217. }
  10218. }
  10219. }
  10220. else
  10221. {
  10222. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10223. {
  10224. return false;
  10225. }
  10226. while (current != ']')
  10227. {
  10228. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10229. {
  10230. return false;
  10231. }
  10232. get_ignore_noop();
  10233. }
  10234. }
  10235. return sax->end_array();
  10236. }
  10237. /*!
  10238. @return whether object creation completed
  10239. */
  10240. bool get_ubjson_object()
  10241. {
  10242. std::pair<std::size_t, char_int_type> size_and_type;
  10243. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10244. {
  10245. return false;
  10246. }
  10247. // do not accept ND-array size in objects in BJData
  10248. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10249. {
  10250. auto last_token = get_token_string();
  10251. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10252. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10253. }
  10254. string_t key;
  10255. if (size_and_type.first != npos)
  10256. {
  10257. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10258. {
  10259. return false;
  10260. }
  10261. if (size_and_type.second != 0)
  10262. {
  10263. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10264. {
  10265. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10266. {
  10267. return false;
  10268. }
  10269. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10270. {
  10271. return false;
  10272. }
  10273. key.clear();
  10274. }
  10275. }
  10276. else
  10277. {
  10278. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10279. {
  10280. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10281. {
  10282. return false;
  10283. }
  10284. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10285. {
  10286. return false;
  10287. }
  10288. key.clear();
  10289. }
  10290. }
  10291. }
  10292. else
  10293. {
  10294. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10295. {
  10296. return false;
  10297. }
  10298. while (current != '}')
  10299. {
  10300. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  10301. {
  10302. return false;
  10303. }
  10304. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10305. {
  10306. return false;
  10307. }
  10308. get_ignore_noop();
  10309. key.clear();
  10310. }
  10311. }
  10312. return sax->end_object();
  10313. }
  10314. // Note, no reader for UBJSON binary types is implemented because they do
  10315. // not exist
  10316. bool get_ubjson_high_precision_number()
  10317. {
  10318. // get size of following number string
  10319. std::size_t size{};
  10320. bool no_ndarray = true;
  10321. auto res = get_ubjson_size_value(size, no_ndarray);
  10322. if (JSON_HEDLEY_UNLIKELY(!res))
  10323. {
  10324. return res;
  10325. }
  10326. // get number string
  10327. std::vector<char> number_vector;
  10328. for (std::size_t i = 0; i < size; ++i)
  10329. {
  10330. get();
  10331. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10332. {
  10333. return false;
  10334. }
  10335. number_vector.push_back(static_cast<char>(current));
  10336. }
  10337. // parse number string
  10338. using ia_type = decltype(detail::input_adapter(number_vector));
  10339. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  10340. const auto result_number = number_lexer.scan();
  10341. const auto number_string = number_lexer.get_token_string();
  10342. const auto result_remainder = number_lexer.scan();
  10343. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10344. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10345. {
  10346. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10347. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10348. }
  10349. switch (result_number)
  10350. {
  10351. case token_type::value_integer:
  10352. return sax->number_integer(number_lexer.get_number_integer());
  10353. case token_type::value_unsigned:
  10354. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10355. case token_type::value_float:
  10356. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10357. case token_type::uninitialized:
  10358. case token_type::literal_true:
  10359. case token_type::literal_false:
  10360. case token_type::literal_null:
  10361. case token_type::value_string:
  10362. case token_type::begin_array:
  10363. case token_type::begin_object:
  10364. case token_type::end_array:
  10365. case token_type::end_object:
  10366. case token_type::name_separator:
  10367. case token_type::value_separator:
  10368. case token_type::parse_error:
  10369. case token_type::end_of_input:
  10370. case token_type::literal_or_value:
  10371. default:
  10372. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10373. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10374. }
  10375. }
  10376. ///////////////////////
  10377. // Utility functions //
  10378. ///////////////////////
  10379. /*!
  10380. @brief get next character from the input
  10381. This function provides the interface to the used input adapter. It does
  10382. not throw in case the input reached EOF, but returns a -'ve valued
  10383. `char_traits<char_type>::eof()` in that case.
  10384. @return character read from the input
  10385. */
  10386. char_int_type get()
  10387. {
  10388. ++chars_read;
  10389. return current = ia.get_character();
  10390. }
  10391. /*!
  10392. @return character read from the input after ignoring all 'N' entries
  10393. */
  10394. char_int_type get_ignore_noop()
  10395. {
  10396. do
  10397. {
  10398. get();
  10399. }
  10400. while (current == 'N');
  10401. return current;
  10402. }
  10403. /*
  10404. @brief read a number from the input
  10405. @tparam NumberType the type of the number
  10406. @param[in] format the current format (for diagnostics)
  10407. @param[out] result number of type @a NumberType
  10408. @return whether conversion completed
  10409. @note This function needs to respect the system's endianness, because
  10410. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  10411. (big endian) and therefore need reordering on little endian systems.
  10412. On the other hand, BSON and BJData use little endian and should reorder
  10413. on big endian systems.
  10414. */
  10415. template<typename NumberType, bool InputIsLittleEndian = false>
  10416. bool get_number(const input_format_t format, NumberType& result)
  10417. {
  10418. // step 1: read input into array with system's byte order
  10419. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  10420. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  10421. {
  10422. get();
  10423. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  10424. {
  10425. return false;
  10426. }
  10427. // reverse byte order prior to conversion if necessary
  10428. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  10429. {
  10430. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  10431. }
  10432. else
  10433. {
  10434. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  10435. }
  10436. }
  10437. // step 2: convert array into number of type T and return
  10438. std::memcpy(&result, vec.data(), sizeof(NumberType));
  10439. return true;
  10440. }
  10441. /*!
  10442. @brief create a string by reading characters from the input
  10443. @tparam NumberType the type of the number
  10444. @param[in] format the current format (for diagnostics)
  10445. @param[in] len number of characters to read
  10446. @param[out] result string created by reading @a len bytes
  10447. @return whether string creation completed
  10448. @note We can not reserve @a len bytes for the result, because @a len
  10449. may be too large. Usually, @ref unexpect_eof() detects the end of
  10450. the input before we run out of string memory.
  10451. */
  10452. template<typename NumberType>
  10453. bool get_string(const input_format_t format,
  10454. const NumberType len,
  10455. string_t& result)
  10456. {
  10457. bool success = true;
  10458. for (NumberType i = 0; i < len; i++)
  10459. {
  10460. get();
  10461. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  10462. {
  10463. success = false;
  10464. break;
  10465. }
  10466. result.push_back(static_cast<typename string_t::value_type>(current));
  10467. }
  10468. return success;
  10469. }
  10470. /*!
  10471. @brief create a byte array by reading bytes from the input
  10472. @tparam NumberType the type of the number
  10473. @param[in] format the current format (for diagnostics)
  10474. @param[in] len number of bytes to read
  10475. @param[out] result byte array created by reading @a len bytes
  10476. @return whether byte array creation completed
  10477. @note We can not reserve @a len bytes for the result, because @a len
  10478. may be too large. Usually, @ref unexpect_eof() detects the end of
  10479. the input before we run out of memory.
  10480. */
  10481. template<typename NumberType>
  10482. bool get_binary(const input_format_t format,
  10483. const NumberType len,
  10484. binary_t& result)
  10485. {
  10486. bool success = true;
  10487. for (NumberType i = 0; i < len; i++)
  10488. {
  10489. get();
  10490. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  10491. {
  10492. success = false;
  10493. break;
  10494. }
  10495. result.push_back(static_cast<std::uint8_t>(current));
  10496. }
  10497. return success;
  10498. }
  10499. /*!
  10500. @param[in] format the current format (for diagnostics)
  10501. @param[in] context further context information (for diagnostics)
  10502. @return whether the last read character is not EOF
  10503. */
  10504. JSON_HEDLEY_NON_NULL(3)
  10505. bool unexpect_eof(const input_format_t format, const char* context) const
  10506. {
  10507. if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
  10508. {
  10509. return sax->parse_error(chars_read, "<end of file>",
  10510. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10511. }
  10512. return true;
  10513. }
  10514. /*!
  10515. @return a string representation of the last read byte
  10516. */
  10517. std::string get_token_string() const
  10518. {
  10519. std::array<char, 3> cr{{}};
  10520. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  10521. return std::string{cr.data()};
  10522. }
  10523. /*!
  10524. @param[in] format the current format
  10525. @param[in] detail a detailed error message
  10526. @param[in] context further context information
  10527. @return a message string to use in the parse_error exceptions
  10528. */
  10529. std::string exception_message(const input_format_t format,
  10530. const std::string& detail,
  10531. const std::string& context) const
  10532. {
  10533. std::string error_msg = "syntax error while parsing ";
  10534. switch (format)
  10535. {
  10536. case input_format_t::cbor:
  10537. error_msg += "CBOR";
  10538. break;
  10539. case input_format_t::msgpack:
  10540. error_msg += "MessagePack";
  10541. break;
  10542. case input_format_t::ubjson:
  10543. error_msg += "UBJSON";
  10544. break;
  10545. case input_format_t::bson:
  10546. error_msg += "BSON";
  10547. break;
  10548. case input_format_t::bjdata:
  10549. error_msg += "BJData";
  10550. break;
  10551. case input_format_t::json: // LCOV_EXCL_LINE
  10552. default: // LCOV_EXCL_LINE
  10553. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  10554. }
  10555. return concat(error_msg, ' ', context, ": ", detail);
  10556. }
  10557. private:
  10558. static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
  10559. /// input adapter
  10560. InputAdapterType ia;
  10561. /// the current character
  10562. char_int_type current = char_traits<char_type>::eof();
  10563. /// the number of characters read
  10564. std::size_t chars_read = 0;
  10565. /// whether we can assume little endianness
  10566. const bool is_little_endian = little_endianness();
  10567. /// input format
  10568. const input_format_t input_format = input_format_t::json;
  10569. /// the SAX parser
  10570. json_sax_t* sax = nullptr;
  10571. // excluded markers in bjdata optimized type
  10572. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  10573. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  10574. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  10575. make_array<bjd_type>( \
  10576. bjd_type{'C', "char"}, \
  10577. bjd_type{'D', "double"}, \
  10578. bjd_type{'I', "int16"}, \
  10579. bjd_type{'L', "int64"}, \
  10580. bjd_type{'M', "uint64"}, \
  10581. bjd_type{'U', "uint8"}, \
  10582. bjd_type{'d', "single"}, \
  10583. bjd_type{'i', "int8"}, \
  10584. bjd_type{'l', "int32"}, \
  10585. bjd_type{'m', "uint32"}, \
  10586. bjd_type{'u', "uint16"})
  10587. JSON_PRIVATE_UNLESS_TESTED:
  10588. // lookup tables
  10589. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10590. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  10591. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  10592. using bjd_type = std::pair<char_int_type, string_t>;
  10593. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10594. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  10595. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  10596. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  10597. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  10598. };
  10599. #ifndef JSON_HAS_CPP_17
  10600. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  10601. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  10602. #endif
  10603. } // namespace detail
  10604. NLOHMANN_JSON_NAMESPACE_END
  10605. // #include <nlohmann/detail/input/input_adapters.hpp>
  10606. // #include <nlohmann/detail/input/lexer.hpp>
  10607. // #include <nlohmann/detail/input/parser.hpp>
  10608. // __ _____ _____ _____
  10609. // __| | __| | | | JSON for Modern C++
  10610. // | | |__ | | | | | | version 3.11.3
  10611. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10612. //
  10613. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  10614. // SPDX-License-Identifier: MIT
  10615. #include <cmath> // isfinite
  10616. #include <cstdint> // uint8_t
  10617. #include <functional> // function
  10618. #include <string> // string
  10619. #include <utility> // move
  10620. #include <vector> // vector
  10621. // #include <nlohmann/detail/exceptions.hpp>
  10622. // #include <nlohmann/detail/input/input_adapters.hpp>
  10623. // #include <nlohmann/detail/input/json_sax.hpp>
  10624. // #include <nlohmann/detail/input/lexer.hpp>
  10625. // #include <nlohmann/detail/macro_scope.hpp>
  10626. // #include <nlohmann/detail/meta/is_sax.hpp>
  10627. // #include <nlohmann/detail/string_concat.hpp>
  10628. // #include <nlohmann/detail/value_t.hpp>
  10629. NLOHMANN_JSON_NAMESPACE_BEGIN
  10630. namespace detail
  10631. {
  10632. ////////////
  10633. // parser //
  10634. ////////////
  10635. enum class parse_event_t : std::uint8_t
  10636. {
  10637. /// the parser read `{` and started to process a JSON object
  10638. object_start,
  10639. /// the parser read `}` and finished processing a JSON object
  10640. object_end,
  10641. /// the parser read `[` and started to process a JSON array
  10642. array_start,
  10643. /// the parser read `]` and finished processing a JSON array
  10644. array_end,
  10645. /// the parser read a key of a value in an object
  10646. key,
  10647. /// the parser finished reading a JSON value
  10648. value
  10649. };
  10650. template<typename BasicJsonType>
  10651. using parser_callback_t =
  10652. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  10653. /*!
  10654. @brief syntax analysis
  10655. This class implements a recursive descent parser.
  10656. */
  10657. template<typename BasicJsonType, typename InputAdapterType>
  10658. class parser
  10659. {
  10660. using number_integer_t = typename BasicJsonType::number_integer_t;
  10661. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  10662. using number_float_t = typename BasicJsonType::number_float_t;
  10663. using string_t = typename BasicJsonType::string_t;
  10664. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  10665. using token_type = typename lexer_t::token_type;
  10666. public:
  10667. /// a parser reading from an input adapter
  10668. explicit parser(InputAdapterType&& adapter,
  10669. const parser_callback_t<BasicJsonType> cb = nullptr,
  10670. const bool allow_exceptions_ = true,
  10671. const bool skip_comments = false)
  10672. : callback(cb)
  10673. , m_lexer(std::move(adapter), skip_comments)
  10674. , allow_exceptions(allow_exceptions_)
  10675. {
  10676. // read first token
  10677. get_token();
  10678. }
  10679. /*!
  10680. @brief public parser interface
  10681. @param[in] strict whether to expect the last token to be EOF
  10682. @param[in,out] result parsed JSON value
  10683. @throw parse_error.101 in case of an unexpected token
  10684. @throw parse_error.102 if to_unicode fails or surrogate error
  10685. @throw parse_error.103 if to_unicode fails
  10686. */
  10687. void parse(const bool strict, BasicJsonType& result)
  10688. {
  10689. if (callback)
  10690. {
  10691. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  10692. sax_parse_internal(&sdp);
  10693. // in strict mode, input must be completely read
  10694. if (strict && (get_token() != token_type::end_of_input))
  10695. {
  10696. sdp.parse_error(m_lexer.get_position(),
  10697. m_lexer.get_token_string(),
  10698. parse_error::create(101, m_lexer.get_position(),
  10699. exception_message(token_type::end_of_input, "value"), nullptr));
  10700. }
  10701. // in case of an error, return discarded value
  10702. if (sdp.is_errored())
  10703. {
  10704. result = value_t::discarded;
  10705. return;
  10706. }
  10707. // set top-level value to null if it was discarded by the callback
  10708. // function
  10709. if (result.is_discarded())
  10710. {
  10711. result = nullptr;
  10712. }
  10713. }
  10714. else
  10715. {
  10716. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  10717. sax_parse_internal(&sdp);
  10718. // in strict mode, input must be completely read
  10719. if (strict && (get_token() != token_type::end_of_input))
  10720. {
  10721. sdp.parse_error(m_lexer.get_position(),
  10722. m_lexer.get_token_string(),
  10723. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10724. }
  10725. // in case of an error, return discarded value
  10726. if (sdp.is_errored())
  10727. {
  10728. result = value_t::discarded;
  10729. return;
  10730. }
  10731. }
  10732. result.assert_invariant();
  10733. }
  10734. /*!
  10735. @brief public accept interface
  10736. @param[in] strict whether to expect the last token to be EOF
  10737. @return whether the input is a proper JSON text
  10738. */
  10739. bool accept(const bool strict = true)
  10740. {
  10741. json_sax_acceptor<BasicJsonType> sax_acceptor;
  10742. return sax_parse(&sax_acceptor, strict);
  10743. }
  10744. template<typename SAX>
  10745. JSON_HEDLEY_NON_NULL(2)
  10746. bool sax_parse(SAX* sax, const bool strict = true)
  10747. {
  10748. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  10749. const bool result = sax_parse_internal(sax);
  10750. // strict mode: next byte must be EOF
  10751. if (result && strict && (get_token() != token_type::end_of_input))
  10752. {
  10753. return sax->parse_error(m_lexer.get_position(),
  10754. m_lexer.get_token_string(),
  10755. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10756. }
  10757. return result;
  10758. }
  10759. private:
  10760. template<typename SAX>
  10761. JSON_HEDLEY_NON_NULL(2)
  10762. bool sax_parse_internal(SAX* sax)
  10763. {
  10764. // stack to remember the hierarchy of structured values we are parsing
  10765. // true = array; false = object
  10766. std::vector<bool> states;
  10767. // value to avoid a goto (see comment where set to true)
  10768. bool skip_to_state_evaluation = false;
  10769. while (true)
  10770. {
  10771. if (!skip_to_state_evaluation)
  10772. {
  10773. // invariant: get_token() was called before each iteration
  10774. switch (last_token)
  10775. {
  10776. case token_type::begin_object:
  10777. {
  10778. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10779. {
  10780. return false;
  10781. }
  10782. // closing } -> we are done
  10783. if (get_token() == token_type::end_object)
  10784. {
  10785. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10786. {
  10787. return false;
  10788. }
  10789. break;
  10790. }
  10791. // parse key
  10792. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  10793. {
  10794. return sax->parse_error(m_lexer.get_position(),
  10795. m_lexer.get_token_string(),
  10796. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10797. }
  10798. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10799. {
  10800. return false;
  10801. }
  10802. // parse separator (:)
  10803. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10804. {
  10805. return sax->parse_error(m_lexer.get_position(),
  10806. m_lexer.get_token_string(),
  10807. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10808. }
  10809. // remember we are now inside an object
  10810. states.push_back(false);
  10811. // parse values
  10812. get_token();
  10813. continue;
  10814. }
  10815. case token_type::begin_array:
  10816. {
  10817. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10818. {
  10819. return false;
  10820. }
  10821. // closing ] -> we are done
  10822. if (get_token() == token_type::end_array)
  10823. {
  10824. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10825. {
  10826. return false;
  10827. }
  10828. break;
  10829. }
  10830. // remember we are now inside an array
  10831. states.push_back(true);
  10832. // parse values (no need to call get_token)
  10833. continue;
  10834. }
  10835. case token_type::value_float:
  10836. {
  10837. const auto res = m_lexer.get_number_float();
  10838. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  10839. {
  10840. return sax->parse_error(m_lexer.get_position(),
  10841. m_lexer.get_token_string(),
  10842. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  10843. }
  10844. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  10845. {
  10846. return false;
  10847. }
  10848. break;
  10849. }
  10850. case token_type::literal_false:
  10851. {
  10852. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  10853. {
  10854. return false;
  10855. }
  10856. break;
  10857. }
  10858. case token_type::literal_null:
  10859. {
  10860. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  10861. {
  10862. return false;
  10863. }
  10864. break;
  10865. }
  10866. case token_type::literal_true:
  10867. {
  10868. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  10869. {
  10870. return false;
  10871. }
  10872. break;
  10873. }
  10874. case token_type::value_integer:
  10875. {
  10876. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  10877. {
  10878. return false;
  10879. }
  10880. break;
  10881. }
  10882. case token_type::value_string:
  10883. {
  10884. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  10885. {
  10886. return false;
  10887. }
  10888. break;
  10889. }
  10890. case token_type::value_unsigned:
  10891. {
  10892. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  10893. {
  10894. return false;
  10895. }
  10896. break;
  10897. }
  10898. case token_type::parse_error:
  10899. {
  10900. // using "uninitialized" to avoid "expected" message
  10901. return sax->parse_error(m_lexer.get_position(),
  10902. m_lexer.get_token_string(),
  10903. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  10904. }
  10905. case token_type::end_of_input:
  10906. {
  10907. if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
  10908. {
  10909. return sax->parse_error(m_lexer.get_position(),
  10910. m_lexer.get_token_string(),
  10911. parse_error::create(101, m_lexer.get_position(),
  10912. "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
  10913. }
  10914. return sax->parse_error(m_lexer.get_position(),
  10915. m_lexer.get_token_string(),
  10916. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10917. }
  10918. case token_type::uninitialized:
  10919. case token_type::end_array:
  10920. case token_type::end_object:
  10921. case token_type::name_separator:
  10922. case token_type::value_separator:
  10923. case token_type::literal_or_value:
  10924. default: // the last token was unexpected
  10925. {
  10926. return sax->parse_error(m_lexer.get_position(),
  10927. m_lexer.get_token_string(),
  10928. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10929. }
  10930. }
  10931. }
  10932. else
  10933. {
  10934. skip_to_state_evaluation = false;
  10935. }
  10936. // we reached this line after we successfully parsed a value
  10937. if (states.empty())
  10938. {
  10939. // empty stack: we reached the end of the hierarchy: done
  10940. return true;
  10941. }
  10942. if (states.back()) // array
  10943. {
  10944. // comma -> next value
  10945. if (get_token() == token_type::value_separator)
  10946. {
  10947. // parse a new value
  10948. get_token();
  10949. continue;
  10950. }
  10951. // closing ]
  10952. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  10953. {
  10954. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10955. {
  10956. return false;
  10957. }
  10958. // We are done with this array. Before we can parse a
  10959. // new value, we need to evaluate the new state first.
  10960. // By setting skip_to_state_evaluation to false, we
  10961. // are effectively jumping to the beginning of this if.
  10962. JSON_ASSERT(!states.empty());
  10963. states.pop_back();
  10964. skip_to_state_evaluation = true;
  10965. continue;
  10966. }
  10967. return sax->parse_error(m_lexer.get_position(),
  10968. m_lexer.get_token_string(),
  10969. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  10970. }
  10971. // states.back() is false -> object
  10972. // comma -> next value
  10973. if (get_token() == token_type::value_separator)
  10974. {
  10975. // parse key
  10976. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  10977. {
  10978. return sax->parse_error(m_lexer.get_position(),
  10979. m_lexer.get_token_string(),
  10980. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10981. }
  10982. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10983. {
  10984. return false;
  10985. }
  10986. // parse separator (:)
  10987. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10988. {
  10989. return sax->parse_error(m_lexer.get_position(),
  10990. m_lexer.get_token_string(),
  10991. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10992. }
  10993. // parse values
  10994. get_token();
  10995. continue;
  10996. }
  10997. // closing }
  10998. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  10999. {
  11000. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  11001. {
  11002. return false;
  11003. }
  11004. // We are done with this object. Before we can parse a
  11005. // new value, we need to evaluate the new state first.
  11006. // By setting skip_to_state_evaluation to false, we
  11007. // are effectively jumping to the beginning of this if.
  11008. JSON_ASSERT(!states.empty());
  11009. states.pop_back();
  11010. skip_to_state_evaluation = true;
  11011. continue;
  11012. }
  11013. return sax->parse_error(m_lexer.get_position(),
  11014. m_lexer.get_token_string(),
  11015. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  11016. }
  11017. }
  11018. /// get next token from lexer
  11019. token_type get_token()
  11020. {
  11021. return last_token = m_lexer.scan();
  11022. }
  11023. std::string exception_message(const token_type expected, const std::string& context)
  11024. {
  11025. std::string error_msg = "syntax error ";
  11026. if (!context.empty())
  11027. {
  11028. error_msg += concat("while parsing ", context, ' ');
  11029. }
  11030. error_msg += "- ";
  11031. if (last_token == token_type::parse_error)
  11032. {
  11033. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  11034. m_lexer.get_token_string(), '\'');
  11035. }
  11036. else
  11037. {
  11038. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  11039. }
  11040. if (expected != token_type::uninitialized)
  11041. {
  11042. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  11043. }
  11044. return error_msg;
  11045. }
  11046. private:
  11047. /// callback function
  11048. const parser_callback_t<BasicJsonType> callback = nullptr;
  11049. /// the type of the last read token
  11050. token_type last_token = token_type::uninitialized;
  11051. /// the lexer
  11052. lexer_t m_lexer;
  11053. /// whether to throw exceptions in case of errors
  11054. const bool allow_exceptions = true;
  11055. };
  11056. } // namespace detail
  11057. NLOHMANN_JSON_NAMESPACE_END
  11058. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11059. // __ _____ _____ _____
  11060. // __| | __| | | | JSON for Modern C++
  11061. // | | |__ | | | | | | version 3.11.3
  11062. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11063. //
  11064. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11065. // SPDX-License-Identifier: MIT
  11066. // #include <nlohmann/detail/abi_macros.hpp>
  11067. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11068. // __ _____ _____ _____
  11069. // __| | __| | | | JSON for Modern C++
  11070. // | | |__ | | | | | | version 3.11.3
  11071. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11072. //
  11073. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11074. // SPDX-License-Identifier: MIT
  11075. #include <cstddef> // ptrdiff_t
  11076. #include <limits> // numeric_limits
  11077. // #include <nlohmann/detail/macro_scope.hpp>
  11078. NLOHMANN_JSON_NAMESPACE_BEGIN
  11079. namespace detail
  11080. {
  11081. /*
  11082. @brief an iterator for primitive JSON types
  11083. This class models an iterator for primitive JSON types (boolean, number,
  11084. string). It's only purpose is to allow the iterator/const_iterator classes
  11085. to "iterate" over primitive values. Internally, the iterator is modeled by
  11086. a `difference_type` variable. Value begin_value (`0`) models the begin,
  11087. end_value (`1`) models past the end.
  11088. */
  11089. class primitive_iterator_t
  11090. {
  11091. private:
  11092. using difference_type = std::ptrdiff_t;
  11093. static constexpr difference_type begin_value = 0;
  11094. static constexpr difference_type end_value = begin_value + 1;
  11095. JSON_PRIVATE_UNLESS_TESTED:
  11096. /// iterator as signed integer type
  11097. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11098. public:
  11099. constexpr difference_type get_value() const noexcept
  11100. {
  11101. return m_it;
  11102. }
  11103. /// set iterator to a defined beginning
  11104. void set_begin() noexcept
  11105. {
  11106. m_it = begin_value;
  11107. }
  11108. /// set iterator to a defined past the end
  11109. void set_end() noexcept
  11110. {
  11111. m_it = end_value;
  11112. }
  11113. /// return whether the iterator can be dereferenced
  11114. constexpr bool is_begin() const noexcept
  11115. {
  11116. return m_it == begin_value;
  11117. }
  11118. /// return whether the iterator is at end
  11119. constexpr bool is_end() const noexcept
  11120. {
  11121. return m_it == end_value;
  11122. }
  11123. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11124. {
  11125. return lhs.m_it == rhs.m_it;
  11126. }
  11127. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11128. {
  11129. return lhs.m_it < rhs.m_it;
  11130. }
  11131. primitive_iterator_t operator+(difference_type n) noexcept
  11132. {
  11133. auto result = *this;
  11134. result += n;
  11135. return result;
  11136. }
  11137. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11138. {
  11139. return lhs.m_it - rhs.m_it;
  11140. }
  11141. primitive_iterator_t& operator++() noexcept
  11142. {
  11143. ++m_it;
  11144. return *this;
  11145. }
  11146. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11147. {
  11148. auto result = *this;
  11149. ++m_it;
  11150. return result;
  11151. }
  11152. primitive_iterator_t& operator--() noexcept
  11153. {
  11154. --m_it;
  11155. return *this;
  11156. }
  11157. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11158. {
  11159. auto result = *this;
  11160. --m_it;
  11161. return result;
  11162. }
  11163. primitive_iterator_t& operator+=(difference_type n) noexcept
  11164. {
  11165. m_it += n;
  11166. return *this;
  11167. }
  11168. primitive_iterator_t& operator-=(difference_type n) noexcept
  11169. {
  11170. m_it -= n;
  11171. return *this;
  11172. }
  11173. };
  11174. } // namespace detail
  11175. NLOHMANN_JSON_NAMESPACE_END
  11176. NLOHMANN_JSON_NAMESPACE_BEGIN
  11177. namespace detail
  11178. {
  11179. /*!
  11180. @brief an iterator value
  11181. @note This structure could easily be a union, but MSVC currently does not allow
  11182. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11183. */
  11184. template<typename BasicJsonType> struct internal_iterator
  11185. {
  11186. /// iterator for JSON objects
  11187. typename BasicJsonType::object_t::iterator object_iterator {};
  11188. /// iterator for JSON arrays
  11189. typename BasicJsonType::array_t::iterator array_iterator {};
  11190. /// generic iterator for all other types
  11191. primitive_iterator_t primitive_iterator {};
  11192. };
  11193. } // namespace detail
  11194. NLOHMANN_JSON_NAMESPACE_END
  11195. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11196. // __ _____ _____ _____
  11197. // __| | __| | | | JSON for Modern C++
  11198. // | | |__ | | | | | | version 3.11.3
  11199. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11200. //
  11201. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11202. // SPDX-License-Identifier: MIT
  11203. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11204. #include <type_traits> // conditional, is_const, remove_const
  11205. // #include <nlohmann/detail/exceptions.hpp>
  11206. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11207. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11208. // #include <nlohmann/detail/macro_scope.hpp>
  11209. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11210. // #include <nlohmann/detail/meta/type_traits.hpp>
  11211. // #include <nlohmann/detail/value_t.hpp>
  11212. NLOHMANN_JSON_NAMESPACE_BEGIN
  11213. namespace detail
  11214. {
  11215. // forward declare, to be able to friend it later on
  11216. template<typename IteratorType> class iteration_proxy;
  11217. template<typename IteratorType> class iteration_proxy_value;
  11218. /*!
  11219. @brief a template for a bidirectional iterator for the @ref basic_json class
  11220. This class implements a both iterators (iterator and const_iterator) for the
  11221. @ref basic_json class.
  11222. @note An iterator is called *initialized* when a pointer to a JSON value has
  11223. been set (e.g., by a constructor or a copy assignment). If the iterator is
  11224. default-constructed, it is *uninitialized* and most methods are undefined.
  11225. **The library uses assertions to detect calls on uninitialized iterators.**
  11226. @requirement The class satisfies the following concept requirements:
  11227. -
  11228. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11229. The iterator that can be moved can be moved in both directions (i.e.
  11230. incremented and decremented).
  11231. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  11232. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  11233. */
  11234. template<typename BasicJsonType>
  11235. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  11236. {
  11237. /// the iterator with BasicJsonType of different const-ness
  11238. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  11239. /// allow basic_json to access private members
  11240. friend other_iter_impl;
  11241. friend BasicJsonType;
  11242. friend iteration_proxy<iter_impl>;
  11243. friend iteration_proxy_value<iter_impl>;
  11244. using object_t = typename BasicJsonType::object_t;
  11245. using array_t = typename BasicJsonType::array_t;
  11246. // make sure BasicJsonType is basic_json or const basic_json
  11247. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  11248. "iter_impl only accepts (const) basic_json");
  11249. // superficial check for the LegacyBidirectionalIterator named requirement
  11250. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  11251. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  11252. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  11253. public:
  11254. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  11255. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  11256. /// A user-defined iterator should provide publicly accessible typedefs named
  11257. /// iterator_category, value_type, difference_type, pointer, and reference.
  11258. /// Note that value_type is required to be non-const, even for constant iterators.
  11259. using iterator_category = std::bidirectional_iterator_tag;
  11260. /// the type of the values when the iterator is dereferenced
  11261. using value_type = typename BasicJsonType::value_type;
  11262. /// a type to represent differences between iterators
  11263. using difference_type = typename BasicJsonType::difference_type;
  11264. /// defines a pointer to the type iterated over (value_type)
  11265. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  11266. typename BasicJsonType::const_pointer,
  11267. typename BasicJsonType::pointer>::type;
  11268. /// defines a reference to the type iterated over (value_type)
  11269. using reference =
  11270. typename std::conditional<std::is_const<BasicJsonType>::value,
  11271. typename BasicJsonType::const_reference,
  11272. typename BasicJsonType::reference>::type;
  11273. iter_impl() = default;
  11274. ~iter_impl() = default;
  11275. iter_impl(iter_impl&&) noexcept = default;
  11276. iter_impl& operator=(iter_impl&&) noexcept = default;
  11277. /*!
  11278. @brief constructor for a given JSON instance
  11279. @param[in] object pointer to a JSON object for this iterator
  11280. @pre object != nullptr
  11281. @post The iterator is initialized; i.e. `m_object != nullptr`.
  11282. */
  11283. explicit iter_impl(pointer object) noexcept : m_object(object)
  11284. {
  11285. JSON_ASSERT(m_object != nullptr);
  11286. switch (m_object->m_data.m_type)
  11287. {
  11288. case value_t::object:
  11289. {
  11290. m_it.object_iterator = typename object_t::iterator();
  11291. break;
  11292. }
  11293. case value_t::array:
  11294. {
  11295. m_it.array_iterator = typename array_t::iterator();
  11296. break;
  11297. }
  11298. case value_t::null:
  11299. case value_t::string:
  11300. case value_t::boolean:
  11301. case value_t::number_integer:
  11302. case value_t::number_unsigned:
  11303. case value_t::number_float:
  11304. case value_t::binary:
  11305. case value_t::discarded:
  11306. default:
  11307. {
  11308. m_it.primitive_iterator = primitive_iterator_t();
  11309. break;
  11310. }
  11311. }
  11312. }
  11313. /*!
  11314. @note The conventional copy constructor and copy assignment are implicitly
  11315. defined. Combined with the following converting constructor and
  11316. assignment, they support: (1) copy from iterator to iterator, (2)
  11317. copy from const iterator to const iterator, and (3) conversion from
  11318. iterator to const iterator. However conversion from const iterator
  11319. to iterator is not defined.
  11320. */
  11321. /*!
  11322. @brief const copy constructor
  11323. @param[in] other const iterator to copy from
  11324. @note This copy constructor had to be defined explicitly to circumvent a bug
  11325. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  11326. information refer to: https://github.com/nlohmann/json/issues/1608
  11327. */
  11328. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  11329. : m_object(other.m_object), m_it(other.m_it)
  11330. {}
  11331. /*!
  11332. @brief converting assignment
  11333. @param[in] other const iterator to copy from
  11334. @return const/non-const iterator
  11335. @note It is not checked whether @a other is initialized.
  11336. */
  11337. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  11338. {
  11339. if (&other != this)
  11340. {
  11341. m_object = other.m_object;
  11342. m_it = other.m_it;
  11343. }
  11344. return *this;
  11345. }
  11346. /*!
  11347. @brief converting constructor
  11348. @param[in] other non-const iterator to copy from
  11349. @note It is not checked whether @a other is initialized.
  11350. */
  11351. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  11352. : m_object(other.m_object), m_it(other.m_it)
  11353. {}
  11354. /*!
  11355. @brief converting assignment
  11356. @param[in] other non-const iterator to copy from
  11357. @return const/non-const iterator
  11358. @note It is not checked whether @a other is initialized.
  11359. */
  11360. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  11361. {
  11362. m_object = other.m_object;
  11363. m_it = other.m_it;
  11364. return *this;
  11365. }
  11366. JSON_PRIVATE_UNLESS_TESTED:
  11367. /*!
  11368. @brief set the iterator to the first value
  11369. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11370. */
  11371. void set_begin() noexcept
  11372. {
  11373. JSON_ASSERT(m_object != nullptr);
  11374. switch (m_object->m_data.m_type)
  11375. {
  11376. case value_t::object:
  11377. {
  11378. m_it.object_iterator = m_object->m_data.m_value.object->begin();
  11379. break;
  11380. }
  11381. case value_t::array:
  11382. {
  11383. m_it.array_iterator = m_object->m_data.m_value.array->begin();
  11384. break;
  11385. }
  11386. case value_t::null:
  11387. {
  11388. // set to end so begin()==end() is true: null is empty
  11389. m_it.primitive_iterator.set_end();
  11390. break;
  11391. }
  11392. case value_t::string:
  11393. case value_t::boolean:
  11394. case value_t::number_integer:
  11395. case value_t::number_unsigned:
  11396. case value_t::number_float:
  11397. case value_t::binary:
  11398. case value_t::discarded:
  11399. default:
  11400. {
  11401. m_it.primitive_iterator.set_begin();
  11402. break;
  11403. }
  11404. }
  11405. }
  11406. /*!
  11407. @brief set the iterator past the last value
  11408. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11409. */
  11410. void set_end() noexcept
  11411. {
  11412. JSON_ASSERT(m_object != nullptr);
  11413. switch (m_object->m_data.m_type)
  11414. {
  11415. case value_t::object:
  11416. {
  11417. m_it.object_iterator = m_object->m_data.m_value.object->end();
  11418. break;
  11419. }
  11420. case value_t::array:
  11421. {
  11422. m_it.array_iterator = m_object->m_data.m_value.array->end();
  11423. break;
  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. m_it.primitive_iterator.set_end();
  11436. break;
  11437. }
  11438. }
  11439. }
  11440. public:
  11441. /*!
  11442. @brief return a reference to the value pointed to by the iterator
  11443. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11444. */
  11445. reference operator*() const
  11446. {
  11447. JSON_ASSERT(m_object != nullptr);
  11448. switch (m_object->m_data.m_type)
  11449. {
  11450. case value_t::object:
  11451. {
  11452. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  11453. return m_it.object_iterator->second;
  11454. }
  11455. case value_t::array:
  11456. {
  11457. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  11458. return *m_it.array_iterator;
  11459. }
  11460. case value_t::null:
  11461. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11462. case value_t::string:
  11463. case value_t::boolean:
  11464. case value_t::number_integer:
  11465. case value_t::number_unsigned:
  11466. case value_t::number_float:
  11467. case value_t::binary:
  11468. case value_t::discarded:
  11469. default:
  11470. {
  11471. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11472. {
  11473. return *m_object;
  11474. }
  11475. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11476. }
  11477. }
  11478. }
  11479. /*!
  11480. @brief dereference the iterator
  11481. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11482. */
  11483. pointer operator->() const
  11484. {
  11485. JSON_ASSERT(m_object != nullptr);
  11486. switch (m_object->m_data.m_type)
  11487. {
  11488. case value_t::object:
  11489. {
  11490. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  11491. return &(m_it.object_iterator->second);
  11492. }
  11493. case value_t::array:
  11494. {
  11495. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  11496. return &*m_it.array_iterator;
  11497. }
  11498. case value_t::null:
  11499. case value_t::string:
  11500. case value_t::boolean:
  11501. case value_t::number_integer:
  11502. case value_t::number_unsigned:
  11503. case value_t::number_float:
  11504. case value_t::binary:
  11505. case value_t::discarded:
  11506. default:
  11507. {
  11508. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11509. {
  11510. return m_object;
  11511. }
  11512. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11513. }
  11514. }
  11515. }
  11516. /*!
  11517. @brief post-increment (it++)
  11518. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11519. */
  11520. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  11521. {
  11522. auto result = *this;
  11523. ++(*this);
  11524. return result;
  11525. }
  11526. /*!
  11527. @brief pre-increment (++it)
  11528. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11529. */
  11530. iter_impl& operator++()
  11531. {
  11532. JSON_ASSERT(m_object != nullptr);
  11533. switch (m_object->m_data.m_type)
  11534. {
  11535. case value_t::object:
  11536. {
  11537. std::advance(m_it.object_iterator, 1);
  11538. break;
  11539. }
  11540. case value_t::array:
  11541. {
  11542. std::advance(m_it.array_iterator, 1);
  11543. break;
  11544. }
  11545. case value_t::null:
  11546. case value_t::string:
  11547. case value_t::boolean:
  11548. case value_t::number_integer:
  11549. case value_t::number_unsigned:
  11550. case value_t::number_float:
  11551. case value_t::binary:
  11552. case value_t::discarded:
  11553. default:
  11554. {
  11555. ++m_it.primitive_iterator;
  11556. break;
  11557. }
  11558. }
  11559. return *this;
  11560. }
  11561. /*!
  11562. @brief post-decrement (it--)
  11563. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11564. */
  11565. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  11566. {
  11567. auto result = *this;
  11568. --(*this);
  11569. return result;
  11570. }
  11571. /*!
  11572. @brief pre-decrement (--it)
  11573. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11574. */
  11575. iter_impl& operator--()
  11576. {
  11577. JSON_ASSERT(m_object != nullptr);
  11578. switch (m_object->m_data.m_type)
  11579. {
  11580. case value_t::object:
  11581. {
  11582. std::advance(m_it.object_iterator, -1);
  11583. break;
  11584. }
  11585. case value_t::array:
  11586. {
  11587. std::advance(m_it.array_iterator, -1);
  11588. break;
  11589. }
  11590. case value_t::null:
  11591. case value_t::string:
  11592. case value_t::boolean:
  11593. case value_t::number_integer:
  11594. case value_t::number_unsigned:
  11595. case value_t::number_float:
  11596. case value_t::binary:
  11597. case value_t::discarded:
  11598. default:
  11599. {
  11600. --m_it.primitive_iterator;
  11601. break;
  11602. }
  11603. }
  11604. return *this;
  11605. }
  11606. /*!
  11607. @brief comparison: equal
  11608. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11609. */
  11610. 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 >
  11611. bool operator==(const IterImpl& other) const
  11612. {
  11613. // if objects are not the same, the comparison is undefined
  11614. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11615. {
  11616. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11617. }
  11618. JSON_ASSERT(m_object != nullptr);
  11619. switch (m_object->m_data.m_type)
  11620. {
  11621. case value_t::object:
  11622. return (m_it.object_iterator == other.m_it.object_iterator);
  11623. case value_t::array:
  11624. return (m_it.array_iterator == other.m_it.array_iterator);
  11625. case value_t::null:
  11626. case value_t::string:
  11627. case value_t::boolean:
  11628. case value_t::number_integer:
  11629. case value_t::number_unsigned:
  11630. case value_t::number_float:
  11631. case value_t::binary:
  11632. case value_t::discarded:
  11633. default:
  11634. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  11635. }
  11636. }
  11637. /*!
  11638. @brief comparison: not equal
  11639. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11640. */
  11641. 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 >
  11642. bool operator!=(const IterImpl& other) const
  11643. {
  11644. return !operator==(other);
  11645. }
  11646. /*!
  11647. @brief comparison: smaller
  11648. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11649. */
  11650. bool operator<(const iter_impl& other) const
  11651. {
  11652. // if objects are not the same, the comparison is undefined
  11653. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11654. {
  11655. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11656. }
  11657. JSON_ASSERT(m_object != nullptr);
  11658. switch (m_object->m_data.m_type)
  11659. {
  11660. case value_t::object:
  11661. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  11662. case value_t::array:
  11663. return (m_it.array_iterator < other.m_it.array_iterator);
  11664. case value_t::null:
  11665. case value_t::string:
  11666. case value_t::boolean:
  11667. case value_t::number_integer:
  11668. case value_t::number_unsigned:
  11669. case value_t::number_float:
  11670. case value_t::binary:
  11671. case value_t::discarded:
  11672. default:
  11673. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  11674. }
  11675. }
  11676. /*!
  11677. @brief comparison: less than or equal
  11678. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11679. */
  11680. bool operator<=(const iter_impl& other) const
  11681. {
  11682. return !other.operator < (*this);
  11683. }
  11684. /*!
  11685. @brief comparison: greater than
  11686. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11687. */
  11688. bool operator>(const iter_impl& other) const
  11689. {
  11690. return !operator<=(other);
  11691. }
  11692. /*!
  11693. @brief comparison: greater than or equal
  11694. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11695. */
  11696. bool operator>=(const iter_impl& other) const
  11697. {
  11698. return !operator<(other);
  11699. }
  11700. /*!
  11701. @brief add to iterator
  11702. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11703. */
  11704. iter_impl& operator+=(difference_type i)
  11705. {
  11706. JSON_ASSERT(m_object != nullptr);
  11707. switch (m_object->m_data.m_type)
  11708. {
  11709. case value_t::object:
  11710. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11711. case value_t::array:
  11712. {
  11713. std::advance(m_it.array_iterator, i);
  11714. break;
  11715. }
  11716. case value_t::null:
  11717. case value_t::string:
  11718. case value_t::boolean:
  11719. case value_t::number_integer:
  11720. case value_t::number_unsigned:
  11721. case value_t::number_float:
  11722. case value_t::binary:
  11723. case value_t::discarded:
  11724. default:
  11725. {
  11726. m_it.primitive_iterator += i;
  11727. break;
  11728. }
  11729. }
  11730. return *this;
  11731. }
  11732. /*!
  11733. @brief subtract from iterator
  11734. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11735. */
  11736. iter_impl& operator-=(difference_type i)
  11737. {
  11738. return operator+=(-i);
  11739. }
  11740. /*!
  11741. @brief add to iterator
  11742. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11743. */
  11744. iter_impl operator+(difference_type i) const
  11745. {
  11746. auto result = *this;
  11747. result += i;
  11748. return result;
  11749. }
  11750. /*!
  11751. @brief addition of distance and iterator
  11752. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11753. */
  11754. friend iter_impl operator+(difference_type i, const iter_impl& it)
  11755. {
  11756. auto result = it;
  11757. result += i;
  11758. return result;
  11759. }
  11760. /*!
  11761. @brief subtract from iterator
  11762. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11763. */
  11764. iter_impl operator-(difference_type i) const
  11765. {
  11766. auto result = *this;
  11767. result -= i;
  11768. return result;
  11769. }
  11770. /*!
  11771. @brief return difference
  11772. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11773. */
  11774. difference_type operator-(const iter_impl& other) const
  11775. {
  11776. JSON_ASSERT(m_object != nullptr);
  11777. switch (m_object->m_data.m_type)
  11778. {
  11779. case value_t::object:
  11780. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11781. case value_t::array:
  11782. return m_it.array_iterator - other.m_it.array_iterator;
  11783. case value_t::null:
  11784. case value_t::string:
  11785. case value_t::boolean:
  11786. case value_t::number_integer:
  11787. case value_t::number_unsigned:
  11788. case value_t::number_float:
  11789. case value_t::binary:
  11790. case value_t::discarded:
  11791. default:
  11792. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  11793. }
  11794. }
  11795. /*!
  11796. @brief access to successor
  11797. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11798. */
  11799. reference operator[](difference_type n) const
  11800. {
  11801. JSON_ASSERT(m_object != nullptr);
  11802. switch (m_object->m_data.m_type)
  11803. {
  11804. case value_t::object:
  11805. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  11806. case value_t::array:
  11807. return *std::next(m_it.array_iterator, n);
  11808. case value_t::null:
  11809. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11810. case value_t::string:
  11811. case value_t::boolean:
  11812. case value_t::number_integer:
  11813. case value_t::number_unsigned:
  11814. case value_t::number_float:
  11815. case value_t::binary:
  11816. case value_t::discarded:
  11817. default:
  11818. {
  11819. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  11820. {
  11821. return *m_object;
  11822. }
  11823. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11824. }
  11825. }
  11826. }
  11827. /*!
  11828. @brief return the key of an object iterator
  11829. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11830. */
  11831. const typename object_t::key_type& key() const
  11832. {
  11833. JSON_ASSERT(m_object != nullptr);
  11834. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  11835. {
  11836. return m_it.object_iterator->first;
  11837. }
  11838. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  11839. }
  11840. /*!
  11841. @brief return the value of an iterator
  11842. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11843. */
  11844. reference value() const
  11845. {
  11846. return operator*();
  11847. }
  11848. JSON_PRIVATE_UNLESS_TESTED:
  11849. /// associated JSON instance
  11850. pointer m_object = nullptr;
  11851. /// the actual iterator of the associated instance
  11852. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  11853. };
  11854. } // namespace detail
  11855. NLOHMANN_JSON_NAMESPACE_END
  11856. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  11857. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  11858. // __ _____ _____ _____
  11859. // __| | __| | | | JSON for Modern C++
  11860. // | | |__ | | | | | | version 3.11.3
  11861. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11862. //
  11863. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11864. // SPDX-License-Identifier: MIT
  11865. #include <cstddef> // ptrdiff_t
  11866. #include <iterator> // reverse_iterator
  11867. #include <utility> // declval
  11868. // #include <nlohmann/detail/abi_macros.hpp>
  11869. NLOHMANN_JSON_NAMESPACE_BEGIN
  11870. namespace detail
  11871. {
  11872. //////////////////////
  11873. // reverse_iterator //
  11874. //////////////////////
  11875. /*!
  11876. @brief a template for a reverse iterator class
  11877. @tparam Base the base iterator type to reverse. Valid types are @ref
  11878. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  11879. create @ref const_reverse_iterator).
  11880. @requirement The class satisfies the following concept requirements:
  11881. -
  11882. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11883. The iterator that can be moved can be moved in both directions (i.e.
  11884. incremented and decremented).
  11885. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  11886. It is possible to write to the pointed-to element (only if @a Base is
  11887. @ref iterator).
  11888. @since version 1.0.0
  11889. */
  11890. template<typename Base>
  11891. class json_reverse_iterator : public std::reverse_iterator<Base>
  11892. {
  11893. public:
  11894. using difference_type = std::ptrdiff_t;
  11895. /// shortcut to the reverse iterator adapter
  11896. using base_iterator = std::reverse_iterator<Base>;
  11897. /// the reference type for the pointed-to element
  11898. using reference = typename Base::reference;
  11899. /// create reverse iterator from iterator
  11900. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  11901. : base_iterator(it) {}
  11902. /// create reverse iterator from base class
  11903. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  11904. /// post-increment (it++)
  11905. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  11906. {
  11907. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  11908. }
  11909. /// pre-increment (++it)
  11910. json_reverse_iterator& operator++()
  11911. {
  11912. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  11913. }
  11914. /// post-decrement (it--)
  11915. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  11916. {
  11917. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  11918. }
  11919. /// pre-decrement (--it)
  11920. json_reverse_iterator& operator--()
  11921. {
  11922. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  11923. }
  11924. /// add to iterator
  11925. json_reverse_iterator& operator+=(difference_type i)
  11926. {
  11927. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  11928. }
  11929. /// add to iterator
  11930. json_reverse_iterator operator+(difference_type i) const
  11931. {
  11932. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  11933. }
  11934. /// subtract from iterator
  11935. json_reverse_iterator operator-(difference_type i) const
  11936. {
  11937. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  11938. }
  11939. /// return difference
  11940. difference_type operator-(const json_reverse_iterator& other) const
  11941. {
  11942. return base_iterator(*this) - base_iterator(other);
  11943. }
  11944. /// access to successor
  11945. reference operator[](difference_type n) const
  11946. {
  11947. return *(this->operator+(n));
  11948. }
  11949. /// return the key of an object iterator
  11950. auto key() const -> decltype(std::declval<Base>().key())
  11951. {
  11952. auto it = --this->base();
  11953. return it.key();
  11954. }
  11955. /// return the value of an iterator
  11956. reference value() const
  11957. {
  11958. auto it = --this->base();
  11959. return it.operator * ();
  11960. }
  11961. };
  11962. } // namespace detail
  11963. NLOHMANN_JSON_NAMESPACE_END
  11964. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11965. // #include <nlohmann/detail/json_custom_base_class.hpp>
  11966. // __ _____ _____ _____
  11967. // __| | __| | | | JSON for Modern C++
  11968. // | | |__ | | | | | | version 3.11.3
  11969. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11970. //
  11971. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  11972. // SPDX-License-Identifier: MIT
  11973. #include <type_traits> // conditional, is_same
  11974. // #include <nlohmann/detail/abi_macros.hpp>
  11975. NLOHMANN_JSON_NAMESPACE_BEGIN
  11976. namespace detail
  11977. {
  11978. /*!
  11979. @brief Default base class of the @ref basic_json class.
  11980. So that the correct implementations of the copy / move ctors / assign operators
  11981. of @ref basic_json do not require complex case distinctions
  11982. (no base class / custom base class used as customization point),
  11983. @ref basic_json always has a base class.
  11984. By default, this class is used because it is empty and thus has no effect
  11985. on the behavior of @ref basic_json.
  11986. */
  11987. struct json_default_base {};
  11988. template<class T>
  11989. using json_base_class = typename std::conditional <
  11990. std::is_same<T, void>::value,
  11991. json_default_base,
  11992. T
  11993. >::type;
  11994. } // namespace detail
  11995. NLOHMANN_JSON_NAMESPACE_END
  11996. // #include <nlohmann/detail/json_pointer.hpp>
  11997. // __ _____ _____ _____
  11998. // __| | __| | | | JSON for Modern C++
  11999. // | | |__ | | | | | | version 3.11.3
  12000. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12001. //
  12002. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12003. // SPDX-License-Identifier: MIT
  12004. #include <algorithm> // all_of
  12005. #include <cctype> // isdigit
  12006. #include <cerrno> // errno, ERANGE
  12007. #include <cstdlib> // strtoull
  12008. #ifndef JSON_NO_IO
  12009. #include <iosfwd> // ostream
  12010. #endif // JSON_NO_IO
  12011. #include <limits> // max
  12012. #include <numeric> // accumulate
  12013. #include <string> // string
  12014. #include <utility> // move
  12015. #include <vector> // vector
  12016. // #include <nlohmann/detail/exceptions.hpp>
  12017. // #include <nlohmann/detail/macro_scope.hpp>
  12018. // #include <nlohmann/detail/string_concat.hpp>
  12019. // #include <nlohmann/detail/string_escape.hpp>
  12020. // #include <nlohmann/detail/value_t.hpp>
  12021. NLOHMANN_JSON_NAMESPACE_BEGIN
  12022. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  12023. /// @sa https://json.nlohmann.me/api/json_pointer/
  12024. template<typename RefStringType>
  12025. class json_pointer
  12026. {
  12027. // allow basic_json to access private members
  12028. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12029. friend class basic_json;
  12030. template<typename>
  12031. friend class json_pointer;
  12032. template<typename T>
  12033. struct string_t_helper
  12034. {
  12035. using type = T;
  12036. };
  12037. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  12038. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  12039. {
  12040. using type = StringType;
  12041. };
  12042. public:
  12043. // for backwards compatibility accept BasicJsonType
  12044. using string_t = typename string_t_helper<RefStringType>::type;
  12045. /// @brief create JSON pointer
  12046. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  12047. explicit json_pointer(const string_t& s = "")
  12048. : reference_tokens(split(s))
  12049. {}
  12050. /// @brief return a string representation of the JSON pointer
  12051. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  12052. string_t to_string() const
  12053. {
  12054. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  12055. string_t{},
  12056. [](const string_t& a, const string_t& b)
  12057. {
  12058. return detail::concat(a, '/', detail::escape(b));
  12059. });
  12060. }
  12061. /// @brief return a string representation of the JSON pointer
  12062. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  12063. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  12064. operator string_t() const
  12065. {
  12066. return to_string();
  12067. }
  12068. #ifndef JSON_NO_IO
  12069. /// @brief write string representation of the JSON pointer to stream
  12070. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  12071. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  12072. {
  12073. o << ptr.to_string();
  12074. return o;
  12075. }
  12076. #endif
  12077. /// @brief append another JSON pointer at the end of this JSON pointer
  12078. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12079. json_pointer& operator/=(const json_pointer& ptr)
  12080. {
  12081. reference_tokens.insert(reference_tokens.end(),
  12082. ptr.reference_tokens.begin(),
  12083. ptr.reference_tokens.end());
  12084. return *this;
  12085. }
  12086. /// @brief append an unescaped reference token at the end of this JSON pointer
  12087. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12088. json_pointer& operator/=(string_t token)
  12089. {
  12090. push_back(std::move(token));
  12091. return *this;
  12092. }
  12093. /// @brief append an array index at the end of this JSON pointer
  12094. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12095. json_pointer& operator/=(std::size_t array_idx)
  12096. {
  12097. return *this /= std::to_string(array_idx);
  12098. }
  12099. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  12100. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12101. friend json_pointer operator/(const json_pointer& lhs,
  12102. const json_pointer& rhs)
  12103. {
  12104. return json_pointer(lhs) /= rhs;
  12105. }
  12106. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  12107. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12108. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12109. {
  12110. return json_pointer(lhs) /= std::move(token);
  12111. }
  12112. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12113. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12114. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12115. {
  12116. return json_pointer(lhs) /= array_idx;
  12117. }
  12118. /// @brief returns the parent of this JSON pointer
  12119. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12120. json_pointer parent_pointer() const
  12121. {
  12122. if (empty())
  12123. {
  12124. return *this;
  12125. }
  12126. json_pointer res = *this;
  12127. res.pop_back();
  12128. return res;
  12129. }
  12130. /// @brief remove last reference token
  12131. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12132. void pop_back()
  12133. {
  12134. if (JSON_HEDLEY_UNLIKELY(empty()))
  12135. {
  12136. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12137. }
  12138. reference_tokens.pop_back();
  12139. }
  12140. /// @brief return last reference token
  12141. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12142. const string_t& back() const
  12143. {
  12144. if (JSON_HEDLEY_UNLIKELY(empty()))
  12145. {
  12146. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12147. }
  12148. return reference_tokens.back();
  12149. }
  12150. /// @brief append an unescaped token at the end of the reference pointer
  12151. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12152. void push_back(const string_t& token)
  12153. {
  12154. reference_tokens.push_back(token);
  12155. }
  12156. /// @brief append an unescaped token at the end of the reference pointer
  12157. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12158. void push_back(string_t&& token)
  12159. {
  12160. reference_tokens.push_back(std::move(token));
  12161. }
  12162. /// @brief return whether pointer points to the root document
  12163. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12164. bool empty() const noexcept
  12165. {
  12166. return reference_tokens.empty();
  12167. }
  12168. private:
  12169. /*!
  12170. @param[in] s reference token to be converted into an array index
  12171. @return integer representation of @a s
  12172. @throw parse_error.106 if an array index begins with '0'
  12173. @throw parse_error.109 if an array index begins not with a digit
  12174. @throw out_of_range.404 if string @a s could not be converted to an integer
  12175. @throw out_of_range.410 if an array index exceeds size_type
  12176. */
  12177. template<typename BasicJsonType>
  12178. static typename BasicJsonType::size_type array_index(const string_t& s)
  12179. {
  12180. using size_type = typename BasicJsonType::size_type;
  12181. // error condition (cf. RFC 6901, Sect. 4)
  12182. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12183. {
  12184. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12185. }
  12186. // error condition (cf. RFC 6901, Sect. 4)
  12187. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12188. {
  12189. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12190. }
  12191. const char* p = s.c_str();
  12192. char* p_end = nullptr;
  12193. errno = 0; // strtoull doesn't reset errno
  12194. const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12195. if (p == p_end // invalid input or empty string
  12196. || errno == ERANGE // out of range
  12197. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12198. {
  12199. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12200. }
  12201. // only triggered on special platforms (like 32bit), see also
  12202. // https://github.com/nlohmann/json/pull/2203
  12203. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12204. {
  12205. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12206. }
  12207. return static_cast<size_type>(res);
  12208. }
  12209. JSON_PRIVATE_UNLESS_TESTED:
  12210. json_pointer top() const
  12211. {
  12212. if (JSON_HEDLEY_UNLIKELY(empty()))
  12213. {
  12214. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12215. }
  12216. json_pointer result = *this;
  12217. result.reference_tokens = {reference_tokens[0]};
  12218. return result;
  12219. }
  12220. private:
  12221. /*!
  12222. @brief create and return a reference to the pointed to value
  12223. @complexity Linear in the number of reference tokens.
  12224. @throw parse_error.109 if array index is not a number
  12225. @throw type_error.313 if value cannot be unflattened
  12226. */
  12227. template<typename BasicJsonType>
  12228. BasicJsonType& get_and_create(BasicJsonType& j) const
  12229. {
  12230. auto* result = &j;
  12231. // in case no reference tokens exist, return a reference to the JSON value
  12232. // j which will be overwritten by a primitive value
  12233. for (const auto& reference_token : reference_tokens)
  12234. {
  12235. switch (result->type())
  12236. {
  12237. case detail::value_t::null:
  12238. {
  12239. if (reference_token == "0")
  12240. {
  12241. // start a new array if reference token is 0
  12242. result = &result->operator[](0);
  12243. }
  12244. else
  12245. {
  12246. // start a new object otherwise
  12247. result = &result->operator[](reference_token);
  12248. }
  12249. break;
  12250. }
  12251. case detail::value_t::object:
  12252. {
  12253. // create an entry in the object
  12254. result = &result->operator[](reference_token);
  12255. break;
  12256. }
  12257. case detail::value_t::array:
  12258. {
  12259. // create an entry in the array
  12260. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  12261. break;
  12262. }
  12263. /*
  12264. The following code is only reached if there exists a reference
  12265. token _and_ the current value is primitive. In this case, we have
  12266. an error situation, because primitive values may only occur as
  12267. single value; that is, with an empty list of reference tokens.
  12268. */
  12269. case detail::value_t::string:
  12270. case detail::value_t::boolean:
  12271. case detail::value_t::number_integer:
  12272. case detail::value_t::number_unsigned:
  12273. case detail::value_t::number_float:
  12274. case detail::value_t::binary:
  12275. case detail::value_t::discarded:
  12276. default:
  12277. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  12278. }
  12279. }
  12280. return *result;
  12281. }
  12282. /*!
  12283. @brief return a reference to the pointed to value
  12284. @note This version does not throw if a value is not present, but tries to
  12285. create nested values instead. For instance, calling this function
  12286. with pointer `"/this/that"` on a null value is equivalent to calling
  12287. `operator[]("this").operator[]("that")` on that value, effectively
  12288. changing the null value to an object.
  12289. @param[in] ptr a JSON value
  12290. @return reference to the JSON value pointed to by the JSON pointer
  12291. @complexity Linear in the length of the JSON pointer.
  12292. @throw parse_error.106 if an array index begins with '0'
  12293. @throw parse_error.109 if an array index was not a number
  12294. @throw out_of_range.404 if the JSON pointer can not be resolved
  12295. */
  12296. template<typename BasicJsonType>
  12297. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  12298. {
  12299. for (const auto& reference_token : reference_tokens)
  12300. {
  12301. // convert null values to arrays or objects before continuing
  12302. if (ptr->is_null())
  12303. {
  12304. // check if reference token is a number
  12305. const bool nums =
  12306. std::all_of(reference_token.begin(), reference_token.end(),
  12307. [](const unsigned char x)
  12308. {
  12309. return std::isdigit(x);
  12310. });
  12311. // change value to array for numbers or "-" or to object otherwise
  12312. *ptr = (nums || reference_token == "-")
  12313. ? detail::value_t::array
  12314. : detail::value_t::object;
  12315. }
  12316. switch (ptr->type())
  12317. {
  12318. case detail::value_t::object:
  12319. {
  12320. // use unchecked object access
  12321. ptr = &ptr->operator[](reference_token);
  12322. break;
  12323. }
  12324. case detail::value_t::array:
  12325. {
  12326. if (reference_token == "-")
  12327. {
  12328. // explicitly treat "-" as index beyond the end
  12329. ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
  12330. }
  12331. else
  12332. {
  12333. // convert array index to number; unchecked access
  12334. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12335. }
  12336. break;
  12337. }
  12338. case detail::value_t::null:
  12339. case detail::value_t::string:
  12340. case detail::value_t::boolean:
  12341. case detail::value_t::number_integer:
  12342. case detail::value_t::number_unsigned:
  12343. case detail::value_t::number_float:
  12344. case detail::value_t::binary:
  12345. case detail::value_t::discarded:
  12346. default:
  12347. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12348. }
  12349. }
  12350. return *ptr;
  12351. }
  12352. /*!
  12353. @throw parse_error.106 if an array index begins with '0'
  12354. @throw parse_error.109 if an array index was not a number
  12355. @throw out_of_range.402 if the array index '-' is used
  12356. @throw out_of_range.404 if the JSON pointer can not be resolved
  12357. */
  12358. template<typename BasicJsonType>
  12359. BasicJsonType& get_checked(BasicJsonType* ptr) const
  12360. {
  12361. for (const auto& reference_token : reference_tokens)
  12362. {
  12363. switch (ptr->type())
  12364. {
  12365. case detail::value_t::object:
  12366. {
  12367. // note: at performs range check
  12368. ptr = &ptr->at(reference_token);
  12369. break;
  12370. }
  12371. case detail::value_t::array:
  12372. {
  12373. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12374. {
  12375. // "-" always fails the range check
  12376. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12377. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  12378. ") is out of range"), ptr));
  12379. }
  12380. // note: at performs range check
  12381. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12382. break;
  12383. }
  12384. case detail::value_t::null:
  12385. case detail::value_t::string:
  12386. case detail::value_t::boolean:
  12387. case detail::value_t::number_integer:
  12388. case detail::value_t::number_unsigned:
  12389. case detail::value_t::number_float:
  12390. case detail::value_t::binary:
  12391. case detail::value_t::discarded:
  12392. default:
  12393. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12394. }
  12395. }
  12396. return *ptr;
  12397. }
  12398. /*!
  12399. @brief return a const reference to the pointed to value
  12400. @param[in] ptr a JSON value
  12401. @return const reference to the JSON value pointed to by the JSON
  12402. pointer
  12403. @throw parse_error.106 if an array index begins with '0'
  12404. @throw parse_error.109 if an array index was not a number
  12405. @throw out_of_range.402 if the array index '-' is used
  12406. @throw out_of_range.404 if the JSON pointer can not be resolved
  12407. */
  12408. template<typename BasicJsonType>
  12409. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  12410. {
  12411. for (const auto& reference_token : reference_tokens)
  12412. {
  12413. switch (ptr->type())
  12414. {
  12415. case detail::value_t::object:
  12416. {
  12417. // use unchecked object access
  12418. ptr = &ptr->operator[](reference_token);
  12419. break;
  12420. }
  12421. case detail::value_t::array:
  12422. {
  12423. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12424. {
  12425. // "-" cannot be used for const access
  12426. JSON_THROW(detail::out_of_range::create(402, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr));
  12427. }
  12428. // use unchecked array access
  12429. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12430. break;
  12431. }
  12432. case detail::value_t::null:
  12433. case detail::value_t::string:
  12434. case detail::value_t::boolean:
  12435. case detail::value_t::number_integer:
  12436. case detail::value_t::number_unsigned:
  12437. case detail::value_t::number_float:
  12438. case detail::value_t::binary:
  12439. case detail::value_t::discarded:
  12440. default:
  12441. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12442. }
  12443. }
  12444. return *ptr;
  12445. }
  12446. /*!
  12447. @throw parse_error.106 if an array index begins with '0'
  12448. @throw parse_error.109 if an array index was not a number
  12449. @throw out_of_range.402 if the array index '-' is used
  12450. @throw out_of_range.404 if the JSON pointer can not be resolved
  12451. */
  12452. template<typename BasicJsonType>
  12453. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  12454. {
  12455. for (const auto& reference_token : reference_tokens)
  12456. {
  12457. switch (ptr->type())
  12458. {
  12459. case detail::value_t::object:
  12460. {
  12461. // note: at performs range check
  12462. ptr = &ptr->at(reference_token);
  12463. break;
  12464. }
  12465. case detail::value_t::array:
  12466. {
  12467. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12468. {
  12469. // "-" always fails the range check
  12470. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12471. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  12472. ") is out of range"), ptr));
  12473. }
  12474. // note: at performs range check
  12475. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12476. break;
  12477. }
  12478. case detail::value_t::null:
  12479. case detail::value_t::string:
  12480. case detail::value_t::boolean:
  12481. case detail::value_t::number_integer:
  12482. case detail::value_t::number_unsigned:
  12483. case detail::value_t::number_float:
  12484. case detail::value_t::binary:
  12485. case detail::value_t::discarded:
  12486. default:
  12487. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12488. }
  12489. }
  12490. return *ptr;
  12491. }
  12492. /*!
  12493. @throw parse_error.106 if an array index begins with '0'
  12494. @throw parse_error.109 if an array index was not a number
  12495. */
  12496. template<typename BasicJsonType>
  12497. bool contains(const BasicJsonType* ptr) const
  12498. {
  12499. for (const auto& reference_token : reference_tokens)
  12500. {
  12501. switch (ptr->type())
  12502. {
  12503. case detail::value_t::object:
  12504. {
  12505. if (!ptr->contains(reference_token))
  12506. {
  12507. // we did not find the key in the object
  12508. return false;
  12509. }
  12510. ptr = &ptr->operator[](reference_token);
  12511. break;
  12512. }
  12513. case detail::value_t::array:
  12514. {
  12515. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12516. {
  12517. // "-" always fails the range check
  12518. return false;
  12519. }
  12520. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  12521. {
  12522. // invalid char
  12523. return false;
  12524. }
  12525. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  12526. {
  12527. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  12528. {
  12529. // first char should be between '1' and '9'
  12530. return false;
  12531. }
  12532. for (std::size_t i = 1; i < reference_token.size(); i++)
  12533. {
  12534. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  12535. {
  12536. // other char should be between '0' and '9'
  12537. return false;
  12538. }
  12539. }
  12540. }
  12541. const auto idx = array_index<BasicJsonType>(reference_token);
  12542. if (idx >= ptr->size())
  12543. {
  12544. // index out of range
  12545. return false;
  12546. }
  12547. ptr = &ptr->operator[](idx);
  12548. break;
  12549. }
  12550. case detail::value_t::null:
  12551. case detail::value_t::string:
  12552. case detail::value_t::boolean:
  12553. case detail::value_t::number_integer:
  12554. case detail::value_t::number_unsigned:
  12555. case detail::value_t::number_float:
  12556. case detail::value_t::binary:
  12557. case detail::value_t::discarded:
  12558. default:
  12559. {
  12560. // we do not expect primitive values if there is still a
  12561. // reference token to process
  12562. return false;
  12563. }
  12564. }
  12565. }
  12566. // no reference token left means we found a primitive value
  12567. return true;
  12568. }
  12569. /*!
  12570. @brief split the string input to reference tokens
  12571. @note This function is only called by the json_pointer constructor.
  12572. All exceptions below are documented there.
  12573. @throw parse_error.107 if the pointer is not empty or begins with '/'
  12574. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  12575. */
  12576. static std::vector<string_t> split(const string_t& reference_string)
  12577. {
  12578. std::vector<string_t> result;
  12579. // special case: empty reference string -> no reference tokens
  12580. if (reference_string.empty())
  12581. {
  12582. return result;
  12583. }
  12584. // check if nonempty reference string begins with slash
  12585. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  12586. {
  12587. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  12588. }
  12589. // extract the reference tokens:
  12590. // - slash: position of the last read slash (or end of string)
  12591. // - start: position after the previous slash
  12592. for (
  12593. // search for the first slash after the first character
  12594. std::size_t slash = reference_string.find_first_of('/', 1),
  12595. // set the beginning of the first reference token
  12596. start = 1;
  12597. // we can stop if start == 0 (if slash == string_t::npos)
  12598. start != 0;
  12599. // set the beginning of the next reference token
  12600. // (will eventually be 0 if slash == string_t::npos)
  12601. start = (slash == string_t::npos) ? 0 : slash + 1,
  12602. // find next slash
  12603. slash = reference_string.find_first_of('/', start))
  12604. {
  12605. // use the text between the beginning of the reference token
  12606. // (start) and the last slash (slash).
  12607. auto reference_token = reference_string.substr(start, slash - start);
  12608. // check reference tokens are properly escaped
  12609. for (std::size_t pos = reference_token.find_first_of('~');
  12610. pos != string_t::npos;
  12611. pos = reference_token.find_first_of('~', pos + 1))
  12612. {
  12613. JSON_ASSERT(reference_token[pos] == '~');
  12614. // ~ must be followed by 0 or 1
  12615. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  12616. (reference_token[pos + 1] != '0' &&
  12617. reference_token[pos + 1] != '1')))
  12618. {
  12619. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  12620. }
  12621. }
  12622. // finally, store the reference token
  12623. detail::unescape(reference_token);
  12624. result.push_back(reference_token);
  12625. }
  12626. return result;
  12627. }
  12628. private:
  12629. /*!
  12630. @param[in] reference_string the reference string to the current value
  12631. @param[in] value the value to consider
  12632. @param[in,out] result the result object to insert values to
  12633. @note Empty objects or arrays are flattened to `null`.
  12634. */
  12635. template<typename BasicJsonType>
  12636. static void flatten(const string_t& reference_string,
  12637. const BasicJsonType& value,
  12638. BasicJsonType& result)
  12639. {
  12640. switch (value.type())
  12641. {
  12642. case detail::value_t::array:
  12643. {
  12644. if (value.m_data.m_value.array->empty())
  12645. {
  12646. // flatten empty array as null
  12647. result[reference_string] = nullptr;
  12648. }
  12649. else
  12650. {
  12651. // iterate array and use index as reference string
  12652. for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
  12653. {
  12654. flatten(detail::concat(reference_string, '/', std::to_string(i)),
  12655. value.m_data.m_value.array->operator[](i), result);
  12656. }
  12657. }
  12658. break;
  12659. }
  12660. case detail::value_t::object:
  12661. {
  12662. if (value.m_data.m_value.object->empty())
  12663. {
  12664. // flatten empty object as null
  12665. result[reference_string] = nullptr;
  12666. }
  12667. else
  12668. {
  12669. // iterate object and use keys as reference string
  12670. for (const auto& element : *value.m_data.m_value.object)
  12671. {
  12672. flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result);
  12673. }
  12674. }
  12675. break;
  12676. }
  12677. case detail::value_t::null:
  12678. case detail::value_t::string:
  12679. case detail::value_t::boolean:
  12680. case detail::value_t::number_integer:
  12681. case detail::value_t::number_unsigned:
  12682. case detail::value_t::number_float:
  12683. case detail::value_t::binary:
  12684. case detail::value_t::discarded:
  12685. default:
  12686. {
  12687. // add primitive value with its reference string
  12688. result[reference_string] = value;
  12689. break;
  12690. }
  12691. }
  12692. }
  12693. /*!
  12694. @param[in] value flattened JSON
  12695. @return unflattened JSON
  12696. @throw parse_error.109 if array index is not a number
  12697. @throw type_error.314 if value is not an object
  12698. @throw type_error.315 if object values are not primitive
  12699. @throw type_error.313 if value cannot be unflattened
  12700. */
  12701. template<typename BasicJsonType>
  12702. static BasicJsonType
  12703. unflatten(const BasicJsonType& value)
  12704. {
  12705. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  12706. {
  12707. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  12708. }
  12709. BasicJsonType result;
  12710. // iterate the JSON object values
  12711. for (const auto& element : *value.m_data.m_value.object)
  12712. {
  12713. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  12714. {
  12715. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  12716. }
  12717. // assign value to reference pointed to by JSON pointer; Note that if
  12718. // the JSON pointer is "" (i.e., points to the whole value), function
  12719. // get_and_create returns a reference to result itself. An assignment
  12720. // will then create a primitive value.
  12721. json_pointer(element.first).get_and_create(result) = element.second;
  12722. }
  12723. return result;
  12724. }
  12725. // can't use conversion operator because of ambiguity
  12726. json_pointer<string_t> convert() const&
  12727. {
  12728. json_pointer<string_t> result;
  12729. result.reference_tokens = reference_tokens;
  12730. return result;
  12731. }
  12732. json_pointer<string_t> convert()&&
  12733. {
  12734. json_pointer<string_t> result;
  12735. result.reference_tokens = std::move(reference_tokens);
  12736. return result;
  12737. }
  12738. public:
  12739. #if JSON_HAS_THREE_WAY_COMPARISON
  12740. /// @brief compares two JSON pointers for equality
  12741. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12742. template<typename RefStringTypeRhs>
  12743. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  12744. {
  12745. return reference_tokens == rhs.reference_tokens;
  12746. }
  12747. /// @brief compares JSON pointer and string for equality
  12748. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12749. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  12750. bool operator==(const string_t& rhs) const
  12751. {
  12752. return *this == json_pointer(rhs);
  12753. }
  12754. /// @brief 3-way compares two JSON pointers
  12755. template<typename RefStringTypeRhs>
  12756. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  12757. {
  12758. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  12759. }
  12760. #else
  12761. /// @brief compares two JSON pointers for equality
  12762. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12763. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12764. // NOLINTNEXTLINE(readability-redundant-declaration)
  12765. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12766. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12767. /// @brief compares JSON pointer and string for equality
  12768. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12769. template<typename RefStringTypeLhs, typename StringType>
  12770. // NOLINTNEXTLINE(readability-redundant-declaration)
  12771. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12772. const StringType& rhs);
  12773. /// @brief compares string and JSON pointer for equality
  12774. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12775. template<typename RefStringTypeRhs, typename StringType>
  12776. // NOLINTNEXTLINE(readability-redundant-declaration)
  12777. friend bool operator==(const StringType& lhs,
  12778. const json_pointer<RefStringTypeRhs>& rhs);
  12779. /// @brief compares two JSON pointers for inequality
  12780. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12781. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12782. // NOLINTNEXTLINE(readability-redundant-declaration)
  12783. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12784. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12785. /// @brief compares JSON pointer and string for inequality
  12786. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12787. template<typename RefStringTypeLhs, typename StringType>
  12788. // NOLINTNEXTLINE(readability-redundant-declaration)
  12789. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12790. const StringType& rhs);
  12791. /// @brief compares string and JSON pointer for inequality
  12792. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12793. template<typename RefStringTypeRhs, typename StringType>
  12794. // NOLINTNEXTLINE(readability-redundant-declaration)
  12795. friend bool operator!=(const StringType& lhs,
  12796. const json_pointer<RefStringTypeRhs>& rhs);
  12797. /// @brief compares two JSON pointer for less-than
  12798. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12799. // NOLINTNEXTLINE(readability-redundant-declaration)
  12800. friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12801. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12802. #endif
  12803. private:
  12804. /// the reference tokens
  12805. std::vector<string_t> reference_tokens;
  12806. };
  12807. #if !JSON_HAS_THREE_WAY_COMPARISON
  12808. // functions cannot be defined inside class due to ODR violations
  12809. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12810. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12811. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12812. {
  12813. return lhs.reference_tokens == rhs.reference_tokens;
  12814. }
  12815. template<typename RefStringTypeLhs,
  12816. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12817. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12818. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12819. const StringType& rhs)
  12820. {
  12821. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  12822. }
  12823. template<typename RefStringTypeRhs,
  12824. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12825. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12826. inline bool operator==(const StringType& lhs,
  12827. const json_pointer<RefStringTypeRhs>& rhs)
  12828. {
  12829. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  12830. }
  12831. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12832. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12833. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12834. {
  12835. return !(lhs == rhs);
  12836. }
  12837. template<typename RefStringTypeLhs,
  12838. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12839. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12840. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12841. const StringType& rhs)
  12842. {
  12843. return !(lhs == rhs);
  12844. }
  12845. template<typename RefStringTypeRhs,
  12846. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12847. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12848. inline bool operator!=(const StringType& lhs,
  12849. const json_pointer<RefStringTypeRhs>& rhs)
  12850. {
  12851. return !(lhs == rhs);
  12852. }
  12853. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12854. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12855. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12856. {
  12857. return lhs.reference_tokens < rhs.reference_tokens;
  12858. }
  12859. #endif
  12860. NLOHMANN_JSON_NAMESPACE_END
  12861. // #include <nlohmann/detail/json_ref.hpp>
  12862. // __ _____ _____ _____
  12863. // __| | __| | | | JSON for Modern C++
  12864. // | | |__ | | | | | | version 3.11.3
  12865. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12866. //
  12867. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12868. // SPDX-License-Identifier: MIT
  12869. #include <initializer_list>
  12870. #include <utility>
  12871. // #include <nlohmann/detail/abi_macros.hpp>
  12872. // #include <nlohmann/detail/meta/type_traits.hpp>
  12873. NLOHMANN_JSON_NAMESPACE_BEGIN
  12874. namespace detail
  12875. {
  12876. template<typename BasicJsonType>
  12877. class json_ref
  12878. {
  12879. public:
  12880. using value_type = BasicJsonType;
  12881. json_ref(value_type&& value)
  12882. : owned_value(std::move(value))
  12883. {}
  12884. json_ref(const value_type& value)
  12885. : value_ref(&value)
  12886. {}
  12887. json_ref(std::initializer_list<json_ref> init)
  12888. : owned_value(init)
  12889. {}
  12890. template <
  12891. class... Args,
  12892. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  12893. json_ref(Args && ... args)
  12894. : owned_value(std::forward<Args>(args)...)
  12895. {}
  12896. // class should be movable only
  12897. json_ref(json_ref&&) noexcept = default;
  12898. json_ref(const json_ref&) = delete;
  12899. json_ref& operator=(const json_ref&) = delete;
  12900. json_ref& operator=(json_ref&&) = delete;
  12901. ~json_ref() = default;
  12902. value_type moved_or_copied() const
  12903. {
  12904. if (value_ref == nullptr)
  12905. {
  12906. return std::move(owned_value);
  12907. }
  12908. return *value_ref;
  12909. }
  12910. value_type const& operator*() const
  12911. {
  12912. return value_ref ? *value_ref : owned_value;
  12913. }
  12914. value_type const* operator->() const
  12915. {
  12916. return &** this;
  12917. }
  12918. private:
  12919. mutable value_type owned_value = nullptr;
  12920. value_type const* value_ref = nullptr;
  12921. };
  12922. } // namespace detail
  12923. NLOHMANN_JSON_NAMESPACE_END
  12924. // #include <nlohmann/detail/macro_scope.hpp>
  12925. // #include <nlohmann/detail/string_concat.hpp>
  12926. // #include <nlohmann/detail/string_escape.hpp>
  12927. // #include <nlohmann/detail/meta/cpp_future.hpp>
  12928. // #include <nlohmann/detail/meta/type_traits.hpp>
  12929. // #include <nlohmann/detail/output/binary_writer.hpp>
  12930. // __ _____ _____ _____
  12931. // __| | __| | | | JSON for Modern C++
  12932. // | | |__ | | | | | | version 3.11.3
  12933. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12934. //
  12935. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12936. // SPDX-License-Identifier: MIT
  12937. #include <algorithm> // reverse
  12938. #include <array> // array
  12939. #include <map> // map
  12940. #include <cmath> // isnan, isinf
  12941. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  12942. #include <cstring> // memcpy
  12943. #include <limits> // numeric_limits
  12944. #include <string> // string
  12945. #include <utility> // move
  12946. #include <vector> // vector
  12947. // #include <nlohmann/detail/input/binary_reader.hpp>
  12948. // #include <nlohmann/detail/macro_scope.hpp>
  12949. // #include <nlohmann/detail/output/output_adapters.hpp>
  12950. // __ _____ _____ _____
  12951. // __| | __| | | | JSON for Modern C++
  12952. // | | |__ | | | | | | version 3.11.3
  12953. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12954. //
  12955. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  12956. // SPDX-License-Identifier: MIT
  12957. #include <algorithm> // copy
  12958. #include <cstddef> // size_t
  12959. #include <iterator> // back_inserter
  12960. #include <memory> // shared_ptr, make_shared
  12961. #include <string> // basic_string
  12962. #include <vector> // vector
  12963. #ifndef JSON_NO_IO
  12964. #include <ios> // streamsize
  12965. #include <ostream> // basic_ostream
  12966. #endif // JSON_NO_IO
  12967. // #include <nlohmann/detail/macro_scope.hpp>
  12968. NLOHMANN_JSON_NAMESPACE_BEGIN
  12969. namespace detail
  12970. {
  12971. /// abstract output adapter interface
  12972. template<typename CharType> struct output_adapter_protocol
  12973. {
  12974. virtual void write_character(CharType c) = 0;
  12975. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  12976. virtual ~output_adapter_protocol() = default;
  12977. output_adapter_protocol() = default;
  12978. output_adapter_protocol(const output_adapter_protocol&) = default;
  12979. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  12980. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  12981. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  12982. };
  12983. /// a type to simplify interfaces
  12984. template<typename CharType>
  12985. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  12986. /// output adapter for byte vectors
  12987. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  12988. class output_vector_adapter : public output_adapter_protocol<CharType>
  12989. {
  12990. public:
  12991. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  12992. : v(vec)
  12993. {}
  12994. void write_character(CharType c) override
  12995. {
  12996. v.push_back(c);
  12997. }
  12998. JSON_HEDLEY_NON_NULL(2)
  12999. void write_characters(const CharType* s, std::size_t length) override
  13000. {
  13001. v.insert(v.end(), s, s + length);
  13002. }
  13003. private:
  13004. std::vector<CharType, AllocatorType>& v;
  13005. };
  13006. #ifndef JSON_NO_IO
  13007. /// output adapter for output streams
  13008. template<typename CharType>
  13009. class output_stream_adapter : public output_adapter_protocol<CharType>
  13010. {
  13011. public:
  13012. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  13013. : stream(s)
  13014. {}
  13015. void write_character(CharType c) override
  13016. {
  13017. stream.put(c);
  13018. }
  13019. JSON_HEDLEY_NON_NULL(2)
  13020. void write_characters(const CharType* s, std::size_t length) override
  13021. {
  13022. stream.write(s, static_cast<std::streamsize>(length));
  13023. }
  13024. private:
  13025. std::basic_ostream<CharType>& stream;
  13026. };
  13027. #endif // JSON_NO_IO
  13028. /// output adapter for basic_string
  13029. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13030. class output_string_adapter : public output_adapter_protocol<CharType>
  13031. {
  13032. public:
  13033. explicit output_string_adapter(StringType& s) noexcept
  13034. : str(s)
  13035. {}
  13036. void write_character(CharType c) override
  13037. {
  13038. str.push_back(c);
  13039. }
  13040. JSON_HEDLEY_NON_NULL(2)
  13041. void write_characters(const CharType* s, std::size_t length) override
  13042. {
  13043. str.append(s, length);
  13044. }
  13045. private:
  13046. StringType& str;
  13047. };
  13048. template<typename CharType, typename StringType = std::basic_string<CharType>>
  13049. class output_adapter
  13050. {
  13051. public:
  13052. template<typename AllocatorType = std::allocator<CharType>>
  13053. output_adapter(std::vector<CharType, AllocatorType>& vec)
  13054. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  13055. #ifndef JSON_NO_IO
  13056. output_adapter(std::basic_ostream<CharType>& s)
  13057. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  13058. #endif // JSON_NO_IO
  13059. output_adapter(StringType& s)
  13060. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  13061. operator output_adapter_t<CharType>()
  13062. {
  13063. return oa;
  13064. }
  13065. private:
  13066. output_adapter_t<CharType> oa = nullptr;
  13067. };
  13068. } // namespace detail
  13069. NLOHMANN_JSON_NAMESPACE_END
  13070. // #include <nlohmann/detail/string_concat.hpp>
  13071. NLOHMANN_JSON_NAMESPACE_BEGIN
  13072. namespace detail
  13073. {
  13074. ///////////////////
  13075. // binary writer //
  13076. ///////////////////
  13077. /*!
  13078. @brief serialization to CBOR and MessagePack values
  13079. */
  13080. template<typename BasicJsonType, typename CharType>
  13081. class binary_writer
  13082. {
  13083. using string_t = typename BasicJsonType::string_t;
  13084. using binary_t = typename BasicJsonType::binary_t;
  13085. using number_float_t = typename BasicJsonType::number_float_t;
  13086. public:
  13087. /*!
  13088. @brief create a binary writer
  13089. @param[in] adapter output adapter to write to
  13090. */
  13091. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  13092. {
  13093. JSON_ASSERT(oa);
  13094. }
  13095. /*!
  13096. @param[in] j JSON value to serialize
  13097. @pre j.type() == value_t::object
  13098. */
  13099. void write_bson(const BasicJsonType& j)
  13100. {
  13101. switch (j.type())
  13102. {
  13103. case value_t::object:
  13104. {
  13105. write_bson_object(*j.m_data.m_value.object);
  13106. break;
  13107. }
  13108. case value_t::null:
  13109. case value_t::array:
  13110. case value_t::string:
  13111. case value_t::boolean:
  13112. case value_t::number_integer:
  13113. case value_t::number_unsigned:
  13114. case value_t::number_float:
  13115. case value_t::binary:
  13116. case value_t::discarded:
  13117. default:
  13118. {
  13119. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13120. }
  13121. }
  13122. }
  13123. /*!
  13124. @param[in] j JSON value to serialize
  13125. */
  13126. void write_cbor(const BasicJsonType& j)
  13127. {
  13128. switch (j.type())
  13129. {
  13130. case value_t::null:
  13131. {
  13132. oa->write_character(to_char_type(0xF6));
  13133. break;
  13134. }
  13135. case value_t::boolean:
  13136. {
  13137. oa->write_character(j.m_data.m_value.boolean
  13138. ? to_char_type(0xF5)
  13139. : to_char_type(0xF4));
  13140. break;
  13141. }
  13142. case value_t::number_integer:
  13143. {
  13144. if (j.m_data.m_value.number_integer >= 0)
  13145. {
  13146. // CBOR does not differentiate between positive signed
  13147. // integers and unsigned integers. Therefore, we used the
  13148. // code from the value_t::number_unsigned case here.
  13149. if (j.m_data.m_value.number_integer <= 0x17)
  13150. {
  13151. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13152. }
  13153. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13154. {
  13155. oa->write_character(to_char_type(0x18));
  13156. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13157. }
  13158. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13159. {
  13160. oa->write_character(to_char_type(0x19));
  13161. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13162. }
  13163. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13164. {
  13165. oa->write_character(to_char_type(0x1A));
  13166. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13167. }
  13168. else
  13169. {
  13170. oa->write_character(to_char_type(0x1B));
  13171. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13172. }
  13173. }
  13174. else
  13175. {
  13176. // The conversions below encode the sign in the first
  13177. // byte, and the value is converted to a positive number.
  13178. const auto positive_number = -1 - j.m_data.m_value.number_integer;
  13179. if (j.m_data.m_value.number_integer >= -24)
  13180. {
  13181. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13182. }
  13183. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13184. {
  13185. oa->write_character(to_char_type(0x38));
  13186. write_number(static_cast<std::uint8_t>(positive_number));
  13187. }
  13188. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13189. {
  13190. oa->write_character(to_char_type(0x39));
  13191. write_number(static_cast<std::uint16_t>(positive_number));
  13192. }
  13193. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13194. {
  13195. oa->write_character(to_char_type(0x3A));
  13196. write_number(static_cast<std::uint32_t>(positive_number));
  13197. }
  13198. else
  13199. {
  13200. oa->write_character(to_char_type(0x3B));
  13201. write_number(static_cast<std::uint64_t>(positive_number));
  13202. }
  13203. }
  13204. break;
  13205. }
  13206. case value_t::number_unsigned:
  13207. {
  13208. if (j.m_data.m_value.number_unsigned <= 0x17)
  13209. {
  13210. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13211. }
  13212. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13213. {
  13214. oa->write_character(to_char_type(0x18));
  13215. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13216. }
  13217. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13218. {
  13219. oa->write_character(to_char_type(0x19));
  13220. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
  13221. }
  13222. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13223. {
  13224. oa->write_character(to_char_type(0x1A));
  13225. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
  13226. }
  13227. else
  13228. {
  13229. oa->write_character(to_char_type(0x1B));
  13230. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
  13231. }
  13232. break;
  13233. }
  13234. case value_t::number_float:
  13235. {
  13236. if (std::isnan(j.m_data.m_value.number_float))
  13237. {
  13238. // NaN is 0xf97e00 in CBOR
  13239. oa->write_character(to_char_type(0xF9));
  13240. oa->write_character(to_char_type(0x7E));
  13241. oa->write_character(to_char_type(0x00));
  13242. }
  13243. else if (std::isinf(j.m_data.m_value.number_float))
  13244. {
  13245. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  13246. oa->write_character(to_char_type(0xf9));
  13247. oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  13248. oa->write_character(to_char_type(0x00));
  13249. }
  13250. else
  13251. {
  13252. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
  13253. }
  13254. break;
  13255. }
  13256. case value_t::string:
  13257. {
  13258. // step 1: write control byte and the string length
  13259. const auto N = j.m_data.m_value.string->size();
  13260. if (N <= 0x17)
  13261. {
  13262. write_number(static_cast<std::uint8_t>(0x60 + N));
  13263. }
  13264. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13265. {
  13266. oa->write_character(to_char_type(0x78));
  13267. write_number(static_cast<std::uint8_t>(N));
  13268. }
  13269. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13270. {
  13271. oa->write_character(to_char_type(0x79));
  13272. write_number(static_cast<std::uint16_t>(N));
  13273. }
  13274. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13275. {
  13276. oa->write_character(to_char_type(0x7A));
  13277. write_number(static_cast<std::uint32_t>(N));
  13278. }
  13279. // LCOV_EXCL_START
  13280. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13281. {
  13282. oa->write_character(to_char_type(0x7B));
  13283. write_number(static_cast<std::uint64_t>(N));
  13284. }
  13285. // LCOV_EXCL_STOP
  13286. // step 2: write the string
  13287. oa->write_characters(
  13288. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13289. j.m_data.m_value.string->size());
  13290. break;
  13291. }
  13292. case value_t::array:
  13293. {
  13294. // step 1: write control byte and the array size
  13295. const auto N = j.m_data.m_value.array->size();
  13296. if (N <= 0x17)
  13297. {
  13298. write_number(static_cast<std::uint8_t>(0x80 + N));
  13299. }
  13300. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13301. {
  13302. oa->write_character(to_char_type(0x98));
  13303. write_number(static_cast<std::uint8_t>(N));
  13304. }
  13305. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13306. {
  13307. oa->write_character(to_char_type(0x99));
  13308. write_number(static_cast<std::uint16_t>(N));
  13309. }
  13310. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13311. {
  13312. oa->write_character(to_char_type(0x9A));
  13313. write_number(static_cast<std::uint32_t>(N));
  13314. }
  13315. // LCOV_EXCL_START
  13316. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13317. {
  13318. oa->write_character(to_char_type(0x9B));
  13319. write_number(static_cast<std::uint64_t>(N));
  13320. }
  13321. // LCOV_EXCL_STOP
  13322. // step 2: write each element
  13323. for (const auto& el : *j.m_data.m_value.array)
  13324. {
  13325. write_cbor(el);
  13326. }
  13327. break;
  13328. }
  13329. case value_t::binary:
  13330. {
  13331. if (j.m_data.m_value.binary->has_subtype())
  13332. {
  13333. if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  13334. {
  13335. write_number(static_cast<std::uint8_t>(0xd8));
  13336. write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
  13337. }
  13338. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  13339. {
  13340. write_number(static_cast<std::uint8_t>(0xd9));
  13341. write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
  13342. }
  13343. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  13344. {
  13345. write_number(static_cast<std::uint8_t>(0xda));
  13346. write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
  13347. }
  13348. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  13349. {
  13350. write_number(static_cast<std::uint8_t>(0xdb));
  13351. write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
  13352. }
  13353. }
  13354. // step 1: write control byte and the binary array size
  13355. const auto N = j.m_data.m_value.binary->size();
  13356. if (N <= 0x17)
  13357. {
  13358. write_number(static_cast<std::uint8_t>(0x40 + N));
  13359. }
  13360. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13361. {
  13362. oa->write_character(to_char_type(0x58));
  13363. write_number(static_cast<std::uint8_t>(N));
  13364. }
  13365. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13366. {
  13367. oa->write_character(to_char_type(0x59));
  13368. write_number(static_cast<std::uint16_t>(N));
  13369. }
  13370. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13371. {
  13372. oa->write_character(to_char_type(0x5A));
  13373. write_number(static_cast<std::uint32_t>(N));
  13374. }
  13375. // LCOV_EXCL_START
  13376. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13377. {
  13378. oa->write_character(to_char_type(0x5B));
  13379. write_number(static_cast<std::uint64_t>(N));
  13380. }
  13381. // LCOV_EXCL_STOP
  13382. // step 2: write each element
  13383. oa->write_characters(
  13384. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13385. N);
  13386. break;
  13387. }
  13388. case value_t::object:
  13389. {
  13390. // step 1: write control byte and the object size
  13391. const auto N = j.m_data.m_value.object->size();
  13392. if (N <= 0x17)
  13393. {
  13394. write_number(static_cast<std::uint8_t>(0xA0 + N));
  13395. }
  13396. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13397. {
  13398. oa->write_character(to_char_type(0xB8));
  13399. write_number(static_cast<std::uint8_t>(N));
  13400. }
  13401. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13402. {
  13403. oa->write_character(to_char_type(0xB9));
  13404. write_number(static_cast<std::uint16_t>(N));
  13405. }
  13406. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13407. {
  13408. oa->write_character(to_char_type(0xBA));
  13409. write_number(static_cast<std::uint32_t>(N));
  13410. }
  13411. // LCOV_EXCL_START
  13412. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13413. {
  13414. oa->write_character(to_char_type(0xBB));
  13415. write_number(static_cast<std::uint64_t>(N));
  13416. }
  13417. // LCOV_EXCL_STOP
  13418. // step 2: write each element
  13419. for (const auto& el : *j.m_data.m_value.object)
  13420. {
  13421. write_cbor(el.first);
  13422. write_cbor(el.second);
  13423. }
  13424. break;
  13425. }
  13426. case value_t::discarded:
  13427. default:
  13428. break;
  13429. }
  13430. }
  13431. /*!
  13432. @param[in] j JSON value to serialize
  13433. */
  13434. void write_msgpack(const BasicJsonType& j)
  13435. {
  13436. switch (j.type())
  13437. {
  13438. case value_t::null: // nil
  13439. {
  13440. oa->write_character(to_char_type(0xC0));
  13441. break;
  13442. }
  13443. case value_t::boolean: // true and false
  13444. {
  13445. oa->write_character(j.m_data.m_value.boolean
  13446. ? to_char_type(0xC3)
  13447. : to_char_type(0xC2));
  13448. break;
  13449. }
  13450. case value_t::number_integer:
  13451. {
  13452. if (j.m_data.m_value.number_integer >= 0)
  13453. {
  13454. // MessagePack does not differentiate between positive
  13455. // signed integers and unsigned integers. Therefore, we used
  13456. // the code from the value_t::number_unsigned case here.
  13457. if (j.m_data.m_value.number_unsigned < 128)
  13458. {
  13459. // positive fixnum
  13460. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13461. }
  13462. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13463. {
  13464. // uint 8
  13465. oa->write_character(to_char_type(0xCC));
  13466. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13467. }
  13468. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13469. {
  13470. // uint 16
  13471. oa->write_character(to_char_type(0xCD));
  13472. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13473. }
  13474. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13475. {
  13476. // uint 32
  13477. oa->write_character(to_char_type(0xCE));
  13478. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13479. }
  13480. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13481. {
  13482. // uint 64
  13483. oa->write_character(to_char_type(0xCF));
  13484. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13485. }
  13486. }
  13487. else
  13488. {
  13489. if (j.m_data.m_value.number_integer >= -32)
  13490. {
  13491. // negative fixnum
  13492. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  13493. }
  13494. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  13495. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  13496. {
  13497. // int 8
  13498. oa->write_character(to_char_type(0xD0));
  13499. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  13500. }
  13501. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  13502. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  13503. {
  13504. // int 16
  13505. oa->write_character(to_char_type(0xD1));
  13506. write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
  13507. }
  13508. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  13509. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  13510. {
  13511. // int 32
  13512. oa->write_character(to_char_type(0xD2));
  13513. write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
  13514. }
  13515. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  13516. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  13517. {
  13518. // int 64
  13519. oa->write_character(to_char_type(0xD3));
  13520. write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
  13521. }
  13522. }
  13523. break;
  13524. }
  13525. case value_t::number_unsigned:
  13526. {
  13527. if (j.m_data.m_value.number_unsigned < 128)
  13528. {
  13529. // positive fixnum
  13530. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13531. }
  13532. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13533. {
  13534. // uint 8
  13535. oa->write_character(to_char_type(0xCC));
  13536. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13537. }
  13538. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13539. {
  13540. // uint 16
  13541. oa->write_character(to_char_type(0xCD));
  13542. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13543. }
  13544. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13545. {
  13546. // uint 32
  13547. oa->write_character(to_char_type(0xCE));
  13548. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13549. }
  13550. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13551. {
  13552. // uint 64
  13553. oa->write_character(to_char_type(0xCF));
  13554. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13555. }
  13556. break;
  13557. }
  13558. case value_t::number_float:
  13559. {
  13560. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
  13561. break;
  13562. }
  13563. case value_t::string:
  13564. {
  13565. // step 1: write control byte and the string length
  13566. const auto N = j.m_data.m_value.string->size();
  13567. if (N <= 31)
  13568. {
  13569. // fixstr
  13570. write_number(static_cast<std::uint8_t>(0xA0 | N));
  13571. }
  13572. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13573. {
  13574. // str 8
  13575. oa->write_character(to_char_type(0xD9));
  13576. write_number(static_cast<std::uint8_t>(N));
  13577. }
  13578. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13579. {
  13580. // str 16
  13581. oa->write_character(to_char_type(0xDA));
  13582. write_number(static_cast<std::uint16_t>(N));
  13583. }
  13584. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13585. {
  13586. // str 32
  13587. oa->write_character(to_char_type(0xDB));
  13588. write_number(static_cast<std::uint32_t>(N));
  13589. }
  13590. // step 2: write the string
  13591. oa->write_characters(
  13592. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13593. j.m_data.m_value.string->size());
  13594. break;
  13595. }
  13596. case value_t::array:
  13597. {
  13598. // step 1: write control byte and the array size
  13599. const auto N = j.m_data.m_value.array->size();
  13600. if (N <= 15)
  13601. {
  13602. // fixarray
  13603. write_number(static_cast<std::uint8_t>(0x90 | N));
  13604. }
  13605. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13606. {
  13607. // array 16
  13608. oa->write_character(to_char_type(0xDC));
  13609. write_number(static_cast<std::uint16_t>(N));
  13610. }
  13611. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13612. {
  13613. // array 32
  13614. oa->write_character(to_char_type(0xDD));
  13615. write_number(static_cast<std::uint32_t>(N));
  13616. }
  13617. // step 2: write each element
  13618. for (const auto& el : *j.m_data.m_value.array)
  13619. {
  13620. write_msgpack(el);
  13621. }
  13622. break;
  13623. }
  13624. case value_t::binary:
  13625. {
  13626. // step 0: determine if the binary type has a set subtype to
  13627. // determine whether or not to use the ext or fixext types
  13628. const bool use_ext = j.m_data.m_value.binary->has_subtype();
  13629. // step 1: write control byte and the byte string length
  13630. const auto N = j.m_data.m_value.binary->size();
  13631. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13632. {
  13633. std::uint8_t output_type{};
  13634. bool fixed = true;
  13635. if (use_ext)
  13636. {
  13637. switch (N)
  13638. {
  13639. case 1:
  13640. output_type = 0xD4; // fixext 1
  13641. break;
  13642. case 2:
  13643. output_type = 0xD5; // fixext 2
  13644. break;
  13645. case 4:
  13646. output_type = 0xD6; // fixext 4
  13647. break;
  13648. case 8:
  13649. output_type = 0xD7; // fixext 8
  13650. break;
  13651. case 16:
  13652. output_type = 0xD8; // fixext 16
  13653. break;
  13654. default:
  13655. output_type = 0xC7; // ext 8
  13656. fixed = false;
  13657. break;
  13658. }
  13659. }
  13660. else
  13661. {
  13662. output_type = 0xC4; // bin 8
  13663. fixed = false;
  13664. }
  13665. oa->write_character(to_char_type(output_type));
  13666. if (!fixed)
  13667. {
  13668. write_number(static_cast<std::uint8_t>(N));
  13669. }
  13670. }
  13671. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13672. {
  13673. const std::uint8_t output_type = use_ext
  13674. ? 0xC8 // ext 16
  13675. : 0xC5; // bin 16
  13676. oa->write_character(to_char_type(output_type));
  13677. write_number(static_cast<std::uint16_t>(N));
  13678. }
  13679. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13680. {
  13681. const std::uint8_t output_type = use_ext
  13682. ? 0xC9 // ext 32
  13683. : 0xC6; // bin 32
  13684. oa->write_character(to_char_type(output_type));
  13685. write_number(static_cast<std::uint32_t>(N));
  13686. }
  13687. // step 1.5: if this is an ext type, write the subtype
  13688. if (use_ext)
  13689. {
  13690. write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
  13691. }
  13692. // step 2: write the byte string
  13693. oa->write_characters(
  13694. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13695. N);
  13696. break;
  13697. }
  13698. case value_t::object:
  13699. {
  13700. // step 1: write control byte and the object size
  13701. const auto N = j.m_data.m_value.object->size();
  13702. if (N <= 15)
  13703. {
  13704. // fixmap
  13705. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  13706. }
  13707. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13708. {
  13709. // map 16
  13710. oa->write_character(to_char_type(0xDE));
  13711. write_number(static_cast<std::uint16_t>(N));
  13712. }
  13713. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13714. {
  13715. // map 32
  13716. oa->write_character(to_char_type(0xDF));
  13717. write_number(static_cast<std::uint32_t>(N));
  13718. }
  13719. // step 2: write each element
  13720. for (const auto& el : *j.m_data.m_value.object)
  13721. {
  13722. write_msgpack(el.first);
  13723. write_msgpack(el.second);
  13724. }
  13725. break;
  13726. }
  13727. case value_t::discarded:
  13728. default:
  13729. break;
  13730. }
  13731. }
  13732. /*!
  13733. @param[in] j JSON value to serialize
  13734. @param[in] use_count whether to use '#' prefixes (optimized format)
  13735. @param[in] use_type whether to use '$' prefixes (optimized format)
  13736. @param[in] add_prefix whether prefixes need to be used for this value
  13737. @param[in] use_bjdata whether write in BJData format, default is false
  13738. */
  13739. void write_ubjson(const BasicJsonType& j, const bool use_count,
  13740. const bool use_type, const bool add_prefix = true,
  13741. const bool use_bjdata = false)
  13742. {
  13743. switch (j.type())
  13744. {
  13745. case value_t::null:
  13746. {
  13747. if (add_prefix)
  13748. {
  13749. oa->write_character(to_char_type('Z'));
  13750. }
  13751. break;
  13752. }
  13753. case value_t::boolean:
  13754. {
  13755. if (add_prefix)
  13756. {
  13757. oa->write_character(j.m_data.m_value.boolean
  13758. ? to_char_type('T')
  13759. : to_char_type('F'));
  13760. }
  13761. break;
  13762. }
  13763. case value_t::number_integer:
  13764. {
  13765. write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
  13766. break;
  13767. }
  13768. case value_t::number_unsigned:
  13769. {
  13770. write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
  13771. break;
  13772. }
  13773. case value_t::number_float:
  13774. {
  13775. write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
  13776. break;
  13777. }
  13778. case value_t::string:
  13779. {
  13780. if (add_prefix)
  13781. {
  13782. oa->write_character(to_char_type('S'));
  13783. }
  13784. write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
  13785. oa->write_characters(
  13786. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13787. j.m_data.m_value.string->size());
  13788. break;
  13789. }
  13790. case value_t::array:
  13791. {
  13792. if (add_prefix)
  13793. {
  13794. oa->write_character(to_char_type('['));
  13795. }
  13796. bool prefix_required = true;
  13797. if (use_type && !j.m_data.m_value.array->empty())
  13798. {
  13799. JSON_ASSERT(use_count);
  13800. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13801. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  13802. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13803. {
  13804. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13805. });
  13806. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13807. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13808. {
  13809. prefix_required = false;
  13810. oa->write_character(to_char_type('$'));
  13811. oa->write_character(first_prefix);
  13812. }
  13813. }
  13814. if (use_count)
  13815. {
  13816. oa->write_character(to_char_type('#'));
  13817. write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
  13818. }
  13819. for (const auto& el : *j.m_data.m_value.array)
  13820. {
  13821. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  13822. }
  13823. if (!use_count)
  13824. {
  13825. oa->write_character(to_char_type(']'));
  13826. }
  13827. break;
  13828. }
  13829. case value_t::binary:
  13830. {
  13831. if (add_prefix)
  13832. {
  13833. oa->write_character(to_char_type('['));
  13834. }
  13835. if (use_type && !j.m_data.m_value.binary->empty())
  13836. {
  13837. JSON_ASSERT(use_count);
  13838. oa->write_character(to_char_type('$'));
  13839. oa->write_character('U');
  13840. }
  13841. if (use_count)
  13842. {
  13843. oa->write_character(to_char_type('#'));
  13844. write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
  13845. }
  13846. if (use_type)
  13847. {
  13848. oa->write_characters(
  13849. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13850. j.m_data.m_value.binary->size());
  13851. }
  13852. else
  13853. {
  13854. for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
  13855. {
  13856. oa->write_character(to_char_type('U'));
  13857. oa->write_character(j.m_data.m_value.binary->data()[i]);
  13858. }
  13859. }
  13860. if (!use_count)
  13861. {
  13862. oa->write_character(to_char_type(']'));
  13863. }
  13864. break;
  13865. }
  13866. case value_t::object:
  13867. {
  13868. if (use_bjdata && j.m_data.m_value.object->size() == 3 && j.m_data.m_value.object->find("_ArrayType_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArraySize_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArrayData_") != j.m_data.m_value.object->end())
  13869. {
  13870. if (!write_bjdata_ndarray(*j.m_data.m_value.object, use_count, use_type)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  13871. {
  13872. break;
  13873. }
  13874. }
  13875. if (add_prefix)
  13876. {
  13877. oa->write_character(to_char_type('{'));
  13878. }
  13879. bool prefix_required = true;
  13880. if (use_type && !j.m_data.m_value.object->empty())
  13881. {
  13882. JSON_ASSERT(use_count);
  13883. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13884. const bool same_prefix = std::all_of(j.begin(), j.end(),
  13885. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13886. {
  13887. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13888. });
  13889. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13890. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13891. {
  13892. prefix_required = false;
  13893. oa->write_character(to_char_type('$'));
  13894. oa->write_character(first_prefix);
  13895. }
  13896. }
  13897. if (use_count)
  13898. {
  13899. oa->write_character(to_char_type('#'));
  13900. write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
  13901. }
  13902. for (const auto& el : *j.m_data.m_value.object)
  13903. {
  13904. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  13905. oa->write_characters(
  13906. reinterpret_cast<const CharType*>(el.first.c_str()),
  13907. el.first.size());
  13908. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  13909. }
  13910. if (!use_count)
  13911. {
  13912. oa->write_character(to_char_type('}'));
  13913. }
  13914. break;
  13915. }
  13916. case value_t::discarded:
  13917. default:
  13918. break;
  13919. }
  13920. }
  13921. private:
  13922. //////////
  13923. // BSON //
  13924. //////////
  13925. /*!
  13926. @return The size of a BSON document entry header, including the id marker
  13927. and the entry name size (and its null-terminator).
  13928. */
  13929. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  13930. {
  13931. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  13932. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  13933. {
  13934. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  13935. static_cast<void>(j);
  13936. }
  13937. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  13938. }
  13939. /*!
  13940. @brief Writes the given @a element_type and @a name to the output adapter
  13941. */
  13942. void write_bson_entry_header(const string_t& name,
  13943. const std::uint8_t element_type)
  13944. {
  13945. oa->write_character(to_char_type(element_type)); // boolean
  13946. oa->write_characters(
  13947. reinterpret_cast<const CharType*>(name.c_str()),
  13948. name.size() + 1u);
  13949. }
  13950. /*!
  13951. @brief Writes a BSON element with key @a name and boolean value @a value
  13952. */
  13953. void write_bson_boolean(const string_t& name,
  13954. const bool value)
  13955. {
  13956. write_bson_entry_header(name, 0x08);
  13957. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  13958. }
  13959. /*!
  13960. @brief Writes a BSON element with key @a name and double value @a value
  13961. */
  13962. void write_bson_double(const string_t& name,
  13963. const double value)
  13964. {
  13965. write_bson_entry_header(name, 0x01);
  13966. write_number<double>(value, true);
  13967. }
  13968. /*!
  13969. @return The size of the BSON-encoded string in @a value
  13970. */
  13971. static std::size_t calc_bson_string_size(const string_t& value)
  13972. {
  13973. return sizeof(std::int32_t) + value.size() + 1ul;
  13974. }
  13975. /*!
  13976. @brief Writes a BSON element with key @a name and string value @a value
  13977. */
  13978. void write_bson_string(const string_t& name,
  13979. const string_t& value)
  13980. {
  13981. write_bson_entry_header(name, 0x02);
  13982. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  13983. oa->write_characters(
  13984. reinterpret_cast<const CharType*>(value.c_str()),
  13985. value.size() + 1);
  13986. }
  13987. /*!
  13988. @brief Writes a BSON element with key @a name and null value
  13989. */
  13990. void write_bson_null(const string_t& name)
  13991. {
  13992. write_bson_entry_header(name, 0x0A);
  13993. }
  13994. /*!
  13995. @return The size of the BSON-encoded integer @a value
  13996. */
  13997. static std::size_t calc_bson_integer_size(const std::int64_t value)
  13998. {
  13999. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  14000. ? sizeof(std::int32_t)
  14001. : sizeof(std::int64_t);
  14002. }
  14003. /*!
  14004. @brief Writes a BSON element with key @a name and integer @a value
  14005. */
  14006. void write_bson_integer(const string_t& name,
  14007. const std::int64_t value)
  14008. {
  14009. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  14010. {
  14011. write_bson_entry_header(name, 0x10); // int32
  14012. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  14013. }
  14014. else
  14015. {
  14016. write_bson_entry_header(name, 0x12); // int64
  14017. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  14018. }
  14019. }
  14020. /*!
  14021. @return The size of the BSON-encoded unsigned integer in @a j
  14022. */
  14023. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  14024. {
  14025. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14026. ? sizeof(std::int32_t)
  14027. : sizeof(std::int64_t);
  14028. }
  14029. /*!
  14030. @brief Writes a BSON element with key @a name and unsigned @a value
  14031. */
  14032. void write_bson_unsigned(const string_t& name,
  14033. const BasicJsonType& j)
  14034. {
  14035. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14036. {
  14037. write_bson_entry_header(name, 0x10 /* int32 */);
  14038. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
  14039. }
  14040. else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14041. {
  14042. write_bson_entry_header(name, 0x12 /* int64 */);
  14043. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
  14044. }
  14045. else
  14046. {
  14047. JSON_THROW(out_of_range::create(407, concat("integer number ", std::to_string(j.m_data.m_value.number_unsigned), " cannot be represented by BSON as it does not fit int64"), &j));
  14048. }
  14049. }
  14050. /*!
  14051. @brief Writes a BSON element with key @a name and object @a value
  14052. */
  14053. void write_bson_object_entry(const string_t& name,
  14054. const typename BasicJsonType::object_t& value)
  14055. {
  14056. write_bson_entry_header(name, 0x03); // object
  14057. write_bson_object(value);
  14058. }
  14059. /*!
  14060. @return The size of the BSON-encoded array @a value
  14061. */
  14062. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  14063. {
  14064. std::size_t array_index = 0ul;
  14065. 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)
  14066. {
  14067. return result + calc_bson_element_size(std::to_string(array_index++), el);
  14068. });
  14069. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  14070. }
  14071. /*!
  14072. @return The size of the BSON-encoded binary array @a value
  14073. */
  14074. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  14075. {
  14076. return sizeof(std::int32_t) + value.size() + 1ul;
  14077. }
  14078. /*!
  14079. @brief Writes a BSON element with key @a name and array @a value
  14080. */
  14081. void write_bson_array(const string_t& name,
  14082. const typename BasicJsonType::array_t& value)
  14083. {
  14084. write_bson_entry_header(name, 0x04); // array
  14085. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  14086. std::size_t array_index = 0ul;
  14087. for (const auto& el : value)
  14088. {
  14089. write_bson_element(std::to_string(array_index++), el);
  14090. }
  14091. oa->write_character(to_char_type(0x00));
  14092. }
  14093. /*!
  14094. @brief Writes a BSON element with key @a name and binary value @a value
  14095. */
  14096. void write_bson_binary(const string_t& name,
  14097. const binary_t& value)
  14098. {
  14099. write_bson_entry_header(name, 0x05);
  14100. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  14101. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  14102. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  14103. }
  14104. /*!
  14105. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  14106. @return The calculated size for the BSON document entry for @a j with the given @a name.
  14107. */
  14108. static std::size_t calc_bson_element_size(const string_t& name,
  14109. const BasicJsonType& j)
  14110. {
  14111. const auto header_size = calc_bson_entry_header_size(name, j);
  14112. switch (j.type())
  14113. {
  14114. case value_t::object:
  14115. return header_size + calc_bson_object_size(*j.m_data.m_value.object);
  14116. case value_t::array:
  14117. return header_size + calc_bson_array_size(*j.m_data.m_value.array);
  14118. case value_t::binary:
  14119. return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
  14120. case value_t::boolean:
  14121. return header_size + 1ul;
  14122. case value_t::number_float:
  14123. return header_size + 8ul;
  14124. case value_t::number_integer:
  14125. return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
  14126. case value_t::number_unsigned:
  14127. return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
  14128. case value_t::string:
  14129. return header_size + calc_bson_string_size(*j.m_data.m_value.string);
  14130. case value_t::null:
  14131. return header_size + 0ul;
  14132. // LCOV_EXCL_START
  14133. case value_t::discarded:
  14134. default:
  14135. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14136. return 0ul;
  14137. // LCOV_EXCL_STOP
  14138. }
  14139. }
  14140. /*!
  14141. @brief Serializes the JSON value @a j to BSON and associates it with the
  14142. key @a name.
  14143. @param name The name to associate with the JSON entity @a j within the
  14144. current BSON document
  14145. */
  14146. void write_bson_element(const string_t& name,
  14147. const BasicJsonType& j)
  14148. {
  14149. switch (j.type())
  14150. {
  14151. case value_t::object:
  14152. return write_bson_object_entry(name, *j.m_data.m_value.object);
  14153. case value_t::array:
  14154. return write_bson_array(name, *j.m_data.m_value.array);
  14155. case value_t::binary:
  14156. return write_bson_binary(name, *j.m_data.m_value.binary);
  14157. case value_t::boolean:
  14158. return write_bson_boolean(name, j.m_data.m_value.boolean);
  14159. case value_t::number_float:
  14160. return write_bson_double(name, j.m_data.m_value.number_float);
  14161. case value_t::number_integer:
  14162. return write_bson_integer(name, j.m_data.m_value.number_integer);
  14163. case value_t::number_unsigned:
  14164. return write_bson_unsigned(name, j);
  14165. case value_t::string:
  14166. return write_bson_string(name, *j.m_data.m_value.string);
  14167. case value_t::null:
  14168. return write_bson_null(name);
  14169. // LCOV_EXCL_START
  14170. case value_t::discarded:
  14171. default:
  14172. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14173. return;
  14174. // LCOV_EXCL_STOP
  14175. }
  14176. }
  14177. /*!
  14178. @brief Calculates the size of the BSON serialization of the given
  14179. JSON-object @a j.
  14180. @param[in] value JSON value to serialize
  14181. @pre value.type() == value_t::object
  14182. */
  14183. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14184. {
  14185. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14186. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14187. {
  14188. return result += calc_bson_element_size(el.first, el.second);
  14189. });
  14190. return sizeof(std::int32_t) + document_size + 1ul;
  14191. }
  14192. /*!
  14193. @param[in] value JSON value to serialize
  14194. @pre value.type() == value_t::object
  14195. */
  14196. void write_bson_object(const typename BasicJsonType::object_t& value)
  14197. {
  14198. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  14199. for (const auto& el : value)
  14200. {
  14201. write_bson_element(el.first, el.second);
  14202. }
  14203. oa->write_character(to_char_type(0x00));
  14204. }
  14205. //////////
  14206. // CBOR //
  14207. //////////
  14208. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  14209. {
  14210. return to_char_type(0xFA); // Single-Precision Float
  14211. }
  14212. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  14213. {
  14214. return to_char_type(0xFB); // Double-Precision Float
  14215. }
  14216. /////////////
  14217. // MsgPack //
  14218. /////////////
  14219. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  14220. {
  14221. return to_char_type(0xCA); // float 32
  14222. }
  14223. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  14224. {
  14225. return to_char_type(0xCB); // float 64
  14226. }
  14227. ////////////
  14228. // UBJSON //
  14229. ////////////
  14230. // UBJSON: write number (floating point)
  14231. template<typename NumberType, typename std::enable_if<
  14232. std::is_floating_point<NumberType>::value, int>::type = 0>
  14233. void write_number_with_ubjson_prefix(const NumberType n,
  14234. const bool add_prefix,
  14235. const bool use_bjdata)
  14236. {
  14237. if (add_prefix)
  14238. {
  14239. oa->write_character(get_ubjson_float_prefix(n));
  14240. }
  14241. write_number(n, use_bjdata);
  14242. }
  14243. // UBJSON: write number (unsigned integer)
  14244. template<typename NumberType, typename std::enable_if<
  14245. std::is_unsigned<NumberType>::value, int>::type = 0>
  14246. void write_number_with_ubjson_prefix(const NumberType n,
  14247. const bool add_prefix,
  14248. const bool use_bjdata)
  14249. {
  14250. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14251. {
  14252. if (add_prefix)
  14253. {
  14254. oa->write_character(to_char_type('i')); // int8
  14255. }
  14256. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14257. }
  14258. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  14259. {
  14260. if (add_prefix)
  14261. {
  14262. oa->write_character(to_char_type('U')); // uint8
  14263. }
  14264. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14265. }
  14266. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14267. {
  14268. if (add_prefix)
  14269. {
  14270. oa->write_character(to_char_type('I')); // int16
  14271. }
  14272. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14273. }
  14274. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  14275. {
  14276. if (add_prefix)
  14277. {
  14278. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14279. }
  14280. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  14281. }
  14282. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14283. {
  14284. if (add_prefix)
  14285. {
  14286. oa->write_character(to_char_type('l')); // int32
  14287. }
  14288. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14289. }
  14290. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  14291. {
  14292. if (add_prefix)
  14293. {
  14294. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14295. }
  14296. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  14297. }
  14298. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14299. {
  14300. if (add_prefix)
  14301. {
  14302. oa->write_character(to_char_type('L')); // int64
  14303. }
  14304. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14305. }
  14306. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  14307. {
  14308. if (add_prefix)
  14309. {
  14310. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  14311. }
  14312. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  14313. }
  14314. else
  14315. {
  14316. if (add_prefix)
  14317. {
  14318. oa->write_character(to_char_type('H')); // high-precision number
  14319. }
  14320. const auto number = BasicJsonType(n).dump();
  14321. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14322. for (std::size_t i = 0; i < number.size(); ++i)
  14323. {
  14324. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14325. }
  14326. }
  14327. }
  14328. // UBJSON: write number (signed integer)
  14329. template < typename NumberType, typename std::enable_if <
  14330. std::is_signed<NumberType>::value&&
  14331. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  14332. void write_number_with_ubjson_prefix(const NumberType n,
  14333. const bool add_prefix,
  14334. const bool use_bjdata)
  14335. {
  14336. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  14337. {
  14338. if (add_prefix)
  14339. {
  14340. oa->write_character(to_char_type('i')); // int8
  14341. }
  14342. write_number(static_cast<std::int8_t>(n), use_bjdata);
  14343. }
  14344. 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)()))
  14345. {
  14346. if (add_prefix)
  14347. {
  14348. oa->write_character(to_char_type('U')); // uint8
  14349. }
  14350. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14351. }
  14352. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  14353. {
  14354. if (add_prefix)
  14355. {
  14356. oa->write_character(to_char_type('I')); // int16
  14357. }
  14358. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14359. }
  14360. 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)())))
  14361. {
  14362. if (add_prefix)
  14363. {
  14364. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14365. }
  14366. write_number(static_cast<uint16_t>(n), use_bjdata);
  14367. }
  14368. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  14369. {
  14370. if (add_prefix)
  14371. {
  14372. oa->write_character(to_char_type('l')); // int32
  14373. }
  14374. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14375. }
  14376. 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)())))
  14377. {
  14378. if (add_prefix)
  14379. {
  14380. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14381. }
  14382. write_number(static_cast<uint32_t>(n), use_bjdata);
  14383. }
  14384. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  14385. {
  14386. if (add_prefix)
  14387. {
  14388. oa->write_character(to_char_type('L')); // int64
  14389. }
  14390. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14391. }
  14392. // LCOV_EXCL_START
  14393. else
  14394. {
  14395. if (add_prefix)
  14396. {
  14397. oa->write_character(to_char_type('H')); // high-precision number
  14398. }
  14399. const auto number = BasicJsonType(n).dump();
  14400. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14401. for (std::size_t i = 0; i < number.size(); ++i)
  14402. {
  14403. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14404. }
  14405. }
  14406. // LCOV_EXCL_STOP
  14407. }
  14408. /*!
  14409. @brief determine the type prefix of container values
  14410. */
  14411. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  14412. {
  14413. switch (j.type())
  14414. {
  14415. case value_t::null:
  14416. return 'Z';
  14417. case value_t::boolean:
  14418. return j.m_data.m_value.boolean ? 'T' : 'F';
  14419. case value_t::number_integer:
  14420. {
  14421. if ((std::numeric_limits<std::int8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  14422. {
  14423. return 'i';
  14424. }
  14425. if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  14426. {
  14427. return 'U';
  14428. }
  14429. if ((std::numeric_limits<std::int16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  14430. {
  14431. return 'I';
  14432. }
  14433. if (use_bjdata && ((std::numeric_limits<std::uint16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)()))
  14434. {
  14435. return 'u';
  14436. }
  14437. if ((std::numeric_limits<std::int32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  14438. {
  14439. return 'l';
  14440. }
  14441. if (use_bjdata && ((std::numeric_limits<std::uint32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)()))
  14442. {
  14443. return 'm';
  14444. }
  14445. if ((std::numeric_limits<std::int64_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  14446. {
  14447. return 'L';
  14448. }
  14449. // anything else is treated as high-precision number
  14450. return 'H'; // LCOV_EXCL_LINE
  14451. }
  14452. case value_t::number_unsigned:
  14453. {
  14454. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14455. {
  14456. return 'i';
  14457. }
  14458. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  14459. {
  14460. return 'U';
  14461. }
  14462. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14463. {
  14464. return 'I';
  14465. }
  14466. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  14467. {
  14468. return 'u';
  14469. }
  14470. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14471. {
  14472. return 'l';
  14473. }
  14474. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  14475. {
  14476. return 'm';
  14477. }
  14478. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14479. {
  14480. return 'L';
  14481. }
  14482. if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14483. {
  14484. return 'M';
  14485. }
  14486. // anything else is treated as high-precision number
  14487. return 'H'; // LCOV_EXCL_LINE
  14488. }
  14489. case value_t::number_float:
  14490. return get_ubjson_float_prefix(j.m_data.m_value.number_float);
  14491. case value_t::string:
  14492. return 'S';
  14493. case value_t::array: // fallthrough
  14494. case value_t::binary:
  14495. return '[';
  14496. case value_t::object:
  14497. return '{';
  14498. case value_t::discarded:
  14499. default: // discarded values
  14500. return 'N';
  14501. }
  14502. }
  14503. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  14504. {
  14505. return 'd'; // float 32
  14506. }
  14507. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  14508. {
  14509. return 'D'; // float 64
  14510. }
  14511. /*!
  14512. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  14513. */
  14514. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  14515. {
  14516. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  14517. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  14518. };
  14519. string_t key = "_ArrayType_";
  14520. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  14521. if (it == bjdtype.end())
  14522. {
  14523. return true;
  14524. }
  14525. CharType dtype = it->second;
  14526. key = "_ArraySize_";
  14527. std::size_t len = (value.at(key).empty() ? 0 : 1);
  14528. for (const auto& el : value.at(key))
  14529. {
  14530. len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
  14531. }
  14532. key = "_ArrayData_";
  14533. if (value.at(key).size() != len)
  14534. {
  14535. return true;
  14536. }
  14537. oa->write_character('[');
  14538. oa->write_character('$');
  14539. oa->write_character(dtype);
  14540. oa->write_character('#');
  14541. key = "_ArraySize_";
  14542. write_ubjson(value.at(key), use_count, use_type, true, true);
  14543. key = "_ArrayData_";
  14544. if (dtype == 'U' || dtype == 'C')
  14545. {
  14546. for (const auto& el : value.at(key))
  14547. {
  14548. write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
  14549. }
  14550. }
  14551. else if (dtype == 'i')
  14552. {
  14553. for (const auto& el : value.at(key))
  14554. {
  14555. write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
  14556. }
  14557. }
  14558. else if (dtype == 'u')
  14559. {
  14560. for (const auto& el : value.at(key))
  14561. {
  14562. write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
  14563. }
  14564. }
  14565. else if (dtype == 'I')
  14566. {
  14567. for (const auto& el : value.at(key))
  14568. {
  14569. write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
  14570. }
  14571. }
  14572. else if (dtype == 'm')
  14573. {
  14574. for (const auto& el : value.at(key))
  14575. {
  14576. write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
  14577. }
  14578. }
  14579. else if (dtype == 'l')
  14580. {
  14581. for (const auto& el : value.at(key))
  14582. {
  14583. write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
  14584. }
  14585. }
  14586. else if (dtype == 'M')
  14587. {
  14588. for (const auto& el : value.at(key))
  14589. {
  14590. write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
  14591. }
  14592. }
  14593. else if (dtype == 'L')
  14594. {
  14595. for (const auto& el : value.at(key))
  14596. {
  14597. write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
  14598. }
  14599. }
  14600. else if (dtype == 'd')
  14601. {
  14602. for (const auto& el : value.at(key))
  14603. {
  14604. write_number(static_cast<float>(el.m_data.m_value.number_float), true);
  14605. }
  14606. }
  14607. else if (dtype == 'D')
  14608. {
  14609. for (const auto& el : value.at(key))
  14610. {
  14611. write_number(static_cast<double>(el.m_data.m_value.number_float), true);
  14612. }
  14613. }
  14614. return false;
  14615. }
  14616. ///////////////////////
  14617. // Utility functions //
  14618. ///////////////////////
  14619. /*
  14620. @brief write a number to output input
  14621. @param[in] n number of type @a NumberType
  14622. @param[in] OutputIsLittleEndian Set to true if output data is
  14623. required to be little endian
  14624. @tparam NumberType the type of the number
  14625. @note This function needs to respect the system's endianness, because bytes
  14626. in CBOR, MessagePack, and UBJSON are stored in network order (big
  14627. endian) and therefore need reordering on little endian systems.
  14628. On the other hand, BSON and BJData use little endian and should reorder
  14629. on big endian systems.
  14630. */
  14631. template<typename NumberType>
  14632. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  14633. {
  14634. // step 1: write number to array of length NumberType
  14635. std::array<CharType, sizeof(NumberType)> vec{};
  14636. std::memcpy(vec.data(), &n, sizeof(NumberType));
  14637. // step 2: write array to output (with possible reordering)
  14638. if (is_little_endian != OutputIsLittleEndian)
  14639. {
  14640. // reverse byte order prior to conversion if necessary
  14641. std::reverse(vec.begin(), vec.end());
  14642. }
  14643. oa->write_characters(vec.data(), sizeof(NumberType));
  14644. }
  14645. void write_compact_float(const number_float_t n, detail::input_format_t format)
  14646. {
  14647. #ifdef __GNUC__
  14648. #pragma GCC diagnostic push
  14649. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14650. #endif
  14651. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  14652. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  14653. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  14654. {
  14655. oa->write_character(format == detail::input_format_t::cbor
  14656. ? get_cbor_float_prefix(static_cast<float>(n))
  14657. : get_msgpack_float_prefix(static_cast<float>(n)));
  14658. write_number(static_cast<float>(n));
  14659. }
  14660. else
  14661. {
  14662. oa->write_character(format == detail::input_format_t::cbor
  14663. ? get_cbor_float_prefix(n)
  14664. : get_msgpack_float_prefix(n));
  14665. write_number(n);
  14666. }
  14667. #ifdef __GNUC__
  14668. #pragma GCC diagnostic pop
  14669. #endif
  14670. }
  14671. public:
  14672. // The following to_char_type functions are implement the conversion
  14673. // between uint8_t and CharType. In case CharType is not unsigned,
  14674. // such a conversion is required to allow values greater than 128.
  14675. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  14676. template < typename C = CharType,
  14677. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  14678. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14679. {
  14680. return *reinterpret_cast<char*>(&x);
  14681. }
  14682. template < typename C = CharType,
  14683. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  14684. static CharType to_char_type(std::uint8_t x) noexcept
  14685. {
  14686. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  14687. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  14688. CharType result;
  14689. std::memcpy(&result, &x, sizeof(x));
  14690. return result;
  14691. }
  14692. template<typename C = CharType,
  14693. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  14694. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14695. {
  14696. return x;
  14697. }
  14698. template < typename InputCharType, typename C = CharType,
  14699. enable_if_t <
  14700. std::is_signed<C>::value &&
  14701. std::is_signed<char>::value &&
  14702. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  14703. > * = nullptr >
  14704. static constexpr CharType to_char_type(InputCharType x) noexcept
  14705. {
  14706. return x;
  14707. }
  14708. private:
  14709. /// whether we can assume little endianness
  14710. const bool is_little_endian = little_endianness();
  14711. /// the output
  14712. output_adapter_t<CharType> oa = nullptr;
  14713. };
  14714. } // namespace detail
  14715. NLOHMANN_JSON_NAMESPACE_END
  14716. // #include <nlohmann/detail/output/output_adapters.hpp>
  14717. // #include <nlohmann/detail/output/serializer.hpp>
  14718. // __ _____ _____ _____
  14719. // __| | __| | | | JSON for Modern C++
  14720. // | | |__ | | | | | | version 3.11.3
  14721. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14722. //
  14723. // SPDX-FileCopyrightText: 2008-2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  14724. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  14725. // SPDX-License-Identifier: MIT
  14726. #include <algorithm> // reverse, remove, fill, find, none_of
  14727. #include <array> // array
  14728. #include <clocale> // localeconv, lconv
  14729. #include <cmath> // labs, isfinite, isnan, signbit
  14730. #include <cstddef> // size_t, ptrdiff_t
  14731. #include <cstdint> // uint8_t
  14732. #include <cstdio> // snprintf
  14733. #include <limits> // numeric_limits
  14734. #include <string> // string, char_traits
  14735. #include <iomanip> // setfill, setw
  14736. #include <type_traits> // is_same
  14737. #include <utility> // move
  14738. // #include <nlohmann/detail/conversions/to_chars.hpp>
  14739. // __ _____ _____ _____
  14740. // __| | __| | | | JSON for Modern C++
  14741. // | | |__ | | | | | | version 3.11.3
  14742. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14743. //
  14744. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  14745. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  14746. // SPDX-License-Identifier: MIT
  14747. #include <array> // array
  14748. #include <cmath> // signbit, isfinite
  14749. #include <cstdint> // intN_t, uintN_t
  14750. #include <cstring> // memcpy, memmove
  14751. #include <limits> // numeric_limits
  14752. #include <type_traits> // conditional
  14753. // #include <nlohmann/detail/macro_scope.hpp>
  14754. NLOHMANN_JSON_NAMESPACE_BEGIN
  14755. namespace detail
  14756. {
  14757. /*!
  14758. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  14759. conversion.
  14760. This implementation is a slightly modified version of the reference
  14761. implementation which may be obtained from
  14762. http://florian.loitsch.com/publications (bench.tar.gz).
  14763. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  14764. For a detailed description of the algorithm see:
  14765. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  14766. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  14767. Language Design and Implementation, PLDI 2010
  14768. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  14769. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  14770. Design and Implementation, PLDI 1996
  14771. */
  14772. namespace dtoa_impl
  14773. {
  14774. template<typename Target, typename Source>
  14775. Target reinterpret_bits(const Source source)
  14776. {
  14777. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  14778. Target target;
  14779. std::memcpy(&target, &source, sizeof(Source));
  14780. return target;
  14781. }
  14782. struct diyfp // f * 2^e
  14783. {
  14784. static constexpr int kPrecision = 64; // = q
  14785. std::uint64_t f = 0;
  14786. int e = 0;
  14787. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  14788. /*!
  14789. @brief returns x - y
  14790. @pre x.e == y.e and x.f >= y.f
  14791. */
  14792. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  14793. {
  14794. JSON_ASSERT(x.e == y.e);
  14795. JSON_ASSERT(x.f >= y.f);
  14796. return {x.f - y.f, x.e};
  14797. }
  14798. /*!
  14799. @brief returns x * y
  14800. @note The result is rounded. (Only the upper q bits are returned.)
  14801. */
  14802. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  14803. {
  14804. static_assert(kPrecision == 64, "internal error");
  14805. // Computes:
  14806. // f = round((x.f * y.f) / 2^q)
  14807. // e = x.e + y.e + q
  14808. // Emulate the 64-bit * 64-bit multiplication:
  14809. //
  14810. // p = u * v
  14811. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  14812. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  14813. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  14814. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  14815. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  14816. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  14817. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  14818. //
  14819. // (Since Q might be larger than 2^32 - 1)
  14820. //
  14821. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  14822. //
  14823. // (Q_hi + H does not overflow a 64-bit int)
  14824. //
  14825. // = p_lo + 2^64 p_hi
  14826. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  14827. const std::uint64_t u_hi = x.f >> 32u;
  14828. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  14829. const std::uint64_t v_hi = y.f >> 32u;
  14830. const std::uint64_t p0 = u_lo * v_lo;
  14831. const std::uint64_t p1 = u_lo * v_hi;
  14832. const std::uint64_t p2 = u_hi * v_lo;
  14833. const std::uint64_t p3 = u_hi * v_hi;
  14834. const std::uint64_t p0_hi = p0 >> 32u;
  14835. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  14836. const std::uint64_t p1_hi = p1 >> 32u;
  14837. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  14838. const std::uint64_t p2_hi = p2 >> 32u;
  14839. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  14840. // The full product might now be computed as
  14841. //
  14842. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  14843. // p_lo = p0_lo + (Q << 32)
  14844. //
  14845. // But in this particular case here, the full p_lo is not required.
  14846. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  14847. // Q_hi + 1 does not overflow).
  14848. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  14849. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  14850. return {h, x.e + y.e + 64};
  14851. }
  14852. /*!
  14853. @brief normalize x such that the significand is >= 2^(q-1)
  14854. @pre x.f != 0
  14855. */
  14856. static diyfp normalize(diyfp x) noexcept
  14857. {
  14858. JSON_ASSERT(x.f != 0);
  14859. while ((x.f >> 63u) == 0)
  14860. {
  14861. x.f <<= 1u;
  14862. x.e--;
  14863. }
  14864. return x;
  14865. }
  14866. /*!
  14867. @brief normalize x such that the result has the exponent E
  14868. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  14869. */
  14870. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  14871. {
  14872. const int delta = x.e - target_exponent;
  14873. JSON_ASSERT(delta >= 0);
  14874. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  14875. return {x.f << delta, target_exponent};
  14876. }
  14877. };
  14878. struct boundaries
  14879. {
  14880. diyfp w;
  14881. diyfp minus;
  14882. diyfp plus;
  14883. };
  14884. /*!
  14885. Compute the (normalized) diyfp representing the input number 'value' and its
  14886. boundaries.
  14887. @pre value must be finite and positive
  14888. */
  14889. template<typename FloatType>
  14890. boundaries compute_boundaries(FloatType value)
  14891. {
  14892. JSON_ASSERT(std::isfinite(value));
  14893. JSON_ASSERT(value > 0);
  14894. // Convert the IEEE representation into a diyfp.
  14895. //
  14896. // If v is denormal:
  14897. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  14898. // If v is normalized:
  14899. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  14900. static_assert(std::numeric_limits<FloatType>::is_iec559,
  14901. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  14902. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  14903. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  14904. constexpr int kMinExp = 1 - kBias;
  14905. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  14906. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  14907. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  14908. const std::uint64_t E = bits >> (kPrecision - 1);
  14909. const std::uint64_t F = bits & (kHiddenBit - 1);
  14910. const bool is_denormal = E == 0;
  14911. const diyfp v = is_denormal
  14912. ? diyfp(F, kMinExp)
  14913. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  14914. // Compute the boundaries m- and m+ of the floating-point value
  14915. // v = f * 2^e.
  14916. //
  14917. // Determine v- and v+, the floating-point predecessor and successor if v,
  14918. // respectively.
  14919. //
  14920. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  14921. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  14922. //
  14923. // v+ = v + 2^e
  14924. //
  14925. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  14926. // between m- and m+ round to v, regardless of how the input rounding
  14927. // algorithm breaks ties.
  14928. //
  14929. // ---+-------------+-------------+-------------+-------------+--- (A)
  14930. // v- m- v m+ v+
  14931. //
  14932. // -----------------+------+------+-------------+-------------+--- (B)
  14933. // v- m- v m+ v+
  14934. const bool lower_boundary_is_closer = F == 0 && E > 1;
  14935. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  14936. const diyfp m_minus = lower_boundary_is_closer
  14937. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  14938. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  14939. // Determine the normalized w+ = m+.
  14940. const diyfp w_plus = diyfp::normalize(m_plus);
  14941. // Determine w- = m- such that e_(w-) = e_(w+).
  14942. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  14943. return {diyfp::normalize(v), w_minus, w_plus};
  14944. }
  14945. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  14946. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  14947. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  14948. //
  14949. // alpha <= e = e_c + e_w + q <= gamma
  14950. //
  14951. // or
  14952. //
  14953. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  14954. // <= f_c * f_w * 2^gamma
  14955. //
  14956. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  14957. //
  14958. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  14959. //
  14960. // or
  14961. //
  14962. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  14963. //
  14964. // The choice of (alpha,gamma) determines the size of the table and the form of
  14965. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  14966. // in practice:
  14967. //
  14968. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  14969. // processed independently: An integral part p1, and a fractional part p2:
  14970. //
  14971. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  14972. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  14973. // = p1 + p2 * 2^e
  14974. //
  14975. // The conversion of p1 into decimal form requires a series of divisions and
  14976. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  14977. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  14978. // achieved by choosing
  14979. //
  14980. // -e >= 32 or e <= -32 := gamma
  14981. //
  14982. // In order to convert the fractional part
  14983. //
  14984. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14985. //
  14986. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  14987. // d[-i] are extracted in order:
  14988. //
  14989. // (10 * p2) div 2^-e = d[-1]
  14990. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  14991. //
  14992. // The multiplication by 10 must not overflow. It is sufficient to choose
  14993. //
  14994. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  14995. //
  14996. // Since p2 = f mod 2^-e < 2^-e,
  14997. //
  14998. // -e <= 60 or e >= -60 := alpha
  14999. constexpr int kAlpha = -60;
  15000. constexpr int kGamma = -32;
  15001. struct cached_power // c = f * 2^e ~= 10^k
  15002. {
  15003. std::uint64_t f;
  15004. int e;
  15005. int k;
  15006. };
  15007. /*!
  15008. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  15009. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  15010. satisfies (Definition 3.2 from [1])
  15011. alpha <= e_c + e + q <= gamma.
  15012. */
  15013. inline cached_power get_cached_power_for_binary_exponent(int e)
  15014. {
  15015. // Now
  15016. //
  15017. // alpha <= e_c + e + q <= gamma (1)
  15018. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  15019. //
  15020. // and since the c's are normalized, 2^(q-1) <= f_c,
  15021. //
  15022. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  15023. // ==> 2^(alpha - e - 1) <= c
  15024. //
  15025. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  15026. //
  15027. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  15028. // = ceil( (alpha - e - 1) * log_10(2) )
  15029. //
  15030. // From the paper:
  15031. // "In theory the result of the procedure could be wrong since c is rounded,
  15032. // and the computation itself is approximated [...]. In practice, however,
  15033. // this simple function is sufficient."
  15034. //
  15035. // For IEEE double precision floating-point numbers converted into
  15036. // normalized diyfp's w = f * 2^e, with q = 64,
  15037. //
  15038. // e >= -1022 (min IEEE exponent)
  15039. // -52 (p - 1)
  15040. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  15041. // -11 (normalize the diyfp)
  15042. // = -1137
  15043. //
  15044. // and
  15045. //
  15046. // e <= +1023 (max IEEE exponent)
  15047. // -52 (p - 1)
  15048. // -11 (normalize the diyfp)
  15049. // = 960
  15050. //
  15051. // This binary exponent range [-1137,960] results in a decimal exponent
  15052. // range [-307,324]. One does not need to store a cached power for each
  15053. // k in this range. For each such k it suffices to find a cached power
  15054. // such that the exponent of the product lies in [alpha,gamma].
  15055. // This implies that the difference of the decimal exponents of adjacent
  15056. // table entries must be less than or equal to
  15057. //
  15058. // floor( (gamma - alpha) * log_10(2) ) = 8.
  15059. //
  15060. // (A smaller distance gamma-alpha would require a larger table.)
  15061. // NB:
  15062. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  15063. constexpr int kCachedPowersMinDecExp = -300;
  15064. constexpr int kCachedPowersDecStep = 8;
  15065. static constexpr std::array<cached_power, 79> kCachedPowers =
  15066. {
  15067. {
  15068. { 0xAB70FE17C79AC6CA, -1060, -300 },
  15069. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  15070. { 0xBE5691EF416BD60C, -1007, -284 },
  15071. { 0x8DD01FAD907FFC3C, -980, -276 },
  15072. { 0xD3515C2831559A83, -954, -268 },
  15073. { 0x9D71AC8FADA6C9B5, -927, -260 },
  15074. { 0xEA9C227723EE8BCB, -901, -252 },
  15075. { 0xAECC49914078536D, -874, -244 },
  15076. { 0x823C12795DB6CE57, -847, -236 },
  15077. { 0xC21094364DFB5637, -821, -228 },
  15078. { 0x9096EA6F3848984F, -794, -220 },
  15079. { 0xD77485CB25823AC7, -768, -212 },
  15080. { 0xA086CFCD97BF97F4, -741, -204 },
  15081. { 0xEF340A98172AACE5, -715, -196 },
  15082. { 0xB23867FB2A35B28E, -688, -188 },
  15083. { 0x84C8D4DFD2C63F3B, -661, -180 },
  15084. { 0xC5DD44271AD3CDBA, -635, -172 },
  15085. { 0x936B9FCEBB25C996, -608, -164 },
  15086. { 0xDBAC6C247D62A584, -582, -156 },
  15087. { 0xA3AB66580D5FDAF6, -555, -148 },
  15088. { 0xF3E2F893DEC3F126, -529, -140 },
  15089. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  15090. { 0x87625F056C7C4A8B, -475, -124 },
  15091. { 0xC9BCFF6034C13053, -449, -116 },
  15092. { 0x964E858C91BA2655, -422, -108 },
  15093. { 0xDFF9772470297EBD, -396, -100 },
  15094. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  15095. { 0xF8A95FCF88747D94, -343, -84 },
  15096. { 0xB94470938FA89BCF, -316, -76 },
  15097. { 0x8A08F0F8BF0F156B, -289, -68 },
  15098. { 0xCDB02555653131B6, -263, -60 },
  15099. { 0x993FE2C6D07B7FAC, -236, -52 },
  15100. { 0xE45C10C42A2B3B06, -210, -44 },
  15101. { 0xAA242499697392D3, -183, -36 },
  15102. { 0xFD87B5F28300CA0E, -157, -28 },
  15103. { 0xBCE5086492111AEB, -130, -20 },
  15104. { 0x8CBCCC096F5088CC, -103, -12 },
  15105. { 0xD1B71758E219652C, -77, -4 },
  15106. { 0x9C40000000000000, -50, 4 },
  15107. { 0xE8D4A51000000000, -24, 12 },
  15108. { 0xAD78EBC5AC620000, 3, 20 },
  15109. { 0x813F3978F8940984, 30, 28 },
  15110. { 0xC097CE7BC90715B3, 56, 36 },
  15111. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15112. { 0xD5D238A4ABE98068, 109, 52 },
  15113. { 0x9F4F2726179A2245, 136, 60 },
  15114. { 0xED63A231D4C4FB27, 162, 68 },
  15115. { 0xB0DE65388CC8ADA8, 189, 76 },
  15116. { 0x83C7088E1AAB65DB, 216, 84 },
  15117. { 0xC45D1DF942711D9A, 242, 92 },
  15118. { 0x924D692CA61BE758, 269, 100 },
  15119. { 0xDA01EE641A708DEA, 295, 108 },
  15120. { 0xA26DA3999AEF774A, 322, 116 },
  15121. { 0xF209787BB47D6B85, 348, 124 },
  15122. { 0xB454E4A179DD1877, 375, 132 },
  15123. { 0x865B86925B9BC5C2, 402, 140 },
  15124. { 0xC83553C5C8965D3D, 428, 148 },
  15125. { 0x952AB45CFA97A0B3, 455, 156 },
  15126. { 0xDE469FBD99A05FE3, 481, 164 },
  15127. { 0xA59BC234DB398C25, 508, 172 },
  15128. { 0xF6C69A72A3989F5C, 534, 180 },
  15129. { 0xB7DCBF5354E9BECE, 561, 188 },
  15130. { 0x88FCF317F22241E2, 588, 196 },
  15131. { 0xCC20CE9BD35C78A5, 614, 204 },
  15132. { 0x98165AF37B2153DF, 641, 212 },
  15133. { 0xE2A0B5DC971F303A, 667, 220 },
  15134. { 0xA8D9D1535CE3B396, 694, 228 },
  15135. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15136. { 0xBB764C4CA7A44410, 747, 244 },
  15137. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15138. { 0xD01FEF10A657842C, 800, 260 },
  15139. { 0x9B10A4E5E9913129, 827, 268 },
  15140. { 0xE7109BFBA19C0C9D, 853, 276 },
  15141. { 0xAC2820D9623BF429, 880, 284 },
  15142. { 0x80444B5E7AA7CF85, 907, 292 },
  15143. { 0xBF21E44003ACDD2D, 933, 300 },
  15144. { 0x8E679C2F5E44FF8F, 960, 308 },
  15145. { 0xD433179D9C8CB841, 986, 316 },
  15146. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15147. }
  15148. };
  15149. // This computation gives exactly the same results for k as
  15150. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15151. // for |e| <= 1500, but doesn't require floating-point operations.
  15152. // NB: log_10(2) ~= 78913 / 2^18
  15153. JSON_ASSERT(e >= -1500);
  15154. JSON_ASSERT(e <= 1500);
  15155. const int f = kAlpha - e - 1;
  15156. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  15157. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15158. JSON_ASSERT(index >= 0);
  15159. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15160. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15161. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15162. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15163. return cached;
  15164. }
  15165. /*!
  15166. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15167. For n == 0, returns 1 and sets pow10 := 1.
  15168. */
  15169. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15170. {
  15171. // LCOV_EXCL_START
  15172. if (n >= 1000000000)
  15173. {
  15174. pow10 = 1000000000;
  15175. return 10;
  15176. }
  15177. // LCOV_EXCL_STOP
  15178. if (n >= 100000000)
  15179. {
  15180. pow10 = 100000000;
  15181. return 9;
  15182. }
  15183. if (n >= 10000000)
  15184. {
  15185. pow10 = 10000000;
  15186. return 8;
  15187. }
  15188. if (n >= 1000000)
  15189. {
  15190. pow10 = 1000000;
  15191. return 7;
  15192. }
  15193. if (n >= 100000)
  15194. {
  15195. pow10 = 100000;
  15196. return 6;
  15197. }
  15198. if (n >= 10000)
  15199. {
  15200. pow10 = 10000;
  15201. return 5;
  15202. }
  15203. if (n >= 1000)
  15204. {
  15205. pow10 = 1000;
  15206. return 4;
  15207. }
  15208. if (n >= 100)
  15209. {
  15210. pow10 = 100;
  15211. return 3;
  15212. }
  15213. if (n >= 10)
  15214. {
  15215. pow10 = 10;
  15216. return 2;
  15217. }
  15218. pow10 = 1;
  15219. return 1;
  15220. }
  15221. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  15222. std::uint64_t rest, std::uint64_t ten_k)
  15223. {
  15224. JSON_ASSERT(len >= 1);
  15225. JSON_ASSERT(dist <= delta);
  15226. JSON_ASSERT(rest <= delta);
  15227. JSON_ASSERT(ten_k > 0);
  15228. // <--------------------------- delta ---->
  15229. // <---- dist --------->
  15230. // --------------[------------------+-------------------]--------------
  15231. // M- w M+
  15232. //
  15233. // ten_k
  15234. // <------>
  15235. // <---- rest ---->
  15236. // --------------[------------------+----+--------------]--------------
  15237. // w V
  15238. // = buf * 10^k
  15239. //
  15240. // ten_k represents a unit-in-the-last-place in the decimal representation
  15241. // stored in buf.
  15242. // Decrement buf by ten_k while this takes buf closer to w.
  15243. // The tests are written in this order to avoid overflow in unsigned
  15244. // integer arithmetic.
  15245. while (rest < dist
  15246. && delta - rest >= ten_k
  15247. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  15248. {
  15249. JSON_ASSERT(buf[len - 1] != '0');
  15250. buf[len - 1]--;
  15251. rest += ten_k;
  15252. }
  15253. }
  15254. /*!
  15255. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  15256. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  15257. */
  15258. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  15259. diyfp M_minus, diyfp w, diyfp M_plus)
  15260. {
  15261. static_assert(kAlpha >= -60, "internal error");
  15262. static_assert(kGamma <= -32, "internal error");
  15263. // Generates the digits (and the exponent) of a decimal floating-point
  15264. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  15265. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  15266. //
  15267. // <--------------------------- delta ---->
  15268. // <---- dist --------->
  15269. // --------------[------------------+-------------------]--------------
  15270. // M- w M+
  15271. //
  15272. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  15273. // V is in [M-,M+].
  15274. JSON_ASSERT(M_plus.e >= kAlpha);
  15275. JSON_ASSERT(M_plus.e <= kGamma);
  15276. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  15277. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  15278. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  15279. //
  15280. // M+ = f * 2^e
  15281. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  15282. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  15283. // = p1 + p2 * 2^e
  15284. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  15285. 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.)
  15286. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  15287. // 1)
  15288. //
  15289. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  15290. JSON_ASSERT(p1 > 0);
  15291. std::uint32_t pow10{};
  15292. const int k = find_largest_pow10(p1, pow10);
  15293. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  15294. //
  15295. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  15296. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  15297. //
  15298. // M+ = p1 + p2 * 2^e
  15299. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  15300. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  15301. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  15302. //
  15303. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  15304. //
  15305. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  15306. //
  15307. // but stop as soon as
  15308. //
  15309. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  15310. int n = k;
  15311. while (n > 0)
  15312. {
  15313. // Invariants:
  15314. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  15315. // pow10 = 10^(n-1) <= p1 < 10^n
  15316. //
  15317. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  15318. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  15319. //
  15320. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  15321. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  15322. //
  15323. JSON_ASSERT(d <= 9);
  15324. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15325. //
  15326. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  15327. //
  15328. p1 = r;
  15329. n--;
  15330. //
  15331. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  15332. // pow10 = 10^n
  15333. //
  15334. // Now check if enough digits have been generated.
  15335. // Compute
  15336. //
  15337. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  15338. //
  15339. // Note:
  15340. // Since rest and delta share the same exponent e, it suffices to
  15341. // compare the significands.
  15342. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  15343. if (rest <= delta)
  15344. {
  15345. // V = buffer * 10^n, with M- <= V <= M+.
  15346. decimal_exponent += n;
  15347. // We may now just stop. But instead look if the buffer could be
  15348. // decremented to bring V closer to w.
  15349. //
  15350. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  15351. // The rounding procedure works with diyfp's with an implicit
  15352. // exponent of e.
  15353. //
  15354. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  15355. //
  15356. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  15357. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  15358. return;
  15359. }
  15360. pow10 /= 10;
  15361. //
  15362. // pow10 = 10^(n-1) <= p1 < 10^n
  15363. // Invariants restored.
  15364. }
  15365. // 2)
  15366. //
  15367. // The digits of the integral part have been generated:
  15368. //
  15369. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  15370. // = buffer + p2 * 2^e
  15371. //
  15372. // Now generate the digits of the fractional part p2 * 2^e.
  15373. //
  15374. // Note:
  15375. // No decimal point is generated: the exponent is adjusted instead.
  15376. //
  15377. // p2 actually represents the fraction
  15378. //
  15379. // p2 * 2^e
  15380. // = p2 / 2^-e
  15381. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15382. //
  15383. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  15384. //
  15385. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  15386. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  15387. //
  15388. // using
  15389. //
  15390. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  15391. // = ( d) * 2^-e + ( r)
  15392. //
  15393. // or
  15394. // 10^m * p2 * 2^e = d + r * 2^e
  15395. //
  15396. // i.e.
  15397. //
  15398. // M+ = buffer + p2 * 2^e
  15399. // = buffer + 10^-m * (d + r * 2^e)
  15400. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  15401. //
  15402. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  15403. JSON_ASSERT(p2 > delta);
  15404. int m = 0;
  15405. for (;;)
  15406. {
  15407. // Invariant:
  15408. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  15409. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  15410. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  15411. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  15412. //
  15413. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  15414. p2 *= 10;
  15415. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  15416. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  15417. //
  15418. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  15419. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  15420. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15421. //
  15422. JSON_ASSERT(d <= 9);
  15423. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15424. //
  15425. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15426. //
  15427. p2 = r;
  15428. m++;
  15429. //
  15430. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  15431. // Invariant restored.
  15432. // Check if enough digits have been generated.
  15433. //
  15434. // 10^-m * p2 * 2^e <= delta * 2^e
  15435. // p2 * 2^e <= 10^m * delta * 2^e
  15436. // p2 <= 10^m * delta
  15437. delta *= 10;
  15438. dist *= 10;
  15439. if (p2 <= delta)
  15440. {
  15441. break;
  15442. }
  15443. }
  15444. // V = buffer * 10^-m, with M- <= V <= M+.
  15445. decimal_exponent -= m;
  15446. // 1 ulp in the decimal representation is now 10^-m.
  15447. // Since delta and dist are now scaled by 10^m, we need to do the
  15448. // same with ulp in order to keep the units in sync.
  15449. //
  15450. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  15451. //
  15452. const std::uint64_t ten_m = one.f;
  15453. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  15454. // By construction this algorithm generates the shortest possible decimal
  15455. // number (Loitsch, Theorem 6.2) which rounds back to w.
  15456. // For an input number of precision p, at least
  15457. //
  15458. // N = 1 + ceil(p * log_10(2))
  15459. //
  15460. // decimal digits are sufficient to identify all binary floating-point
  15461. // numbers (Matula, "In-and-Out conversions").
  15462. // This implies that the algorithm does not produce more than N decimal
  15463. // digits.
  15464. //
  15465. // N = 17 for p = 53 (IEEE double precision)
  15466. // N = 9 for p = 24 (IEEE single precision)
  15467. }
  15468. /*!
  15469. v = buf * 10^decimal_exponent
  15470. len is the length of the buffer (number of decimal digits)
  15471. The buffer must be large enough, i.e. >= max_digits10.
  15472. */
  15473. JSON_HEDLEY_NON_NULL(1)
  15474. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  15475. diyfp m_minus, diyfp v, diyfp m_plus)
  15476. {
  15477. JSON_ASSERT(m_plus.e == m_minus.e);
  15478. JSON_ASSERT(m_plus.e == v.e);
  15479. // --------(-----------------------+-----------------------)-------- (A)
  15480. // m- v m+
  15481. //
  15482. // --------------------(-----------+-----------------------)-------- (B)
  15483. // m- v m+
  15484. //
  15485. // First scale v (and m- and m+) such that the exponent is in the range
  15486. // [alpha, gamma].
  15487. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  15488. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  15489. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  15490. const diyfp w = diyfp::mul(v, c_minus_k);
  15491. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  15492. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  15493. // ----(---+---)---------------(---+---)---------------(---+---)----
  15494. // w- w w+
  15495. // = c*m- = c*v = c*m+
  15496. //
  15497. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  15498. // w+ are now off by a small amount.
  15499. // In fact:
  15500. //
  15501. // w - v * 10^k < 1 ulp
  15502. //
  15503. // To account for this inaccuracy, add resp. subtract 1 ulp.
  15504. //
  15505. // --------+---[---------------(---+---)---------------]---+--------
  15506. // w- M- w M+ w+
  15507. //
  15508. // Now any number in [M-, M+] (bounds included) will round to w when input,
  15509. // regardless of how the input rounding algorithm breaks ties.
  15510. //
  15511. // And digit_gen generates the shortest possible such number in [M-, M+].
  15512. // Note that this does not mean that Grisu2 always generates the shortest
  15513. // possible number in the interval (m-, m+).
  15514. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  15515. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  15516. decimal_exponent = -cached.k; // = -(-k) = k
  15517. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  15518. }
  15519. /*!
  15520. v = buf * 10^decimal_exponent
  15521. len is the length of the buffer (number of decimal digits)
  15522. The buffer must be large enough, i.e. >= max_digits10.
  15523. */
  15524. template<typename FloatType>
  15525. JSON_HEDLEY_NON_NULL(1)
  15526. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  15527. {
  15528. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  15529. "internal error: not enough precision");
  15530. JSON_ASSERT(std::isfinite(value));
  15531. JSON_ASSERT(value > 0);
  15532. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  15533. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  15534. // decimal representations are not exactly "short".
  15535. //
  15536. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  15537. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  15538. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  15539. // does.
  15540. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  15541. // representation using the corresponding std::from_chars function recovers value exactly". That
  15542. // indicates that single precision floating-point numbers should be recovered using
  15543. // 'std::strtof'.
  15544. //
  15545. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  15546. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  15547. // value is off by 1 ulp.
  15548. #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
  15549. const boundaries w = compute_boundaries(static_cast<double>(value));
  15550. #else
  15551. const boundaries w = compute_boundaries(value);
  15552. #endif
  15553. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  15554. }
  15555. /*!
  15556. @brief appends a decimal representation of e to buf
  15557. @return a pointer to the element following the exponent.
  15558. @pre -1000 < e < 1000
  15559. */
  15560. JSON_HEDLEY_NON_NULL(1)
  15561. JSON_HEDLEY_RETURNS_NON_NULL
  15562. inline char* append_exponent(char* buf, int e)
  15563. {
  15564. JSON_ASSERT(e > -1000);
  15565. JSON_ASSERT(e < 1000);
  15566. if (e < 0)
  15567. {
  15568. e = -e;
  15569. *buf++ = '-';
  15570. }
  15571. else
  15572. {
  15573. *buf++ = '+';
  15574. }
  15575. auto k = static_cast<std::uint32_t>(e);
  15576. if (k < 10)
  15577. {
  15578. // Always print at least two digits in the exponent.
  15579. // This is for compatibility with printf("%g").
  15580. *buf++ = '0';
  15581. *buf++ = static_cast<char>('0' + k);
  15582. }
  15583. else if (k < 100)
  15584. {
  15585. *buf++ = static_cast<char>('0' + k / 10);
  15586. k %= 10;
  15587. *buf++ = static_cast<char>('0' + k);
  15588. }
  15589. else
  15590. {
  15591. *buf++ = static_cast<char>('0' + k / 100);
  15592. k %= 100;
  15593. *buf++ = static_cast<char>('0' + k / 10);
  15594. k %= 10;
  15595. *buf++ = static_cast<char>('0' + k);
  15596. }
  15597. return buf;
  15598. }
  15599. /*!
  15600. @brief prettify v = buf * 10^decimal_exponent
  15601. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  15602. notation. Otherwise it will be printed in exponential notation.
  15603. @pre min_exp < 0
  15604. @pre max_exp > 0
  15605. */
  15606. JSON_HEDLEY_NON_NULL(1)
  15607. JSON_HEDLEY_RETURNS_NON_NULL
  15608. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  15609. int min_exp, int max_exp)
  15610. {
  15611. JSON_ASSERT(min_exp < 0);
  15612. JSON_ASSERT(max_exp > 0);
  15613. const int k = len;
  15614. const int n = len + decimal_exponent;
  15615. // v = buf * 10^(n-k)
  15616. // k is the length of the buffer (number of decimal digits)
  15617. // n is the position of the decimal point relative to the start of the buffer.
  15618. if (k <= n && n <= max_exp)
  15619. {
  15620. // digits[000]
  15621. // len <= max_exp + 2
  15622. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  15623. // Make it look like a floating-point number (#362, #378)
  15624. buf[n + 0] = '.';
  15625. buf[n + 1] = '0';
  15626. return buf + (static_cast<size_t>(n) + 2);
  15627. }
  15628. if (0 < n && n <= max_exp)
  15629. {
  15630. // dig.its
  15631. // len <= max_digits10 + 1
  15632. JSON_ASSERT(k > n);
  15633. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  15634. buf[n] = '.';
  15635. return buf + (static_cast<size_t>(k) + 1U);
  15636. }
  15637. if (min_exp < n && n <= 0)
  15638. {
  15639. // 0.[000]digits
  15640. // len <= 2 + (-min_exp - 1) + max_digits10
  15641. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  15642. buf[0] = '0';
  15643. buf[1] = '.';
  15644. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  15645. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  15646. }
  15647. if (k == 1)
  15648. {
  15649. // dE+123
  15650. // len <= 1 + 5
  15651. buf += 1;
  15652. }
  15653. else
  15654. {
  15655. // d.igitsE+123
  15656. // len <= max_digits10 + 1 + 5
  15657. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  15658. buf[1] = '.';
  15659. buf += 1 + static_cast<size_t>(k);
  15660. }
  15661. *buf++ = 'e';
  15662. return append_exponent(buf, n - 1);
  15663. }
  15664. } // namespace dtoa_impl
  15665. /*!
  15666. @brief generates a decimal representation of the floating-point number value in [first, last).
  15667. The format of the resulting decimal representation is similar to printf's %g
  15668. format. Returns an iterator pointing past-the-end of the decimal representation.
  15669. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  15670. @note The buffer must be large enough.
  15671. @note The result is NOT null-terminated.
  15672. */
  15673. template<typename FloatType>
  15674. JSON_HEDLEY_NON_NULL(1, 2)
  15675. JSON_HEDLEY_RETURNS_NON_NULL
  15676. char* to_chars(char* first, const char* last, FloatType value)
  15677. {
  15678. static_cast<void>(last); // maybe unused - fix warning
  15679. JSON_ASSERT(std::isfinite(value));
  15680. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  15681. if (std::signbit(value))
  15682. {
  15683. value = -value;
  15684. *first++ = '-';
  15685. }
  15686. #ifdef __GNUC__
  15687. #pragma GCC diagnostic push
  15688. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15689. #endif
  15690. if (value == 0) // +-0
  15691. {
  15692. *first++ = '0';
  15693. // Make it look like a floating-point number (#362, #378)
  15694. *first++ = '.';
  15695. *first++ = '0';
  15696. return first;
  15697. }
  15698. #ifdef __GNUC__
  15699. #pragma GCC diagnostic pop
  15700. #endif
  15701. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  15702. // Compute v = buffer * 10^decimal_exponent.
  15703. // The decimal digits are stored in the buffer, which needs to be interpreted
  15704. // as an unsigned decimal integer.
  15705. // len is the length of the buffer, i.e. the number of decimal digits.
  15706. int len = 0;
  15707. int decimal_exponent = 0;
  15708. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  15709. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  15710. // Format the buffer like printf("%.*g", prec, value)
  15711. constexpr int kMinExp = -4;
  15712. // Use digits10 here to increase compatibility with version 2.
  15713. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  15714. JSON_ASSERT(last - first >= kMaxExp + 2);
  15715. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  15716. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  15717. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  15718. }
  15719. } // namespace detail
  15720. NLOHMANN_JSON_NAMESPACE_END
  15721. // #include <nlohmann/detail/exceptions.hpp>
  15722. // #include <nlohmann/detail/macro_scope.hpp>
  15723. // #include <nlohmann/detail/meta/cpp_future.hpp>
  15724. // #include <nlohmann/detail/output/binary_writer.hpp>
  15725. // #include <nlohmann/detail/output/output_adapters.hpp>
  15726. // #include <nlohmann/detail/string_concat.hpp>
  15727. // #include <nlohmann/detail/value_t.hpp>
  15728. NLOHMANN_JSON_NAMESPACE_BEGIN
  15729. namespace detail
  15730. {
  15731. ///////////////////
  15732. // serialization //
  15733. ///////////////////
  15734. /// how to treat decoding errors
  15735. enum class error_handler_t
  15736. {
  15737. strict, ///< throw a type_error exception in case of invalid UTF-8
  15738. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  15739. ignore ///< ignore invalid UTF-8 sequences
  15740. };
  15741. template<typename BasicJsonType>
  15742. class serializer
  15743. {
  15744. using string_t = typename BasicJsonType::string_t;
  15745. using number_float_t = typename BasicJsonType::number_float_t;
  15746. using number_integer_t = typename BasicJsonType::number_integer_t;
  15747. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  15748. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  15749. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  15750. static constexpr std::uint8_t UTF8_REJECT = 1;
  15751. public:
  15752. /*!
  15753. @param[in] s output stream to serialize to
  15754. @param[in] ichar indentation character to use
  15755. @param[in] error_handler_ how to react on decoding errors
  15756. */
  15757. serializer(output_adapter_t<char> s, const char ichar,
  15758. error_handler_t error_handler_ = error_handler_t::strict)
  15759. : o(std::move(s))
  15760. , loc(std::localeconv())
  15761. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  15762. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  15763. , indent_char(ichar)
  15764. , indent_string(512, indent_char)
  15765. , error_handler(error_handler_)
  15766. {}
  15767. // delete because of pointer members
  15768. serializer(const serializer&) = delete;
  15769. serializer& operator=(const serializer&) = delete;
  15770. serializer(serializer&&) = delete;
  15771. serializer& operator=(serializer&&) = delete;
  15772. ~serializer() = default;
  15773. /*!
  15774. @brief internal implementation of the serialization function
  15775. This function is called by the public member function dump and organizes
  15776. the serialization internally. The indentation level is propagated as
  15777. additional parameter. In case of arrays and objects, the function is
  15778. called recursively.
  15779. - strings and object keys are escaped using `escape_string()`
  15780. - integer numbers are converted implicitly via `operator<<`
  15781. - floating-point numbers are converted to a string using `"%g"` format
  15782. - binary values are serialized as objects containing the subtype and the
  15783. byte array
  15784. @param[in] val value to serialize
  15785. @param[in] pretty_print whether the output shall be pretty-printed
  15786. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  15787. in the output are escaped with `\uXXXX` sequences, and the result consists
  15788. of ASCII characters only.
  15789. @param[in] indent_step the indent level
  15790. @param[in] current_indent the current indent level (only used internally)
  15791. */
  15792. void dump(const BasicJsonType& val,
  15793. const bool pretty_print,
  15794. const bool ensure_ascii,
  15795. const unsigned int indent_step,
  15796. const unsigned int current_indent = 0)
  15797. {
  15798. switch (val.m_data.m_type)
  15799. {
  15800. case value_t::object:
  15801. {
  15802. if (val.m_data.m_value.object->empty())
  15803. {
  15804. o->write_characters("{}", 2);
  15805. return;
  15806. }
  15807. if (pretty_print)
  15808. {
  15809. o->write_characters("{\n", 2);
  15810. // variable to hold indentation for recursive calls
  15811. const auto new_indent = current_indent + indent_step;
  15812. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15813. {
  15814. indent_string.resize(indent_string.size() * 2, ' ');
  15815. }
  15816. // first n-1 elements
  15817. auto i = val.m_data.m_value.object->cbegin();
  15818. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  15819. {
  15820. o->write_characters(indent_string.c_str(), new_indent);
  15821. o->write_character('\"');
  15822. dump_escaped(i->first, ensure_ascii);
  15823. o->write_characters("\": ", 3);
  15824. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15825. o->write_characters(",\n", 2);
  15826. }
  15827. // last element
  15828. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  15829. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  15830. o->write_characters(indent_string.c_str(), new_indent);
  15831. o->write_character('\"');
  15832. dump_escaped(i->first, ensure_ascii);
  15833. o->write_characters("\": ", 3);
  15834. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15835. o->write_character('\n');
  15836. o->write_characters(indent_string.c_str(), current_indent);
  15837. o->write_character('}');
  15838. }
  15839. else
  15840. {
  15841. o->write_character('{');
  15842. // first n-1 elements
  15843. auto i = val.m_data.m_value.object->cbegin();
  15844. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  15845. {
  15846. o->write_character('\"');
  15847. dump_escaped(i->first, ensure_ascii);
  15848. o->write_characters("\":", 2);
  15849. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15850. o->write_character(',');
  15851. }
  15852. // last element
  15853. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  15854. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  15855. o->write_character('\"');
  15856. dump_escaped(i->first, ensure_ascii);
  15857. o->write_characters("\":", 2);
  15858. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15859. o->write_character('}');
  15860. }
  15861. return;
  15862. }
  15863. case value_t::array:
  15864. {
  15865. if (val.m_data.m_value.array->empty())
  15866. {
  15867. o->write_characters("[]", 2);
  15868. return;
  15869. }
  15870. if (pretty_print)
  15871. {
  15872. o->write_characters("[\n", 2);
  15873. // variable to hold indentation for recursive calls
  15874. const auto new_indent = current_indent + indent_step;
  15875. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15876. {
  15877. indent_string.resize(indent_string.size() * 2, ' ');
  15878. }
  15879. // first n-1 elements
  15880. for (auto i = val.m_data.m_value.array->cbegin();
  15881. i != val.m_data.m_value.array->cend() - 1; ++i)
  15882. {
  15883. o->write_characters(indent_string.c_str(), new_indent);
  15884. dump(*i, true, ensure_ascii, indent_step, new_indent);
  15885. o->write_characters(",\n", 2);
  15886. }
  15887. // last element
  15888. JSON_ASSERT(!val.m_data.m_value.array->empty());
  15889. o->write_characters(indent_string.c_str(), new_indent);
  15890. dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  15891. o->write_character('\n');
  15892. o->write_characters(indent_string.c_str(), current_indent);
  15893. o->write_character(']');
  15894. }
  15895. else
  15896. {
  15897. o->write_character('[');
  15898. // first n-1 elements
  15899. for (auto i = val.m_data.m_value.array->cbegin();
  15900. i != val.m_data.m_value.array->cend() - 1; ++i)
  15901. {
  15902. dump(*i, false, ensure_ascii, indent_step, current_indent);
  15903. o->write_character(',');
  15904. }
  15905. // last element
  15906. JSON_ASSERT(!val.m_data.m_value.array->empty());
  15907. dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  15908. o->write_character(']');
  15909. }
  15910. return;
  15911. }
  15912. case value_t::string:
  15913. {
  15914. o->write_character('\"');
  15915. dump_escaped(*val.m_data.m_value.string, ensure_ascii);
  15916. o->write_character('\"');
  15917. return;
  15918. }
  15919. case value_t::binary:
  15920. {
  15921. if (pretty_print)
  15922. {
  15923. o->write_characters("{\n", 2);
  15924. // variable to hold indentation for recursive calls
  15925. const auto new_indent = current_indent + indent_step;
  15926. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15927. {
  15928. indent_string.resize(indent_string.size() * 2, ' ');
  15929. }
  15930. o->write_characters(indent_string.c_str(), new_indent);
  15931. o->write_characters("\"bytes\": [", 10);
  15932. if (!val.m_data.m_value.binary->empty())
  15933. {
  15934. for (auto i = val.m_data.m_value.binary->cbegin();
  15935. i != val.m_data.m_value.binary->cend() - 1; ++i)
  15936. {
  15937. dump_integer(*i);
  15938. o->write_characters(", ", 2);
  15939. }
  15940. dump_integer(val.m_data.m_value.binary->back());
  15941. }
  15942. o->write_characters("],\n", 3);
  15943. o->write_characters(indent_string.c_str(), new_indent);
  15944. o->write_characters("\"subtype\": ", 11);
  15945. if (val.m_data.m_value.binary->has_subtype())
  15946. {
  15947. dump_integer(val.m_data.m_value.binary->subtype());
  15948. }
  15949. else
  15950. {
  15951. o->write_characters("null", 4);
  15952. }
  15953. o->write_character('\n');
  15954. o->write_characters(indent_string.c_str(), current_indent);
  15955. o->write_character('}');
  15956. }
  15957. else
  15958. {
  15959. o->write_characters("{\"bytes\":[", 10);
  15960. if (!val.m_data.m_value.binary->empty())
  15961. {
  15962. for (auto i = val.m_data.m_value.binary->cbegin();
  15963. i != val.m_data.m_value.binary->cend() - 1; ++i)
  15964. {
  15965. dump_integer(*i);
  15966. o->write_character(',');
  15967. }
  15968. dump_integer(val.m_data.m_value.binary->back());
  15969. }
  15970. o->write_characters("],\"subtype\":", 12);
  15971. if (val.m_data.m_value.binary->has_subtype())
  15972. {
  15973. dump_integer(val.m_data.m_value.binary->subtype());
  15974. o->write_character('}');
  15975. }
  15976. else
  15977. {
  15978. o->write_characters("null}", 5);
  15979. }
  15980. }
  15981. return;
  15982. }
  15983. case value_t::boolean:
  15984. {
  15985. if (val.m_data.m_value.boolean)
  15986. {
  15987. o->write_characters("true", 4);
  15988. }
  15989. else
  15990. {
  15991. o->write_characters("false", 5);
  15992. }
  15993. return;
  15994. }
  15995. case value_t::number_integer:
  15996. {
  15997. dump_integer(val.m_data.m_value.number_integer);
  15998. return;
  15999. }
  16000. case value_t::number_unsigned:
  16001. {
  16002. dump_integer(val.m_data.m_value.number_unsigned);
  16003. return;
  16004. }
  16005. case value_t::number_float:
  16006. {
  16007. dump_float(val.m_data.m_value.number_float);
  16008. return;
  16009. }
  16010. case value_t::discarded:
  16011. {
  16012. o->write_characters("<discarded>", 11);
  16013. return;
  16014. }
  16015. case value_t::null:
  16016. {
  16017. o->write_characters("null", 4);
  16018. return;
  16019. }
  16020. default: // LCOV_EXCL_LINE
  16021. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16022. }
  16023. }
  16024. JSON_PRIVATE_UNLESS_TESTED:
  16025. /*!
  16026. @brief dump escaped string
  16027. Escape a string by replacing certain special characters by a sequence of an
  16028. escape character (backslash) and another character and other control
  16029. characters by a sequence of "\u" followed by a four-digit hex
  16030. representation. The escaped string is written to output stream @a o.
  16031. @param[in] s the string to escape
  16032. @param[in] ensure_ascii whether to escape non-ASCII characters with
  16033. \uXXXX sequences
  16034. @complexity Linear in the length of string @a s.
  16035. */
  16036. void dump_escaped(const string_t& s, const bool ensure_ascii)
  16037. {
  16038. std::uint32_t codepoint{};
  16039. std::uint8_t state = UTF8_ACCEPT;
  16040. std::size_t bytes = 0; // number of bytes written to string_buffer
  16041. // number of bytes written at the point of the last valid byte
  16042. std::size_t bytes_after_last_accept = 0;
  16043. std::size_t undumped_chars = 0;
  16044. for (std::size_t i = 0; i < s.size(); ++i)
  16045. {
  16046. const auto byte = static_cast<std::uint8_t>(s[i]);
  16047. switch (decode(state, codepoint, byte))
  16048. {
  16049. case UTF8_ACCEPT: // decode found a new code point
  16050. {
  16051. switch (codepoint)
  16052. {
  16053. case 0x08: // backspace
  16054. {
  16055. string_buffer[bytes++] = '\\';
  16056. string_buffer[bytes++] = 'b';
  16057. break;
  16058. }
  16059. case 0x09: // horizontal tab
  16060. {
  16061. string_buffer[bytes++] = '\\';
  16062. string_buffer[bytes++] = 't';
  16063. break;
  16064. }
  16065. case 0x0A: // newline
  16066. {
  16067. string_buffer[bytes++] = '\\';
  16068. string_buffer[bytes++] = 'n';
  16069. break;
  16070. }
  16071. case 0x0C: // formfeed
  16072. {
  16073. string_buffer[bytes++] = '\\';
  16074. string_buffer[bytes++] = 'f';
  16075. break;
  16076. }
  16077. case 0x0D: // carriage return
  16078. {
  16079. string_buffer[bytes++] = '\\';
  16080. string_buffer[bytes++] = 'r';
  16081. break;
  16082. }
  16083. case 0x22: // quotation mark
  16084. {
  16085. string_buffer[bytes++] = '\\';
  16086. string_buffer[bytes++] = '\"';
  16087. break;
  16088. }
  16089. case 0x5C: // reverse solidus
  16090. {
  16091. string_buffer[bytes++] = '\\';
  16092. string_buffer[bytes++] = '\\';
  16093. break;
  16094. }
  16095. default:
  16096. {
  16097. // escape control characters (0x00..0x1F) or, if
  16098. // ensure_ascii parameter is used, non-ASCII characters
  16099. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  16100. {
  16101. if (codepoint <= 0xFFFF)
  16102. {
  16103. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16104. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  16105. static_cast<std::uint16_t>(codepoint)));
  16106. bytes += 6;
  16107. }
  16108. else
  16109. {
  16110. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16111. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16112. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16113. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16114. bytes += 12;
  16115. }
  16116. }
  16117. else
  16118. {
  16119. // copy byte to buffer (all previous bytes
  16120. // been copied have in default case above)
  16121. string_buffer[bytes++] = s[i];
  16122. }
  16123. break;
  16124. }
  16125. }
  16126. // write buffer and reset index; there must be 13 bytes
  16127. // left, as this is the maximal number of bytes to be
  16128. // written ("\uxxxx\uxxxx\0") for one code point
  16129. if (string_buffer.size() - bytes < 13)
  16130. {
  16131. o->write_characters(string_buffer.data(), bytes);
  16132. bytes = 0;
  16133. }
  16134. // remember the byte position of this accept
  16135. bytes_after_last_accept = bytes;
  16136. undumped_chars = 0;
  16137. break;
  16138. }
  16139. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16140. {
  16141. switch (error_handler)
  16142. {
  16143. case error_handler_t::strict:
  16144. {
  16145. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16146. }
  16147. case error_handler_t::ignore:
  16148. case error_handler_t::replace:
  16149. {
  16150. // in case we saw this character the first time, we
  16151. // would like to read it again, because the byte
  16152. // may be OK for itself, but just not OK for the
  16153. // previous sequence
  16154. if (undumped_chars > 0)
  16155. {
  16156. --i;
  16157. }
  16158. // reset length buffer to the last accepted index;
  16159. // thus removing/ignoring the invalid characters
  16160. bytes = bytes_after_last_accept;
  16161. if (error_handler == error_handler_t::replace)
  16162. {
  16163. // add a replacement character
  16164. if (ensure_ascii)
  16165. {
  16166. string_buffer[bytes++] = '\\';
  16167. string_buffer[bytes++] = 'u';
  16168. string_buffer[bytes++] = 'f';
  16169. string_buffer[bytes++] = 'f';
  16170. string_buffer[bytes++] = 'f';
  16171. string_buffer[bytes++] = 'd';
  16172. }
  16173. else
  16174. {
  16175. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16176. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16177. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16178. }
  16179. // write buffer and reset index; there must be 13 bytes
  16180. // left, as this is the maximal number of bytes to be
  16181. // written ("\uxxxx\uxxxx\0") for one code point
  16182. if (string_buffer.size() - bytes < 13)
  16183. {
  16184. o->write_characters(string_buffer.data(), bytes);
  16185. bytes = 0;
  16186. }
  16187. bytes_after_last_accept = bytes;
  16188. }
  16189. undumped_chars = 0;
  16190. // continue processing the string
  16191. state = UTF8_ACCEPT;
  16192. break;
  16193. }
  16194. default: // LCOV_EXCL_LINE
  16195. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16196. }
  16197. break;
  16198. }
  16199. default: // decode found yet incomplete multi-byte code point
  16200. {
  16201. if (!ensure_ascii)
  16202. {
  16203. // code point will not be escaped - copy byte to buffer
  16204. string_buffer[bytes++] = s[i];
  16205. }
  16206. ++undumped_chars;
  16207. break;
  16208. }
  16209. }
  16210. }
  16211. // we finished processing the string
  16212. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  16213. {
  16214. // write buffer
  16215. if (bytes > 0)
  16216. {
  16217. o->write_characters(string_buffer.data(), bytes);
  16218. }
  16219. }
  16220. else
  16221. {
  16222. // we finish reading, but do not accept: string was incomplete
  16223. switch (error_handler)
  16224. {
  16225. case error_handler_t::strict:
  16226. {
  16227. 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));
  16228. }
  16229. case error_handler_t::ignore:
  16230. {
  16231. // write all accepted bytes
  16232. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16233. break;
  16234. }
  16235. case error_handler_t::replace:
  16236. {
  16237. // write all accepted bytes
  16238. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16239. // add a replacement character
  16240. if (ensure_ascii)
  16241. {
  16242. o->write_characters("\\ufffd", 6);
  16243. }
  16244. else
  16245. {
  16246. o->write_characters("\xEF\xBF\xBD", 3);
  16247. }
  16248. break;
  16249. }
  16250. default: // LCOV_EXCL_LINE
  16251. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16252. }
  16253. }
  16254. }
  16255. private:
  16256. /*!
  16257. @brief count digits
  16258. Count the number of decimal (base 10) digits for an input unsigned integer.
  16259. @param[in] x unsigned integer number to count its digits
  16260. @return number of decimal digits
  16261. */
  16262. inline unsigned int count_digits(number_unsigned_t x) noexcept
  16263. {
  16264. unsigned int n_digits = 1;
  16265. for (;;)
  16266. {
  16267. if (x < 10)
  16268. {
  16269. return n_digits;
  16270. }
  16271. if (x < 100)
  16272. {
  16273. return n_digits + 1;
  16274. }
  16275. if (x < 1000)
  16276. {
  16277. return n_digits + 2;
  16278. }
  16279. if (x < 10000)
  16280. {
  16281. return n_digits + 3;
  16282. }
  16283. x = x / 10000u;
  16284. n_digits += 4;
  16285. }
  16286. }
  16287. /*!
  16288. * @brief convert a byte to a uppercase hex representation
  16289. * @param[in] byte byte to represent
  16290. * @return representation ("00".."FF")
  16291. */
  16292. static std::string hex_bytes(std::uint8_t byte)
  16293. {
  16294. std::string result = "FF";
  16295. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  16296. result[0] = nibble_to_hex[byte / 16];
  16297. result[1] = nibble_to_hex[byte % 16];
  16298. return result;
  16299. }
  16300. // templates to avoid warnings about useless casts
  16301. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  16302. bool is_negative_number(NumberType x)
  16303. {
  16304. return x < 0;
  16305. }
  16306. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  16307. bool is_negative_number(NumberType /*unused*/)
  16308. {
  16309. return false;
  16310. }
  16311. /*!
  16312. @brief dump an integer
  16313. Dump a given integer to output stream @a o. Works internally with
  16314. @a number_buffer.
  16315. @param[in] x integer number (signed or unsigned) to dump
  16316. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  16317. */
  16318. template < typename NumberType, detail::enable_if_t <
  16319. std::is_integral<NumberType>::value ||
  16320. std::is_same<NumberType, number_unsigned_t>::value ||
  16321. std::is_same<NumberType, number_integer_t>::value ||
  16322. std::is_same<NumberType, binary_char_t>::value,
  16323. int > = 0 >
  16324. void dump_integer(NumberType x)
  16325. {
  16326. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  16327. {
  16328. {
  16329. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  16330. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  16331. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  16332. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  16333. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  16334. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  16335. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  16336. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  16337. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  16338. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  16339. }
  16340. };
  16341. // special case for "0"
  16342. if (x == 0)
  16343. {
  16344. o->write_character('0');
  16345. return;
  16346. }
  16347. // use a pointer to fill the buffer
  16348. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16349. number_unsigned_t abs_value;
  16350. unsigned int n_chars{};
  16351. if (is_negative_number(x))
  16352. {
  16353. *buffer_ptr = '-';
  16354. abs_value = remove_sign(static_cast<number_integer_t>(x));
  16355. // account one more byte for the minus sign
  16356. n_chars = 1 + count_digits(abs_value);
  16357. }
  16358. else
  16359. {
  16360. abs_value = static_cast<number_unsigned_t>(x);
  16361. n_chars = count_digits(abs_value);
  16362. }
  16363. // spare 1 byte for '\0'
  16364. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  16365. // jump to the end to generate the string from backward,
  16366. // so we later avoid reversing the result
  16367. buffer_ptr += n_chars;
  16368. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  16369. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  16370. while (abs_value >= 100)
  16371. {
  16372. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  16373. abs_value /= 100;
  16374. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16375. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16376. }
  16377. if (abs_value >= 10)
  16378. {
  16379. const auto digits_index = static_cast<unsigned>(abs_value);
  16380. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16381. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16382. }
  16383. else
  16384. {
  16385. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  16386. }
  16387. o->write_characters(number_buffer.data(), n_chars);
  16388. }
  16389. /*!
  16390. @brief dump a floating-point number
  16391. Dump a given floating-point number to output stream @a o. Works internally
  16392. with @a number_buffer.
  16393. @param[in] x floating-point number to dump
  16394. */
  16395. void dump_float(number_float_t x)
  16396. {
  16397. // NaN / inf
  16398. if (!std::isfinite(x))
  16399. {
  16400. o->write_characters("null", 4);
  16401. return;
  16402. }
  16403. // If number_float_t is an IEEE-754 single or double precision number,
  16404. // use the Grisu2 algorithm to produce short numbers which are
  16405. // guaranteed to round-trip, using strtof and strtod, resp.
  16406. //
  16407. // NB: The test below works if <long double> == <double>.
  16408. static constexpr bool is_ieee_single_or_double
  16409. = (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) ||
  16410. (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);
  16411. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  16412. }
  16413. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  16414. {
  16415. auto* begin = number_buffer.data();
  16416. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  16417. o->write_characters(begin, static_cast<size_t>(end - begin));
  16418. }
  16419. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  16420. {
  16421. // get number of digits for a float -> text -> float round-trip
  16422. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  16423. // the actual conversion
  16424. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16425. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  16426. // negative value indicates an error
  16427. JSON_ASSERT(len > 0);
  16428. // check if buffer was large enough
  16429. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  16430. // erase thousands separator
  16431. if (thousands_sep != '\0')
  16432. {
  16433. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16434. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  16435. std::fill(end, number_buffer.end(), '\0');
  16436. JSON_ASSERT((end - number_buffer.begin()) <= len);
  16437. len = (end - number_buffer.begin());
  16438. }
  16439. // convert decimal point to '.'
  16440. if (decimal_point != '\0' && decimal_point != '.')
  16441. {
  16442. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16443. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  16444. if (dec_pos != number_buffer.end())
  16445. {
  16446. *dec_pos = '.';
  16447. }
  16448. }
  16449. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  16450. // determine if we need to append ".0"
  16451. const bool value_is_int_like =
  16452. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  16453. [](char c)
  16454. {
  16455. return c == '.' || c == 'e';
  16456. });
  16457. if (value_is_int_like)
  16458. {
  16459. o->write_characters(".0", 2);
  16460. }
  16461. }
  16462. /*!
  16463. @brief check whether a string is UTF-8 encoded
  16464. The function checks each byte of a string whether it is UTF-8 encoded. The
  16465. result of the check is stored in the @a state parameter. The function must
  16466. be called initially with state 0 (accept). State 1 means the string must
  16467. be rejected, because the current byte is not allowed. If the string is
  16468. completely processed, but the state is non-zero, the string ended
  16469. prematurely; that is, the last byte indicated more bytes should have
  16470. followed.
  16471. @param[in,out] state the state of the decoding
  16472. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  16473. @param[in] byte next byte to decode
  16474. @return new state
  16475. @note The function has been edited: a std::array is used.
  16476. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  16477. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  16478. */
  16479. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  16480. {
  16481. static const std::array<std::uint8_t, 400> utf8d =
  16482. {
  16483. {
  16484. 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
  16485. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3F
  16486. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5F
  16487. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7F
  16488. 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
  16489. 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
  16490. 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
  16491. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  16492. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  16493. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  16494. 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
  16495. 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
  16496. 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
  16497. 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
  16498. }
  16499. };
  16500. JSON_ASSERT(byte < utf8d.size());
  16501. const std::uint8_t type = utf8d[byte];
  16502. codep = (state != UTF8_ACCEPT)
  16503. ? (byte & 0x3fu) | (codep << 6u)
  16504. : (0xFFu >> type) & (byte);
  16505. const std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  16506. JSON_ASSERT(index < utf8d.size());
  16507. state = utf8d[index];
  16508. return state;
  16509. }
  16510. /*
  16511. * Overload to make the compiler happy while it is instantiating
  16512. * dump_integer for number_unsigned_t.
  16513. * Must never be called.
  16514. */
  16515. number_unsigned_t remove_sign(number_unsigned_t x)
  16516. {
  16517. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16518. return x; // LCOV_EXCL_LINE
  16519. }
  16520. /*
  16521. * Helper function for dump_integer
  16522. *
  16523. * This function takes a negative signed integer and returns its absolute
  16524. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  16525. * not directly remove the sign of an arbitrary signed integer as the
  16526. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  16527. * #1708 for details.
  16528. */
  16529. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  16530. {
  16531. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  16532. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  16533. }
  16534. private:
  16535. /// the output of the serializer
  16536. output_adapter_t<char> o = nullptr;
  16537. /// a (hopefully) large enough character buffer
  16538. std::array<char, 64> number_buffer{{}};
  16539. /// the locale
  16540. const std::lconv* loc = nullptr;
  16541. /// the locale's thousand separator character
  16542. const char thousands_sep = '\0';
  16543. /// the locale's decimal point character
  16544. const char decimal_point = '\0';
  16545. /// string buffer
  16546. std::array<char, 512> string_buffer{{}};
  16547. /// the indentation character
  16548. const char indent_char;
  16549. /// the indentation string
  16550. string_t indent_string;
  16551. /// error_handler how to react on decoding errors
  16552. const error_handler_t error_handler;
  16553. };
  16554. } // namespace detail
  16555. NLOHMANN_JSON_NAMESPACE_END
  16556. // #include <nlohmann/detail/value_t.hpp>
  16557. // #include <nlohmann/json_fwd.hpp>
  16558. // #include <nlohmann/ordered_map.hpp>
  16559. // __ _____ _____ _____
  16560. // __| | __| | | | JSON for Modern C++
  16561. // | | |__ | | | | | | version 3.11.3
  16562. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  16563. //
  16564. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  16565. // SPDX-License-Identifier: MIT
  16566. #include <functional> // equal_to, less
  16567. #include <initializer_list> // initializer_list
  16568. #include <iterator> // input_iterator_tag, iterator_traits
  16569. #include <memory> // allocator
  16570. #include <stdexcept> // for out_of_range
  16571. #include <type_traits> // enable_if, is_convertible
  16572. #include <utility> // pair
  16573. #include <vector> // vector
  16574. // #include <nlohmann/detail/macro_scope.hpp>
  16575. // #include <nlohmann/detail/meta/type_traits.hpp>
  16576. NLOHMANN_JSON_NAMESPACE_BEGIN
  16577. /// ordered_map: a minimal map-like container that preserves insertion order
  16578. /// for use within nlohmann::basic_json<ordered_map>
  16579. template <class Key, class T, class IgnoredLess = std::less<Key>,
  16580. class Allocator = std::allocator<std::pair<const Key, T>>>
  16581. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  16582. {
  16583. using key_type = Key;
  16584. using mapped_type = T;
  16585. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  16586. using iterator = typename Container::iterator;
  16587. using const_iterator = typename Container::const_iterator;
  16588. using size_type = typename Container::size_type;
  16589. using value_type = typename Container::value_type;
  16590. #ifdef JSON_HAS_CPP_14
  16591. using key_compare = std::equal_to<>;
  16592. #else
  16593. using key_compare = std::equal_to<Key>;
  16594. #endif
  16595. // Explicit constructors instead of `using Container::Container`
  16596. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  16597. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  16598. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  16599. template <class It>
  16600. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  16601. : Container{first, last, alloc} {}
  16602. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  16603. : Container{init, alloc} {}
  16604. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  16605. {
  16606. for (auto it = this->begin(); it != this->end(); ++it)
  16607. {
  16608. if (m_compare(it->first, key))
  16609. {
  16610. return {it, false};
  16611. }
  16612. }
  16613. Container::emplace_back(key, std::forward<T>(t));
  16614. return {std::prev(this->end()), true};
  16615. }
  16616. template<class KeyType, detail::enable_if_t<
  16617. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16618. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  16619. {
  16620. for (auto it = this->begin(); it != this->end(); ++it)
  16621. {
  16622. if (m_compare(it->first, key))
  16623. {
  16624. return {it, false};
  16625. }
  16626. }
  16627. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  16628. return {std::prev(this->end()), true};
  16629. }
  16630. T& operator[](const key_type& key)
  16631. {
  16632. return emplace(key, T{}).first->second;
  16633. }
  16634. template<class KeyType, detail::enable_if_t<
  16635. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16636. T & operator[](KeyType && key)
  16637. {
  16638. return emplace(std::forward<KeyType>(key), T{}).first->second;
  16639. }
  16640. const T& operator[](const key_type& key) const
  16641. {
  16642. return at(key);
  16643. }
  16644. template<class KeyType, detail::enable_if_t<
  16645. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16646. const T & operator[](KeyType && key) const
  16647. {
  16648. return at(std::forward<KeyType>(key));
  16649. }
  16650. T& at(const key_type& key)
  16651. {
  16652. for (auto it = this->begin(); it != this->end(); ++it)
  16653. {
  16654. if (m_compare(it->first, key))
  16655. {
  16656. return it->second;
  16657. }
  16658. }
  16659. JSON_THROW(std::out_of_range("key not found"));
  16660. }
  16661. template<class KeyType, detail::enable_if_t<
  16662. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16663. T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16664. {
  16665. for (auto it = this->begin(); it != this->end(); ++it)
  16666. {
  16667. if (m_compare(it->first, key))
  16668. {
  16669. return it->second;
  16670. }
  16671. }
  16672. JSON_THROW(std::out_of_range("key not found"));
  16673. }
  16674. const T& at(const key_type& key) const
  16675. {
  16676. for (auto it = this->begin(); it != this->end(); ++it)
  16677. {
  16678. if (m_compare(it->first, key))
  16679. {
  16680. return it->second;
  16681. }
  16682. }
  16683. JSON_THROW(std::out_of_range("key not found"));
  16684. }
  16685. template<class KeyType, detail::enable_if_t<
  16686. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16687. const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  16688. {
  16689. for (auto it = this->begin(); it != this->end(); ++it)
  16690. {
  16691. if (m_compare(it->first, key))
  16692. {
  16693. return it->second;
  16694. }
  16695. }
  16696. JSON_THROW(std::out_of_range("key not found"));
  16697. }
  16698. size_type erase(const key_type& key)
  16699. {
  16700. for (auto it = this->begin(); it != this->end(); ++it)
  16701. {
  16702. if (m_compare(it->first, key))
  16703. {
  16704. // Since we cannot move const Keys, re-construct them in place
  16705. for (auto next = it; ++next != this->end(); ++it)
  16706. {
  16707. it->~value_type(); // Destroy but keep allocation
  16708. new (&*it) value_type{std::move(*next)};
  16709. }
  16710. Container::pop_back();
  16711. return 1;
  16712. }
  16713. }
  16714. return 0;
  16715. }
  16716. template<class KeyType, detail::enable_if_t<
  16717. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16718. size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16719. {
  16720. for (auto it = this->begin(); it != this->end(); ++it)
  16721. {
  16722. if (m_compare(it->first, key))
  16723. {
  16724. // Since we cannot move const Keys, re-construct them in place
  16725. for (auto next = it; ++next != this->end(); ++it)
  16726. {
  16727. it->~value_type(); // Destroy but keep allocation
  16728. new (&*it) value_type{std::move(*next)};
  16729. }
  16730. Container::pop_back();
  16731. return 1;
  16732. }
  16733. }
  16734. return 0;
  16735. }
  16736. iterator erase(iterator pos)
  16737. {
  16738. return erase(pos, std::next(pos));
  16739. }
  16740. iterator erase(iterator first, iterator last)
  16741. {
  16742. if (first == last)
  16743. {
  16744. return first;
  16745. }
  16746. const auto elements_affected = std::distance(first, last);
  16747. const auto offset = std::distance(Container::begin(), first);
  16748. // This is the start situation. We need to delete elements_affected
  16749. // elements (3 in this example: e, f, g), and need to return an
  16750. // iterator past the last deleted element (h in this example).
  16751. // Note that offset is the distance from the start of the vector
  16752. // to first. We will need this later.
  16753. // [ a, b, c, d, e, f, g, h, i, j ]
  16754. // ^ ^
  16755. // first last
  16756. // Since we cannot move const Keys, we re-construct them in place.
  16757. // We start at first and re-construct (viz. copy) the elements from
  16758. // the back of the vector. Example for first iteration:
  16759. // ,--------.
  16760. // v | destroy e and re-construct with h
  16761. // [ a, b, c, d, e, f, g, h, i, j ]
  16762. // ^ ^
  16763. // it it + elements_affected
  16764. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  16765. {
  16766. it->~value_type(); // destroy but keep allocation
  16767. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  16768. }
  16769. // [ a, b, c, d, h, i, j, h, i, j ]
  16770. // ^ ^
  16771. // first last
  16772. // remove the unneeded elements at the end of the vector
  16773. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  16774. // [ a, b, c, d, h, i, j ]
  16775. // ^ ^
  16776. // first last
  16777. // first is now pointing past the last deleted element, but we cannot
  16778. // use this iterator, because it may have been invalidated by the
  16779. // resize call. Instead, we can return begin() + offset.
  16780. return Container::begin() + offset;
  16781. }
  16782. size_type count(const key_type& key) const
  16783. {
  16784. for (auto it = this->begin(); it != this->end(); ++it)
  16785. {
  16786. if (m_compare(it->first, key))
  16787. {
  16788. return 1;
  16789. }
  16790. }
  16791. return 0;
  16792. }
  16793. template<class KeyType, detail::enable_if_t<
  16794. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16795. size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  16796. {
  16797. for (auto it = this->begin(); it != this->end(); ++it)
  16798. {
  16799. if (m_compare(it->first, key))
  16800. {
  16801. return 1;
  16802. }
  16803. }
  16804. return 0;
  16805. }
  16806. iterator find(const key_type& key)
  16807. {
  16808. for (auto it = this->begin(); it != this->end(); ++it)
  16809. {
  16810. if (m_compare(it->first, key))
  16811. {
  16812. return it;
  16813. }
  16814. }
  16815. return Container::end();
  16816. }
  16817. template<class KeyType, detail::enable_if_t<
  16818. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16819. iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16820. {
  16821. for (auto it = this->begin(); it != this->end(); ++it)
  16822. {
  16823. if (m_compare(it->first, key))
  16824. {
  16825. return it;
  16826. }
  16827. }
  16828. return Container::end();
  16829. }
  16830. const_iterator find(const key_type& key) const
  16831. {
  16832. for (auto it = this->begin(); it != this->end(); ++it)
  16833. {
  16834. if (m_compare(it->first, key))
  16835. {
  16836. return it;
  16837. }
  16838. }
  16839. return Container::end();
  16840. }
  16841. std::pair<iterator, bool> insert( value_type&& value )
  16842. {
  16843. return emplace(value.first, std::move(value.second));
  16844. }
  16845. std::pair<iterator, bool> insert( const value_type& value )
  16846. {
  16847. for (auto it = this->begin(); it != this->end(); ++it)
  16848. {
  16849. if (m_compare(it->first, value.first))
  16850. {
  16851. return {it, false};
  16852. }
  16853. }
  16854. Container::push_back(value);
  16855. return {--this->end(), true};
  16856. }
  16857. template<typename InputIt>
  16858. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  16859. std::input_iterator_tag>::value>::type;
  16860. template<typename InputIt, typename = require_input_iter<InputIt>>
  16861. void insert(InputIt first, InputIt last)
  16862. {
  16863. for (auto it = first; it != last; ++it)
  16864. {
  16865. insert(*it);
  16866. }
  16867. }
  16868. private:
  16869. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  16870. };
  16871. NLOHMANN_JSON_NAMESPACE_END
  16872. #if defined(JSON_HAS_CPP_17)
  16873. #if JSON_HAS_STATIC_RTTI
  16874. #include <any>
  16875. #endif
  16876. #include <string_view>
  16877. #endif
  16878. /*!
  16879. @brief namespace for Niels Lohmann
  16880. @see https://github.com/nlohmann
  16881. @since version 1.0.0
  16882. */
  16883. NLOHMANN_JSON_NAMESPACE_BEGIN
  16884. /*!
  16885. @brief a class to store JSON values
  16886. @internal
  16887. @invariant The member variables @a m_value and @a m_type have the following
  16888. relationship:
  16889. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  16890. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  16891. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  16892. The invariants are checked by member function assert_invariant().
  16893. @note ObjectType trick from https://stackoverflow.com/a/9860911
  16894. @endinternal
  16895. @since version 1.0.0
  16896. @nosubgrouping
  16897. */
  16898. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  16899. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  16900. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  16901. {
  16902. private:
  16903. template<detail::value_t> friend struct detail::external_constructor;
  16904. template<typename>
  16905. friend class ::nlohmann::json_pointer;
  16906. // can be restored when json_pointer backwards compatibility is removed
  16907. // friend ::nlohmann::json_pointer<StringType>;
  16908. template<typename BasicJsonType, typename InputType>
  16909. friend class ::nlohmann::detail::parser;
  16910. friend ::nlohmann::detail::serializer<basic_json>;
  16911. template<typename BasicJsonType>
  16912. friend class ::nlohmann::detail::iter_impl;
  16913. template<typename BasicJsonType, typename CharType>
  16914. friend class ::nlohmann::detail::binary_writer;
  16915. template<typename BasicJsonType, typename InputType, typename SAX>
  16916. friend class ::nlohmann::detail::binary_reader;
  16917. template<typename BasicJsonType>
  16918. friend class ::nlohmann::detail::json_sax_dom_parser;
  16919. template<typename BasicJsonType>
  16920. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  16921. friend class ::nlohmann::detail::exception;
  16922. /// workaround type for MSVC
  16923. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  16924. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  16925. JSON_PRIVATE_UNLESS_TESTED:
  16926. // convenience aliases for types residing in namespace detail;
  16927. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  16928. template<typename InputAdapterType>
  16929. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  16930. InputAdapterType adapter,
  16931. detail::parser_callback_t<basic_json>cb = nullptr,
  16932. const bool allow_exceptions = true,
  16933. const bool ignore_comments = false
  16934. )
  16935. {
  16936. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  16937. std::move(cb), allow_exceptions, ignore_comments);
  16938. }
  16939. private:
  16940. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  16941. template<typename BasicJsonType>
  16942. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  16943. template<typename BasicJsonType>
  16944. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  16945. template<typename Iterator>
  16946. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  16947. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  16948. template<typename CharType>
  16949. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  16950. template<typename InputType>
  16951. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  16952. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  16953. JSON_PRIVATE_UNLESS_TESTED:
  16954. using serializer = ::nlohmann::detail::serializer<basic_json>;
  16955. public:
  16956. using value_t = detail::value_t;
  16957. /// JSON Pointer, see @ref nlohmann::json_pointer
  16958. using json_pointer = ::nlohmann::json_pointer<StringType>;
  16959. template<typename T, typename SFINAE>
  16960. using json_serializer = JSONSerializer<T, SFINAE>;
  16961. /// how to treat decoding errors
  16962. using error_handler_t = detail::error_handler_t;
  16963. /// how to treat CBOR tags
  16964. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  16965. /// helper type for initializer lists of basic_json values
  16966. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  16967. using input_format_t = detail::input_format_t;
  16968. /// SAX interface type, see @ref nlohmann::json_sax
  16969. using json_sax_t = json_sax<basic_json>;
  16970. ////////////////
  16971. // exceptions //
  16972. ////////////////
  16973. /// @name exceptions
  16974. /// Classes to implement user-defined exceptions.
  16975. /// @{
  16976. using exception = detail::exception;
  16977. using parse_error = detail::parse_error;
  16978. using invalid_iterator = detail::invalid_iterator;
  16979. using type_error = detail::type_error;
  16980. using out_of_range = detail::out_of_range;
  16981. using other_error = detail::other_error;
  16982. /// @}
  16983. /////////////////////
  16984. // container types //
  16985. /////////////////////
  16986. /// @name container types
  16987. /// The canonic container types to use @ref basic_json like any other STL
  16988. /// container.
  16989. /// @{
  16990. /// the type of elements in a basic_json container
  16991. using value_type = basic_json;
  16992. /// the type of an element reference
  16993. using reference = value_type&;
  16994. /// the type of an element const reference
  16995. using const_reference = const value_type&;
  16996. /// a type to represent differences between iterators
  16997. using difference_type = std::ptrdiff_t;
  16998. /// a type to represent container sizes
  16999. using size_type = std::size_t;
  17000. /// the allocator type
  17001. using allocator_type = AllocatorType<basic_json>;
  17002. /// the type of an element pointer
  17003. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  17004. /// the type of an element const pointer
  17005. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  17006. /// an iterator for a basic_json container
  17007. using iterator = iter_impl<basic_json>;
  17008. /// a const iterator for a basic_json container
  17009. using const_iterator = iter_impl<const basic_json>;
  17010. /// a reverse iterator for a basic_json container
  17011. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  17012. /// a const reverse iterator for a basic_json container
  17013. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  17014. /// @}
  17015. /// @brief returns the allocator associated with the container
  17016. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  17017. static allocator_type get_allocator()
  17018. {
  17019. return allocator_type();
  17020. }
  17021. /// @brief returns version information on the library
  17022. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  17023. JSON_HEDLEY_WARN_UNUSED_RESULT
  17024. static basic_json meta()
  17025. {
  17026. basic_json result;
  17027. result["copyright"] = "(C) 2013-2023 Niels Lohmann";
  17028. result["name"] = "JSON for Modern C++";
  17029. result["url"] = "https://github.com/nlohmann/json";
  17030. result["version"]["string"] =
  17031. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  17032. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  17033. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  17034. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  17035. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  17036. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  17037. #ifdef _WIN32
  17038. result["platform"] = "win32";
  17039. #elif defined __linux__
  17040. result["platform"] = "linux";
  17041. #elif defined __APPLE__
  17042. result["platform"] = "apple";
  17043. #elif defined __unix__
  17044. result["platform"] = "unix";
  17045. #else
  17046. result["platform"] = "unknown";
  17047. #endif
  17048. #if defined(__ICC) || defined(__INTEL_COMPILER)
  17049. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  17050. #elif defined(__clang__)
  17051. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  17052. #elif defined(__GNUC__) || defined(__GNUG__)
  17053. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  17054. std::to_string(__GNUC__), '.',
  17055. std::to_string(__GNUC_MINOR__), '.',
  17056. std::to_string(__GNUC_PATCHLEVEL__))
  17057. }
  17058. };
  17059. #elif defined(__HP_cc) || defined(__HP_aCC)
  17060. result["compiler"] = "hp"
  17061. #elif defined(__IBMCPP__)
  17062. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  17063. #elif defined(_MSC_VER)
  17064. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  17065. #elif defined(__PGI)
  17066. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  17067. #elif defined(__SUNPRO_CC)
  17068. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  17069. #else
  17070. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  17071. #endif
  17072. #if defined(_MSVC_LANG)
  17073. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  17074. #elif defined(__cplusplus)
  17075. result["compiler"]["c++"] = std::to_string(__cplusplus);
  17076. #else
  17077. result["compiler"]["c++"] = "unknown";
  17078. #endif
  17079. return result;
  17080. }
  17081. ///////////////////////////
  17082. // JSON value data types //
  17083. ///////////////////////////
  17084. /// @name JSON value data types
  17085. /// The data types to store a JSON value. These types are derived from
  17086. /// the template arguments passed to class @ref basic_json.
  17087. /// @{
  17088. /// @brief default object key comparator type
  17089. /// The actual object key comparator type (@ref object_comparator_t) may be
  17090. /// different.
  17091. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  17092. #if defined(JSON_HAS_CPP_14)
  17093. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  17094. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  17095. using default_object_comparator_t = std::less<>;
  17096. #else
  17097. using default_object_comparator_t = std::less<StringType>;
  17098. #endif
  17099. /// @brief a type for an object
  17100. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  17101. using object_t = ObjectType<StringType,
  17102. basic_json,
  17103. default_object_comparator_t,
  17104. AllocatorType<std::pair<const StringType,
  17105. basic_json>>>;
  17106. /// @brief a type for an array
  17107. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  17108. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  17109. /// @brief a type for a string
  17110. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  17111. using string_t = StringType;
  17112. /// @brief a type for a boolean
  17113. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17114. using boolean_t = BooleanType;
  17115. /// @brief a type for a number (integer)
  17116. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17117. using number_integer_t = NumberIntegerType;
  17118. /// @brief a type for a number (unsigned)
  17119. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17120. using number_unsigned_t = NumberUnsignedType;
  17121. /// @brief a type for a number (floating-point)
  17122. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17123. using number_float_t = NumberFloatType;
  17124. /// @brief a type for a packed binary type
  17125. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17126. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17127. /// @brief object key comparator type
  17128. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17129. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17130. /// @}
  17131. private:
  17132. /// helper for exception-safe object creation
  17133. template<typename T, typename... Args>
  17134. JSON_HEDLEY_RETURNS_NON_NULL
  17135. static T* create(Args&& ... args)
  17136. {
  17137. AllocatorType<T> alloc;
  17138. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17139. auto deleter = [&](T * obj)
  17140. {
  17141. AllocatorTraits::deallocate(alloc, obj, 1);
  17142. };
  17143. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17144. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17145. JSON_ASSERT(obj != nullptr);
  17146. return obj.release();
  17147. }
  17148. ////////////////////////
  17149. // JSON value storage //
  17150. ////////////////////////
  17151. JSON_PRIVATE_UNLESS_TESTED:
  17152. /*!
  17153. @brief a JSON value
  17154. The actual storage for a JSON value of the @ref basic_json class. This
  17155. union combines the different storage types for the JSON value types
  17156. defined in @ref value_t.
  17157. JSON type | value_t type | used type
  17158. --------- | --------------- | ------------------------
  17159. object | object | pointer to @ref object_t
  17160. array | array | pointer to @ref array_t
  17161. string | string | pointer to @ref string_t
  17162. boolean | boolean | @ref boolean_t
  17163. number | number_integer | @ref number_integer_t
  17164. number | number_unsigned | @ref number_unsigned_t
  17165. number | number_float | @ref number_float_t
  17166. binary | binary | pointer to @ref binary_t
  17167. null | null | *no value is stored*
  17168. @note Variable-length types (objects, arrays, and strings) are stored as
  17169. pointers. The size of the union should not exceed 64 bits if the default
  17170. value types are used.
  17171. @since version 1.0.0
  17172. */
  17173. union json_value
  17174. {
  17175. /// object (stored with pointer to save storage)
  17176. object_t* object;
  17177. /// array (stored with pointer to save storage)
  17178. array_t* array;
  17179. /// string (stored with pointer to save storage)
  17180. string_t* string;
  17181. /// binary (stored with pointer to save storage)
  17182. binary_t* binary;
  17183. /// boolean
  17184. boolean_t boolean;
  17185. /// number (integer)
  17186. number_integer_t number_integer;
  17187. /// number (unsigned integer)
  17188. number_unsigned_t number_unsigned;
  17189. /// number (floating-point)
  17190. number_float_t number_float;
  17191. /// default constructor (for null values)
  17192. json_value() = default;
  17193. /// constructor for booleans
  17194. json_value(boolean_t v) noexcept : boolean(v) {}
  17195. /// constructor for numbers (integer)
  17196. json_value(number_integer_t v) noexcept : number_integer(v) {}
  17197. /// constructor for numbers (unsigned)
  17198. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  17199. /// constructor for numbers (floating-point)
  17200. json_value(number_float_t v) noexcept : number_float(v) {}
  17201. /// constructor for empty values of a given type
  17202. json_value(value_t t)
  17203. {
  17204. switch (t)
  17205. {
  17206. case value_t::object:
  17207. {
  17208. object = create<object_t>();
  17209. break;
  17210. }
  17211. case value_t::array:
  17212. {
  17213. array = create<array_t>();
  17214. break;
  17215. }
  17216. case value_t::string:
  17217. {
  17218. string = create<string_t>("");
  17219. break;
  17220. }
  17221. case value_t::binary:
  17222. {
  17223. binary = create<binary_t>();
  17224. break;
  17225. }
  17226. case value_t::boolean:
  17227. {
  17228. boolean = static_cast<boolean_t>(false);
  17229. break;
  17230. }
  17231. case value_t::number_integer:
  17232. {
  17233. number_integer = static_cast<number_integer_t>(0);
  17234. break;
  17235. }
  17236. case value_t::number_unsigned:
  17237. {
  17238. number_unsigned = static_cast<number_unsigned_t>(0);
  17239. break;
  17240. }
  17241. case value_t::number_float:
  17242. {
  17243. number_float = static_cast<number_float_t>(0.0);
  17244. break;
  17245. }
  17246. case value_t::null:
  17247. {
  17248. object = nullptr; // silence warning, see #821
  17249. break;
  17250. }
  17251. case value_t::discarded:
  17252. default:
  17253. {
  17254. object = nullptr; // silence warning, see #821
  17255. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  17256. {
  17257. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.11.3", nullptr)); // LCOV_EXCL_LINE
  17258. }
  17259. break;
  17260. }
  17261. }
  17262. }
  17263. /// constructor for strings
  17264. json_value(const string_t& value) : string(create<string_t>(value)) {}
  17265. /// constructor for rvalue strings
  17266. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  17267. /// constructor for objects
  17268. json_value(const object_t& value) : object(create<object_t>(value)) {}
  17269. /// constructor for rvalue objects
  17270. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  17271. /// constructor for arrays
  17272. json_value(const array_t& value) : array(create<array_t>(value)) {}
  17273. /// constructor for rvalue arrays
  17274. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  17275. /// constructor for binary arrays
  17276. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  17277. /// constructor for rvalue binary arrays
  17278. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  17279. /// constructor for binary arrays (internal type)
  17280. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  17281. /// constructor for rvalue binary arrays (internal type)
  17282. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  17283. void destroy(value_t t)
  17284. {
  17285. if (
  17286. (t == value_t::object && object == nullptr) ||
  17287. (t == value_t::array && array == nullptr) ||
  17288. (t == value_t::string && string == nullptr) ||
  17289. (t == value_t::binary && binary == nullptr)
  17290. )
  17291. {
  17292. //not initialized (e.g. due to exception in the ctor)
  17293. return;
  17294. }
  17295. if (t == value_t::array || t == value_t::object)
  17296. {
  17297. // flatten the current json_value to a heap-allocated stack
  17298. std::vector<basic_json> stack;
  17299. // move the top-level items to stack
  17300. if (t == value_t::array)
  17301. {
  17302. stack.reserve(array->size());
  17303. std::move(array->begin(), array->end(), std::back_inserter(stack));
  17304. }
  17305. else
  17306. {
  17307. stack.reserve(object->size());
  17308. for (auto&& it : *object)
  17309. {
  17310. stack.push_back(std::move(it.second));
  17311. }
  17312. }
  17313. while (!stack.empty())
  17314. {
  17315. // move the last item to local variable to be processed
  17316. basic_json current_item(std::move(stack.back()));
  17317. stack.pop_back();
  17318. // if current_item is array/object, move
  17319. // its children to the stack to be processed later
  17320. if (current_item.is_array())
  17321. {
  17322. std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
  17323. current_item.m_data.m_value.array->clear();
  17324. }
  17325. else if (current_item.is_object())
  17326. {
  17327. for (auto&& it : *current_item.m_data.m_value.object)
  17328. {
  17329. stack.push_back(std::move(it.second));
  17330. }
  17331. current_item.m_data.m_value.object->clear();
  17332. }
  17333. // it's now safe that current_item get destructed
  17334. // since it doesn't have any children
  17335. }
  17336. }
  17337. switch (t)
  17338. {
  17339. case value_t::object:
  17340. {
  17341. AllocatorType<object_t> alloc;
  17342. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  17343. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  17344. break;
  17345. }
  17346. case value_t::array:
  17347. {
  17348. AllocatorType<array_t> alloc;
  17349. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  17350. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  17351. break;
  17352. }
  17353. case value_t::string:
  17354. {
  17355. AllocatorType<string_t> alloc;
  17356. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  17357. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  17358. break;
  17359. }
  17360. case value_t::binary:
  17361. {
  17362. AllocatorType<binary_t> alloc;
  17363. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  17364. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  17365. break;
  17366. }
  17367. case value_t::null:
  17368. case value_t::boolean:
  17369. case value_t::number_integer:
  17370. case value_t::number_unsigned:
  17371. case value_t::number_float:
  17372. case value_t::discarded:
  17373. default:
  17374. {
  17375. break;
  17376. }
  17377. }
  17378. }
  17379. };
  17380. private:
  17381. /*!
  17382. @brief checks the class invariants
  17383. This function asserts the class invariants. It needs to be called at the
  17384. end of every constructor to make sure that created objects respect the
  17385. invariant. Furthermore, it has to be called each time the type of a JSON
  17386. value is changed, because the invariant expresses a relationship between
  17387. @a m_type and @a m_value.
  17388. Furthermore, the parent relation is checked for arrays and objects: If
  17389. @a check_parents true and the value is an array or object, then the
  17390. container's elements must have the current value as parent.
  17391. @param[in] check_parents whether the parent relation should be checked.
  17392. The value is true by default and should only be set to false
  17393. during destruction of objects when the invariant does not
  17394. need to hold.
  17395. */
  17396. void assert_invariant(bool check_parents = true) const noexcept
  17397. {
  17398. JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
  17399. JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
  17400. JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
  17401. JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
  17402. #if JSON_DIAGNOSTICS
  17403. JSON_TRY
  17404. {
  17405. // cppcheck-suppress assertWithSideEffect
  17406. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  17407. {
  17408. return j.m_parent == this;
  17409. }));
  17410. }
  17411. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  17412. #endif
  17413. static_cast<void>(check_parents);
  17414. }
  17415. void set_parents()
  17416. {
  17417. #if JSON_DIAGNOSTICS
  17418. switch (m_data.m_type)
  17419. {
  17420. case value_t::array:
  17421. {
  17422. for (auto& element : *m_data.m_value.array)
  17423. {
  17424. element.m_parent = this;
  17425. }
  17426. break;
  17427. }
  17428. case value_t::object:
  17429. {
  17430. for (auto& element : *m_data.m_value.object)
  17431. {
  17432. element.second.m_parent = this;
  17433. }
  17434. break;
  17435. }
  17436. case value_t::null:
  17437. case value_t::string:
  17438. case value_t::boolean:
  17439. case value_t::number_integer:
  17440. case value_t::number_unsigned:
  17441. case value_t::number_float:
  17442. case value_t::binary:
  17443. case value_t::discarded:
  17444. default:
  17445. break;
  17446. }
  17447. #endif
  17448. }
  17449. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  17450. {
  17451. #if JSON_DIAGNOSTICS
  17452. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  17453. {
  17454. (it + i)->m_parent = this;
  17455. }
  17456. #else
  17457. static_cast<void>(count_set_parents);
  17458. #endif
  17459. return it;
  17460. }
  17461. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  17462. {
  17463. #if JSON_DIAGNOSTICS
  17464. if (old_capacity != static_cast<std::size_t>(-1))
  17465. {
  17466. // see https://github.com/nlohmann/json/issues/2838
  17467. JSON_ASSERT(type() == value_t::array);
  17468. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  17469. {
  17470. // capacity has changed: update all parents
  17471. set_parents();
  17472. return j;
  17473. }
  17474. }
  17475. // ordered_json uses a vector internally, so pointers could have
  17476. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  17477. #ifdef JSON_HEDLEY_MSVC_VERSION
  17478. #pragma warning(push )
  17479. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  17480. #endif
  17481. if (detail::is_ordered_map<object_t>::value)
  17482. {
  17483. set_parents();
  17484. return j;
  17485. }
  17486. #ifdef JSON_HEDLEY_MSVC_VERSION
  17487. #pragma warning( pop )
  17488. #endif
  17489. j.m_parent = this;
  17490. #else
  17491. static_cast<void>(j);
  17492. static_cast<void>(old_capacity);
  17493. #endif
  17494. return j;
  17495. }
  17496. public:
  17497. //////////////////////////
  17498. // JSON parser callback //
  17499. //////////////////////////
  17500. /// @brief parser event types
  17501. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  17502. using parse_event_t = detail::parse_event_t;
  17503. /// @brief per-element parser callback type
  17504. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  17505. using parser_callback_t = detail::parser_callback_t<basic_json>;
  17506. //////////////////
  17507. // constructors //
  17508. //////////////////
  17509. /// @name constructors and destructors
  17510. /// Constructors of class @ref basic_json, copy/move constructor, copy
  17511. /// assignment, static functions creating objects, and the destructor.
  17512. /// @{
  17513. /// @brief create an empty value with a given type
  17514. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17515. basic_json(const value_t v)
  17516. : m_data(v)
  17517. {
  17518. assert_invariant();
  17519. }
  17520. /// @brief create a null object
  17521. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17522. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  17523. : basic_json(value_t::null)
  17524. {
  17525. assert_invariant();
  17526. }
  17527. /// @brief create a JSON value from compatible types
  17528. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17529. template < typename CompatibleType,
  17530. typename U = detail::uncvref_t<CompatibleType>,
  17531. detail::enable_if_t <
  17532. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  17533. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  17534. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  17535. std::forward<CompatibleType>(val))))
  17536. {
  17537. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  17538. set_parents();
  17539. assert_invariant();
  17540. }
  17541. /// @brief create a JSON value from an existing one
  17542. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17543. template < typename BasicJsonType,
  17544. detail::enable_if_t <
  17545. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  17546. basic_json(const BasicJsonType& val)
  17547. {
  17548. using other_boolean_t = typename BasicJsonType::boolean_t;
  17549. using other_number_float_t = typename BasicJsonType::number_float_t;
  17550. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  17551. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  17552. using other_string_t = typename BasicJsonType::string_t;
  17553. using other_object_t = typename BasicJsonType::object_t;
  17554. using other_array_t = typename BasicJsonType::array_t;
  17555. using other_binary_t = typename BasicJsonType::binary_t;
  17556. switch (val.type())
  17557. {
  17558. case value_t::boolean:
  17559. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  17560. break;
  17561. case value_t::number_float:
  17562. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  17563. break;
  17564. case value_t::number_integer:
  17565. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  17566. break;
  17567. case value_t::number_unsigned:
  17568. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  17569. break;
  17570. case value_t::string:
  17571. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  17572. break;
  17573. case value_t::object:
  17574. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  17575. break;
  17576. case value_t::array:
  17577. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  17578. break;
  17579. case value_t::binary:
  17580. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  17581. break;
  17582. case value_t::null:
  17583. *this = nullptr;
  17584. break;
  17585. case value_t::discarded:
  17586. m_data.m_type = value_t::discarded;
  17587. break;
  17588. default: // LCOV_EXCL_LINE
  17589. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17590. }
  17591. JSON_ASSERT(m_data.m_type == val.type());
  17592. set_parents();
  17593. assert_invariant();
  17594. }
  17595. /// @brief create a container (array or object) from an initializer list
  17596. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17597. basic_json(initializer_list_t init,
  17598. bool type_deduction = true,
  17599. value_t manual_type = value_t::array)
  17600. {
  17601. // check if each element is an array with two elements whose first
  17602. // element is a string
  17603. bool is_an_object = std::all_of(init.begin(), init.end(),
  17604. [](const detail::json_ref<basic_json>& element_ref)
  17605. {
  17606. // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
  17607. // (many string types can be constructed from 0 via its null-pointer guise, so we get a
  17608. // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
  17609. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
  17610. });
  17611. // adjust type if type deduction is not wanted
  17612. if (!type_deduction)
  17613. {
  17614. // if array is wanted, do not create an object though possible
  17615. if (manual_type == value_t::array)
  17616. {
  17617. is_an_object = false;
  17618. }
  17619. // if object is wanted but impossible, throw an exception
  17620. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  17621. {
  17622. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  17623. }
  17624. }
  17625. if (is_an_object)
  17626. {
  17627. // the initializer list is a list of pairs -> create object
  17628. m_data.m_type = value_t::object;
  17629. m_data.m_value = value_t::object;
  17630. for (auto& element_ref : init)
  17631. {
  17632. auto element = element_ref.moved_or_copied();
  17633. m_data.m_value.object->emplace(
  17634. std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
  17635. std::move((*element.m_data.m_value.array)[1]));
  17636. }
  17637. }
  17638. else
  17639. {
  17640. // the initializer list describes an array -> create array
  17641. m_data.m_type = value_t::array;
  17642. m_data.m_value.array = create<array_t>(init.begin(), init.end());
  17643. }
  17644. set_parents();
  17645. assert_invariant();
  17646. }
  17647. /// @brief explicitly create a binary array (without subtype)
  17648. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17649. JSON_HEDLEY_WARN_UNUSED_RESULT
  17650. static basic_json binary(const typename binary_t::container_type& init)
  17651. {
  17652. auto res = basic_json();
  17653. res.m_data.m_type = value_t::binary;
  17654. res.m_data.m_value = init;
  17655. return res;
  17656. }
  17657. /// @brief explicitly create a binary array (with subtype)
  17658. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17659. JSON_HEDLEY_WARN_UNUSED_RESULT
  17660. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  17661. {
  17662. auto res = basic_json();
  17663. res.m_data.m_type = value_t::binary;
  17664. res.m_data.m_value = binary_t(init, subtype);
  17665. return res;
  17666. }
  17667. /// @brief explicitly create a binary array
  17668. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17669. JSON_HEDLEY_WARN_UNUSED_RESULT
  17670. static basic_json binary(typename binary_t::container_type&& init)
  17671. {
  17672. auto res = basic_json();
  17673. res.m_data.m_type = value_t::binary;
  17674. res.m_data.m_value = std::move(init);
  17675. return res;
  17676. }
  17677. /// @brief explicitly create a binary array (with subtype)
  17678. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17679. JSON_HEDLEY_WARN_UNUSED_RESULT
  17680. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  17681. {
  17682. auto res = basic_json();
  17683. res.m_data.m_type = value_t::binary;
  17684. res.m_data.m_value = binary_t(std::move(init), subtype);
  17685. return res;
  17686. }
  17687. /// @brief explicitly create an array from an initializer list
  17688. /// @sa https://json.nlohmann.me/api/basic_json/array/
  17689. JSON_HEDLEY_WARN_UNUSED_RESULT
  17690. static basic_json array(initializer_list_t init = {})
  17691. {
  17692. return basic_json(init, false, value_t::array);
  17693. }
  17694. /// @brief explicitly create an object from an initializer list
  17695. /// @sa https://json.nlohmann.me/api/basic_json/object/
  17696. JSON_HEDLEY_WARN_UNUSED_RESULT
  17697. static basic_json object(initializer_list_t init = {})
  17698. {
  17699. return basic_json(init, false, value_t::object);
  17700. }
  17701. /// @brief construct an array with count copies of given value
  17702. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17703. basic_json(size_type cnt, const basic_json& val):
  17704. m_data{cnt, val}
  17705. {
  17706. set_parents();
  17707. assert_invariant();
  17708. }
  17709. /// @brief construct a JSON container given an iterator range
  17710. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17711. template < class InputIT, typename std::enable_if <
  17712. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  17713. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  17714. basic_json(InputIT first, InputIT last)
  17715. {
  17716. JSON_ASSERT(first.m_object != nullptr);
  17717. JSON_ASSERT(last.m_object != nullptr);
  17718. // make sure iterator fits the current value
  17719. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  17720. {
  17721. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  17722. }
  17723. // copy type from first iterator
  17724. m_data.m_type = first.m_object->m_data.m_type;
  17725. // check if iterator range is complete for primitive values
  17726. switch (m_data.m_type)
  17727. {
  17728. case value_t::boolean:
  17729. case value_t::number_float:
  17730. case value_t::number_integer:
  17731. case value_t::number_unsigned:
  17732. case value_t::string:
  17733. {
  17734. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  17735. || !last.m_it.primitive_iterator.is_end()))
  17736. {
  17737. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  17738. }
  17739. break;
  17740. }
  17741. case value_t::null:
  17742. case value_t::object:
  17743. case value_t::array:
  17744. case value_t::binary:
  17745. case value_t::discarded:
  17746. default:
  17747. break;
  17748. }
  17749. switch (m_data.m_type)
  17750. {
  17751. case value_t::number_integer:
  17752. {
  17753. m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
  17754. break;
  17755. }
  17756. case value_t::number_unsigned:
  17757. {
  17758. m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
  17759. break;
  17760. }
  17761. case value_t::number_float:
  17762. {
  17763. m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
  17764. break;
  17765. }
  17766. case value_t::boolean:
  17767. {
  17768. m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
  17769. break;
  17770. }
  17771. case value_t::string:
  17772. {
  17773. m_data.m_value = *first.m_object->m_data.m_value.string;
  17774. break;
  17775. }
  17776. case value_t::object:
  17777. {
  17778. m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
  17779. last.m_it.object_iterator);
  17780. break;
  17781. }
  17782. case value_t::array:
  17783. {
  17784. m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
  17785. last.m_it.array_iterator);
  17786. break;
  17787. }
  17788. case value_t::binary:
  17789. {
  17790. m_data.m_value = *first.m_object->m_data.m_value.binary;
  17791. break;
  17792. }
  17793. case value_t::null:
  17794. case value_t::discarded:
  17795. default:
  17796. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  17797. }
  17798. set_parents();
  17799. assert_invariant();
  17800. }
  17801. ///////////////////////////////////////
  17802. // other constructors and destructor //
  17803. ///////////////////////////////////////
  17804. template<typename JsonRef,
  17805. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  17806. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  17807. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  17808. /// @brief copy constructor
  17809. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17810. basic_json(const basic_json& other)
  17811. : json_base_class_t(other)
  17812. {
  17813. m_data.m_type = other.m_data.m_type;
  17814. // check of passed value is valid
  17815. other.assert_invariant();
  17816. switch (m_data.m_type)
  17817. {
  17818. case value_t::object:
  17819. {
  17820. m_data.m_value = *other.m_data.m_value.object;
  17821. break;
  17822. }
  17823. case value_t::array:
  17824. {
  17825. m_data.m_value = *other.m_data.m_value.array;
  17826. break;
  17827. }
  17828. case value_t::string:
  17829. {
  17830. m_data.m_value = *other.m_data.m_value.string;
  17831. break;
  17832. }
  17833. case value_t::boolean:
  17834. {
  17835. m_data.m_value = other.m_data.m_value.boolean;
  17836. break;
  17837. }
  17838. case value_t::number_integer:
  17839. {
  17840. m_data.m_value = other.m_data.m_value.number_integer;
  17841. break;
  17842. }
  17843. case value_t::number_unsigned:
  17844. {
  17845. m_data.m_value = other.m_data.m_value.number_unsigned;
  17846. break;
  17847. }
  17848. case value_t::number_float:
  17849. {
  17850. m_data.m_value = other.m_data.m_value.number_float;
  17851. break;
  17852. }
  17853. case value_t::binary:
  17854. {
  17855. m_data.m_value = *other.m_data.m_value.binary;
  17856. break;
  17857. }
  17858. case value_t::null:
  17859. case value_t::discarded:
  17860. default:
  17861. break;
  17862. }
  17863. set_parents();
  17864. assert_invariant();
  17865. }
  17866. /// @brief move constructor
  17867. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17868. basic_json(basic_json&& other) noexcept
  17869. : json_base_class_t(std::forward<json_base_class_t>(other)),
  17870. m_data(std::move(other.m_data))
  17871. {
  17872. // check that passed value is valid
  17873. other.assert_invariant(false);
  17874. // invalidate payload
  17875. other.m_data.m_type = value_t::null;
  17876. other.m_data.m_value = {};
  17877. set_parents();
  17878. assert_invariant();
  17879. }
  17880. /// @brief copy assignment
  17881. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  17882. basic_json& operator=(basic_json other) noexcept (
  17883. std::is_nothrow_move_constructible<value_t>::value&&
  17884. std::is_nothrow_move_assignable<value_t>::value&&
  17885. std::is_nothrow_move_constructible<json_value>::value&&
  17886. std::is_nothrow_move_assignable<json_value>::value&&
  17887. std::is_nothrow_move_assignable<json_base_class_t>::value
  17888. )
  17889. {
  17890. // check that passed value is valid
  17891. other.assert_invariant();
  17892. using std::swap;
  17893. swap(m_data.m_type, other.m_data.m_type);
  17894. swap(m_data.m_value, other.m_data.m_value);
  17895. json_base_class_t::operator=(std::move(other));
  17896. set_parents();
  17897. assert_invariant();
  17898. return *this;
  17899. }
  17900. /// @brief destructor
  17901. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  17902. ~basic_json() noexcept
  17903. {
  17904. assert_invariant(false);
  17905. }
  17906. /// @}
  17907. public:
  17908. ///////////////////////
  17909. // object inspection //
  17910. ///////////////////////
  17911. /// @name object inspection
  17912. /// Functions to inspect the type of a JSON value.
  17913. /// @{
  17914. /// @brief serialization
  17915. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  17916. string_t dump(const int indent = -1,
  17917. const char indent_char = ' ',
  17918. const bool ensure_ascii = false,
  17919. const error_handler_t error_handler = error_handler_t::strict) const
  17920. {
  17921. string_t result;
  17922. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17923. if (indent >= 0)
  17924. {
  17925. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17926. }
  17927. else
  17928. {
  17929. s.dump(*this, false, ensure_ascii, 0);
  17930. }
  17931. return result;
  17932. }
  17933. /// @brief return the type of the JSON value (explicit)
  17934. /// @sa https://json.nlohmann.me/api/basic_json/type/
  17935. constexpr value_t type() const noexcept
  17936. {
  17937. return m_data.m_type;
  17938. }
  17939. /// @brief return whether type is primitive
  17940. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  17941. constexpr bool is_primitive() const noexcept
  17942. {
  17943. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17944. }
  17945. /// @brief return whether type is structured
  17946. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  17947. constexpr bool is_structured() const noexcept
  17948. {
  17949. return is_array() || is_object();
  17950. }
  17951. /// @brief return whether value is null
  17952. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  17953. constexpr bool is_null() const noexcept
  17954. {
  17955. return m_data.m_type == value_t::null;
  17956. }
  17957. /// @brief return whether value is a boolean
  17958. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  17959. constexpr bool is_boolean() const noexcept
  17960. {
  17961. return m_data.m_type == value_t::boolean;
  17962. }
  17963. /// @brief return whether value is a number
  17964. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  17965. constexpr bool is_number() const noexcept
  17966. {
  17967. return is_number_integer() || is_number_float();
  17968. }
  17969. /// @brief return whether value is an integer number
  17970. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  17971. constexpr bool is_number_integer() const noexcept
  17972. {
  17973. return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
  17974. }
  17975. /// @brief return whether value is an unsigned integer number
  17976. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  17977. constexpr bool is_number_unsigned() const noexcept
  17978. {
  17979. return m_data.m_type == value_t::number_unsigned;
  17980. }
  17981. /// @brief return whether value is a floating-point number
  17982. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  17983. constexpr bool is_number_float() const noexcept
  17984. {
  17985. return m_data.m_type == value_t::number_float;
  17986. }
  17987. /// @brief return whether value is an object
  17988. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  17989. constexpr bool is_object() const noexcept
  17990. {
  17991. return m_data.m_type == value_t::object;
  17992. }
  17993. /// @brief return whether value is an array
  17994. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  17995. constexpr bool is_array() const noexcept
  17996. {
  17997. return m_data.m_type == value_t::array;
  17998. }
  17999. /// @brief return whether value is a string
  18000. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  18001. constexpr bool is_string() const noexcept
  18002. {
  18003. return m_data.m_type == value_t::string;
  18004. }
  18005. /// @brief return whether value is a binary array
  18006. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  18007. constexpr bool is_binary() const noexcept
  18008. {
  18009. return m_data.m_type == value_t::binary;
  18010. }
  18011. /// @brief return whether value is discarded
  18012. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  18013. constexpr bool is_discarded() const noexcept
  18014. {
  18015. return m_data.m_type == value_t::discarded;
  18016. }
  18017. /// @brief return the type of the JSON value (implicit)
  18018. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  18019. constexpr operator value_t() const noexcept
  18020. {
  18021. return m_data.m_type;
  18022. }
  18023. /// @}
  18024. private:
  18025. //////////////////
  18026. // value access //
  18027. //////////////////
  18028. /// get a boolean (explicit)
  18029. boolean_t get_impl(boolean_t* /*unused*/) const
  18030. {
  18031. if (JSON_HEDLEY_LIKELY(is_boolean()))
  18032. {
  18033. return m_data.m_value.boolean;
  18034. }
  18035. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  18036. }
  18037. /// get a pointer to the value (object)
  18038. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  18039. {
  18040. return is_object() ? m_data.m_value.object : nullptr;
  18041. }
  18042. /// get a pointer to the value (object)
  18043. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  18044. {
  18045. return is_object() ? m_data.m_value.object : nullptr;
  18046. }
  18047. /// get a pointer to the value (array)
  18048. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  18049. {
  18050. return is_array() ? m_data.m_value.array : nullptr;
  18051. }
  18052. /// get a pointer to the value (array)
  18053. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  18054. {
  18055. return is_array() ? m_data.m_value.array : nullptr;
  18056. }
  18057. /// get a pointer to the value (string)
  18058. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  18059. {
  18060. return is_string() ? m_data.m_value.string : nullptr;
  18061. }
  18062. /// get a pointer to the value (string)
  18063. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  18064. {
  18065. return is_string() ? m_data.m_value.string : nullptr;
  18066. }
  18067. /// get a pointer to the value (boolean)
  18068. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  18069. {
  18070. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18071. }
  18072. /// get a pointer to the value (boolean)
  18073. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  18074. {
  18075. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18076. }
  18077. /// get a pointer to the value (integer number)
  18078. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  18079. {
  18080. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  18081. }
  18082. /// get a pointer to the value (integer number)
  18083. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  18084. {
  18085. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  18086. }
  18087. /// get a pointer to the value (unsigned number)
  18088. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  18089. {
  18090. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18091. }
  18092. /// get a pointer to the value (unsigned number)
  18093. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  18094. {
  18095. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18096. }
  18097. /// get a pointer to the value (floating-point number)
  18098. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  18099. {
  18100. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18101. }
  18102. /// get a pointer to the value (floating-point number)
  18103. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  18104. {
  18105. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18106. }
  18107. /// get a pointer to the value (binary)
  18108. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  18109. {
  18110. return is_binary() ? m_data.m_value.binary : nullptr;
  18111. }
  18112. /// get a pointer to the value (binary)
  18113. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  18114. {
  18115. return is_binary() ? m_data.m_value.binary : nullptr;
  18116. }
  18117. /*!
  18118. @brief helper function to implement get_ref()
  18119. This function helps to implement get_ref() without code duplication for
  18120. const and non-const overloads
  18121. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  18122. @throw type_error.303 if ReferenceType does not match underlying value
  18123. type of the current JSON
  18124. */
  18125. template<typename ReferenceType, typename ThisType>
  18126. static ReferenceType get_ref_impl(ThisType& obj)
  18127. {
  18128. // delegate the call to get_ptr<>()
  18129. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18130. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18131. {
  18132. return *ptr;
  18133. }
  18134. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18135. }
  18136. public:
  18137. /// @name value access
  18138. /// Direct access to the stored value of a JSON value.
  18139. /// @{
  18140. /// @brief get a pointer value (implicit)
  18141. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18142. template<typename PointerType, typename std::enable_if<
  18143. std::is_pointer<PointerType>::value, int>::type = 0>
  18144. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18145. {
  18146. // delegate the call to get_impl_ptr<>()
  18147. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18148. }
  18149. /// @brief get a pointer value (implicit)
  18150. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18151. template < typename PointerType, typename std::enable_if <
  18152. std::is_pointer<PointerType>::value&&
  18153. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18154. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18155. {
  18156. // delegate the call to get_impl_ptr<>() const
  18157. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18158. }
  18159. private:
  18160. /*!
  18161. @brief get a value (explicit)
  18162. Explicit type conversion between the JSON value and a compatible value
  18163. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18164. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18165. The value is converted by calling the @ref json_serializer<ValueType>
  18166. `from_json()` method.
  18167. The function is equivalent to executing
  18168. @code {.cpp}
  18169. ValueType ret;
  18170. JSONSerializer<ValueType>::from_json(*this, ret);
  18171. return ret;
  18172. @endcode
  18173. This overloads is chosen if:
  18174. - @a ValueType is not @ref basic_json,
  18175. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18176. `void from_json(const basic_json&, ValueType&)`, and
  18177. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  18178. the form `ValueType from_json(const basic_json&)`
  18179. @tparam ValueType the returned value type
  18180. @return copy of the JSON value, converted to @a ValueType
  18181. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18182. @liveexample{The example below shows several conversions from JSON values
  18183. to other types. There a few things to note: (1) Floating-point numbers can
  18184. be converted to integers\, (2) A JSON array can be converted to a standard
  18185. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18186. associative containers such as `std::unordered_map<std::string\,
  18187. json>`.,get__ValueType_const}
  18188. @since version 2.1.0
  18189. */
  18190. template < typename ValueType,
  18191. detail::enable_if_t <
  18192. detail::is_default_constructible<ValueType>::value&&
  18193. detail::has_from_json<basic_json_t, ValueType>::value,
  18194. int > = 0 >
  18195. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  18196. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  18197. {
  18198. auto ret = ValueType();
  18199. JSONSerializer<ValueType>::from_json(*this, ret);
  18200. return ret;
  18201. }
  18202. /*!
  18203. @brief get a value (explicit); special case
  18204. Explicit type conversion between the JSON value and a compatible value
  18205. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18206. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18207. The value is converted by calling the @ref json_serializer<ValueType>
  18208. `from_json()` method.
  18209. The function is equivalent to executing
  18210. @code {.cpp}
  18211. return JSONSerializer<ValueType>::from_json(*this);
  18212. @endcode
  18213. This overloads is chosen if:
  18214. - @a ValueType is not @ref basic_json and
  18215. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18216. `ValueType from_json(const basic_json&)`
  18217. @note If @ref json_serializer<ValueType> has both overloads of
  18218. `from_json()`, this one is chosen.
  18219. @tparam ValueType the returned value type
  18220. @return copy of the JSON value, converted to @a ValueType
  18221. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18222. @since version 2.1.0
  18223. */
  18224. template < typename ValueType,
  18225. detail::enable_if_t <
  18226. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  18227. int > = 0 >
  18228. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  18229. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  18230. {
  18231. return JSONSerializer<ValueType>::from_json(*this);
  18232. }
  18233. /*!
  18234. @brief get special-case overload
  18235. This overloads converts the current @ref basic_json in a different
  18236. @ref basic_json type
  18237. @tparam BasicJsonType == @ref basic_json
  18238. @return a copy of *this, converted into @a BasicJsonType
  18239. @complexity Depending on the implementation of the called `from_json()`
  18240. method.
  18241. @since version 3.2.0
  18242. */
  18243. template < typename BasicJsonType,
  18244. detail::enable_if_t <
  18245. detail::is_basic_json<BasicJsonType>::value,
  18246. int > = 0 >
  18247. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  18248. {
  18249. return *this;
  18250. }
  18251. /*!
  18252. @brief get special-case overload
  18253. This overloads avoids a lot of template boilerplate, it can be seen as the
  18254. identity method
  18255. @tparam BasicJsonType == @ref basic_json
  18256. @return a copy of *this
  18257. @complexity Constant.
  18258. @since version 2.1.0
  18259. */
  18260. template<typename BasicJsonType,
  18261. detail::enable_if_t<
  18262. std::is_same<BasicJsonType, basic_json_t>::value,
  18263. int> = 0>
  18264. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  18265. {
  18266. return *this;
  18267. }
  18268. /*!
  18269. @brief get a pointer value (explicit)
  18270. @copydoc get()
  18271. */
  18272. template<typename PointerType,
  18273. detail::enable_if_t<
  18274. std::is_pointer<PointerType>::value,
  18275. int> = 0>
  18276. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  18277. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  18278. {
  18279. // delegate the call to get_ptr
  18280. return get_ptr<PointerType>();
  18281. }
  18282. public:
  18283. /*!
  18284. @brief get a (pointer) value (explicit)
  18285. Performs explicit type conversion between the JSON value and a compatible value if required.
  18286. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  18287. No copies are made.
  18288. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  18289. from the current @ref basic_json.
  18290. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  18291. method.
  18292. @tparam ValueTypeCV the provided value type
  18293. @tparam ValueType the returned value type
  18294. @return copy of the JSON value, converted to @tparam ValueType if necessary
  18295. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  18296. @since version 2.1.0
  18297. */
  18298. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  18299. #if defined(JSON_HAS_CPP_14)
  18300. constexpr
  18301. #endif
  18302. auto get() const noexcept(
  18303. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  18304. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  18305. {
  18306. // we cannot static_assert on ValueTypeCV being non-const, because
  18307. // there is support for get<const basic_json_t>(), which is why we
  18308. // still need the uncvref
  18309. static_assert(!std::is_reference<ValueTypeCV>::value,
  18310. "get() cannot be used with reference types, you might want to use get_ref()");
  18311. return get_impl<ValueType>(detail::priority_tag<4> {});
  18312. }
  18313. /*!
  18314. @brief get a pointer value (explicit)
  18315. Explicit pointer access to the internally stored JSON value. No copies are
  18316. made.
  18317. @warning The pointer becomes invalid if the underlying JSON object
  18318. changes.
  18319. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  18320. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  18321. @ref number_unsigned_t, or @ref number_float_t.
  18322. @return pointer to the internally stored JSON value if the requested
  18323. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  18324. @complexity Constant.
  18325. @liveexample{The example below shows how pointers to internal values of a
  18326. JSON value can be requested. Note that no type conversions are made and a
  18327. `nullptr` is returned if the value and the requested pointer type does not
  18328. match.,get__PointerType}
  18329. @sa see @ref get_ptr() for explicit pointer-member access
  18330. @since version 1.0.0
  18331. */
  18332. template<typename PointerType, typename std::enable_if<
  18333. std::is_pointer<PointerType>::value, int>::type = 0>
  18334. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  18335. {
  18336. // delegate the call to get_ptr
  18337. return get_ptr<PointerType>();
  18338. }
  18339. /// @brief get a value (explicit)
  18340. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  18341. template < typename ValueType,
  18342. detail::enable_if_t <
  18343. !detail::is_basic_json<ValueType>::value&&
  18344. detail::has_from_json<basic_json_t, ValueType>::value,
  18345. int > = 0 >
  18346. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  18347. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  18348. {
  18349. JSONSerializer<ValueType>::from_json(*this, v);
  18350. return v;
  18351. }
  18352. // specialization to allow calling get_to with a basic_json value
  18353. // see https://github.com/nlohmann/json/issues/2175
  18354. template<typename ValueType,
  18355. detail::enable_if_t <
  18356. detail::is_basic_json<ValueType>::value,
  18357. int> = 0>
  18358. ValueType & get_to(ValueType& v) const
  18359. {
  18360. v = *this;
  18361. return v;
  18362. }
  18363. template <
  18364. typename T, std::size_t N,
  18365. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18366. detail::enable_if_t <
  18367. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  18368. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18369. noexcept(noexcept(JSONSerializer<Array>::from_json(
  18370. std::declval<const basic_json_t&>(), v)))
  18371. {
  18372. JSONSerializer<Array>::from_json(*this, v);
  18373. return v;
  18374. }
  18375. /// @brief get a reference value (implicit)
  18376. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18377. template<typename ReferenceType, typename std::enable_if<
  18378. std::is_reference<ReferenceType>::value, int>::type = 0>
  18379. ReferenceType get_ref()
  18380. {
  18381. // delegate call to get_ref_impl
  18382. return get_ref_impl<ReferenceType>(*this);
  18383. }
  18384. /// @brief get a reference value (implicit)
  18385. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18386. template < typename ReferenceType, typename std::enable_if <
  18387. std::is_reference<ReferenceType>::value&&
  18388. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  18389. ReferenceType get_ref() const
  18390. {
  18391. // delegate call to get_ref_impl
  18392. return get_ref_impl<ReferenceType>(*this);
  18393. }
  18394. /*!
  18395. @brief get a value (implicit)
  18396. Implicit type conversion between the JSON value and a compatible value.
  18397. The call is realized by calling @ref get() const.
  18398. @tparam ValueType non-pointer type compatible to the JSON value, for
  18399. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  18400. `std::vector` types for JSON arrays. The character type of @ref string_t
  18401. as well as an initializer list of this type is excluded to avoid
  18402. ambiguities as these types implicitly convert to `std::string`.
  18403. @return copy of the JSON value, converted to type @a ValueType
  18404. @throw type_error.302 in case passed type @a ValueType is incompatible
  18405. to the JSON value type (e.g., the JSON value is of type boolean, but a
  18406. string is requested); see example below
  18407. @complexity Linear in the size of the JSON value.
  18408. @liveexample{The example below shows several conversions from JSON values
  18409. to other types. There a few things to note: (1) Floating-point numbers can
  18410. be converted to integers\, (2) A JSON array can be converted to a standard
  18411. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18412. associative containers such as `std::unordered_map<std::string\,
  18413. json>`.,operator__ValueType}
  18414. @since version 1.0.0
  18415. */
  18416. template < typename ValueType, typename std::enable_if <
  18417. detail::conjunction <
  18418. detail::negation<std::is_pointer<ValueType>>,
  18419. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  18420. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  18421. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  18422. detail::negation<detail::is_basic_json<ValueType>>,
  18423. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  18424. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  18425. detail::negation<std::is_same<ValueType, std::string_view>>,
  18426. #endif
  18427. #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
  18428. detail::negation<std::is_same<ValueType, std::any>>,
  18429. #endif
  18430. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  18431. >::value, int >::type = 0 >
  18432. JSON_EXPLICIT operator ValueType() const
  18433. {
  18434. // delegate the call to get<>() const
  18435. return get<ValueType>();
  18436. }
  18437. /// @brief get a binary value
  18438. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18439. binary_t& get_binary()
  18440. {
  18441. if (!is_binary())
  18442. {
  18443. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18444. }
  18445. return *get_ptr<binary_t*>();
  18446. }
  18447. /// @brief get a binary value
  18448. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18449. const binary_t& get_binary() const
  18450. {
  18451. if (!is_binary())
  18452. {
  18453. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18454. }
  18455. return *get_ptr<const binary_t*>();
  18456. }
  18457. /// @}
  18458. ////////////////////
  18459. // element access //
  18460. ////////////////////
  18461. /// @name element access
  18462. /// Access to the JSON value.
  18463. /// @{
  18464. /// @brief access specified array element with bounds checking
  18465. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18466. reference at(size_type idx)
  18467. {
  18468. // at only works for arrays
  18469. if (JSON_HEDLEY_LIKELY(is_array()))
  18470. {
  18471. JSON_TRY
  18472. {
  18473. return set_parent(m_data.m_value.array->at(idx));
  18474. }
  18475. JSON_CATCH (std::out_of_range&)
  18476. {
  18477. // create better exception explanation
  18478. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18479. }
  18480. }
  18481. else
  18482. {
  18483. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18484. }
  18485. }
  18486. /// @brief access specified array element with bounds checking
  18487. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18488. const_reference at(size_type idx) const
  18489. {
  18490. // at only works for arrays
  18491. if (JSON_HEDLEY_LIKELY(is_array()))
  18492. {
  18493. JSON_TRY
  18494. {
  18495. return m_data.m_value.array->at(idx);
  18496. }
  18497. JSON_CATCH (std::out_of_range&)
  18498. {
  18499. // create better exception explanation
  18500. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18501. }
  18502. }
  18503. else
  18504. {
  18505. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18506. }
  18507. }
  18508. /// @brief access specified object element with bounds checking
  18509. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18510. reference at(const typename object_t::key_type& key)
  18511. {
  18512. // at only works for objects
  18513. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18514. {
  18515. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18516. }
  18517. auto it = m_data.m_value.object->find(key);
  18518. if (it == m_data.m_value.object->end())
  18519. {
  18520. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18521. }
  18522. return set_parent(it->second);
  18523. }
  18524. /// @brief access specified object element with bounds checking
  18525. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18526. template<class KeyType, detail::enable_if_t<
  18527. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18528. reference at(KeyType && key)
  18529. {
  18530. // at only works for objects
  18531. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18532. {
  18533. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18534. }
  18535. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18536. if (it == m_data.m_value.object->end())
  18537. {
  18538. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18539. }
  18540. return set_parent(it->second);
  18541. }
  18542. /// @brief access specified object element with bounds checking
  18543. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18544. const_reference at(const typename object_t::key_type& key) const
  18545. {
  18546. // at only works for objects
  18547. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18548. {
  18549. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18550. }
  18551. auto it = m_data.m_value.object->find(key);
  18552. if (it == m_data.m_value.object->end())
  18553. {
  18554. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18555. }
  18556. return it->second;
  18557. }
  18558. /// @brief access specified object element with bounds checking
  18559. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18560. template<class KeyType, detail::enable_if_t<
  18561. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18562. const_reference at(KeyType && key) const
  18563. {
  18564. // at only works for objects
  18565. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18566. {
  18567. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18568. }
  18569. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18570. if (it == m_data.m_value.object->end())
  18571. {
  18572. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18573. }
  18574. return it->second;
  18575. }
  18576. /// @brief access specified array element
  18577. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18578. reference operator[](size_type idx)
  18579. {
  18580. // implicitly convert null value to an empty array
  18581. if (is_null())
  18582. {
  18583. m_data.m_type = value_t::array;
  18584. m_data.m_value.array = create<array_t>();
  18585. assert_invariant();
  18586. }
  18587. // operator[] only works for arrays
  18588. if (JSON_HEDLEY_LIKELY(is_array()))
  18589. {
  18590. // fill up array with null values if given idx is outside range
  18591. if (idx >= m_data.m_value.array->size())
  18592. {
  18593. #if JSON_DIAGNOSTICS
  18594. // remember array size & capacity before resizing
  18595. const auto old_size = m_data.m_value.array->size();
  18596. const auto old_capacity = m_data.m_value.array->capacity();
  18597. #endif
  18598. m_data.m_value.array->resize(idx + 1);
  18599. #if JSON_DIAGNOSTICS
  18600. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  18601. {
  18602. // capacity has changed: update all parents
  18603. set_parents();
  18604. }
  18605. else
  18606. {
  18607. // set parent for values added above
  18608. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  18609. }
  18610. #endif
  18611. assert_invariant();
  18612. }
  18613. return m_data.m_value.array->operator[](idx);
  18614. }
  18615. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18616. }
  18617. /// @brief access specified array element
  18618. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18619. const_reference operator[](size_type idx) const
  18620. {
  18621. // const operator[] only works for arrays
  18622. if (JSON_HEDLEY_LIKELY(is_array()))
  18623. {
  18624. return m_data.m_value.array->operator[](idx);
  18625. }
  18626. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18627. }
  18628. /// @brief access specified object element
  18629. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18630. reference operator[](typename object_t::key_type key)
  18631. {
  18632. // implicitly convert null value to an empty object
  18633. if (is_null())
  18634. {
  18635. m_data.m_type = value_t::object;
  18636. m_data.m_value.object = create<object_t>();
  18637. assert_invariant();
  18638. }
  18639. // operator[] only works for objects
  18640. if (JSON_HEDLEY_LIKELY(is_object()))
  18641. {
  18642. auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
  18643. return set_parent(result.first->second);
  18644. }
  18645. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18646. }
  18647. /// @brief access specified object element
  18648. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18649. const_reference operator[](const typename object_t::key_type& key) const
  18650. {
  18651. // const operator[] only works for objects
  18652. if (JSON_HEDLEY_LIKELY(is_object()))
  18653. {
  18654. auto it = m_data.m_value.object->find(key);
  18655. JSON_ASSERT(it != m_data.m_value.object->end());
  18656. return it->second;
  18657. }
  18658. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18659. }
  18660. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  18661. // (they seemingly cannot be constrained to resolve the ambiguity)
  18662. template<typename T>
  18663. reference operator[](T* key)
  18664. {
  18665. return operator[](typename object_t::key_type(key));
  18666. }
  18667. template<typename T>
  18668. const_reference operator[](T* key) const
  18669. {
  18670. return operator[](typename object_t::key_type(key));
  18671. }
  18672. /// @brief access specified object element
  18673. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18674. template<class KeyType, detail::enable_if_t<
  18675. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18676. reference operator[](KeyType && key)
  18677. {
  18678. // implicitly convert null value to an empty object
  18679. if (is_null())
  18680. {
  18681. m_data.m_type = value_t::object;
  18682. m_data.m_value.object = create<object_t>();
  18683. assert_invariant();
  18684. }
  18685. // operator[] only works for objects
  18686. if (JSON_HEDLEY_LIKELY(is_object()))
  18687. {
  18688. auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  18689. return set_parent(result.first->second);
  18690. }
  18691. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18692. }
  18693. /// @brief access specified object element
  18694. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18695. template<class KeyType, detail::enable_if_t<
  18696. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18697. const_reference operator[](KeyType && key) const
  18698. {
  18699. // const operator[] only works for objects
  18700. if (JSON_HEDLEY_LIKELY(is_object()))
  18701. {
  18702. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18703. JSON_ASSERT(it != m_data.m_value.object->end());
  18704. return it->second;
  18705. }
  18706. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18707. }
  18708. private:
  18709. template<typename KeyType>
  18710. using is_comparable_with_object_key = detail::is_comparable <
  18711. object_comparator_t, const typename object_t::key_type&, KeyType >;
  18712. template<typename ValueType>
  18713. using value_return_type = std::conditional <
  18714. detail::is_c_string_uncvref<ValueType>::value,
  18715. string_t, typename std::decay<ValueType>::type >;
  18716. public:
  18717. /// @brief access specified object element with default value
  18718. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18719. template < class ValueType, detail::enable_if_t <
  18720. !detail::is_transparent<object_comparator_t>::value
  18721. && detail::is_getable<basic_json_t, ValueType>::value
  18722. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18723. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  18724. {
  18725. // value only works for objects
  18726. if (JSON_HEDLEY_LIKELY(is_object()))
  18727. {
  18728. // if key is found, return value and given default value otherwise
  18729. const auto it = find(key);
  18730. if (it != end())
  18731. {
  18732. return it->template get<ValueType>();
  18733. }
  18734. return default_value;
  18735. }
  18736. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18737. }
  18738. /// @brief access specified object element with default value
  18739. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18740. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18741. detail::enable_if_t <
  18742. !detail::is_transparent<object_comparator_t>::value
  18743. && detail::is_getable<basic_json_t, ReturnType>::value
  18744. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18745. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  18746. {
  18747. // value only works for objects
  18748. if (JSON_HEDLEY_LIKELY(is_object()))
  18749. {
  18750. // if key is found, return value and given default value otherwise
  18751. const auto it = find(key);
  18752. if (it != end())
  18753. {
  18754. return it->template get<ReturnType>();
  18755. }
  18756. return std::forward<ValueType>(default_value);
  18757. }
  18758. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18759. }
  18760. /// @brief access specified object element with default value
  18761. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18762. template < class ValueType, class KeyType, detail::enable_if_t <
  18763. detail::is_transparent<object_comparator_t>::value
  18764. && !detail::is_json_pointer<KeyType>::value
  18765. && is_comparable_with_object_key<KeyType>::value
  18766. && detail::is_getable<basic_json_t, ValueType>::value
  18767. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18768. ValueType value(KeyType && key, const ValueType& default_value) const
  18769. {
  18770. // value only works for objects
  18771. if (JSON_HEDLEY_LIKELY(is_object()))
  18772. {
  18773. // if key is found, return value and given default value otherwise
  18774. const auto it = find(std::forward<KeyType>(key));
  18775. if (it != end())
  18776. {
  18777. return it->template get<ValueType>();
  18778. }
  18779. return default_value;
  18780. }
  18781. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18782. }
  18783. /// @brief access specified object element via JSON Pointer with default value
  18784. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18785. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  18786. detail::enable_if_t <
  18787. detail::is_transparent<object_comparator_t>::value
  18788. && !detail::is_json_pointer<KeyType>::value
  18789. && is_comparable_with_object_key<KeyType>::value
  18790. && detail::is_getable<basic_json_t, ReturnType>::value
  18791. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18792. ReturnType value(KeyType && key, ValueType && default_value) const
  18793. {
  18794. // value only works for objects
  18795. if (JSON_HEDLEY_LIKELY(is_object()))
  18796. {
  18797. // if key is found, return value and given default value otherwise
  18798. const auto it = find(std::forward<KeyType>(key));
  18799. if (it != end())
  18800. {
  18801. return it->template get<ReturnType>();
  18802. }
  18803. return std::forward<ValueType>(default_value);
  18804. }
  18805. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18806. }
  18807. /// @brief access specified object element via JSON Pointer with default value
  18808. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18809. template < class ValueType, detail::enable_if_t <
  18810. detail::is_getable<basic_json_t, ValueType>::value
  18811. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18812. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18813. {
  18814. // value only works for objects
  18815. if (JSON_HEDLEY_LIKELY(is_object()))
  18816. {
  18817. // if pointer resolves a value, return it or use default value
  18818. JSON_TRY
  18819. {
  18820. return ptr.get_checked(this).template get<ValueType>();
  18821. }
  18822. JSON_INTERNAL_CATCH (out_of_range&)
  18823. {
  18824. return default_value;
  18825. }
  18826. }
  18827. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18828. }
  18829. /// @brief access specified object element via JSON Pointer with default value
  18830. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18831. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18832. detail::enable_if_t <
  18833. detail::is_getable<basic_json_t, ReturnType>::value
  18834. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18835. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  18836. {
  18837. // value only works for objects
  18838. if (JSON_HEDLEY_LIKELY(is_object()))
  18839. {
  18840. // if pointer resolves a value, return it or use default value
  18841. JSON_TRY
  18842. {
  18843. return ptr.get_checked(this).template get<ReturnType>();
  18844. }
  18845. JSON_INTERNAL_CATCH (out_of_range&)
  18846. {
  18847. return std::forward<ValueType>(default_value);
  18848. }
  18849. }
  18850. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18851. }
  18852. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  18853. detail::is_basic_json<BasicJsonType>::value
  18854. && detail::is_getable<basic_json_t, ValueType>::value
  18855. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18856. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18857. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  18858. {
  18859. return value(ptr.convert(), default_value);
  18860. }
  18861. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  18862. detail::enable_if_t <
  18863. detail::is_basic_json<BasicJsonType>::value
  18864. && detail::is_getable<basic_json_t, ReturnType>::value
  18865. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18866. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18867. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  18868. {
  18869. return value(ptr.convert(), std::forward<ValueType>(default_value));
  18870. }
  18871. /// @brief access the first element
  18872. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18873. reference front()
  18874. {
  18875. return *begin();
  18876. }
  18877. /// @brief access the first element
  18878. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18879. const_reference front() const
  18880. {
  18881. return *cbegin();
  18882. }
  18883. /// @brief access the last element
  18884. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18885. reference back()
  18886. {
  18887. auto tmp = end();
  18888. --tmp;
  18889. return *tmp;
  18890. }
  18891. /// @brief access the last element
  18892. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18893. const_reference back() const
  18894. {
  18895. auto tmp = cend();
  18896. --tmp;
  18897. return *tmp;
  18898. }
  18899. /// @brief remove element given an iterator
  18900. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18901. template < class IteratorType, detail::enable_if_t <
  18902. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18903. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18904. IteratorType erase(IteratorType pos)
  18905. {
  18906. // make sure iterator fits the current value
  18907. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18908. {
  18909. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18910. }
  18911. IteratorType result = end();
  18912. switch (m_data.m_type)
  18913. {
  18914. case value_t::boolean:
  18915. case value_t::number_float:
  18916. case value_t::number_integer:
  18917. case value_t::number_unsigned:
  18918. case value_t::string:
  18919. case value_t::binary:
  18920. {
  18921. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18922. {
  18923. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  18924. }
  18925. if (is_string())
  18926. {
  18927. AllocatorType<string_t> alloc;
  18928. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  18929. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  18930. m_data.m_value.string = nullptr;
  18931. }
  18932. else if (is_binary())
  18933. {
  18934. AllocatorType<binary_t> alloc;
  18935. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  18936. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  18937. m_data.m_value.binary = nullptr;
  18938. }
  18939. m_data.m_type = value_t::null;
  18940. assert_invariant();
  18941. break;
  18942. }
  18943. case value_t::object:
  18944. {
  18945. result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
  18946. break;
  18947. }
  18948. case value_t::array:
  18949. {
  18950. result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
  18951. break;
  18952. }
  18953. case value_t::null:
  18954. case value_t::discarded:
  18955. default:
  18956. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18957. }
  18958. return result;
  18959. }
  18960. /// @brief remove elements given an iterator range
  18961. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18962. template < class IteratorType, detail::enable_if_t <
  18963. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18964. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18965. IteratorType erase(IteratorType first, IteratorType last)
  18966. {
  18967. // make sure iterator fits the current value
  18968. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18969. {
  18970. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  18971. }
  18972. IteratorType result = end();
  18973. switch (m_data.m_type)
  18974. {
  18975. case value_t::boolean:
  18976. case value_t::number_float:
  18977. case value_t::number_integer:
  18978. case value_t::number_unsigned:
  18979. case value_t::string:
  18980. case value_t::binary:
  18981. {
  18982. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18983. || !last.m_it.primitive_iterator.is_end()))
  18984. {
  18985. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  18986. }
  18987. if (is_string())
  18988. {
  18989. AllocatorType<string_t> alloc;
  18990. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  18991. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  18992. m_data.m_value.string = nullptr;
  18993. }
  18994. else if (is_binary())
  18995. {
  18996. AllocatorType<binary_t> alloc;
  18997. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  18998. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  18999. m_data.m_value.binary = nullptr;
  19000. }
  19001. m_data.m_type = value_t::null;
  19002. assert_invariant();
  19003. break;
  19004. }
  19005. case value_t::object:
  19006. {
  19007. result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
  19008. last.m_it.object_iterator);
  19009. break;
  19010. }
  19011. case value_t::array:
  19012. {
  19013. result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
  19014. last.m_it.array_iterator);
  19015. break;
  19016. }
  19017. case value_t::null:
  19018. case value_t::discarded:
  19019. default:
  19020. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19021. }
  19022. return result;
  19023. }
  19024. private:
  19025. template < typename KeyType, detail::enable_if_t <
  19026. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19027. size_type erase_internal(KeyType && key)
  19028. {
  19029. // this erase only works for objects
  19030. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19031. {
  19032. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19033. }
  19034. return m_data.m_value.object->erase(std::forward<KeyType>(key));
  19035. }
  19036. template < typename KeyType, detail::enable_if_t <
  19037. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  19038. size_type erase_internal(KeyType && key)
  19039. {
  19040. // this erase only works for objects
  19041. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19042. {
  19043. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19044. }
  19045. const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  19046. if (it != m_data.m_value.object->end())
  19047. {
  19048. m_data.m_value.object->erase(it);
  19049. return 1;
  19050. }
  19051. return 0;
  19052. }
  19053. public:
  19054. /// @brief remove element from a JSON object given a key
  19055. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19056. size_type erase(const typename object_t::key_type& key)
  19057. {
  19058. // the indirection via erase_internal() is added to avoid making this
  19059. // function a template and thus de-rank it during overload resolution
  19060. return erase_internal(key);
  19061. }
  19062. /// @brief remove element from a JSON object given a key
  19063. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19064. template<class KeyType, detail::enable_if_t<
  19065. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19066. size_type erase(KeyType && key)
  19067. {
  19068. return erase_internal(std::forward<KeyType>(key));
  19069. }
  19070. /// @brief remove element from a JSON array given an index
  19071. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  19072. void erase(const size_type idx)
  19073. {
  19074. // this erase only works for arrays
  19075. if (JSON_HEDLEY_LIKELY(is_array()))
  19076. {
  19077. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  19078. {
  19079. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19080. }
  19081. m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
  19082. }
  19083. else
  19084. {
  19085. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19086. }
  19087. }
  19088. /// @}
  19089. ////////////
  19090. // lookup //
  19091. ////////////
  19092. /// @name lookup
  19093. /// @{
  19094. /// @brief find an element in a JSON object
  19095. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19096. iterator find(const typename object_t::key_type& key)
  19097. {
  19098. auto result = end();
  19099. if (is_object())
  19100. {
  19101. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19102. }
  19103. return result;
  19104. }
  19105. /// @brief find an element in a JSON object
  19106. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19107. const_iterator find(const typename object_t::key_type& key) const
  19108. {
  19109. auto result = cend();
  19110. if (is_object())
  19111. {
  19112. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19113. }
  19114. return result;
  19115. }
  19116. /// @brief find an element in a JSON object
  19117. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19118. template<class KeyType, detail::enable_if_t<
  19119. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19120. iterator find(KeyType && key)
  19121. {
  19122. auto result = end();
  19123. if (is_object())
  19124. {
  19125. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19126. }
  19127. return result;
  19128. }
  19129. /// @brief find an element in a JSON object
  19130. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19131. template<class KeyType, detail::enable_if_t<
  19132. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19133. const_iterator find(KeyType && key) const
  19134. {
  19135. auto result = cend();
  19136. if (is_object())
  19137. {
  19138. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19139. }
  19140. return result;
  19141. }
  19142. /// @brief returns the number of occurrences of a key in a JSON object
  19143. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19144. size_type count(const typename object_t::key_type& key) const
  19145. {
  19146. // return 0 for all nonobject types
  19147. return is_object() ? m_data.m_value.object->count(key) : 0;
  19148. }
  19149. /// @brief returns the number of occurrences of a key in a JSON object
  19150. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19151. template<class KeyType, detail::enable_if_t<
  19152. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19153. size_type count(KeyType && key) const
  19154. {
  19155. // return 0 for all nonobject types
  19156. return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
  19157. }
  19158. /// @brief check the existence of an element in a JSON object
  19159. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19160. bool contains(const typename object_t::key_type& key) const
  19161. {
  19162. return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
  19163. }
  19164. /// @brief check the existence of an element in a JSON object
  19165. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19166. template<class KeyType, detail::enable_if_t<
  19167. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19168. bool contains(KeyType && key) const
  19169. {
  19170. return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
  19171. }
  19172. /// @brief check the existence of an element in a JSON object given a JSON pointer
  19173. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19174. bool contains(const json_pointer& ptr) const
  19175. {
  19176. return ptr.contains(this);
  19177. }
  19178. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  19179. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19180. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  19181. {
  19182. return ptr.contains(this);
  19183. }
  19184. /// @}
  19185. ///////////////
  19186. // iterators //
  19187. ///////////////
  19188. /// @name iterators
  19189. /// @{
  19190. /// @brief returns an iterator to the first element
  19191. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19192. iterator begin() noexcept
  19193. {
  19194. iterator result(this);
  19195. result.set_begin();
  19196. return result;
  19197. }
  19198. /// @brief returns an iterator to the first element
  19199. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19200. const_iterator begin() const noexcept
  19201. {
  19202. return cbegin();
  19203. }
  19204. /// @brief returns a const iterator to the first element
  19205. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  19206. const_iterator cbegin() const noexcept
  19207. {
  19208. const_iterator result(this);
  19209. result.set_begin();
  19210. return result;
  19211. }
  19212. /// @brief returns an iterator to one past the last element
  19213. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19214. iterator end() noexcept
  19215. {
  19216. iterator result(this);
  19217. result.set_end();
  19218. return result;
  19219. }
  19220. /// @brief returns an iterator to one past the last element
  19221. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19222. const_iterator end() const noexcept
  19223. {
  19224. return cend();
  19225. }
  19226. /// @brief returns an iterator to one past the last element
  19227. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  19228. const_iterator cend() const noexcept
  19229. {
  19230. const_iterator result(this);
  19231. result.set_end();
  19232. return result;
  19233. }
  19234. /// @brief returns an iterator to the reverse-beginning
  19235. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19236. reverse_iterator rbegin() noexcept
  19237. {
  19238. return reverse_iterator(end());
  19239. }
  19240. /// @brief returns an iterator to the reverse-beginning
  19241. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19242. const_reverse_iterator rbegin() const noexcept
  19243. {
  19244. return crbegin();
  19245. }
  19246. /// @brief returns an iterator to the reverse-end
  19247. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19248. reverse_iterator rend() noexcept
  19249. {
  19250. return reverse_iterator(begin());
  19251. }
  19252. /// @brief returns an iterator to the reverse-end
  19253. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19254. const_reverse_iterator rend() const noexcept
  19255. {
  19256. return crend();
  19257. }
  19258. /// @brief returns a const reverse iterator to the last element
  19259. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  19260. const_reverse_iterator crbegin() const noexcept
  19261. {
  19262. return const_reverse_iterator(cend());
  19263. }
  19264. /// @brief returns a const reverse iterator to one before the first
  19265. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  19266. const_reverse_iterator crend() const noexcept
  19267. {
  19268. return const_reverse_iterator(cbegin());
  19269. }
  19270. public:
  19271. /// @brief wrapper to access iterator member functions in range-based for
  19272. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19273. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19274. /// version 4.0.0 of the library. Please use @ref items() instead;
  19275. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19276. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19277. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19278. {
  19279. return ref.items();
  19280. }
  19281. /// @brief wrapper to access iterator member functions in range-based for
  19282. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19283. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19284. /// version 4.0.0 of the library. Please use @ref items() instead;
  19285. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19286. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19287. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19288. {
  19289. return ref.items();
  19290. }
  19291. /// @brief helper to access iterator member functions in range-based for
  19292. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19293. iteration_proxy<iterator> items() noexcept
  19294. {
  19295. return iteration_proxy<iterator>(*this);
  19296. }
  19297. /// @brief helper to access iterator member functions in range-based for
  19298. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19299. iteration_proxy<const_iterator> items() const noexcept
  19300. {
  19301. return iteration_proxy<const_iterator>(*this);
  19302. }
  19303. /// @}
  19304. //////////////
  19305. // capacity //
  19306. //////////////
  19307. /// @name capacity
  19308. /// @{
  19309. /// @brief checks whether the container is empty.
  19310. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  19311. bool empty() const noexcept
  19312. {
  19313. switch (m_data.m_type)
  19314. {
  19315. case value_t::null:
  19316. {
  19317. // null values are empty
  19318. return true;
  19319. }
  19320. case value_t::array:
  19321. {
  19322. // delegate call to array_t::empty()
  19323. return m_data.m_value.array->empty();
  19324. }
  19325. case value_t::object:
  19326. {
  19327. // delegate call to object_t::empty()
  19328. return m_data.m_value.object->empty();
  19329. }
  19330. case value_t::string:
  19331. case value_t::boolean:
  19332. case value_t::number_integer:
  19333. case value_t::number_unsigned:
  19334. case value_t::number_float:
  19335. case value_t::binary:
  19336. case value_t::discarded:
  19337. default:
  19338. {
  19339. // all other types are nonempty
  19340. return false;
  19341. }
  19342. }
  19343. }
  19344. /// @brief returns the number of elements
  19345. /// @sa https://json.nlohmann.me/api/basic_json/size/
  19346. size_type size() const noexcept
  19347. {
  19348. switch (m_data.m_type)
  19349. {
  19350. case value_t::null:
  19351. {
  19352. // null values are empty
  19353. return 0;
  19354. }
  19355. case value_t::array:
  19356. {
  19357. // delegate call to array_t::size()
  19358. return m_data.m_value.array->size();
  19359. }
  19360. case value_t::object:
  19361. {
  19362. // delegate call to object_t::size()
  19363. return m_data.m_value.object->size();
  19364. }
  19365. case value_t::string:
  19366. case value_t::boolean:
  19367. case value_t::number_integer:
  19368. case value_t::number_unsigned:
  19369. case value_t::number_float:
  19370. case value_t::binary:
  19371. case value_t::discarded:
  19372. default:
  19373. {
  19374. // all other types have size 1
  19375. return 1;
  19376. }
  19377. }
  19378. }
  19379. /// @brief returns the maximum possible number of elements
  19380. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  19381. size_type max_size() const noexcept
  19382. {
  19383. switch (m_data.m_type)
  19384. {
  19385. case value_t::array:
  19386. {
  19387. // delegate call to array_t::max_size()
  19388. return m_data.m_value.array->max_size();
  19389. }
  19390. case value_t::object:
  19391. {
  19392. // delegate call to object_t::max_size()
  19393. return m_data.m_value.object->max_size();
  19394. }
  19395. case value_t::null:
  19396. case value_t::string:
  19397. case value_t::boolean:
  19398. case value_t::number_integer:
  19399. case value_t::number_unsigned:
  19400. case value_t::number_float:
  19401. case value_t::binary:
  19402. case value_t::discarded:
  19403. default:
  19404. {
  19405. // all other types have max_size() == size()
  19406. return size();
  19407. }
  19408. }
  19409. }
  19410. /// @}
  19411. ///////////////
  19412. // modifiers //
  19413. ///////////////
  19414. /// @name modifiers
  19415. /// @{
  19416. /// @brief clears the contents
  19417. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  19418. void clear() noexcept
  19419. {
  19420. switch (m_data.m_type)
  19421. {
  19422. case value_t::number_integer:
  19423. {
  19424. m_data.m_value.number_integer = 0;
  19425. break;
  19426. }
  19427. case value_t::number_unsigned:
  19428. {
  19429. m_data.m_value.number_unsigned = 0;
  19430. break;
  19431. }
  19432. case value_t::number_float:
  19433. {
  19434. m_data.m_value.number_float = 0.0;
  19435. break;
  19436. }
  19437. case value_t::boolean:
  19438. {
  19439. m_data.m_value.boolean = false;
  19440. break;
  19441. }
  19442. case value_t::string:
  19443. {
  19444. m_data.m_value.string->clear();
  19445. break;
  19446. }
  19447. case value_t::binary:
  19448. {
  19449. m_data.m_value.binary->clear();
  19450. break;
  19451. }
  19452. case value_t::array:
  19453. {
  19454. m_data.m_value.array->clear();
  19455. break;
  19456. }
  19457. case value_t::object:
  19458. {
  19459. m_data.m_value.object->clear();
  19460. break;
  19461. }
  19462. case value_t::null:
  19463. case value_t::discarded:
  19464. default:
  19465. break;
  19466. }
  19467. }
  19468. /// @brief add an object to an array
  19469. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19470. void push_back(basic_json&& val)
  19471. {
  19472. // push_back only works for null objects or arrays
  19473. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19474. {
  19475. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19476. }
  19477. // transform null object into an array
  19478. if (is_null())
  19479. {
  19480. m_data.m_type = value_t::array;
  19481. m_data.m_value = value_t::array;
  19482. assert_invariant();
  19483. }
  19484. // add element to array (move semantics)
  19485. const auto old_capacity = m_data.m_value.array->capacity();
  19486. m_data.m_value.array->push_back(std::move(val));
  19487. set_parent(m_data.m_value.array->back(), old_capacity);
  19488. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  19489. }
  19490. /// @brief add an object to an array
  19491. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19492. reference operator+=(basic_json&& val)
  19493. {
  19494. push_back(std::move(val));
  19495. return *this;
  19496. }
  19497. /// @brief add an object to an array
  19498. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19499. void push_back(const basic_json& val)
  19500. {
  19501. // push_back only works for null objects or arrays
  19502. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19503. {
  19504. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19505. }
  19506. // transform null object into an array
  19507. if (is_null())
  19508. {
  19509. m_data.m_type = value_t::array;
  19510. m_data.m_value = value_t::array;
  19511. assert_invariant();
  19512. }
  19513. // add element to array
  19514. const auto old_capacity = m_data.m_value.array->capacity();
  19515. m_data.m_value.array->push_back(val);
  19516. set_parent(m_data.m_value.array->back(), old_capacity);
  19517. }
  19518. /// @brief add an object to an array
  19519. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19520. reference operator+=(const basic_json& val)
  19521. {
  19522. push_back(val);
  19523. return *this;
  19524. }
  19525. /// @brief add an object to an object
  19526. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19527. void push_back(const typename object_t::value_type& val)
  19528. {
  19529. // push_back only works for null objects or objects
  19530. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19531. {
  19532. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19533. }
  19534. // transform null object into an object
  19535. if (is_null())
  19536. {
  19537. m_data.m_type = value_t::object;
  19538. m_data.m_value = value_t::object;
  19539. assert_invariant();
  19540. }
  19541. // add element to object
  19542. auto res = m_data.m_value.object->insert(val);
  19543. set_parent(res.first->second);
  19544. }
  19545. /// @brief add an object to an object
  19546. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19547. reference operator+=(const typename object_t::value_type& val)
  19548. {
  19549. push_back(val);
  19550. return *this;
  19551. }
  19552. /// @brief add an object to an object
  19553. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19554. void push_back(initializer_list_t init)
  19555. {
  19556. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  19557. {
  19558. basic_json&& key = init.begin()->moved_or_copied();
  19559. push_back(typename object_t::value_type(
  19560. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  19561. }
  19562. else
  19563. {
  19564. push_back(basic_json(init));
  19565. }
  19566. }
  19567. /// @brief add an object to an object
  19568. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19569. reference operator+=(initializer_list_t init)
  19570. {
  19571. push_back(init);
  19572. return *this;
  19573. }
  19574. /// @brief add an object to an array
  19575. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  19576. template<class... Args>
  19577. reference emplace_back(Args&& ... args)
  19578. {
  19579. // emplace_back only works for null objects or arrays
  19580. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19581. {
  19582. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  19583. }
  19584. // transform null object into an array
  19585. if (is_null())
  19586. {
  19587. m_data.m_type = value_t::array;
  19588. m_data.m_value = value_t::array;
  19589. assert_invariant();
  19590. }
  19591. // add element to array (perfect forwarding)
  19592. const auto old_capacity = m_data.m_value.array->capacity();
  19593. m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
  19594. return set_parent(m_data.m_value.array->back(), old_capacity);
  19595. }
  19596. /// @brief add an object to an object if key does not exist
  19597. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  19598. template<class... Args>
  19599. std::pair<iterator, bool> emplace(Args&& ... args)
  19600. {
  19601. // emplace only works for null objects or arrays
  19602. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19603. {
  19604. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  19605. }
  19606. // transform null object into an object
  19607. if (is_null())
  19608. {
  19609. m_data.m_type = value_t::object;
  19610. m_data.m_value = value_t::object;
  19611. assert_invariant();
  19612. }
  19613. // add element to array (perfect forwarding)
  19614. auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
  19615. set_parent(res.first->second);
  19616. // create result iterator and set iterator to the result of emplace
  19617. auto it = begin();
  19618. it.m_it.object_iterator = res.first;
  19619. // return pair of iterator and boolean
  19620. return {it, res.second};
  19621. }
  19622. /// Helper for insertion of an iterator
  19623. /// @note: This uses std::distance to support GCC 4.8,
  19624. /// see https://github.com/nlohmann/json/pull/1257
  19625. template<typename... Args>
  19626. iterator insert_iterator(const_iterator pos, Args&& ... args)
  19627. {
  19628. iterator result(this);
  19629. JSON_ASSERT(m_data.m_value.array != nullptr);
  19630. auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
  19631. m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  19632. result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
  19633. // This could have been written as:
  19634. // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  19635. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  19636. set_parents();
  19637. return result;
  19638. }
  19639. /// @brief inserts element into array
  19640. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19641. iterator insert(const_iterator pos, const basic_json& val)
  19642. {
  19643. // insert only works for arrays
  19644. if (JSON_HEDLEY_LIKELY(is_array()))
  19645. {
  19646. // check if iterator pos fits to this JSON value
  19647. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19648. {
  19649. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19650. }
  19651. // insert to array and return iterator
  19652. return insert_iterator(pos, val);
  19653. }
  19654. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19655. }
  19656. /// @brief inserts element into array
  19657. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19658. iterator insert(const_iterator pos, basic_json&& val)
  19659. {
  19660. return insert(pos, val);
  19661. }
  19662. /// @brief inserts copies of element into array
  19663. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19664. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  19665. {
  19666. // insert only works for arrays
  19667. if (JSON_HEDLEY_LIKELY(is_array()))
  19668. {
  19669. // check if iterator pos fits to this JSON value
  19670. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19671. {
  19672. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19673. }
  19674. // insert to array and return iterator
  19675. return insert_iterator(pos, cnt, val);
  19676. }
  19677. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19678. }
  19679. /// @brief inserts range of elements into array
  19680. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19681. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  19682. {
  19683. // insert only works for arrays
  19684. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19685. {
  19686. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19687. }
  19688. // check if iterator pos fits to this JSON value
  19689. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19690. {
  19691. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19692. }
  19693. // check if range iterators belong to the same JSON object
  19694. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19695. {
  19696. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19697. }
  19698. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  19699. {
  19700. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  19701. }
  19702. // insert to array and return iterator
  19703. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  19704. }
  19705. /// @brief inserts elements from initializer list into array
  19706. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19707. iterator insert(const_iterator pos, initializer_list_t ilist)
  19708. {
  19709. // insert only works for arrays
  19710. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19711. {
  19712. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19713. }
  19714. // check if iterator pos fits to this JSON value
  19715. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19716. {
  19717. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19718. }
  19719. // insert to array and return iterator
  19720. return insert_iterator(pos, ilist.begin(), ilist.end());
  19721. }
  19722. /// @brief inserts range of elements into object
  19723. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19724. void insert(const_iterator first, const_iterator last)
  19725. {
  19726. // insert only works for objects
  19727. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19728. {
  19729. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19730. }
  19731. // check if range iterators belong to the same JSON object
  19732. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19733. {
  19734. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19735. }
  19736. // passed iterators must belong to objects
  19737. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19738. {
  19739. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  19740. }
  19741. m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  19742. }
  19743. /// @brief updates a JSON object from another object, overwriting existing keys
  19744. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19745. void update(const_reference j, bool merge_objects = false)
  19746. {
  19747. update(j.begin(), j.end(), merge_objects);
  19748. }
  19749. /// @brief updates a JSON object from another object, overwriting existing keys
  19750. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19751. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  19752. {
  19753. // implicitly convert null value to an empty object
  19754. if (is_null())
  19755. {
  19756. m_data.m_type = value_t::object;
  19757. m_data.m_value.object = create<object_t>();
  19758. assert_invariant();
  19759. }
  19760. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19761. {
  19762. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  19763. }
  19764. // check if range iterators belong to the same JSON object
  19765. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19766. {
  19767. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19768. }
  19769. // passed iterators must belong to objects
  19770. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19771. {
  19772. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  19773. }
  19774. for (auto it = first; it != last; ++it)
  19775. {
  19776. if (merge_objects && it.value().is_object())
  19777. {
  19778. auto it2 = m_data.m_value.object->find(it.key());
  19779. if (it2 != m_data.m_value.object->end())
  19780. {
  19781. it2->second.update(it.value(), true);
  19782. continue;
  19783. }
  19784. }
  19785. m_data.m_value.object->operator[](it.key()) = it.value();
  19786. #if JSON_DIAGNOSTICS
  19787. m_data.m_value.object->operator[](it.key()).m_parent = this;
  19788. #endif
  19789. }
  19790. }
  19791. /// @brief exchanges the values
  19792. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19793. void swap(reference other) noexcept (
  19794. std::is_nothrow_move_constructible<value_t>::value&&
  19795. std::is_nothrow_move_assignable<value_t>::value&&
  19796. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19797. std::is_nothrow_move_assignable<json_value>::value
  19798. )
  19799. {
  19800. std::swap(m_data.m_type, other.m_data.m_type);
  19801. std::swap(m_data.m_value, other.m_data.m_value);
  19802. set_parents();
  19803. other.set_parents();
  19804. assert_invariant();
  19805. }
  19806. /// @brief exchanges the values
  19807. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19808. friend void swap(reference left, reference right) noexcept (
  19809. std::is_nothrow_move_constructible<value_t>::value&&
  19810. std::is_nothrow_move_assignable<value_t>::value&&
  19811. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19812. std::is_nothrow_move_assignable<json_value>::value
  19813. )
  19814. {
  19815. left.swap(right);
  19816. }
  19817. /// @brief exchanges the values
  19818. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19819. void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19820. {
  19821. // swap only works for arrays
  19822. if (JSON_HEDLEY_LIKELY(is_array()))
  19823. {
  19824. using std::swap;
  19825. swap(*(m_data.m_value.array), other);
  19826. }
  19827. else
  19828. {
  19829. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  19830. }
  19831. }
  19832. /// @brief exchanges the values
  19833. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19834. void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19835. {
  19836. // swap only works for objects
  19837. if (JSON_HEDLEY_LIKELY(is_object()))
  19838. {
  19839. using std::swap;
  19840. swap(*(m_data.m_value.object), other);
  19841. }
  19842. else
  19843. {
  19844. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  19845. }
  19846. }
  19847. /// @brief exchanges the values
  19848. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19849. void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19850. {
  19851. // swap only works for strings
  19852. if (JSON_HEDLEY_LIKELY(is_string()))
  19853. {
  19854. using std::swap;
  19855. swap(*(m_data.m_value.string), other);
  19856. }
  19857. else
  19858. {
  19859. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  19860. }
  19861. }
  19862. /// @brief exchanges the values
  19863. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19864. void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19865. {
  19866. // swap only works for strings
  19867. if (JSON_HEDLEY_LIKELY(is_binary()))
  19868. {
  19869. using std::swap;
  19870. swap(*(m_data.m_value.binary), other);
  19871. }
  19872. else
  19873. {
  19874. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  19875. }
  19876. }
  19877. /// @brief exchanges the values
  19878. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19879. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  19880. {
  19881. // swap only works for strings
  19882. if (JSON_HEDLEY_LIKELY(is_binary()))
  19883. {
  19884. using std::swap;
  19885. swap(*(m_data.m_value.binary), other);
  19886. }
  19887. else
  19888. {
  19889. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  19890. }
  19891. }
  19892. /// @}
  19893. //////////////////////////////////////////
  19894. // lexicographical comparison operators //
  19895. //////////////////////////////////////////
  19896. /// @name lexicographical comparison operators
  19897. /// @{
  19898. // note parentheses around operands are necessary; see
  19899. // https://github.com/nlohmann/json/issues/1530
  19900. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  19901. const auto lhs_type = lhs.type(); \
  19902. const auto rhs_type = rhs.type(); \
  19903. \
  19904. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  19905. { \
  19906. switch (lhs_type) \
  19907. { \
  19908. case value_t::array: \
  19909. return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
  19910. \
  19911. case value_t::object: \
  19912. return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
  19913. \
  19914. case value_t::null: \
  19915. return (null_result); \
  19916. \
  19917. case value_t::string: \
  19918. return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
  19919. \
  19920. case value_t::boolean: \
  19921. return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
  19922. \
  19923. case value_t::number_integer: \
  19924. return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
  19925. \
  19926. case value_t::number_unsigned: \
  19927. return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
  19928. \
  19929. case value_t::number_float: \
  19930. return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
  19931. \
  19932. case value_t::binary: \
  19933. return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
  19934. \
  19935. case value_t::discarded: \
  19936. default: \
  19937. return (unordered_result); \
  19938. } \
  19939. } \
  19940. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  19941. { \
  19942. return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
  19943. } \
  19944. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  19945. { \
  19946. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
  19947. } \
  19948. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  19949. { \
  19950. return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
  19951. } \
  19952. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  19953. { \
  19954. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
  19955. } \
  19956. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  19957. { \
  19958. return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
  19959. } \
  19960. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  19961. { \
  19962. return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
  19963. } \
  19964. else if(compares_unordered(lhs, rhs))\
  19965. {\
  19966. return (unordered_result);\
  19967. }\
  19968. \
  19969. return (default_result);
  19970. JSON_PRIVATE_UNLESS_TESTED:
  19971. // returns true if:
  19972. // - any operand is NaN and the other operand is of number type
  19973. // - any operand is discarded
  19974. // in legacy mode, discarded values are considered ordered if
  19975. // an operation is computed as an odd number of inverses of others
  19976. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  19977. {
  19978. if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
  19979. || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
  19980. {
  19981. return true;
  19982. }
  19983. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19984. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  19985. #else
  19986. static_cast<void>(inverse);
  19987. return lhs.is_discarded() || rhs.is_discarded();
  19988. #endif
  19989. }
  19990. private:
  19991. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  19992. {
  19993. return compares_unordered(*this, rhs, inverse);
  19994. }
  19995. public:
  19996. #if JSON_HAS_THREE_WAY_COMPARISON
  19997. /// @brief comparison: equal
  19998. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19999. bool operator==(const_reference rhs) const noexcept
  20000. {
  20001. #ifdef __GNUC__
  20002. #pragma GCC diagnostic push
  20003. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20004. #endif
  20005. const_reference lhs = *this;
  20006. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20007. #ifdef __GNUC__
  20008. #pragma GCC diagnostic pop
  20009. #endif
  20010. }
  20011. /// @brief comparison: equal
  20012. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20013. template<typename ScalarType>
  20014. requires std::is_scalar_v<ScalarType>
  20015. bool operator==(ScalarType rhs) const noexcept
  20016. {
  20017. return *this == basic_json(rhs);
  20018. }
  20019. /// @brief comparison: not equal
  20020. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20021. bool operator!=(const_reference rhs) const noexcept
  20022. {
  20023. if (compares_unordered(rhs, true))
  20024. {
  20025. return false;
  20026. }
  20027. return !operator==(rhs);
  20028. }
  20029. /// @brief comparison: 3-way
  20030. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20031. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  20032. {
  20033. const_reference lhs = *this;
  20034. // default_result is used if we cannot compare values. In that case,
  20035. // we compare types.
  20036. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  20037. std::partial_ordering::equivalent,
  20038. std::partial_ordering::unordered,
  20039. lhs_type <=> rhs_type) // *NOPAD*
  20040. }
  20041. /// @brief comparison: 3-way
  20042. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  20043. template<typename ScalarType>
  20044. requires std::is_scalar_v<ScalarType>
  20045. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  20046. {
  20047. return *this <=> basic_json(rhs); // *NOPAD*
  20048. }
  20049. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20050. // all operators that are computed as an odd number of inverses of others
  20051. // need to be overloaded to emulate the legacy comparison behavior
  20052. /// @brief comparison: less than or equal
  20053. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20054. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20055. bool operator<=(const_reference rhs) const noexcept
  20056. {
  20057. if (compares_unordered(rhs, true))
  20058. {
  20059. return false;
  20060. }
  20061. return !(rhs < *this);
  20062. }
  20063. /// @brief comparison: less than or equal
  20064. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20065. template<typename ScalarType>
  20066. requires std::is_scalar_v<ScalarType>
  20067. bool operator<=(ScalarType rhs) const noexcept
  20068. {
  20069. return *this <= basic_json(rhs);
  20070. }
  20071. /// @brief comparison: greater than or equal
  20072. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20073. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20074. bool operator>=(const_reference rhs) const noexcept
  20075. {
  20076. if (compares_unordered(rhs, true))
  20077. {
  20078. return false;
  20079. }
  20080. return !(*this < rhs);
  20081. }
  20082. /// @brief comparison: greater than or equal
  20083. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20084. template<typename ScalarType>
  20085. requires std::is_scalar_v<ScalarType>
  20086. bool operator>=(ScalarType rhs) const noexcept
  20087. {
  20088. return *this >= basic_json(rhs);
  20089. }
  20090. #endif
  20091. #else
  20092. /// @brief comparison: equal
  20093. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20094. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20095. {
  20096. #ifdef __GNUC__
  20097. #pragma GCC diagnostic push
  20098. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20099. #endif
  20100. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20101. #ifdef __GNUC__
  20102. #pragma GCC diagnostic pop
  20103. #endif
  20104. }
  20105. /// @brief comparison: equal
  20106. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20107. template<typename ScalarType, typename std::enable_if<
  20108. std::is_scalar<ScalarType>::value, int>::type = 0>
  20109. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20110. {
  20111. return lhs == basic_json(rhs);
  20112. }
  20113. /// @brief comparison: equal
  20114. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20115. template<typename ScalarType, typename std::enable_if<
  20116. std::is_scalar<ScalarType>::value, int>::type = 0>
  20117. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20118. {
  20119. return basic_json(lhs) == rhs;
  20120. }
  20121. /// @brief comparison: not equal
  20122. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20123. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20124. {
  20125. if (compares_unordered(lhs, rhs, true))
  20126. {
  20127. return false;
  20128. }
  20129. return !(lhs == rhs);
  20130. }
  20131. /// @brief comparison: not equal
  20132. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20133. template<typename ScalarType, typename std::enable_if<
  20134. std::is_scalar<ScalarType>::value, int>::type = 0>
  20135. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20136. {
  20137. return lhs != basic_json(rhs);
  20138. }
  20139. /// @brief comparison: not equal
  20140. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20141. template<typename ScalarType, typename std::enable_if<
  20142. std::is_scalar<ScalarType>::value, int>::type = 0>
  20143. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20144. {
  20145. return basic_json(lhs) != rhs;
  20146. }
  20147. /// @brief comparison: less than
  20148. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20149. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20150. {
  20151. // default_result is used if we cannot compare values. In that case,
  20152. // we compare types. Note we have to call the operator explicitly,
  20153. // because MSVC has problems otherwise.
  20154. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20155. }
  20156. /// @brief comparison: less than
  20157. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20158. template<typename ScalarType, typename std::enable_if<
  20159. std::is_scalar<ScalarType>::value, int>::type = 0>
  20160. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20161. {
  20162. return lhs < basic_json(rhs);
  20163. }
  20164. /// @brief comparison: less than
  20165. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20166. template<typename ScalarType, typename std::enable_if<
  20167. std::is_scalar<ScalarType>::value, int>::type = 0>
  20168. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20169. {
  20170. return basic_json(lhs) < rhs;
  20171. }
  20172. /// @brief comparison: less than or equal
  20173. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20174. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20175. {
  20176. if (compares_unordered(lhs, rhs, true))
  20177. {
  20178. return false;
  20179. }
  20180. return !(rhs < lhs);
  20181. }
  20182. /// @brief comparison: less than or equal
  20183. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20184. template<typename ScalarType, typename std::enable_if<
  20185. std::is_scalar<ScalarType>::value, int>::type = 0>
  20186. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20187. {
  20188. return lhs <= basic_json(rhs);
  20189. }
  20190. /// @brief comparison: less than or equal
  20191. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20192. template<typename ScalarType, typename std::enable_if<
  20193. std::is_scalar<ScalarType>::value, int>::type = 0>
  20194. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20195. {
  20196. return basic_json(lhs) <= rhs;
  20197. }
  20198. /// @brief comparison: greater than
  20199. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20200. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20201. {
  20202. // double inverse
  20203. if (compares_unordered(lhs, rhs))
  20204. {
  20205. return false;
  20206. }
  20207. return !(lhs <= rhs);
  20208. }
  20209. /// @brief comparison: greater than
  20210. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20211. template<typename ScalarType, typename std::enable_if<
  20212. std::is_scalar<ScalarType>::value, int>::type = 0>
  20213. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  20214. {
  20215. return lhs > basic_json(rhs);
  20216. }
  20217. /// @brief comparison: greater than
  20218. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20219. template<typename ScalarType, typename std::enable_if<
  20220. std::is_scalar<ScalarType>::value, int>::type = 0>
  20221. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  20222. {
  20223. return basic_json(lhs) > rhs;
  20224. }
  20225. /// @brief comparison: greater than or equal
  20226. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20227. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  20228. {
  20229. if (compares_unordered(lhs, rhs, true))
  20230. {
  20231. return false;
  20232. }
  20233. return !(lhs < rhs);
  20234. }
  20235. /// @brief comparison: greater than or equal
  20236. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20237. template<typename ScalarType, typename std::enable_if<
  20238. std::is_scalar<ScalarType>::value, int>::type = 0>
  20239. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  20240. {
  20241. return lhs >= basic_json(rhs);
  20242. }
  20243. /// @brief comparison: greater than or equal
  20244. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20245. template<typename ScalarType, typename std::enable_if<
  20246. std::is_scalar<ScalarType>::value, int>::type = 0>
  20247. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  20248. {
  20249. return basic_json(lhs) >= rhs;
  20250. }
  20251. #endif
  20252. #undef JSON_IMPLEMENT_OPERATOR
  20253. /// @}
  20254. ///////////////////
  20255. // serialization //
  20256. ///////////////////
  20257. /// @name serialization
  20258. /// @{
  20259. #ifndef JSON_NO_IO
  20260. /// @brief serialize to stream
  20261. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20262. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20263. {
  20264. // read width member and use it as indentation parameter if nonzero
  20265. const bool pretty_print = o.width() > 0;
  20266. const auto indentation = pretty_print ? o.width() : 0;
  20267. // reset width to 0 for subsequent calls to this stream
  20268. o.width(0);
  20269. // do the actual serialization
  20270. serializer s(detail::output_adapter<char>(o), o.fill());
  20271. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20272. return o;
  20273. }
  20274. /// @brief serialize to stream
  20275. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20276. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  20277. /// version 4.0.0 of the library. Please use
  20278. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  20279. /// replace calls like `j >> o;` with `o << j;`.
  20280. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20281. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20282. {
  20283. return o << j;
  20284. }
  20285. #endif // JSON_NO_IO
  20286. /// @}
  20287. /////////////////////
  20288. // deserialization //
  20289. /////////////////////
  20290. /// @name deserialization
  20291. /// @{
  20292. /// @brief deserialize from a compatible input
  20293. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20294. template<typename InputType>
  20295. JSON_HEDLEY_WARN_UNUSED_RESULT
  20296. static basic_json parse(InputType&& i,
  20297. const parser_callback_t cb = nullptr,
  20298. const bool allow_exceptions = true,
  20299. const bool ignore_comments = false)
  20300. {
  20301. basic_json result;
  20302. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20303. return result;
  20304. }
  20305. /// @brief deserialize from a pair of character iterators
  20306. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20307. template<typename IteratorType>
  20308. JSON_HEDLEY_WARN_UNUSED_RESULT
  20309. static basic_json parse(IteratorType first,
  20310. IteratorType last,
  20311. const parser_callback_t cb = nullptr,
  20312. const bool allow_exceptions = true,
  20313. const bool ignore_comments = false)
  20314. {
  20315. basic_json result;
  20316. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20317. return result;
  20318. }
  20319. JSON_HEDLEY_WARN_UNUSED_RESULT
  20320. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20321. static basic_json parse(detail::span_input_adapter&& i,
  20322. const parser_callback_t cb = nullptr,
  20323. const bool allow_exceptions = true,
  20324. const bool ignore_comments = false)
  20325. {
  20326. basic_json result;
  20327. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  20328. return result;
  20329. }
  20330. /// @brief check if the input is valid JSON
  20331. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20332. template<typename InputType>
  20333. static bool accept(InputType&& i,
  20334. const bool ignore_comments = false)
  20335. {
  20336. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20337. }
  20338. /// @brief check if the input is valid JSON
  20339. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20340. template<typename IteratorType>
  20341. static bool accept(IteratorType first, IteratorType last,
  20342. const bool ignore_comments = false)
  20343. {
  20344. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  20345. }
  20346. JSON_HEDLEY_WARN_UNUSED_RESULT
  20347. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  20348. static bool accept(detail::span_input_adapter&& i,
  20349. const bool ignore_comments = false)
  20350. {
  20351. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  20352. }
  20353. /// @brief generate SAX events
  20354. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20355. template <typename InputType, typename SAX>
  20356. JSON_HEDLEY_NON_NULL(2)
  20357. static bool sax_parse(InputType&& i, SAX* sax,
  20358. input_format_t format = input_format_t::json,
  20359. const bool strict = true,
  20360. const bool ignore_comments = false)
  20361. {
  20362. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20363. return format == input_format_t::json
  20364. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20365. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20366. }
  20367. /// @brief generate SAX events
  20368. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20369. template<class IteratorType, class SAX>
  20370. JSON_HEDLEY_NON_NULL(3)
  20371. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  20372. input_format_t format = input_format_t::json,
  20373. const bool strict = true,
  20374. const bool ignore_comments = false)
  20375. {
  20376. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20377. return format == input_format_t::json
  20378. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20379. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20380. }
  20381. /// @brief generate SAX events
  20382. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20383. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  20384. /// version 4.0.0 of the library. Please use
  20385. /// sax_parse(ptr, ptr + len) instead.
  20386. template <typename SAX>
  20387. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  20388. JSON_HEDLEY_NON_NULL(2)
  20389. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  20390. input_format_t format = input_format_t::json,
  20391. const bool strict = true,
  20392. const bool ignore_comments = false)
  20393. {
  20394. auto ia = i.get();
  20395. return format == input_format_t::json
  20396. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20397. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20398. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20399. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20400. }
  20401. #ifndef JSON_NO_IO
  20402. /// @brief deserialize from stream
  20403. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20404. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  20405. /// version 4.0.0 of the library. Please use
  20406. /// operator>>(std::istream&, basic_json&) instead; that is,
  20407. /// replace calls like `j << i;` with `i >> j;`.
  20408. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  20409. friend std::istream& operator<<(basic_json& j, std::istream& i)
  20410. {
  20411. return operator>>(i, j);
  20412. }
  20413. /// @brief deserialize from stream
  20414. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20415. friend std::istream& operator>>(std::istream& i, basic_json& j)
  20416. {
  20417. parser(detail::input_adapter(i)).parse(false, j);
  20418. return i;
  20419. }
  20420. #endif // JSON_NO_IO
  20421. /// @}
  20422. ///////////////////////////
  20423. // convenience functions //
  20424. ///////////////////////////
  20425. /// @brief return the type as string
  20426. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  20427. JSON_HEDLEY_RETURNS_NON_NULL
  20428. const char* type_name() const noexcept
  20429. {
  20430. switch (m_data.m_type)
  20431. {
  20432. case value_t::null:
  20433. return "null";
  20434. case value_t::object:
  20435. return "object";
  20436. case value_t::array:
  20437. return "array";
  20438. case value_t::string:
  20439. return "string";
  20440. case value_t::boolean:
  20441. return "boolean";
  20442. case value_t::binary:
  20443. return "binary";
  20444. case value_t::discarded:
  20445. return "discarded";
  20446. case value_t::number_integer:
  20447. case value_t::number_unsigned:
  20448. case value_t::number_float:
  20449. default:
  20450. return "number";
  20451. }
  20452. }
  20453. JSON_PRIVATE_UNLESS_TESTED:
  20454. //////////////////////
  20455. // member variables //
  20456. //////////////////////
  20457. struct data
  20458. {
  20459. /// the type of the current element
  20460. value_t m_type = value_t::null;
  20461. /// the value of the current element
  20462. json_value m_value = {};
  20463. data(const value_t v)
  20464. : m_type(v), m_value(v)
  20465. {
  20466. }
  20467. data(size_type cnt, const basic_json& val)
  20468. : m_type(value_t::array)
  20469. {
  20470. m_value.array = create<array_t>(cnt, val);
  20471. }
  20472. data() noexcept = default;
  20473. data(data&&) noexcept = default;
  20474. data(const data&) noexcept = delete;
  20475. data& operator=(data&&) noexcept = delete;
  20476. data& operator=(const data&) noexcept = delete;
  20477. ~data() noexcept
  20478. {
  20479. m_value.destroy(m_type);
  20480. }
  20481. };
  20482. data m_data = {};
  20483. #if JSON_DIAGNOSTICS
  20484. /// a pointer to a parent value (for debugging purposes)
  20485. basic_json* m_parent = nullptr;
  20486. #endif
  20487. //////////////////////////////////////////
  20488. // binary serialization/deserialization //
  20489. //////////////////////////////////////////
  20490. /// @name binary serialization/deserialization support
  20491. /// @{
  20492. public:
  20493. /// @brief create a CBOR serialization of a given JSON value
  20494. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20495. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  20496. {
  20497. std::vector<std::uint8_t> result;
  20498. to_cbor(j, result);
  20499. return result;
  20500. }
  20501. /// @brief create a CBOR serialization of a given JSON value
  20502. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20503. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20504. {
  20505. binary_writer<std::uint8_t>(o).write_cbor(j);
  20506. }
  20507. /// @brief create a CBOR serialization of a given JSON value
  20508. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20509. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  20510. {
  20511. binary_writer<char>(o).write_cbor(j);
  20512. }
  20513. /// @brief create a MessagePack serialization of a given JSON value
  20514. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20515. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  20516. {
  20517. std::vector<std::uint8_t> result;
  20518. to_msgpack(j, result);
  20519. return result;
  20520. }
  20521. /// @brief create a MessagePack serialization of a given JSON value
  20522. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20523. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20524. {
  20525. binary_writer<std::uint8_t>(o).write_msgpack(j);
  20526. }
  20527. /// @brief create a MessagePack serialization of a given JSON value
  20528. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20529. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  20530. {
  20531. binary_writer<char>(o).write_msgpack(j);
  20532. }
  20533. /// @brief create a UBJSON serialization of a given JSON value
  20534. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20535. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  20536. const bool use_size = false,
  20537. const bool use_type = false)
  20538. {
  20539. std::vector<std::uint8_t> result;
  20540. to_ubjson(j, result, use_size, use_type);
  20541. return result;
  20542. }
  20543. /// @brief create a UBJSON serialization of a given JSON value
  20544. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20545. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20546. const bool use_size = false, const bool use_type = false)
  20547. {
  20548. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  20549. }
  20550. /// @brief create a UBJSON serialization of a given JSON value
  20551. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20552. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  20553. const bool use_size = false, const bool use_type = false)
  20554. {
  20555. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  20556. }
  20557. /// @brief create a BJData serialization of a given JSON value
  20558. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20559. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  20560. const bool use_size = false,
  20561. const bool use_type = false)
  20562. {
  20563. std::vector<std::uint8_t> result;
  20564. to_bjdata(j, result, use_size, use_type);
  20565. return result;
  20566. }
  20567. /// @brief create a BJData serialization of a given JSON value
  20568. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20569. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20570. const bool use_size = false, const bool use_type = false)
  20571. {
  20572. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  20573. }
  20574. /// @brief create a BJData serialization of a given JSON value
  20575. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20576. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  20577. const bool use_size = false, const bool use_type = false)
  20578. {
  20579. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  20580. }
  20581. /// @brief create a BSON serialization of a given JSON value
  20582. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20583. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  20584. {
  20585. std::vector<std::uint8_t> result;
  20586. to_bson(j, result);
  20587. return result;
  20588. }
  20589. /// @brief create a BSON serialization of a given JSON value
  20590. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20591. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20592. {
  20593. binary_writer<std::uint8_t>(o).write_bson(j);
  20594. }
  20595. /// @brief create a BSON serialization of a given JSON value
  20596. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20597. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  20598. {
  20599. binary_writer<char>(o).write_bson(j);
  20600. }
  20601. /// @brief create a JSON value from an input in CBOR format
  20602. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20603. template<typename InputType>
  20604. JSON_HEDLEY_WARN_UNUSED_RESULT
  20605. static basic_json from_cbor(InputType&& i,
  20606. const bool strict = true,
  20607. const bool allow_exceptions = true,
  20608. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20609. {
  20610. basic_json result;
  20611. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20612. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20613. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20614. return res ? result : basic_json(value_t::discarded);
  20615. }
  20616. /// @brief create a JSON value from an input in CBOR format
  20617. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20618. template<typename IteratorType>
  20619. JSON_HEDLEY_WARN_UNUSED_RESULT
  20620. static basic_json from_cbor(IteratorType first, IteratorType last,
  20621. const bool strict = true,
  20622. const bool allow_exceptions = true,
  20623. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20624. {
  20625. basic_json result;
  20626. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20627. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20628. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20629. return res ? result : basic_json(value_t::discarded);
  20630. }
  20631. template<typename T>
  20632. JSON_HEDLEY_WARN_UNUSED_RESULT
  20633. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20634. static basic_json from_cbor(const T* ptr, std::size_t len,
  20635. const bool strict = true,
  20636. const bool allow_exceptions = true,
  20637. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20638. {
  20639. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  20640. }
  20641. JSON_HEDLEY_WARN_UNUSED_RESULT
  20642. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20643. static basic_json from_cbor(detail::span_input_adapter&& i,
  20644. const bool strict = true,
  20645. const bool allow_exceptions = true,
  20646. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20647. {
  20648. basic_json result;
  20649. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20650. auto ia = i.get();
  20651. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20652. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20653. return res ? result : basic_json(value_t::discarded);
  20654. }
  20655. /// @brief create a JSON value from an input in MessagePack format
  20656. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20657. template<typename InputType>
  20658. JSON_HEDLEY_WARN_UNUSED_RESULT
  20659. static basic_json from_msgpack(InputType&& i,
  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::forward<InputType>(i));
  20666. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20667. return res ? result : basic_json(value_t::discarded);
  20668. }
  20669. /// @brief create a JSON value from an input in MessagePack format
  20670. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20671. template<typename IteratorType>
  20672. JSON_HEDLEY_WARN_UNUSED_RESULT
  20673. static basic_json from_msgpack(IteratorType first, IteratorType last,
  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::move(first), std::move(last));
  20680. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20681. return res ? result : basic_json(value_t::discarded);
  20682. }
  20683. template<typename T>
  20684. JSON_HEDLEY_WARN_UNUSED_RESULT
  20685. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20686. static basic_json from_msgpack(const T* ptr, std::size_t len,
  20687. const bool strict = true,
  20688. const bool allow_exceptions = true)
  20689. {
  20690. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  20691. }
  20692. JSON_HEDLEY_WARN_UNUSED_RESULT
  20693. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20694. static basic_json from_msgpack(detail::span_input_adapter&& i,
  20695. const bool strict = true,
  20696. const bool allow_exceptions = true)
  20697. {
  20698. basic_json result;
  20699. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20700. auto ia = i.get();
  20701. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20702. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20703. return res ? result : basic_json(value_t::discarded);
  20704. }
  20705. /// @brief create a JSON value from an input in UBJSON format
  20706. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20707. template<typename InputType>
  20708. JSON_HEDLEY_WARN_UNUSED_RESULT
  20709. static basic_json from_ubjson(InputType&& i,
  20710. const bool strict = true,
  20711. const bool allow_exceptions = true)
  20712. {
  20713. basic_json result;
  20714. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20715. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20716. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20717. return res ? result : basic_json(value_t::discarded);
  20718. }
  20719. /// @brief create a JSON value from an input in UBJSON format
  20720. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20721. template<typename IteratorType>
  20722. JSON_HEDLEY_WARN_UNUSED_RESULT
  20723. static basic_json from_ubjson(IteratorType first, IteratorType last,
  20724. const bool strict = true,
  20725. const bool allow_exceptions = true)
  20726. {
  20727. basic_json result;
  20728. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20729. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20730. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20731. return res ? result : basic_json(value_t::discarded);
  20732. }
  20733. template<typename T>
  20734. JSON_HEDLEY_WARN_UNUSED_RESULT
  20735. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20736. static basic_json from_ubjson(const T* ptr, std::size_t len,
  20737. const bool strict = true,
  20738. const bool allow_exceptions = true)
  20739. {
  20740. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  20741. }
  20742. JSON_HEDLEY_WARN_UNUSED_RESULT
  20743. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20744. static basic_json from_ubjson(detail::span_input_adapter&& i,
  20745. const bool strict = true,
  20746. const bool allow_exceptions = true)
  20747. {
  20748. basic_json result;
  20749. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20750. auto ia = i.get();
  20751. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20752. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20753. return res ? result : basic_json(value_t::discarded);
  20754. }
  20755. /// @brief create a JSON value from an input in BJData format
  20756. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20757. template<typename InputType>
  20758. JSON_HEDLEY_WARN_UNUSED_RESULT
  20759. static basic_json from_bjdata(InputType&& i,
  20760. const bool strict = true,
  20761. const bool allow_exceptions = true)
  20762. {
  20763. basic_json result;
  20764. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20765. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20766. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20767. return res ? result : basic_json(value_t::discarded);
  20768. }
  20769. /// @brief create a JSON value from an input in BJData format
  20770. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20771. template<typename IteratorType>
  20772. JSON_HEDLEY_WARN_UNUSED_RESULT
  20773. static basic_json from_bjdata(IteratorType first, IteratorType last,
  20774. const bool strict = true,
  20775. const bool allow_exceptions = true)
  20776. {
  20777. basic_json result;
  20778. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20779. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20780. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20781. return res ? result : basic_json(value_t::discarded);
  20782. }
  20783. /// @brief create a JSON value from an input in BSON format
  20784. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20785. template<typename InputType>
  20786. JSON_HEDLEY_WARN_UNUSED_RESULT
  20787. static basic_json from_bson(InputType&& i,
  20788. const bool strict = true,
  20789. const bool allow_exceptions = true)
  20790. {
  20791. basic_json result;
  20792. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20793. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20794. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20795. return res ? result : basic_json(value_t::discarded);
  20796. }
  20797. /// @brief create a JSON value from an input in BSON format
  20798. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20799. template<typename IteratorType>
  20800. JSON_HEDLEY_WARN_UNUSED_RESULT
  20801. static basic_json from_bson(IteratorType first, IteratorType last,
  20802. const bool strict = true,
  20803. const bool allow_exceptions = true)
  20804. {
  20805. basic_json result;
  20806. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20807. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20808. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20809. return res ? result : basic_json(value_t::discarded);
  20810. }
  20811. template<typename T>
  20812. JSON_HEDLEY_WARN_UNUSED_RESULT
  20813. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20814. static basic_json from_bson(const T* ptr, std::size_t len,
  20815. const bool strict = true,
  20816. const bool allow_exceptions = true)
  20817. {
  20818. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  20819. }
  20820. JSON_HEDLEY_WARN_UNUSED_RESULT
  20821. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20822. static basic_json from_bson(detail::span_input_adapter&& i,
  20823. const bool strict = true,
  20824. const bool allow_exceptions = true)
  20825. {
  20826. basic_json result;
  20827. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20828. auto ia = i.get();
  20829. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20830. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20831. return res ? result : basic_json(value_t::discarded);
  20832. }
  20833. /// @}
  20834. //////////////////////////
  20835. // JSON Pointer support //
  20836. //////////////////////////
  20837. /// @name JSON Pointer functions
  20838. /// @{
  20839. /// @brief access specified element via JSON Pointer
  20840. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20841. reference operator[](const json_pointer& ptr)
  20842. {
  20843. return ptr.get_unchecked(this);
  20844. }
  20845. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20846. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20847. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20848. {
  20849. return ptr.get_unchecked(this);
  20850. }
  20851. /// @brief access specified element via JSON Pointer
  20852. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20853. const_reference operator[](const json_pointer& ptr) const
  20854. {
  20855. return ptr.get_unchecked(this);
  20856. }
  20857. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20858. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20859. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20860. {
  20861. return ptr.get_unchecked(this);
  20862. }
  20863. /// @brief access specified element via JSON Pointer
  20864. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20865. reference at(const json_pointer& ptr)
  20866. {
  20867. return ptr.get_checked(this);
  20868. }
  20869. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20870. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20871. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20872. {
  20873. return ptr.get_checked(this);
  20874. }
  20875. /// @brief access specified element via JSON Pointer
  20876. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20877. const_reference at(const json_pointer& ptr) const
  20878. {
  20879. return ptr.get_checked(this);
  20880. }
  20881. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20882. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20883. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20884. {
  20885. return ptr.get_checked(this);
  20886. }
  20887. /// @brief return flattened JSON value
  20888. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  20889. basic_json flatten() const
  20890. {
  20891. basic_json result(value_t::object);
  20892. json_pointer::flatten("", *this, result);
  20893. return result;
  20894. }
  20895. /// @brief unflatten a previously flattened JSON value
  20896. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  20897. basic_json unflatten() const
  20898. {
  20899. return json_pointer::unflatten(*this);
  20900. }
  20901. /// @}
  20902. //////////////////////////
  20903. // JSON Patch functions //
  20904. //////////////////////////
  20905. /// @name JSON Patch functions
  20906. /// @{
  20907. /// @brief applies a JSON patch in-place without copying the object
  20908. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20909. void patch_inplace(const basic_json& json_patch)
  20910. {
  20911. basic_json& result = *this;
  20912. // the valid JSON Patch operations
  20913. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  20914. const auto get_op = [](const std::string & op)
  20915. {
  20916. if (op == "add")
  20917. {
  20918. return patch_operations::add;
  20919. }
  20920. if (op == "remove")
  20921. {
  20922. return patch_operations::remove;
  20923. }
  20924. if (op == "replace")
  20925. {
  20926. return patch_operations::replace;
  20927. }
  20928. if (op == "move")
  20929. {
  20930. return patch_operations::move;
  20931. }
  20932. if (op == "copy")
  20933. {
  20934. return patch_operations::copy;
  20935. }
  20936. if (op == "test")
  20937. {
  20938. return patch_operations::test;
  20939. }
  20940. return patch_operations::invalid;
  20941. };
  20942. // wrapper for "add" operation; add value at ptr
  20943. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  20944. {
  20945. // adding to the root of the target document means replacing it
  20946. if (ptr.empty())
  20947. {
  20948. result = val;
  20949. return;
  20950. }
  20951. // make sure the top element of the pointer exists
  20952. json_pointer const top_pointer = ptr.top();
  20953. if (top_pointer != ptr)
  20954. {
  20955. result.at(top_pointer);
  20956. }
  20957. // get reference to parent of JSON pointer ptr
  20958. const auto last_path = ptr.back();
  20959. ptr.pop_back();
  20960. // parent must exist when performing patch add per RFC6902 specs
  20961. basic_json& parent = result.at(ptr);
  20962. switch (parent.m_data.m_type)
  20963. {
  20964. case value_t::null:
  20965. case value_t::object:
  20966. {
  20967. // use operator[] to add value
  20968. parent[last_path] = val;
  20969. break;
  20970. }
  20971. case value_t::array:
  20972. {
  20973. if (last_path == "-")
  20974. {
  20975. // special case: append to back
  20976. parent.push_back(val);
  20977. }
  20978. else
  20979. {
  20980. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  20981. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  20982. {
  20983. // avoid undefined behavior
  20984. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  20985. }
  20986. // default case: insert add offset
  20987. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  20988. }
  20989. break;
  20990. }
  20991. // if there exists a parent it cannot be primitive
  20992. case value_t::string: // LCOV_EXCL_LINE
  20993. case value_t::boolean: // LCOV_EXCL_LINE
  20994. case value_t::number_integer: // LCOV_EXCL_LINE
  20995. case value_t::number_unsigned: // LCOV_EXCL_LINE
  20996. case value_t::number_float: // LCOV_EXCL_LINE
  20997. case value_t::binary: // LCOV_EXCL_LINE
  20998. case value_t::discarded: // LCOV_EXCL_LINE
  20999. default: // LCOV_EXCL_LINE
  21000. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  21001. }
  21002. };
  21003. // wrapper for "remove" operation; remove value at ptr
  21004. const auto operation_remove = [this, & result](json_pointer & ptr)
  21005. {
  21006. // get reference to parent of JSON pointer ptr
  21007. const auto last_path = ptr.back();
  21008. ptr.pop_back();
  21009. basic_json& parent = result.at(ptr);
  21010. // remove child
  21011. if (parent.is_object())
  21012. {
  21013. // perform range check
  21014. auto it = parent.find(last_path);
  21015. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  21016. {
  21017. parent.erase(it);
  21018. }
  21019. else
  21020. {
  21021. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  21022. }
  21023. }
  21024. else if (parent.is_array())
  21025. {
  21026. // note erase performs range check
  21027. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  21028. }
  21029. };
  21030. // type check: top level value must be an array
  21031. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  21032. {
  21033. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  21034. }
  21035. // iterate and apply the operations
  21036. for (const auto& val : json_patch)
  21037. {
  21038. // wrapper to get a value for an operation
  21039. const auto get_value = [&val](const std::string & op,
  21040. const std::string & member,
  21041. bool string_type) -> basic_json &
  21042. {
  21043. // find value
  21044. auto it = val.m_data.m_value.object->find(member);
  21045. // context-sensitive error message
  21046. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\'');
  21047. // check if desired value is present
  21048. if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
  21049. {
  21050. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21051. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  21052. }
  21053. // check if result is of type string
  21054. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  21055. {
  21056. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  21057. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  21058. }
  21059. // no error: return value
  21060. return it->second;
  21061. };
  21062. // type check: every element of the array must be an object
  21063. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  21064. {
  21065. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  21066. }
  21067. // collect mandatory members
  21068. const auto op = get_value("op", "op", true).template get<std::string>();
  21069. const auto path = get_value(op, "path", true).template get<std::string>();
  21070. json_pointer ptr(path);
  21071. switch (get_op(op))
  21072. {
  21073. case patch_operations::add:
  21074. {
  21075. operation_add(ptr, get_value("add", "value", false));
  21076. break;
  21077. }
  21078. case patch_operations::remove:
  21079. {
  21080. operation_remove(ptr);
  21081. break;
  21082. }
  21083. case patch_operations::replace:
  21084. {
  21085. // the "path" location must exist - use at()
  21086. result.at(ptr) = get_value("replace", "value", false);
  21087. break;
  21088. }
  21089. case patch_operations::move:
  21090. {
  21091. const auto from_path = get_value("move", "from", true).template get<std::string>();
  21092. json_pointer from_ptr(from_path);
  21093. // the "from" location must exist - use at()
  21094. basic_json const v = result.at(from_ptr);
  21095. // The move operation is functionally identical to a
  21096. // "remove" operation on the "from" location, followed
  21097. // immediately by an "add" operation at the target
  21098. // location with the value that was just removed.
  21099. operation_remove(from_ptr);
  21100. operation_add(ptr, v);
  21101. break;
  21102. }
  21103. case patch_operations::copy:
  21104. {
  21105. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  21106. const json_pointer from_ptr(from_path);
  21107. // the "from" location must exist - use at()
  21108. basic_json const v = result.at(from_ptr);
  21109. // The copy is functionally identical to an "add"
  21110. // operation at the target location using the value
  21111. // specified in the "from" member.
  21112. operation_add(ptr, v);
  21113. break;
  21114. }
  21115. case patch_operations::test:
  21116. {
  21117. bool success = false;
  21118. JSON_TRY
  21119. {
  21120. // check if "value" matches the one at "path"
  21121. // the "path" location must exist - use at()
  21122. success = (result.at(ptr) == get_value("test", "value", false));
  21123. }
  21124. JSON_INTERNAL_CATCH (out_of_range&)
  21125. {
  21126. // ignore out of range errors: success remains false
  21127. }
  21128. // throw an exception if test fails
  21129. if (JSON_HEDLEY_UNLIKELY(!success))
  21130. {
  21131. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  21132. }
  21133. break;
  21134. }
  21135. case patch_operations::invalid:
  21136. default:
  21137. {
  21138. // op must be "add", "remove", "replace", "move", "copy", or
  21139. // "test"
  21140. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  21141. }
  21142. }
  21143. }
  21144. }
  21145. /// @brief applies a JSON patch to a copy of the current object
  21146. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21147. basic_json patch(const basic_json& json_patch) const
  21148. {
  21149. basic_json result = *this;
  21150. result.patch_inplace(json_patch);
  21151. return result;
  21152. }
  21153. /// @brief creates a diff as a JSON patch
  21154. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  21155. JSON_HEDLEY_WARN_UNUSED_RESULT
  21156. static basic_json diff(const basic_json& source, const basic_json& target,
  21157. const std::string& path = "")
  21158. {
  21159. // the patch
  21160. basic_json result(value_t::array);
  21161. // if the values are the same, return empty patch
  21162. if (source == target)
  21163. {
  21164. return result;
  21165. }
  21166. if (source.type() != target.type())
  21167. {
  21168. // different types: replace value
  21169. result.push_back(
  21170. {
  21171. {"op", "replace"}, {"path", path}, {"value", target}
  21172. });
  21173. return result;
  21174. }
  21175. switch (source.type())
  21176. {
  21177. case value_t::array:
  21178. {
  21179. // first pass: traverse common elements
  21180. std::size_t i = 0;
  21181. while (i < source.size() && i < target.size())
  21182. {
  21183. // recursive call to compare array values at index i
  21184. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  21185. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21186. ++i;
  21187. }
  21188. // We now reached the end of at least one array
  21189. // in a second pass, traverse the remaining elements
  21190. // remove my remaining elements
  21191. const auto end_index = static_cast<difference_type>(result.size());
  21192. while (i < source.size())
  21193. {
  21194. // add operations in reverse order to avoid invalid
  21195. // indices
  21196. result.insert(result.begin() + end_index, object(
  21197. {
  21198. {"op", "remove"},
  21199. {"path", detail::concat(path, '/', std::to_string(i))}
  21200. }));
  21201. ++i;
  21202. }
  21203. // add other remaining elements
  21204. while (i < target.size())
  21205. {
  21206. result.push_back(
  21207. {
  21208. {"op", "add"},
  21209. {"path", detail::concat(path, "/-")},
  21210. {"value", target[i]}
  21211. });
  21212. ++i;
  21213. }
  21214. break;
  21215. }
  21216. case value_t::object:
  21217. {
  21218. // first pass: traverse this object's elements
  21219. for (auto it = source.cbegin(); it != source.cend(); ++it)
  21220. {
  21221. // escape the key name to be used in a JSON patch
  21222. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21223. if (target.find(it.key()) != target.end())
  21224. {
  21225. // recursive call to compare object values at key it
  21226. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  21227. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21228. }
  21229. else
  21230. {
  21231. // found a key that is not in o -> remove it
  21232. result.push_back(object(
  21233. {
  21234. {"op", "remove"}, {"path", path_key}
  21235. }));
  21236. }
  21237. }
  21238. // second pass: traverse other object's elements
  21239. for (auto it = target.cbegin(); it != target.cend(); ++it)
  21240. {
  21241. if (source.find(it.key()) == source.end())
  21242. {
  21243. // found a key that is not in this -> add it
  21244. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21245. result.push_back(
  21246. {
  21247. {"op", "add"}, {"path", path_key},
  21248. {"value", it.value()}
  21249. });
  21250. }
  21251. }
  21252. break;
  21253. }
  21254. case value_t::null:
  21255. case value_t::string:
  21256. case value_t::boolean:
  21257. case value_t::number_integer:
  21258. case value_t::number_unsigned:
  21259. case value_t::number_float:
  21260. case value_t::binary:
  21261. case value_t::discarded:
  21262. default:
  21263. {
  21264. // both primitive type: replace value
  21265. result.push_back(
  21266. {
  21267. {"op", "replace"}, {"path", path}, {"value", target}
  21268. });
  21269. break;
  21270. }
  21271. }
  21272. return result;
  21273. }
  21274. /// @}
  21275. ////////////////////////////////
  21276. // JSON Merge Patch functions //
  21277. ////////////////////////////////
  21278. /// @name JSON Merge Patch functions
  21279. /// @{
  21280. /// @brief applies a JSON Merge Patch
  21281. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  21282. void merge_patch(const basic_json& apply_patch)
  21283. {
  21284. if (apply_patch.is_object())
  21285. {
  21286. if (!is_object())
  21287. {
  21288. *this = object();
  21289. }
  21290. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  21291. {
  21292. if (it.value().is_null())
  21293. {
  21294. erase(it.key());
  21295. }
  21296. else
  21297. {
  21298. operator[](it.key()).merge_patch(it.value());
  21299. }
  21300. }
  21301. }
  21302. else
  21303. {
  21304. *this = apply_patch;
  21305. }
  21306. }
  21307. /// @}
  21308. };
  21309. /// @brief user-defined to_string function for JSON values
  21310. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  21311. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21312. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  21313. {
  21314. return j.dump();
  21315. }
  21316. inline namespace literals
  21317. {
  21318. inline namespace json_literals
  21319. {
  21320. /// @brief user-defined string literal for JSON values
  21321. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  21322. JSON_HEDLEY_NON_NULL(1)
  21323. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21324. inline nlohmann::json operator ""_json(const char* s, std::size_t n)
  21325. #else
  21326. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  21327. #endif
  21328. {
  21329. return nlohmann::json::parse(s, s + n);
  21330. }
  21331. /// @brief user-defined string literal for JSON pointer
  21332. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  21333. JSON_HEDLEY_NON_NULL(1)
  21334. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21335. inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
  21336. #else
  21337. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  21338. #endif
  21339. {
  21340. return nlohmann::json::json_pointer(std::string(s, n));
  21341. }
  21342. } // namespace json_literals
  21343. } // namespace literals
  21344. NLOHMANN_JSON_NAMESPACE_END
  21345. ///////////////////////
  21346. // nonmember support //
  21347. ///////////////////////
  21348. namespace std // NOLINT(cert-dcl58-cpp)
  21349. {
  21350. /// @brief hash value for JSON objects
  21351. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  21352. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21353. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  21354. {
  21355. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  21356. {
  21357. return nlohmann::detail::hash(j);
  21358. }
  21359. };
  21360. // specialization for std::less<value_t>
  21361. template<>
  21362. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  21363. {
  21364. /*!
  21365. @brief compare two value_t enum values
  21366. @since version 3.0.0
  21367. */
  21368. bool operator()(::nlohmann::detail::value_t lhs,
  21369. ::nlohmann::detail::value_t rhs) const noexcept
  21370. {
  21371. #if JSON_HAS_THREE_WAY_COMPARISON
  21372. return std::is_lt(lhs <=> rhs); // *NOPAD*
  21373. #else
  21374. return ::nlohmann::detail::operator<(lhs, rhs);
  21375. #endif
  21376. }
  21377. };
  21378. // C++20 prohibit function specialization in the std namespace.
  21379. #ifndef JSON_HAS_CPP_20
  21380. /// @brief exchanges the values of two JSON objects
  21381. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  21382. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21383. 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)
  21384. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  21385. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  21386. {
  21387. j1.swap(j2);
  21388. }
  21389. #endif
  21390. } // namespace std
  21391. #if JSON_USE_GLOBAL_UDLS
  21392. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21393. using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21394. using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21395. #else
  21396. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21397. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21398. #endif
  21399. #endif
  21400. // #include <nlohmann/detail/macro_unscope.hpp>
  21401. // __ _____ _____ _____
  21402. // __| | __| | | | JSON for Modern C++
  21403. // | | |__ | | | | | | version 3.11.3
  21404. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21405. //
  21406. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  21407. // SPDX-License-Identifier: MIT
  21408. // restore clang diagnostic settings
  21409. #if defined(__clang__)
  21410. #pragma clang diagnostic pop
  21411. #endif
  21412. // clean up
  21413. #undef JSON_ASSERT
  21414. #undef JSON_INTERNAL_CATCH
  21415. #undef JSON_THROW
  21416. #undef JSON_PRIVATE_UNLESS_TESTED
  21417. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21418. #undef NLOHMANN_BASIC_JSON_TPL
  21419. #undef JSON_EXPLICIT
  21420. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  21421. #undef JSON_INLINE_VARIABLE
  21422. #undef JSON_NO_UNIQUE_ADDRESS
  21423. #undef JSON_DISABLE_ENUM_SERIALIZATION
  21424. #undef JSON_USE_GLOBAL_UDLS
  21425. #ifndef JSON_TEST_KEEP_MACROS
  21426. #undef JSON_CATCH
  21427. #undef JSON_TRY
  21428. #undef JSON_HAS_CPP_11
  21429. #undef JSON_HAS_CPP_14
  21430. #undef JSON_HAS_CPP_17
  21431. #undef JSON_HAS_CPP_20
  21432. #undef JSON_HAS_FILESYSTEM
  21433. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  21434. #undef JSON_HAS_THREE_WAY_COMPARISON
  21435. #undef JSON_HAS_RANGES
  21436. #undef JSON_HAS_STATIC_RTTI
  21437. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  21438. #endif
  21439. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  21440. // __ _____ _____ _____
  21441. // __| | __| | | | JSON for Modern C++
  21442. // | | |__ | | | | | | version 3.11.3
  21443. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21444. //
  21445. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  21446. // SPDX-License-Identifier: MIT
  21447. #undef JSON_HEDLEY_ALWAYS_INLINE
  21448. #undef JSON_HEDLEY_ARM_VERSION
  21449. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  21450. #undef JSON_HEDLEY_ARRAY_PARAM
  21451. #undef JSON_HEDLEY_ASSUME
  21452. #undef JSON_HEDLEY_BEGIN_C_DECLS
  21453. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  21454. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  21455. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  21456. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  21457. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  21458. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  21459. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  21460. #undef JSON_HEDLEY_COMPCERT_VERSION
  21461. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  21462. #undef JSON_HEDLEY_CONCAT
  21463. #undef JSON_HEDLEY_CONCAT3
  21464. #undef JSON_HEDLEY_CONCAT3_EX
  21465. #undef JSON_HEDLEY_CONCAT_EX
  21466. #undef JSON_HEDLEY_CONST
  21467. #undef JSON_HEDLEY_CONSTEXPR
  21468. #undef JSON_HEDLEY_CONST_CAST
  21469. #undef JSON_HEDLEY_CPP_CAST
  21470. #undef JSON_HEDLEY_CRAY_VERSION
  21471. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  21472. #undef JSON_HEDLEY_C_DECL
  21473. #undef JSON_HEDLEY_DEPRECATED
  21474. #undef JSON_HEDLEY_DEPRECATED_FOR
  21475. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  21476. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  21477. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  21478. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  21479. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  21480. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  21481. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  21482. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  21483. #undef JSON_HEDLEY_DMC_VERSION
  21484. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  21485. #undef JSON_HEDLEY_EMPTY_BASES
  21486. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  21487. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  21488. #undef JSON_HEDLEY_END_C_DECLS
  21489. #undef JSON_HEDLEY_FLAGS
  21490. #undef JSON_HEDLEY_FLAGS_CAST
  21491. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  21492. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  21493. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  21494. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  21495. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  21496. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  21497. #undef JSON_HEDLEY_GCC_HAS_WARNING
  21498. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  21499. #undef JSON_HEDLEY_GCC_VERSION
  21500. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  21501. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  21502. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  21503. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  21504. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  21505. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  21506. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  21507. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  21508. #undef JSON_HEDLEY_GNUC_VERSION
  21509. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  21510. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  21511. #undef JSON_HEDLEY_HAS_BUILTIN
  21512. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  21513. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  21514. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  21515. #undef JSON_HEDLEY_HAS_EXTENSION
  21516. #undef JSON_HEDLEY_HAS_FEATURE
  21517. #undef JSON_HEDLEY_HAS_WARNING
  21518. #undef JSON_HEDLEY_IAR_VERSION
  21519. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  21520. #undef JSON_HEDLEY_IBM_VERSION
  21521. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  21522. #undef JSON_HEDLEY_IMPORT
  21523. #undef JSON_HEDLEY_INLINE
  21524. #undef JSON_HEDLEY_INTEL_CL_VERSION
  21525. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  21526. #undef JSON_HEDLEY_INTEL_VERSION
  21527. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  21528. #undef JSON_HEDLEY_IS_CONSTANT
  21529. #undef JSON_HEDLEY_IS_CONSTEXPR_
  21530. #undef JSON_HEDLEY_LIKELY
  21531. #undef JSON_HEDLEY_MALLOC
  21532. #undef JSON_HEDLEY_MCST_LCC_VERSION
  21533. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  21534. #undef JSON_HEDLEY_MESSAGE
  21535. #undef JSON_HEDLEY_MSVC_VERSION
  21536. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  21537. #undef JSON_HEDLEY_NEVER_INLINE
  21538. #undef JSON_HEDLEY_NON_NULL
  21539. #undef JSON_HEDLEY_NO_ESCAPE
  21540. #undef JSON_HEDLEY_NO_RETURN
  21541. #undef JSON_HEDLEY_NO_THROW
  21542. #undef JSON_HEDLEY_NULL
  21543. #undef JSON_HEDLEY_PELLES_VERSION
  21544. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  21545. #undef JSON_HEDLEY_PGI_VERSION
  21546. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  21547. #undef JSON_HEDLEY_PREDICT
  21548. #undef JSON_HEDLEY_PRINTF_FORMAT
  21549. #undef JSON_HEDLEY_PRIVATE
  21550. #undef JSON_HEDLEY_PUBLIC
  21551. #undef JSON_HEDLEY_PURE
  21552. #undef JSON_HEDLEY_REINTERPRET_CAST
  21553. #undef JSON_HEDLEY_REQUIRE
  21554. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  21555. #undef JSON_HEDLEY_REQUIRE_MSG
  21556. #undef JSON_HEDLEY_RESTRICT
  21557. #undef JSON_HEDLEY_RETURNS_NON_NULL
  21558. #undef JSON_HEDLEY_SENTINEL
  21559. #undef JSON_HEDLEY_STATIC_ASSERT
  21560. #undef JSON_HEDLEY_STATIC_CAST
  21561. #undef JSON_HEDLEY_STRINGIFY
  21562. #undef JSON_HEDLEY_STRINGIFY_EX
  21563. #undef JSON_HEDLEY_SUNPRO_VERSION
  21564. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  21565. #undef JSON_HEDLEY_TINYC_VERSION
  21566. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  21567. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  21568. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  21569. #undef JSON_HEDLEY_TI_CL2000_VERSION
  21570. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  21571. #undef JSON_HEDLEY_TI_CL430_VERSION
  21572. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  21573. #undef JSON_HEDLEY_TI_CL6X_VERSION
  21574. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  21575. #undef JSON_HEDLEY_TI_CL7X_VERSION
  21576. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  21577. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  21578. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  21579. #undef JSON_HEDLEY_TI_VERSION
  21580. #undef JSON_HEDLEY_TI_VERSION_CHECK
  21581. #undef JSON_HEDLEY_UNAVAILABLE
  21582. #undef JSON_HEDLEY_UNLIKELY
  21583. #undef JSON_HEDLEY_UNPREDICTABLE
  21584. #undef JSON_HEDLEY_UNREACHABLE
  21585. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  21586. #undef JSON_HEDLEY_VERSION
  21587. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  21588. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  21589. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  21590. #undef JSON_HEDLEY_VERSION_ENCODE
  21591. #undef JSON_HEDLEY_WARNING
  21592. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  21593. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  21594. #undef JSON_HEDLEY_FALL_THROUGH
  21595. #endif // INCLUDE_NLOHMANN_JSON_HPP_